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

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

$yil = $_GET['yil'] ?? date('Y');
$ay = $_GET['ay'] ?? date('m');
$format = $_GET['format'] ?? 'print'; // pdf veya excel
$personel_filtre_id = $_GET['personel_id'] ?? 'tumu';

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'];
$baslik = "MAAŞ BORDROSU - " . $ay_isimleri[$ay] . " " . $yil;

function formatSaat($saat) {
    if ($saat === null || $saat === 0.0 || $saat === 0) return 0;
    if ($saat == floor($saat)) return (int)$saat;
    return number_format($saat, 1, '.', '');
}

// --- VERİ HAZIRLAMA ---
$gun_sayisi = cal_days_in_month(CAL_GREGORIAN, $ay, $yil);
$tarih_bas = date('Y-m-01', strtotime("$yil-$ay-01"));
$tarih_bit = date('Y-m-t', strtotime("$yil-$ay-01"));
$tatiller = $pdo->query("SELECT tarih FROM resmi_tatiller")->fetchAll(PDO::FETCH_COLUMN);

// Personel Sorgusu
$sql_per = "SELECT id, ad, soyad, tc_no, ise_giris_tarihi, isten_cikis_tarihi, rol FROM kullanicilar WHERE rol != 'root'"; 
if ($personel_filtre_id != 'tumu') {
    if (is_numeric($personel_filtre_id)) $sql_per .= " AND id = " . (int)$personel_filtre_id;
    else $sql_per .= " AND rol = " . $pdo->quote($personel_filtre_id);
}
$sql_per .= " ORDER BY ad ASC";
$personeller = $pdo->query($sql_per)->fetchAll();

$rapor_verileri = [];
$tek_personel_bilgi = (count($personeller) == 1) ? $personeller[0] : null;

