pdo->prepare( "SELECT p.*, COALESCE(c.contact_name, '') AS contact_name FROM td_purchase_order p LEFT JOIN md_contact c ON c.company_id = p.company_id AND c.id = p.contact_id WHERE p.company_id = :cid AND p.id = :id LIMIT 1" ); $sth->execute([':cid' => $this->companyId, ':id' => $id]); $doc = $sth->fetch(PDO::FETCH_ASSOC); if (!$doc) throw new Exception("Purchase order #{$id} not found."); $sth2 = $this->pdo->prepare( "SELECT * FROM td_purchase_order_item WHERE company_id = :cid AND order_id = :id ORDER BY item_id ASC" ); $sth2->execute([':cid' => $this->companyId, ':id' => $id]); $doc['items'] = $sth2->fetchAll(PDO::FETCH_ASSOC); return $doc; } protected function getAmounts(array $doc): array { $grand_total = abs((float)$doc['grand_total']); $tax = abs((float)$doc['tax']); return [ 'grand_total' => $grand_total, 'tax' => $tax, 'total' => $grand_total - $tax, 'amount' => $grand_total, ]; } protected function getDocDate(array $doc): string { return $doc['po_date'] ?? date('Y-m-d'); } 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->buildSkuSplitLines($doc['items'], $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, ]; } private function buildSkuSplitLines(array $items, array $formula_item, float $amount, int $dept_id): array { $items = array_values(array_filter($items, function($row) { return abs((float)($row['total_price'] ?? 0)) > 0.0000001; })); if (!$items) return []; $skus = array_values(array_unique(array_filter(array_column($items, 'product_sku')))); $account_map = []; if ($skus) { $placeholders = implode(',', array_fill(0, count($skus), '?')); $sth = $this->pdo->prepare( "SELECT sku, purchase_account_code FROM md_product WHERE company_id = ? AND sku IN ({$placeholders})" ); $sth->execute(array_merge([$this->companyId], $skus)); foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $row) { $account_map[$row['sku']] = trim((string)($row['purchase_account_code'] ?? '')); } } $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; $sku = trim((string)($row['product_sku'] ?? '')); $account_code = ($sku !== '' && ($account_map[$sku] ?? '') !== '') ? $account_map[$sku] : (string)$formula_item['account_code']; $name = trim((string)($row['product_name'] ?? '')); $desc = trim((string)($formula_item['description'] ?? '')); $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; } }