Files
wms-app/app/expense/purchase_request.php
2026-05-22 14:07:22 +07:00

237 lines
9.8 KiB
PHP

<?php
session_start();
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 Requests</h1>
<p class="mb-0 text-muted">Internal purchase requests before purchase order handoff</p>
</div>
<a href="<?php echo $server_url?>expense/manage_purchase_request.php" class="btn btn-primary">
<i class="ti ti-plus me-1"></i>New Purchase Request
</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-notes fs-4"></i></div>
<div><p class="mb-0 text-muted small">Total</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-pencil 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">Submitted</p><h3 class="fw-bold mb-0" id="stat_submitted">—</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">Approved</p><h3 class="fw-bold mb-0" id="stat_approved">—</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 request number or supplier...">
</div>
<div class="col-lg-2">
<select id="filter_status" class="form-select">
<option value="">All Status</option>
<option value="0">Draft</option>
<option value="1">Submitted</option>
<option value="2">Approved</option>
<option value="3">Rejected</option>
<option value="5">Converted</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_requests()">
<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="request_list">
<thead class="table-primary border-light">
<tr>
<th>Request #</th>
<th>Date</th>
<th>Required</th>
<th>Supplier</th>
<th>Department</th>
<th>Items</th>
<th>Grand Total</th>
<th>Status</th>
<th>PO</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);
var status_badge = {
'-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">Submitted</span>',
'2': '<span class="badge bg-success">Approved</span>',
'3': '<span class="badge bg-danger">Rejected</span>',
'5': '<span class="badge bg-primary">Converted</span>',
};
function retrieve_requests() {
return ajax_request({
url: '<?php echo $server_url?>expense/api/engine/retrieve_purchase_request.php',
autoPrepare: true,
checkRequired: 0,
action: 'read',
onSuccess: function(res) {
var all = res.output || [];
var keyword = $('#search_input').val().toLowerCase();
var status = $('#filter_status').val();
var fil_dept = $('#filter_dept').val();
var data = all.filter(function(r) {
var match_kw = !keyword ||
String(r.request_number || '').toLowerCase().includes(keyword) ||
String(r.contact_name || '').toLowerCase().includes(keyword);
var match_st = status === '' || String(r.status) === status;
var match_dept = !fil_dept || String(r.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(r => String(r.status) === '0').length));
$('#stat_submitted').text(format_number(all.filter(r => String(r.status) === '1').length));
$('#stat_approved').text(format_number(all.filter(r => String(r.status) === '2').length));
alasql('CREATE TABLE IF NOT EXISTS purchase_request_list');
alasql.tables.purchase_request_list.data = data;
$('table#request_list tfoot').html(generate_pagination('request_list', data.length));
change_page_request_list(1);
}
});
}
function change_page_request_list(page_num) {
var offset = (page_num - 1) * prop_limit;
var rows = alasql(`SELECT * FROM purchase_request_list LIMIT ${prop_limit} OFFSET ${offset}`);
var body = '';
if (!rows.length) {
$('table#request_list tbody').html('<tr><td colspan="9" class="text-center py-5 text-muted">No purchase requests found.</td></tr>');
return;
}
$.each(rows, function(i, r) {
var items = typeof r.items === 'string' ? JSON.parse(r.items || '[]') : (r.items || []);
var po_link = r.po_id > 0
? `<a href="<?php echo $server_url?>expense/manage_purchase_order.php?id=${r.po_id}">${escape_html(r.po_number || ('PO #' + r.po_id))}</a>`
: '<span class="text-muted">—</span>';
body += `<tr>
<td class="py-3 fw-semibold">${escape_html(r.request_number || '')}</td>
<td class="py-3">${r.request_date ? format_date(r.request_date) : '<span class="text-muted">—</span>'}</td>
<td class="py-3">${r.required_date ? format_date(r.required_date) : '<span class="text-muted">—</span>'}</td>
<td class="py-3">${escape_html(r.contact_name || '—')}</td>
<td class="py-3">${get_dept_label(r.department_id)}</td>
<td class="py-3">${items.length} item${items.length !== 1 ? 's' : ''}</td>
<td class="py-3 fw-semibold">${format_number(r.grand_total, 2)}</td>
<td class="py-3">${status_badge[String(r.status)] || r.status}</td>
<td class="py-3">${po_link}</td>
<td class="py-3">
<a href="<?php echo $server_url?>expense/manage_purchase_request.php?id=${r.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_request(${r.id})"><i class="ti ti-trash fs-5"></i></a>` : ''}
</td>
</tr>`;
});
$('table#request_list tbody').html(body);
}
function delete_request(id) {
bootbox.confirm({
message: 'Delete this purchase request? This cannot be undone.',
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_purchase_request.php',
autoPrepare: false,
checkRequired: 0,
action: 'delete',
data: { id: id },
onSuccess: function() { retrieve_requests(); }
});
}
});
}
$(async function() {
await load_dept_cache();
populate_dept_filter('filter_dept');
retrieve_requests();
$('#search_input').on('keyup', function(e) { if (e.key === 'Enter') retrieve_requests(); });
$('#filter_status, #filter_dept').on('change', retrieve_requests);
});
</script>
</body>
</html>