« Back to History
sales_import.php
|
20260921_175631.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; require_once __DIR__ . '/modules/activity/activity_logger.php'; $ctx = page_require_access('sales_import'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; $u = $ctx['user'] ?? null; require_once __DIR__ . '/partials/header.php'; function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } /* ===== FINAL DATE PARSER ===== */ function parseDateSafe($d){ $d = trim($d); if(!$d) return null; // dd/mm/yy OR dd-mm-yy if(preg_match('/^(\d{2})[\/\-](\d{2})[\/\-](\d{2})$/', $d, $m)){ $year = (int)$m[3]; $year = ($year < 50) ? 2000 + $year : 1900 + $year; return $year . '-' . $m[2] . '-' . $m[1]; } // dd/mm/yyyy OR dd-mm-yyyy if(preg_match('/^(\d{2})[\/\-](\d{2})[\/\-](\d{4})$/', $d, $m)){ return $m[3] . '-' . $m[2] . '-' . $m[1]; } // yyyy-mm-dd if(preg_match('/^\d{4}-\d{2}-\d{2}$/', $d)){ return $d; } return null; } $data = []; /* ================= PREVIEW ================= */ if(isset($_POST['preview'])){ if(isset($_FILES['file']) && $_FILES['file']['tmp_name']){ $file = fopen($_FILES['file']['tmp_name'], 'r'); $i = 0; while(($row = fgetcsv($file)) !== false){ if($i++ < 3) continue; $parsed_date = parseDateSafe($row[1] ?? ''); $data[] = [ 'customer' => trim($row[0] ?? ''), 'date_raw' => trim($row[1] ?? ''), 'date' => $parsed_date, 'bill' => (int)($row[2] ?? 0), 'screen' => trim($row[3] ?? ''), 'pcs' => (float)($row[4] ?? 0), 'mts' => (float)($row[5] ?? 0), 'cut' => (float)($row[6] ?? 0), ]; } fclose($file); } } /* ================= IMPORT ================= */ if(isset($_POST['import'])){ $rows = json_decode($_POST['json_data'], true); $stmt = $pdo->prepare(" INSERT INTO sales_entry (company_id, sale_date, bill_no, customer_name, screen_name, pcs, mts, cut) VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE customer_name = VALUES(customer_name), screen_name = VALUES(screen_name), pcs = VALUES(pcs), mts = VALUES(mts), cut = VALUES(cut) "); $inserted = 0; $skipped = 0; foreach($rows as $r){ $date = $r['date'] ?? null; // skip invalid if(!$date || $date === '1970-01-01'){ $skipped++; continue; } $stmt->execute([ $company_id, $date, $r['bill'], $r['customer'], $r['screen'], $r['pcs'], $r['mts'], $r['cut'] ]); $inserted++; } activity_log([ 'company_id' => $company_id ?? 0, 'user_id' => $u['id'] ?? ($_SESSION['user_id'] ?? 0), 'module' => 'sales', 'action_name' => 'create', 'entity_type' => 'sales_import', 'entity_id' => 0, 'remarks' => 'Sales import completed' ]); echo "<div class='alert alert-success'> Import Completed<br> Inserted: $inserted<br> Skipped (Invalid Date): $skipped </div>"; } ?> <div class="container py-4"> <h4 class="fw-bold mb-3">Sales Import</h4> <div class="card mb-4"> <div class="card-body"> <form method="post" enctype="multipart/form-data"> <div class="row g-3"> <div class="col-md-4"> <input type="file" name="file" class="form-control" required> </div> <div class="col-md-2"> <button name="preview" class="btn btn-primary w-100">Preview</button> </div> </div> </form> </div> </div> <?php if($data): ?> <div class="card"> <div class="card-body"> <form method="post"> <input type="hidden" name="json_data" value='<?= json_encode($data) ?>'> <div class="table-responsive"> <table class="table table-bordered table-sm"> <thead class="table-light"> <tr> <th>Customer</th> <th>Raw Date</th> <th>Parsed Date</th> <th>Bill</th> <th>Screen</th> <th>PCS</th> <th>MTS</th> <th>CUT</th> </tr> </thead> <tbody> <?php foreach($data as $d): ?> <tr class="<?= !$d['date'] ? 'table-danger' : '' ?>"> <td><?= h($d['customer']) ?></td> <td><?= h($d['date_raw']) ?></td> <td> <?= $d['date'] ? h($d['date']) : '<span class="text-danger">Invalid</span>' ?> </td> <td><?= h($d['bill']) ?></td> <td><?= h($d['screen']) ?></td> <td><?= h($d['pcs']) ?></td> <td><?= h($d['mts']) ?></td> <td><?= h($d['cut']) ?></td> </tr> <?php endforeach; ?> </tbody> </table> </div> <button name="import" class="btn btn-success mt-3"> Confirm Import </button> </form> </div> </div> <?php endif; ?> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>