عذراً، ليس لديك صلاحية الوصول لهذه الصفحة.'; require_once __DIR__ . '/includes/footer.php'; exit; } $success = ''; $error = ''; // Handle Form Submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { $store_id = $_POST['store_id'] ?? null; $item_id = $_POST['item_id'] ?? null; $quantity = $_POST['quantity'] ?? 0; $type = $_POST['type'] ?? 'out'; // 'out' or 'damage' $reference = $_POST['reference'] ?? ''; $notes = $_POST['notes'] ?? ''; if ($store_id && $item_id && $quantity > 0) { try { $pdo = db(); // Check availability first $check = $pdo->prepare("SELECT id, quantity FROM stock_quantities WHERE store_id = ? AND item_id = ?"); $check->execute([$store_id, $item_id]); $stock = $check->fetch(); if (!$stock || $stock['quantity'] < $quantity) { $error = 'الكمية غير متوفرة في المستودع المحدد. الكمية الحالية: ' . ($stock['quantity'] ?? 0); } else { $pdo->beginTransaction(); // 1. Create Transaction $stmt = $pdo->prepare("INSERT INTO stock_transactions (transaction_type, store_id, item_id, quantity, user_id, reference, notes) VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$type, $store_id, $item_id, $quantity, $_SESSION['user_id'], $reference, $notes]); // 2. Update Quantity $new_qty = $stock['quantity'] - $quantity; $update = $pdo->prepare("UPDATE stock_quantities SET quantity = ? WHERE id = ?"); $update->execute([$new_qty, $stock['id']]); $pdo->commit(); $success = 'تم تسجيل عملية الصرف بنجاح'; } } catch (PDOException $e) { if ($pdo->inTransaction()) $pdo->rollBack(); $error = 'حدث خطأ: ' . $e->getMessage(); } } else { $error = 'يرجى تعبئة جميع الحقول المطلوبة'; } } // Fetch Data for Dropdowns $stores = db()->query("SELECT * FROM stock_stores ORDER BY name ASC")->fetchAll(); $items = db()->query("SELECT * FROM stock_items ORDER BY name ASC")->fetchAll(); // Pagination for History $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; if ($page < 1) $page = 1; $limit = 10; $offset = ($page - 1) * $limit; $where = "WHERE t.transaction_type IN ('out', 'damage')"; $params = []; // Count Total $countQuery = "SELECT COUNT(*) FROM stock_transactions t $where"; $countStmt = db()->prepare($countQuery); $countStmt->execute($params); $totalFiltered = $countStmt->fetchColumn(); // Fetch History $historyQuery = " SELECT t.*, i.name as item_name, s.name as store_name, u.full_name as user_name FROM stock_transactions t JOIN stock_items i ON t.item_id = i.id JOIN stock_stores s ON t.store_id = s.id LEFT JOIN users u ON t.user_id = u.id $where ORDER BY t.created_at DESC LIMIT $limit OFFSET $offset "; $stmt = db()->prepare($historyQuery); $stmt->execute($params); $history = $stmt->fetchAll(); ?>
| # | النوع | الصنف | المستودع | الكمية | بواسطة | التاريخ | المرجع |
|---|---|---|---|---|---|---|---|
| لا توجد عمليات صرف سابقة. | |||||||
| = $h['id'] ?> | تالف صرف | = htmlspecialchars($h['item_name']) ?> | = htmlspecialchars($h['store_name']) ?> | -= number_format($h['quantity'], 2) ?> | = htmlspecialchars($h['user_name'] ?? '-') ?> | = date('Y-m-d H:i', strtotime($h['created_at'])) ?> | = htmlspecialchars($h['reference'] ?? '-') ?> |