38808-vm/stock_in.php
2026-03-27 03:32:55 +00:00

137 lines
6.0 KiB
PHP

<?php
require_once __DIR__ . '/includes/header.php';
if (!canView('stock_in')) {
echo '<div class="alert alert-danger">عذراً، ليس لديك صلاحية الوصول لهذه الصفحة.</div>';
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();
?>
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">توريد مخزون (وارد)</h1>
<a href="stock_dashboard.php" class="btn btn-secondary">
<i class="fas fa-arrow-right"></i> عودة للوحة التحكم
</a>
</div>
<?php if ($success): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?= $success ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?= $error ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card shadow-sm border-0">
<div class="card-body p-4">
<form method="POST">
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label fw-bold">المستودع <span class="text-danger">*</span></label>
<select name="store_id" class="form-select" required>
<option value="">-- اختر المستودع --</option>
<?php foreach ($stores as $store): ?>
<option value="<?= $store['id'] ?>"><?= htmlspecialchars($store['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6 mb-3">
<label class="form-label fw-bold">الصنف <span class="text-danger">*</span></label>
<select name="item_id" class="form-select" required>
<option value="">-- اختر الصنف --</option>
<?php foreach ($items as $item): ?>
<option value="<?= $item['id'] ?>"><?= htmlspecialchars($item['name']) ?> (<?= htmlspecialchars($item['sku'] ?: '-') ?>)</option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label fw-bold">الكمية <span class="text-danger">*</span></label>
<input type="number" step="0.01" name="quantity" class="form-control" required min="0.01">
</div>
<div class="col-md-6 mb-3">
<label class="form-label fw-bold">رقم المرجع / الفاتورة</label>
<input type="text" name="reference" class="form-control" placeholder="مثال: فاتورة رقم 123">
</div>
</div>
<div class="mb-3">
<label class="form-label fw-bold">ملاحظات</label>
<textarea name="notes" class="form-control" rows="3"></textarea>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-success btn-lg">
<i class="fas fa-save me-2"></i> حفظ عملية التوريد
</button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>