softDelete features
This commit is contained in:
@@ -513,5 +513,53 @@ class PaymentManager {
|
||||
':company_id' => $this->company_id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Soft-delete a payment 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 $payment_id): void
|
||||
{
|
||||
$sth = $this->pdo->prepare(
|
||||
"SELECT id, payment_billing_id, payment_date FROM td_payment
|
||||
WHERE company_id = :cid AND id = :id LIMIT 1"
|
||||
);
|
||||
$sth->execute([':cid' => $this->company_id, ':id' => $payment_id]);
|
||||
$payment = $sth->fetch(PDO::FETCH_ASSOC);
|
||||
if (!$payment) throw new Exception('Payment not found.');
|
||||
|
||||
$this->assertPostingWindow($payment['payment_date'] ?: date('Y-m-d'), 'Payment deletion');
|
||||
|
||||
// Collect linked invoice ids before negating
|
||||
$sth2 = $this->pdo->prepare(
|
||||
"SELECT invoice_id FROM td_payment_item
|
||||
WHERE company_id = :cid AND payment_id = :id"
|
||||
);
|
||||
$sth2->execute([':cid' => $this->company_id, ':id' => $payment_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('payment', $payment_id);
|
||||
}
|
||||
|
||||
$this->pdo->prepare(
|
||||
"UPDATE td_payment_item SET company_id = company_id * -1
|
||||
WHERE payment_id = :id AND company_id = :cid"
|
||||
)->execute([':id' => $payment_id, ':cid' => $this->company_id]);
|
||||
|
||||
$this->pdo->prepare(
|
||||
"UPDATE td_payment SET company_id = company_id * -1
|
||||
WHERE id = :id AND company_id = :cid"
|
||||
)->execute([':id' => $payment_id, ':cid' => $this->company_id]);
|
||||
|
||||
foreach ($invoice_ids as $invoice_id) {
|
||||
$this->refreshInvoiceSettlementStatus((int)$invoice_id);
|
||||
}
|
||||
$this->refreshPaymentBillingStatus((int)$payment['payment_billing_id']);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user