// --- GÖRSEL PUANTAJ TABLOSU (Sadece Tek Personel Seçiliyse ve Excel Değilse) ---
$puantaj_html = '';
if ($tek_personel_bilgi && $format != 'excel') {
    // Görsel puantajı da aktiflik durumuna göre kontrol edelim
    $goster = true;
    if ($tek_personel_bilgi['ise_giris_tarihi'] > $tarih_bit) $goster = false;
    if (!empty($tek_personel_bilgi['isten_cikis_tarihi']) && $tek_personel_bilgi['isten_cikis_tarihi'] < $tarih_bas) $goster = false;

    if ($goster) {
        $gun_kisa = ['Mon'=>'Pzt', 'Tue'=>'Sal', 'Wed'=>'Çar', 'Thu'=>'Per', 'Fri'=>'Cum', 'Sat'=>'Cts', 'Sun'=>'Pzr'];
        
        $head = '<tr><th style="width:80px; background:#333; color:#fff;">GÜN</th>';
        $body = '<tr><td style="font-weight:bold; background:#f0f0f0;">DURUM</td>';
        
        $p_giris = $tek_personel_bilgi['ise_giris_tarihi'];
        $p_cikis = $tek_personel_bilgi['isten_cikis_tarihi'];

        for($d=1; $d<=$gun_sayisi; $d++):
            $t_str = "$yil-$ay-" . sprintf('%02d', $d);
            $g_ing = date('D', strtotime($t_str));
            $gun_no = date('N', strtotime($t_str));
            
            $aktif = true;
            if($t_str < $p_giris) $aktif = false;
            if(!empty($p_cikis) && $t_str > $p_cikis) $aktif = false;

            $is_tatil = in_array($t_str, $tatiller);
            
            $bg_head = ($is_tatil || $gun_no == 7) ? 'background:#d32f2f; color:white;' : (($gun_no == 6) ? 'background:#757575; color:white;' : 'background:#eee;');
            $head .= "<th style='$bg_head font-size:9px; border:1px solid #ccc;'>{$d}<br>{$gun_kisa[$g_ing]}</th>";
            
            $hucre_icerik = "";
            
            if(!$aktif) {
                $hucre_icerik = "<span style='color:#ccc;'>X</span>";
            } else {
                // --- ÇOKLU İZİN KONTROLÜ ---
                $izinler = $pdo->query("SELECT * FROM izin_talepleri WHERE calisan_id={$tek_personel_bilgi['id']} AND durum='onaylandi' AND '$t_str' BETWEEN DATE(baslangic_tarihi) AND DATE(bitis_tarihi)")->fetchAll();
                
                $mesai = $pdo->query("SELECT SUM(toplam_saat) as toplam FROM mesai_hareketleri WHERE calisan_id={$tek_personel_bilgi['id']} AND durum='onaylandi' AND tarih='$t_str' AND mesai_turu IN ('fazla_mesai', 'hafta_tatili', 'resmi_tatil_mesaisi')")->fetch();
                
                foreach ($izinler as $iz) {
                    if ($iz['izin_turu'] == 'sut_izni') {
                        $hucre_icerik .= "<div style='color:#d63384; font-weight:bold; font-size:8px;'>SÜT</div>";
                    } elseif (in_array($iz['izin_turu'], ['hastalik','ucretsiz','saatlik','mesaiye_gelmedi','mazeret'])) {
                        if ($iz['izin_turu'] == 'saatlik' || $iz['izin_turu'] == 'mesaiye_gelmedi') {
                            $hucre_icerik .= "<div style='color:red; font-weight:bold; font-size:8px;'>-{$iz['saatlik_sure']}</div>";
                        } else {
                            $tur = strtoupper(substr($iz['izin_turu'], 0, 3));
                            $hucre_icerik .= "<div style='color:red; font-weight:bold; font-size:8px;'>$tur</div>";
                        }
                    } else {
                        $tur = strtoupper(substr($iz['izin_turu'], 0, 3));
                        $hucre_icerik .= "<div style='color:blue; font-weight:bold; font-size:8px;'>$tur</div>";
                    }
                }

                if ($mesai && $mesai['toplam'] > 0) {
                    $hucre_icerik .= "<div style='color:green; font-weight:bold; font-size:8px;'>+{$mesai['toplam']}</div>";
                }
                
                if(empty($hucre_icerik) && !$is_tatil && $gun_no != 7) {
                    $hucre_icerik = "<span style='color:#ccc;'>&#10003;</span>";
                }
            }

            $td_bg = ($is_tatil || $gun_no == 7) ? 'background:#ffebee;' : (($gun_no == 6) ? 'background:#f8f9fa;' : '');
            $body .= "<td style='$td_bg border:1px solid #ccc; font-size:9px; text-align:center; vertical-align:middle; height:40px;'>$hucre_icerik</td>";
        endfor;
        $head .= '</tr>'; $body .= '</tr>';
        
        $puantaj_html = "
        <div style='margin-top:20px; page-break-inside:avoid;'>
            <h5 style='border-bottom:2px solid #333; margin-bottom:5px; font-size:12px;'>PUANTAJ ÖZETİ</h5>
            <table style='width:100%; border-collapse:collapse; border:1px solid #ccc;'>
                <thead>$head</thead>
                <tbody>$body</tbody>
            </table>
        </div>";
    }
}

