« Back to History
party_payment_export.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php header('X-Frame-Options: SAMEORIGIN'); error_reporting(E_ALL); ini_set('display_errors',1); require_once __DIR__ . '/modules/auth/auth.php'; require_login(); $u = auth_user(); $company_id = (int)($u['company_id'] ?? 0); require_once __DIR__ . '/core/db.php'; /* ================= XLSX HELPERS ================= */ function xlsx_col_name(int $n): string { $s=''; while($n>0){ $m=($n-1)%26; $s=chr(65+$m).$s; $n=(int)(($n-$m-1)/26); } return $s; } function xlsx_xml_escape(string $s): string { return htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); } function xlsx_zip_build(string $sheetName, array $rows): string { $sheetName = $sheetName !== '' ? mb_substr($sheetName,0,31) : 'Sheet1'; $tmp = tempnam(sys_get_temp_dir(),'xlsx_'); if(file_exists($tmp)) unlink($tmp); /* ---------- sheet XML ---------- */ $sheetXml = '<?xml version="1.0" encoding="UTF-8"?> <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> <sheetData>'; $r=0; foreach($rows as $row){ $r++; $sheetXml .= '<row r="'.$r.'">'; $c=0; foreach($row as $val){ $c++; $addr=xlsx_col_name($c).$r; $sheetXml.='<c r="'.$addr.'" t="inlineStr"> <is><t>'.xlsx_xml_escape((string)$val).'</t></is></c>'; } $sheetXml.='</row>'; } $sheetXml .= '</sheetData></worksheet>'; /* ---------- other xml ---------- */ $coreDate = gmdate('Y-m-d\TH:i:s\Z'); $content_types='<?xml version="1.0" encoding="UTF-8"?> <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> <Default Extension="xml" ContentType="application/xml"/> <Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/> <Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/> <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/> <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/> <Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/> </Types>'; $rels='<?xml version="1.0" encoding="UTF-8"?> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/> <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/> <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/package/2006/relationships/extended-properties" Target="docProps/app.xml"/> </Relationships>'; $app='<?xml version="1.0" encoding="UTF-8"?> <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/docPropsVTypes"> <Application>ERP</Application> </Properties>'; $core='<?xml version="1.0" encoding="UTF-8"?> <cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <dc:creator>ERP</dc:creator> <cp:lastModifiedBy>ERP</cp:lastModifiedBy> <dcterms:created xsi:type="dcterms:W3CDTF">'.$coreDate.'</dcterms:created> <dcterms:modified xsi:type="dcterms:W3CDTF">'.$coreDate.'</dcterms:modified> </cp:coreProperties>'; $workbook='<?xml version="1.0" encoding="UTF-8"?> <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> <sheets> <sheet name="'.$sheetName.'" sheetId="1" r:id="rId1"/> </sheets> </workbook>'; $wb_rels='<?xml version="1.0" encoding="UTF-8"?> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/> <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/> </Relationships>'; $styles='<?xml version="1.0" encoding="UTF-8"?> <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> <fonts count="1"><font><sz val="11"/><name val="Calibri"/></font></fonts> <fills count="1"><fill><patternFill patternType="none"/></fill></fills> <borders count="1"><border/></borders> <cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs> <cellXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellXfs> </styleSheet>'; $zip = new ZipArchive(); if(!$zip->open($tmp, ZipArchive::CREATE)) throw new RuntimeException('Zip open failed'); $zip->addFromString('[Content_Types].xml',$content_types); $zip->addFromString('_rels/.rels',$rels); $zip->addFromString('docProps/app.xml',$app); $zip->addFromString('docProps/core.xml',$core); $zip->addFromString('xl/workbook.xml',$workbook); $zip->addFromString('xl/_rels/workbook.xml.rels',$wb_rels); $zip->addFromString('xl/styles.xml',$styles); $zip->addFromString('xl/worksheets/sheet1.xml',$sheetXml); $zip->close(); return $tmp; } /* ================= INPUT ================= */ $data = json_decode($_POST['export_json'] ?? '[]', true); if(empty($data)){ exit('No data'); } /* ================= SPLIT SUPPORT ================= */ $rows=[]; if(isset($data['rows'])){ $rows=$data['rows']; }else{ $rows[]=[ 'party_account_id'=>$data['party_account_id'], 'amount'=>$data['amount'], 'remark'=>$data['remark'] ?? '' ]; } /* ================= COMPANY BANK ================= */ $st=$pdo->prepare(" SELECT debit_ac_no,bank_user_id FROM company_bank_accounts WHERE company_id=? ORDER BY id DESC LIMIT 1 "); $st->execute([$company_id]); $bank=$st->fetch(PDO::FETCH_ASSOC); $debit_ac=trim((string)$bank['debit_ac_no']); $bank_user_id=trim((string)$bank['bank_user_id']); /* ================= HEADERS ================= */ $headers=[ 'Debit A/c Number', 'Beneficiary A/c Number', 'Beneficiary Name', 'Amount', 'Payment Type', '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' ]; $export_data=[]; $export_data[]=$headers; /* ================= BUILD ROWS ================= */ foreach($rows as $r){ $account_id=$r['party_account_id']; $amount=$r['amount']; $remark=$r['remark'] ?? ''; $stp=$pdo->prepare(" SELECT * FROM party_ac_detail WHERE id=? AND company_id=? "); $stp->execute([$account_id,$company_id]); $p=$stp->fetch(PDO::FETCH_ASSOC); if(!$p) continue; $ptype=(stripos($p['ifsc_code'],'ICIC')===0)?'I':'N'; $export_data[]=[ $debit_ac, $p['account_no'], $p['beneficiary_name'], number_format($amount,2,'.',''), $ptype, date('d-M-Y'), strtoupper($p['ifsc_code']), '','','','','','','','','','','', $remark, date('M-Y') ]; } /* ================= FILE NAME ================= */ $seqStr="01"; $dateStr=date('dmY'); $sheetName="{$bank_user_id}_{$bank_user_id}UPLD_{$dateStr}_{$seqStr}"; $fileName=$sheetName.".xlsx"; /* ================= EXPORT ================= */ $file=xlsx_zip_build($sheetName,$export_data); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment; filename="'.$fileName.'"'); header('Content-Length: '.filesize($file)); readfile($file); unlink($file); exit;