<?php
// modules/izin-yazdir.php
session_start();
require_once '../config/db.php';
require_once '../config/functions.php';
yetkiKontrol(['root', 'yonetici', 'mudur', 'muhasebe', 'insan_kaynaklari', 'calisan']);
$id = (int)$_GET['id'];
$sql = "SELECT i.*, k.ad, k.soyad, k.tc_no, k.ise_giris_tarihi, k.rol, k.devreden_izin, y.ad as yonetici_ad, y.soyad as yonetici_soyad
FROM izin_talepleri i JOIN kullanicilar k ON i.calisan_id = k.id
LEFT JOIN kullanicilar y ON i.hedef_yonetici_id = y.id WHERE i.id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id]);
$izin = $stmt->fetch();
if (!$izin) die("İzin bulunamadı.");
if ($_SESSION['rol'] == 'calisan' && $izin['calisan_id'] != $_SESSION['kullanici_id']) die("Yetkisiz.");
// İş Başı Hesapla
$tatiller = $pdo->query("SELECT tarih FROM resmi_tatiller")->fetchAll(PDO::FETCH_COLUMN);
$bitis = new DateTime($izin['bitis_tarihi']);
$is_basi = clone $bitis;
$is_basi->modify('+1 day');
while (true) {
if ($is_basi->format('N') == 7 || in_array($is_basi->format('Y-m-d'), $tatiller)) {
$is_basi->modify('+1 day');
} else break;
}
// Bakiye
$ise_giris = new DateTime($izin['ise_giris_tarihi']);
$kidem = $ise_giris->diff(new DateTime())->y;
$hakedilen = ($kidem >= 15) ? 26 : (($kidem >= 5) ? 20 : 14);
$toplam_hak = $hakedilen + (float)$izin['devreden_izin'];
$kullanilan = $pdo->query("SELECT SUM(toplam_gun) FROM izin_talepleri WHERE calisan_id = {$izin['calisan_id']} AND durum = 'onaylandi' AND izin_turu='yillik' AND YEAR(baslangic_tarihi) = YEAR(CURDATE())")->fetchColumn() ?: 0;
?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<title>İzin Formu</title>
<style>
body { font-family: 'Times New Roman', serif; padding: 40px; max-width: 800px; margin: 0 auto; }
.header { text-align: center; border-bottom: 2px solid #000; padding-bottom: 20px; margin-bottom: 30px; }
.logo { max-height: 80px; }
.info-table { width: 100%; border-collapse: collapse; margin-top: 10px; }
.info-table td { border: 1px solid #000; padding: 8px; vertical-align: top; width: 50%; }
.label { font-weight: bold; display: block; font-size: 12px; color: #555; }
.section-title { background: #eee; padding: 5px; font-weight: bold; border: 1px solid #000; margin-top: 20px; }
@media print { .no-print { display: none; } }
</style>
</head>
<body>
<div class="no-print" style="text-align:right; margin-bottom:20px;">
<button onclick="window.print()">YAZDIR</button>
</div>
<div class="header">
<img src="../assets/img/logo.png" class="logo"><br>
<h2>İZİN TALEP VE ONAY FORMU</h2>
</div>
<div class="section-title">1. PERSONEL</div>
<table class="info-table">
<tr>
<td><span class="label">Ad Soyad:</span> <?php echo $izin['ad'].' '.$izin['soyad']; ?></td>
<td><span class="label">TC No:</span> <?php echo $izin['tc_no']; ?></td>
</tr>
</table>
<div class="section-title">2. İZİN DETAYLARI</div>
<table class="info-table">
<tr>
<td><span class="label">İzin Türü:</span>
<?php
$map = [
'yillik' => 'YILLIK İZİN (ÜCRETLİ)', 'hastalik' => 'RAPOR / HASTALIK (ÜCRETSİZ)',
'saatlik' => 'SAATLİK İZİN (ÜCRETSİZ)', 'evlilik' => 'EVLİLİK İZNİ (ÜCRETLİ)',
'babalik' => 'BABALIK İZNİ (ÜCRETLİ)', 'olum' => 'ÖLÜM İZNİ (ÜCRETLİ)',
'sut_izni' => 'SÜT İZNİ (ÜCRETLİ)', 'diger' => 'DİĞER (ÜCRETLİ)'
];
echo $map[$izin['izin_turu']] ?? strtoupper($izin['izin_turu']);
?>
</td>
<td><span class="label">Süre:</span> <?php echo ($izin['izin_turu']=='saatlik') ? $izin['saatlik_sure'].' Saat' : $izin['toplam_gun'].' Gün'; ?></td>
</tr>
<tr>
<td><span class="label">Başlangıç:</span> <?php echo date('d.m.Y H:i', strtotime($izin['baslangic_tarihi'])); ?></td>
<td><span class="label">Bitiş / İş Başı:</span>
Bitiş: <?php echo date('d.m.Y', strtotime($izin['bitis_tarihi'])); ?><br>
<b>İş Başı: <?php echo $is_basi->format('d.m.Y'); ?></b>
</td>
</tr>
<tr><td colspan="2"><span class="label">Açıklama:</span> <?php echo $izin['aciklama']; ?></td></tr>
</table>
<div class="section-title">3. ONAY</div>
<table class="info-table" style="margin-top:20px; border:none;">
<tr style="border:none;">
<td style="border:none; text-align:center;"><b>Talep Eden</b><br><br>Imza</td>
<td style="border:none; text-align:center;"><b>Onaylayan Yönetici</b><br><br>Imza</td>
</tr>
</table>
</body>
</html>