« Back to History
yarn_stock_import.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/../modules/auth/page_acl.php'; $ctx = page_require_access('mesr_yarn_stock_import'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } $message = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) { if ($_FILES['csv_file']['error'] === 0) { $month_input = $_POST['month'] ?? date('Y-m'); $month = date('Y-m-01', strtotime($month_input.'-01')); $file = fopen($_FILES['csv_file']['tmp_name'], 'r'); if ($file) { // Remove old month data $pdo->prepare(" DELETE FROM mesr_yarn_in_yarn_stock WHERE company_id = :cid AND month = :month ")->execute([ ':cid'=>$company_id, ':month'=>$month ]); // Skip header fgetcsv($file); $insert = $pdo->prepare(" INSERT INTO mesr_yarn_in_yarn_stock ( company_id, month, location_id, stock_type, yarn_type, supplier_name, denier, color, total_boxes, total_weight, avg_per_box, rate, value, status ) VALUES ( :cid, :month, 16, :stock_type, :yarn_type, :supplier, :denier, :color, :boxes, :weight, :avg, :rate, :value, :status ) "); $count = 0; while (($row = fgetcsv($file)) !== false) { if (count($row) < 11) continue; $stock_type = trim($row[0]); $yarn_type = trim($row[1]); $supplier = trim($row[2]); $denier = trim($row[3]); $color = trim($row[4]); $boxes = (float)str_replace(',', '', $row[5]); $weight = (float)str_replace(',', '', $row[6]); $avg = (float)str_replace(',', '', $row[7]); $rate = (float)str_replace(',', '', $row[8]); $value = (float)str_replace(',', '', $row[9]); $status = trim($row[10]); if ($denier === '') continue; $insert->execute([ ':cid'=>$company_id, ':month'=>$month, ':stock_type'=>$stock_type, ':yarn_type'=>$yarn_type, ':supplier'=>$supplier, ':denier'=>$denier, ':color'=>$color, ':boxes'=>$boxes, ':weight'=>$weight, ':avg'=>$avg, ':rate'=>$rate, ':value'=>$value, ':status'=>$status ?: 'OK' ]); $count++; } fclose($file); $message = "Import successful. Rows: ".$count; } } } require_once __DIR__ . '/../partials/header.php'; ?> <div class="card"> <h2>Yarn Stock Import (Full Excel Format)</h2> <?php if ($message): ?> <div class="notice"><?= h($message) ?></div> <?php endif; ?> <form method="post" enctype="multipart/form-data" class="form-grid"> <div> <label>Select Month</label> <input type="month" name="month" required> </div> <div> <label>Upload CSV</label> <input type="file" name="csv_file" accept=".csv" required> </div> <div style="align-self:end;"> <button class="btn">Import</button> </div> </form> <p> Expected CSV Order:<br> Stock Type, Yarn Type, Company, Denier, Color, Boxes, Weight, AVG/Box, Rate, Value, Status </p> </div> <?php require_once __DIR__ . '/../partials/footer.php'; ?>