<?php
// modules/avans-rapor.php
session_start();
require_once '../config/db.php';
require_once '../config/functions.php';
// Yetkili Roller: Root, Yönetici, Muhasebe, İnsan Kaynakları
yetkiKontrol(['root', 'yonetici', 'muhasebe', 'insan_kaynaklari']);
// --- FİLTRELER VE PARAMETRELER ---
$yil = guvenlik($_GET['yil'] ?? date('Y'));
$ay = guvenlik($_GET['ay'] ?? date('m'));
$filtre_personel_id = isset($_GET['personel_id']) ? (int)$_GET['personel_id'] : 0; // Yeni Parametre
$format = guvenlik($_GET['format'] ?? 'print'); // 'excel' veya 'pdf' (print)
// Rapor tarihi, saat ve zaman dilimi ayarı
date_default_timezone_set('Europe/Istanbul');
$rapor_tarihi_saati = date('d.m.Y H:i');
$ay_isimleri = ['01'=>'OCAK','02'=>'ŞUBAT','03'=>'MART','04'=>'NİSAN','05'=>'MAYIS','06'=>'HAZİRAN','07'=>'TEMMUZ','08'=>'AĞUSTOS','09'=>'EYLÜL','10'=>'EKİM','11'=>'KASIM','12'=>'ARALIK'];
// --- BAŞLIK AYARLAMA ---
$baslik_ek = "";
if ($filtre_personel_id > 0) {
// Seçilen personelin adını bulalım
$stmt_p = $pdo->prepare("SELECT ad, soyad FROM kullanicilar WHERE id = ?");
$stmt_p->execute([$filtre_personel_id]);
$p_bilgi = $stmt_p->fetch();
if($p_bilgi) {
$baslik_ek = $p_bilgi['ad'] . " " . $p_bilgi['soyad'] . " - ";
}
}
$baslik = $baslik_ek . "PERSONEL AVANS HAREKETLERİ - " . $ay_isimleri[$ay] . " " . $yil;
$ay_baslangici = date('Y-m-01', strtotime("$yil-$ay-01"));
$ay_sonu = date('Y-m-t', strtotime("$yil-$ay-01"));
// --- AVANS VERİSİNİ ÇEKME (FİLTRELİ) ---
$sql_avans = "
SELECT ah.*, k.ad, k.soyad, k.rol
FROM avans_hareketleri ah
JOIN kullanicilar k ON ah.calisan_id = k.id
WHERE ah.islem_tarihi BETWEEN :baslangic AND :sonu
AND k.rol != 'root'
";
// Parametre dizisi
$params = [':baslangic' => $ay_baslangici, ':sonu' => $ay_sonu];
// Eğer personel seçildiyse sorguya ekle
if ($filtre_personel_id > 0) {
$sql_avans .= " AND ah.calisan_id = :pid ";
$params[':pid'] = $filtre_personel_id;
}
$sql_avans .= " ORDER BY k.ad ASC, ah.islem_tarihi DESC";
$stmt_avans = $pdo->prepare($sql_avans);
$stmt_avans->execute($params);
$rapor_verileri = $stmt_avans->fetchAll();
// Toplam Avans Miktarını Hesapla
$toplam_avans = array_sum(array_column($rapor_verileri, 'avans_miktari'));
// --- RAPOR ÇIKTI BAŞLIKLARI (Excel/PDF) ---
if ($format == 'excel') {
$dosya_adi = "Avans_Raporu_" . ($filtre_personel_id > 0 ? "Personel_" : "Genel_") . "{$yil}_{$ay}.xls";
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=$dosya_adi");
header("Pragma: no-cache");
header("Expires: 0");
echo "\xEF\xBB\xBF"; // BOM
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<title>Ramsa Makine Sanayii - Avans Raporu</title>
<style>
body { font-family: Arial, sans-serif; font-size: 11px; margin: 0; padding: 20px; }
/* RAPOR ANTEPİ VE LOGO */
.rapor-header {
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 3px solid #333;
padding-bottom: 10px;
margin-bottom: 20px;
}
.rapor-header img { max-height: 50px; }
.rapor-header h1 {
font-size: 16px;
font-weight: bold;
margin: 0;
color: #333;
}
.rapor-header p {
font-size: 10px;
margin: 0;
color: #666;
}
/* TABLO STİLİ */
table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
th, td { border: 1px solid #000; padding: 5px; text-align: center; }
th { background-color: #ddd; font-weight: bold; }
.text-start { text-align: left !important; }
.text-end { text-align: right !important; }
.bg-danger-light { background-color: #f8d7da; }
.bg-info-light { background-color: #e2f3ff; }
/* İmza Alanı Stilleri */
.imza-blogu { display: flex; justify-content: space-around; margin-top: 50px; page-break-inside: avoid; }
.imza-kutu { width: 25%; text-align: center; padding: 10px; }
.imza-baslik { font-weight: bold; border-top: 1px solid #000; padding-top: 5px; margin-top: 40px; font-size: 11px; }
/* PDF/PRINT ÖZEL STİLLER */
@media print {
body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.no-print { display: none; }
.rapor-header { border-bottom-color: #000; }
th { background-color: #eee !important; }
.bg-danger-light { background-color: #f8d7da !important; }
.bg-info-light { background-color: #e2f3ff !important; }
@page {
size: portrait;
margin: 15mm;
}
}
</style>
</head>
<body>
<?php if($format == 'pdf'): ?>
<div class="no-print" style="margin-bottom: 20px;">
<button onclick="window.print()" style="padding: 10px 20px; font-weight: bold; cursor: pointer;">🖨️ YAZDIR / PDF OLUŞTUR</button>
</div>
<?php endif; ?>
<div class="rapor-header">
<img src="../assets/img/logo.png" alt="Şirket Logosu">
<div class="text-center">
<h1><?php echo $baslik; ?></h1>
<p>Rapor Tarihi: <?php echo $rapor_tarihi_saati; ?></p>
</div>
<div style="width: 50px;"></div>
</div>
<?php if (empty($rapor_verileri)): ?>
<div style="text-align: center; padding: 50px; border: 1px dashed #ccc;">
Seçilen kriterlere uygun avans hareketi bulunmamaktadır.
</div>
<?php else: ?>
<table style="font-size: 10px;">
<thead>
<tr>
<th class="text-start" style="width: 25%;">PERSONEL</th>
<th style="width: 15%;">ROL</th>
<th style="width: 15%;">İŞLEM TARİHİ</th>
<th class="text-end" style="width: 20%;">AVANS MİKTARI (TL)</th>
<th style="width: 25%;">AÇIKLAMA</th>
</tr>
</thead>
<tbody>
<?php foreach($rapor_verileri as $veri): ?>
<tr>
<td class="text-start"><?php echo $veri['ad'] . ' ' . $veri['soyad']; ?></td>
<td><?php echo strtoupper($veri['rol']); ?></td>
<td><?php echo tarihTurkce($veri['islem_tarihi']); ?></td>
<td class="text-end bg-danger-light" style="font-weight: bold;">
<?php echo number_format($veri['avans_miktari'], 2); ?> ₺
</td>
<td><?php echo guvenlik($veri['aciklama']); ?></td>
</tr>
<?php endforeach; ?>
<tr style="border-top: 3px double #000;">
<td colspan="3" class="text-end" style="font-weight: bold; font-size: 12px;">GENEL TOPLAM:</td>
<td class="text-end bg-info-light" style="font-weight: bold; font-size: 12px;">
<?php echo number_format($toplam_avans, 2); ?> ₺
</td>
<td></td>
</tr>
</tbody>
</table>
<?php endif; ?>
<div class="imza-blogu">
<div class="imza-kutu">
<div class="imza-baslik">MUHASEBE YETKİLİSİ</div>
</div>
<div class="imza-kutu">
<div class="imza-baslik">YÖNETİCİ ONAYI</div>
</div>
<?php if($filtre_personel_id > 0): ?>
<div class="imza-kutu">
<div class="imza-baslik">PERSONEL İMZA</div>
</div>
<?php endif; ?>
</div>
<?php if($format == 'pdf'): ?>
<script>
window.onload = function() { setTimeout(function() { window.print(); }, 500); }
</script>
<?php endif; ?>
</body>
</html>