36782-vm/order_details.php
Flatlogic Bot 11f95cfd4b ENG v.1
2025-12-12 19:32:49 +00:00

153 lines
6.9 KiB
PHP

<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once 'includes/auth.php';
require_login();
require_once 'includes/helpers.php';
require_once 'includes/i18n.php';
$order_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($order_id === 0) {
header('Location: orders.php');
exit;
}
$client_id = $_SESSION['client_id'] ?? 0;
$error_message = null;
$order = null;
$order_items = [];
$product_images = [];
if ($client_id === 0) {
$error_message = 'Brak uprawnień do wyświetlenia tego zamówienia.';
} else {
try {
$pdo = db();
$stmt = $pdo->prepare('SELECT * FROM orders WHERE id = :order_id AND client_id = :client_id');
$stmt->execute([':order_id' => $order_id, ':client_id' => $client_id]);
$order = $stmt->fetch();
if (!$order) {
$error_message = 'Nie znaleziono zamówienia lub nie masz do niego dostępu.';
} else {
$stmt = $pdo->prepare(
'SELECT oi.*, p.name as product_name FROM order_items oi JOIN products p ON oi.product_id = p.id WHERE oi.order_id = ?'
);
$stmt->execute([$order_id]);
$order_items = $stmt->fetchAll();
if (!empty($order_items)) {
$product_ids = array_map(fn($item) => $item['product_id'], $order_items);
$placeholders = implode(',', array_fill(0, count($product_ids), '?'));
$image_stmt = $pdo->prepare(
"SELECT product_id, file_path, is_primary, id FROM product_images WHERE product_id IN ($placeholders) ORDER BY product_id, is_primary DESC, id ASC"
);
$image_stmt->execute($product_ids);
$images_data = $image_stmt->fetchAll();
$product_images_temp = [];
foreach ($images_data as $image) {
if (!isset($product_images_temp[$image['product_id']])) {
$product_images_temp[$image['product_id']] = 'uploads/products/' . $image['product_id'] . '/' . basename($image['file_path']);
}
}
$product_images = $product_images_temp;
}
}
} catch (PDOException $e) {
$error_message = 'Błąd bazy danych. Prosimy spróbować ponownie później.';
error_log($e->getMessage());
}
}
$page_title = $order ? 'Szczegóły zamówienia #' . $order['id'] : 'Szczegóły zamówienia';
$user_role = get_user_role();
?>
<!DOCTYPE html>
<html lang="<?= get_lang() ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($page_title); ?> - B2B Commerce</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<?php require_once 'includes/header.php'; ?>
<main class="container my-5">
<?php if ($error_message): ?>
<div class="alert alert-danger">
<i class="bi bi-exclamation-triangle-fill"></i> <?php echo htmlspecialchars($error_message); ?>
</div>
<a href="orders.php" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> Wróć do listy zamówień</a>
<?php elseif ($order): ?>
<h1 class="mb-4"><?php echo htmlspecialchars($page_title); ?></h1>
<div class="card mb-4">
<div class="card-header">Podsumowanie</div>
<div class="card-body">
<p><strong>Data zamówienia:</strong> <?php echo date('d.m.Y H:i', strtotime($order['created_at'])); ?></p>
<p><strong>Status:</strong> <span class="badge bg-info"><?php echo htmlspecialchars(t_status($order['status'])); ?></span></p>
<p><strong>Metoda płatności:</strong> <?php echo htmlspecialchars(t_status($order['payment_method'])); ?></p>
<p><strong>Suma (brutto):</strong> <?php echo number_format($order['total_amount'], 2, ',', ' '); ?> zł</p>
<p><strong>Uwagi:</strong> <?php echo nl2br(htmlspecialchars($order['notes'])); ?></p>
</div>
</div>
<div class="card">
<div class="card-header">Szczegóły zamówienia</div>
<div class="card-body">
<table class="table">
<thead>
<tr>
<th>Zdjęcie</th>
<th>Produkt</th>
<th>Cena jedn. netto</th>
<th>Cena jedn. brutto</th>
<th>Ilość</th>
<th>Suma (brutto)</th>
</tr>
</thead>
<tbody>
<?php
$vatRate = 1.23;
foreach ($order_items as $item):
$image_url = $product_images[$item['product_id']] ?? 'https://placehold.co/100x100/EEE/31343C?text=' . urlencode('Brak zdjęcia');
$unit_price_gross = (float)$item['unit_price'];
$unit_price_net = $unit_price_gross / $vatRate;
?>
<tr>
<td><img src="<?php echo htmlspecialchars($image_url); ?>" alt="<?php echo htmlspecialchars($item['product_name']); ?>" style="width: 50px; height: 50px; object-fit: cover;"></td>
<td><?php echo htmlspecialchars($item['product_name']); ?></td>
<td><?php echo number_format($unit_price_net, 2, ',', ' '); ?> zł</td>
<td><?php echo number_format($unit_price_gross, 2, ',', ' '); ?> zł</td>
<td><?php echo $item['quantity']; ?></td>
<td><?php echo number_format($item['line_total'], 2, ',', ' '); ?> zł</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<div class="mt-4">
<a href="orders.php" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> Wróć do listy zamówień</a>
</div>
<?php endif; ?>
</main>
<footer class="text-center py-4 mt-auto text-muted bg-light">
<div class="container">
<p class="mb-0">&copy; <?php echo date("Y"); ?> powered by LEA24. All Rights Reserved.</p>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>