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.
96 lines
3.4 KiB
PHP
96 lines
3.4 KiB
PHP
<?php
|
|
require 'vendor/autoload.php'; // Ładowanie biblioteki PhpSpreadsheet
|
|
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'])) {
|
|
$file = $_FILES['excel_file']['tmp_name'];
|
|
|
|
try {
|
|
$spreadsheet = IOFactory::load($file);
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
$rows = $worksheet->toArray();
|
|
|
|
// Pomijamy nagłówek (pierwszy wiersz w Excelu)
|
|
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) {
|
|
// Zgodnie z wymaganiem: pomijamy 1. kolumnę ($row[0])
|
|
// Mapowanie kolumn Excela:
|
|
// $row[1] -> Produkt, $row[2] -> Ilość, $row[3] -> Miejsce,
|
|
// $row[4] -> Cena, $row[5] -> Data, $row[6] -> Notatki, $row[7] -> Status
|
|
|
|
if (empty($row[1])) continue; // Pomiń puste wiersze
|
|
|
|
$stmt->execute([
|
|
$row[1], // Produkt
|
|
(int)$row[2], // Ilość
|
|
$row[3], // Miejsce zakupu
|
|
(float)$row[4], // Cena
|
|
$row[5], // Data dostawy (format YYYY-MM-DD)
|
|
$row[6], // Notatki
|
|
$row[7] ?? 'nowe' // Status
|
|
]);
|
|
$count++;
|
|
}
|
|
$pdo->commit();
|
|
$message = "<div class='alert alert-success'>Pomyślnie zaimportowano $count zamówień!</div>";
|
|
|
|
} catch (Exception $e) {
|
|
if ($pdo->inTransaction()) $pdo->rollBack();
|
|
$message = "<div class='alert alert-danger'>Błąd importu: " . $e->getMessage() . "</div>";
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="pl">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Import Zamówień - <?php echo 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 pierwszą kolumnę (LP).<br>
|
|
2. Kolejne kolumny to: Produkt, Ilość, Miejsce, Cena, Data (RRRR-MM-DD), Notatki, Status.
|
|
</div>
|
|
|
|
<form method="POST" enctype="multipart/form-data">
|
|
<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">Powrót</a>
|
|
<button type="submit" class="btn btn-success">Rozpocznij import</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|