عذراً، ليس لديك صلاحية الوصول لهذه الصفحة.'; 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; $reference = $_POST['reference'] ?? ''; $notes = $_POST['notes'] ?? ''; if ($store_id && $item_id && $quantity > 0) { try { $pdo = db(); $pdo->beginTransaction(); // 1. Create Transaction $stmt = $pdo->prepare("INSERT INTO stock_transactions (transaction_type, store_id, item_id, quantity, user_id, reference, notes) VALUES ('in', ?, ?, ?, ?, ?, ?)"); $stmt->execute([$store_id, $item_id, $quantity, $_SESSION['user_id'], $reference, $notes]); // 2. Update Quantity // Check if record exists $check = $pdo->prepare("SELECT id, quantity FROM stock_quantities WHERE store_id = ? AND item_id = ?"); $check->execute([$store_id, $item_id]); $exists = $check->fetch(); if ($exists) { $new_qty = $exists['quantity'] + $quantity; $update = $pdo->prepare("UPDATE stock_quantities SET quantity = ? WHERE id = ?"); $update->execute([$new_qty, $exists['id']]); } else { $insert = $pdo->prepare("INSERT INTO stock_quantities (store_id, item_id, quantity) VALUES (?, ?, ?)"); $insert->execute([$store_id, $item_id, $quantity]); } $pdo->commit(); $success = 'تم تسجيل عملية التوريد بنجاح'; } catch (PDOException $e) { $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(); ?>

توريد مخزون (وارد)

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