« Back to History
semi_exporter.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php /* ============================================================================= File: /erp/payment_export.php Purpose: Generic Bank Payment Export (Semi-monthly + Loom salaries) Logic : - If salary_rows includes bank detail (beneficiary_name, account_number, ifsc), use directly (Semi-monthly export). - Else fallback to karigar_bank_data + meta (Loom export). ============================================================================ */ error_reporting(E_ALL); ini_set('display_errors', 1); const PAYMENT_TYPE_DEFAULT = 'N'; const AMOUNT_DECIMALS = 0; const REMARKS_DEFAULT = ''; const NARR_PREFIX_DEFAULT = ''; require_once __DIR__ . '/modules/auth/auth.php'; require_login(); $u = auth_user(); $company_id = (int)$u['company_id']; $user_id = (int)$u['id']; if (!isset($pdo) || !($pdo instanceof PDO)) { require_once __DIR__ . '/core/db.php'; } /* --- helpers --- */ function fmt_amt($n){ return number_format((float)$n, AMOUNT_DECIMALS, '.', ''); } function payment_type_for_ifsc(?string $ifsc): string { $x=strtoupper(trim((string)$ifsc)); return (strpos($x,'ICIC0')===0)?'I':PAYMENT_TYPE_DEFAULT; } function ci_idx(array $headers, array $needles){ foreach ($needles as $needle){ foreach ($headers as $i=>$h){ if (strcasecmp(trim((string)$h), trim((string)$needle))===0) return (int)$i; } } return -1; } /* --- inputs --- */ $rows_json = $_POST['salary_rows'] ?? '[]'; $period_label = trim((string)($_POST['period_label'] ?? '')); $period_start = $_POST['period_start'] ?? null; $period_end = $_POST['period_end'] ?? null; $fmt = strtolower($_POST['fmt'] ?? 'xlsx'); $pay_rows = json_decode($rows_json, true); if (!is_array($pay_rows) || empty($pay_rows)) { http_response_code(400); exit('No rows to export.'); } /* --- headers --- */ $headers = [ 'Debit A/c Number','Beneficiary A/c Number','Beneficiary Name','Amount', 'Payment Type (Mandatory for all types of payments)','Payment date','IFSC Code', 'Beneficiary Mobile No.','Beneficiary email-id', 'Bene Address 1','Bene Address 2','Bene Address 3','Bene Address 4', 'Add detail 1','Add detail 2','Add detail 3','Add detail 4','Add detail 5', 'Remarks','Credit Narration', ]; $idx = [ 'debit'=>ci_idx($headers,['Debit A/c Number']), 'account'=>ci_idx($headers,['Beneficiary A/c Number']), 'name'=>ci_idx($headers,['Beneficiary Name']), 'amount'=>ci_idx($headers,['Amount']), 'ptype'=>ci_idx($headers,['Payment Type (Mandatory for all types of payments)','Payment Type']), 'pdate'=>ci_idx($headers,['Payment date','Payment Date']), 'ifsc'=>ci_idx($headers,['IFSC Code','IFSC']), 'mobile'=>ci_idx($headers,['Beneficiary Mobile No.','Mobile']), 'email'=>ci_idx($headers,['Beneficiary email-id','Email']), 'remarks'=>ci_idx($headers,['Remarks']), 'narr'=>ci_idx($headers,['Credit Narration','Narration']), ]; /* --- fallback (loom karigar) data --- */ $emp_ids = array_values(array_filter(array_unique(array_map(fn($r)=>(int)($r['employee_id']??0),$pay_rows)))); $bank=[];$meta=[]; if ($emp_ids){ $in = implode(',',array_fill(0,count($emp_ids),'?')); try { $sql="SELECT id, karigar_name AS nm, mobile, email FROM loom_karigar_master WHERE company_id=? AND id IN ($in)"; $params = array_merge([$company_id],$emp_ids); $q=$pdo->prepare($sql); $q->execute($params); foreach($q as $r){ $eid=(int)$r['id']; $bank[$eid]=['name'=>$r['nm'],'ac'=>'','ifsc'=>'','entry_name'=>'']; $meta[$eid]=['mobile'=>$r['mobile'],'email'=>$r['email']]; } }catch(Throwable $e){ /* ignore */ } try { $sql="SELECT karigar_id, beneficiary_name, beneficiary_ac_number AS account_no, ifsc_code AS ifsc, entry_name FROM karigar_bank_data WHERE company_id=? AND karigar_id IN ($in) ORDER BY karigar_id, updated_at DESC, id DESC"; $params = array_merge([$company_id],$emp_ids); $st=$pdo->prepare($sql); $st->execute($params); foreach($st as $r){ $eid=(int)$r['karigar_id']; if (!isset($bank[$eid])) $bank[$eid]=['name'=>'','ac'=>'','ifsc'=>'','entry_name'=>'']; if ($r['beneficiary_name']!=='') $bank[$eid]['name']=$r['beneficiary_name']; if ($bank[$eid]['ac']==='') $bank[$eid]['ac']=$r['account_no']; if ($bank[$eid]['ifsc']==='') $bank[$eid]['ifsc']=$r['ifsc']; if ($bank[$eid]['entry_name']==='') $bank[$eid]['entry_name']=$r['entry_name']; } }catch(Throwable $e){ /* ignore */ } } /* --- build rows_for --- */ $rows_for=[];$rows_for[]=$headers; $debit_ac=''; // company debit ac resolve karna ho to helper add kar sakte $payDate = date('Y-m-d'); $payDateFmt = date('d-M-Y',strtotime($payDate)); $monthLbl= date('M-Y',strtotime($payDate)); $narration=($period_label!==''?$period_label.' - ':'').$monthLbl; foreach ($pay_rows as $r){ $eid=(int)($r['employee_id']??0); $amt=(float)($r['amount']??0); if ($eid<=0||$amt<=0) continue; // ✅ NEW: direct from salary_rows if available $benef=trim((string)($r['beneficiary_name']??'')); $acc =trim((string)($r['account_number']??'')); $ifsc =strtoupper(trim((string)($r['ifsc']??''))); $ptype=strtoupper(trim((string)($r['payment_type']??PAYMENT_TYPE_DEFAULT))); // fallback loom karigar if ($acc===''||$ifsc===''||$benef===''){ $rec=$bank[$eid]??['name'=>'','ac'=>'','ifsc'=>'','entry_name'=>'']; if ($benef==='') $benef=$rec['name']; if ($acc==='') $acc=$rec['ac']; if ($ifsc==='') $ifsc=$rec['ifsc']; } $info=$meta[$eid]??['mobile'=>'','email'=>'']; $line=array_fill(0,count($headers),''); if ($idx['debit']>=0) $line[$idx['debit']] =$debit_ac; if ($idx['account']>=0) $line[$idx['account']] =$acc; if ($idx['name']>=0) $line[$idx['name']] =$benef; if ($idx['amount']>=0) $line[$idx['amount']] =fmt_amt($amt); if ($idx['ptype']>=0) $line[$idx['ptype']] =$ptype; if ($idx['pdate']>=0) $line[$idx['pdate']] =$payDateFmt; if ($idx['ifsc']>=0) $line[$idx['ifsc']] =$ifsc; if ($idx['mobile']>=0) $line[$idx['mobile']] =$info['mobile']; if ($idx['email']>=0) $line[$idx['email']] =$info['email']; if ($idx['remarks']>=0) $line[$idx['remarks']] =$rec['entry_name']??REMARKS_DEFAULT; if ($idx['narr']>=0) $line[$idx['narr']] =$narration; $rows_for[]=$line; } /* --- output TSV (Excel-friendly) --- */ $out=fopen('php://temp','w+'); fwrite($out,"\xEF\xBB\xBF"); foreach($rows_for as $row){ fwrite($out,implode("\t",array_map('strval',$row))."\r\n"); } rewind($out); header('Content-Type: text/tab-separated-values; charset=UTF-8'); header('Content-Disposition: attachment; filename="payment_export.tsv"'); fpassthru($out); fclose($out); exit;