accounting workflows

This commit is contained in:
Thanakorn S
2026-05-20 10:17:02 +07:00
parent 04a683bd02
commit 7396db6ffc
199 changed files with 23539 additions and 5345 deletions
@@ -1,21 +1,109 @@
<?php
/**
* ReceiptPosting
*
* Builds the journal entry when a customer receipt is recorded.
* DR: Bank / Cash
* CR: Accounts Receivable
*/
class ReceiptPosting
{
private PDO $pdo;
private int $companyId;
require_once __DIR__ . '/BasePosting.php';
public function __construct(PDO $pdo, int $company_id)
class ReceiptPosting extends BasePosting
{
protected function getDocType(): string { return 'receipt'; }
public function build(int $id, ?int $formula_id = null): array
{
$this->pdo = $pdo;
$this->companyId = $company_id;
$doc = $this->fetchDocument($id);
$formula = $this->resolveFormula($this->getDocType(), $formula_id);
$doc_date = $this->getDocDate($doc);
$period = substr($doc_date, 0, 7);
$dept_amounts = $this->getDepartmentAmounts($id);
$lines = [];
foreach ($dept_amounts as $dept_id => $amount) {
if (abs($amount) <= 0.0001) continue;
$is_negative = $amount < 0;
$line_amount = abs($amount);
foreach ($formula['items'] as $item) {
if ($item['amount_key'] === 'tax') continue;
$drcr = $item['drcr'];
if ($is_negative) {
$drcr = $drcr === 'D' ? 'C' : 'D';
}
$lines[] = [
'account_code' => $item['account_code'],
'department_id' => (int)$dept_id,
'debit' => $drcr === 'D' ? $line_amount : 0.0,
'credit' => $drcr === 'C' ? $line_amount : 0.0,
'description' => (string)($item['description'] ?? ''),
];
}
}
return [
'formula_id' => (int)$formula['id'],
'period' => $period,
'doc_date' => $doc_date,
'lines' => $lines,
];
}
// TODO: build(int $receipt_id): array — returns [header, lines] ready for JournalManager
protected function fetchDocument(int $id): array
{
$sth = $this->pdo->prepare(
"SELECT r.*, COALESCE(c.contact_name, '') AS contact_name
FROM td_receipt r
LEFT JOIN md_contact c
ON c.company_id = r.company_id AND c.id = r.contact_id
WHERE r.company_id = :cid AND r.id = :id
LIMIT 1"
);
$sth->execute([':cid' => $this->companyId, ':id' => $id]);
$doc = $sth->fetch(PDO::FETCH_ASSOC);
if (!$doc) throw new Exception("Receipt #{$id} not found.");
return $doc;
}
protected function getAmounts(array $doc): array
{
$amount = abs((float)$doc['amount']);
return [
'amount' => $amount,
'grand_total' => $amount,
'total' => $amount,
'tax' => 0.0,
];
}
protected function getDocDate(array $doc): string
{
return $doc['receipt_date'] ?? date('Y-m-d');
}
private function getDepartmentAmounts(int $receipt_id): array
{
$sth = $this->pdo->prepare(
"SELECT ri.department_id,
ri.doc_type,
ri.amount
FROM td_receipt_item ri
WHERE ri.company_id = :cid
AND ri.receipt_id = :receipt_id"
);
$sth->execute([
':cid' => $this->companyId,
':receipt_id' => $receipt_id,
]);
$dept_amounts = [];
foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $line) {
$dept_id = (int)($line['department_id'] ?? 0);
$amount = abs((float)($line['amount'] ?? 0));
if ($line['doc_type'] === 'credit_note') {
$amount *= -1;
}
$dept_amounts[$dept_id] = ($dept_amounts[$dept_id] ?? 0) + $amount;
}
if (!$dept_amounts) throw new Exception("Receipt #{$receipt_id} has no allocation lines for GL posting.");
return $dept_amounts;
}
}