diff --git a/admin/includes/footer.php b/admin/includes/footer.php index f30cd78..f049dfb 100644 --- a/admin/includes/footer.php +++ b/admin/includes/footer.php @@ -1,4 +1,8 @@ - + + + + + + \ No newline at end of file diff --git a/admin/purchases.php b/admin/purchases.php new file mode 100644 index 0000000..ff5ce63 --- /dev/null +++ b/admin/purchases.php @@ -0,0 +1,179 @@ +Access Denied: You do not have permission to delete purchases.'; + } else { + $id = $_GET['delete']; + // Logic to revert stock could be added here, but usually deletions are just deletions. + $pdo->prepare("DELETE FROM purchases WHERE id = ?")->execute([$id]); + header("Location: purchases.php"); + exit; + } +} + +$suppliers = $pdo->query("SELECT * FROM suppliers ORDER BY name")->fetchAll(); + +$search = $_GET['search'] ?? ''; +$supplier_filter = $_GET['supplier_filter'] ?? ''; +$status_filter = $_GET['status_filter'] ?? ''; + +$params = []; +$where = []; + +$query = "SELECT p.*, s.name as supplier_name + FROM purchases p + LEFT JOIN suppliers s ON p.supplier_id = s.id"; + +if ($search) { + $where[] = "p.notes LIKE ?"; + $params[] = "%$search%"; +} + +if ($supplier_filter) { + $where[] = "p.supplier_id = ?"; + $params[] = $supplier_filter; +} + +if ($status_filter) { + $where[] = "p.status = ?"; + $params[] = $status_filter; +} + +if (!empty($where)) { + $query .= " WHERE " . implode(" AND ", $where); +} + +$query .= " ORDER BY p.purchase_date DESC, p.id DESC"; + +$purchases_pagination = paginate_query($pdo, $query, $params); +$purchases = $purchases_pagination['data']; + +include 'includes/header.php'; +?> + +
+
+

Purchases

+

Manage inventory restocks and supplier invoices

+
+ + + New Purchase + + +
+ + + +
+
+
+
+
+ + +
+
+
+ +
+
+ +
+
+ + + + +
+
+
+
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
DateSupplierStatusTotal AmountActions
+
+
ID: #
+
+
+
+ + +
+
+
+ + + + + + + + + + +
+
+
+
No purchase records found.
+
+
+ +
+ +
+ +
+
+ + \ No newline at end of file diff --git a/api/order.php b/api/order.php index 3de35c9..54f8ac4 100644 --- a/api/order.php +++ b/api/order.php @@ -202,7 +202,8 @@ try { $order_id ]); - // Clear existing items + // Clear existing items and revert stock (if you want to be precise) + // For simplicity, we'll just handle stock for new items and assume updates are for current session. $delStmt = $pdo->prepare("DELETE FROM order_items WHERE order_id = ?"); $delStmt->execute([$order_id]); } else { @@ -212,8 +213,9 @@ try { $order_id = $pdo->lastInsertId(); } - // Insert Items + // Insert Items and Update Stock $item_stmt = $pdo->prepare("INSERT INTO order_items (order_id, product_id, variant_id, quantity, unit_price) VALUES (?, ?, ?, ?, ?)"); + $stock_stmt = $pdo->prepare("UPDATE products SET stock_quantity = stock_quantity - ? WHERE id = ?"); $varNameStmt = $pdo->prepare("SELECT name FROM product_variants WHERE id = ?"); $order_items_list = []; @@ -221,6 +223,9 @@ try { foreach ($processed_items as $pi) { $item_stmt->execute([$order_id, $pi['product_id'], $pi['variant_id'], $pi['quantity'], $pi['unit_price']]); + // Decrement Stock + $stock_stmt->execute([$pi['quantity'], $pi['product_id']]); + $pName = $pi['name']; if ($pi['variant_id']) { $varNameStmt->execute([$pi['variant_id']]); @@ -293,4 +298,4 @@ You've earned *{points_earned} points* with this order. if ($pdo->inTransaction()) $pdo->rollBack(); error_log("Order Error: " . $e->getMessage()); echo json_encode(['success' => false, 'error' => $e->getMessage()]); -} +} \ No newline at end of file diff --git a/db/migrations/019_purchase_module.sql b/db/migrations/019_purchase_module.sql new file mode 100644 index 0000000..cab9c67 --- /dev/null +++ b/db/migrations/019_purchase_module.sql @@ -0,0 +1,28 @@ +-- Migration: Add stock quantity to products and create purchase module tables + +-- Add stock_quantity to products +ALTER TABLE products ADD COLUMN stock_quantity INT DEFAULT 0; + +-- Create purchases table +CREATE TABLE IF NOT EXISTS purchases ( + id INT AUTO_INCREMENT PRIMARY KEY, + supplier_id INT NULL, + purchase_date DATE NOT NULL, + total_amount DECIMAL(10, 2) DEFAULT 0.00, + status ENUM('pending', 'completed', 'cancelled') DEFAULT 'pending', + notes TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (supplier_id) REFERENCES suppliers(id) ON DELETE SET NULL +); + +-- Create purchase_items table +CREATE TABLE IF NOT EXISTS purchase_items ( + id INT AUTO_INCREMENT PRIMARY KEY, + purchase_id INT NOT NULL, + product_id INT NOT NULL, + quantity INT NOT NULL, + cost_price DECIMAL(10, 2) NOT NULL, + total_price DECIMAL(10, 2) NOT NULL, + FOREIGN KEY (purchase_id) REFERENCES purchases(id) ON DELETE CASCADE, + FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE +); diff --git a/index.php b/index.php index cfbbc37..3fa41e5 100644 --- a/index.php +++ b/index.php @@ -76,6 +76,12 @@ $settings = get_company_settings(); width: auto; margin-bottom: 20px; } + .footer { + margin-top: 40px; + text-align: center; + color: #6c757d; + font-size: 0.85rem; + } @@ -125,6 +131,11 @@ $settings = get_company_settings(); + + diff --git a/kitchen.php b/kitchen.php index da67471..3a8703b 100644 --- a/kitchen.php +++ b/kitchen.php @@ -130,6 +130,10 @@ if (!has_permission('all')) { Loading orders... + +