« Back to History
gray_receive_import.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('gray_receive_import'); $pdo = $ctx['pdo'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); require_once __DIR__ . '/partials/header.php'; $preview_data = []; $message = ''; $error_rows = 0; /* ================= DATE PARSER (ROBUST) ================= */ function parse_date_safe($date) { $date = trim($date); if ($date === '') return null; // Excel serial date if (is_numeric($date)) { $unix = ($date - 25569) * 86400; return gmdate("Y-m-d", $unix); } // remove time $date = explode(' ', $date)[0]; $formats = [ 'd-m-y','d-m-Y', 'd/m/y','d/m/Y', 'Y-m-d','y-m-d' ]; foreach ($formats as $f) { $dt = DateTime::createFromFormat($f, $date); if ($dt) return $dt->format('Y-m-d'); } return null; } /* ================= PREVIEW ================= */ if (isset($_POST['preview']) && isset($_FILES['csv_file'])) { $file = $_FILES['csv_file']['tmp_name']; $handle = fopen($file, "r"); if (!$handle) exit("File error"); // detect delimiter $first = fgets($handle); rewind($handle); $delimiter = (substr_count($first, ';') > substr_count($first, ',')) ? ';' : ','; $row = 0; $last_mill = ''; $last_gp = ''; $last_date = ''; while (($data = fgetcsv($handle, 10000, $delimiter)) !== FALSE) { $row++; if (count(array_filter($data)) == 0) continue; // skip header row if ($row == 1) continue; // stop at GRAND TOTAL if (stripos($data[0] ?? '', 'GRAND') !== false) break; $mill = trim($data[0] ?? ''); $gpno = trim($data[1] ?? ''); $date = trim($data[2] ?? ''); $quality = trim($data[3] ?? ''); if ($mill !== '') $last_mill = $mill; if ($gpno !== '') $last_gp = $gpno; if ($date !== '') $last_date = $date; $mill = $last_mill; $gpno = $last_gp; $date = $last_date; $normalized_date = parse_date_safe($date); if (!$mill || !$gpno || !$normalized_date || !$quality) { $error_rows++; continue; } $grey = (float)($data[4] ?? 0); $pcs = (int)($data[5] ?? 0); $rec = (float)($data[6] ?? 0); $key = $gpno.'|'.$normalized_date.'|'.$quality; if (!isset($preview_data[$key])) { $preview_data[$key] = [ 'mill_name'=>$mill, 'gpno'=>$gpno, 'rec_date'=>$normalized_date, 'quality'=>$quality, 'grey_mts'=>0, 'rec_pcs'=>0, 'rec_mts'=>0 ]; } $preview_data[$key]['grey_mts'] += $grey; $preview_data[$key]['rec_pcs'] += $pcs; $preview_data[$key]['rec_mts'] += $rec; } fclose($handle); } /* ================= IMPORT ================= */ if (isset($_POST['import']) && isset($_POST['data_json'])) { $rows = json_decode($_POST['data_json'], true); $inserted = 0; $skipped = 0; foreach ($rows as $r) { $check = $pdo->prepare(" SELECT id FROM gray_recieve WHERE company_id = :cid AND gpno = :gp AND rec_date = :rd AND quality = :q "); $check->execute([ ':cid'=>$company_id, ':gp'=>$r['gpno'], ':rd'=>$r['rec_date'], ':q'=>$r['quality'] ]); if ($check->fetch()) { $skipped++; continue; } $stmt = $pdo->prepare(" INSERT INTO gray_recieve (company_id,mill_name,gpno,rec_date,quality, grey_mts,rec_pcs,rec_mts,remark) VALUES (:cid,:mill,:gp,:rd,:q, :gm,:pcs,:rm,'') "); $stmt->execute([ ':cid'=>$company_id, ':mill'=>$r['mill_name'], ':gp'=>$r['gpno'], ':rd'=>$r['rec_date'], ':q'=>$r['quality'], ':gm'=>$r['grey_mts'], ':pcs'=>$r['rec_pcs'], ':rm'=>$r['rec_mts'] ]); $inserted++; } $message = "Imported: $inserted | Skipped Duplicate: $skipped"; } ?> <div class="container mt-4"> <div class="card p-3"> <h4>Gray Receive CSV Import</h4> <?php if($message): ?> <div class="alert alert-success"><?= $message ?></div> <?php endif; ?> <?php if($error_rows>0): ?> <div class="alert alert-warning"> Skipped Invalid Rows: <?= $error_rows ?> </div> <?php endif; ?> <form method="post" enctype="multipart/form-data" class="mb-3"> <input type="file" name="csv_file" required class="form-control mb-2"> <button type="submit" name="preview" class="btn btn-primary">Preview</button> </form> <?php if(!empty($preview_data)): ?> <form method="post"> <input type="hidden" name="data_json" value='<?= json_encode(array_values($preview_data), JSON_HEX_APOS|JSON_HEX_QUOT) ?>'> <div class="table-responsive"> <table class="table table-bordered table-sm"> <thead> <tr> <th>Mill</th> <th>GP No</th> <th>Date</th> <th>Quality</th> <th>Grey</th> <th>PCS</th> <th>Rec</th> </tr> </thead> <tbody> <?php foreach($preview_data as $r): ?> <tr> <td><?= $r['mill_name'] ?></td> <td><?= $r['gpno'] ?></td> <td><?= $r['rec_date'] ?></td> <td><?= $r['quality'] ?></td> <td><?= $r['grey_mts'] ?></td> <td><?= $r['rec_pcs'] ?></td> <td><?= $r['rec_mts'] ?></td> </tr> <?php endforeach; ?> </tbody> </table> </div> <button type="submit" name="import" class="btn btn-success"> Confirm Import </button> </form> <?php endif; ?> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>