// --- FİNANSAL HESAPLAMA MOTORU ---
foreach($personeller as $per) {
    // --- GÜNCELLEME: AKTİFLİK KONTROLÜ (Tutarlılık Sağlandı) ---
    // Eğer personel işe giriş tarihi rapor ayının bitişinden sonraysa VEYA işten çıkış tarihi rapor ayının başından önceyse listeye ekleme.
    if ($per['ise_giris_tarihi'] > $tarih_bit) continue; 
    if (!empty($per['isten_cikis_tarihi']) && $per['isten_cikis_tarihi'] < $tarih_bas) continue;

    $maas_db = maasGetir($pdo, $per['id'], $yil, $ay);
    if($maas_db <= 0) continue;
    
    $gunluk_ucret = $maas_db / 30;
    $saatlik = $maas_db / 225;
    
    $k15 = 0; $k20 = 0; $toplam_kesinti_saat = 0; $calisilmayan_gun = 0;

    $avans = $pdo->query("SELECT SUM(avans_miktari) FROM avans_hareketleri WHERE calisan_id={$per['id']} AND islem_tarihi BETWEEN '$tarih_bas' AND '$tarih_bit' AND durum='onaylandi'")->fetchColumn() ?: 0;
    $ikramiye = $pdo->query("SELECT SUM(miktar) FROM ikramiyeler WHERE calisan_id={$per['id']} AND donem_tarihi BETWEEN '$tarih_bas' AND '$tarih_bit'")->fetchColumn() ?: 0;

    $ise_giris = $per['ise_giris_tarihi'];
    $isten_cikis = $per['isten_cikis_tarihi'];

    for($d=1; $d<=$gun_sayisi; $d++) {
        $tarih = "$yil-$ay-" . sprintf('%02d', $d);
        $gun_no = date('N', strtotime($tarih)); 
        
        $aktif_mi = true;
        if ($tarih < $ise_giris) $aktif_mi = false;
        elseif (!empty($isten_cikis) && $tarih > $isten_cikis) $aktif_mi = false;

        if (!$aktif_mi) { $calisilmayan_gun++; continue; }

        $is_tatil = in_array($tarih, $tatiller);
        $is_calisma_gunu = ($gun_no < 6 && !$is_tatil);
        
        // --- KESİNTİ HESABI ---
        $gunluk_ham_kesinti = 0;
        
        // Tüm izinleri çek
        $izinler = $pdo->query("SELECT * FROM izin_talepleri WHERE calisan_id={$per['id']} AND durum='onaylandi' AND '$tarih' BETWEEN DATE(baslangic_tarihi) AND DATE(bitis_tarihi)")->fetchAll();
        
        foreach ($izinler as $izin) {
            $ucretsiz_turler = ['hastalik', 'saatlik', 'ucretsiz', 'mazeret', 'mesaiye_gelmedi'];
            // Kesinti sadece çalışma günlerinde yapılır
            if (in_array($izin['izin_turu'], $ucretsiz_turler) && $is_calisma_gunu) {
                if ($izin['izin_turu'] == 'saatlik' || $izin['izin_turu'] == 'mesaiye_gelmedi') {
                    $gunluk_ham_kesinti += (float)$izin['saatlik_sure'];
                } else {
                    $gunluk_ham_kesinti = 9; 
                }
            }
        }
        
        if ($gunluk_ham_kesinti > 9) $gunluk_ham_kesinti = 9;

        // --- MESAİ HESABI ---
        $gunluk_ham_mesai = 0;
        $mesai_kaydi = $pdo->query("SELECT SUM(toplam_saat) as toplam, mesai_turu FROM mesai_hareketleri WHERE calisan_id={$per['id']} AND durum='onaylandi' AND tarih='$tarih' AND mesai_turu IN ('fazla_mesai', 'hafta_tatili', 'resmi_tatil_mesaisi', 'vardiya_gece')")->fetch();
        if ($mesai_kaydi && $mesai_kaydi['toplam'] > 0) {
            $gunluk_ham_mesai = (float)$mesai_kaydi['toplam'];
        }

        // --- MAHSUPLAŞMA ---
        $net_kesinti = $gunluk_ham_kesinti;
        $net_mesai = $gunluk_ham_mesai;

        if ($gunluk_ham_kesinti > 0 && $gunluk_ham_mesai > 0) {
            if ($gunluk_ham_mesai >= $gunluk_ham_kesinti) {
                $net_mesai = $gunluk_ham_mesai - $gunluk_ham_kesinti;
                $net_kesinti = 0;
            } else {
                $net_kesinti = $gunluk_ham_kesinti - $gunluk_ham_mesai;
                $net_mesai = 0;
            }
        }

        if ($net_kesinti > 0) $toplam_kesinti_saat += $net_kesinti;
        
        if ($net_mesai > 0) {
            if ($is_tatil || $gun_no == 7 || $mesai_kaydi['mesai_turu'] == 'resmi_tatil_mesaisi') {
                $k20 += $net_mesai;
            } else {
                $k15 += $net_mesai;
            }
        }
    }
    
    $kist_kesinti_tutar = $calisilmayan_gun * $gunluk_ucret;
    $ek_tutar = ($k15 * $saatlik * 1.5) + ($k20 * $saatlik * 2.0);
    $izin_kesinti_tutar = ($toplam_kesinti_saat * $saatlik);
    
    $toplam_kesinti = $izin_kesinti_tutar + $avans + $kist_kesinti_tutar;
    $toplam_hakedis = ($maas_db + $ek_tutar + $ikramiye) - $toplam_kesinti;
    if($toplam_hakedis < 0) $toplam_hakedis = 0;
    
    $rapor_verileri[] = [
        'ad_soyad' => $per['ad'].' '.$per['soyad'],
        'net_maas' => $maas_db,
        's15' => $k15, 
        's20' => $k20,
        'ek_tutar' => $ek_tutar,
        'ikramiye' => $ikramiye,
        'kesinti_s' => $toplam_kesinti_saat,
        'kist_gun' => $calisilmayan_gun,
        'avans' => $avans,
        'top_kes' => $toplam_kesinti,
        'hakedis' => $toplam_hakedis
    ];
}

