softDelete features
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../classes_ac/PostingWindowGuard.php';
|
||||
|
||||
/**
|
||||
* PurchaseRequestManager
|
||||
@@ -347,4 +348,43 @@ class PurchaseRequestManager
|
||||
)->execute([':id' => $request_id, ':cid' => $this->company_id]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Soft-delete a purchase request by negating company_id on the header and all items.
|
||||
* Blocked if any purchase order (not yet soft-deleted) was converted from this request.
|
||||
*/
|
||||
public function softDelete(int $id): void
|
||||
{
|
||||
$sth = $this->pdo->prepare(
|
||||
"SELECT id, request_date FROM td_purchase_request WHERE id = :id AND company_id = :cid LIMIT 1"
|
||||
);
|
||||
$sth->execute([':id' => $id, ':cid' => $this->company_id]);
|
||||
$row = $sth->fetch(PDO::FETCH_ASSOC);
|
||||
if (!$row) throw new Exception('Purchase request not found.');
|
||||
|
||||
global $pdo1;
|
||||
if (isset($pdo1) && $pdo1 instanceof PDO) {
|
||||
$guard = new PostingWindowGuard($pdo1, $this->company_id);
|
||||
$guard->assertOpenDate($row['request_date'] ?: date('Y-m-d'), 'Purchase request deletion');
|
||||
}
|
||||
|
||||
$sth2 = $this->pdo->prepare(
|
||||
"SELECT COUNT(*) FROM td_purchase_order
|
||||
WHERE company_id = :cid AND source = 'purchase_request' AND source_id = :id"
|
||||
);
|
||||
$sth2->execute([':cid' => $this->company_id, ':id' => $id]);
|
||||
if ((int)$sth2->fetchColumn() > 0) {
|
||||
throw new Exception('Cannot delete — this request has linked purchase orders. Delete the POs first.');
|
||||
}
|
||||
|
||||
$this->pdo->prepare(
|
||||
"UPDATE td_purchase_request_item SET company_id = company_id * -1
|
||||
WHERE request_id = :id AND company_id = :cid"
|
||||
)->execute([':id' => $id, ':cid' => $this->company_id]);
|
||||
|
||||
$this->pdo->prepare(
|
||||
"UPDATE td_purchase_request SET company_id = company_id * -1
|
||||
WHERE id = :id AND company_id = :cid"
|
||||
)->execute([':id' => $id, ':cid' => $this->company_id]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user