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.

95 lines
3.0 KiB
PHP

<?php
require 'vendor/autoload.php';
require_once 'includes/db.php';
require_once 'includes/auth.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
checkAuth();
$message = '';
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_FILES['excel_file'])) {
requireCsrfToken();
$file = $_FILES['excel_file']['tmp_name'];
try {
$spreadsheet = IOFactory::load($file);
$worksheet = $spreadsheet->getActiveSheet();
$rows = $worksheet->toArray();
unset($rows[0]);
$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;
foreach ($rows as $row) {
if (empty($row[1])) {
continue;
}
$stmt->execute([
$row[1],
(int)$row[2],
$row[3],
(float)$row[4],
$row[5],
$row[6],
$row[7] ?? 'nowe'
]);
$count++;
}
$pdo->commit();
$message = "<div class='alert alert-success'>Pomyslnie zaimportowano $count zamowien.</div>";
} catch (Exception $e) {
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
error_log($e->getMessage());
$message = "<div class='alert alert-danger'>Import nie powiodl sie.</div>";
}
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Import zamowien - <?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 Excel (.xlsx)</h4>
</div>
<div class="card-body">
<?php echo $message; ?>
<div class="alert alert-info">
<strong>Instrukcja:</strong><br>
1. System pomija pierwsza kolumne (LP).<br>
2. Kolejne kolumny to: Produkt, Ilosc, Miejsce, Cena, Data (RRRR-MM-DD), Notatki, Status.
</div>
<form method="POST" enctype="multipart/form-data">
<?php echo csrfInput(); ?>
<div class="mb-3">
<label class="form-label">Wybierz plik Excel</label>
<input type="file" name="excel_file" class="form-control" accept=".xlsx, .xls" required>
</div>
<div class="d-flex justify-content-between">
<a href="index.php" class="btn btn-secondary">Powrot</a>
<button type="submit" class="btn btn-success">Rozpocznij import</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>