accounting workflows
This commit is contained in:
@@ -13,8 +13,9 @@
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
// 2=paid 3=overdue 4=void — issue (0→1) handled by issue_invoice.php
|
||||
if (!in_array($status, [2, 3, 4], true)) {
|
||||
// Manual status changes are limited to void. Paid is handled by payments;
|
||||
// overdue is calculated from due_date and is not stored as a status.
|
||||
if ($status !== 4) {
|
||||
$answer['message'] = 'Invalid status value.';
|
||||
http_response_code(400);
|
||||
exit(json_encode($answer));
|
||||
@@ -22,38 +23,8 @@
|
||||
|
||||
try {
|
||||
dbTransaction($pdo2, function($pdo) use ($id, $status, $company_id, $logging) {
|
||||
|
||||
$sth = $pdo->prepare(
|
||||
"SELECT status, doc_type, `log` FROM td_invoice
|
||||
WHERE company_id = :company_id AND id = :id"
|
||||
);
|
||||
$sth->execute([':company_id' => $company_id, ':id' => $id]);
|
||||
$row = $sth->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$row) throw new Exception("Invoice not found.");
|
||||
if ($row['doc_type'] !== 'invoice') throw new Exception("Only invoices can be updated this way.");
|
||||
|
||||
$current = (int)$row['status'];
|
||||
|
||||
if ($current === 4) throw new Exception("Invoice is already void.");
|
||||
if ($current === 0) throw new Exception("Issue the invoice before changing payment status.");
|
||||
|
||||
$log = json_decode($row['log'] ?? '[]', true) ?: [];
|
||||
$log[] = array_merge($logging, [
|
||||
'action' => 'update_status',
|
||||
'status' => $status,
|
||||
'previous_status' => $current,
|
||||
]);
|
||||
|
||||
$pdo->prepare(
|
||||
"UPDATE td_invoice SET status = :status, `log` = :log
|
||||
WHERE id = :id AND company_id = :company_id"
|
||||
)->execute([
|
||||
':status' => $status,
|
||||
':log' => json_encode($log),
|
||||
':id' => $id,
|
||||
':company_id' => $company_id,
|
||||
]);
|
||||
$inv = new InvoiceManager($pdo, $company_id);
|
||||
$inv->voidInvoice($id, array_merge($logging, ['action' => 'update_status', 'status' => $status]));
|
||||
});
|
||||
|
||||
$answer['success'] = 1;
|
||||
@@ -69,4 +40,4 @@
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user