PYBS (Personel Yönetim Bilgi Sistemi) / Ramsa/modules/ikramiye-ekle.php
ikramiye-ekle.php 234 satır • 10.67 KB
<?php
// modules/ikramiye-ekle.php
session_start();
require_once '../config/db.php';
require_once '../config/functions.php';

// Yetki: Yönetici, Muhasebe, İK
yetkiKontrol(['root', 'yonetici', 'muhasebe', 'insan_kaynaklari']);

// Türkçe Tarih Çeviri Dizisi
$aylar_tr = [
    'January' => 'Ocak', 'February' => 'Şubat', 'March' => 'Mart',
    'April' => 'Nisan', 'May' => 'Mayıs', 'June' => 'Haziran',
    'July' => 'Temmuz', 'August' => 'Ağustos', 'September' => 'Eylül',
    'October' => 'Ekim', 'November' => 'Kasım', 'December' => 'Aralık'
];

// --- EXCEL ÇIKTISI ---
if (isset($_GET['format']) && $_GET['format'] == 'excel') {
    $dosya_adi = "Ikramiye_Listesi_" . date('Y-m-d') . ".xls";
    header("Content-Type: application/vnd.ms-excel; charset=utf-8");
    header("Content-Disposition: attachment; filename=$dosya_adi");
    echo "\xEF\xBB\xBF"; 
    ?>
    <table border="1">
        <thead>
            <tr style="background-color:#f2f2f2;">
                <th>Personel</th>
                <th>TC No</th>
                <th>Dönem</th>
                <th>Açıklama</th>
                <th>Tutar</th>
                <th>İşlemi Yapan</th>
                <th>Kayıt Tarihi</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $sql_excel = "SELECT i.*, k.ad, k.soyad, k.tc_no, 
                          u.ad as u_ad, u.soyad as u_soyad 
                          FROM ikramiyeler i 
                          JOIN kullanicilar k ON i.calisan_id = k.id 
                          LEFT JOIN kullanicilar u ON i.olusturan_id = u.id 
                          ORDER BY i.donem_tarihi DESC";
            $excel_rows = $pdo->query($sql_excel)->fetchAll();
            
            foreach($excel_rows as $row): 
                // Tarihi Türkçe Yap (Excel İçin)
                $tarih_eng = date('d F Y', strtotime($row['donem_tarihi']));
                $tarih_tr = strtr($tarih_eng, $aylar_tr);
            ?>
            <tr>
                <td><?php echo $row['ad'] . ' ' . $row['soyad']; ?></td>
                <td><?php echo $row['tc_no']; ?></td>
                <td><?php echo $tarih_tr; ?></td>
                <td><?php echo $row['aciklama']; ?></td>
                <td><?php echo number_format($row['miktar'], 2); ?></td>
                <td><?php echo $row['u_ad'] . ' ' . $row['u_soyad']; ?></td>
                <td><?php echo $row['olusturulma_tarihi']; ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php
    exit;
}

include '../includes/header.php';
include '../includes/menu.php';

$mesaj = '';

// EKLEME İŞLEMİ
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['ekle'])) {
    csrfKontrol($_POST['csrf_token']);
    
    $calisan_id = $_POST['calisan_id'];
    $tarih = $_POST['tarih']; 
    $miktar = (float)$_POST['miktar'];
    $aciklama = guvenlik($_POST['aciklama']);
    
    if ($miktar > 0 && !empty($tarih) && !empty($calisan_id)) {
        $sql = "INSERT INTO ikramiyeler (calisan_id, donem_tarihi, miktar, aciklama, olusturan_id) VALUES (?, ?, ?, ?, ?)";
        $stmt = $pdo->prepare($sql);
        if ($stmt->execute([$calisan_id, $tarih, $miktar, $aciklama, $_SESSION['kullanici_id']])) {
            logKaydet($pdo, $_SESSION['kullanici_id'], 'ekleme', "Personel ID: $calisan_id için $miktar TL ikramiye tanımlandı.", 'ikramiyeler', $pdo->lastInsertId());
            $mesaj = '<div class="alert alert-success">İkramiye başarıyla tanımlandı ve bordroya işlendi.</div>';
        } else {
            $mesaj = '<div class="alert alert-danger">Kayıt başarısız oldu.</div>';
        }
    } else {
        $mesaj = '<div class="alert alert-danger">Lütfen tüm alanları doldurun.</div>';
    }
}

// SİLME İŞLEMİ
if (isset($_GET['sil_id'])) {
    $sil_id = (int)$_GET['sil_id'];
    $del = $pdo->prepare("DELETE FROM ikramiyeler WHERE id = ?");
    if ($del->execute([$sil_id])) {
        logKaydet($pdo, $_SESSION['kullanici_id'], 'silme', "İkramiye silindi (ID: $sil_id)", 'ikramiyeler', $sil_id);
        $mesaj = '<div class="alert alert-warning">İkramiye kaydı silindi.</div>';
    }
}

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

