72 lines
2.5 KiB
PHP
72 lines
2.5 KiB
PHP
<?php
|
|
session_start();
|
|
require '../../../assets/utils/db_auth.php';
|
|
require_role($user_role, ['owner', 'admin', 'staff']);
|
|
require '../../../assets/utils/classes/InvoiceManager.php';
|
|
|
|
$id = (int)($data['invoice_id'] ?? 0);
|
|
$status = (int)($data['status'] ?? 0);
|
|
|
|
if (!$id) {
|
|
$answer['message'] = 'Invalid invoice ID.';
|
|
http_response_code(400);
|
|
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)) {
|
|
$answer['message'] = 'Invalid status value.';
|
|
http_response_code(400);
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
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,
|
|
]);
|
|
});
|
|
|
|
$answer['success'] = 1;
|
|
$answer['message'] = 'Invoice status updated.';
|
|
|
|
} 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));
|
|
?>
|