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.

114 lines
5.4 KiB
PHP

<?php
require_once 'includes/db.php';
require_once 'includes/auth.php';
checkAuth();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['order_ids'])) {
requireCsrfToken();
$ids = $_POST['order_ids'];
$placeholders = str_repeat('?,', count($ids) - 1) . '?';
$cols = $_POST['print_cols'] ?? ['id', 'product', 'quantity', 'place', 'price', 'status'];
$hasCol = function($colName) use ($cols) { return in_array($colName, $cols); };
$sql = "SELECT * FROM " . DB_PREFIX . "orders WHERE id IN ($placeholders) ORDER BY id DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute($ids);
$orders = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Obliczamy ile głównych kolumn wydrukować, żeby ustawić odpowiedni colspan dla sub-wiersza
$mainColsCount = 0;
$main_available = ['id', 'product', 'quantity', 'place', 'price', 'status'];
foreach($main_available as $mc) { if($hasCol($mc)) $mainColsCount++; }
if($mainColsCount == 0) $mainColsCount = 1;
} else {
die("Błąd: Nie wybrano żadnych zamówień do druku.");
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Wydruk listy - <?php echo date('Y-m-d'); ?></title>
<style>
body { font-family: Arial, sans-serif; font-size: 11px; margin: 20px; color: #000; }
h2 { text-align: center; margin-bottom: 20px; font-size: 18px; text-transform: uppercase; }
table { width: 100%; border-collapse: collapse; margin-bottom: 30px; }
th, td { border: 1px solid #777; padding: 6px; text-align: left; vertical-align: top; }
th { background-color: #f0f0f0; font-weight: bold; }
.footer { font-size: 10px; color: #555; text-align: right; border-top: 1px solid #ccc; padding-top: 5px; }
/* Magia sub-wiersza na papierze */
.sub-row td { border-top: none; padding: 4px 8px 12px 8px; background-color: #fcfcfc; font-size: 10px; color: #333; }
.sub-item { margin-right: 20px; display: inline-block; }
.sub-item strong { color: #000; }
@media print {
.no-print { display: none; }
body { margin: 0; }
}
</style>
</head>
<body onload="window.print()">
<div class="no-print" style="margin-bottom: 20px; text-align: center; background: #e9ecef; padding: 10px; border-radius: 5px;">
<button onclick="window.print()" style="padding: 8px 16px; cursor: pointer; font-weight: bold;">🖨️ Drukuj listę</button>
<button onclick="window.close()" style="padding: 8px 16px; cursor: pointer;">❌ Zamknij</button>
</div>
<h2>Zestawienie zamówień IT</h2>
<table>
<thead>
<tr>
<?php if($hasCol('id')): ?> <th style="width: 40px;">ID</th> <?php endif; ?>
<?php if($hasCol('product')): ?> <th>Produkt (PN)</th> <?php endif; ?>
<?php if($hasCol('quantity')): ?> <th style="width: 30px; text-align: center;">Szt.</th> <?php endif; ?>
<?php if($hasCol('place')): ?> <th>Sklep</th> <?php endif; ?>
<?php if($hasCol('price')): ?> <th style="width: 70px;">Cena jedn.</th> <?php endif; ?>
<?php if($hasCol('status')): ?> <th style="width: 90px;">Status</th> <?php endif; ?>
</tr>
</thead>
<tbody>
<?php foreach ($orders as $o): ?>
<tr>
<?php if($hasCol('id')): ?> <td>#<?php echo $o['id']; ?></td> <?php endif; ?>
<?php if($hasCol('product')): ?>
<td>
<strong><?php echo htmlspecialchars($o['product_name']); ?></strong>
<?php if(!empty($o['part_number'])): ?><br><span style="font-size: 9px; color: #555;">PN: <?php echo htmlspecialchars($o['part_number']); ?></span><?php endif; ?>
</td>
<?php endif; ?>
<?php if($hasCol('quantity')): ?> <td style="text-align: center;"><strong><?php echo $o['quantity']; ?></strong></td> <?php endif; ?>
<?php if($hasCol('place')): ?> <td><?php echo htmlspecialchars($o['purchase_place']); ?></td> <?php endif; ?>
<?php if($hasCol('price')): ?> <td><?php echo number_format($o['price_per_unit'], 2, ',', ' '); ?> zł</td> <?php endif; ?>
<?php if($hasCol('status')): ?> <td><?php echo e($o['status']); ?></td> <?php endif; ?>
</tr>
<?php if($hasCol('recipient') || $hasCol('address') || $hasCol('notes')): ?>
<tr class="sub-row">
<td colspan="<?php echo $mainColsCount; ?>">
<?php if($hasCol('recipient') && !empty($o['recipient'])): ?>
<div class="sub-item"><strong>Odbiorca:</strong> <?php echo htmlspecialchars($o['recipient']); ?></div>
<?php endif; ?>
<?php if($hasCol('address') && !empty($o['delivery_address'])): ?>
<div class="sub-item"><strong>Adres:</strong> <?php echo htmlspecialchars($o['delivery_address']); ?></div>
<?php endif; ?>
<?php if($hasCol('notes') && !empty($o['notes'])): ?>
<div class="sub-item"><strong>Notatki:</strong> <em><?php echo htmlspecialchars($o['notes']); ?></em></div>
<?php endif; ?>
</td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
<div class="footer">Wygenerowano: <?php echo date('d.m.Y H:i'); ?> | System IT</div>
</body>
</html>