125 lines
4.9 KiB
PHP
125 lines
4.9 KiB
PHP
|
|
<?php
|
|
require_once 'db/config.php';
|
|
|
|
$error_message = null;
|
|
$import_summary = null;
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
|
|
if ($_FILES['csv_file']['error'] == UPLOAD_ERR_OK && is_uploaded_file($_FILES['csv_file']['tmp_name'])) {
|
|
$file = $_FILES['csv_file']['tmp_name'];
|
|
|
|
$pdo = db();
|
|
$pdo->beginTransaction();
|
|
|
|
$success_count = 0;
|
|
$error_count = 0;
|
|
$errors = [];
|
|
|
|
try {
|
|
$handle = fopen($file, "r");
|
|
$header = fgetcsv($handle, 1000, ","); // Read header row
|
|
|
|
// Prepare statement for checking duplicates
|
|
$check_stmt = $pdo->prepare("SELECT COUNT(*) FROM tabelmaster WHERE NO_FAKTUR = ? AND NAMA_SALES = ? AND TANGGAL = ?");
|
|
|
|
// Prepare statement for insertion
|
|
$insert_stmt = $pdo->prepare("INSERT INTO tabelmaster (NAMA_SALES, TANGGAL, NO_FAKTUR, MERK, OUTLET, PRODUK, QTY, HARGA, BULAN, TAHUN, PERIODE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
|
|
|
$row_number = 1;
|
|
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
|
|
$row_number++;
|
|
if (count($data) !== 11) {
|
|
$errors[] = "Row {$row_number}: Invalid column count.";
|
|
$error_count++;
|
|
continue;
|
|
}
|
|
|
|
// Assign variables from CSV data
|
|
list($nama_sales, $tanggal, $no_faktur, $merk, $outlet, $produk, $qty, $harga, $bulan, $tahun, $periode) = $data;
|
|
|
|
// Basic Validation
|
|
$check_stmt->execute([$no_faktur, $nama_sales, $tanggal]);
|
|
if ($check_stmt->fetchColumn() > 0) {
|
|
$errors[] = "Row {$row_number}: Duplicate record found for NO_FAKTUR {$no_faktur}, NAMA_SALES {$nama_sales}, TANGGAL {$tanggal}.";
|
|
$error_count++;
|
|
continue;
|
|
}
|
|
|
|
// Data Normalization
|
|
$harga_numeric = str_replace(['Rp', '.', ','], '', $harga);
|
|
|
|
// Insert data
|
|
$insert_stmt->execute([
|
|
$nama_sales, $tanggal, $no_faktur, $merk, $outlet, $produk,
|
|
(int)$qty, $harga_numeric, (int)$bulan, (int)$tahun, $periode
|
|
]);
|
|
|
|
$success_count++;
|
|
}
|
|
fclose($handle);
|
|
|
|
$pdo->commit();
|
|
$import_summary = ['success' => $success_count, 'errors' => $error_count, 'error_details' => $errors];
|
|
|
|
} catch (Exception $e) {
|
|
$pdo->rollBack();
|
|
$error_message = "An error occurred during import: " . $e->getMessage();
|
|
}
|
|
} else {
|
|
$error_message = "File upload failed. Please try again.";
|
|
}
|
|
}
|
|
|
|
require_once 'header.php';
|
|
?>
|
|
|
|
<main class="container">
|
|
<?php if (isset($error_message)): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php echo htmlspecialchars($error_message); ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (isset($import_summary)): ?>
|
|
<div class="card mb-4">
|
|
<div class="card-header">Import Summary</div>
|
|
<div class="card-body">
|
|
<p class="text-success">Successfully imported <strong><?php echo $import_summary['success']; ?></strong> records.</p>
|
|
<p class="text-danger">Failed to import <strong><?php echo $import_summary['errors']; ?></strong> records.</p>
|
|
<?php if (!empty($import_summary['error_details'])): ?>
|
|
<hr>
|
|
<h5>Error Details:</h5>
|
|
<ul class="list-group">
|
|
<?php foreach ($import_summary['error_details'] as $error): ?>
|
|
<li class="list-group-item list-group-item-danger">- <?php echo htmlspecialchars($error); ?></li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card">
|
|
<div class="card-header">
|
|
Import Data from CSV
|
|
</div>
|
|
<div class="card-body">
|
|
<form method="POST" action="import.php" enctype="multipart/form-data">
|
|
<div class="mb-3">
|
|
<label for="csv_file" class="form-label">Select CSV File</label>
|
|
<input class="form-control" type="file" id="csv_file" name="csv_file" accept=".csv" required>
|
|
</div>
|
|
<div class="alert alert-info">
|
|
<strong>CSV Format:</strong> The file must have 11 columns in the following order: <br>
|
|
<code>NAMA_SALES, TANGGAL, NO_FAKTUR, MERK, OUTLET, PRODUK, QTY, HARGA, BULAN, TAHUN, PERIODE</code>
|
|
<br>The first row should be the header and will be skipped.
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Upload and Import</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<?php require_once 'footer.php'; ?>
|