<?php
// modules/puantaj-rapor.php
session_start();
require_once '../config/db.php';
require_once '../config/functions.php';
yetkiKontrol(['root', 'yonetici', 'muhasebe', 'insan_kaynaklari']);
$yil = $_GET['yil'];
$ay = $_GET['ay'];
$rol_filtre = $_GET['rol'] ?? 'tumu';
$personel_id = $_GET['personel_id'] ?? 'tumu';
$format = $_GET['format']; // excel veya print
$gun_sayisi = cal_days_in_month(CAL_GREGORIAN, $ay, $yil);
$month_start = "$yil-$ay-01";
$month_end = date("Y-m-t", strtotime($month_start));
$ay_isimleri = ['01'=>'Ocak','02'=>'Şubat','03'=>'Mart','04'=>'Nisan','05'=>'Mayıs','06'=>'Haziran','07'=>'Temmuz','08'=>'Ağustos','09'=>'Eylül','10'=>'Ekim','11'=>'Kasım','12'=>'Aralık'];
$gun_kisa = ['Mon'=>'Pzt', 'Tue'=>'Sal', 'Wed'=>'Çar', 'Thu'=>'Per', 'Fri'=>'Cum', 'Sat'=>'Cts', 'Sun'=>'Pzr'];
$baslik = "PUANTAJ CETVELİ - " . mb_strtoupper($ay_isimleri[$ay]) . " " . $yil;
// --- PERSONEL SORGUSU ---
$sql_per = "SELECT * FROM kullanicilar WHERE rol != 'root'";
if ($rol_filtre != 'tumu') $sql_per .= " AND rol = '$rol_filtre'";
if ($personel_id != 'tumu') $sql_per .= " AND id = $personel_id";
$sql_per .= " ORDER BY ad ASC";
$tum_personeller = $pdo->query($sql_per)->fetchAll();
// --- FİLTRELEME: SADECE AKTİF VEYA HAREKETİ OLANLAR ---
$personeller = [];
foreach ($tum_personeller as $p) {
$giris = $p['ise_giris_tarihi'];
$cikis = $p['isten_cikis_tarihi'];
// 1. Tarihsel Kontrol
$is_active = true;
if ($giris > $month_end) $is_active = false;
if (!empty($cikis) && $cikis < $month_start) $is_active = false;
// 2. Aktivite Kontrolü
if (!$is_active) {
$has_mesai = $pdo->query("SELECT COUNT(*) FROM mesai_hareketleri WHERE calisan_id={$p['id']} AND tarih BETWEEN '$month_start' AND '$month_end' AND durum='onaylandi'")->fetchColumn();
$has_izin = $pdo->query("SELECT COUNT(*) FROM izin_talepleri WHERE calisan_id={$p['id']} AND durum='onaylandi' AND (baslangic_tarihi <= '$month_end 23:59:59' AND bitis_tarihi >= '$month_start 00:00:00')")->fetchColumn();
if ($has_mesai > 0 || $has_izin > 0) $is_active = true;
}
if ($is_active) $personeller[] = $p;
}
$tatiller = $pdo->query("SELECT tarih FROM resmi_tatiller")->fetchAll(PDO::FETCH_COLUMN);
$gunluk_saat_std = (float)($pdo->query("SELECT ayar_degeri FROM site_ayarlari WHERE ayar_anahtari = 'gunluk_mesai_saati'")->fetchColumn() ?: 9);
// EXCEL HEADER
if ($format == 'excel') {
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=Puantaj_{$yil}_{$ay}.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo "\xEF\xBB\xBF";
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<title><?php echo $baslik; ?></title>
<style>
body { font-family: Arial, sans-serif; font-size: 10px; margin: 0; padding: 20px; }
table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
th, td { border: 1px solid #000; padding: 4px; text-align: center; }
th { background-color: #eee; font-size: 9px; }
.header-print { text-align: center; margin-bottom: 20px; border-bottom: 2px solid #000; padding-bottom: 10px; }
.logo { max-height: 60px; }
.rapor-title { font-size: 18px; font-weight: bold; margin-top: 10px; }
.imza-blogu { display: flex; justify-content: space-between; margin-top: 40px; page-break-inside: avoid; }
.imza-kutu { width: 24%; text-align: center; border: 1px solid #ccc; padding: 10px; height: 80px; }
.imza-baslik { font-weight: bold; border-bottom: 1px solid #ccc; padding-bottom: 5px; margin-bottom: 30px; font-size: 11px; }
@media print {
body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.no-print { display: none; }
@page { size: landscape; margin: 10mm; }
}
</style>
</head>
<body>
<?php if($format == 'print'): ?>
<div class="no-print" style="margin-bottom: 20px;">
<button onclick="window.print()" style="padding: 10px 20px; font-weight: bold; cursor: pointer;">🖨️ YAZDIR</button>
</div>
<div class="header-print">
<img src="../assets/img/logo.png" alt="Logo" class="logo"><br>
<div class="rapor-title"><?php echo $baslik; ?></div>
<div>Rapor Tarihi: <?php echo date('d.m.Y H:i'); ?></div>
</div>
<?php endif; ?>
<table>
<thead>
<tr>
<th style="width: 150px; text-align:left;">PERSONEL</th>
<?php for($d=1; $d<=$gun_sayisi; $d++):
$tarih = "$yil-$ay-" . sprintf('%02d', $d);
$gun_ing = date('D', strtotime($tarih));
$gun_tr = $gun_kisa[$gun_ing];
$bg = ($gun_ing=='Sat' || $gun_ing=='Sun') ? 'background-color:#ccc;' : '';
?>
<th style="<?php echo $bg; ?>"><?php echo $d; ?><br><?php echo $gun_tr; ?></th>
<?php endfor; ?>
<th style="background-color:#ddd;">NM</th>
<th style="background-color:#ddd;">FM</th>
<th style="background-color:#ddd;">İZ</th>
</tr>
</thead>
<tbody>
<?php if(empty($personeller)): ?>
<tr><td colspan="<?php echo $gun_sayisi + 4; ?>" style="text-align:center;">Görüntülenecek aktif kayıt bulunamadı.</td></tr>
<?php endif; ?>
<?php foreach($personeller as $per):
$toplam_normal = 0; $toplam_fazla = 0; $toplam_izin = 0;
$ise_giris = $per['ise_giris_tarihi'];
$isten_cikis = $per['isten_cikis_tarihi'];
?>
<tr>
<td style="text-align:left; font-weight:bold;"><?php echo $per['ad'].' '.$per['soyad']; ?></td>
<?php for($d=1; $d<=$gun_sayisi; $d++):
$g_tarih = "$yil-$ay-" . sprintf('%02d', $d);
$haftanin_gunu = date('N', strtotime($g_tarih));
$hucre = "";
$style = "";
$aktif_mi = true;
if ($g_tarih < $ise_giris) $aktif_mi = false;
elseif (!empty($isten_cikis) && $g_tarih > $isten_cikis) $aktif_mi = false;
if (!$aktif_mi) {
$hucre = "X";
$style = "background-color:#e9ecef; color:#aaa;";
} else {
$is_tatil = in_array($g_tarih, $tatiller);
$is_haftasonu = ($haftanin_gunu == 7);
$izin = $pdo->query("SELECT izin_turu, saatlik_sure FROM izin_talepleri WHERE calisan_id={$per['id']} AND durum='onaylandi' AND '$g_tarih' BETWEEN DATE(baslangic_tarihi) AND DATE(bitis_tarihi)")->fetch();
$mesai = $pdo->query("SELECT SUM(toplam_saat) FROM mesai_hareketleri WHERE calisan_id={$per['id']} AND durum='onaylandi' AND tarih='$g_tarih' AND mesai_turu='fazla_mesai'")->fetchColumn() ?: 0;
$gunluk_normal = 0;
if (!$is_haftasonu && !$is_tatil) $gunluk_normal = $gunluk_saat_std;
if ($izin) {
if ($izin['izin_turu'] == 'saatlik') {
$gunluk_normal -= $izin['saatlik_sure'];
$hucre = (float)$gunluk_normal;
$style = "background-color:#ffeeba;";
} else {
$gunluk_normal = 0;
$tur_map = [
'yillik'=>'Y.İZ', 'mazeret'=>'MAZ', 'hastalik'=>'RAP',
'evlilik'=>'EVL', 'babalik'=>'BBL', 'olum'=>'ÖLM', 'sut_izni'=>'SÜT'
];
$hucre = $tur_map[$izin['izin_turu']] ?? "İZİN";
$style = "background-color:#ffc107; font-weight:bold;";
$toplam_izin++;
}
} else {
if($gunluk_normal > 0) $hucre = (float)$gunluk_normal;
}
if ($mesai > 0) {
$toplam_fazla += $mesai;
if (!is_numeric($hucre)) $hucre .= "+".(float)$mesai;
else $hucre .= "+".(float)$mesai;
$style = "background-color:#d1e7dd; font-weight:bold;";
}
if ($hucre === "" && ($is_haftasonu || $is_tatil)) $style = "background-color:#eee;";
$toplam_normal += $gunluk_normal;
}
?>
<td style="<?php echo $style; ?>"><?php echo $hucre; ?></td>
<?php endfor; ?>
<td style="font-weight:bold;"><?php echo (float)$toplam_normal; ?></td>
<td style="font-weight:bold; color:green;"><?php echo (float)$toplam_fazla; ?></td>
<td style="font-weight:bold; color:orange;"><?php echo (float)$toplam_izin; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php if($format == 'print'): ?>
<div class="imza-blogu">
<div class="imza-kutu"><div class="imza-baslik">HAZIRLAYAN</div></div>
<div class="imza-kutu"><div class="imza-baslik">MUHASEBE</div></div>
<div class="imza-kutu"><div class="imza-baslik">İNSAN KAYNAKLARI</div></div>
<div class="imza-kutu"><div class="imza-baslik">GENEL MÜDÜR ONAYI</div></div>
</div>
<script>window.onload = function() { setTimeout(function() { window.print(); }, 500); }</script>
<?php endif; ?>
</body>
</html>