« Back to History
recon_lib.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php // recon_lib.php - common helpers for expense_recon APIs // Place in same folder as other API files. if (!function_exists('recon_init')) { function recon_init($ctx) { $pdo = $ctx['pdo'] ?? null; if (!($pdo instanceof PDO)) throw new RuntimeException("pdo_missing"); return $pdo; } } if (!function_exists('persist_recon_result')) { function persist_recon_result(PDO $pdo, array $bankRow, array $matchDetails, array $allocs, $company_id, $ctx_user_id, $source_type='generic') { $sql = "INSERT INTO expense_recon_results (company_id, source_type, source_ref, bank_row_json, match_json, allocations_json, status, created_by) VALUES (:cid, :stype, :sref, :bankjson, :matchjson, :allocjson, :status, :cb)"; $st = $pdo->prepare($sql); $status = (count($allocs) > 0) ? 'auto_matched' : 'pending'; $st->execute([ ':cid' => $company_id, ':stype' => $source_type, ':sref' => $bankRow['ref_no'] ?? null, ':bankjson' => json_encode($bankRow, JSON_UNESCAPED_UNICODE), ':matchjson' => json_encode($matchDetails, JSON_UNESCAPED_UNICODE), ':allocjson' => json_encode($allocs, JSON_UNESCAPED_UNICODE), ':status' => $status, ':cb' => $ctx_user_id ]); return (int)$pdo->lastInsertId(); } } if (!function_exists('alloc_folding_split')) { function alloc_folding_split($dept, $amt, $rules) { $repair = round($amt * ($rules['folding']['repair_pct'] / 100.0), 2); $loom = round($amt * ($rules['folding']['loom_pct'] / 100.0), 2); return [ ['account'=>'repair_expense','dept'=>$dept,'amount'=>$repair], ['account'=>'loom_expense','dept'=>$dept,'amount'=>$loom] ]; } } if (!function_exists('allocate_dept_amount_generic')) { function allocate_dept_amount_generic($dept, $amt, $alloc_rules) { if (stripos($dept,'fold') !== false) { return alloc_folding_split($dept,$amt,$alloc_rules); } $map = $alloc_rules['dept_map'] ?? []; $account = $map[$dept] ?? $map[ucfirst($dept)] ?? ($alloc_rules['default_to'] ?? 'other_expense'); return [['account'=>$account,'dept'=>$dept,'amount'=>round($amt,2)]]; } }