diff --git a/admin/client_prices.php b/admin/client_prices.php index 6a3937c..db9a094 100644 --- a/admin/client_prices.php +++ b/admin/client_prices.php @@ -6,6 +6,8 @@ $pdo = db(); $message = ''; // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['client_id'], $_POST['product_id'])) { + $action = $_POST['form_action'] ?? 'add'; + $clientId = $_POST['client_id']; $productId = $_POST['product_id']; $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) { - // Upsert logic - $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) { + if ($action === 'update') { $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]); $message = '
Cena została zaktualizowana.
'; - } 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 = '
Nowa cena została dodana.
'; + } else { // 'add' action + $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) { + $message = '
Cena dla tego klienta i produktu już istnieje. Użyj opcji edycji w tabeli poniżej.
'; + } 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 = '
Nowa cena została dodana.
'; + } } } else { $message = '
Wszystkie pola są wymagane, a ceny muszą być prawidłowymi liczbami.
'; @@ -63,10 +68,11 @@ $page_title = "Cennik indywidualny";
-

Dodaj/Edytuj cenę

+

Dodaj nową cenę

+
@@ -95,7 +101,7 @@ $page_title = "Cennik indywidualny";
- +
@@ -228,7 +234,8 @@ $page_title = "Cennik indywidualny"; client_id: clientId, product_id: productId, price_net: priceNet, - price_gross: priceGross + price_gross: priceGross, + form_action: 'update' }; for (const key in fields) { diff --git a/debug_price.log b/debug_price.log index 9ab506e..497c882 100644 --- a/debug_price.log +++ b/debug_price.log @@ -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 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 +---