// Son Eklenen İkramiyeler (Ekranda gösterilecek kısım - Limitli)
$son_ikramiyeler = $pdo->query("SELECT i.*, k.ad, k.soyad FROM ikramiyeler i JOIN kullanicilar k ON i.calisan_id = k.id ORDER BY i.id DESC LIMIT 20")->fetchAll();
?>

<style>
    @media print {
        body * { visibility: hidden; }
        #printableArea, #printableArea * { visibility: visible; }
        #printableArea { 
            position: absolute; 
            left: 0; 
            top: 0; 
            width: 100%; 
        }
        /* Formu ve butonları gizle */
        .no-print { display: none !important; }
        /* Tablo sütununu tam genişlik yap */
        .col-md-8 { width: 100% !important; flex: 0 0 100%; max-width: 100%; }
        .card { border: none !important; box-shadow: none !important; }
    }
</style>

<div class="container-fluid">
    <div class="row">
        <div class="col-md-4 no-print">
            <div class="card shadow-sm border-0 rounded-4">
                <div class="card-header bg-warning text-dark fw-bold">
                    <i class="fas fa-gift me-2"></i> İkramiye / Prim Ver
                </div>
                <div class="card-body">
                    <?php echo $mesaj; ?>
                    <form method="POST">
                        <input type="hidden" name="csrf_token" value="<?php echo csrfTokenOlustur(); ?>">
                        
                        <div class="mb-3">
                            <label class="form-label fw-bold small">Personel Seçiniz</label>
                            <select name="calisan_id" class="form-select select2" required>
                                <option value="">Seçiniz...</option>
                                <?php foreach($personeller as $p): ?>
                                    <option value="<?php echo $p['id']; ?>"><?php echo $p['ad'].' '.$p['soyad']; ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-bold small">Yansıyacak Dönem (Tarih)</label>
                            <input type="date" name="tarih" class="form-control" value="<?php echo date('Y-m-d'); ?>" required>
                            <div class="form-text">Seçilen tarihin ait olduğu ayın bordrosuna eklenir.</div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-bold small text-success">İkramiye Tutarı (TL)</label>
                            <div class="input-group">
                                <span class="input-group-text">₺</span>
                                <input type="number" step="0.01" name="miktar" class="form-control fw-bold" required placeholder="0.00">
                            </div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-bold small">Açıklama (Neden?)</label>
                            <input type="text" name="aciklama" class="form-control" placeholder="Örn: Bayram İkramiyesi, Performans Primi..." required>
                        </div>

                        <div class="d-grid">
                            <button type="submit" name="ekle" class="btn btn-warning fw-bold text-dark">
                                <i class="fas fa-save me-2"></i> İKRAMİYEYİ KAYDET
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-md-8" id="printableArea">
            <div class="card shadow-sm border-0 rounded-4">
                <div class="card-header bg-white border-bottom-0 d-flex justify-content-between align-items-center">
                    <h5 class="mb-0 text-secondary"><i class="fas fa-history me-2"></i> Son Verilen İkramiyeler</h5>
                    
                    <div class="btn-group no-print">
                        <a href="?format=excel" class="btn btn-sm btn-success fw-bold">
                            <i class="fas fa-file-excel me-1"></i> Excel
                        </a>
                        <button onclick="window.print()" class="btn btn-sm btn-dark fw-bold">
                            <i class="fas fa-print me-1"></i> PDF / Yazdır
                        </button>
                    </div>
                </div>
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>Personel</th>
                                <th>Dönem</th>
                                <th>Açıklama</th>
                                <th>Tutar</th>
                                <th class="no-print">İşlem</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($son_ikramiyeler as $ik): 
                                // Tarihi Türkçe Yap (HTML İçin)
                                $tarih_eng = date('d F Y', strtotime($ik['donem_tarihi']));
                                $tarih_tr = strtr($tarih_eng, $aylar_tr);
                            ?>
                            <tr>
                                <td class="fw-bold"><?php echo $ik['ad'].' '.$ik['soyad']; ?></td>
                                <td><?php echo $tarih_tr; ?></td>
                                <td><?php echo htmlspecialchars($ik['aciklama']); ?></td>
                                <td class="text-success fw-bold">+<?php echo number_format($ik['miktar'], 2); ?> ₺</td>
                                <td class="no-print">
                                    <a href="?sil_id=<?php echo $ik['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Bu ikramiyeyi silmek istediğinize emin misiniz?');">
                                        <i class="fas fa-trash"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                            <?php if(empty($son_ikramiyeler)) echo '<tr><td colspan="5" class="text-center text-muted">Kayıt yok.</td></tr>'; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include '../includes/footer.php'; ?>