39728-vm/purchases.php
2026-04-19 09:18:23 +00:00

157 lines
6.9 KiB
PHP

<?php
require_once __DIR__ . '/includes/app.php';
$user = require_roles(['owner', 'manager']);
$pageTitle = tr('المشتريات', 'Purchases');
$activeNav = 'purchases';
// Search logic
$search = $_GET['q'] ?? '';
$page = max(1, (int)($_GET['p'] ?? 1));
$limit = 10;
$offset = ($page - 1) * $limit;
$params = [];
$where = ' WHERE 1=1 ';
if ($search) {
$where .= ' AND (supplier_name LIKE :search OR reference_no LIKE :search) ';
$params[':search'] = "%$search%";
}
// Pagination counts
$countSql = 'SELECT COUNT(*) FROM purchase_orders' . $where;
$countStmt = db()->prepare($countSql);
foreach ($params as $key => $value) {
$countStmt->bindValue($key, $value);
}
$countStmt->execute();
$total = $countStmt->fetchColumn();
$totalPages = max(1, ceil($total / $limit));
// Fetch Data
$sql = 'SELECT * FROM purchase_orders' . $where . ' ORDER BY purchase_date DESC LIMIT :limit OFFSET :offset';
$stmt = db()->prepare($sql);
foreach ($params as $key => $value) {
$stmt->bindValue($key, $value);
}
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$purchaseRows = $stmt->fetchAll();
require __DIR__ . '/includes/header.php';
?>
<section class="surface-card mb-4">
<div class="row g-4 align-items-center mb-3">
<div class="col-lg-8">
<h3 class="h5 mb-2"><i class="bi bi-bag-plus me-2"></i><?= h(tr('قائمة المشتريات', 'Purchase List')) ?></h3>
<p class="text-muted mb-0"><?= h(tr('عرض المشتريات من الموردين وتحديث المخزون.', 'View supplier purchases and inventory updates.')) ?></p>
</div>
<div class="col-lg-4 text-lg-end">
<a href="new_purchase.php" class="btn btn-primary">
<i class="bi bi-plus-lg"></i> <?= h(tr('إضافة فاتورة مشتريات', 'Add Purchase Invoice')) ?>
</a>
</div>
</div>
<form class="d-flex mb-3" method="GET" action="purchases.php">
<div class="input-group" style="max-width: 400px;">
<input type="text" name="q" class="form-control" placeholder="<?= h(tr('بحث...', 'Search...')) ?>" value="<?= h($search) ?>">
<button class="btn btn-outline-secondary" type="submit"><i class="bi bi-search"></i></button>
</div>
</form>
</section>
<section class="surface-card">
<div class="table-responsive shadow-sm" style="border-radius: 12px; overflow: hidden; border: 1px solid rgba(0,0,0,0.05);">
<table class="table table-hover align-middle mb-0 text-center" style="background-color: #fff;">
<thead style="background: linear-gradient(90deg, #0d6efd, #0dcaf0);">
<tr>
<th class="text-white border-0 py-3 fw-semibold bg-transparent"><?= h(tr('المورد', 'Supplier')) ?></th>
<th class="text-white border-0 py-3 fw-semibold bg-transparent"><?= h(tr('المرجع', 'Reference')) ?></th>
<th class="text-white border-0 py-3 fw-semibold bg-transparent"><?= h(tr('الفرع', 'Branch')) ?></th>
<th class="text-white border-0 py-3 fw-semibold bg-transparent"><?= h(tr('الإجمالي', 'Total Amount')) ?></th>
<th class="text-white border-0 py-3 fw-semibold bg-transparent"><?= h(tr('الحالة', 'Status')) ?></th>
<th class="text-white border-0 py-3 fw-semibold bg-transparent"><?= h(tr('التاريخ', 'Date')) ?></th>
<th class="text-white border-0 py-3 fw-semibold bg-transparent"><?= h(tr('إجراءات', 'Actions')) ?></th>
</tr>
</thead>
<tbody class="border-top-0">
<?php if(empty($purchaseRows)): ?>
<tr><td colspan="7" class="text-center text-muted py-4"><?= h(tr('لا توجد بيانات', 'No data found')) ?></td></tr>
<?php endif; ?>
<?php foreach ($purchaseRows as $row): ?>
<tr>
<td><?= h($row['supplier_name'] ?: '-') ?></td>
<td><?= h($row['reference_no']) ?></td>
<td><?= h(branch_label($row['branch_code'])) ?></td>
<td class="fw-semibold"><?= h(currency((float)$row['total_amount'])) ?></td>
<td>
<?php if ($row['status'] === 'order'): ?>
<span class="badge bg-warning text-dark px-3 py-2 rounded-pill"><i class="bi bi-clock"></i> <?= h(tr('طلب شراء', 'Order')) ?></span>
<?php else: ?>
<span class="badge bg-success px-3 py-2 rounded-pill"><i class="bi bi-check-circle"></i> <?= h(tr('مكتمل', 'Completed')) ?></span>
<?php endif; ?>
</td>
<td><?= h(date('Y-m-d', strtotime((string)$row['purchase_date']))) ?></td>
<td>
<button class="btn btn-sm btn-outline-primary rounded-circle shadow-sm" style="width: 34px; height: 34px; padding: 0;" onclick="mockEdit()" title="<?= h(tr('تعديل', 'Edit')) ?>">
<i class="bi bi-pencil"></i>
</button>
<button class="btn btn-sm btn-outline-danger rounded-circle shadow-sm ms-1" style="width: 34px; height: 34px; padding: 0;" onclick="mockDelete()" title="<?= h(tr('حذف', 'Delete')) ?>">
<i class="bi bi-trash"></i>
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php if ($totalPages > 1): ?>
<nav class="mt-4">
<ul class="pagination justify-content-center mb-0">
<?php for($i=1; $i<=$totalPages; $i++): ?>
<li class="page-item <?= $i === $page ? 'active' : '' ?>">
<a class="page-link" href="<?= h(url_for('purchases.php', ['p' => $i, 'q' => $search])) ?>"><?= $i ?></a>
</li>
<?php endfor; ?>
</ul>
</nav>
<?php endif; ?>
</section>
<script>
function mockEdit() {
Swal.fire({
title: '<?= h(tr('تعديل (غير متاح)', 'Edit (Disabled)')) ?>',
text: '<?= h(tr('تعديل الفواتير غير متاح حالياً.', 'Editing invoices is currently disabled.')) ?>',
icon: 'info',
confirmButtonText: '<?= h(tr('حسناً', 'OK')) ?>'
});
}
function mockDelete() {
Swal.fire({
title: '<?= h(tr('هل أنت متأكد؟', 'Are you sure?')) ?>',
text: '<?= h(tr('هل تريد حذف هذه الفاتورة؟ (هذه الميزة غير متاحة حالياً)', "Do you want to delete this invoice? (Currently disabled)")) ?>',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#dc3545',
cancelButtonColor: '#6c757d',
confirmButtonText: '<?= h(tr('نعم، احذف', 'Yes, delete it!')) ?>',
cancelButtonText: '<?= h(tr('إلغاء', 'Cancel')) ?>'
}).then((result) => {
if (result.isConfirmed) {
Swal.fire(
'<?= h(tr('مرفوض!', 'Denied!')) ?>',
'<?= h(tr('حذف الفاتورة غير مدعوم في هذه النسخة.', 'Deleting invoice is not supported in this version.')) ?>',
'error'
);
}
});
}
</script>
<?php require __DIR__ . '/includes/footer.php'; ?>