diff --git a/source/add_order.php b/source/add_order.php index 513f17e..0bf23d0 100644 --- a/source/add_order.php +++ b/source/add_order.php @@ -42,7 +42,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { $message = "
Nie udało się zapisać zamówienia.
"; } } else { - $message = "
Wypełnij nazwę produktu i ilość.
"; + $message = "
Wypełnij nazwę produktu i podaj ilość większą od zera.
"; } } ?> diff --git a/source/ajax_add_row.php b/source/ajax_add_row.php index 4652ef5..fd4e00d 100644 --- a/source/ajax_add_row.php +++ b/source/ajax_add_row.php @@ -22,6 +22,9 @@ try { $price = (float)str_replace(',', '.', $data['price_per_unit'] ?? 0); $qty = (int)($data['quantity'] ?? 1); + if ($qty < 1) { + die(json_encode(['success' => false, 'error' => 'Ilość musi być większa od zera.'])); + } $stmt->execute([ trim($data['product_name']), diff --git a/source/ajax_edit.php b/source/ajax_edit.php index ae6c8b8..1ecc25f 100644 --- a/source/ajax_edit.php +++ b/source/ajax_edit.php @@ -45,6 +45,13 @@ try { $old_val = $stmt->fetchColumn(); if ((string)$old_val !== (string)$value) { + if ($field === 'quantity') { + $value = (int)$value; + if ($value < 1) { + die(json_encode(['success' => false, 'error' => 'Ilość musi być większa od zera.'])); + } + } + if ($field === 'price_per_unit') { $value = (float)str_replace(',', '.', $value); } diff --git a/source/edit_order.php b/source/edit_order.php index b0c4bdd..aa97be8 100644 --- a/source/edit_order.php +++ b/source/edit_order.php @@ -66,40 +66,44 @@ if ($_SERVER["REQUEST_METHOD"] == "POST" && !isset($_POST['archive_action']) && $delivery_address = trim($_POST['delivery_address'] ?? ''); $company = trim($_POST['company'] ?? ''); - $changes = []; - if (($order['company'] ?? '') !== $company) $changes[] = "Firma: [{$order['company']}] ➔ [$company]"; - if (($order['product_name'] ?? '') !== $product_name) $changes[] = "Produkt: [{$order['product_name']}] ➔ [$product_name]"; - if (($order['part_number'] ?? '') !== $part_number) $changes[] = "PN: [{$order['part_number']}] ➔ [$part_number]"; - if ((int)$order['quantity'] !== $quantity) $changes[] = "Ilość: [{$order['quantity']}] ➔ [$quantity]"; - if (($order['purchase_place'] ?? '') !== $purchase_place) $changes[] = "Sklep: [{$order['purchase_place']}] ➔ [$purchase_place]"; - if (($order['status'] ?? '') !== $status) $changes[] = "Status: [{$order['status']}] ➔ [$status]"; - if ((float)$order['price_per_unit'] !== $price) $changes[] = "Cena: [{$order['price_per_unit']}] ➔ [$price]"; - if (($order['delivery_date'] ?? '') !== $delivery_date) $changes[] = "Dostawa: [{$order['delivery_date']}] ➔ [$delivery_date]"; - if (($order['recipient'] ?? '') !== $recipient) $changes[] = "Odbiorca: [{$order['recipient']}] ➔ [$recipient]"; - if (($order['delivery_address'] ?? '') !== $delivery_address) $changes[] = "Adres: [{$order['delivery_address']}] ➔ [$delivery_address]"; - if (($order['notes'] ?? '') !== $notes) $changes[] = "Zaktualizowano notatki"; - - if (!empty($changes)) { - try { - $pdo->beginTransaction(); - - $update = $pdo->prepare("UPDATE " . DB_PREFIX . "orders SET - product_name=?, part_number=?, quantity=?, purchase_place=?, status=?, price_per_unit=?, delivery_date=?, notes=?, recipient=?, delivery_address=?, company=? - WHERE id=?"); - $update->execute([$product_name, $part_number, $quantity, $purchase_place, $status, $price, $delivery_date, $notes, $recipient, $delivery_address, $company, $id]); - - $action_text = "Zmieniono: " . implode(', ', $changes); - $hist = $pdo->prepare("INSERT INTO " . DB_PREFIX . "order_history (order_id, user_id, action) VALUES (?, ?, ?)"); - $hist->execute([$id, $_SESSION['user_id'], $action_text]); - - $pdo->commit(); - $message = "
Zmiany zostały zapisane!
"; - - $stmt->execute([$id]); - $order = $stmt->fetch(); - } catch (PDOException $e) { - $pdo->rollBack(); - $message = "
Błąd bazy: " . $e->getMessage() . "
"; + if ($quantity < 1) { + $message = "
Ilość musi być większa od zera.
"; + } else { + $changes = []; + if (($order['company'] ?? '') !== $company) $changes[] = "Firma: [{$order['company']}] ➔ [$company]"; + if (($order['product_name'] ?? '') !== $product_name) $changes[] = "Produkt: [{$order['product_name']}] ➔ [$product_name]"; + if (($order['part_number'] ?? '') !== $part_number) $changes[] = "PN: [{$order['part_number']}] ➔ [$part_number]"; + if ((int)$order['quantity'] !== $quantity) $changes[] = "Ilość: [{$order['quantity']}] ➔ [$quantity]"; + if (($order['purchase_place'] ?? '') !== $purchase_place) $changes[] = "Sklep: [{$order['purchase_place']}] ➔ [$purchase_place]"; + if (($order['status'] ?? '') !== $status) $changes[] = "Status: [{$order['status']}] ➔ [$status]"; + if ((float)$order['price_per_unit'] !== $price) $changes[] = "Cena: [{$order['price_per_unit']}] ➔ [$price]"; + if (($order['delivery_date'] ?? '') !== $delivery_date) $changes[] = "Dostawa: [{$order['delivery_date']}] ➔ [$delivery_date]"; + if (($order['recipient'] ?? '') !== $recipient) $changes[] = "Odbiorca: [{$order['recipient']}] ➔ [$recipient]"; + if (($order['delivery_address'] ?? '') !== $delivery_address) $changes[] = "Adres: [{$order['delivery_address']}] ➔ [$delivery_address]"; + if (($order['notes'] ?? '') !== $notes) $changes[] = "Zaktualizowano notatki"; + + if (!empty($changes)) { + try { + $pdo->beginTransaction(); + + $update = $pdo->prepare("UPDATE " . DB_PREFIX . "orders SET + product_name=?, part_number=?, quantity=?, purchase_place=?, status=?, price_per_unit=?, delivery_date=?, notes=?, recipient=?, delivery_address=?, company=? + WHERE id=?"); + $update->execute([$product_name, $part_number, $quantity, $purchase_place, $status, $price, $delivery_date, $notes, $recipient, $delivery_address, $company, $id]); + + $action_text = "Zmieniono: " . implode(', ', $changes); + $hist = $pdo->prepare("INSERT INTO " . DB_PREFIX . "order_history (order_id, user_id, action) VALUES (?, ?, ?)"); + $hist->execute([$id, $_SESSION['user_id'], $action_text]); + + $pdo->commit(); + $message = "
Zmiany zostały zapisane!
"; + + $stmt->execute([$id]); + $order = $stmt->fetch(); + } catch (PDOException $e) { + $pdo->rollBack(); + $message = "
Błąd bazy: " . $e->getMessage() . "
"; + } } } } diff --git a/source/index.php b/source/index.php index bc59f62..bf330d4 100644 --- a/source/index.php +++ b/source/index.php @@ -501,7 +501,9 @@ function getStatusClass($status) { `; } else if (fieldName === 'delivery_date') { inputHTML = ``; - } else if (fieldName === 'quantity' || fieldName === 'price_per_unit') { + } else if (fieldName === 'quantity') { + inputHTML = ``; + } else if (fieldName === 'price_per_unit') { inputHTML = ``; } else { inputHTML = ``; @@ -558,7 +560,7 @@ function getStatusClass($status) { Nowe - + Brak @@ -581,6 +583,7 @@ function getStatusClass($status) { const price = document.getElementById('add-price').value; if(!prod) { alert("Nazwa produktu jest obowiązkowa!"); document.getElementById('add-prod').focus(); return; } + if(!qty || Number(qty) < 1) { alert("Ilość musi być większa od zera!"); document.getElementById('add-qty').focus(); return; } try { let response = await fetch('ajax_add_row.php', {