عذراً، ليس لديك صلاحية الوصول لهذه الصفحة.'; 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(); ?>

صرف مخزون (صادر)

عودة للوحة التحكم