// --- EXCEL ÇIKTISI ---
if ($format == 'excel') {
    header("Content-Type: application/vnd.ms-excel; charset=utf-8");
    header("Content-Disposition: attachment; filename=Maas_Bordrosu_$yil-$ay.xls");
    echo "\xEF\xBB\xBF"; 
    ?>
    <table border="1">
        <thead>
            <tr><th colspan="11" style="background-color:#ccc; font-size:14px;"><?php echo $baslik; ?></th></tr>
            <tr>
                <th>Personel</th><th>Net Maaş</th><th>FM 1.5x (Saat)</th><th>FM 2.0x (Saat)</th>
                <th>Mesai Tutar</th><th>İkramiye</th><th>İzin Kes. (Saat)</th><th>Kıst (Gün)</th><th>Avans</th><th>Top. Kesinti</th><th>NET ÖDENECEK</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($rapor_verileri as $v): ?>
            <tr>
                <td><?php echo $v['ad_soyad']; ?></td>
                <td><?php echo number_format($v['net_maas'], 2); ?></td>
                <td><?php echo str_replace('.',',',$v['s15']); ?></td>
                <td><?php echo str_replace('.',',',$v['s20']); ?></td>
                <td><?php echo number_format($v['ek_tutar'], 2); ?></td>
                <td><?php echo number_format($v['ikramiye'], 2); ?></td>
                <td><?php echo str_replace('.',',',$v['kesinti_s']); ?></td>
                <td><?php echo $v['kist_gun']; ?></td>
                <td><?php echo number_format($v['avans'], 2); ?></td>
                <td><?php echo number_format($v['top_kes'], 2); ?></td>
                <td style="font-weight:bold;"><?php echo number_format($v['hakedis'], 2); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php
    exit;
}
?>

