Files
2026-05-22 14:07:22 +07:00

322 lines
11 KiB
PHP

<?php
session_start();
require '../config.php';
require '../include_header.php';
?>
<body>
<?php require '../include_topbar.php'; ?>
<?php require '../include_sidebar.php'; ?>
<main id="content" class="content py-15">
<div class="container-fluid">
<!-- Page header -->
<div class="row">
<div class="col-12">
<div class="mb-6 d-flex justify-content-between align-items-center">
<div>
<h1 class="fs-3 mb-1">Purchase Orders</h1>
<p class="mb-0">Manage and track all purchase orders from suppliers</p>
</div>
<a href="<?php echo $server_url?>po/manage_po.php" class="btn btn-primary">
<i class="ti ti-plus"></i>
<span>New PO</span>
</a>
</div>
</div>
</div>
<!-- Stat cards -->
<div class="row g-5 mb-5">
<div class="col-lg-3 col-sm-6 col-12">
<div class="card p-4">
<div class="d-flex align-items-center gap-3">
<div class="icon-shape icon-md bg-primary bg-opacity-10 text-primary rounded-2">
<i class="ti ti-clipboard-list fs-4"></i>
</div>
<div>
<p class="mb-0 text-muted small">Total POs</p>
<h3 class="fw-bold mb-0" id="stat_total">—</h3>
</div>
</div>
</div>
</div>
<div class="col-lg-3 col-sm-6 col-12">
<div class="card p-4">
<div class="d-flex align-items-center gap-3">
<div class="icon-shape icon-md bg-warning bg-opacity-10 text-warning rounded-2">
<i class="ti ti-clock fs-4"></i>
</div>
<div>
<p class="mb-0 text-muted small">Draft</p>
<h3 class="fw-bold mb-0" id="stat_draft">—</h3>
</div>
</div>
</div>
</div>
<div class="col-lg-3 col-sm-6 col-12">
<div class="card p-4">
<div class="d-flex align-items-center gap-3">
<div class="icon-shape icon-md bg-info bg-opacity-10 text-info rounded-2">
<i class="ti ti-send fs-4"></i>
</div>
<div>
<p class="mb-0 text-muted small">Confirmed</p>
<h3 class="fw-bold mb-0" id="stat_confirmed">—</h3>
</div>
</div>
</div>
</div>
<div class="col-lg-3 col-sm-6 col-12">
<div class="card p-4">
<div class="d-flex align-items-center gap-3">
<div class="icon-shape icon-md bg-success bg-opacity-10 text-success rounded-2">
<i class="ti ti-circle-check fs-4"></i>
</div>
<div>
<p class="mb-0 text-muted small">Received</p>
<h3 class="fw-bold mb-0" id="stat_received">—</h3>
</div>
</div>
</div>
</div>
</div>
<!-- Filter + Table -->
<div class="row g-5">
<div class="col-12">
<div class="card">
<div class="card-body p-4">
<div class="row g-3 align-items-end">
<div class="col-lg-4">
<div class="position-relative">
<input type="text" id="search_input" class="form-control ps-9"
placeholder="Search PO number or supplier...">
<span class="position-absolute top-25 ms-4">
<i class="ti ti-search text-muted"></i>
</span>
</div>
</div>
<div class="col-lg-2">
<select id="filter_status" class="form-select">
<option value="">All Status</option>
<option value="-2">Pending Warehouse</option>
<option value="-1">Cancelled</option>
<option value="0">Draft</option>
<option value="1">Confirmed</option>
</select>
</div>
<div class="col-lg-2">
<select id="filter_dept" class="form-select">
<option value="">All Departments</option>
</select>
</div>
<div class="col-lg-2">
<button class="btn btn-secondary w-100" onclick="retrieve_pos()">
<i class="ti ti-search me-1"></i>Search
</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-12">
<div class="card">
<div class="card-body p-5">
<div class="table-responsive">
<table class="table table-hover mb-0 table-centered" id="po_list">
<thead class="table-primary border-light">
<tr>
<th class="py-3">PO #</th>
<th class="py-3">Date</th>
<th class="py-3">Supplier</th>
<th class="py-3">Department</th>
<th class="py-3">Items</th>
<th class="py-3">Grand Total</th>
<th class="py-3">Status</th>
<th class="py-3">Stock</th>
<th class="py-3">Expected</th>
<th class="py-3">Action</th>
</tr>
</thead>
<tbody></tbody>
<tfoot></tfoot>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</main>
<?php require '../include_ending.php'; ?>
<script>
const can_delete = ['admin', 'owner'].includes(user_role);
function po_status_badge(status) {
status = parseInt(status);
const map = {
'-2': '<span class="badge bg-danger">Pending Warehouse</span>',
'-1': '<span class="badge bg-secondary">Cancelled</span>',
'0': '<span class="badge bg-warning text-dark">Draft</span>',
'1': '<span class="badge bg-info text-white">Confirmed</span>',
};
return map[String(status)] ?? '<span class="badge bg-light text-dark">—</span>';
}
function receipt_status_badge(status) {
const map = {
'2': '<span class="badge bg-primary">Partial</span>',
'3': '<span class="badge bg-success">Received</span>',
};
return map[String(status)] ?? '<span class="text-muted">—</span>';
}
function retrieve_pos() {
return ajax_request({
url: '<?php echo $server_url?>po/api/engine/retrieve_po.php',
autoPrepare: true,
checkRequired: 0,
action: 'read',
onSuccess: function(res) {
var all = res.output || [];
var keyword = $('#search_input').val().toLowerCase();
var fil_status = $('#filter_status').val();
var fil_dept = $('#filter_dept').val();
var data = all.filter(function(o) {
var match_kw = !keyword ||
(o.po_number || '').toLowerCase().includes(keyword) ||
(o.contact_name || '').toLowerCase().includes(keyword);
var match_st = fil_status === '' || String(o.status) === fil_status;
var match_dept = !fil_dept || String(o.department_id) === fil_dept;
return match_kw && match_st && match_dept;
});
$('#stat_total').text(format_number(all.length));
$('#stat_draft').text(format_number(all.filter(o => parseInt(o.status) === 0).length));
$('#stat_confirmed').text(format_number(all.filter(o => parseInt(o.status) === 1).length));
$('#stat_received').text(format_number(all.filter(o => parseInt(o.receipt_status) === 3).length));
alasql('CREATE TABLE IF NOT EXISTS po_list');
alasql.tables.po_list.data = data;
var page = generate_pagination('po_list', data.length);
$('table#po_list tfoot').html(page);
if (typeof change_page_po_list === 'function') {
change_page_po_list(1);
}
}
});
}
function change_page_po_list(page_num) {
var offset = (page_num - 1) * prop_limit;
var page_data = alasql(`SELECT * FROM po_list LIMIT ${prop_limit} OFFSET ${offset}`);
var body = '';
$.each(page_data, function(i, o) {
var items = JSON.parse(o.items || '[]');
var item_count = items.length;
var can_cancel = parseInt(o.status) >= -2 && parseInt(o.status) <= 1;
body += `<tr>
<td class="py-3 fw-semibold">${o.po_number}</td>
<td class="py-3">${format_date(o.po_date)}</td>
<td class="py-3">${o.contact_name || '<span class="text-muted">—</span>'}</td>
<td class="py-3">${get_dept_label(o.department_id)}</td>
<td class="py-3">${item_count} item${item_count !== 1 ? 's' : ''}</td>
<td class="py-3 fw-semibold">${format_number(o.grand_total, 2)}</td>
<td class="py-3">${po_status_badge(o.status)}</td>
<td class="py-3">${receipt_status_badge(o.receipt_status)}</td>
<td class="py-3">${o.expected_date ? format_date(o.expected_date) : '<span class="text-muted">—</span>'}</td>
<td class="py-3">
<a href="<?php echo $server_url?>po/manage_po.php?id=${o.id}" title="View">
<i class="ti ti-eye fs-5"></i>
</a>
<a href="javascript:void(0);" class="ms-2 text-secondary" title="Stock"
onclick="show_stock_rows('po', ${o.id}, '${escape_html(o.po_number || '')}')">
<i class="ti ti-packages fs-5"></i>
</a>
${can_cancel ? `
<a href="javascript:void(0);" class="link-danger ms-2" title="Cancel"
onclick="cancel_po(${o.id})">
<i class="ti ti-x fs-5"></i>
</a>` : ''}
${can_delete ? `<a href="javascript:void(0);" class="link-danger ms-2" title="Delete" onclick="delete_po(${o.id})"><i class="ti ti-trash fs-5"></i></a>` : ''}
</td>
</tr>`;
});
$('table#po_list > tbody').html(body);
}
function cancel_po(id) {
bootbox.confirm({
message: 'Cancel this purchase order?',
buttons: {
confirm: { label: 'Yes, Cancel PO', className: 'btn-danger' },
cancel: { label: 'Back', className: 'btn-secondary' }
},
callback: function(result) {
if (!result) return;
ajax_request({
url: '<?php echo $server_url?>po/api/engine/cancel_po.php',
autoPrepare: true,
checkRequired: 0,
action: 'update',
data: { id: id },
onSuccess: function() {
retrieve_pos();
}
});
}
});
}
function delete_po(id) {
bootbox.confirm({
message: 'Delete this PO? Unconfirmed stock-in reservations will be released.',
buttons: { confirm: { label: 'Delete', className: 'btn-danger' }, cancel: { label: 'Back', className: 'btn-secondary' } },
callback: function(result) {
if (!result) return;
ajax_request({
url: '<?php echo $server_url?>po/api/engine/delete_po.php',
autoPrepare: false,
checkRequired: 0,
action: 'delete',
data: { id: id },
onSuccess: function() { retrieve_pos(); }
});
}
});
}
$(async function() {
await load_dept_cache();
populate_dept_filter('filter_dept');
retrieve_pos();
$('#search_input').on('keyup', function(e) {
if (e.key === 'Enter') retrieve_pos();
});
$('#filter_status, #filter_dept').on('change', retrieve_pos);
});
</script>
</body>
</html>