PYBS (Personel Yönetim Bilgi Sistemi) / Ramsa/modules/rapor-olustur.php
rapor-olustur.php 334 satır • 14.09 KB
<?php
// modules/rapor-olustur.php
session_start();
require_once '../config/db.php';
require_once '../config/functions.php';

// Yetki Kontrolü
yetkiKontrol(['root', 'yonetici', 'muhasebe', 'insan_kaynaklari']);

// Parametreleri Al
$tur = $_GET['tur'] ?? 'mesai';
$format = $_GET['format'] ?? 'yazdir'; // yazdir veya excel
$personel_id = $_GET['personel_id'] ?? 'tumu'; // Mesai/İzin için
$rol_filtre = $_GET['rol_filtre'] ?? 'tumu'; // Personel Bilgi için
$durum_filtre = $_GET['durum_filtre'] ?? '1'; // Personel Bilgi için

// TC No görünümünü TC Kimlik formatına çevirir.
function formatTC($tc) {
    // İstenilen değişiklik: Gizlemeyi kaldırıp, orijinal TC numarasını olduğu gibi göster
    if (strlen($tc) == 11 && is_numeric($tc)) {
        return $tc; // TC'nin tamamını göster
    }
    return $tc;
}
// Rolleri Türkçeleştirir
function turkceRol($rol) {
    $roller = [
        'calisan' => 'Çalışan', 'stajyer' => 'Stajyer', 'vardiya_amiri' => 'V. Amiri', 
        'mudur' => 'Müdür', 'muhasebe' => 'Muhasebe', 'insan_kaynaklari' => 'İK',
        'yonetici' => 'Yönetici', 'root' => 'Sistem Yöneticisi'
    ];
    return $roller[$rol] ?? $rol;
}

$dosya_adi_kisa = ''; // Dosya adına eklenecek kısa isim

// Dosya Adı için Türkçe Karakter Temizleme (Varsayım: functions.php'de var)
if (!function_exists('temizDosyaAdi')) {
    function temizDosyaAdi($str) {
        $str = str_replace(array('ç', 'Ç', 'ğ', 'Ğ', 'ı', 'İ', 'ö', 'Ö', 'ş', 'Ş', 'ü', 'Ü', ' '), array('c', 'C', 'g', 'G', 'i', 'I', 'o', 'O', 's', 'S', 'u', 'U', '_'), $str);
        $str = preg_replace('/[^a-zA-Z0-9_-]/', '', $str);
        return $str;
    }
}
// --------------------------------------------------------------------------------
// 1. MESAİ RAPORU SORGUSU
// --------------------------------------------------------------------------------
if ($tur == 'mesai') {
    $baslangic = $_GET['baslangic'];
    $bitis = $_GET['bitis'];
    
    $sql = "SELECT m.*, k.ad, k.soyad, k.tc_no 
            FROM mesai_hareketleri m 
            JOIN kullanicilar k ON m.calisan_id = k.id 
            WHERE m.durum = 'onaylandi' 
            AND m.tarih BETWEEN '$baslangic' AND '$bitis'";
            
    if ($personel_id != 'tumu') {
        $sql .= " AND m.calisan_id = $personel_id";
        $p_info = $pdo->query("SELECT ad, soyad FROM kullanicilar WHERE id = $personel_id")->fetch();
        if ($p_info) $dosya_adi_kisa = "_" . temizDosyaAdi($p_info['ad'] . '_' . $p_info['soyad']);
    }
    
    $sql .= " ORDER BY m.tarih ASC, k.ad ASC";
    $veriler = $pdo->query($sql)->fetchAll();
    $baslik = "MESAİ VE PUANTAJ DÖKÜMÜ (" . date('d.m.Y', strtotime($baslangic)) . " - " . date('d.m.Y', strtotime($bitis)) . ")";
    $dosya_adi_kisa = "Mesai_Dokumu" . $dosya_adi_kisa . "_" . date('Ymd', strtotime($baslangic));
} 

