271 lines
10 KiB
PHP
271 lines
10 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../session.php';
|
|
require '../config.php';
|
|
require '../include_header.php';
|
|
?>
|
|
|
|
<body>
|
|
<?php require '../include_topbar.php'; ?>
|
|
<?php require '../include_sidebar_ac.php'; ?>
|
|
|
|
<main id="content" class="content py-15">
|
|
<div class="container-fluid">
|
|
|
|
<div class="row mb-6">
|
|
<div class="col-12">
|
|
<div class="d-flex justify-content-between align-items-center">
|
|
<div>
|
|
<h1 class="fs-3 mb-1">Purchase Orders</h1>
|
|
<p class="mb-0 text-muted">Supplier orders handed off to WMS receiving</p>
|
|
</div>
|
|
<a href="<?php echo $server_url?>expense/manage_purchase_order.php" class="btn btn-primary">
|
|
<i class="ti ti-plus me-1"></i>New Purchase Order
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<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-danger bg-opacity-10 text-danger rounded-2">
|
|
<i class="ti ti-building-warehouse fs-4"></i>
|
|
</div>
|
|
<div>
|
|
<p class="mb-0 text-muted small">Pending Warehouse</p>
|
|
<h3 class="fw-bold mb-0" id="stat_pending">—</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>
|
|
|
|
<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">
|
|
<input type="text" id="search_input" class="form-control" placeholder="Search PO number or supplier...">
|
|
</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="0">Draft</option>
|
|
<option value="1">Confirmed</option>
|
|
<option value="-1">Cancelled</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>PO #</th>
|
|
<th>Source</th>
|
|
<th>Date</th>
|
|
<th>Supplier</th>
|
|
<th>Department</th>
|
|
<th>Grand Total</th>
|
|
<th>Status</th>
|
|
<th>Receiving</th>
|
|
<th>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) {
|
|
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)] || '—';
|
|
}
|
|
|
|
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 || []).filter(function(po) {
|
|
return ['purchase', 'purchase_request'].includes(String(po.source || ''));
|
|
});
|
|
var keyword = $('#search_input').val().toLowerCase();
|
|
var status = $('#filter_status').val();
|
|
var fil_dept = $('#filter_dept').val();
|
|
var data = all.filter(function(po) {
|
|
var source_id = String(po.source_id || '');
|
|
var source = String(po.source || '');
|
|
var source_label = source === 'purchase' ? 'direct' : 'purchase request';
|
|
var match_kw = !keyword ||
|
|
String(po.po_number || '').toLowerCase().includes(keyword) ||
|
|
String(po.contact_name || '').toLowerCase().includes(keyword) ||
|
|
source_label.includes(keyword) ||
|
|
source_id.includes(keyword);
|
|
var match_status = status === '' || String(po.status) === status;
|
|
var match_dept = !fil_dept || String(po.department_id) === fil_dept;
|
|
return match_kw && match_status && match_dept;
|
|
});
|
|
|
|
$('#stat_total').text(format_number(all.length));
|
|
$('#stat_pending').text(format_number(all.filter(po => String(po.status) === '-2').length));
|
|
$('#stat_confirmed').text(format_number(all.filter(po => String(po.status) === '1').length));
|
|
$('#stat_received').text(format_number(all.filter(po => String(po.receipt_status) === '3').length));
|
|
|
|
alasql('CREATE TABLE IF NOT EXISTS purchase_po_list');
|
|
alasql.tables.purchase_po_list.data = data;
|
|
$('table#po_list tfoot').html(generate_pagination('po_list', data.length));
|
|
change_page_po_list(1);
|
|
}
|
|
});
|
|
}
|
|
|
|
function change_page_po_list(page_num) {
|
|
var offset = (page_num - 1) * prop_limit;
|
|
var rows = alasql(`SELECT * FROM purchase_po_list LIMIT ${prop_limit} OFFSET ${offset}`);
|
|
var body = '';
|
|
if (!rows.length) {
|
|
$('table#po_list tbody').html('<tr><td colspan="8" class="text-center py-5 text-muted">No purchase orders found.</td></tr>');
|
|
return;
|
|
}
|
|
$.each(rows, function(i, po) {
|
|
var source = String(po.source || '');
|
|
var source_display = source === 'purchase_request' && parseInt(po.source_id) > 0
|
|
? `<a href="<?php echo $server_url?>expense/manage_purchase_request.php?id=${po.source_id}">Request #${po.source_id}</a>`
|
|
: (source === 'purchase' ? 'Direct' : '<span class="text-muted">—</span>');
|
|
body += `<tr>
|
|
<td class="py-3 fw-semibold">${escape_html(po.po_number || '')}</td>
|
|
<td class="py-3">${source_display}</td>
|
|
<td class="py-3">${po.po_date ? format_date(po.po_date) : '<span class="text-muted">—</span>'}</td>
|
|
<td class="py-3">${escape_html(po.contact_name || '—')}</td>
|
|
<td class="py-3">${get_dept_label(po.department_id)}</td>
|
|
<td class="py-3 fw-semibold">${format_number(po.grand_total, 2)}</td>
|
|
<td class="py-3">${po_status_badge(po.status)}</td>
|
|
<td class="py-3">${receipt_status_badge(po.receipt_status)}</td>
|
|
<td class="py-3">
|
|
<a href="<?php echo $server_url?>expense/manage_purchase_order.php?id=${po.id}" title="View">
|
|
<i class="ti ti-eye fs-5"></i>
|
|
</a>
|
|
${can_delete ? `<a href="javascript:void(0);" class="link-danger ms-2" title="Delete" onclick="delete_po(${po.id})"><i class="ti ti-trash fs-5"></i></a>` : ''}
|
|
</td>
|
|
</tr>`;
|
|
});
|
|
$('table#po_list tbody').html(body);
|
|
}
|
|
|
|
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>
|