« Back to History
cms_payment_import.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php declare(strict_types=1); require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('cms_payment_import'); $u = $ctx['user'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); $pdo = $ctx['pdo'] ?? null; require_once __DIR__ . '/partials/header.php'; if (!$pdo) { require_once __DIR__ . '/core/db.php'; } if (session_status() === PHP_SESSION_NONE) { session_start(); } if (empty($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } $message = ''; $error = ''; function h($str) { return htmlspecialchars((string)$str, ENT_QUOTES, 'UTF-8'); } function parse_date_value(?string $date): ?string { if (!$date) { return null; } $date = trim($date); if ($date === '') { return null; } $formats = [ 'd-M-Y', 'd-m-Y', 'Y-m-d', 'd/m/Y', ]; foreach ($formats as $format) { $dt = DateTime::createFromFormat($format, $date); if ($dt instanceof DateTime) { return $dt->format('Y-m-d'); } } return null; } if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ( empty($_POST['csrf_token']) || $_POST['csrf_token'] !== $_SESSION['csrf_token'] ) { $error = 'Invalid CSRF Token.'; } elseif ( empty($_FILES['csv_file']['tmp_name']) || !is_uploaded_file($_FILES['csv_file']['tmp_name']) ) { $error = 'Please select CSV file.'; } else { $file = $_FILES['csv_file']['tmp_name']; if (($handle = fopen($file, 'r')) === false) { $error = 'Unable to open CSV file.'; } else { $header = fgetcsv($handle); $insert = $pdo->prepare(" INSERT INTO CMS_payment_detail ( company_id, debit_account_no, beneficiary_account_no, beneficiary_name, amount, payment_mode, payment_date, ifsc_code, payable_location_name, print_location_name, bene_mobile_no, bene_email_id, bene_add_1, bene_add_2, bene_add_3, bene_add_4, add_details_1, add_details_2, add_details_3, add_details_4, add_details_5, remarks, payment_ref_no, status, liquidation_date, customer_ref_no, instrument_ref_no, instrument_no, utr_no ) VALUES ( :company_id, :debit_account_no, :beneficiary_account_no, :beneficiary_name, :amount, :payment_mode, :payment_date, :ifsc_code, :payable_location_name, :print_location_name, :bene_mobile_no, :bene_email_id, :bene_add_1, :bene_add_2, :bene_add_3, :bene_add_4, :add_details_1, :add_details_2, :add_details_3, :add_details_4, :add_details_5, :remarks, :payment_ref_no, :status, :liquidation_date, :customer_ref_no, :instrument_ref_no, :instrument_no, :utr_no ) "); $imported = 0; $skipped = 0; while (($row = fgetcsv($handle)) !== false) { if (count($row) < 5) { $skipped++; continue; } if (empty(array_filter($row))) { $skipped++; continue; } $insert->execute([ ':company_id' => $company_id, ':debit_account_no' => trim(str_replace("'", '', $row[0] ?? '')), ':beneficiary_account_no' => trim(str_replace("'", '', $row[1] ?? '')), ':beneficiary_name' => trim($row[2] ?? ''), ':amount' => (float)str_replace(',', '', ($row[3] ?? 0)), ':payment_mode' => trim($row[4] ?? ''), ':payment_date' => parse_date_value($row[5] ?? ''), ':ifsc_code' => trim($row[6] ?? ''), ':payable_location_name' => trim($row[7] ?? ''), ':print_location_name' => trim($row[8] ?? ''), ':bene_mobile_no' => trim($row[9] ?? ''), ':bene_email_id' => trim($row[10] ?? ''), ':bene_add_1' => trim($row[11] ?? ''), ':bene_add_2' => trim($row[12] ?? ''), ':bene_add_3' => trim($row[13] ?? ''), ':bene_add_4' => trim($row[14] ?? ''), ':add_details_1' => trim($row[15] ?? ''), ':add_details_2' => trim($row[16] ?? ''), ':add_details_3' => trim($row[17] ?? ''), ':add_details_4' => trim($row[18] ?? ''), ':add_details_5' => trim($row[19] ?? ''), ':remarks' => trim($row[20] ?? ''), ':payment_ref_no' => trim($row[21] ?? ''), ':status' => trim($row[22] ?? ''), ':liquidation_date' => parse_date_value($row[23] ?? ''), ':customer_ref_no' => trim($row[24] ?? ''), ':instrument_ref_no' => trim($row[25] ?? ''), ':instrument_no' => trim($row[26] ?? ''), ':utr_no' => trim($row[27] ?? ''), ]); $imported++; } fclose($handle); $message = "Import completed. Imported: {$imported}, Skipped: {$skipped}"; } } } ?> <div class="container-fluid py-3"> <div class="card shadow-sm"> <div class="card-header"> <h4 class="mb-0">CMS Payment CSV Import</h4> </div> <div class="card-body"> <?php if ($message): ?> <div class="alert alert-success"> <?= h($message) ?> </div> <?php endif; ?> <?php if ($error): ?> <div class="alert alert-danger"> <?= h($error) ?> </div> <?php endif; ?> <form method="POST" enctype="multipart/form-data"> <input type="hidden" name="csrf_token" value="<?= h($_SESSION['csrf_token']) ?>" > <div class="mb-3"> <label class="form-label"> Select CSV File </label> <input type="file" name="csv_file" accept=".csv" class="form-control" required > </div> <button type="submit" class="btn btn-primary"> Import CSV </button> </form> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>