accounting workflows
This commit is contained in:
+125
-97
@@ -42,6 +42,8 @@
|
||||
<th>Product</th>
|
||||
<th class="text-end">Qty</th>
|
||||
<th class="text-end">Unit Price</th>
|
||||
<th class="text-end" style="width:60px;">Tax%</th>
|
||||
<th class="text-end">Tax Amt</th>
|
||||
<th class="text-end">Total</th>
|
||||
</tr>
|
||||
</thead>
|
||||
@@ -93,15 +95,31 @@
|
||||
<span class="text-muted">Contact:</span>
|
||||
<span class="ms-1 fw-semibold" id="display_contact"></span>
|
||||
</div>
|
||||
<div class="mb-2 small">
|
||||
<span class="text-muted">Department:</span>
|
||||
<span class="ms-1" id="display_department">—</span>
|
||||
</div>
|
||||
<div class="mb-2 small">
|
||||
<span class="text-muted">Issued:</span>
|
||||
<span class="ms-1" id="display_issued"></span>
|
||||
</div>
|
||||
<div class="mb-2 small">
|
||||
<div class="mb-2 small" id="due_date_row">
|
||||
<span class="text-muted">Due:</span>
|
||||
<input type="text" id="due_date" class="form-control form-control-sm ms-1 d-inline-block"
|
||||
style="width:140px;" placeholder="DD/MM/YYYY">
|
||||
</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 class="mt-3 small">
|
||||
<span class="text-muted">Notes:</span>
|
||||
<textarea id="notes" class="form-control form-control-sm mt-1" rows="2"></textarea>
|
||||
@@ -118,32 +136,15 @@
|
||||
<i class="ti ti-device-floppy me-1"></i>Save
|
||||
</button>
|
||||
|
||||
<button class="btn btn-success w-100" id="btn_issue"
|
||||
onclick="issue_invoice()">
|
||||
<i class="ti ti-send me-1"></i>Issue Invoice
|
||||
</button>
|
||||
|
||||
<button class="btn btn-outline-secondary w-100" id="btn_print"
|
||||
onclick="print_invoice()">
|
||||
<i class="ti ti-printer me-1"></i>Print / Save PDF
|
||||
</button>
|
||||
|
||||
<!-- Status selector (issued and above) -->
|
||||
<div class="" id="invoice_status_group">
|
||||
<label class="form-label small text-muted mb-1">Invoice Status</label>
|
||||
<div class="d-flex gap-2">
|
||||
<select id="invoice_status_select" class="form-select form-select-sm">
|
||||
<option value="1">Issued</option>
|
||||
<option value="2">Paid</option>
|
||||
<option value="3">Overdue</option>
|
||||
<option value="4">Void</option>
|
||||
</select>
|
||||
<button class="btn btn-sm btn-outline-primary text-nowrap"
|
||||
onclick="update_invoice_status($('#invoice_status_select').val())">
|
||||
<i class="ti ti-check"></i>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn btn-outline-danger w-100" id="btn_void"
|
||||
onclick="void_invoice()">
|
||||
<i class="ti ti-ban me-1"></i>Void Invoice
|
||||
</button>
|
||||
|
||||
|
||||
</div>
|
||||
@@ -157,6 +158,7 @@
|
||||
<?php require '../include_ending.php'; ?>
|
||||
|
||||
<script>
|
||||
// custom.js globals: format_number()
|
||||
|
||||
var invoice_id = <?php echo $invoice_id; ?>;
|
||||
var invoice_data = null;
|
||||
@@ -166,17 +168,34 @@
|
||||
const map = {
|
||||
'invoice': '<span class="badge bg-primary">Invoice</span>',
|
||||
'credit_note': '<span class="badge bg-warning text-dark">Credit Note</span>',
|
||||
'debit_note': '<span class="badge bg-info text-white">Debit Note</span>',
|
||||
'supplier_credit_note': '<span class="badge bg-info text-white">Supplier Credit Note</span>',
|
||||
};
|
||||
return map[doc_type] ?? '—';
|
||||
}
|
||||
|
||||
function invoice_status_badge(status) {
|
||||
function is_invoice_overdue(status, due_date) {
|
||||
if (String(status) !== '1' || !due_date) return false;
|
||||
var d = new Date();
|
||||
var today = d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
|
||||
return due_date < today;
|
||||
}
|
||||
|
||||
function invoice_status_badge(status, due_date, payment_state, doc_type) {
|
||||
if (payment_state === 'partial') {
|
||||
return '<span class="badge bg-warning text-dark">Partial</span>';
|
||||
}
|
||||
if (payment_state === 'paid') {
|
||||
return doc_type === 'credit_note'
|
||||
? '<span class="badge bg-success">Settled</span>'
|
||||
: '<span class="badge bg-success">Paid</span>';
|
||||
}
|
||||
if (is_invoice_overdue(status, due_date)) {
|
||||
return '<span class="badge bg-danger">Overdue</span>';
|
||||
}
|
||||
const map = {
|
||||
'0': '<span class="badge bg-secondary">Draft</span>',
|
||||
'1': '<span class="badge bg-info text-white">Issued</span>',
|
||||
'2': '<span class="badge bg-success">Paid</span>',
|
||||
'3': '<span class="badge bg-danger">Overdue</span>',
|
||||
'4': '<span class="badge bg-light text-dark">Void</span>',
|
||||
};
|
||||
return map[String(status)] ?? '—';
|
||||
@@ -184,31 +203,26 @@
|
||||
|
||||
|
||||
function set_mode(inv) {
|
||||
var status = parseInt(inv.status);
|
||||
var doc_type = inv.doc_type;
|
||||
var is_invoice = doc_type === 'invoice';
|
||||
var is_draft = status === 0;
|
||||
var is_active = status >= 1 && status <= 3;
|
||||
var status = parseInt(inv.status);
|
||||
var doc_type = inv.doc_type;
|
||||
var is_invoice = doc_type === 'invoice';
|
||||
var is_cn = doc_type === 'credit_note';
|
||||
var is_draft = status === 0;
|
||||
var is_void = status === 4;
|
||||
var is_editable = is_draft && (is_invoice || is_cn);
|
||||
|
||||
// Fields editable only on draft invoice
|
||||
$('#due_date, #notes').prop('disabled', !(is_draft && is_invoice));
|
||||
// Due date only relevant for invoices
|
||||
$('#due_date_row').toggleClass('d-none', is_cn);
|
||||
$('#due_date, #notes').prop('disabled', !is_editable);
|
||||
|
||||
// Save + Issue
|
||||
var is_draft_invoice = is_draft && is_invoice;
|
||||
var not_invoice_hint = doc_type !== 'invoice' ? 'Only original invoices can be edited' : '';
|
||||
set_btn_state('#btn_save', is_draft_invoice,
|
||||
not_invoice_hint || 'Only draft invoices can be edited');
|
||||
set_btn_state('#btn_issue', is_draft_invoice,
|
||||
not_invoice_hint || (status > 0 ? 'Invoice has already been issued' : 'Only draft invoices can be issued'));
|
||||
|
||||
// Status selector
|
||||
var can_change_status = is_active && is_invoice;
|
||||
var status_hint = !is_invoice ? 'Status cannot be changed on credit/debit notes'
|
||||
: status === 0 ? 'Issue the invoice first before changing status'
|
||||
: status === 4 ? 'Invoice is void' : '';
|
||||
|
||||
$('#invoice_status_select').prop('disabled', !can_change_status);
|
||||
var not_editable_hint = (!is_invoice && !is_cn) ? 'Only invoices and credit notes can be edited' : '';
|
||||
set_btn_state('#btn_save', is_editable,
|
||||
not_editable_hint || 'Only draft documents can be edited');
|
||||
|
||||
set_btn_state('#btn_void', is_invoice && status >= 1 && status !== 4,
|
||||
!is_invoice ? 'Only original invoices can be voided'
|
||||
: status === 0 ? 'Invoice has not been issued yet'
|
||||
: status === 4 ? 'Invoice is already void' : '');
|
||||
}
|
||||
|
||||
|
||||
@@ -225,12 +239,32 @@
|
||||
|
||||
$('#page_title').text(inv.invoice_number);
|
||||
$('#badge_doc_type').html(doc_type_badge(inv.doc_type));
|
||||
$('#badge_status').html(invoice_status_badge(inv.status));
|
||||
$('#badge_status').html(invoice_status_badge(inv.status, inv.due_date, inv.payment_state, inv.doc_type));
|
||||
$('#display_contact').text(inv.contact_name || '—');
|
||||
$('#display_department').text(get_dept_label(inv.department_id));
|
||||
$('#display_issued').text(format_date(inv.issued_date) || '—');
|
||||
$('#due_date').val(inv.due_date ? format_date(inv.due_date) : '');
|
||||
$('#notes').val(inv.notes || '');
|
||||
|
||||
var gl_type = inv.doc_type === 'credit_note' ? 'sales_credit_note' : 'sales_invoice';
|
||||
var selected_formula = inv.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: gl_type },
|
||||
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', parseInt(inv.status) === 4);
|
||||
|
||||
if (inv.order_id) {
|
||||
$('#link_order')
|
||||
.attr('href', '<?php echo $server_url?>order/manage_order.php?id=' + inv.order_id)
|
||||
@@ -244,19 +278,29 @@
|
||||
tbody += `<tr>
|
||||
<td>${item.product_name || item.product_sku}</td>
|
||||
<td class="text-end">${format_number(item.quantity, 2)}</td>
|
||||
<td class="text-end">${format_number(item.price, 2)}</td>
|
||||
<td class="text-end">${format_number(item.unit_price, 2)}</td>
|
||||
<td class="text-end">${format_number(item.tax_rate, 2)}</td>
|
||||
<td class="text-end">${format_number(item.tax_amount, 4)}</td>
|
||||
<td class="text-end">${format_number(item.total_price, 2)}</td>
|
||||
</tr>`;
|
||||
});
|
||||
$('#item_tbody').html(tbody);
|
||||
|
||||
// Totals
|
||||
var is_draft = parseInt(inv.status) === 0;
|
||||
var is_editable_doc = inv.doc_type === 'invoice' || inv.doc_type === 'credit_note';
|
||||
var tax_adj_val = parseFloat(inv.tax_adjustment) || 0;
|
||||
var tax_adj_cell = (is_draft && is_editable_doc)
|
||||
? `<input type="number" id="tax_adjustment" class="form-control form-control-sm text-end d-inline-block" style="width:90px;" min="-0.3" max="0.3" step="0.01" value="${tax_adj_val.toFixed(2)}">`
|
||||
: `${format_number(tax_adj_val, 2)}`;
|
||||
|
||||
var tfoot = `
|
||||
<tr><td colspan="3" class="text-muted border-bottom-0">Subtotal</td><td class="text-end border-bottom-0">${format_number(inv.subtotal, 2)}</td></tr>
|
||||
<tr><td colspan="3" class="text-muted border-bottom-0">Discount</td><td class="text-end border-bottom-0">${format_number(inv.discount, 2)}</td></tr>
|
||||
<tr><td colspan="3" class="text-muted border-bottom-0">Tax</td><td class="text-end border-bottom-0">${format_number(inv.tax, 2)}</td></tr>
|
||||
<tr><td colspan="3" class="text-muted">Shipping</td><td class="text-end">${format_number(inv.shipping_fee, 2)}</td></tr>
|
||||
<tr><td colspan="3" class="fw-bold">Grand Total</td><td class="text-end fw-bold">${format_number(inv.grand_total, 2)}</td></tr>`;
|
||||
<tr><td colspan="5" class="text-muted border-bottom-0">Subtotal</td><td class="text-end border-bottom-0">${format_number(inv.subtotal, 2)}</td></tr>
|
||||
<tr><td colspan="5" class="text-muted border-bottom-0">Discount</td><td class="text-end border-bottom-0">${format_number(inv.discount, 2)}</td></tr>
|
||||
<tr><td colspan="5" class="text-muted border-bottom-0">Tax</td><td class="text-end border-bottom-0">${format_number(inv.tax, 2)}</td></tr>
|
||||
<tr><td colspan="5" class="text-muted border-bottom-0">Tax Adj.</td><td class="text-end border-bottom-0">${tax_adj_cell}</td></tr>
|
||||
<tr><td colspan="5" class="text-muted">Shipping</td><td class="text-end">${format_number(inv.shipping_fee, 2)}</td></tr>
|
||||
<tr><td colspan="5" class="fw-bold">Grand Total</td><td class="text-end fw-bold">${format_number(inv.grand_total, 2)}</td></tr>`;
|
||||
$('#item_tfoot').html(tfoot);
|
||||
|
||||
// Linked credit notes
|
||||
@@ -267,7 +311,7 @@
|
||||
<td>${cn.invoice_number}</td>
|
||||
<td>${format_date(cn.issued_date)}</td>
|
||||
<td class="text-end text-danger">${format_number(cn.grand_total, 2)}</td>
|
||||
<td>${invoice_status_badge(cn.status)}</td>
|
||||
<td>${invoice_status_badge(cn.status, cn.due_date, cn.payment_state, cn.doc_type)}</td>
|
||||
<td><a href="<?php echo $server_url?>order/manage_invoice.php?id=${cn.id}"><i class="ti ti-eye fs-5"></i></a></td>
|
||||
</tr>`;
|
||||
});
|
||||
@@ -276,7 +320,6 @@
|
||||
}
|
||||
|
||||
set_mode(inv);
|
||||
$('#invoice_status_select').val(inv.status);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -289,50 +332,23 @@
|
||||
checkRequired: 0,
|
||||
action: 'update',
|
||||
data: {
|
||||
id: invoice_id,
|
||||
due_date: $('#due_date').val().split('/').reverse().join('-'),
|
||||
notes: $('#notes').val(),
|
||||
id: invoice_id,
|
||||
due_date: $('#due_date').val().split('/').reverse().join('-'),
|
||||
notes: $('#notes').val(),
|
||||
tax_adjustment: parseFloat($('#tax_adjustment').val()) || 0,
|
||||
},
|
||||
onSuccess: function() { retrieve_invoice(); }
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function issue_invoice() {
|
||||
|
||||
|
||||
function void_invoice() {
|
||||
bootbox.confirm({
|
||||
message: 'Issue this invoice? It will be sent to the customer.',
|
||||
message: '<strong>Void this invoice?</strong><br>This action cannot be undone.',
|
||||
buttons: {
|
||||
confirm: { label: 'Issue', className: 'btn-success' },
|
||||
cancel: { label: 'Back', className: 'btn-secondary' }
|
||||
},
|
||||
callback: function(result) {
|
||||
if (!result) return;
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>order/api/engine/issue_invoice.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'update',
|
||||
data: { invoice_id: invoice_id },
|
||||
onSuccess: function() { retrieve_invoice(); }
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
function update_invoice_status(status) {
|
||||
const labels = { 2: 'Mark as Paid', 3: 'Mark as Overdue', 4: 'Void Invoice' };
|
||||
const classes = { 2: 'btn-success', 3: 'btn-warning', 4: 'btn-danger' };
|
||||
const messages = {
|
||||
2: 'Mark this invoice as paid?',
|
||||
3: 'Mark this invoice as overdue?',
|
||||
4: '<strong>Void this invoice?</strong><br>This action cannot be undone.',
|
||||
};
|
||||
bootbox.confirm({
|
||||
message: messages[status],
|
||||
buttons: {
|
||||
confirm: { label: labels[status], className: classes[status] },
|
||||
confirm: { label: 'Void Invoice', className: 'btn-danger' },
|
||||
cancel: { label: 'Back', className: 'btn-secondary' }
|
||||
},
|
||||
callback: function(result) {
|
||||
@@ -342,7 +358,7 @@
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'update',
|
||||
data: { invoice_id: invoice_id, status: status },
|
||||
data: { invoice_id: invoice_id, status: 4 },
|
||||
onSuccess: function() { retrieve_invoice(); }
|
||||
});
|
||||
}
|
||||
@@ -350,6 +366,19 @@
|
||||
}
|
||||
|
||||
|
||||
function save_formula() {
|
||||
ajax_request({
|
||||
url: '<?php echo $server_url?>order/api/engine/manage_invoice.php',
|
||||
autoPrepare: true,
|
||||
checkRequired: 0,
|
||||
action: 'save_formula',
|
||||
data: { id: invoice_id, formula_id: $('#formula_id').val() || null },
|
||||
onSuccess: function() {
|
||||
bootbox.alert({ message: 'GL formula saved.', size: 'small' });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function print_invoice() {
|
||||
if (!invoice_id) return;
|
||||
window.open('<?php echo $server_url?>order/print_invoice.php?id=' + invoice_id, '_blank');
|
||||
@@ -357,13 +386,12 @@
|
||||
|
||||
|
||||
$(function() {
|
||||
if (invoice_id) {
|
||||
retrieve_invoice();
|
||||
}
|
||||
load_dept_cache();
|
||||
if (invoice_id) retrieve_invoice();
|
||||
flatpickr('#due_date', { dateFormat: 'd/m/Y', allowInput: true });
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user