int, 'period' => 'YYYY-MM', 'doc_date' => 'YYYY-MM-DD', 'lines' => [...]] * * Amount keys understood by formulas: * grand_total — full document total (abs value used) * total — pre-tax amount (grand_total - tax) * tax — tax portion * amount — used by receipt and payment */ abstract class BasePosting { protected PDO $pdo; protected int $companyId; public function __construct(PDO $pdo, int $company_id) { $this->pdo = $pdo; $this->companyId = $company_id; } abstract protected function fetchDocument(int $id): array; abstract protected function getAmounts(array $doc): array; abstract protected function getDocType(): string; abstract protected function getDocDate(array $doc): string; public function build(int $id, ?int $formula_id = null): array { $doc = $this->fetchDocument($id); $formula = $this->resolveFormula($this->getDocType(), $formula_id); $amounts = $this->getAmounts($doc); $doc_date = $this->getDocDate($doc); $period = substr($doc_date, 0, 7); $dept_id = (int)($doc['department_id'] ?? 0); $lines = []; foreach ($formula['items'] as $item) { $amount = abs((float)($amounts[$item['amount_key']] ?? 0)); if ($amount == 0) continue; if ($item['amount_key'] === 'total') { $split_lines = $this->buildSkuTotalLines($doc, $item, $amount, $dept_id); if ($split_lines) { $lines = array_merge($lines, $split_lines); continue; } } $lines[] = [ 'account_code' => $item['account_code'], 'department_id' => $dept_id, 'debit' => $item['drcr'] === 'D' ? $amount : 0.0, 'credit' => $item['drcr'] === 'C' ? $amount : 0.0, 'description' => (string)($item['description'] ?? ''), ]; } return [ 'formula_id' => (int)$formula['id'], 'period' => $period, 'doc_date' => $doc_date, 'lines' => $lines, ]; } protected function resolveFormula(string $doc_type, ?int $formula_id): array { if ($formula_id) { $sth = $this->pdo->prepare( "SELECT * FROM md_account_formula WHERE company_id = :cid AND id = :id AND status = 1 LIMIT 1" ); $sth->execute([':cid' => $this->companyId, ':id' => $formula_id]); } else { $sth = $this->pdo->prepare( "SELECT * FROM md_account_formula WHERE company_id = :cid AND document_type = :doc_type AND is_default = 1 AND status = 1 LIMIT 1" ); $sth->execute([':cid' => $this->companyId, ':doc_type' => $doc_type]); } $formula = $sth->fetch(PDO::FETCH_ASSOC); if (!$formula) throw new Exception("No active formula found for '{$doc_type}'."); $sth = $this->pdo->prepare( "SELECT * FROM md_account_formula_item WHERE company_id = :cid AND formula_id = :formula_id ORDER BY sort_order ASC" ); $sth->execute([':cid' => $this->companyId, ':formula_id' => $formula['id']]); $formula['items'] = $sth->fetchAll(PDO::FETCH_ASSOC); return $formula; } protected function getSkuTotalAccountColumn(): string { return ''; } private function buildSkuTotalLines(array $doc, array $formula_item, float $amount, int $dept_id): array { $column = $this->getSkuTotalAccountColumn(); if ($column === '' || !in_array($column, ['sales_account_code', 'purchase_account_code'], true)) { return []; } $invoice_id = (int)($doc['id'] ?? 0); if ($invoice_id <= 0) return []; $sth = $this->pdo->prepare(" SELECT i.product_sku, i.product_name, i.total_price, COALESCE(NULLIF(p.{$column}, ''), '') AS mapped_account_code FROM td_invoice_item i LEFT JOIN md_product p ON p.company_id = i.company_id AND p.sku = i.product_sku WHERE i.company_id = :cid AND i.invoice_id = :invoice_id ORDER BY i.item_id ASC, i.id ASC "); $sth->execute([':cid' => $this->companyId, ':invoice_id' => $invoice_id]); $items = array_values(array_filter($sth->fetchAll(PDO::FETCH_ASSOC), function($row) { return abs((float)($row['total_price'] ?? 0)) > 0.0000001; })); if (!$items) return []; $base_total = array_reduce($items, function($carry, $row) { return $carry + abs((float)($row['total_price'] ?? 0)); }, 0.0); if ($base_total <= 0) return []; $remaining = round($amount, 4); $last = count($items) - 1; $lines = []; foreach ($items as $idx => $row) { $line_amount = $idx === $last ? $remaining : round($amount * (abs((float)$row['total_price']) / $base_total), 4); $remaining = round($remaining - $line_amount, 4); if (abs($line_amount) <= 0.0000001) continue; $account_code = trim((string)($row['mapped_account_code'] ?? '')); if ($account_code === '') { $account_code = (string)$formula_item['account_code']; } $desc = trim((string)($formula_item['description'] ?? '')); $sku = trim((string)($row['product_sku'] ?? '')); $name = trim((string)($row['product_name'] ?? '')); $item_desc = trim($sku . ($name !== '' ? ' - ' . $name : '')); if ($item_desc !== '') { $desc = $desc !== '' ? $desc . ': ' . $item_desc : $item_desc; } $lines[] = [ 'account_code' => $account_code, 'department_id' => $dept_id, 'debit' => $formula_item['drcr'] === 'D' ? $line_amount : 0.0, 'credit' => $formula_item['drcr'] === 'C' ? $line_amount : 0.0, 'description' => $desc, ]; } return $lines; } }