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

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

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

// --- PARAMETRELERİ AL ---
$filtre_durum = $_GET['filtre'] ?? 'aktif';
$secilen_ay = $_GET['ay'] ?? date('m');
$secilen_yil = $_GET['yil'] ?? date('Y');
$format = guvenlik($_GET['format'] ?? 'print'); 

// Filtreye göre Hesaplama Dönemini Belirle
if ($filtre_durum == 'aktif') {
    $hesap_ay = date('m');
    $hesap_yil = date('Y');
} else {
    $hesap_ay = $secilen_ay;
    $hesap_yil = $secilen_yil;
}

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 = "MERKEZİ MALİ RAPOR - " . $ay_isimleri[$hesap_ay] . " " . $hesap_yil;

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

// Personel Listesi
$personeller = $pdo->query("SELECT id, ad, soyad, rol, ise_giris_tarihi, isten_cikis_tarihi FROM kullanicilar WHERE rol != 'root' ORDER BY ad ASC")->fetchAll();

$mali_veriler = [];
$g_maas = 0; $g_mesai = 0; $g_kesinti = 0; $g_avans = 0; $g_odenecek = 0;

foreach ($personeller as $p) {
    // --- GÜNCELLEME: AKTİFLİK KONTROLÜ (Diğer sayfalarla tutarlılık) ---
    // 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 rapora ekleme.
    if ($p['ise_giris_tarihi'] > $tarih_bit) continue; 
    if (!empty($p['isten_cikis_tarihi']) && $p['isten_cikis_tarihi'] < $tarih_bas) continue;

    // Tarihsel Maaş (Geçmişe Yönelik Doğru Hesap)
    $maas_db = maasGetir($pdo, $p['id'], $hesap_yil, $hesap_ay);
    if ($maas_db <= 0) continue; 

    $gunluk_ucret = $maas_db / 30;
    $saatlik_ucret = $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={$p['id']} AND islem_tarihi BETWEEN '$tarih_bas' AND '$tarih_bit' AND durum='onaylandi'")->fetchColumn() ?: 0;

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

    for($d=1; $d<=$gun_sayisi; $d++) {
        $tarih = "$hesap_yil-$hesap_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_haftasonu = ($gun_no >= 6);
        $is_calisma_gunu = ($gun_no < 6 && !$is_tatil);

        $izin = $pdo->query("SELECT * FROM izin_talepleri WHERE calisan_id={$p['id']} AND durum='onaylandi' AND '$tarih' BETWEEN DATE(baslangic_tarihi) AND DATE(bitis_tarihi)")->fetch();
        $gunluk_ham_kesinti = 0;

        if ($izin) {
            $ucretsizler = ['hastalik', 'saatlik', 'mazeret', 'ucretsiz', 'mesaiye_gelmedi'];
            if (in_array($izin['izin_turu'], $ucretsizler) && $is_calisma_gunu) {
                if ($izin['izin_turu'] == 'saatlik' || $izin['izin_turu'] == 'mesaiye_gelmedi') {
                    $gunluk_ham_kesinti = (float)$izin['saatlik_sure'];
                } else {
                    $bas_saat = date('H:i', strtotime($izin['baslangic_tarihi']));
                    if (date('Y-m-d', strtotime($izin['baslangic_tarihi'])) == $tarih) {
                        if ($bas_saat == '08:00') $gunluk_ham_kesinti = 9;
                        elseif ($bas_saat >= '13:00') $gunluk_ham_kesinti = 5; 
                        else {
                            $diff = (strtotime('18:00') - strtotime($bas_saat)) / 3600;
                            $gunluk_ham_kesinti = ($diff > 0) ? $diff : 9;
                        }
                    } else {
                        $gunluk_ham_kesinti = 9; 
                    }
                }
            }
        }

        $mesai = $pdo->query("SELECT SUM(toplam_saat) as toplam, mesai_turu FROM mesai_hareketleri WHERE calisan_id={$p['id']} AND durum='onaylandi' AND tarih='$tarih' AND mesai_turu IN ('fazla_mesai', 'hafta_tatili', 'resmi_tatil_mesaisi', 'vardiya_gece')")->fetch();
        $gunluk_ham_mesai = ($mesai && $mesai['toplam'] > 0) ? (float)$mesai['toplam'] : 0;

        $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_haftasonu || $is_tatil || $mesai['mesai_turu'] == 'resmi_tatil_mesaisi' || $mesai['mesai_turu'] == 'hafta_tatili') $k20 += $net_mesai; 
            else $k15 += $net_mesai;
        }
    }

    $mesai_hakedis = ($k15 * $saatlik_ucret * 1.5) + ($k20 * $saatlik_ucret * 2.0);
    $izin_kesinti_tutar = ($toplam_kesinti_saat * $saatlik_ucret);
    $kist_kesinti_tutar = $calisilmayan_gun * $gunluk_ucret;
    $toplam_kesinti = $izin_kesinti_tutar + $kist_kesinti_tutar;
    
    $net_odenecek = $maas_db + $mesai_hakedis - $toplam_kesinti - $avans;
    if($net_odenecek < 0) $net_odenecek = 0;

    $g_maas += $maas_db;
    $g_mesai += $mesai_hakedis;
    $g_kesinti += $toplam_kesinti;
    $g_avans += $avans;
    $g_odenecek += $net_odenecek;

    $mali_veriler[] = [
        'ad_soyad' => $p['ad'].' '.$p['soyad'],
        'net_maas' => $maas_db,
        'mesai_15' => formatSaatListeleme($k15),
        'mesai_20' => formatSaatListeleme($k20),
        'mesai_tl' => $mesai_hakedis,
        'kesinti_tl' => $toplam_kesinti,
        'avans' => $avans,
        'odenecek' => $net_odenecek
    ];
}

