accounting workflows
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require_role($user_role, ['owner', 'admin', 'staff']);
|
||||
require '../../../assets/utils/classes/PaymentManager.php';
|
||||
|
||||
try {
|
||||
$mgr = new PaymentManager($pdo2, $company_id);
|
||||
$action = $data['action'] ?? ($_POST['action'] ?? '');
|
||||
|
||||
if ($action === 'open_documents') {
|
||||
$contact_id = (int)($data['contact_id'] ?? 0);
|
||||
$answer['output'] = $mgr->getOpenDocuments($contact_id);
|
||||
$answer['success'] = 1;
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'save_formula') {
|
||||
$id = (int)($data['id'] ?? 0);
|
||||
$formula_id = isset($data['formula_id']) && (int)$data['formula_id'] > 0 ? (int)$data['formula_id'] : null;
|
||||
$mgr->saveFormula($id, $formula_id);
|
||||
$answer['success'] = 1;
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'retrieve') {
|
||||
$payment_id = (int)($data['id'] ?? $data['payment_id'] ?? 0);
|
||||
$answer['output'] = $mgr->getPaymentById($payment_id);
|
||||
$answer['success'] = 1;
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'void') {
|
||||
$payment_id = (int)($data['id'] ?? $data['payment_id'] ?? 0);
|
||||
dbTransaction($pdo2, function($pdo) use ($payment_id, $company_id, $logging) {
|
||||
$mgr = new PaymentManager($pdo, $company_id);
|
||||
$mgr->voidPayment($payment_id, $logging);
|
||||
});
|
||||
$answer['success'] = 1;
|
||||
$answer['message'] = 'Payment voided.';
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'create') {
|
||||
$new_id = null;
|
||||
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging, &$new_id) {
|
||||
$mgr = new PaymentManager($pdo, $company_id);
|
||||
$new_id = $mgr->createPayment($data, $logging);
|
||||
});
|
||||
$answer['success'] = 1;
|
||||
$answer['new_id'] = $new_id;
|
||||
$answer['message'] = 'Payment posted.';
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
$answer['output'] = $mgr->getPaymentList();
|
||||
$answer['success'] = 1;
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$answer['message'] = 'Database error, please try again.';
|
||||
http_response_code(500);
|
||||
|
||||
} catch (Exception $e) {
|
||||
$answer['message'] = $e->getMessage();
|
||||
http_response_code(400);
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require_role($user_role, ['owner', 'admin', 'staff']);
|
||||
require '../../../assets/utils/classes/PaymentBillingManager.php';
|
||||
|
||||
try {
|
||||
$mgr = new PaymentBillingManager($pdo2, $company_id);
|
||||
$action = $data['action'] ?? ($_POST['action'] ?? '');
|
||||
|
||||
if ($action === 'billable_documents') {
|
||||
$contact_id = (int)($data['contact_id'] ?? 0);
|
||||
$answer['output'] = $mgr->getBillableDocuments($contact_id);
|
||||
$answer['success'] = 1;
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'open_billings') {
|
||||
$contact_id = (int)($data['contact_id'] ?? 0);
|
||||
$answer['output'] = $mgr->getOpenBillingList($contact_id);
|
||||
$answer['success'] = 1;
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'retrieve') {
|
||||
$billing_id = (int)($data['id'] ?? $data['billing_id'] ?? 0);
|
||||
$answer['output'] = $mgr->getBillingById($billing_id);
|
||||
$answer['success'] = 1;
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'void') {
|
||||
$billing_id = (int)($data['id'] ?? $data['billing_id'] ?? 0);
|
||||
dbTransaction($pdo2, function($pdo) use ($billing_id, $company_id, $logging) {
|
||||
$mgr = new PaymentBillingManager($pdo, $company_id);
|
||||
$mgr->voidBilling($billing_id, $logging);
|
||||
});
|
||||
$answer['success'] = 1;
|
||||
$answer['message'] = 'Payment billing voided.';
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'create') {
|
||||
$new_id = null;
|
||||
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging, &$new_id) {
|
||||
$mgr = new PaymentBillingManager($pdo, $company_id);
|
||||
$new_id = $mgr->createBilling($data, $logging);
|
||||
});
|
||||
$answer['success'] = 1;
|
||||
$answer['new_id'] = $new_id;
|
||||
$answer['message'] = 'Payment billing created.';
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
$answer['output'] = $mgr->getBillingList();
|
||||
$answer['success'] = 1;
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$answer['message'] = 'Database error, please try again.';
|
||||
http_response_code(500);
|
||||
|
||||
} catch (Exception $e) {
|
||||
$answer['message'] = $e->getMessage();
|
||||
http_response_code(400);
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require_role($user_role, ['owner', 'admin', 'staff']);
|
||||
require '../../../assets/utils/classes/ReceiptManager.php';
|
||||
|
||||
try {
|
||||
$mgr = new ReceiptManager($pdo2, $company_id);
|
||||
$action = $data['action'] ?? ($_POST['action'] ?? '');
|
||||
|
||||
if ($action === 'open_documents') {
|
||||
$contact_id = (int)($data['contact_id'] ?? 0);
|
||||
$answer['output'] = $mgr->getOpenDocuments($contact_id);
|
||||
$answer['success'] = 1;
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'save_formula') {
|
||||
$id = (int)($data['id'] ?? 0);
|
||||
$formula_id = isset($data['formula_id']) && (int)$data['formula_id'] > 0 ? (int)$data['formula_id'] : null;
|
||||
$mgr->saveFormula($id, $formula_id);
|
||||
$answer['success'] = 1;
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'retrieve') {
|
||||
$receipt_id = (int)($data['id'] ?? $data['receipt_id'] ?? 0);
|
||||
$answer['output'] = $mgr->getReceiptById($receipt_id);
|
||||
$answer['success'] = 1;
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'void') {
|
||||
$receipt_id = (int)($data['id'] ?? $data['receipt_id'] ?? 0);
|
||||
dbTransaction($pdo2, function($pdo) use ($receipt_id, $company_id, $logging) {
|
||||
$mgr = new ReceiptManager($pdo, $company_id);
|
||||
$mgr->voidReceipt($receipt_id, $logging);
|
||||
});
|
||||
$answer['success'] = 1;
|
||||
$answer['message'] = 'Receipt voided.';
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'create') {
|
||||
$new_id = null;
|
||||
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging, &$new_id) {
|
||||
$mgr = new ReceiptManager($pdo, $company_id);
|
||||
$new_id = $mgr->createReceipt($data, $logging);
|
||||
});
|
||||
$answer['success'] = 1;
|
||||
$answer['new_id'] = $new_id;
|
||||
$answer['message'] = 'Receipt posted.';
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
$answer['output'] = $mgr->getReceiptList();
|
||||
$answer['success'] = 1;
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$answer['message'] = 'Database error, please try again.';
|
||||
http_response_code(500);
|
||||
|
||||
} catch (Exception $e) {
|
||||
$answer['message'] = $e->getMessage();
|
||||
http_response_code(400);
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require_role($user_role, ['owner', 'admin', 'staff']);
|
||||
require '../../../assets/utils/classes/ReceiptBillingManager.php';
|
||||
|
||||
try {
|
||||
$mgr = new ReceiptBillingManager($pdo2, $company_id);
|
||||
$action = $data['action'] ?? ($_POST['action'] ?? '');
|
||||
|
||||
if ($action === 'billable_documents') {
|
||||
$contact_id = (int)($data['contact_id'] ?? 0);
|
||||
$answer['output'] = $mgr->getBillableDocuments($contact_id);
|
||||
$answer['success'] = 1;
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'open_billings') {
|
||||
$contact_id = (int)($data['contact_id'] ?? 0);
|
||||
$answer['output'] = $mgr->getOpenBillingList($contact_id);
|
||||
$answer['success'] = 1;
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'retrieve') {
|
||||
$billing_id = (int)($data['id'] ?? $data['billing_id'] ?? 0);
|
||||
$answer['output'] = $mgr->getBillingById($billing_id);
|
||||
$answer['success'] = 1;
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'void') {
|
||||
$billing_id = (int)($data['id'] ?? $data['billing_id'] ?? 0);
|
||||
dbTransaction($pdo2, function($pdo) use ($billing_id, $company_id, $logging) {
|
||||
$mgr = new ReceiptBillingManager($pdo, $company_id);
|
||||
$mgr->voidBilling($billing_id, $logging);
|
||||
});
|
||||
$answer['success'] = 1;
|
||||
$answer['message'] = 'Receipt billing voided.';
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
if ($action === 'create') {
|
||||
$new_id = null;
|
||||
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging, &$new_id) {
|
||||
$mgr = new ReceiptBillingManager($pdo, $company_id);
|
||||
$new_id = $mgr->createBilling($data, $logging);
|
||||
});
|
||||
$answer['success'] = 1;
|
||||
$answer['new_id'] = $new_id;
|
||||
$answer['message'] = 'Receipt billing created.';
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
$answer['output'] = $mgr->getBillingList();
|
||||
$answer['success'] = 1;
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$answer['message'] = 'Database error, please try again.';
|
||||
http_response_code(500);
|
||||
|
||||
} catch (Exception $e) {
|
||||
$answer['message'] = $e->getMessage();
|
||||
http_response_code(400);
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
@@ -0,0 +1,274 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../config.php';
|
||||
require '../include_header.php';
|
||||
|
||||
$payment_id = (int)($_GET['id'] ?? 0);
|
||||
?>
|
||||
|
||||
<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">Payment Detail</h1>
|
||||
<p class="mb-0 text-muted">Supplier payment and document allocations</p>
|
||||
</div>
|
||||
<a href="<?php echo $server_url?>finance/payment.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>PO #</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 payment...</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">Payment Info</h2>
|
||||
<div class="mb-3" id="badge_status"></div>
|
||||
<div class="mb-2 small">
|
||||
<span class="text-muted">Payment #:</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">Supplier:</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">Purchase Invoice Allocations</span>
|
||||
<span id="summary_purchase_invoice">0.00</span>
|
||||
</div>
|
||||
<div class="d-flex justify-content-between mb-2">
|
||||
<span class="text-muted">Supplier 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 Payment</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_payment()">
|
||||
<i class="ti ti-ban me-1"></i>Void Payment
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<?php require '../include_ending.php'; ?>
|
||||
|
||||
<script>
|
||||
var payment_id = <?php echo $payment_id; ?>;
|
||||
var payment = null;
|
||||
|
||||
function display_text(value) {
|
||||
return value ? escape_html(value) : '<span class="text-muted">—</span>';
|
||||
}
|
||||
|
||||
function payment_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 === 'supplier_credit_note'
|
||||
? '<span class="badge bg-warning text-dark">Supplier Credit Note</span>'
|
||||
: '<span class="badge bg-primary">Purchase Invoice</span>';
|
||||
}
|
||||
|
||||
function retrieve_payment() {
|
||||
return ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'retrieve',
|
||||
data: { id: payment_id },
|
||||
onSuccess: function(res) {
|
||||
payment = res.output || {};
|
||||
render_payment();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function render_payment() {
|
||||
$('#page_title').text(payment.payment_number || 'Payment Detail');
|
||||
$('#badge_status').html(payment_status_badge(payment.status));
|
||||
$('#display_number').html(display_text(payment.payment_number));
|
||||
$('#display_billing').html(payment.billing_number
|
||||
? `<a href="<?php echo $server_url?>finance/manage_payment_billing.php?id=${payment.payment_billing_id}">${escape_html(payment.billing_number)}</a>`
|
||||
: '<span class="text-muted">—</span>');
|
||||
$('#display_contact').html(display_text(payment.contact_name));
|
||||
$('#display_date').html(payment.payment_date ? format_date(payment.payment_date) : '<span class="text-muted">—</span>');
|
||||
$('#display_method').html(display_text(method_label(payment.payment_method)));
|
||||
$('#display_notes').html(payment.notes ? escape_html(payment.notes).replace(/\n/g, '<br>') : '<span class="text-muted">—</span>');
|
||||
$('#btn_void').toggleClass('d-none', String(payment.status) === '4');
|
||||
|
||||
var selected_formula = payment.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: 'payment' },
|
||||
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(payment.status) === '4');
|
||||
|
||||
var rows = '';
|
||||
var purchase_invoice_total = 0;
|
||||
var cn_total = 0;
|
||||
|
||||
$.each(payment.allocations || [], function(i, line) {
|
||||
var amount = parseFloat(line.amount || 0);
|
||||
var doc_total = parseFloat(line.grand_total || 0);
|
||||
var is_cn = line.doc_type === 'supplier_credit_note';
|
||||
if (is_cn) cn_total += amount;
|
||||
else purchase_invoice_total += amount;
|
||||
|
||||
rows += `<tr>
|
||||
<td>
|
||||
<div class="fw-semibold">${escape_html(line.invoice_number || '')}</div>
|
||||
<a href="<?php echo $server_url?>expense/manage_purchase_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_purchase_invoice').text(format_number(purchase_invoice_total, 2));
|
||||
$('#summary_cn').text('-' + format_number(cn_total, 2));
|
||||
$('#summary_cash').text(format_number(purchase_invoice_total - cn_total, 2));
|
||||
}
|
||||
|
||||
function save_formula() {
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'save_formula',
|
||||
data: { id: payment_id, formula_id: $('#formula_id').val() || null },
|
||||
onSuccess: function() {
|
||||
bootbox.alert({ message: 'GL formula saved.', size: 'small' });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function void_payment() {
|
||||
bootbox.confirm('Void this payment? Allocated purchase invoices and supplier credit notes will be reopened if not fully settled by other payments.', function(ok) {
|
||||
if (!ok) return;
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'void',
|
||||
data: { id: payment_id },
|
||||
onSuccess: function() {
|
||||
retrieve_payment();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
$(function() {
|
||||
if (!payment_id) {
|
||||
bootbox.alert('Payment id is required.');
|
||||
return;
|
||||
}
|
||||
retrieve_payment();
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../config.php';
|
||||
require '../include_header.php';
|
||||
|
||||
$billing_id = (int)($_GET['id'] ?? 0);
|
||||
?>
|
||||
|
||||
<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">Payment Billing Detail</h1>
|
||||
<p class="mb-0 text-muted">Supplier payment billing and document allocations</p>
|
||||
</div>
|
||||
<a href="<?php echo $server_url?>finance/payment_billing.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">Documents</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table mb-0">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
<th>Document</th>
|
||||
<th>Type</th>
|
||||
<th>PO #</th>
|
||||
<th class="text-end">Included</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 payment billing...</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">Billing Info</h2>
|
||||
<div class="mb-3" id="badge_status"></div>
|
||||
<div class="mb-2 small"><span class="text-muted">Billing #:</span><span class="ms-1 fw-semibold" id="display_number">—</span></div>
|
||||
<div class="mb-2 small"><span class="text-muted">Supplier:</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="mt-3 small">
|
||||
<span class="text-muted">Notes:</span>
|
||||
<div class="mt-1" id="display_notes">—</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">Amount Due</span><span id="summary_amount">0.00</span></div>
|
||||
<div class="d-flex justify-content-between mb-2"><span class="text-muted">Paid</span><span id="summary_payment">0.00</span></div>
|
||||
<hr>
|
||||
<div class="d-flex justify-content-between fw-bold fs-5"><span>Balance</span><span id="summary_balance">0.00</span></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-body p-4 d-flex flex-column gap-2">
|
||||
<a href="#" class="btn btn-primary w-100" id="btn_payment">
|
||||
<i class="ti ti-cash me-1"></i>Create Payment
|
||||
</a>
|
||||
<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_billing()">
|
||||
<i class="ti ti-ban me-1"></i>Void Billing
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<?php require '../include_ending.php'; ?>
|
||||
|
||||
<script>
|
||||
var billing_id = <?php echo $billing_id; ?>;
|
||||
var billing = null;
|
||||
|
||||
function display_text(value) {
|
||||
return value ? escape_html(value) : '<span class="text-muted">—</span>';
|
||||
}
|
||||
|
||||
function status_badge(status) {
|
||||
const map = {
|
||||
'1': '<span class="badge bg-info text-white">Open</span>',
|
||||
'2': '<span class="badge bg-success">Settled</span>',
|
||||
'3': '<span class="badge bg-warning text-dark">Partial</span>',
|
||||
'4': '<span class="badge bg-light text-dark">Void</span>',
|
||||
};
|
||||
return map[String(status)] || '—';
|
||||
}
|
||||
|
||||
function doc_type_badge(doc_type) {
|
||||
return doc_type === 'supplier_credit_note'
|
||||
? '<span class="badge bg-warning text-dark">Supplier Credit Note</span>'
|
||||
: '<span class="badge bg-primary">Purchase Invoice</span>';
|
||||
}
|
||||
|
||||
function retrieve_billing() {
|
||||
return ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment_billing.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'retrieve',
|
||||
data: { id: billing_id },
|
||||
onSuccess: function(res) {
|
||||
billing = res.output || {};
|
||||
render_billing();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function render_billing() {
|
||||
$('#page_title').text(billing.billing_number || 'Payment Billing Detail');
|
||||
$('#badge_status').html(status_badge(billing.status));
|
||||
$('#display_number').html(display_text(billing.billing_number));
|
||||
$('#display_contact').html(display_text(billing.contact_name));
|
||||
$('#display_date').html(billing.billing_date ? format_date(billing.billing_date) : '<span class="text-muted">—</span>');
|
||||
$('#display_notes').html(billing.notes ? escape_html(billing.notes).replace(/\n/g, '<br>') : '<span class="text-muted">—</span>');
|
||||
$('#summary_amount').text(format_number(billing.amount, 2));
|
||||
$('#summary_payment').text(format_number(billing.paid_amount, 2));
|
||||
$('#summary_balance').text(format_number(billing.balance_amount, 2));
|
||||
$('#btn_payment').attr('href', '<?php echo $server_url?>finance/payment.php?billing_id=' + billing.id);
|
||||
$('#btn_payment').toggleClass('d-none', String(billing.status) === '4' || parseFloat(billing.balance_amount || 0) <= 0.0001);
|
||||
$('#btn_void').toggleClass('d-none', String(billing.status) === '4' || parseFloat(billing.paid_amount || 0) > 0.0001);
|
||||
|
||||
var rows = '';
|
||||
$.each(billing.allocations || [], function(i, line) {
|
||||
var is_cn = line.doc_type === 'supplier_credit_note';
|
||||
var included = parseFloat(line.amount || 0);
|
||||
var doc_total = parseFloat(line.grand_total || 0);
|
||||
rows += `<tr>
|
||||
<td>
|
||||
<div class="fw-semibold">${escape_html(line.invoice_number || '')}</div>
|
||||
<a href="<?php echo $server_url?>expense/manage_purchase_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 ? -included : included, 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 documents found.</td></tr>');
|
||||
}
|
||||
|
||||
function void_billing() {
|
||||
bootbox.confirm('Void this payment billing?', function(ok) {
|
||||
if (!ok) return;
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment_billing.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'void',
|
||||
data: { id: billing_id },
|
||||
onSuccess: function() {
|
||||
retrieve_billing();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
$(function() {
|
||||
if (!billing_id) {
|
||||
bootbox.alert('Payment billing id is required.');
|
||||
return;
|
||||
}
|
||||
retrieve_billing();
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,274 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../config.php';
|
||||
require '../include_header.php';
|
||||
|
||||
$receipt_id = (int)($_GET['id'] ?? 0);
|
||||
?>
|
||||
|
||||
<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>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<?php require '../include_ending.php'; ?>
|
||||
|
||||
<script>
|
||||
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');
|
||||
|
||||
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 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>
|
||||
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../config.php';
|
||||
require '../include_header.php';
|
||||
|
||||
$billing_id = (int)($_GET['id'] ?? 0);
|
||||
?>
|
||||
|
||||
<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 Billing Detail</h1>
|
||||
<p class="mb-0 text-muted">Customer collection bill and document allocations</p>
|
||||
</div>
|
||||
<a href="<?php echo $server_url?>finance/receipt_billing.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">Documents</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">Included</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 billing...</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">Billing Info</h2>
|
||||
<div class="mb-3" id="badge_status"></div>
|
||||
<div class="mb-2 small"><span class="text-muted">Billing #:</span><span class="ms-1 fw-semibold" id="display_number">—</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="mt-3 small">
|
||||
<span class="text-muted">Notes:</span>
|
||||
<div class="mt-1" id="display_notes">—</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">Amount Due</span><span id="summary_amount">0.00</span></div>
|
||||
<div class="d-flex justify-content-between mb-2"><span class="text-muted">Received</span><span id="summary_received">0.00</span></div>
|
||||
<hr>
|
||||
<div class="d-flex justify-content-between fw-bold fs-5"><span>Balance</span><span id="summary_balance">0.00</span></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-body p-4 d-flex flex-column gap-2">
|
||||
<a href="#" class="btn btn-primary w-100" id="btn_receipt">
|
||||
<i class="ti ti-cash me-1"></i>Create Receipt
|
||||
</a>
|
||||
<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_billing()">
|
||||
<i class="ti ti-ban me-1"></i>Void Billing
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<?php require '../include_ending.php'; ?>
|
||||
|
||||
<script>
|
||||
var billing_id = <?php echo $billing_id; ?>;
|
||||
var billing = null;
|
||||
|
||||
function display_text(value) {
|
||||
return value ? escape_html(value) : '<span class="text-muted">—</span>';
|
||||
}
|
||||
|
||||
function status_badge(status) {
|
||||
const map = {
|
||||
'1': '<span class="badge bg-info text-white">Open</span>',
|
||||
'2': '<span class="badge bg-success">Settled</span>',
|
||||
'3': '<span class="badge bg-warning text-dark">Partial</span>',
|
||||
'4': '<span class="badge bg-light text-dark">Void</span>',
|
||||
};
|
||||
return map[String(status)] || '—';
|
||||
}
|
||||
|
||||
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_billing() {
|
||||
return ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_receipt_billing.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'retrieve',
|
||||
data: { id: billing_id },
|
||||
onSuccess: function(res) {
|
||||
billing = res.output || {};
|
||||
render_billing();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function render_billing() {
|
||||
$('#page_title').text(billing.billing_number || 'Receipt Billing Detail');
|
||||
$('#badge_status').html(status_badge(billing.status));
|
||||
$('#display_number').html(display_text(billing.billing_number));
|
||||
$('#display_contact').html(display_text(billing.contact_name));
|
||||
$('#display_date').html(billing.billing_date ? format_date(billing.billing_date) : '<span class="text-muted">—</span>');
|
||||
$('#display_notes').html(billing.notes ? escape_html(billing.notes).replace(/\n/g, '<br>') : '<span class="text-muted">—</span>');
|
||||
$('#summary_amount').text(format_number(billing.amount, 2));
|
||||
$('#summary_received').text(format_number(billing.received_amount, 2));
|
||||
$('#summary_balance').text(format_number(billing.balance_amount, 2));
|
||||
$('#btn_receipt').attr('href', '<?php echo $server_url?>finance/receipt.php?billing_id=' + billing.id);
|
||||
$('#btn_receipt').toggleClass('d-none', String(billing.status) === '4' || parseFloat(billing.balance_amount || 0) <= 0.0001);
|
||||
$('#btn_void').toggleClass('d-none', String(billing.status) === '4' || parseFloat(billing.received_amount || 0) > 0.0001);
|
||||
|
||||
var rows = '';
|
||||
$.each(billing.allocations || [], function(i, line) {
|
||||
var is_cn = line.doc_type === 'credit_note';
|
||||
var included = parseFloat(line.amount || 0);
|
||||
var doc_total = parseFloat(line.grand_total || 0);
|
||||
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 ? -included : included, 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 documents found.</td></tr>');
|
||||
}
|
||||
|
||||
function void_billing() {
|
||||
bootbox.confirm('Void this receipt billing?', function(ok) {
|
||||
if (!ok) return;
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_receipt_billing.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'void',
|
||||
data: { id: billing_id },
|
||||
onSuccess: function() {
|
||||
retrieve_billing();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
$(function() {
|
||||
if (!billing_id) {
|
||||
bootbox.alert('Receipt billing id is required.');
|
||||
return;
|
||||
}
|
||||
retrieve_billing();
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
+458
-6
@@ -2,6 +2,8 @@
|
||||
session_start();
|
||||
require '../config.php';
|
||||
require '../include_header.php';
|
||||
|
||||
$prefill_billing_id = (int)($_GET['billing_id'] ?? 0);
|
||||
?>
|
||||
|
||||
<body>
|
||||
@@ -13,16 +15,141 @@
|
||||
|
||||
<div class="row mb-6">
|
||||
<div class="col-12">
|
||||
<h1 class="fs-3 mb-1">Payments</h1>
|
||||
<p class="mb-0 text-muted">Cash and bank payments</p>
|
||||
<div class="d-flex justify-content-between align-items-center">
|
||||
<div>
|
||||
<h1 class="fs-3 mb-1">Payments</h1>
|
||||
<p class="mb-0 text-muted">Supplier payments against payment billings</p>
|
||||
</div>
|
||||
<button class="btn btn-primary" onclick="show_payment_form()">
|
||||
<i class="ti ti-plus me-1"></i>New Payment
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="row g-5 d-none" id="payment_form">
|
||||
<div class="col-lg-8">
|
||||
<div class="card mb-5">
|
||||
<div class="card-body p-5">
|
||||
<h2 class="fs-5 mb-4">Payment Information</h2>
|
||||
<div class="row gx-5">
|
||||
<div class="mb-3 col-lg-6">
|
||||
<label class="form-label">Supplier</label>
|
||||
<input type="text" id="contact" class="form-control contact_search"
|
||||
placeholder="Search supplier..." autocomplete="off">
|
||||
<input type="hidden" id="contact_id">
|
||||
</div>
|
||||
<div class="mb-3 col-lg-3">
|
||||
<label class="form-label">Payment Date</label>
|
||||
<input type="text" id="payment_date" class="form-control" placeholder="DD/MM/YYYY">
|
||||
</div>
|
||||
<div class="mb-3 col-lg-3">
|
||||
<label class="form-label">Method</label>
|
||||
<select id="payment_method" class="form-select">
|
||||
<option value="bank_transfer">Bank Transfer</option>
|
||||
<option value="cash">Cash</option>
|
||||
<option value="cheque">Cheque</option>
|
||||
<option value="card">Card</option>
|
||||
<option value="other">Other</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3 col-lg-4">
|
||||
<label class="form-label">GL Formula</label>
|
||||
<select id="formula_id" class="form-select">
|
||||
<option value="">— None —</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3 col-lg-4">
|
||||
<label class="form-label">Department</label>
|
||||
<select id="department_id" class="form-select">
|
||||
<option value="0">— No Department —</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3 col-lg-12">
|
||||
<label class="form-label">Notes</label>
|
||||
<textarea id="notes" class="form-control" rows="2"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn btn-outline-secondary" onclick="load_open_billings()">
|
||||
<i class="ti ti-search me-1"></i>Load Open Billings
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-body p-5">
|
||||
<h2 class="fs-5 mb-4">Payment Billings</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table mb-0" id="allocation_table">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
<th>Document / Billing</th>
|
||||
<th>Type / Date</th>
|
||||
<th>Department</th>
|
||||
<th>Balance</th>
|
||||
<th style="width:150px;">Pay / Use</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="allocation_tbody">
|
||||
<tr><td colspan="5" class="text-center py-5 text-muted">Select a supplier and load open billings.</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">Summary</h2>
|
||||
<div class="d-flex justify-content-between mb-2">
|
||||
<span class="text-muted">Selected Billings</span>
|
||||
<span id="summary_billing">0.00</span>
|
||||
</div>
|
||||
<hr>
|
||||
<div class="d-flex justify-content-between fw-bold fs-5">
|
||||
<span>Cash Payment</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-primary w-100" onclick="post_payment()">
|
||||
<i class="ti ti-check me-1"></i>Post Payment
|
||||
</button>
|
||||
<button class="btn btn-light w-100" onclick="hide_payment_form()">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row mt-5" id="payment_list_section">
|
||||
<div class="col-12">
|
||||
<div class="card p-5 text-center text-muted">
|
||||
<i class="ti ti-credit-card fs-1 mb-3 d-block"></i>
|
||||
<p class="mb-0">Payments coming soon.</p>
|
||||
<div class="card">
|
||||
<div class="card-body p-5">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover mb-0 table-centered" id="payment_list">
|
||||
<thead class="table-primary border-light">
|
||||
<tr>
|
||||
<th>Payment #</th>
|
||||
<th>Billing #</th>
|
||||
<th>Date</th>
|
||||
<th>Supplier</th>
|
||||
<th>Method</th>
|
||||
<th>Amount</th>
|
||||
<th>GL Formula</th>
|
||||
<th>Status</th>
|
||||
<th>Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody></tbody>
|
||||
<tfoot></tfoot>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -32,5 +159,330 @@
|
||||
|
||||
<?php require '../include_ending.php'; ?>
|
||||
|
||||
<script>
|
||||
var open_billings = [];
|
||||
var prefill_billing_id = <?php echo $prefill_billing_id; ?>;
|
||||
var formula_map = {};
|
||||
|
||||
function load_formula_map() {
|
||||
return ajax_request({
|
||||
url: '<?php echo $server_url?>accounting/api/engine/account_formula.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'read',
|
||||
data: {},
|
||||
onSuccess: function(res) {
|
||||
formula_map = {};
|
||||
$.each(res.output || [], function(i, f) { formula_map[f.id] = f.formula_name; });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function show_payment_form() {
|
||||
$('#payment_form').removeClass('d-none');
|
||||
$('#payment_date').val(flatpickr.formatDate(new Date(), 'd/m/Y'));
|
||||
ajax_request({
|
||||
url: server_url + 'accounting/api/engine/account_formula.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'get_by_type',
|
||||
data: { document_type: 'payment' },
|
||||
onSuccess: function(fres) {
|
||||
var opts = '<option value="">— None —</option>';
|
||||
$.each(fres.output || [], function(i, f) {
|
||||
opts += '<option value="' + f.id + '">' + escape_html(f.formula_name) + '</option>';
|
||||
});
|
||||
$('#formula_id').html(opts);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function hide_payment_form() {
|
||||
$('#payment_form').addClass('d-none');
|
||||
}
|
||||
|
||||
function payment_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 load_payments() {
|
||||
return ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'read',
|
||||
data: { action: 'list' },
|
||||
onSuccess: function(res) {
|
||||
var data = res.output || [];
|
||||
alasql('DROP TABLE IF EXISTS payment_list');
|
||||
alasql('CREATE TABLE payment_list');
|
||||
alasql.tables.payment_list.data = data;
|
||||
$('table#payment_list tfoot').html(generate_pagination('payment_list', data.length));
|
||||
change_page_payment_list(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function change_page_payment_list(page_num) {
|
||||
var offset = (page_num - 1) * prop_limit;
|
||||
var rows = alasql(`SELECT * FROM payment_list LIMIT ${prop_limit} OFFSET ${offset}`);
|
||||
var body = '';
|
||||
|
||||
if (!rows.length) {
|
||||
$('table#payment_list tbody').html('<tr><td colspan="9" class="text-center py-5 text-muted">No payments found.</td></tr>');
|
||||
return;
|
||||
}
|
||||
|
||||
$.each(rows, function(i, r) {
|
||||
var formula_name = r.formula_id && formula_map[r.formula_id]
|
||||
? escape_html(formula_map[r.formula_id])
|
||||
: '<span class="text-muted">—</span>';
|
||||
body += `<tr>
|
||||
<td class="py-3 fw-semibold">${escape_html(r.payment_number || '')}</td>
|
||||
<td class="py-3">${escape_html(r.billing_number || '—')}</td>
|
||||
<td class="py-3">${r.payment_date ? format_date(r.payment_date) : '<span class="text-muted">—</span>'}</td>
|
||||
<td class="py-3">${escape_html(r.contact_name || '—')}</td>
|
||||
<td class="py-3">${escape_html(method_label(r.payment_method) || '—')}</td>
|
||||
<td class="py-3 fw-semibold">${format_number(r.amount, 2)}</td>
|
||||
<td class="py-3 small">${formula_name}</td>
|
||||
<td class="py-3">${payment_status_badge(r.status)}</td>
|
||||
<td class="py-3">
|
||||
<a href="<?php echo $server_url?>finance/manage_payment.php?id=${r.id}" title="View payment">
|
||||
<i class="ti ti-eye fs-5"></i>
|
||||
</a>
|
||||
</td>
|
||||
</tr>`;
|
||||
});
|
||||
$('table#payment_list tbody').html(body);
|
||||
}
|
||||
|
||||
function load_open_billings() {
|
||||
var contact_id = parseInt($('#contact_id').val() || $('#contact').attr('data-id')) || 0;
|
||||
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment_billing.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'open_billings',
|
||||
data: { contact_id: contact_id },
|
||||
onSuccess: function(res) {
|
||||
open_billings = res.output || [];
|
||||
render_open_billings();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function render_open_billings() {
|
||||
var body = '';
|
||||
if (!open_billings.length) {
|
||||
$('#allocation_tbody').html('<tr><td colspan="5" class="text-center py-5 text-muted">No open payment billings for this supplier.</td></tr>');
|
||||
recalc_payment();
|
||||
return;
|
||||
}
|
||||
|
||||
if (open_billings.length === 1 && Array.isArray(open_billings[0].allocations)) {
|
||||
var billing = open_billings[0];
|
||||
$.each(billing.allocations || [], function(i, line) {
|
||||
var balance = parseFloat(line.balance_amount || 0);
|
||||
if (balance <= 0.0001) return;
|
||||
var is_cn = line.doc_type === 'supplier_credit_note';
|
||||
var display_balance = is_cn ? -balance : balance;
|
||||
body += `<tr data-billing-id="${billing.id}" data-doc-id="${line.invoice_id}" data-doc-type="${line.doc_type}" data-department-id="${line.department_id || 0}" data-balance="${balance}">
|
||||
<td>
|
||||
<div class="fw-semibold">${escape_html(line.invoice_number || '')}</div>
|
||||
<div class="small text-muted">${escape_html(billing.billing_number || '')}</div>
|
||||
</td>
|
||||
<td>${is_cn ? '<span class="badge bg-warning text-dark">Supplier Credit Note</span>' : '<span class="badge bg-primary">Purchase Invoice</span>'}</td>
|
||||
<td>${get_dept_label(line.department_id)}</td>
|
||||
<td class="fw-semibold ${is_cn ? 'text-danger' : ''}">${format_number(display_balance, 2)}</td>
|
||||
<td>
|
||||
<input type="number" class="form-control form-control-sm payment_amount"
|
||||
${is_cn ? `min="${-balance}" max="0" value="0"` : `min="0" max="${balance}" value="0"`}
|
||||
step="any" oninput="recalc_payment()">
|
||||
</td>
|
||||
</tr>`;
|
||||
});
|
||||
$('#allocation_tbody').html(body || '<tr><td colspan="5" class="text-center py-5 text-muted">No remaining billing lines.</td></tr>');
|
||||
recalc_payment();
|
||||
return;
|
||||
}
|
||||
|
||||
$.each(open_billings, function(i, d) {
|
||||
var balance = parseFloat(d.balance_amount || 0);
|
||||
body += `<tr data-billing-id="${d.id}" data-balance="${balance}">
|
||||
<td>
|
||||
<div class="fw-semibold">${escape_html(d.billing_number || '')}</div>
|
||||
<div class="small text-muted">Amount Due ${format_number(d.amount, 2)}</div>
|
||||
</td>
|
||||
<td>${d.billing_date ? format_date(d.billing_date) : '<span class="text-muted">—</span>'}</td>
|
||||
<td><span class="text-muted">—</span></td>
|
||||
<td class="fw-semibold">${format_number(balance, 2)}</td>
|
||||
<td>
|
||||
<input type="number" class="form-control form-control-sm payment_amount"
|
||||
min="0" max="${balance}" step="any" value="0" oninput="recalc_payment()">
|
||||
</td>
|
||||
</tr>`;
|
||||
});
|
||||
$('#allocation_tbody').html(body);
|
||||
recalc_payment();
|
||||
}
|
||||
|
||||
function recalc_payment() {
|
||||
var purchase_invoice_total = 0;
|
||||
var cn_total = 0;
|
||||
|
||||
$('#allocation_tbody tr[data-billing-id]').each(function() {
|
||||
var amount = parseFloat($(this).find('.payment_amount').val()) || 0;
|
||||
if ($(this).data('doc-id')) {
|
||||
if ($(this).data('doc-type') === 'supplier_credit_note') cn_total += Math.abs(amount);
|
||||
else purchase_invoice_total += Math.abs(amount);
|
||||
} else {
|
||||
purchase_invoice_total += amount;
|
||||
}
|
||||
});
|
||||
|
||||
var payment_total = purchase_invoice_total - cn_total;
|
||||
$('#summary_billing').text(format_number(payment_total, 2));
|
||||
$('#summary_cash').text(format_number(payment_total, 2));
|
||||
}
|
||||
|
||||
function collect_payment_billing() {
|
||||
var selected = [];
|
||||
$('#allocation_tbody tr[data-billing-id]').each(function() {
|
||||
var amount = parseFloat($(this).find('.payment_amount').val()) || 0;
|
||||
var balance = parseFloat($(this).data('balance')) || 0;
|
||||
var abs_amount = Math.abs(amount);
|
||||
if (abs_amount <= 0) return;
|
||||
if (abs_amount - balance > 0.0001) {
|
||||
throw new Error('Payment amount exceeds billing balance.');
|
||||
}
|
||||
if ($(this).data('doc-id')) {
|
||||
selected.push({
|
||||
payment_billing_id: parseInt($(this).data('billing-id')),
|
||||
invoice_id: parseInt($(this).data('doc-id')),
|
||||
department_id: parseInt($(this).data('department-id')) || 0,
|
||||
amount: abs_amount,
|
||||
});
|
||||
return;
|
||||
}
|
||||
selected.push({
|
||||
payment_billing_id: parseInt($(this).data('billing-id')),
|
||||
amount: abs_amount,
|
||||
});
|
||||
});
|
||||
var billing_ids = [...new Set(selected.map(row => row.payment_billing_id))];
|
||||
if (billing_ids.length > 1) {
|
||||
throw new Error('Please post one payment billing per payment.');
|
||||
}
|
||||
if (!selected.length) return null;
|
||||
if (selected[0].invoice_id) {
|
||||
var purchase_invoice_total = 0;
|
||||
var cn_total = 0;
|
||||
selected.forEach(function(row) {
|
||||
var $tr = $(`#allocation_tbody tr[data-doc-id="${row.invoice_id}"]`);
|
||||
if ($tr.data('doc-type') === 'supplier_credit_note') cn_total += row.amount;
|
||||
else purchase_invoice_total += row.amount;
|
||||
});
|
||||
if (purchase_invoice_total - cn_total <= 0.0001) {
|
||||
throw new Error('Cash payment amount must be greater than zero.');
|
||||
}
|
||||
return {
|
||||
payment_billing_id: billing_ids[0],
|
||||
amount: purchase_invoice_total - cn_total,
|
||||
allocations: selected.map(row => ({ invoice_id: row.invoice_id, department_id: row.department_id || 0, amount: row.amount })),
|
||||
};
|
||||
}
|
||||
if (selected[0].amount <= 0.0001) {
|
||||
throw new Error('Payment amount must be greater than zero.');
|
||||
}
|
||||
return selected[0];
|
||||
}
|
||||
|
||||
function post_payment() {
|
||||
var billing;
|
||||
try {
|
||||
billing = collect_payment_billing();
|
||||
} catch(e) {
|
||||
bootbox.alert(e.message);
|
||||
return;
|
||||
}
|
||||
if (!billing) {
|
||||
bootbox.alert('Please enter a payment amount for one payment billing.');
|
||||
return;
|
||||
}
|
||||
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'create',
|
||||
data: {
|
||||
payment_billing_id: billing.payment_billing_id,
|
||||
amount: billing.amount,
|
||||
allocations: billing.allocations ? JSON.stringify(billing.allocations) : '',
|
||||
payment_date: to_iso_date($('#payment_date').val()),
|
||||
payment_method: $('#payment_method').val(),
|
||||
formula_id: $('#formula_id').val() || null,
|
||||
department_id: $('#department_id').val() || 0,
|
||||
notes: $('#notes').val(),
|
||||
},
|
||||
onSuccess: function(res) {
|
||||
if (prefill_billing_id > 0 && res.new_id) {
|
||||
window.location.href = '<?php echo $server_url?>finance/manage_payment.php?id=' + res.new_id;
|
||||
return;
|
||||
}
|
||||
hide_payment_form();
|
||||
$('#contact, #contact_id, #notes').val('');
|
||||
$('#allocation_tbody').html('<tr><td colspan="5" class="text-center py-5 text-muted">Select a supplier and load open billings.</td></tr>');
|
||||
load_payments();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$(document).on('contact:selected', '#contact', function(e, contact) {
|
||||
$('#contact_id').val(contact.id);
|
||||
$('#contact').attr('data-id', contact.id);
|
||||
});
|
||||
|
||||
$(document).on('input', '#contact', function() {
|
||||
$('#contact_id').val('');
|
||||
$(this).removeAttr('data-id');
|
||||
});
|
||||
|
||||
$(async function() {
|
||||
flatpickr('#payment_date', { dateFormat: 'd/m/Y', allowInput: true });
|
||||
load_departments('department_id');
|
||||
await load_formula_map();
|
||||
load_payments();
|
||||
if (prefill_billing_id > 0) {
|
||||
$('#payment_list_section').addClass('d-none');
|
||||
show_payment_form();
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment_billing.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
queueLock: false,
|
||||
action: 'retrieve',
|
||||
data: { id: prefill_billing_id },
|
||||
onSuccess: function(res) {
|
||||
var billing = res.output || {};
|
||||
$('#contact').val(billing.contact_name || '').attr('data-id', billing.contact_id || '');
|
||||
$('#contact_id').val(billing.contact_id || '');
|
||||
open_billings = parseFloat(billing.balance_amount || 0) > 0.0001 ? [billing] : [];
|
||||
render_open_billings();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,357 @@
|
||||
<?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">Payment Billings</h1>
|
||||
<p class="mb-0 text-muted">Supplier payment billings from purchase invoices and supplier credit notes</p>
|
||||
</div>
|
||||
<button class="btn btn-primary" onclick="show_billing_form()">
|
||||
<i class="ti ti-plus me-1"></i>New Payment Billing
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row g-5 d-none" id="billing_form">
|
||||
<div class="col-lg-8">
|
||||
<div class="card mb-5">
|
||||
<div class="card-body p-5">
|
||||
<h2 class="fs-5 mb-4">Billing Information</h2>
|
||||
<div class="row gx-5">
|
||||
<div class="mb-3 col-lg-8">
|
||||
<label class="form-label">Supplier</label>
|
||||
<input type="text" id="contact" class="form-control contact_search"
|
||||
placeholder="Search supplier..." autocomplete="off">
|
||||
<input type="hidden" id="contact_id">
|
||||
</div>
|
||||
<div class="mb-3 col-lg-4">
|
||||
<label class="form-label">Billing Date</label>
|
||||
<input type="text" id="billing_date" class="form-control" placeholder="DD/MM/YYYY">
|
||||
</div>
|
||||
<div class="mb-3 col-12">
|
||||
<label class="form-label">Notes</label>
|
||||
<textarea id="notes" class="form-control" rows="2"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn btn-outline-secondary" onclick="load_billable_documents()">
|
||||
<i class="ti ti-search me-1"></i>Load Billable Documents
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-body p-5">
|
||||
<h2 class="fs-5 mb-4">Documents</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table mb-0" id="document_table">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
<th>Document</th>
|
||||
<th>Type</th>
|
||||
<th>Billable</th>
|
||||
<th style="width:150px;">Include</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="document_tbody">
|
||||
<tr><td colspan="4" class="text-center py-5 text-muted">Select a supplier and load billable documents.</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">Summary</h2>
|
||||
<div class="d-flex justify-content-between mb-2">
|
||||
<span class="text-muted">Purchase Invoices</span>
|
||||
<span id="summary_purchase_invoice">0.00</span>
|
||||
</div>
|
||||
<div class="d-flex justify-content-between mb-2">
|
||||
<span class="text-muted">Supplier Credit Notes</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>Amount Due</span>
|
||||
<span id="summary_due">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-primary w-100" onclick="create_billing()">
|
||||
<i class="ti ti-check me-1"></i>Create Billing
|
||||
</button>
|
||||
<button class="btn btn-light w-100" onclick="hide_billing_form()">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row mt-5" id="billing_list_section">
|
||||
<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="billing_list">
|
||||
<thead class="table-primary border-light">
|
||||
<tr>
|
||||
<th>Billing #</th>
|
||||
<th>Date</th>
|
||||
<th>Supplier</th>
|
||||
<th>Amount Due</th>
|
||||
<th>Paid</th>
|
||||
<th>Balance</th>
|
||||
<th>Status</th>
|
||||
<th>Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody></tbody>
|
||||
<tfoot></tfoot>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<?php require '../include_ending.php'; ?>
|
||||
|
||||
<script>
|
||||
var billable_documents = [];
|
||||
|
||||
function show_billing_form() {
|
||||
$('#billing_form').removeClass('d-none');
|
||||
$('#billing_list_section').addClass('d-none');
|
||||
$('#billing_date').val(flatpickr.formatDate(new Date(), 'd/m/Y'));
|
||||
}
|
||||
|
||||
function hide_billing_form() {
|
||||
$('#billing_form').addClass('d-none');
|
||||
$('#billing_list_section').removeClass('d-none');
|
||||
}
|
||||
|
||||
function status_badge(status) {
|
||||
const map = {
|
||||
'1': '<span class="badge bg-info text-white">Open</span>',
|
||||
'2': '<span class="badge bg-success">Settled</span>',
|
||||
'3': '<span class="badge bg-warning text-dark">Partial</span>',
|
||||
'4': '<span class="badge bg-light text-dark">Void</span>',
|
||||
};
|
||||
return map[String(status)] || '—';
|
||||
}
|
||||
|
||||
function doc_type_label(doc_type) {
|
||||
return doc_type === 'supplier_credit_note'
|
||||
? '<span class="badge bg-warning text-dark">Supplier Credit Note</span>'
|
||||
: '<span class="badge bg-primary">Purchase Invoice</span>';
|
||||
}
|
||||
|
||||
function load_billings() {
|
||||
return ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment_billing.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'read',
|
||||
onSuccess: function(res) {
|
||||
var data = res.output || [];
|
||||
alasql('DROP TABLE IF EXISTS billing_list');
|
||||
alasql('CREATE TABLE billing_list');
|
||||
alasql.tables.billing_list.data = data;
|
||||
$('table#billing_list tfoot').html(generate_pagination('billing_list', data.length));
|
||||
change_page_billing_list(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function change_page_billing_list(page_num) {
|
||||
var offset = (page_num - 1) * prop_limit;
|
||||
var rows = alasql(`SELECT * FROM billing_list LIMIT ${prop_limit} OFFSET ${offset}`);
|
||||
var body = '';
|
||||
|
||||
if (!rows.length) {
|
||||
$('table#billing_list tbody').html('<tr><td colspan="8" class="text-center py-5 text-muted">No payment billings found.</td></tr>');
|
||||
return;
|
||||
}
|
||||
|
||||
$.each(rows, function(i, r) {
|
||||
body += `<tr>
|
||||
<td class="py-3 fw-semibold">${escape_html(r.billing_number || '')}</td>
|
||||
<td class="py-3">${r.billing_date ? format_date(r.billing_date) : '<span class="text-muted">—</span>'}</td>
|
||||
<td class="py-3">${escape_html(r.contact_name || '—')}</td>
|
||||
<td class="py-3 fw-semibold">${format_number(r.amount, 2)}</td>
|
||||
<td class="py-3">${format_number(r.paid_amount, 2)}</td>
|
||||
<td class="py-3">${format_number(r.balance_amount, 2)}</td>
|
||||
<td class="py-3">${status_badge(r.status)}</td>
|
||||
<td class="py-3">
|
||||
<a href="<?php echo $server_url?>finance/manage_payment_billing.php?id=${r.id}" title="View payment billing">
|
||||
<i class="ti ti-eye fs-5"></i>
|
||||
</a>
|
||||
</td>
|
||||
</tr>`;
|
||||
});
|
||||
$('table#billing_list tbody').html(body);
|
||||
}
|
||||
|
||||
function load_billable_documents() {
|
||||
var contact_id = parseInt($('#contact_id').val() || $('#contact').attr('data-id')) || 0;
|
||||
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment_billing.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'billable_documents',
|
||||
data: { contact_id: contact_id },
|
||||
onSuccess: function(res) {
|
||||
billable_documents = res.output || [];
|
||||
render_billable_documents();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function render_billable_documents() {
|
||||
var body = '';
|
||||
if (!billable_documents.length) {
|
||||
$('#document_tbody').html('<tr><td colspan="4" class="text-center py-5 text-muted">No issued unbilled purchase invoices or supplier credit notes for this supplier.</td></tr>');
|
||||
recalc_billing();
|
||||
return;
|
||||
}
|
||||
|
||||
$.each(billable_documents, function(i, d) {
|
||||
var balance = parseFloat(d.balance_amount || 0);
|
||||
var is_cn = d.doc_type === 'supplier_credit_note';
|
||||
var display_balance = is_cn ? -balance : balance;
|
||||
body += `<tr data-doc-id="${d.id}" data-doc-type="${d.doc_type}" data-balance="${balance}">
|
||||
<td>
|
||||
<div class="fw-semibold">${escape_html(d.invoice_number || '')}</div>
|
||||
<div class="small text-muted">${d.order_number ? escape_html(d.order_number) : ''}</div>
|
||||
</td>
|
||||
<td>${doc_type_label(d.doc_type)}</td>
|
||||
<td class="fw-semibold ${is_cn ? 'text-danger' : ''}">${format_number(display_balance, 2)}</td>
|
||||
<td>
|
||||
<input type="number" class="form-control form-control-sm billing_amount"
|
||||
${is_cn ? `min="${-balance}" max="0" value="${-balance}"` : `min="0" max="${balance}" value="${balance}"`}
|
||||
step="any" oninput="recalc_billing()">
|
||||
</td>
|
||||
</tr>`;
|
||||
});
|
||||
$('#document_tbody').html(body);
|
||||
recalc_billing();
|
||||
}
|
||||
|
||||
function recalc_billing() {
|
||||
var purchase_invoice_total = 0;
|
||||
var cn_total = 0;
|
||||
|
||||
$('#document_tbody tr[data-doc-id]').each(function() {
|
||||
var type = $(this).data('doc-type');
|
||||
var amount = Math.abs(parseFloat($(this).find('.billing_amount').val()) || 0);
|
||||
if (type === 'supplier_credit_note') cn_total += amount;
|
||||
else purchase_invoice_total += amount;
|
||||
});
|
||||
|
||||
$('#summary_purchase_invoice').text(format_number(purchase_invoice_total, 2));
|
||||
$('#summary_cn').text('-' + format_number(cn_total, 2));
|
||||
$('#summary_due').text(format_number(purchase_invoice_total - cn_total, 2));
|
||||
}
|
||||
|
||||
function collect_allocations() {
|
||||
var allocations = [];
|
||||
$('#document_tbody tr[data-doc-id]').each(function() {
|
||||
var amount = Math.abs(parseFloat($(this).find('.billing_amount').val()) || 0);
|
||||
var balance = parseFloat($(this).data('balance')) || 0;
|
||||
if (amount <= 0) return;
|
||||
if (amount - balance > 0.0001) {
|
||||
throw new Error('Billing amount exceeds document balance.');
|
||||
}
|
||||
allocations.push({
|
||||
invoice_id: parseInt($(this).data('doc-id')),
|
||||
amount: amount,
|
||||
});
|
||||
});
|
||||
return allocations;
|
||||
}
|
||||
|
||||
function get_billing_net_amount() {
|
||||
var purchase_invoice_total = 0;
|
||||
var cn_total = 0;
|
||||
$('#document_tbody tr[data-doc-id]').each(function() {
|
||||
var amount = Math.abs(parseFloat($(this).find('.billing_amount').val()) || 0);
|
||||
if ($(this).data('doc-type') === 'supplier_credit_note') cn_total += amount;
|
||||
else purchase_invoice_total += amount;
|
||||
});
|
||||
return purchase_invoice_total - cn_total;
|
||||
}
|
||||
|
||||
function create_billing() {
|
||||
var allocations;
|
||||
try {
|
||||
allocations = collect_allocations();
|
||||
} catch(e) {
|
||||
bootbox.alert(e.message);
|
||||
return;
|
||||
}
|
||||
if (!allocations.length) {
|
||||
bootbox.alert('Please include at least one purchase invoice or supplier credit note.');
|
||||
return;
|
||||
}
|
||||
if (get_billing_net_amount() <= 0.0001) {
|
||||
bootbox.alert('Payment billing amount must be greater than zero.');
|
||||
return;
|
||||
}
|
||||
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_payment_billing.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'create',
|
||||
data: {
|
||||
contact_id: $('#contact_id').val() || $('#contact').attr('data-id') || 0,
|
||||
billing_date: to_iso_date($('#billing_date').val()),
|
||||
notes: $('#notes').val(),
|
||||
allocations: JSON.stringify(allocations),
|
||||
},
|
||||
onSuccess: function(res) {
|
||||
window.location.href = '<?php echo $server_url?>finance/manage_payment_billing.php?id=' + res.new_id;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$(document).on('contact:selected', '#contact', function(e, contact) {
|
||||
$('#contact_id').val(contact.id);
|
||||
$('#contact').attr('data-id', contact.id);
|
||||
});
|
||||
|
||||
$(document).on('input', '#contact', function() {
|
||||
$('#contact_id').val('');
|
||||
$(this).removeAttr('data-id');
|
||||
});
|
||||
|
||||
$(function() {
|
||||
flatpickr('#billing_date', { dateFormat: 'd/m/Y', allowInput: true });
|
||||
load_billings();
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
+483
-6
@@ -2,6 +2,8 @@
|
||||
session_start();
|
||||
require '../config.php';
|
||||
require '../include_header.php';
|
||||
|
||||
$prefill_billing_id = (int)($_GET['billing_id'] ?? 0);
|
||||
?>
|
||||
|
||||
<body>
|
||||
@@ -13,16 +15,169 @@
|
||||
|
||||
<div class="row mb-6">
|
||||
<div class="col-12">
|
||||
<h1 class="fs-3 mb-1">Receipts</h1>
|
||||
<p class="mb-0 text-muted">Cash and bank receipts</p>
|
||||
<div class="d-flex justify-content-between align-items-center">
|
||||
<div>
|
||||
<h1 class="fs-3 mb-1">Receipts</h1>
|
||||
<p class="mb-0 text-muted">Customer payments against receipt billings</p>
|
||||
</div>
|
||||
<button class="btn btn-primary" onclick="show_receipt_form()">
|
||||
<i class="ti ti-plus me-1"></i>New Receipt
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="row g-5 d-none" id="receipt_form">
|
||||
<div class="col-lg-8">
|
||||
<div class="card mb-5">
|
||||
<div class="card-body p-5">
|
||||
<h2 class="fs-5 mb-4">Receipt Information</h2>
|
||||
<div class="row gx-5">
|
||||
<div class="mb-3 col-lg-6">
|
||||
<label class="form-label">Customer</label>
|
||||
<input type="text" id="contact" class="form-control contact_search"
|
||||
placeholder="Search customer..." autocomplete="off">
|
||||
<input type="hidden" id="contact_id">
|
||||
</div>
|
||||
<div class="mb-3 col-lg-3">
|
||||
<label class="form-label">Receipt Date</label>
|
||||
<input type="text" id="receipt_date" class="form-control" placeholder="DD/MM/YYYY">
|
||||
</div>
|
||||
<div class="mb-3 col-lg-3">
|
||||
<label class="form-label">Method</label>
|
||||
<select id="payment_method" class="form-select">
|
||||
<option value="bank_transfer">Bank Transfer</option>
|
||||
<option value="cash">Cash</option>
|
||||
<option value="cheque">Cheque</option>
|
||||
<option value="card">Card</option>
|
||||
<option value="other">Other</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3 col-lg-4">
|
||||
<label class="form-label">GL Formula</label>
|
||||
<select id="formula_id" class="form-select">
|
||||
<option value="">— None —</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3 col-lg-4">
|
||||
<label class="form-label">Department</label>
|
||||
<select id="department_id" class="form-select">
|
||||
<option value="0">— No Department —</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3 col-lg-12">
|
||||
<label class="form-label">Notes</label>
|
||||
<textarea id="notes" class="form-control" rows="2"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn btn-outline-secondary" onclick="load_open_billings()">
|
||||
<i class="ti ti-search me-1"></i>Load Open Billings
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-body p-5">
|
||||
<h2 class="fs-5 mb-4">Receipt Billings</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table mb-0" id="allocation_table">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
<th>Document / Billing</th>
|
||||
<th>Type / Date</th>
|
||||
<th>Department</th>
|
||||
<th>Balance</th>
|
||||
<th style="width:150px;">Receive / Use</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="allocation_tbody">
|
||||
<tr><td colspan="5" class="text-center py-5 text-muted">Select a customer and load open billings.</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">Summary</h2>
|
||||
<div class="d-flex justify-content-between mb-2">
|
||||
<span class="text-muted">Selected Billings</span>
|
||||
<span id="summary_billing">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-primary w-100" onclick="post_receipt()">
|
||||
<i class="ti ti-check me-1"></i>Post Receipt
|
||||
</button>
|
||||
<button class="btn btn-light w-100" onclick="hide_receipt_form()">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row mt-5" id="receipt_list_section">
|
||||
<div class="col-12 mb-4">
|
||||
<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 receipt number or customer...">
|
||||
<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_dept" class="form-select">
|
||||
<option value="">All Departments</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-lg-2">
|
||||
<button class="btn btn-secondary w-100" onclick="load_receipts()">
|
||||
<i class="ti ti-search me-1"></i>Search
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-12">
|
||||
<div class="card p-5 text-center text-muted">
|
||||
<i class="ti ti-cash fs-1 mb-3 d-block"></i>
|
||||
<p class="mb-0">Receipts coming soon.</p>
|
||||
<div class="card">
|
||||
<div class="card-body p-5">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover mb-0 table-centered" id="receipt_list">
|
||||
<thead class="table-primary border-light">
|
||||
<tr>
|
||||
<th>Receipt #</th>
|
||||
<th>Billing #</th>
|
||||
<th>Date</th>
|
||||
<th>Customer</th>
|
||||
<th>Department</th>
|
||||
<th>Method</th>
|
||||
<th>Amount</th>
|
||||
<th>GL Formula</th>
|
||||
<th>Status</th>
|
||||
<th>Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody></tbody>
|
||||
<tfoot></tfoot>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -32,5 +187,327 @@
|
||||
|
||||
<?php require '../include_ending.php'; ?>
|
||||
|
||||
<script>
|
||||
var open_billings = [];
|
||||
var prefill_billing_id = <?php echo $prefill_billing_id; ?>;
|
||||
var formula_map = {};
|
||||
var all_receipts = [];
|
||||
|
||||
function load_formula_map() {
|
||||
return ajax_request({
|
||||
url: '<?php echo $server_url?>accounting/api/engine/account_formula.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'read',
|
||||
data: {},
|
||||
onSuccess: function(res) {
|
||||
formula_map = {};
|
||||
$.each(res.output || [], function(i, f) { formula_map[f.id] = f.formula_name; });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function show_receipt_form() {
|
||||
$('#receipt_form').removeClass('d-none');
|
||||
$('#receipt_date').val(flatpickr.formatDate(new Date(), 'd/m/Y'));
|
||||
ajax_request({
|
||||
url: 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) {
|
||||
opts += '<option value="' + f.id + '">' + escape_html(f.formula_name) + '</option>';
|
||||
});
|
||||
$('#formula_id').html(opts);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function hide_receipt_form() {
|
||||
$('#receipt_form').addClass('d-none');
|
||||
}
|
||||
|
||||
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 load_receipts() {
|
||||
return ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_receipt.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'read',
|
||||
data: { action: 'list' },
|
||||
onSuccess: function(res) {
|
||||
var data = res.output || [];
|
||||
alasql('DROP TABLE IF EXISTS receipt_list');
|
||||
alasql('CREATE TABLE receipt_list');
|
||||
alasql.tables.receipt_list.data = data;
|
||||
$('table#receipt_list tfoot').html(generate_pagination('receipt_list', data.length));
|
||||
change_page_receipt_list(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function change_page_receipt_list(page_num) {
|
||||
var offset = (page_num - 1) * prop_limit;
|
||||
var rows = alasql(`SELECT * FROM receipt_list LIMIT ${prop_limit} OFFSET ${offset}`);
|
||||
var body = '';
|
||||
|
||||
if (!rows.length) {
|
||||
$('table#receipt_list tbody').html('<tr><td colspan="10" class="text-center py-5 text-muted">No receipts found.</td></tr>');
|
||||
return;
|
||||
}
|
||||
|
||||
$.each(rows, function(i, r) {
|
||||
var formula_name = r.formula_id && formula_map[r.formula_id]
|
||||
? escape_html(formula_map[r.formula_id])
|
||||
: '<span class="text-muted">—</span>';
|
||||
body += `<tr>
|
||||
<td class="py-3 fw-semibold">${escape_html(r.receipt_number || '')}</td>
|
||||
<td class="py-3">${escape_html(r.billing_number || '—')}</td>
|
||||
<td class="py-3">${r.receipt_date ? format_date(r.receipt_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">${escape_html(method_label(r.payment_method) || '—')}</td>
|
||||
<td class="py-3 fw-semibold">${format_number(r.amount, 2)}</td>
|
||||
<td class="py-3 small">${formula_name}</td>
|
||||
<td class="py-3">${receipt_status_badge(r.status)}</td>
|
||||
<td class="py-3">
|
||||
<a href="<?php echo $server_url?>finance/manage_receipt.php?id=${r.id}" title="View receipt">
|
||||
<i class="ti ti-eye fs-5"></i>
|
||||
</a>
|
||||
</td>
|
||||
</tr>`;
|
||||
});
|
||||
$('table#receipt_list tbody').html(body);
|
||||
}
|
||||
|
||||
function load_open_billings() {
|
||||
var contact_id = parseInt($('#contact_id').val() || $('#contact').attr('data-id')) || 0;
|
||||
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_receipt_billing.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'open_billings',
|
||||
data: { contact_id: contact_id },
|
||||
onSuccess: function(res) {
|
||||
open_billings = res.output || [];
|
||||
render_open_billings();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function render_open_billings() {
|
||||
var body = '';
|
||||
if (!open_billings.length) {
|
||||
$('#allocation_tbody').html('<tr><td colspan="5" class="text-center py-5 text-muted">No open receipt billings for this contact.</td></tr>');
|
||||
recalc_receipt();
|
||||
return;
|
||||
}
|
||||
|
||||
if (open_billings.length === 1 && Array.isArray(open_billings[0].allocations)) {
|
||||
var billing = open_billings[0];
|
||||
$.each(billing.allocations || [], function(i, line) {
|
||||
var balance = parseFloat(line.balance_amount || 0);
|
||||
if (balance <= 0.0001) return;
|
||||
var is_cn = line.doc_type === 'credit_note';
|
||||
var display_balance = is_cn ? -balance : balance;
|
||||
body += `<tr data-billing-id="${billing.id}" data-doc-id="${line.invoice_id}" data-doc-type="${line.doc_type}" data-department-id="${line.department_id || 0}" data-balance="${balance}">
|
||||
<td>
|
||||
<div class="fw-semibold">${escape_html(line.invoice_number || '')}</div>
|
||||
<div class="small text-muted">${escape_html(billing.billing_number || '')}</div>
|
||||
</td>
|
||||
<td>${is_cn ? '<span class="badge bg-warning text-dark">Credit Note</span>' : '<span class="badge bg-primary">Invoice</span>'}</td>
|
||||
<td>${get_dept_label(line.department_id)}</td>
|
||||
<td class="fw-semibold ${is_cn ? 'text-danger' : ''}">${format_number(display_balance, 2)}</td>
|
||||
<td>
|
||||
<input type="number" class="form-control form-control-sm receipt_amount"
|
||||
${is_cn ? `min="${-balance}" max="0" value="0"` : `min="0" max="${balance}" value="0"`}
|
||||
step="any" oninput="recalc_receipt()">
|
||||
</td>
|
||||
</tr>`;
|
||||
});
|
||||
$('#allocation_tbody').html(body || '<tr><td colspan="5" class="text-center py-5 text-muted">No remaining billing lines.</td></tr>');
|
||||
recalc_receipt();
|
||||
return;
|
||||
}
|
||||
|
||||
$.each(open_billings, function(i, d) {
|
||||
var balance = parseFloat(d.balance_amount || 0);
|
||||
body += `<tr data-billing-id="${d.id}" data-balance="${balance}">
|
||||
<td>
|
||||
<div class="fw-semibold">${escape_html(d.billing_number || '')}</div>
|
||||
<div class="small text-muted">Amount Due ${format_number(d.amount, 2)}</div>
|
||||
</td>
|
||||
<td>${d.billing_date ? format_date(d.billing_date) : '<span class="text-muted">—</span>'}</td>
|
||||
<td><span class="text-muted">—</span></td>
|
||||
<td class="fw-semibold">${format_number(balance, 2)}</td>
|
||||
<td>
|
||||
<input type="number" class="form-control form-control-sm receipt_amount"
|
||||
min="0" max="${balance}" step="any" value="0" oninput="recalc_receipt()">
|
||||
</td>
|
||||
</tr>`;
|
||||
});
|
||||
$('#allocation_tbody').html(body);
|
||||
recalc_receipt();
|
||||
}
|
||||
|
||||
function recalc_receipt() {
|
||||
var invoice_total = 0;
|
||||
var cn_total = 0;
|
||||
|
||||
$('#allocation_tbody tr[data-billing-id]').each(function() {
|
||||
var amount = parseFloat($(this).find('.receipt_amount').val()) || 0;
|
||||
if ($(this).data('doc-id')) {
|
||||
if ($(this).data('doc-type') === 'credit_note') cn_total += Math.abs(amount);
|
||||
else invoice_total += Math.abs(amount);
|
||||
} else {
|
||||
invoice_total += amount;
|
||||
}
|
||||
});
|
||||
|
||||
var receipt_total = invoice_total - cn_total;
|
||||
$('#summary_billing').text(format_number(receipt_total, 2));
|
||||
$('#summary_cash').text(format_number(receipt_total, 2));
|
||||
}
|
||||
|
||||
function collect_receipt_billing() {
|
||||
var selected = [];
|
||||
$('#allocation_tbody tr[data-billing-id]').each(function() {
|
||||
var amount = parseFloat($(this).find('.receipt_amount').val()) || 0;
|
||||
var balance = parseFloat($(this).data('balance')) || 0;
|
||||
var abs_amount = Math.abs(amount);
|
||||
if (abs_amount <= 0) return;
|
||||
if (abs_amount - balance > 0.0001) {
|
||||
throw new Error('Receipt amount exceeds billing balance.');
|
||||
}
|
||||
if ($(this).data('doc-id')) {
|
||||
selected.push({
|
||||
receipt_billing_id: parseInt($(this).data('billing-id')),
|
||||
invoice_id: parseInt($(this).data('doc-id')),
|
||||
department_id: parseInt($(this).data('department-id')) || 0,
|
||||
amount: abs_amount,
|
||||
});
|
||||
return;
|
||||
}
|
||||
selected.push({
|
||||
receipt_billing_id: parseInt($(this).data('billing-id')),
|
||||
amount: abs_amount,
|
||||
});
|
||||
});
|
||||
var billing_ids = [...new Set(selected.map(row => row.receipt_billing_id))];
|
||||
if (billing_ids.length > 1) {
|
||||
throw new Error('Please post one receipt billing per receipt.');
|
||||
}
|
||||
if (!selected.length) return null;
|
||||
if (selected[0].invoice_id) {
|
||||
var invoice_total = 0;
|
||||
var cn_total = 0;
|
||||
selected.forEach(function(row) {
|
||||
var $tr = $(`#allocation_tbody tr[data-doc-id="${row.invoice_id}"]`);
|
||||
if ($tr.data('doc-type') === 'credit_note') cn_total += row.amount;
|
||||
else invoice_total += row.amount;
|
||||
});
|
||||
if (invoice_total - cn_total <= 0.0001) {
|
||||
throw new Error('Cash receipt amount must be greater than zero.');
|
||||
}
|
||||
return {
|
||||
receipt_billing_id: billing_ids[0],
|
||||
amount: invoice_total - cn_total,
|
||||
allocations: selected.map(row => ({ invoice_id: row.invoice_id, department_id: row.department_id || 0, amount: row.amount })),
|
||||
};
|
||||
}
|
||||
if (selected[0].amount <= 0.0001) {
|
||||
throw new Error('Receipt amount must be greater than zero.');
|
||||
}
|
||||
return selected[0];
|
||||
}
|
||||
|
||||
function post_receipt() {
|
||||
var billing;
|
||||
try {
|
||||
billing = collect_receipt_billing();
|
||||
} catch(e) {
|
||||
bootbox.alert(e.message);
|
||||
return;
|
||||
}
|
||||
if (!billing) {
|
||||
bootbox.alert('Please enter a receipt amount for one receipt billing.');
|
||||
return;
|
||||
}
|
||||
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_receipt.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'create',
|
||||
data: {
|
||||
receipt_billing_id: billing.receipt_billing_id,
|
||||
amount: billing.amount,
|
||||
allocations: billing.allocations ? JSON.stringify(billing.allocations) : '',
|
||||
receipt_date: to_iso_date($('#receipt_date').val()),
|
||||
payment_method: $('#payment_method').val(),
|
||||
formula_id: $('#formula_id').val() || null,
|
||||
department_id: $('#department_id').val() || 0,
|
||||
notes: $('#notes').val(),
|
||||
},
|
||||
onSuccess: function(res) {
|
||||
if (res.new_id) {
|
||||
window.location.href = '<?php echo $server_url?>finance/manage_receipt.php?id=' + res.new_id;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$(document).on('contact:selected', '#contact', function(e, contact) {
|
||||
$('#contact_id').val(contact.id);
|
||||
$('#contact').attr('data-id', contact.id);
|
||||
});
|
||||
|
||||
$(document).on('input', '#contact', function() {
|
||||
$('#contact_id').val('');
|
||||
$(this).removeAttr('data-id');
|
||||
});
|
||||
|
||||
$(async function() {
|
||||
flatpickr('#receipt_date', { dateFormat: 'd/m/Y', allowInput: true });
|
||||
load_departments('department_id');
|
||||
await load_formula_map();
|
||||
load_receipts();
|
||||
if (prefill_billing_id > 0) {
|
||||
$('#receipt_list_section').addClass('d-none');
|
||||
show_receipt_form();
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_receipt_billing.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
queueLock: false,
|
||||
action: 'retrieve',
|
||||
data: { id: prefill_billing_id },
|
||||
onSuccess: function(res) {
|
||||
var billing = res.output || {};
|
||||
$('#contact').val(billing.contact_name || '').attr('data-id', billing.contact_id || '');
|
||||
$('#contact_id').val(billing.contact_id || '');
|
||||
open_billings = parseFloat(billing.balance_amount || 0) > 0.0001 ? [billing] : [];
|
||||
render_open_billings();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,357 @@
|
||||
<?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">Receipt Billings</h1>
|
||||
<p class="mb-0 text-muted">Customer collection bills from invoices and credit notes</p>
|
||||
</div>
|
||||
<button class="btn btn-primary" onclick="show_billing_form()">
|
||||
<i class="ti ti-plus me-1"></i>New Receipt Billing
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row g-5 d-none" id="billing_form">
|
||||
<div class="col-lg-8">
|
||||
<div class="card mb-5">
|
||||
<div class="card-body p-5">
|
||||
<h2 class="fs-5 mb-4">Billing Information</h2>
|
||||
<div class="row gx-5">
|
||||
<div class="mb-3 col-lg-8">
|
||||
<label class="form-label">Customer</label>
|
||||
<input type="text" id="contact" class="form-control contact_search"
|
||||
placeholder="Search customer..." autocomplete="off">
|
||||
<input type="hidden" id="contact_id">
|
||||
</div>
|
||||
<div class="mb-3 col-lg-4">
|
||||
<label class="form-label">Billing Date</label>
|
||||
<input type="text" id="billing_date" class="form-control" placeholder="DD/MM/YYYY">
|
||||
</div>
|
||||
<div class="mb-3 col-12">
|
||||
<label class="form-label">Notes</label>
|
||||
<textarea id="notes" class="form-control" rows="2"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn btn-outline-secondary" onclick="load_billable_documents()">
|
||||
<i class="ti ti-search me-1"></i>Load Billable Documents
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-body p-5">
|
||||
<h2 class="fs-5 mb-4">Documents</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table mb-0" id="document_table">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
<th>Document</th>
|
||||
<th>Type</th>
|
||||
<th>Billable</th>
|
||||
<th style="width:150px;">Include</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="document_tbody">
|
||||
<tr><td colspan="4" class="text-center py-5 text-muted">Select a customer and load billable documents.</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">Summary</h2>
|
||||
<div class="d-flex justify-content-between mb-2">
|
||||
<span class="text-muted">Invoices</span>
|
||||
<span id="summary_invoice">0.00</span>
|
||||
</div>
|
||||
<div class="d-flex justify-content-between mb-2">
|
||||
<span class="text-muted">Credit Notes</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>Amount Due</span>
|
||||
<span id="summary_due">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-primary w-100" onclick="create_billing()">
|
||||
<i class="ti ti-check me-1"></i>Create Billing
|
||||
</button>
|
||||
<button class="btn btn-light w-100" onclick="hide_billing_form()">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row mt-5" id="billing_list_section">
|
||||
<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="billing_list">
|
||||
<thead class="table-primary border-light">
|
||||
<tr>
|
||||
<th>Billing #</th>
|
||||
<th>Date</th>
|
||||
<th>Customer</th>
|
||||
<th>Amount Due</th>
|
||||
<th>Received</th>
|
||||
<th>Balance</th>
|
||||
<th>Status</th>
|
||||
<th>Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody></tbody>
|
||||
<tfoot></tfoot>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<?php require '../include_ending.php'; ?>
|
||||
|
||||
<script>
|
||||
var billable_documents = [];
|
||||
|
||||
function show_billing_form() {
|
||||
$('#billing_form').removeClass('d-none');
|
||||
$('#billing_list_section').addClass('d-none');
|
||||
$('#billing_date').val(flatpickr.formatDate(new Date(), 'd/m/Y'));
|
||||
}
|
||||
|
||||
function hide_billing_form() {
|
||||
$('#billing_form').addClass('d-none');
|
||||
$('#billing_list_section').removeClass('d-none');
|
||||
}
|
||||
|
||||
function status_badge(status) {
|
||||
const map = {
|
||||
'1': '<span class="badge bg-info text-white">Open</span>',
|
||||
'2': '<span class="badge bg-success">Settled</span>',
|
||||
'3': '<span class="badge bg-warning text-dark">Partial</span>',
|
||||
'4': '<span class="badge bg-light text-dark">Void</span>',
|
||||
};
|
||||
return map[String(status)] || '—';
|
||||
}
|
||||
|
||||
function doc_type_label(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 load_billings() {
|
||||
return ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_receipt_billing.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'read',
|
||||
onSuccess: function(res) {
|
||||
var data = res.output || [];
|
||||
alasql('DROP TABLE IF EXISTS billing_list');
|
||||
alasql('CREATE TABLE billing_list');
|
||||
alasql.tables.billing_list.data = data;
|
||||
$('table#billing_list tfoot').html(generate_pagination('billing_list', data.length));
|
||||
change_page_billing_list(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function change_page_billing_list(page_num) {
|
||||
var offset = (page_num - 1) * prop_limit;
|
||||
var rows = alasql(`SELECT * FROM billing_list LIMIT ${prop_limit} OFFSET ${offset}`);
|
||||
var body = '';
|
||||
|
||||
if (!rows.length) {
|
||||
$('table#billing_list tbody').html('<tr><td colspan="8" class="text-center py-5 text-muted">No receipt billings found.</td></tr>');
|
||||
return;
|
||||
}
|
||||
|
||||
$.each(rows, function(i, r) {
|
||||
body += `<tr>
|
||||
<td class="py-3 fw-semibold">${escape_html(r.billing_number || '')}</td>
|
||||
<td class="py-3">${r.billing_date ? format_date(r.billing_date) : '<span class="text-muted">—</span>'}</td>
|
||||
<td class="py-3">${escape_html(r.contact_name || '—')}</td>
|
||||
<td class="py-3 fw-semibold">${format_number(r.amount, 2)}</td>
|
||||
<td class="py-3">${format_number(r.received_amount, 2)}</td>
|
||||
<td class="py-3">${format_number(r.balance_amount, 2)}</td>
|
||||
<td class="py-3">${status_badge(r.status)}</td>
|
||||
<td class="py-3">
|
||||
<a href="<?php echo $server_url?>finance/manage_receipt_billing.php?id=${r.id}" title="View receipt billing">
|
||||
<i class="ti ti-eye fs-5"></i>
|
||||
</a>
|
||||
</td>
|
||||
</tr>`;
|
||||
});
|
||||
$('table#billing_list tbody').html(body);
|
||||
}
|
||||
|
||||
function load_billable_documents() {
|
||||
var contact_id = parseInt($('#contact_id').val() || $('#contact').attr('data-id')) || 0;
|
||||
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_receipt_billing.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'billable_documents',
|
||||
data: { contact_id: contact_id },
|
||||
onSuccess: function(res) {
|
||||
billable_documents = res.output || [];
|
||||
render_billable_documents();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function render_billable_documents() {
|
||||
var body = '';
|
||||
if (!billable_documents.length) {
|
||||
$('#document_tbody').html('<tr><td colspan="4" class="text-center py-5 text-muted">No issued unbilled invoices or credit notes for this contact.</td></tr>');
|
||||
recalc_billing();
|
||||
return;
|
||||
}
|
||||
|
||||
$.each(billable_documents, function(i, d) {
|
||||
var balance = parseFloat(d.balance_amount || 0);
|
||||
var is_cn = d.doc_type === 'credit_note';
|
||||
var display_balance = is_cn ? -balance : balance;
|
||||
body += `<tr data-doc-id="${d.id}" data-doc-type="${d.doc_type}" data-balance="${balance}">
|
||||
<td>
|
||||
<div class="fw-semibold">${escape_html(d.invoice_number || '')}</div>
|
||||
<div class="small text-muted">${d.order_number ? escape_html(d.order_number) : ''}</div>
|
||||
</td>
|
||||
<td>${doc_type_label(d.doc_type)}</td>
|
||||
<td class="fw-semibold ${is_cn ? 'text-danger' : ''}">${format_number(display_balance, 2)}</td>
|
||||
<td>
|
||||
<input type="number" class="form-control form-control-sm billing_amount"
|
||||
${is_cn ? `min="${-balance}" max="0" value="${-balance}"` : `min="0" max="${balance}" value="${balance}"`}
|
||||
step="any" oninput="recalc_billing()">
|
||||
</td>
|
||||
</tr>`;
|
||||
});
|
||||
$('#document_tbody').html(body);
|
||||
recalc_billing();
|
||||
}
|
||||
|
||||
function recalc_billing() {
|
||||
var invoice_total = 0;
|
||||
var cn_total = 0;
|
||||
|
||||
$('#document_tbody tr[data-doc-id]').each(function() {
|
||||
var type = $(this).data('doc-type');
|
||||
var amount = Math.abs(parseFloat($(this).find('.billing_amount').val()) || 0);
|
||||
if (type === 'credit_note') cn_total += amount;
|
||||
else invoice_total += amount;
|
||||
});
|
||||
|
||||
$('#summary_invoice').text(format_number(invoice_total, 2));
|
||||
$('#summary_cn').text('-' + format_number(cn_total, 2));
|
||||
$('#summary_due').text(format_number(invoice_total - cn_total, 2));
|
||||
}
|
||||
|
||||
function collect_allocations() {
|
||||
var allocations = [];
|
||||
$('#document_tbody tr[data-doc-id]').each(function() {
|
||||
var amount = Math.abs(parseFloat($(this).find('.billing_amount').val()) || 0);
|
||||
var balance = parseFloat($(this).data('balance')) || 0;
|
||||
if (amount <= 0) return;
|
||||
if (amount - balance > 0.0001) {
|
||||
throw new Error('Billing amount exceeds document balance.');
|
||||
}
|
||||
allocations.push({
|
||||
invoice_id: parseInt($(this).data('doc-id')),
|
||||
amount: amount,
|
||||
});
|
||||
});
|
||||
return allocations;
|
||||
}
|
||||
|
||||
function get_billing_net_amount() {
|
||||
var invoice_total = 0;
|
||||
var cn_total = 0;
|
||||
$('#document_tbody tr[data-doc-id]').each(function() {
|
||||
var amount = Math.abs(parseFloat($(this).find('.billing_amount').val()) || 0);
|
||||
if ($(this).data('doc-type') === 'credit_note') cn_total += amount;
|
||||
else invoice_total += amount;
|
||||
});
|
||||
return invoice_total - cn_total;
|
||||
}
|
||||
|
||||
function create_billing() {
|
||||
var allocations;
|
||||
try {
|
||||
allocations = collect_allocations();
|
||||
} catch(e) {
|
||||
bootbox.alert(e.message);
|
||||
return;
|
||||
}
|
||||
if (!allocations.length) {
|
||||
bootbox.alert('Please include at least one invoice or credit note.');
|
||||
return;
|
||||
}
|
||||
if (get_billing_net_amount() <= 0.0001) {
|
||||
bootbox.alert('Receipt billing amount must be greater than zero.');
|
||||
return;
|
||||
}
|
||||
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>finance/api/engine/manage_receipt_billing.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'create',
|
||||
data: {
|
||||
contact_id: $('#contact_id').val() || $('#contact').attr('data-id') || 0,
|
||||
billing_date: to_iso_date($('#billing_date').val()),
|
||||
notes: $('#notes').val(),
|
||||
allocations: JSON.stringify(allocations),
|
||||
},
|
||||
onSuccess: function(res) {
|
||||
window.location.href = '<?php echo $server_url?>finance/manage_receipt_billing.php?id=' + res.new_id;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$(document).on('contact:selected', '#contact', function(e, contact) {
|
||||
$('#contact_id').val(contact.id);
|
||||
$('#contact').attr('data-id', contact.id);
|
||||
});
|
||||
|
||||
$(document).on('input', '#contact', function() {
|
||||
$('#contact_id').val('');
|
||||
$(this).removeAttr('data-id');
|
||||
});
|
||||
|
||||
$(function() {
|
||||
flatpickr('#billing_date', { dateFormat: 'd/m/Y', allowInput: true });
|
||||
load_billings();
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user