softDelete features
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../classes_ac/PostingWindowGuard.php';
|
||||
|
||||
class ReceiptBillingManager {
|
||||
|
||||
@@ -389,5 +390,44 @@ class ReceiptBillingManager {
|
||||
':id' => $billing_id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Soft-delete a receipt billing by negating company_id on the header and all items.
|
||||
* Blocked if any receipt (not yet soft-deleted) is linked to this billing.
|
||||
*/
|
||||
public function softDelete(int $billing_id): void
|
||||
{
|
||||
$sth = $this->pdo->prepare(
|
||||
"SELECT id, billing_date FROM td_receipt_billing WHERE id = :id AND company_id = :cid LIMIT 1"
|
||||
);
|
||||
$sth->execute([':id' => $billing_id, ':cid' => $this->company_id]);
|
||||
$row = $sth->fetch(PDO::FETCH_ASSOC);
|
||||
if (!$row) throw new Exception('Receipt billing not found.');
|
||||
|
||||
global $pdo1;
|
||||
if (isset($pdo1) && $pdo1 instanceof PDO) {
|
||||
$guard = new PostingWindowGuard($pdo1, $this->company_id);
|
||||
$guard->assertOpenDate($row['billing_date'] ?: date('Y-m-d'), 'Receipt billing deletion');
|
||||
}
|
||||
|
||||
$sth2 = $this->pdo->prepare(
|
||||
"SELECT COUNT(*) FROM td_receipt
|
||||
WHERE company_id = :cid AND receipt_billing_id = :id"
|
||||
);
|
||||
$sth2->execute([':cid' => $this->company_id, ':id' => $billing_id]);
|
||||
if ((int)$sth2->fetchColumn() > 0) {
|
||||
throw new Exception('Cannot delete — receipts are linked to this billing. Delete the receipts first.');
|
||||
}
|
||||
|
||||
$this->pdo->prepare(
|
||||
"UPDATE td_receipt_billing_item SET company_id = company_id * -1
|
||||
WHERE billing_id = :id AND company_id = :cid"
|
||||
)->execute([':id' => $billing_id, ':cid' => $this->company_id]);
|
||||
|
||||
$this->pdo->prepare(
|
||||
"UPDATE td_receipt_billing SET company_id = company_id * -1
|
||||
WHERE id = :id AND company_id = :cid"
|
||||
)->execute([':id' => $billing_id, ':cid' => $this->company_id]);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user