Edycja cen indywidualnych

This commit is contained in:
Flatlogic Bot 2025-12-28 20:36:18 +00:00
parent d11a1a5c0d
commit fc35395c51
2 changed files with 70 additions and 13 deletions

View File

@ -6,6 +6,8 @@ $pdo = db();
$message = ''; $message = '';
// Handle form submission // Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['client_id'], $_POST['product_id'])) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['client_id'], $_POST['product_id'])) {
$action = $_POST['form_action'] ?? 'add';
$clientId = $_POST['client_id']; $clientId = $_POST['client_id'];
$productId = $_POST['product_id']; $productId = $_POST['product_id'];
$priceNet = isset($_POST['price_net']) && is_numeric($_POST['price_net']) ? (float)$_POST['price_net'] : null; $priceNet = isset($_POST['price_net']) && is_numeric($_POST['price_net']) ? (float)$_POST['price_net'] : null;
@ -19,19 +21,22 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['client_id'], $_POST['
} }
if (!empty($clientId) && !empty($productId) && $priceNet !== null && $priceGross !== null) { if (!empty($clientId) && !empty($productId) && $priceNet !== null && $priceGross !== null) {
// Upsert logic if ($action === 'update') {
$stmt = $pdo->prepare("SELECT COUNT(*) FROM client_prices WHERE client_id = :client_id AND product_id = :product_id");
$stmt->execute(['client_id' => $clientId, 'product_id' => $productId]);
$exists = $stmt->fetchColumn() > 0;
if ($exists) {
$stmt = $pdo->prepare("UPDATE client_prices SET price_net = :price_net, price_gross = :price_gross WHERE client_id = :client_id AND product_id = :product_id"); $stmt = $pdo->prepare("UPDATE client_prices SET price_net = :price_net, price_gross = :price_gross WHERE client_id = :client_id AND product_id = :product_id");
$stmt->execute(['price_net' => $priceNet, 'price_gross' => $priceGross, 'client_id' => $clientId, 'product_id' => $productId]); $stmt->execute(['price_net' => $priceNet, 'price_gross' => $priceGross, 'client_id' => $clientId, 'product_id' => $productId]);
$message = '<div class="alert alert-success">Cena została zaktualizowana.</div>'; $message = '<div class="alert alert-success">Cena została zaktualizowana.</div>';
} else { } else { // 'add' action
$stmt = $pdo->prepare("INSERT INTO client_prices (client_id, product_id, price_net, price_gross) VALUES (:client_id, :product_id, :price_net, :price_gross)"); $stmt = $pdo->prepare("SELECT COUNT(*) FROM client_prices WHERE client_id = :client_id AND product_id = :product_id");
$stmt->execute(['client_id' => $clientId, 'product_id' => $productId, 'price_net' => $priceNet, 'price_gross' => $priceGross]); $stmt->execute(['client_id' => $clientId, 'product_id' => $productId]);
$message = '<div class="alert alert-success">Nowa cena została dodana.</div>'; $exists = $stmt->fetchColumn() > 0;
if ($exists) {
$message = '<div class="alert alert-danger">Cena dla tego klienta i produktu już istnieje. Użyj opcji edycji w tabeli poniżej.</div>';
} else {
$stmt = $pdo->prepare("INSERT INTO client_prices (client_id, product_id, price_net, price_gross) VALUES (:client_id, :product_id, :price_net, :price_gross)");
$stmt->execute(['client_id' => $clientId, 'product_id' => $productId, 'price_net' => $priceNet, 'price_gross' => $priceGross]);
$message = '<div class="alert alert-success">Nowa cena została dodana.</div>';
}
} }
} else { } else {
$message = '<div class="alert alert-danger">Wszystkie pola są wymagane, a ceny muszą być prawidłowymi liczbami.</div>'; $message = '<div class="alert alert-danger">Wszystkie pola są wymagane, a ceny muszą być prawidłowymi liczbami.</div>';
@ -63,10 +68,11 @@ $page_title = "Cennik indywidualny";
<div class="card mb-4"> <div class="card mb-4">
<div class="card-header"> <div class="card-header">
<h3>Dodaj/Edytuj cenę</h3> <h3>Dodaj nową cenę</h3>
</div> </div>
<div class="card-body"> <div class="card-body">
<form method="POST" action="client_prices.php"> <form method="POST" action="client_prices.php">
<input type="hidden" name="form_action" value="add">
<div class="row"> <div class="row">
<div class="col-md-3 mb-3"> <div class="col-md-3 mb-3">
<label for="client_id" class="form-label">Klient:</label> <label for="client_id" class="form-label">Klient:</label>
@ -95,7 +101,7 @@ $page_title = "Cennik indywidualny";
<input type="number" step="0.01" name="price_gross" id="price_gross" class="form-control"> <input type="number" step="0.01" name="price_gross" id="price_gross" class="form-control">
</div> </div>
<div class="col-md-2 d-flex align-items-end"> <div class="col-md-2 d-flex align-items-end">
<button type="submit" class="btn btn-primary w-100">Zapisz</button> <button type="submit" class="btn btn-primary w-100">Dodaj</button>
</div> </div>
</div> </div>
</form> </form>
@ -228,7 +234,8 @@ $page_title = "Cennik indywidualny";
client_id: clientId, client_id: clientId,
product_id: productId, product_id: productId,
price_net: priceNet, price_net: priceNet,
price_gross: priceGross price_gross: priceGross,
form_action: 'update'
}; };
for (const key in fields) { for (const key in fields) {

View File

@ -9160,3 +9160,53 @@ Product price query executed. Found: {"price_net":"233.20","price_gross":"286.84
Found product price. Net: 233.2, Gross: 286.84 Found product price. Net: 233.2, Gross: 286.84
FINAL: Returning Net: 233.2, Gross: 286.84 FINAL: Returning Net: 233.2, Gross: 286.84
--- ---
---
START getEffectivePrice for product 1, client 1
Client price query executed. Found: {"price_net":"813.01","price_gross":"1000.00"}
Found client price. Net: 813.01, Gross: 1000
FINAL: Returning Net: 813.01, Gross: 1000
---
---
START getEffectivePrice for product 2, client 1
Client price query executed. Found: {"price_net":"1056.91","price_gross":"1300.00"}
Found client price. Net: 1056.91, Gross: 1300
FINAL: Returning Net: 1056.91, Gross: 1300
---
---
START getEffectivePrice for product 3, client 1
Client price query executed. Found: {"price_net":"32.52","price_gross":"40.00"}
Found client price. Net: 32.52, Gross: 40
FINAL: Returning Net: 32.52, Gross: 40
---
---
START getEffectivePrice for product 4, client 1
Client price query executed. Found: No
Client price not found or not set, falling back to product price.
Product price query executed. Found: {"price_net":"9.95","price_gross":"12.24"}
Found product price. Net: 9.95, Gross: 12.24
FINAL: Returning Net: 9.95, Gross: 12.24
---
---
START getEffectivePrice for product 5, client 1
Client price query executed. Found: No
Client price not found or not set, falling back to product price.
Product price query executed. Found: {"price_net":"68.00","price_gross":"83.64"}
Found product price. Net: 68, Gross: 83.64
FINAL: Returning Net: 68, Gross: 83.64
---
---
START getEffectivePrice for product 6, client 1
Client price query executed. Found: No
Client price not found or not set, falling back to product price.
Product price query executed. Found: {"price_net":"171.60","price_gross":"211.07"}
Found product price. Net: 171.6, Gross: 211.07
FINAL: Returning Net: 171.6, Gross: 211.07
---
---
START getEffectivePrice for product 7, client 1
Client price query executed. Found: No
Client price not found or not set, falling back to product price.
Product price query executed. Found: {"price_net":"233.20","price_gross":"286.84"}
Found product price. Net: 233.2, Gross: 286.84
FINAL: Returning Net: 233.2, Gross: 286.84
---