<?php
// modules/mali-rapor-indirme.php
session_start();
require_once '../config/db.php';
require_once '../config/functions.php';
// Yetkili Roller: Root, Yönetici, Muhasebe, İnsan Kaynakları
yetkiKontrol(['root', 'yonetici', 'muhasebe', 'insan_kaynaklari']);
// Fonksiyonları yükle (mali-rapor.php'den alınmıştır)
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, '.', '');
}
// --- FİLTRELER VE PARAMETRELER ---
$yil = guvenlik($_GET['yil'] ?? date('Y'));
$ay = guvenlik($_GET['ay'] ?? date('m'));
$format = guvenlik($_GET['format'] ?? 'print'); // 'excel' veya 'pdf' (print)
// Rapor tarihi, saat ve zaman dilimi ayarı
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'];
// BAŞLIK
$baslik = "MERKEZİ MALİ RAPOR - " . $ay_isimleri[$ay] . " " . $yil;
$ay_baslangici = date('Y-m-01', strtotime("$yil-$ay-01"));
$ay_sonu = date('Y-m-t', strtotime("$yil-$ay-01"));
// --- KATSAYILAR ---
$carpan_15 = 1.5;
$carpan_20 = 2.0;
// Maaş hesaplama mantığı maas-hesapla.php'den alınmıştır, aylık standart saat 225 saat varsayılır.
$aylik_std_saat = (float)($pdo->query("SELECT ayar_degeri FROM site_ayarlari WHERE ayar_anahtari = 'aylik_mesai_saati'")->fetchColumn() ?: 225.0);
// Resmi Tatilleri Çek
$tatiller = $pdo->query("SELECT tarih FROM resmi_tatiller")->fetchAll(PDO::FETCH_COLUMN);
// Personel Listesi: Root hariç, aktif personel
$sql_per = "SELECT id, ad, soyad, aylik_net_maas FROM kullanicilar WHERE durum=1 AND rol != 'root' ORDER BY ad ASC";
$personeller = $pdo->query($sql_per)->fetchAll();
// --- VERİ TOPLAMA VE HESAPLAMA (MANTIK BURADA TEKRAR EDİLDİ) ---
$mali_veriler = [];
$genel_toplam_net_maas = 0;
$genel_toplam_mesai_hakedis = 0;
$genel_toplam_izin_kesinti = 0;
$genel_toplam_avans = 0;
$genel_toplam_odenecek = 0;
foreach ($personeller as $p) {
$p_id = $p['id'];
$net_maas = (float)$p['aylik_net_maas'];
$genel_toplam_net_maas += $net_maas;
// Geçici Mesai ve Kesinti Toplamları
$toplam_fazla_mesai_15 = 0;
$toplam_fazla_mesai_20 = 0;
$izin_kesinti_saati = 0;
// GÜNLÜK DÖNGÜ (Maaş ve Kesinti Hesaplama)
$gun_sayisi = cal_days_in_month(CAL_GREGORIAN, $ay, $yil);
for($d=1; $d<=$gun_sayisi; $d++) {
$tarih = date('Y-m-d', strtotime("$yil-$ay-$d"));
$gun_no = date('N', strtotime($tarih));
$is_tatil = in_array($tarih, $tatiller);
// 1. İzin Kesinti Hesaplama
$izin = $pdo->query("SELECT izin_turu, saatlik_sure, baslangic_tarihi, bitis_tarihi FROM izin_talepleri
WHERE calisan_id={$p_id} AND durum='onaylandi'
AND '$tarih' BETWEEN DATE(baslangic_tarihi) AND DATE(bitis_tarihi)")->fetch();
if ($izin) {
if ($izin['izin_turu'] == 'diger') {
$izin_kesinti_saati += 9;
} elseif ($izin['izin_turu'] == 'saatlik') {
$izin_sure = (float)$izin['saatlik_sure'];
$bas_saat_str = date('H:i', strtotime($izin['baslangic_tarihi']));
$bit_saat_str = date('H:i', strtotime($izin['bitis_tarihi']));
if ($gun_no <= 5 && $bas_saat_str === '08:00' && $bit_saat_str === '12:00') {
$izin_kesinti_saati += 4.0;
} elseif ($gun_no <= 5 && $bas_saat_str === '13:00' && $bit_saat_str === '18:00') {
$izin_kesinti_saati += 5.0;
} else {
$izin_kesinti_saati += $izin_sure;
}
}
}
// 2. Fazla Mesai Hesaplama
$mesai = $pdo->query("SELECT SUM(toplam_saat) 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')")->fetchColumn() ?: 0;
if ($mesai > 0) {
$is_pazar = ($gun_no == 7);
if ($is_pazar || $is_tatil) {
$toplam_fazla_mesai_20 += $mesai;
} else {
$toplam_fazla_mesai_15 += $mesai;
}
}
}
// MAAŞ HESAPLAMA BİTİŞ
$saatlik_net_ucret = $net_maas / $aylik_std_saat;
$fazla_mesai_hakedis_15 = $toplam_fazla_mesai_15 * $saatlik_net_ucret * $carpan_15;
$fazla_mesai_hakedis_20 = $toplam_fazla_mesai_20 * $saatlik_net_ucret * $carpan_20;
$toplam_mesai_hakedis_tutar = $fazla_mesai_hakedis_15 + $fazla_mesai_hakedis_20;
$izin_kesinti_tutar = $izin_kesinti_saati * $saatlik_net_ucret;
$toplam_kesinti_tutar = $izin_kesinti_tutar;
// AVANS HESAPLAMA
$stmt_avans = $pdo->prepare("
SELECT COALESCE(SUM(avans_miktari), 0)
FROM avans_hareketleri
WHERE calisan_id = ? AND durum = 'onaylandi'
AND islem_tarihi BETWEEN ? AND ?
");
$stmt_avans->execute([$p_id, $ay_baslangici, $ay_sonu]);
$aylik_avans = (float)$stmt_avans->fetchColumn();
// NET ÖDENECEK HESAPLAMA
$net_hakedis = $net_maas + $toplam_mesai_hakedis_tutar - $toplam_kesinti_tutar;
$odenecek_tutar = $net_hakedis - $aylik_avans;
// Genel Toplamları Güncelle
$genel_toplam_mesai_hakedis += $toplam_mesai_hakedis_tutar;
$genel_toplam_izin_kesinti += $izin_kesinti_tutar;
$genel_toplam_avans += $aylik_avans;
$genel_toplam_odenecek += $odenecek_tutar;
$mali_veriler[] = [
'ad_soyad' => $p['ad'] . ' ' . $p['soyad'],
'net_maas' => $net_maas,
'mesai_15_saat' => formatSaatListeleme($toplam_fazla_mesai_15),
'mesai_20_saat' => formatSaatListeleme($toplam_fazla_mesai_20),
'mesai_hakedis' => $toplam_mesai_hakedis_tutar,
'izin_kesinti_saat' => formatSaatListeleme($izin_kesinti_saati),
'izin_kesinti_tutar' => $izin_kesinti_tutar,
'avans' => $aylik_avans,
'net_hakedis' => $net_hakedis,
'odenecek' => $odenecek_tutar,
];
}
// --- RAPOR ÇIKTI BAŞLIKLARI (Excel/PDF) ---
if ($format == 'excel') {
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=Mali_Rapor_{$yil}_{$ay}.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo "\xEF\xBB\xBF"; // BOM
} elseif ($format == 'pdf') {
// Print/PDF çıktısı için özel CSS eklenecek ve HTML formatı kullanılacak
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<title>Ramsa Makine Sanayii - <?php echo $baslik; ?></title>
<style>
body { font-family: Arial, sans-serif; font-size: 10px; margin: 0; padding: 20px; }
/* RAPOR ANTEPİ VE LOGO */
.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;
}
.rapor-header p {
font-size: 10px;
margin: 0;
color: #666;
}
/* TABLO STİLİ */
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; }
.text-end { text-align: right !important; }
/* Satır Stilleri */
/* BAŞLIK VE TOPLAM SATIRLARI YAZICI DOSTU VE OKUNAKLI HALE GETİRİLDİ */
.header-row { background-color: #343a40; color: #000; } /* Yazı Siyah */
.subheader-row { background-color: #ccc; color: #000; } /* Yazı Siyah */
.total-row {
background-color: #ffc107;
color: #000; /* Yazı Siyah */
font-weight: bold;
border: 2px solid #000; /* Çerçeveyi kalınlaştırdık */
}
/* Tekil Ödenecek Tutar Stili (Yazıcıda ve Ekranda Görünen) */
.odenecek-tutar-cell {
background-color: #f0f0f0; /* Açık gri zemin */
font-weight: bold;
font-size: 11px;
color: #000; /* Yazı Siyah */
}
/* İmza Alanı Stilleri */
.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; }
/* PDF/PRINT ÖZEL STİLLER */
@media print {
body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.no-print { display: none; }
.rapor-header { border-bottom-color: #000; }
th { background-color: #eee !important; }
/* Yazıcı Dostu Başlıklar (Renk basılmasa bile metin okunmalı) */
.header-row { background-color: #eee !important; color: #000 !important; }
.subheader-row { background-color: #ddd !important; color: #000 !important; }
.total-row {
background-color: #ffc107 !important; /* Sarı tonunu koru */
color: #000 !important; /* Yazı Siyah */
border-color: #000 !important;
}
/* Genel Toplamın Son Sütunu */
.total-row > td:last-child {
background-color: #d1ecf1 !important; /* Farklı bir açık ton, zorunlu değil ama okunaklılığı artırır */
color: #000 !important;
border: 2px solid #000 !important;
}
/* Sayfa URL'sini gizlemek için altbilgi ayarları */
@page {
size: landscape; /* Geniş tablo için yatay */
margin: 15mm;
@bottom-left { content: ""; }
@bottom-right { content: ""; }
@top-left { content: ""; }
@top-right { content: ""; }
}
}
</style>
</head>
<body>
<?php if($format == 'pdf'): ?>
<div class="no-print" style="margin-bottom: 20px;">
<button onclick="window.print()" style="padding: 10px 20px; font-weight: bold; cursor: pointer;">🖨️ YAZDIR / PDF OLUŞTUR</button>
</div>
<?php endif; ?>
<div class="rapor-header">
<img src="../assets/img/logo.png" alt="Şirket Logosu">
<div class="text-center">
<h1><?php echo $baslik; ?></h1>
<p>Rapor Tarihi: <?php echo $rapor_tarihi_saati; ?></p>
</div>
<div style="width: 50px;"></div>
</div>
<?php if (empty($mali_veriler)): ?>
<div style="text-align: center; padding: 50px; border: 1px dashed #ccc;">
Seçilen dönemde aktif personel veya mali veri bulunmamaktadır.
</div>
<?php else: ?>
<table style="font-size: 9px;">
<thead>
<tr class="header-row">
<th rowspan="2" class="text-start" style="width: 15%;">PERSONEL</th>
<th rowspan="2" style="width: 10%;">AYLIK NET MAAŞ (A)</th>
<th colspan="2" class="subheader-row" style="width: 15%;">FAZLA MESAİ (SAAT)</th>
<th rowspan="2" style="width: 10%;">MESAİ HAKEDİŞİ (B)</th>
<th rowspan="2" style="width: 10%;">İZİN/KESİNTİ (C)</th>
<th rowspan="2" style="width: 10%;">NET HAKEDİŞ (A+B-C)</th>
<th rowspan="2" style="width: 10%;">AVANS KESİNTİSİ (D)</th>
<th rowspan="2" class="header-row" style="width: 10%;">ÖDENECEK TUTAR (TL)</th>
</tr>
<tr class="subheader-row">
<th>1.5x</th>
<th>2.0x</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_saat']; ?> saat</td>
<td><?php echo $m['mesai_20_saat']; ?> saat</td>
<td style="color: green; font-weight: bold;">+<?php echo number_format($m['mesai_hakedis'], 2); ?> ₺</td>
<td style="color: red; font-weight: bold;">-<?php echo number_format($m['izin_kesinti_tutar'], 2); ?> ₺</td>
<td><?php echo number_format($m['net_hakedis'], 2); ?> ₺</td>
<td style="color: red; font-weight: bold;">-<?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($genel_toplam_net_maas, 2); ?> ₺</td>
<td colspan="2" style="background-color: #343a40; color: white;">ÖZET</td>
<td style="color: green;">+<?php echo number_format($genel_toplam_mesai_hakedis, 2); ?> ₺</td>
<td style="color: red;">-<?php echo number_format($genel_toplam_izin_kesinti, 2); ?> ₺</td>
<td><?php echo number_format($genel_toplam_net_maas + $genel_toplam_mesai_hakedis - $genel_toplam_izin_kesinti, 2); ?> ₺</td>
<td style="color: red;">-<?php echo number_format($genel_toplam_avans, 2); ?> ₺</td>
<td style="background-color: #d1ecf1; color: #000; font-size: 11px; border: 2px solid #000;"><?php echo number_format($genel_toplam_odenecek, 2); ?> ₺</td>
</tr>
</tbody>
</table>
<?php endif; ?>
<div class="imza-blogu">
<div class="imza-kutu">
<div class="imza-baslik">HAZIRLAYAN / MUHASEBE</div>
</div>
<div class="imza-kutu">
<div class="imza-baslik">YÖNETİCİ ONAYI</div>
</div>
</div>
<?php if($format == 'pdf'): ?>
<script>
// PDF/Print için otomatik tetikleme
window.onload = function() { setTimeout(function() { window.print(); }, 500); }
</script>
<?php endif; ?>
</body>
</html>