عذراً، ليس لديك صلاحية الوصول لهذه الصفحة.'; require_once __DIR__ . '/includes/footer.php'; exit; } $success = ''; $error = ''; // Handle Actions if ($_SERVER['REQUEST_METHOD'] === 'POST') { $action = $_POST['action'] ?? ''; $id = $_POST['id'] ?? 0; $name = $_POST['name'] ?? ''; $sku = $_POST['sku'] ?? ''; $category_id = $_POST['category_id'] ?? null; $min_quantity = $_POST['min_quantity'] ?? 0; $unit = $_POST['unit'] ?? 'piece'; $description = $_POST['description'] ?? ''; if ($action === 'add' && canAdd('stock_items')) { if ($name) { $stmt = db()->prepare("INSERT INTO stock_items (name, sku, category_id, min_quantity, unit, description) VALUES (?, ?, ?, ?, ?, ?)"); if ($stmt->execute([$name, $sku, $category_id, $min_quantity, $unit, $description])) { $success = 'تم إضافة الصنف بنجاح'; } else { $error = 'حدث خطأ أثناء الإضافة'; } } } elseif ($action === 'edit' && canEdit('stock_items')) { if ($name && $id) { $stmt = db()->prepare("UPDATE stock_items SET name=?, sku=?, category_id=?, min_quantity=?, unit=?, description=? WHERE id=?"); if ($stmt->execute([$name, $sku, $category_id, $min_quantity, $unit, $description, $id])) { $success = 'تم تحديث الصنف بنجاح'; } else { $error = 'حدث خطأ أثناء التحديث'; } } } elseif ($action === 'delete' && canDelete('stock_items')) { if ($id) { $stmt = db()->prepare("DELETE FROM stock_items WHERE id=?"); if ($stmt->execute([$id])) { $success = 'تم حذف الصنف بنجاح'; } else { $error = 'لا يمكن حذف الصنف لوجود حركات مرتبطة به'; } } } } // Pagination & Search $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; if ($page < 1) $page = 1; $limit = 10; $offset = ($page - 1) * $limit; $where = "WHERE 1=1"; $params = []; if (isset($_GET['search']) && !empty($_GET['search'])) { $where .= " AND (i.name LIKE ? OR i.sku LIKE ?)"; $search = "%" . $_GET['search'] . "%"; $params = array_merge($params, [$search, $search]); } if (isset($_GET['category_id']) && !empty($_GET['category_id'])) { $where .= " AND i.category_id = ?"; $params[] = $_GET['category_id']; } // Count Total $countQuery = "SELECT COUNT(*) FROM stock_items i $where"; $countStmt = db()->prepare($countQuery); $countStmt->execute($params); $totalFiltered = $countStmt->fetchColumn(); // Fetch Items with Category Name and Total Quantity $query = " SELECT i.*, c.name as category_name, (SELECT SUM(quantity) FROM stock_quantities q WHERE q.item_id = i.id) as total_quantity FROM stock_items i LEFT JOIN stock_categories c ON i.category_id = c.id $where ORDER BY i.name ASC LIMIT $limit OFFSET $offset "; $stmt = db()->prepare($query); $stmt->execute($params); $items = $stmt->fetchAll(); // Fetch Categories for Dropdown $categories = db()->query("SELECT * FROM stock_categories ORDER BY name ASC")->fetchAll(); ?>
| اسم الصنف | الرمز (SKU) | التصنيف | الكمية الحالية | الحد الأدنى | الوحدة | الإجراءات |
|---|---|---|---|---|---|---|
|
لا يوجد أصناف مطابقة. |
||||||
| = htmlspecialchars($item['name']) ?> | = htmlspecialchars($item['sku'] ?? '-') ?> | = htmlspecialchars($item['category_name'] ?? 'عام') ?> | = number_format($qty, 2) ?> | = number_format($item['min_quantity']) ?> | = htmlspecialchars($item['unit']) ?> | |