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.

142 lines
6.6 KiB
PHP

<?php
require_once 'includes/db.php';
require_once 'includes/auth.php';
checkAuth();
$message = '';
if (isset($_GET['saved']) && $_GET['saved'] === '1') {
$message = "<div class='alert alert-success'>Zamówienie dodane pomyślnie. Możesz dodać kolejne.</div>";
}
if ($_SERVER["REQUEST_METHOD"] === "POST") {
requireCsrfToken();
$product_name = trim($_POST['product_name'] ?? '');
$quantity = (int)($_POST['quantity'] ?? 0);
$purchase_place = trim($_POST['purchase_place'] ?? '');
$price = (float)str_replace(',', '.', $_POST['price_per_unit'] ?? '0');
$delivery_date = $_POST['delivery_date'] ?? '';
$notes = trim($_POST['notes'] ?? '');
$recipient = trim($_POST['recipient'] ?? '');
$delivery_address = trim($_POST['delivery_address'] ?? '');
$company = trim($_POST['company'] ?? '');
$submit_action = $_POST['submit_action'] ?? 'save_and_list';
if ($product_name !== '' && $quantity > 0) {
try {
$pdo->beginTransaction();
$sql = "INSERT INTO " . DB_PREFIX . "orders
(product_name, quantity, purchase_place, price_per_unit, delivery_date, notes, recipient, delivery_address, company, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'nowe')";
$stmt = $pdo->prepare($sql);
$stmt->execute([$product_name, $quantity, $purchase_place, $price, $delivery_date, $notes, $recipient, $delivery_address, $company]);
$order_id = $pdo->lastInsertId();
$hist_sql = "INSERT INTO " . DB_PREFIX . "order_history (order_id, user_id, action) VALUES (?, ?, ?)";
$hist_stmt = $pdo->prepare($hist_sql);
$hist_stmt->execute([$order_id, $_SESSION['user_id'], 'Utworzono nowe zamówienie.']);
$pdo->commit();
if ($submit_action === 'save_and_add_new') {
header("Location: add_order.php?saved=1");
exit();
}
header("Location: index.php");
exit();
} catch (PDOException $e) {
$pdo->rollBack();
error_log($e->getMessage());
$message = "<div class='alert alert-danger'>Nie udało się zapisać zamówienia.</div>";
}
} else {
$message = "<div class='alert alert-warning'>Wypełnij nazwę produktu i podaj ilość większą od zera.</div>";
}
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Dodaj zamówienie - <?php echo e(APP_NAME); ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
</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-primary text-white">
<h5 class="mb-0"><i class="bi bi-plus-circle"></i> Nowe zamówienie</h5>
</div>
<div class="card-body">
<?php echo $message; ?>
<form method="POST">
<?php echo csrfInput(); ?>
<div class="mb-3 p-2 bg-success bg-opacity-10 border border-success rounded">
<label class="form-label small fw-bold text-success"><i class="bi bi-building"></i> Firma kupująca</label>
<select name="company" class="form-select border-success">
<option value="">Wybierz firmę...</option>
<option value="Przedsiębiorstwo">Przedsiębiorstwo</option>
<option value="Spółka">Spółka</option>
</select>
</div>
<div class="mb-3">
<label class="form-label small fw-bold">Nazwa produktu *</label>
<input type="text" name="product_name" class="form-control" required>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label small fw-bold">Ilość *</label>
<input type="number" name="quantity" class="form-control" value="1" min="1" required>
</div>
<div class="col-md-6 mb-3">
<label class="form-label small fw-bold">Cena za sztukę</label>
<div class="input-group">
<input type="text" name="price_per_unit" class="form-control" placeholder="0.00">
<span class="input-group-text">zł</span>
</div>
</div>
</div>
<div class="mb-3">
<label class="form-label small fw-bold">Sklep / miejsce zakupu</label>
<input type="text" name="purchase_place" class="form-control">
</div>
<div class="row bg-light p-3 border rounded mb-3">
<div class="col-md-6 mb-3 mb-md-0">
<label class="form-label small fw-bold text-primary">Odbiorca / projekt</label>
<input type="text" name="recipient" class="form-control form-control-sm">
</div>
<div class="col-md-6">
<label class="form-label small fw-bold text-primary">Adres dostawy</label>
<textarea name="delivery_address" class="form-control form-control-sm" rows="1"></textarea>
</div>
</div>
<div class="mb-3">
<label class="form-label small fw-bold">Szacowana data dostawy</label>
<input type="date" name="delivery_date" class="form-control">
</div>
<div class="mb-3">
<label class="form-label small fw-bold">Notatki</label>
<textarea name="notes" class="form-control" rows="2"></textarea>
</div>
<div class="d-flex justify-content-between">
<a href="index.php" class="btn btn-secondary">Powrót do listy</a>
<div class="d-flex gap-2">
<button type="submit" name="submit_action" value="save_and_list" class="btn btn-primary"><i class="bi bi-check-lg"></i> Zapisz zamówienie</button>
<button type="submit" name="submit_action" value="save_and_add_new" class="btn btn-outline-primary"><i class="bi bi-plus-lg"></i> Dodaj nowe</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>