// --------------------------------------------------------------------------------
// 2. İZİN RAPORU SORGUSU
// --------------------------------------------------------------------------------
elseif ($tur == 'izin') {
    $yil = $_GET['yil'];
    
    $sql = "SELECT i.*, k.ad, k.soyad, k.tc_no 
            FROM izin_talepleri i 
            JOIN kullanicilar k ON i.calisan_id = k.id 
            WHERE i.durum = 'onaylandi' 
            AND YEAR(i.baslangic_tarihi) = '$yil'";
            
    if ($personel_id != 'tumu') {
        $sql .= " AND i.calisan_id = $personel_id";
        $p_info = $pdo->query("SELECT ad, soyad FROM kullanicilar WHERE id = $personel_id")->fetch();
        if ($p_info) $dosya_adi_kisa = "_" . temizDosyaAdi($p_info['ad'] . '_' . $p_info['soyad']);
    }
    
    $sql .= " ORDER BY i.baslangic_tarihi ASC";
    $veriler = $pdo->query($sql)->fetchAll();
    $baslik = "$yil YILI İZİN KULLANIM RAPORU";
    $dosya_adi_kisa = "Izin_Kullanim" . $dosya_adi_kisa . "_" . $yil;
}

// --------------------------------------------------------------------------------
// 4. PERSONEL BİLGİLERİ DÖKÜMÜ (YENİ ALANLAR EKLENDİ)
// --------------------------------------------------------------------------------
elseif ($tur == 'personel_bilgi') {
    $sql = "SELECT id, ad, soyad, tc_no, sgk_no, dogum_tarihi, medeni_durum, cocuk_sayisi, telefon, email, kan_grubu, adres, rol, durum, ise_giris_tarihi, isten_cikis_tarihi 
            FROM kullanicilar 
            WHERE rol != 'root'"; 
            
    if ($rol_filtre != 'tumu') {
        $sql .= " AND rol = " . $pdo->quote($rol_filtre);
        $dosya_adi_kisa .= "_" . temizDosyaAdi($rol_filtre);
    }
    
    if ($durum_filtre != 'tumu') {
        $sql .= " AND durum = " . (int)$durum_filtre; 
        $dosya_adi_kisa .= ($durum_filtre == 1) ? "_Aktif" : "_Pasif";
    } else {
         $dosya_adi_kisa .= "_Tumu";
    }
    
    $sql .= " ORDER BY ad ASC, soyad ASC";
    $veriler = $pdo->query($sql)->fetchAll();
    $baslik = "DETAYLI PERSONEL BİLGİLERİ DÖKÜMÜ";
    $dosya_adi_kisa = "Personel_Bilgi" . $dosya_adi_kisa;
}
// --------------------------------------------------------------------------------

// --- DİKKAT: EXCEL ÇIKTISI İÇİN DOSYA ADI BURADA BELİRLENİYOR ---
if ($format == 'excel') {
    $dosya_adi = temizDosyaAdi($dosya_adi_kisa) . "_" . date("Ymd_His") . ".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");
    // Excel için BOM
    echo "\xEF\xBB\xBF"; 
}
?>

