softDelete features

This commit is contained in:
Thanakorn S
2026-05-22 14:07:22 +07:00
parent 738f600fe8
commit 648efee991
59 changed files with 1783 additions and 16 deletions
@@ -511,5 +511,53 @@ class ReceiptManager {
':company_id' => $this->company_id,
]);
}
/**
* Soft-delete a receipt by negating company_id on the header and all items.
* Deletes the GL entry if posted. Refreshes settlement and billing status.
*/
public function softDelete(int $receipt_id): void
{
$sth = $this->pdo->prepare(
"SELECT id, receipt_billing_id, receipt_date FROM td_receipt
WHERE company_id = :cid AND id = :id LIMIT 1"
);
$sth->execute([':cid' => $this->company_id, ':id' => $receipt_id]);
$receipt = $sth->fetch(PDO::FETCH_ASSOC);
if (!$receipt) throw new Exception('Receipt not found.');
$this->assertPostingWindow($receipt['receipt_date'] ?: date('Y-m-d'), 'Receipt deletion');
// Collect linked invoice ids before negating
$sth2 = $this->pdo->prepare(
"SELECT invoice_id FROM td_receipt_item
WHERE company_id = :cid AND receipt_id = :id"
);
$sth2->execute([':cid' => $this->company_id, ':id' => $receipt_id]);
$invoice_ids = $sth2->fetchAll(PDO::FETCH_COLUMN);
// Delete GL entry if posted (no-op if none; throws if period closed)
global $pdo1;
if (isset($pdo1) && $pdo1 instanceof PDO) {
$guard = new PostingWindowGuard($pdo1, $this->company_id);
$gl = new GlManager($this->pdo, $this->company_id, $guard);
$gl->delete('receipt', $receipt_id);
}
$this->pdo->prepare(
"UPDATE td_receipt_item SET company_id = company_id * -1
WHERE receipt_id = :id AND company_id = :cid"
)->execute([':id' => $receipt_id, ':cid' => $this->company_id]);
$this->pdo->prepare(
"UPDATE td_receipt SET company_id = company_id * -1
WHERE id = :id AND company_id = :cid"
)->execute([':id' => $receipt_id, ':cid' => $this->company_id]);
foreach ($invoice_ids as $invoice_id) {
$this->refreshInvoiceSettlementStatus((int)$invoice_id);
}
$this->refreshReceiptBillingStatus((int)$receipt['receipt_billing_id']);
}
}
?>