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.

152 lines
7.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) . '?';
$doc_type = $_POST['doc_type'] ?? 'List przewozowy';
$company_footer = $_POST['company_footer'] ?? 'Przedsiębiorstwo';
$delivery_address = trim($_POST['global_delivery_address'] ?? '');
$issued_by = trim($_POST['issued_by'] ?? '');
$received_by = trim($_POST['received_by'] ?? '');
$cols = $_POST['delivery_cols'] ?? ['id', 'product', 'quantity', 'notes'];
$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);
$logo_db = $pdo->query("SELECT setting_value FROM " . DB_PREFIX . "settings WHERE setting_key = 'logo_path'")->fetchColumn();
$logo_src = (file_exists('uploads/logo.png')) ? 'uploads/logo.png' : ($logo_db ?: '');
// --- KONFIGURACJA STOPEK (pobierana z bazy) ---
$settings_stmt = $pdo->query("SELECT setting_key, setting_value FROM " . DB_PREFIX . "settings WHERE setting_key IN ('company_1_name', 'company_1_details', 'company_2_name', 'company_2_details')");
$settings = $settings_stmt->fetchAll(PDO::FETCH_KEY_PAIR);
if ($company_footer === 'Spółka') {
$footer_company_name = $settings['company_2_name'] ?? "Spółka Celowa Sp. z o.o.";
$footer_company_details = $settings['company_2_details'] ?? "-";
} else {
$footer_company_name = $settings['company_1_name'] ?? "Moje Przedsiębiorstwo IT";
$footer_company_details = $settings['company_1_details'] ?? "-";
}
} else {
die("Błąd: Nie wybrano zamówień.");
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title><?php echo htmlspecialchars($doc_type); ?> - <?php echo date('Y-m-d'); ?></title>
<style>
body { font-family: Arial, sans-serif; font-size: 12px; margin: 30px; color: #000; position: relative; min-height: 90vh; }
.header-box { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 30px; border-bottom: 2px solid #000; padding-bottom: 20px; }
.logo-container { max-width: 50%; }
.logo-container img { max-height: 65px; margin-bottom: 10px; }
.title { font-size: 24px; font-weight: bold; text-transform: uppercase; letter-spacing: 1px; }
.address-box { border: 2px solid #000; padding: 15px; width: 45%; min-height: 80px; font-size: 14px; background-color: #fdfdfd; }
.address-title { font-weight: bold; margin-bottom: 5px; font-size: 11px; color: #555; text-transform: uppercase; }
table { width: 100%; border-collapse: collapse; margin-bottom: 50px; }
th, td { border: 1px solid #000; padding: 10px; text-align: left; }
th { background-color: #eee; font-weight: bold; text-transform: uppercase; font-size: 11px; }
.signatures { display: flex; justify-content: space-between; margin-top: 60px; padding: 0 30px; }
.sig-block { text-align: center; width: 250px; }
.sig-line { border-top: 1px solid #000; padding-top: 5px; font-size: 11px; margin-bottom: 5px; }
.sig-name { font-size: 14px; font-weight: bold; min-height: 18px; }
.footer { position: absolute; bottom: 0; left: 0; right: 0; text-align: center; border-top: 1px solid #ddd; padding-top: 10px; }
.footer-company { font-weight: bold; font-size: 14px; margin-bottom: 2px; }
.footer-details { font-size: 10px; color: #555; margin-bottom: 5px; }
.footer-meta { font-size: 9px; color: #999; }
@media print {
.no-print { display: none; }
body { margin: 0; }
}
</style>
</head>
<body onload="window.print()">
<div class="no-print" style="margin-bottom: 20px; background: #fff3cd; padding: 10px; border-radius: 5px; text-align: center;">
<button onclick="window.print()" style="padding: 8px 16px; font-weight: bold;">🖨️ Drukuj Dokument</button>
<button onclick="window.close()" style="padding: 8px 16px;">❌ Zamknij</button>
</div>
<div class="header-box">
<div class="logo-container">
<?php if(!empty($logo_src)): ?>
<img src="<?php echo htmlspecialchars($logo_src); ?>" alt="Logo">
<?php endif; ?>
<div class="title"><?php echo htmlspecialchars($doc_type); ?></div>
</div>
<div class="address-box">
<div class="address-title">Miejsce dostawy / Odbiorca:</div>
<?php echo nl2br(htmlspecialchars($delivery_address)); ?>
<?php if(empty($delivery_address)) echo "<span style='color:#ccc;'>(Brak podanego adresu na dokumencie)</span>"; ?>
</div>
</div>
<table>
<thead>
<tr>
<th style="width: 30px;">LP</th>
<?php if($hasCol('id')): ?> <th style="width: 50px;">ID</th> <?php endif; ?>
<?php if($hasCol('product')): ?> <th>Wydawany Towar (Produkt / PN)</th> <?php endif; ?>
<?php if($hasCol('quantity')): ?> <th style="width: 50px; text-align: center;">Ilość</th> <?php endif; ?>
<?php if($hasCol('notes')): ?> <th>Notatki</th> <?php endif; ?>
</tr>
</thead>
<tbody>
<?php $lp = 1; foreach ($orders as $o): ?>
<tr>
<td style="text-align: center;"><?php echo $lp++; ?></td>
<?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: 10px; color: #555;">PN: <?php echo htmlspecialchars($o['part_number'] ?? ''); ?></span><?php endif; ?>
</td>
<?php endif; ?>
<?php if($hasCol('quantity')): ?> <td style="text-align: center; font-size: 14px;"><strong><?php echo $o['quantity']; ?></strong></td> <?php endif; ?>
<?php if($hasCol('notes')): ?>
<td><?php echo htmlspecialchars($o['notes'] ?? ''); ?></td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="signatures">
<div class="sig-block">
<div class="sig-line">Wydający (Podpis i data)</div>
<div class="sig-name"><?php echo htmlspecialchars($issued_by); ?></div>
</div>
<div class="sig-block">
<div class="sig-line">Odbierający (Czytelny podpis i data)</div>
<div class="sig-name"><?php echo htmlspecialchars($received_by); ?></div>
</div>
</div>
<div class="footer">
<div class="footer-company"><?php echo htmlspecialchars($footer_company_name); ?></div>
<div class="footer-details"><?php echo htmlspecialchars($footer_company_details); ?></div>
<div class="footer-meta">Data wydruku: <?php echo date('d.m.Y H:i'); ?> | System: <?php echo defined('APP_NAME') ? APP_NAME : 'System IT'; ?> | Generujący: <?php echo htmlspecialchars($_SESSION['username']); ?></div>
</div>
</body>
</html>