« Back to History
parcel_stock_import.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php error_reporting(E_ALL); ini_set('display_errors', 1); /* ---------- AUTH / CONTEXT ---------- */ require_once __DIR__ . '/modules/auth/page_acl.php'; require_once __DIR__ . '/helpers/activity_helper.php'; // After successful import, add: activity_create('parcel', 'parcel_stock_import', 0, 'Parcel stock import started'); $ctx = page_require_access('parcel_stock_import'); $pdo = $ctx['pdo']; $COMPANY_ID = (int)$ctx['company_id']; $USER_ID = (int)$ctx['user']['id']; /* ---------- HELPERS ---------- */ function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } /* ---------- CITY CHECK ---------- */ function is_surat($value): bool { return trim(mb_strtolower((string)$value)) === 'surat'; } /* ---------- CSV DATE PARSER ---------- */ function parse_csv_date($raw) { $raw = trim($raw); if ($raw === '') return null; $raw = str_replace(['/', '.'], '-', $raw); if (preg_match('/^\d{2}-\d{2}-\d{2}$/', $raw)) { $dt = DateTime::createFromFormat('d-m-y', $raw); } elseif (preg_match('/^\d{2}-\d{2}-\d{4}$/', $raw)) { $dt = DateTime::createFromFormat('d-m-Y', $raw); } else { return null; } return $dt ? $dt->format('Y-m-d') : null; } function financial_year_from_date($ymd) { if (!$ymd) return ''; $ts = strtotime($ymd); if ($ts === false) return ''; $year = (int)date('Y', $ts); $month = (int)date('n', $ts); if ($month >= 4) { return $year . '-' . ($year + 1); } return ($year - 1) . '-' . $year; } $msg = $err = ''; $previewRows = []; $tmpFilePath = ''; /* ================= PREVIEW ================= */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['preview'])) { if ( !isset($_FILES['import_file']) || $_FILES['import_file']['error'] !== UPLOAD_ERR_OK ) { $err = 'Please choose a CSV file first.'; } else { $name = strtolower($_FILES['import_file']['name']); if (pathinfo($name, PATHINFO_EXTENSION) !== 'csv') { $err = 'Please upload CSV file only.'; } else { $tmpFilePath = sys_get_temp_dir() . '/parcel_import_' . time() . '_' . rand(1000,9999) . '.csv'; move_uploaded_file($_FILES['import_file']['tmp_name'], $tmpFilePath); if (($fh = fopen($tmpFilePath, 'r')) !== false) { $rowNo = 0; while (($r = fgetcsv($fh)) !== false) { $rowNo++; if ($rowNo <= 2) continue; $party = trim($r[1] ?? ''); $billRaw = trim($r[2] ?? ''); $bill_no = trim($r[3] ?? ''); $transport = trim($r[4] ?? ''); $station = trim($r[5] ?? ''); /* ❌ Skip Surat */ if (is_surat($station) || is_surat($party)) { continue; } if (!$party || !$billRaw || !$bill_no) continue; $parsed = parse_csv_date($billRaw); $previewRows[] = [ 'party' => $party, 'bill_raw' => $billRaw, 'bill_date' => $parsed ?: 'INVALID', 'bill_no' => $bill_no, 'transport' => $transport, 'station' => $station ]; if (count($previewRows) >= 20) break; } fclose($fh); } } } } /* ================= IMPORT ================= */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['confirm_import'])) { $tmpFilePath = $_POST['csv_path'] ?? ''; if (!is_file($tmpFilePath)) { $err = 'Temporary CSV file missing. Please upload again.'; } else { try { $pdo->beginTransaction(); $inserted = 0; $skipped = 0; $rowNo = 0; if (($fh = fopen($tmpFilePath, 'r')) !== false) { while (($r = fgetcsv($fh)) !== false) { $rowNo++; if ($rowNo <= 2) continue; $party = trim($r[1] ?? ''); $billRaw = trim($r[2] ?? ''); $bill_no = trim($r[3] ?? ''); $transport = trim($r[4] ?? ''); $station = trim($r[5] ?? ''); /* ❌ Skip Surat */ if (is_surat($station) || is_surat($party)) { $skipped++; continue; } if (!$party || !$billRaw || !$bill_no) { $skipped++; continue; } $bill_date = parse_csv_date($billRaw); if (!$bill_date) { $skipped++; continue; } $financial_year = financial_year_from_date($bill_date); try { $pdo->prepare(" INSERT INTO parcel_stock_in (company_id, financial_year, bill_date, bill_no, party, station, transport, status, created_at, created_by) VALUES (?,?,?,?,?,?,?,'in',NOW(),?) ")->execute([ $COMPANY_ID, $financial_year, $bill_date, $bill_no, $party, $station, $transport, $USER_ID ]); $inserted++; } catch (PDOException $e) { $skipped++; continue; } } fclose($fh); } unlink($tmpFilePath); $pdo->commit(); if ($inserted > 0) { activity_create('parcel', 'parcel_stock_import', 0, 'Parcel stock imported'); // activity_log([ // 'company_id' => $COMPANY_ID ?? 0, // 'user_id' => $USER_ID ?? ($_SESSION['user_id'] ?? 0), // 'module' => 'parcel', // 'action_name' => 'create', // 'entity_type' => 'parcel_stock_import', // 'entity_id' => 0, // 'remarks' => 'Parcel stock import completed' // ]); } $msg = "Import completed. Inserted: $inserted, Skipped: $skipped"; } catch (Throwable $e) { $pdo->rollBack(); $err = $e->getMessage(); } } } /* ---------- UI ---------- */ require_once __DIR__ . '/partials/header.php'; ?> <div class="wrap"> <div class="d-flex justify-content-between align-items-center mb-4"> <h2 class="fw-bold"><i class="bi bi-file-earmark-arrow-up text-primary me-2"></i> Parcel Stock Import</h2> <span class="badge bg-light text-dark border shadow-sm px-3 py-2">Accepted Format: .CSV</span> </div> <?php if ($err): ?> <div class="alert alert-danger shadow-sm border-start border-4 border-danger rounded-3"> <i class="bi bi-exclamation-octagon-fill me-2"></i> <?= h($err) ?> </div> <?php endif; ?> <?php if ($msg): ?> <div class="alert alert-success shadow-sm border-start border-4 border-success rounded-3"> <i class="bi bi-check-circle-fill me-2"></i> <?= h($msg) ?> </div> <?php endif; ?> <?php if ($previewRows): ?> <div class="card shadow-sm border-0 mb-4"> <div class="card-header bg-white py-3 d-flex justify-content-between align-items-center"> <h6 class="m-0 fw-bold text-primary"><i class="bi bi-eye me-2"></i> Preview (First 20 Rows)</h6> <span class="text-muted small">Kripya data check karein import karne se pehle</span> </div> <div class="card-body p-0"> <div class="table-responsive"> <table class="table table-hover table-striped align-middle mb-0"> <thead class="table-light"> <tr> <th class="ps-4">Party</th> <th>Bill Date (CSV)</th> <th>Parsed Date</th> <th>Bill No</th> <th>Transport</th> <th class="pe-4">Station</th> </tr> </thead> <tbody> <?php foreach ($previewRows as $r): ?> <tr> <td class="ps-4 fw-medium text-dark"><?= h($r['party']) ?></td> <td><code class="text-muted"><?= h($r['bill_raw']) ?></code></td> <td><span class="badge bg-info-soft text-info"><?= h($r['bill_date']) ?></span></td> <td><span class="fw-bold text-primary"><?= h($r['bill_no']) ?></span></td> <td><?= h($r['transport']) ?></td> <td class="pe-4"><?= h($r['station']) ?></td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> <div class="card-footer bg-white text-end py-3"> <form method="post" class="d-inline"> <input type="hidden" name="csv_path" value="<?= h($tmpFilePath) ?>"> <a href="?" class="btn btn-link text-muted me-3 text-decoration-none">Cancel</a> <button class="btn btn-primary px-5 fw-bold shadow-sm" name="confirm_import" value="1"> <i class="bi bi-cloud-upload-fill me-2"></i> Confirm & Import Data </button> </form> </div> </div> <?php else: ?> <div class="row justify-content-center"> <div class="col-md-6"> <div class="card shadow-sm border-0"> <div class="card-body p-5 text-center"> <div class="mb-4"> <i class="bi bi-filetype-csv display-1 text-primary opacity-25"></i> </div> <h4 class="fw-bold mb-3">Upload CSV File</h4> <p class="text-muted mb-4 small">Stock data ko bulk mein import karne ke liye CSV file select karein.</p> <form method="post" enctype="multipart/form-data"> <div class="mb-4 text-start"> <label class="form-label small fw-bold text-muted">CHOOSE FILE</label> <input type="file" name="import_file" class="form-control form-control-lg" accept=".csv" required> </div> <button class="btn btn-primary btn-lg w-100 fw-bold shadow-sm" name="preview" value="1"> <i class="bi bi-search me-2"></i> Preview CSV Data </button> </form> </div> </div> </div> </div> <?php endif; ?> </div> <style> /* Styling for a modern ERP feel */ .bg-info-soft { background-color: #e0f7fa; } .table thead th { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.5px; font-weight: 700; color: #6c757d; border-top: none; } .form-control-lg { font-size: 0.9rem; border: 2px dashed #dee2e6; } .form-control-lg:focus { border-style: solid; } </style> <?php require_once __DIR__ . '/partials/footer.php'; ?>