diff --git a/api/inventory.php b/api/inventory.php new file mode 100644 index 0000000..8e0f531 --- /dev/null +++ b/api/inventory.php @@ -0,0 +1,32 @@ + 'Unauthorized']); + exit; +} + +$action = $_GET['action'] ?? ''; +$db = db(); + +if ($action === 'get_batches') { + $item_id = $_GET['item_id'] ?? 0; + if (!$item_id) { + echo json_encode([]); + exit; + } + + $stmt = $db->prepare("SELECT id, batch_number, quantity, expiry_date, cost_price FROM inventory_batches WHERE item_id = ? AND quantity > 0 ORDER BY expiry_date ASC"); + $stmt->execute([$item_id]); + $batches = $stmt->fetchAll(PDO::FETCH_ASSOC); + + echo json_encode($batches); + exit; +} + +echo json_encode(['error' => 'Invalid action']); diff --git a/db/migrations/20260321_create_inventory_module.sql b/db/migrations/20260321_create_inventory_module.sql new file mode 100644 index 0000000..3bb8073 --- /dev/null +++ b/db/migrations/20260321_create_inventory_module.sql @@ -0,0 +1,52 @@ +CREATE TABLE IF NOT EXISTS inventory_categories ( + id INT AUTO_INCREMENT PRIMARY KEY, + name_en VARCHAR(255) NOT NULL, + name_ar VARCHAR(255) NOT NULL, + description TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS inventory_items ( + id INT AUTO_INCREMENT PRIMARY KEY, + category_id INT, + name_en VARCHAR(255) NOT NULL, + name_ar VARCHAR(255) NOT NULL, + description TEXT, + sku VARCHAR(100) UNIQUE, + unit VARCHAR(50) DEFAULT 'piece', + min_level INT DEFAULT 10, + reorder_level INT DEFAULT 20, + is_active BOOLEAN DEFAULT TRUE, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (category_id) REFERENCES inventory_categories(id) ON DELETE SET NULL +); + +CREATE TABLE IF NOT EXISTS inventory_batches ( + id INT AUTO_INCREMENT PRIMARY KEY, + item_id INT NOT NULL, + batch_number VARCHAR(100), + expiry_date DATE, + quantity INT NOT NULL DEFAULT 0, + cost_price DECIMAL(10, 2) DEFAULT 0.00, + supplier_id INT, + received_date DATE, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (item_id) REFERENCES inventory_items(id) ON DELETE CASCADE, + FOREIGN KEY (supplier_id) REFERENCES suppliers(id) ON DELETE SET NULL +); + +CREATE TABLE IF NOT EXISTS inventory_transactions ( + id INT AUTO_INCREMENT PRIMARY KEY, + item_id INT NOT NULL, + batch_id INT, + transaction_type ENUM('in', 'out', 'adjustment') NOT NULL, + quantity INT NOT NULL, + reference_type VARCHAR(50), -- 'purchase', 'consumption', 'manual_adjustment' + reference_id INT, + user_id INT, + transaction_date DATETIME DEFAULT CURRENT_TIMESTAMP, + notes TEXT, + FOREIGN KEY (item_id) REFERENCES inventory_items(id) ON DELETE CASCADE, + FOREIGN KEY (batch_id) REFERENCES inventory_batches(id) ON DELETE SET NULL, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL +); diff --git a/includes/layout/header.php b/includes/layout/header.php index 022f96d..c6e3a7f 100644 --- a/includes/layout/header.php +++ b/includes/layout/header.php @@ -113,29 +113,31 @@ $site_favicon = !empty($site_settings['company_favicon']) ? $site_settings['comp