309 lines
12 KiB
PHP
309 lines
12 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../session.php';
|
|
require '../config.php';
|
|
|
|
// A detail-only page: receipts are created from the Receipts list, so with no
|
|
// id there is nothing to show. Send the user there instead of rendering an
|
|
// empty page that alerts "Receipt id is required."
|
|
$receipt_id = (int)($_GET['id'] ?? 0);
|
|
if (!$receipt_id) {
|
|
header('Location: ' . $server_url . 'finance/receipt.php');
|
|
exit;
|
|
}
|
|
|
|
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">
|
|
<div class="col-12">
|
|
<div class="mb-6 d-flex justify-content-between align-items-center">
|
|
<div>
|
|
<h1 class="fs-3 mb-1" id="page_title">Receipt Detail</h1>
|
|
<p class="mb-0 text-muted">Customer receipt and document allocations</p>
|
|
</div>
|
|
<a href="<?php echo $server_url?>finance/receipt.php" class="btn btn-light">
|
|
<i class="ti ti-arrow-left me-1"></i>Back
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row g-5">
|
|
<div class="col-lg-8">
|
|
<div class="card">
|
|
<div class="card-body p-5">
|
|
<h2 class="fs-5 mb-4">Allocations</h2>
|
|
<div class="table-responsive">
|
|
<table class="table mb-0">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>Document</th>
|
|
<th>Type</th>
|
|
<th>Order #</th>
|
|
<th class="text-end">Allocated</th>
|
|
<th class="text-end">Document Total</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="allocation_tbody">
|
|
<tr><td colspan="5" class="text-center py-5 text-muted">Loading receipt...</td></tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-lg-4">
|
|
<div class="card mb-5">
|
|
<div class="card-body p-4">
|
|
<h2 class="fs-5 mb-3">Receipt Info</h2>
|
|
<div class="mb-3" id="badge_status"></div>
|
|
<div class="mb-2 small">
|
|
<span class="text-muted">Receipt #:</span>
|
|
<span class="ms-1 fw-semibold" id="display_number">—</span>
|
|
</div>
|
|
<div class="mb-2 small">
|
|
<span class="text-muted">Billing #:</span>
|
|
<span class="ms-1 fw-semibold" id="display_billing">—</span>
|
|
</div>
|
|
<div class="mb-2 small">
|
|
<span class="text-muted">Customer:</span>
|
|
<span class="ms-1 fw-semibold" id="display_contact">—</span>
|
|
</div>
|
|
<div class="mb-2 small">
|
|
<span class="text-muted">Date:</span>
|
|
<span class="ms-1" id="display_date">—</span>
|
|
</div>
|
|
<div class="mb-2 small">
|
|
<span class="text-muted">Method:</span>
|
|
<span class="ms-1" id="display_method">—</span>
|
|
</div>
|
|
<div class="mt-3 small">
|
|
<span class="text-muted">Notes:</span>
|
|
<div class="mt-1" id="display_notes">—</div>
|
|
</div>
|
|
<div class="mt-3 small">
|
|
<span class="text-muted">GL Formula:</span>
|
|
<div class="d-flex gap-1 mt-1">
|
|
<select id="formula_id" class="form-select form-select-sm">
|
|
<option value="">— None —</option>
|
|
</select>
|
|
<button class="btn btn-outline-secondary btn-sm px-2" id="btn_save_formula"
|
|
onclick="save_formula()" title="Save formula">
|
|
<i class="ti ti-device-floppy"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card mb-5">
|
|
<div class="card-body p-4">
|
|
<h2 class="fs-5 mb-3">Summary</h2>
|
|
<div class="d-flex justify-content-between mb-2">
|
|
<span class="text-muted">Invoice Allocations</span>
|
|
<span id="summary_invoice">0.00</span>
|
|
</div>
|
|
<div class="d-flex justify-content-between mb-2">
|
|
<span class="text-muted">Credit Notes Used</span>
|
|
<span id="summary_cn" class="text-danger">-0.00</span>
|
|
</div>
|
|
<hr>
|
|
<div class="d-flex justify-content-between fw-bold fs-5">
|
|
<span>Cash Receipt</span>
|
|
<span id="summary_cash">0.00</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-body p-4 d-flex flex-column gap-2">
|
|
<button class="btn btn-outline-secondary w-100" onclick="window.print()">
|
|
<i class="ti ti-printer me-1"></i>Print / Save PDF
|
|
</button>
|
|
<button class="btn btn-outline-danger w-100 d-none" id="btn_void" onclick="void_receipt()">
|
|
<i class="ti ti-ban me-1"></i>Void Receipt
|
|
</button>
|
|
<button class="btn btn-outline-danger w-100 d-none" id="btn_delete" onclick="delete_receipt()">
|
|
<i class="ti ti-trash me-1"></i>Delete Receipt
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<?php require '../include_ending.php'; ?>
|
|
|
|
<script>
|
|
|
|
const can_delete = ['admin', 'owner'].includes(user_role);
|
|
|
|
var receipt_id = <?php echo $receipt_id; ?>;
|
|
var receipt = null;
|
|
|
|
function display_text(value) {
|
|
return value ? escape_html(value) : '<span class="text-muted">—</span>';
|
|
}
|
|
|
|
function receipt_status_badge(status) {
|
|
return String(status) === '4'
|
|
? '<span class="badge bg-light text-dark">Void</span>'
|
|
: '<span class="badge bg-success">Posted</span>';
|
|
}
|
|
|
|
function method_label(value) {
|
|
return String(value || '').replace(/_/g, ' ').replace(/\b\w/g, function(c) {
|
|
return c.toUpperCase();
|
|
});
|
|
}
|
|
|
|
function doc_type_badge(doc_type) {
|
|
return doc_type === 'credit_note'
|
|
? '<span class="badge bg-warning text-dark">Credit Note</span>'
|
|
: '<span class="badge bg-primary">Invoice</span>';
|
|
}
|
|
|
|
function retrieve_receipt() {
|
|
return ajax_request({
|
|
url: '<?php echo $server_url?>finance/api/engine/manage_receipt.php',
|
|
autoPrepare: true,
|
|
checkRequired: 0,
|
|
action: 'retrieve',
|
|
data: { id: receipt_id },
|
|
onSuccess: function(res) {
|
|
receipt = res.output || {};
|
|
render_receipt();
|
|
}
|
|
});
|
|
}
|
|
|
|
function render_receipt() {
|
|
$('#page_title').text(receipt.receipt_number || 'Receipt Detail');
|
|
$('#badge_status').html(receipt_status_badge(receipt.status));
|
|
$('#display_number').html(display_text(receipt.receipt_number));
|
|
$('#display_billing').html(receipt.billing_number
|
|
? `<a href="<?php echo $server_url?>finance/manage_receipt_billing.php?id=${receipt.receipt_billing_id}">${escape_html(receipt.billing_number)}</a>`
|
|
: '<span class="text-muted">—</span>');
|
|
$('#display_contact').html(display_text(receipt.contact_name));
|
|
$('#display_date').html(receipt.receipt_date ? format_date(receipt.receipt_date) : '<span class="text-muted">—</span>');
|
|
$('#display_method').html(display_text(method_label(receipt.payment_method)));
|
|
$('#display_notes').html(receipt.notes ? escape_html(receipt.notes).replace(/\n/g, '<br>') : '<span class="text-muted">—</span>');
|
|
$('#btn_void').toggleClass('d-none', String(receipt.status) === '4');
|
|
$('#btn_delete').toggleClass('d-none', !can_delete || !receipt_id);
|
|
|
|
var selected_formula = receipt.formula_id || '';
|
|
ajax_request({
|
|
url: '<?php echo $server_url?>accounting/api/engine/account_formula.php',
|
|
autoPrepare: true,
|
|
checkRequired: 0,
|
|
action: 'get_by_type',
|
|
data: { document_type: 'receipt' },
|
|
onSuccess: function(fres) {
|
|
var opts = '<option value="">— None —</option>';
|
|
$.each(fres.output || [], function(i, f) {
|
|
var sel = String(f.id) === String(selected_formula) ? ' selected' : '';
|
|
opts += '<option value="' + f.id + '"' + sel + '>' + escape_html(f.formula_name) + '</option>';
|
|
});
|
|
$('#formula_id').html(opts);
|
|
}
|
|
});
|
|
$('#btn_save_formula').prop('disabled', String(receipt.status) === '4');
|
|
|
|
var rows = '';
|
|
var invoice_total = 0;
|
|
var cn_total = 0;
|
|
|
|
$.each(receipt.allocations || [], function(i, line) {
|
|
var amount = parseFloat(line.amount || 0);
|
|
var doc_total = parseFloat(line.grand_total || 0);
|
|
var is_cn = line.doc_type === 'credit_note';
|
|
if (is_cn) cn_total += amount;
|
|
else invoice_total += amount;
|
|
|
|
rows += `<tr>
|
|
<td>
|
|
<div class="fw-semibold">${escape_html(line.invoice_number || '')}</div>
|
|
<a href="<?php echo $server_url?>revenue/manage_invoice.php?id=${line.invoice_id}" class="small">View document</a>
|
|
</td>
|
|
<td>${doc_type_badge(line.doc_type)}</td>
|
|
<td>${line.order_number ? escape_html(line.order_number) : '<span class="text-muted">—</span>'}</td>
|
|
<td class="text-end fw-semibold ${is_cn ? 'text-danger' : ''}">${format_number(is_cn ? -amount : amount, 2)}</td>
|
|
<td class="text-end ${is_cn ? 'text-danger' : ''}">${format_number(doc_total, 2)}</td>
|
|
</tr>`;
|
|
});
|
|
|
|
$('#allocation_tbody').html(rows || '<tr><td colspan="5" class="text-center py-5 text-muted">No allocations found.</td></tr>');
|
|
$('#summary_invoice').text(format_number(invoice_total, 2));
|
|
$('#summary_cn').text('-' + format_number(cn_total, 2));
|
|
$('#summary_cash').text(format_number(invoice_total - cn_total, 2));
|
|
}
|
|
|
|
function save_formula() {
|
|
ajax_request({
|
|
url: '<?php echo $server_url?>finance/api/engine/manage_receipt.php',
|
|
autoPrepare: true,
|
|
checkRequired: 0,
|
|
action: 'save_formula',
|
|
data: { id: receipt_id, formula_id: $('#formula_id').val() || null },
|
|
onSuccess: function() {
|
|
bootbox.alert({ message: 'GL formula saved.', size: 'small' });
|
|
}
|
|
});
|
|
}
|
|
|
|
function delete_receipt() {
|
|
bootbox.confirm({
|
|
message: 'Delete this receipt? The GL entry and invoice allocations will be reversed.',
|
|
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?>finance/api/engine/delete_receipt.php',
|
|
autoPrepare: false,
|
|
checkRequired: 0,
|
|
action: 'delete',
|
|
data: { id: receipt_id },
|
|
onSuccess: function() { window.location.href = '<?php echo $server_url?>finance/receipt.php'; }
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
function void_receipt() {
|
|
bootbox.confirm('Void this receipt? Allocated invoices and credit notes will be reopened if not fully settled by other receipts.', function(ok) {
|
|
if (!ok) return;
|
|
ajax_request({
|
|
url: '<?php echo $server_url?>finance/api/engine/manage_receipt.php',
|
|
autoPrepare: true,
|
|
checkRequired: 0,
|
|
action: 'void',
|
|
data: { id: receipt_id },
|
|
onSuccess: function() {
|
|
retrieve_receipt();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
$(function() {
|
|
if (!receipt_id) {
|
|
bootbox.alert('Receipt id is required.');
|
|
return;
|
|
}
|
|
retrieve_receipt();
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|