<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <title>Maaş Raporu</title>
    <style>
        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; font-size: 11px; color: #333; padding: 20px; }
        .antet { display: flex; justify-content: space-between; align-items: center; border-bottom: 2px solid #2A7BB8; padding-bottom: 10px; margin-bottom: 20px; }
        .logo img { height: 60px; }
        .sirket-bilgi { text-align: right; }
        .sirket-bilgi h2 { margin: 0; color: #2A7BB8; font-size: 16px; font-weight: 800; text-transform: uppercase; }
        .sirket-bilgi p { margin: 2px 0; font-size: 10px; color: #666; }
        table { width: 100%; border-collapse: collapse; margin-bottom: 10px; }
        th, td { border: 1px solid #ccc; padding: 5px; text-align: center; font-size: 10px; }
        th { background-color: #f2f2f2; font-weight: bold; }
        .imza-kutusu { margin-top: 50px; display: flex; justify-content: space-between; page-break-inside: avoid; }
        .kutu { text-align: center; width: 30%; }
        .kutu p { margin-bottom: 40px; font-weight: bold; text-decoration: underline; }
        @media print { .no-print { display: none; } body { padding: 0; } }
    </style>
</head>
<body>
    <div class="no-print" style="text-align: right; margin-bottom: 10px;">
        <button onclick="window.print()" style="padding: 8px 15px; background: #2A7BB8; color: white; border: none; cursor: pointer; border-radius: 4px;">Yazdır / PDF Kaydet</button>
    </div>

    <div class="antet">
        <div class="logo"><img src="../assets/img/logo.png" alt="Firma Logosu"></div>
        <div class="sirket-bilgi">
            <h2>Ramsa Personel Yönetim Sistemi</h2>
            <p>Maaş Bordrosu ve Hakediş Raporu</p>
            <p>Rapor Tarihi: <?php echo $rapor_tarihi_saati; ?></p>
        </div>
    </div>

    <div style="text-align: center; margin-bottom: 15px;">
        <h3 style="margin: 0; border: 1px solid #eee; background: #fafafa; padding: 5px;"><?php echo $baslik; ?></h3>
    </div>

    <?php if ($tek_personel_bilgi && count($rapor_verileri) > 0): ?>
        <div style="margin-bottom: 10px; padding: 8px; background: #f9f9f9; border-left: 4px solid #2A7BB8; font-size: 11px;">
            <strong>Personel:</strong> <?php echo $tek_personel_bilgi['ad'].' '.$tek_personel_bilgi['soyad']; ?> &nbsp;|&nbsp; 
            <strong>TC Kimlik:</strong> <?php echo $tek_personel_bilgi['tc_no']; ?> &nbsp;|&nbsp;
            <?php $guncel_maas = maasGetir($pdo, $tek_personel_bilgi['id'], $yil, $ay); ?>
            <strong>Net Maaş (<?php echo "$ay/$yil"; ?>):</strong> <?php echo number_format($guncel_maas, 2); ?> ₺
        </div>
    <?php endif; ?>

    <?php if(count($rapor_verileri) > 0): ?>
    <table>
        <thead>
            <tr>
                <th>Personel</th><th>Net Maaş</th><th>FM 1.5x (S)</th><th>FM 2.0x (S)</th>
                <th>Mesai Tutar</th><th>İkramiye</th><th>Kesinti (S)</th><th>Kıst (G)</th><th>Avans</th>
                <th style="background-color: #ffebee;">Top. Kesinti</th>
                <th style="background-color: #e8f5e9;">Net Ödenecek</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($rapor_verileri as $v): ?>
            <tr>
                <td style="text-align:left;"><?php echo $v['ad_soyad']; ?></td>
                <td><?php echo number_format($v['net_maas'], 2); ?></td>
                <td><?php echo formatSaat($v['s15']); ?></td>
                <td><?php echo formatSaat($v['s20']); ?></td>
                <td style="color: green;">+<?php echo number_format($v['ek_tutar'], 2); ?></td>
                <td><?php echo ($v['ikramiye'] > 0) ? number_format($v['ikramiye'], 2) : '-'; ?></td>
                <td><?php echo formatSaat($v['kesinti_s']); ?></td>
                <td><?php echo $v['kist_gun'] > 0 ? $v['kist_gun'] : '-'; ?></td>
                <td><?php echo number_format($v['avans'], 2); ?></td>
                <td style="color: red; font-weight: bold;">-<?php echo number_format($v['top_kes'], 2); ?></td>
                <td style="font-weight:bold; background-color: #f1f8e9;"><?php echo number_format($v['hakedis'], 2); ?> ₺</td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php else: ?>
        <div style="text-align:center; padding:20px; color:#999; border:1px solid #ddd; background:#fcfcfc;">
            Bu dönem için görüntülenecek kayıt bulunamadı veya seçilen personel bu dönemde aktif değil.
        </div>
    <?php endif; ?>

    <?php echo $puantaj_html; ?>

    <div class="imza-kutusu">
        <div class="kutu"><p>Düzenleyen</p><br><span style="font-size:10px;">İnsan Kaynakları</span></div>
        <div class="kutu"><p>Kontrol</p><br><span style="font-size:10px;">Muhasebe</span></div>
        <div class="kutu"><p>Onay</p><br><span style="font-size:10px;">Genel Müdür</span></div>
    </div>

    <?php if($format == 'pdf'): ?>
    <script>window.onload = function() { setTimeout(function(){ window.print(); }, 500); }</script>
    <?php endif; ?>
</body>
</html>