Files
wms-app/app/asset/purchase_invoices.php
T
Thanakorn c957280aa5 Add Asset Management app
Assets from purchase invoice lines, straight-line depreciation/amortization with salvage value, disposals, and [Asset Mgmt] sources in Batch GL Entries. Green third workspace with an app access guard.
2026-09-15 09:50:55 +07:00

174 lines
6.8 KiB
PHP

<?php
session_start();
require '../config.php';
require '../include_header.php';
$pi_link_ok = in_array($_SESSION['login_app_access'] ?? 'wms', ['all', 'accounting'], true);
?>
<body>
<?php require '../include_topbar.php'; ?>
<?php require '../include_sidebar_asset.php'; ?>
<main id="content" class="content py-15">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="mb-5">
<h1 class="fs-3 mb-1">Assets from Purchase Invoices</h1>
<p class="mb-0">Issued purchase invoice lines (AP) that can be capitalized as assets</p>
</div>
</div>
</div>
<div class="card mb-5">
<div class="card-body p-4">
<div class="row g-2 align-items-end">
<div class="col-auto">
<label class="form-label mb-1 small">Invoice Date From</label>
<input type="text" id="date_from" class="form-control form-control-sm" placeholder="d/m/Y" style="width:140px;">
</div>
<div class="col-auto">
<label class="form-label mb-1 small">Invoice Date To</label>
<input type="text" id="date_to" class="form-control form-control-sm" placeholder="d/m/Y" style="width:140px;">
</div>
<div class="col-auto">
<button class="btn btn-secondary btn-sm" onclick="load_lines()">
<i class="ti ti-search me-1"></i>Load
</button>
</div>
<div class="col-auto ms-3">
<div class="form-check mb-1">
<input class="form-check-input" type="checkbox" id="show_capitalized">
<label class="form-check-label small" for="show_capitalized">Show fully capitalized lines</label>
</div>
</div>
<div class="col ms-auto d-flex justify-content-end">
<div class="position-relative" style="min-width:220px;">
<input type="search" id="search_line" class="form-control form-control-sm ps-8" placeholder="Search invoice, supplier, item…">
<span class="position-absolute top-25 ms-3"><i class="ti ti-search text-muted small"></i></span>
</div>
</div>
</div>
</div>
</div>
<div class="card">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover mb-0 table-centered text-nowrap" id="line_table">
<thead class="table-primary border-light">
<tr>
<th>Invoice</th>
<th>Date</th>
<th>Supplier</th>
<th>Item</th>
<th class="text-end">Qty</th>
<th class="text-end">Line Amount</th>
<th class="text-end">Capitalized</th>
<th class="text-end">Remaining</th>
<th></th>
</tr>
</thead>
<tbody>
<tr><td colspan="9" class="text-center text-muted py-5">Use the filters above and click Load.</td></tr>
</tbody>
<tfoot></tfoot>
</table>
</div>
</div>
</div>
</div>
</main>
<?php require '../include_ending.php'; ?>
<script src="<?php echo $server_url?>assets/js/asset.js"></script>
<script>
var pi_link_ok = <?php echo $pi_link_ok ? 'true' : 'false'; ?>;
var all_lines = [];
var search_term = '';
function load_lines() {
return ajax_request({
url: server_url + 'asset/api/engine/invoice_lines.php',
autoPrepare: true,
checkRequired: 0,
action: 'read',
data: { date_from: $('#date_from').val(), date_to: $('#date_to').val() },
onSuccess: function(res) {
all_lines = res.output || [];
change_page(1);
}
});
}
function visible_lines() {
var show_all = $('#show_capitalized').is(':checked');
var kw = search_term.toLowerCase();
return all_lines.filter(function(l) {
if (!show_all && parseFloat(l.remaining) <= 0) return false;
if (!kw) return true;
return [l.invoice_number, l.contact_name, l.product_sku, l.product_name]
.some(function(v) { return String(v || '').toLowerCase().indexOf(kw) !== -1; });
});
}
function change_page(page_num) {
var rows = visible_lines();
var limit = parseInt(prop_limit, 10);
var offset = (page_num - 1) * limit;
$('#line_table tfoot').html(generate_pagination('line_table', rows.length));
if (!rows.length) {
$('#line_table tbody').html('<tr><td colspan="9" class="text-center text-muted py-5">No invoice lines to capitalize in this date range.</td></tr>');
return;
}
var html = '';
rows.slice(offset, offset + limit).forEach(function(l) {
var invoice = pi_link_ok
? '<a href="' + server_url + 'expense/manage_purchase_invoice.php?id=' + l.invoice_id + '">' + escape_html(l.invoice_number) + '</a>'
: escape_html(l.invoice_number);
var action = parseFloat(l.remaining) > 0
? '<a class="btn btn-sm btn-primary" href="' + server_url + 'asset/manage_asset.php?invoice_id=' + l.invoice_id + '&item_id=' + l.item_id + '">'
+ '<i class="ti ti-plus me-1"></i>Create Asset</a>'
: '<span class="badge bg-success-subtle text-success">Capitalized</span>';
var assets = parseInt(l.asset_count, 10) > 0
? ' <span class="small text-muted">(' + l.asset_count + ' asset' + (l.asset_count == 1 ? '' : 's') + ')</span>'
: '';
html += '<tr>'
+ '<td>' + invoice + '</td>'
+ '<td>' + format_date(l.issued_date) + '</td>'
+ '<td>' + escape_html(l.contact_name || '—') + '</td>'
+ '<td>' + escape_html(l.product_name || l.product_sku) + '<div class="small text-muted">' + escape_html(l.product_sku) + '</div></td>'
+ '<td class="text-end">' + format_number(l.quantity, 2) + '</td>'
+ '<td class="text-end">' + format_number(l.line_amount, 2) + '</td>'
+ '<td class="text-end">' + format_number(l.capitalized, 2) + assets + '</td>'
+ '<td class="text-end fw-semibold">' + format_number(l.remaining, 2) + '</td>'
+ '<td class="text-end">' + action + '</td>'
+ '</tr>';
});
$('#line_table tbody').html(html);
}
$('#show_capitalized').on('change', function() { change_page(1); });
$('#search_line').on('input', debounce(function() {
search_term = $(this).val().trim();
change_page(1);
}, 200));
$(function() {
var today = new Date();
var start = new Date(today.getFullYear(), 0, 1);
flatpickr('#date_from', { dateFormat: 'd/m/Y', allowInput: true, defaultDate: start });
flatpickr('#date_to', { dateFormat: 'd/m/Y', allowInput: true, defaultDate: today });
load_lines().catch(function(e) { console.log(e); });
});
</script>
</body>
</html>