You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
3.0 KiB
PHP
92 lines
3.0 KiB
PHP
<?php
|
|
require_once 'includes/db.php';
|
|
require_once 'includes/auth.php';
|
|
|
|
checkAuth();
|
|
|
|
$message = '';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_FILES['csv_file'])) {
|
|
requireCsrfToken();
|
|
$file = $_FILES['csv_file']['tmp_name'];
|
|
|
|
if (($handle = fopen($file, "r")) !== false) {
|
|
fgetcsv($handle, 1000, ";");
|
|
|
|
try {
|
|
$pdo->beginTransaction();
|
|
$sql = "INSERT INTO " . DB_PREFIX . "orders
|
|
(product_name, quantity, purchase_place, price_per_unit, delivery_date, notes, status)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)";
|
|
$stmt = $pdo->prepare($sql);
|
|
|
|
$count = 0;
|
|
while (($row = fgetcsv($handle, 1000, ";")) !== false) {
|
|
foreach ($row as $key => $value) {
|
|
$row[$key] = mb_convert_encoding($value, "UTF-8", "auto");
|
|
}
|
|
|
|
if (empty($row[1])) {
|
|
continue;
|
|
}
|
|
|
|
$stmt->execute([
|
|
$row[1],
|
|
(int)$row[2],
|
|
$row[3],
|
|
(float)str_replace(',', '.', $row[4]),
|
|
$row[5],
|
|
$row[6],
|
|
$row[7] ?? 'nowe'
|
|
]);
|
|
$count++;
|
|
}
|
|
$pdo->commit();
|
|
$message = "<div class='alert alert-success'>Zaimportowano $count zamówień z pliku CSV.</div>";
|
|
} catch (Exception $e) {
|
|
$pdo->rollBack();
|
|
error_log($e->getMessage());
|
|
$message = "<div class='alert alert-danger'>Import nie powiódł się.</div>";
|
|
}
|
|
fclose($handle);
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="pl">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Import CSV - <?php echo e(APP_NAME); ?></title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
</head>
|
|
<body class="bg-light">
|
|
<div class="container py-5">
|
|
<div class="card shadow mx-auto" style="max-width: 600px;">
|
|
<div class="card-header bg-success text-white">
|
|
<h4 class="mb-0">Import z pliku CSV</h4>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php echo $message; ?>
|
|
|
|
<div class="alert alert-warning small">
|
|
<strong>Ważne:</strong> W Excelu wybierz <em>Zapisz jako</em> -> <strong>CSV (rozdzielany średnikami)</strong>.
|
|
</div>
|
|
|
|
<form method="POST" enctype="multipart/form-data">
|
|
<?php echo csrfInput(); ?>
|
|
<div class="mb-3">
|
|
<label class="form-label">Wybierz plik .csv</label>
|
|
<input type="file" name="csv_file" class="form-control" accept=".csv" required>
|
|
</div>
|
|
<div class="d-flex justify-content-between">
|
|
<a href="index.php" class="btn btn-secondary">Powrót</a>
|
|
<button type="submit" class="btn btn-success">Importuj dane</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|