36782-vm/order_details.php
2025-12-12 14:13:03 +00:00

212 lines
9.6 KiB
PHP

<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once 'includes/auth.php';
require_login();
require_once 'includes/helpers.php';
function get_polish_status_translation(string $key): string {
$translations = [
'status_pending' => 'Oczekujące',
'status_pending_payment' => 'Oczekuje na płatność',
'status_paid' => 'Zapłacone',
'status_in_progress' => 'W realizacji',
'status_shipped' => 'Wysłane',
'status_partially_shipped' => 'Częściowo wysłane',
'status_completed' => 'Zrealizowane',
'status_cancelled' => 'Anulowane',
'payment_bank_transfer' => 'Przelew tradycyjny',
'payment_online' => 'Płatność online (Przelewy24)',
'payment_credit' => 'Kredyt kupiecki',
];
$payment_methods = ['bank_transfer', 'online', 'credit'];
if (in_array($key, $payment_methods)) {
$translation_key = 'payment_' . $key;
} else {
$translation_key = 'status_' . $key;
}
return $translations[$translation_key] ?? ucfirst(str_replace('_', ' ', $key));
}
$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();
$lang = 'pl';
?>
<!DOCTYPE html>
<html lang="pl">
<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>
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
<div class="container-fluid">
<a class="navbar-brand" href="index.php">
<img src="assets/pasted-20251209-065617-6bf1b4e6.png" alt="Logo" style="height: 40px;">
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ms-auto mb-2 mb-lg-0 align-items-center">
<li class="nav-item">
<a class="nav-link" href="index.php">Katalog</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cart.php">
<i class="bi bi-cart"></i> Koszyk
<span class="badge bg-primary rounded-pill"><?= count($_SESSION['cart'] ?? []) ?></span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="orders.php">Zamówienia</a>
</li>
<?php if ($user_role === 'admin'): ?>
<li class="nav-item">
<a class="nav-link" href="/admin/products.php">Admin</a>
</li>
<?php endif; ?>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-person-circle"></i> Witaj, <?= isset($_SESSION['username']) ? htmlspecialchars($_SESSION['username']) : '' ?>
</a>
<ul class="dropdown-menu dropdown-menu-end">
<li><a class="dropdown-item p-2" href="profile.php">Profil</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item p-2" href="logout.php">Wyloguj</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<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(get_polish_status_translation($order['status'])); ?></span></p>
<p><strong>Metoda płatności:</strong> <?php echo htmlspecialchars(get_polish_status_translation($order['payment_method'])); ?></p>
<p><strong>Suma:</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 jednostkowa</th>
<th>Ilość</th>
<th>Suma częściowa</th>
</tr>
</thead>
<tbody>
<?php foreach ($order_items as $item):
$image_url = $product_images[$item['product_id']] ?? 'https://placehold.co/100x100/EEE/31343C?text=' . urlencode('Brak zdjęcia');
?>
<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($item['unit_price'], 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>