« Back to History
loom_recon.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php // loom_recon.php - reconcile loom salaries require_once __DIR__ . '/../../auth/page_acl.php'; require_once __DIR__ . '/recon_lib.php'; $ctx = page_require_access('expense_recon'); $pdo = $ctx['pdo'] ?? null; header('Content-Type: application/json; charset=utf-8'); $raw = $GLOBALS['__recon_payload'] ?? file_get_contents('php://input'); $in = json_decode($raw,true); if (!is_array($in)) { echo json_encode(['error'=>'invalid_json']); exit; } $company_id = (int)($in['company_id'] ?? $ctx['company_id'] ?? 0); $start = $in['pay_period_start'] ?? null; $end = $in['pay_period_end'] ?? null; $bank_rows = $in['bank_rows'] ?? []; if (!$start || !$end) { echo json_encode(['error'=>'missing_period']); exit; } // try saved table: loom_salary_report $deptTotals=[]; try { $st = $pdo->prepare("SELECT COALESCE(department_name,'Loom') AS department, SUM(net_pay) AS amount FROM loom_salary_report WHERE company_id=:cid AND date BETWEEN :from AND :to GROUP BY department"); $st->execute([':cid'=>$company_id, ':from'=>$start, ':to'=>$end]); foreach ($st->fetchAll(PDO::FETCH_ASSOC) as $r) $deptTotals[$r['department']] = (float)$r['amount']; } catch (Throwable $e) {} // fallback: try loom_entry if (empty($deptTotals)) { try { $st = $pdo->prepare("SELECT COALESCE(shift,'Loom') AS department, SUM(amount) AS amount FROM loom_entry WHERE company_id=:cid AND entry_date BETWEEN :from AND :to GROUP BY department"); $st->execute([':cid'=>$company_id, ':from'=>$start, ':to'=>$end]); foreach ($st->fetchAll(PDO::FETCH_ASSOC) as $r) $deptTotals[$r['department']]=(float)$r['amount']; } catch (Throwable $e) {} } $grandTotal = array_sum($deptTotals); $results = []; $alloc_rules=['folding'=>['repair_pct'=>30,'loom_pct'=>70],'dept_map'=>[],'default_to'=>'loom_expense']; foreach ($bank_rows as $b) { $bAmt = (float)($b['amount'] ?? 0); $matched=false;$match_details=[];$suggested_allocs=[]; foreach ($deptTotals as $dept=>$dAmt) { if (abs($dAmt - $bAmt) <= 0.5) { $match_details[]=['reason'=>'matched_dept_exact','dept'=>$dept]; $suggested_allocs = array_merge($suggested_allocs, allocate_dept_amount_generic($dept,$dAmt,$alloc_rules)); $matched=true; break; } } if (!$matched && abs($grandTotal - $bAmt) <= 0.5) { foreach ($deptTotals as $dept=>$dAmt) $suggested_allocs = array_merge($suggested_allocs, allocate_dept_amount_generic($dept,$dAmt,$alloc_rules)); $match_details[]=['reason'=>'matched_grand_total']; $matched=true; } if (!$matched && $grandTotal>0) { foreach ($deptTotals as $dept=>$dAmt) { $share = ($dAmt/$grandTotal)*$bAmt; $suggested_allocs = array_merge($suggested_allocs, allocate_dept_amount_generic($dept,$share,$alloc_rules)); } $match_details[]=['reason'=>'proportional_fallback']; $matched=true; } $id = persist_recon_result($pdo,$b,$match_details,$suggested_allocs,$company_id,$ctx['user']['id'],'loom'); $results[]=['id'=>$id,'bank_row'=>$b,'matched'=>$matched,'alloc'=>$suggested_allocs]; } echo json_encode(['company_id'=>$company_id,'dept_totals'=>$deptTotals,'grand_total'=>$grandTotal,'results'=>$results], JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE); exit;