« Back to History
payment_export_bridge.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php /* ============================================================================ File: /erp/payment_export_bridge.php Purpose: Bridge payment_history -> payment_export.php (your working exporter) Scope : Local-only; no global/base changes How it works: - GET: company_id, from (YYYY-MM-DD), to (YYYY-MM-DD) - Reads payment_history and groups by employee_id (SUM amount) - Sets POST payload salary_rows/period_start/period_end/period_label - Requires payment_export.php (streams CSV and exit) ============================================================================ */ error_reporting(E_ALL); ini_set('display_errors', 1); /* ---------- Auth + DB ---------- */ require_once __DIR__ . '/modules/auth/auth.php'; require_login(); $u = auth_user(); $company_id = (int)$u['company_id']; if (!isset($pdo) || !($pdo instanceof PDO)) { require_once __DIR__ . '/core/db.php'; } function valid_date($d){ return is_string($d) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $d); } $cid = isset($_GET['company_id']) ? (int)$_GET['company_id'] : $company_id; if ($cid <= 0) $cid = $company_id; $from = $_GET['from'] ?? date('Y-m-01'); $to = $_GET['to'] ?? date('Y-m-d'); if (!valid_date($from)) $from = date('Y-m-01'); if (!valid_date($to)) $to = date('Y-m-d'); /* ---------- Read payment_history -> rows (employee_id, amount) ---------- */ $sql = "SELECT employee_id, SUM(amount) AS amount FROM payment_history WHERE company_id = :cid AND DATE(created_at) BETWEEN :from AND :to GROUP BY employee_id HAVING SUM(amount) > 0"; $st = $pdo->prepare($sql); $st->execute([':cid'=>$cid, ':from'=>$from, ':to'=>$to]); $rows = []; foreach ($st as $r) { $eid = (int)$r['employee_id']; $amt = (float)$r['amount']; if ($eid > 0 && $amt > 0) { $rows[] = ['employee_id'=>$eid, 'amount'=>$amt]; } } if (!$rows) { http_response_code(400); exit('No rows to export for selected date range.'); } /* ---------- Build POST payload for your exporter ---------- */ $period_start = $from; $period_end = $to; /* label only for narration in your exporter */ $period_label = date('d M Y', strtotime($from)) . ' - ' . date('d M Y', strtotime($to)); $_POST['salary_rows'] = json_encode($rows, JSON_UNESCAPED_UNICODE); $_POST['period_start'] = $period_start; $_POST['period_end'] = $period_end; $_POST['period_label'] = $period_label; /* ---------- Hand off to your existing exporter (streams CSV) ---------- */ require __DIR__ . '/payment_export.php'; exit;