<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <title><?php echo $baslik; ?></title>
    <style>
        body { font-family: 'Segoe UI', Arial, sans-serif; font-size: 12px; }
        
        /* TABLO STİLLERİ */
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th, td { border: 1px solid #333; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; font-weight: bold; }
        
        /* EXCEL MODUNDA LOGO GİZLE, YAZDIR MODUNDA GÖSTER */
        .header-print { text-align: center; margin-bottom: 30px; }
        .logo { max-width: 200px; max-height: 80px; margin-bottom: 10px; }
        .rapor-baslik { font-size: 18px; font-weight: bold; text-decoration: underline; }
        .meta-bilgi { font-size: 11px; margin-top: 5px; }

        /* İMZA BÖLÜMÜ */
        .imza-blogu { margin-top: 50px; display: flex; justify-content: space-between; page-break-inside: avoid; }
        .imza-kutu { width: 30%; text-align: center; }
        .imza-cizgi { border-top: 1px solid #000; margin-top: 30px; width: 80%; margin-left: 10%; }

        /* YAZDIRMA BUTONU (Çıktıda Gizlenir) */
        .no-print { text-align: center; padding: 20px; background: #eee; border-bottom: 1px solid #ccc; }
        
        @media print {
            .no-print { display: none; }
            body { margin: 0; padding: 20px; }
            th { background-color: #f2f2f2 !important; }
        }
    </style>
</head>
<body>

    <?php if($format == 'yazdir'): ?>
    <div class="no-print">
        <button onclick="window.print()" style="padding: 10px 20px; font-weight: bold; cursor: pointer;">🖨️ Yazdır / PDF Olarak Kaydet</button>
        <button onclick="window.close()" style="padding: 10px 20px; cursor: pointer;">Pencereyi Kapat</button>
    </div>
    <?php endif; ?>

    <div class="header-print">
        <?php if($format == 'yazdir'): ?>
            <img src="../assets/img/logo.png" alt="Ramsa Makina" class="logo"><br>
        <?php endif; ?>
        
        <div class="rapor-baslik">RAMSA MAKİNA - <?php echo $baslik; ?></div>
        <div class="meta-bilgi">Rapor Tarihi: <?php echo date("d.m.Y H:i"); ?> | Düzenleyen: <?php echo $_SESSION['ad_soyad']; ?></div>
    </div>

    <table style="font-size: 11px;">
        <thead>
            <?php if($tur == 'mesai'): ?>
            <tr>
                <th>TC No</th>
                <th>Ad Soyad</th>
                <th>Tarih</th>
                <th>Saat Aralığı</th>
                <th>Süre</th>
                <th>Mesai Türü</th>
                <th>Açıklama</th>
            </tr>
            <?php elseif($tur == 'izin'): ?>
            <tr>
                <th>TC No</th>
                <th>Ad Soyad</th>
                <th>İzin Türü</th>
                <th>Başlangıç</th>
                <th>Bitiş / İş Başı</th>
                <th>Süre</th>
                <th>Açıklama</th>
            </tr>
            <?php elseif($tur == 'personel_bilgi'): ?>
            <tr style="background-color: #d1ecf1;">
                <th>ID</th>
                <th>Ad Soyad</th>
                <th>Rol</th>
                <th>Durum</th>
                <th>TC No</th>
                <th>SGK No</th>
                <th>Doğum Tarihi</th>
                <th>Medeni Hal</th>
                <th>Çocuk Sayısı</th>
                <th>İşe Giriş</th>
                <th>Telefon</th>
                <th>E-Posta</th>
                <th>Kan Grubu</th>
                <th>Adres</th>
            </tr>
            <?php endif; ?>
        </thead>
        <tbody>
            <?php 
            $toplam_saat = 0;
            $toplam_gun = 0;
            
            foreach($veriler as $v): 
            ?>
                <tr>
                    <?php if($tur == 'mesai'): 
                        $toplam_saat += $v['toplam_saat'];
                    ?>
                        <td><?php echo formatTC($v['tc_no']); ?></td>
                        <td><?php echo $v['ad'] . ' ' . $v['soyad']; ?></td>
                        <td><?php echo date('d.m.Y', strtotime($v['tarih'])); ?></td>
                        <td><?php echo substr($v['baslangic_saati'], 0, 5) . ' - ' . substr($v['bitis_saati'], 0, 5); ?></td>
                        <td style="text-align: center; font-weight: bold;"><?php echo $v['toplam_saat']; ?></td>
                        <td>
                            <?php 
                            if($v['mesai_turu'] == 'fazla_mesai') echo 'Fazla Mesai';
                            elseif($v['mesai_turu'] == 'hafta_tatili') echo 'Hafta Tatili';
                            elseif($v['mesai_turu'] == 'resmi_tatil_mesaisi') echo 'Resmi Tatil';
                            else echo 'Normal';
                            ?>
                        </td>
                        <td><?php echo $v['aciklama']; ?></td>

                    <?php elseif($tur == 'izin'): // İzin
                        $toplam_gun += $v['toplam_gun'];
                    ?>
                        <td><?php echo formatTC($v['tc_no']); ?></td>
                        <td><?php echo $v['ad'] . ' ' . $v['soyad']; ?></td>
                        <td><?php echo strtoupper($v['izin_turu']); ?></td>
                        <td><?php echo date('d.m.Y H:i', strtotime($v['baslangic_tarihi'])); ?></td>
                        <td><?php echo date('d.m.Y H:i', strtotime($v['bitis_tarihi'])); ?></td>
                        <td style="text-align: center; font-weight: bold;">
                            <?php echo ($v['izin_turu']=='saatlik') ? $v['saatlik_sure'].' Saat' : floatval($v['toplam_gun']).' Gün'; ?>
                        </td>
                        <td><?php echo $v['aciklama']; ?></td>

                    <?php elseif($tur == 'personel_bilgi'): // Personel Bilgileri (YENİ ALANLAR) ?>
                        <td style="text-align: center;"><?php echo $v['id']; ?></td>
                        <td style="font-weight: bold;"><?php echo $v['ad'] . ' ' . $v['soyad']; ?></td>
                        <td><?php echo turkceRol($v['rol']); ?></td>
                         <td style="text-align: center; color: <?php echo ($v['durum'] == 1) ? 'green' : 'red'; ?>;">
                            <?php echo ($v['durum'] == 1) ? 'Aktif' : 'Pasif'; ?>
                        </td>
                        <td><?php echo formatTC($v['tc_no']); ?></td>
                        <td><?php echo $v['sgk_no'] ?: '-'; ?></td>
                        <td><?php echo $v['dogum_tarihi'] ? date('d.m.Y', strtotime($v['dogum_tarihi'])) : '-'; ?></td>
                        <td><?php echo $v['medeni_durum'] ?: '-'; ?></td>
                        <td style="text-align: center;"><?php echo $v['cocuk_sayisi'] ?: '0'; ?></td>
                        <td><?php echo date('d.m.Y', strtotime($v['ise_giris_tarihi'])); ?></td>
                        <td><?php echo $v['telefon']; ?></td>
                        <td><?php echo $v['email']; ?></td>
                        <td><?php echo $v['kan_grubu']; ?></td>
                        <td><?php echo $v['adres']; ?></td>
                    <?php endif; ?>
                </tr>
            <?php endforeach; ?>
            
            <?php if($tur == 'mesai'): ?>
            <tr style="background-color: #f2f2f2; font-weight: bold;">
                <td colspan="4" style="text-align: right;">GENEL TOPLAM:</td>
                <td style="text-align: center;">
                    <?php echo number_format($toplam_saat, 1) . " Saat"; ?>
                </td>
                <td colspan="2"></td>
            </tr>
            <?php elseif($tur == 'izin'): ?>
            <tr style="background-color: #f2f2f2; font-weight: bold;">
                <td colspan="5" style="text-align: right;">GENEL TOPLAM GÜN:</td>
                <td style="text-align: center;">
                    <?php echo $toplam_gun . " Gün (Sadece Günlük İzin)"; ?>
                </td>
                <td colspan="1"></td>
            </tr>
            <?php endif; ?>
        </tbody>
    </table>

    <?php if($format == 'yazdir'): ?>
    <div class="imza-blogu">
        <div class="imza-kutu">
            <div>Düzenleyen</div>
            <div style="margin-top:20px;">&nbsp;</div>
            <div class="imza-cizgi"></div>
        </div>
        <div class="imza-kutu">
            <div>Kontrol Eden (İK/Muhasebe)</div>
            <div style="margin-top:20px;">&nbsp;</div>
            <div class="imza-cizgi"></div>
        </div>
        <div class="imza-kutu">
            <div>Onaylayan</div>
            <div style="margin-top:20px;">&nbsp;</div>
            <div class="imza-cizgi"></div>
        </div>
    </div>
    
    <script>
        window.onload = function() {
            setTimeout(function() { window.print(); }, 500);
        }
    </script>
    <?php endif; ?>

</body>
</html>