// --- EXCEL ÇIKTISI ---
if ($format == 'excel') {
    header("Content-Type: application/vnd.ms-excel; charset=utf-8");
    header("Content-Disposition: attachment; filename=Mali_Rapor_{$hesap_yil}_{$hesap_ay}.xls");
    header("Pragma: no-cache"); header("Expires: 0"); echo "\xEF\xBB\xBF";
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <title>Ramsa Makine - <?php echo $baslik; ?></title>
    <style>
        body { font-family: Arial, sans-serif; font-size: 10px; margin: 0; padding: 20px; }
        .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; text-transform: uppercase; }
        table { width: 100%; border-collapse: collapse; margin-bottom: 20px; table-layout: fixed; }
        th, td { border: 1px solid #000; padding: 5px; text-align: center; }
        th { background-color: #ddd; font-weight: bold; }
        .text-start { text-align: left !important; }
        .total-row { background-color: #ffc107; color: #000; font-weight: bold; border: 2px solid #000; }
        .odenecek-tutar-cell { background-color: #f0f0f0; font-weight: bold; font-size: 11px; }
        .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; }
        @media print { .no-print { display: none; } th { background-color: #eee !important; } .total-row { background-color: #ffc107 !important; -webkit-print-color-adjust: exact; } }
    </style>
</head>
<body>
    <?php if($format == 'pdf'): ?>
        <div class="no-print" style="margin-bottom: 20px; text-align: right;">
            <button onclick="window.print()" style="padding: 10px 20px; background-color: #2A7BB8; color: white; border: none; font-weight: bold; cursor: pointer; border-radius: 5px;">🖨️ YAZDIR / PDF KAYDET</button>
        </div>
    <?php endif; ?>

    <div class="rapor-header">
        <img src="../assets/img/logo.png" alt="Ramsa Makine">
        <div class="text-center">
            <h1><?php echo $baslik; ?></h1>
            <p style="margin: 5px 0 0 0; font-size: 10px;">Rapor Oluşturulma Tarihi: <?php echo $rapor_tarihi_saati; ?></p>
        </div>
        <div style="width: 50px;"></div>
    </div>
    
    <table style="font-size: 9px;">
        <thead>
            <tr>
                <th class="text-start" style="width: 15%;">PERSONEL</th>
                <th>BAZ MAAŞ</th>
                <th>FM 1.5x (S)</th>
                <th>FM 2.0x (S)</th>
                <th>MESAİ TUTAR</th>
                <th>TOP. KESİNTİ</th>
                <th>AVANS</th>
                <th>ÖDENECEK</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($mali_veriler as $m): ?>
            <tr>
                <td class="text-start"><?php echo $m['ad_soyad']; ?></td>
                <td><?php echo number_format($m['net_maas'], 2); ?> ₺</td>
                <td><?php echo $m['mesai_15']; ?></td>
                <td><?php echo $m['mesai_20']; ?></td>
                <td style="color: green; font-weight: bold;">+<?php echo number_format($m['mesai_tl'], 2); ?> ₺</td>
                <td style="color: red; font-weight: bold;">-<?php echo number_format($m['kesinti_tl'], 2); ?> ₺</td>
                <td style="color: red;">-<?php echo number_format($m['avans'], 2); ?> ₺</td>
                <td class="odenecek-tutar-cell"><?php echo number_format($m['odenecek'], 2); ?> ₺</td>
            </tr>
            <?php endforeach; ?>
            <tr class="total-row">
                <td class="text-start">GENEL TOPLAM:</td>
                <td><?php echo number_format($g_maas, 2); ?> ₺</td>
                <td colspan="2">-</td>
                <td style="color: green;">+<?php echo number_format($g_mesai, 2); ?> ₺</td>
                <td style="color: red;">-<?php echo number_format($g_kesinti, 2); ?> ₺</td>
                <td style="color: red;">-<?php echo number_format($g_avans, 2); ?> ₺</td>
                <td style="background-color: #d1ecf1; border: 2px solid #000;"><?php echo number_format($g_odenecek, 2); ?> ₺</td>
            </tr>
        </tbody>
    </table>

    <div class="imza-blogu">
        <div class="imza-kutu"><div class="imza-baslik">HAZIRLAYAN / MUHASEBE</div></div>
        <div class="imza-kutu"><div class="imza-baslik">FİNANS MÜDÜRÜ</div></div>
        <div class="imza-kutu"><div class="imza-baslik">GENEL MÜDÜR ONAYI</div></div>
    </div>
    
    <?php if($format == 'pdf'): ?><script>window.onload = function() { setTimeout(function() { window.print(); }, 500); }</script><?php endif; ?>
</body>
</html>