<?php
$page_title = 'Tambah Gaji Karyawan';
require_once('includes/load.php');
page_require_level(4);

// Ambil semua karyawan yang BELUM memiliki catatan gaji
// Kueri ini menggunakan LEFT JOIN untuk menemukan karyawan yang ID-nya tidak ada di tabel 'salaries'
$sql = "SELECT e.* FROM employees e LEFT JOIN salaries s ON e.id = s.employee_id WHERE s.employee_id IS NULL";
$employees = find_by_sql($sql);

if (isset($_POST['add_salary'])) {
    $req_fields = array(
        'employee_id',
        'pay_date',
        'basic_salary',
        'tunjangan_jabatan_percent',
        'adjustment_percent',
        'tunjangan_kehadiran_percent',
        'tunjangan_transport',
        'tunjangan_makan',
        'work_visit',
        'lembur',
        'bpjs_kesehatan_percent',
        'bpjs_ketenagakerjaan_percent'
    );
    validate_fields($req_fields);

    if (empty($errors)) {
        $employee_id = $db->escape($_POST['employee_id']);
        $pay_date = $db->escape($_POST['pay_date']);

        // Ambil dan bersihkan data nominal
        $basic_salary = (float)$db->escape($_POST['basic_salary']);
        $tunjangan_transport = (float)$db->escape($_POST['tunjangan_transport']);
        $tunjangan_makan = (float)$db->escape($_POST['tunjangan_makan']);
        $work_visit = (float)$db->escape($_POST['work_visit']);
        $lembur = (float)$db->escape($_POST['lembur']);

        // Ambil dan bersihkan data persentase
        $tunjangan_jabatan_percent = (float)$db->escape($_POST['tunjangan_jabatan_percent']);
        $adjustment_percent = (float)$db->escape($_POST['adjustment_percent']);
        $tunjangan_kehadiran_percent = (float)$db->escape($_POST['tunjangan_kehadiran_percent']);
        $bpjs_kesehatan_percent = (float)$db->escape($_POST['bpjs_kesehatan_percent']);
        $bpjs_ketenagakerjaan_percent = (float)$db->escape($_POST['bpjs_ketenagakerjaan_percent']);

        // Hitung nilai nominal dari persentase tunjangan berdasarkan gaji pokok
        $tunjangan_jabatan = ($basic_salary * $tunjangan_jabatan_percent) / 100;
        $adjustment = ($basic_salary * $adjustment_percent) / 100;
        $tunjangan_kehadiran = ($basic_salary * $tunjangan_kehadiran_percent) / 100;

        // Hitung gaji kotor (gross salary)
        $gross_salary = $basic_salary + $tunjangan_jabatan + $adjustment + $tunjangan_kehadiran + $tunjangan_transport + $tunjangan_makan + $work_visit + $lembur;

        // Hitung nilai nominal dari persentase potongan berdasarkan gaji kotor
        $bpjs_kesehatan = ($gross_salary * $bpjs_kesehatan_percent) / 100;
        $bpjs_ketenagakerjaan = ($gross_salary * $bpjs_ketenagakerjaan_percent) / 100;
        
        // Hitung gaji bersih (net salary)
        $net_salary = $gross_salary - $bpjs_kesehatan - $bpjs_ketenagakerjaan;

        // Cek apakah data gaji untuk karyawan dan tanggal tersebut sudah ada
        $check_sql = "SELECT * FROM salaries WHERE employee_id = '{$employee_id}' AND pay_date = '{$pay_date}' LIMIT 1";
        $result = $db->query($check_sql);
        if ($db->num_rows($result) > 0) {
            $session->msg('d', 'Data gaji untuk karyawan dan tanggal yang sama sudah ada.');
            redirect('add_salary.php', false);
        }

        // Masukkan data ke tabel 'salaries' dengan kolom-kolom baru
        $query = "INSERT INTO salaries (employee_id, pay_date, basic_salary, tunjangan_jabatan, adjustment, tunjangan_kehadiran, tunjangan_transport, tunjangan_makan, work_visit, lembur, bpjs_kesehatan, bpjs_ketenagakerjaan, net_salary, created_at, updated_at) VALUES ('{$employee_id}', '{$pay_date}', '{$basic_salary}', '{$tunjangan_jabatan}', '{$adjustment}', '{$tunjangan_kehadiran}', '{$tunjangan_transport}', '{$tunjangan_makan}', '{$work_visit}', '{$lembur}', '{$bpjs_kesehatan}', '{$bpjs_ketenagakerjaan}', '{$net_salary}', NOW(), NOW())";

        if ($db->query($query)) {
            $session->msg('s', "Gaji untuk karyawan telah berhasil ditambahkan.");
            redirect('gaji.php', false);
        } else {
            $session->msg('d', 'Maaf, penambahan gaji gagal. Mungkin ada masalah dengan database.');
            redirect('add_salary.php', false);
        }
    } else {
        $session->msg("d", $errors);
        redirect('add_salary.php', false);
    }
}
?>

<?php include_once('layouts/header.php'); ?>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <strong>
                    <span class="glyphicon glyphicon-usd"></span>
                    <span>Tambah Gaji Karyawan</span>
                </strong>
            </div>
            <div class="panel-body">
                <form method="post" action="add_salary.php">
                    <div class="form-group">
                        <label for="employee_id">Pilih Karyawan</label>
                        <select class="form-control" name="employee_id" required>
                            <option value="">Pilih Karyawan</option>
                            <?php foreach ($employees as $employee): ?>
                                <option value="<?php echo (int)$employee['id']; ?>">
                                    <?php echo remove_junk($employee['first_name'] . ' ' . $employee['last_name']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="pay_date">Tanggal Gaji</label>
                        <input type="date" class="form-control" name="pay_date" value="<?php echo date('Y-m-d'); ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="basic_salary">Gaji Pokok (Rp)</label>
                        <input type="number" class="form-control" name="basic_salary" placeholder="Masukkan gaji pokok" required>
                    </div>
                    
                    <hr>
                    <label>Tunjangan</label>
                    <div class="form-group">
                        <label for="tunjangan_jabatan_percent">Tunjangan Jabatan (%)</label>
                        <div class="input-group">
                            <input type="number" class="form-control" name="tunjangan_jabatan_percent" value="0" placeholder="Persentase dari gaji pokok">
                            <span class="input-group-addon">%</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="adjustment_percent">Adjustment (%)</label>
                        <div class="input-group">
                            <input type="number" class="form-control" name="adjustment_percent" value="0" placeholder="Persentase dari gaji pokok">
                            <span class="input-group-addon">%</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="tunjangan_kehadiran_percent">Tunjangan Kehadiran (%)</label>
                        <div class="input-group">
                            <input type="number" class="form-control" name="tunjangan_kehadiran_percent" value="0" placeholder="Persentase dari gaji pokok">
                            <span class="input-group-addon">%</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="tunjangan_transport">Tunjangan Transport (Rp)</label>
                        <input type="number" class="form-control" name="tunjangan_transport" value="0" placeholder="Masukkan nominal">
                    </div>
                    <div class="form-group">
                        <label for="tunjangan_makan">Tunjangan Makan (Rp)</label>
                        <input type="number" class="form-control" name="tunjangan_makan" value="0" placeholder="Masukkan nominal">
                    </div>
                    <div class="form-group">
                        <label for="work_visit">Work Visit (Rp)</label>
                        <input type="number" class="form-control" name="work_visit" value="0" placeholder="Masukkan nominal">
                    </div>
                    <div class="form-group">
                        <label for="lembur">Lembur (Rp)</label>
                        <input type="number" class="form-control" name="lembur" value="0" placeholder="Masukkan nominal">
                    </div>

                    <hr>
                    <label>Potongan</label>
                    <div class="form-group">
                        <label for="bpjs_kesehatan_percent">BPJS Kesehatan (%)</label>
                        <div class="input-group">
                            <input type="number" class="form-control" name="bpjs_kesehatan_percent" value="0" placeholder="Persentase dari total gaji">
                            <span class="input-group-addon">%</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="bpjs_ketenagakerjaan_percent">BPJS Ketenagakerjaan (%)</label>
                        <div class="input-group">
                            <input type="number" class="form-control" name="bpjs_ketenagakerjaan_percent" value="0" placeholder="Persentase dari total gaji">
                            <span class="input-group-addon">%</span>
                        </div>
                    </div>
                    
                    <div class="form-group clearfix">
                        <button type="submit" name="add_salary" class="btn btn-primary">Simpan Gaji</button>
                        <a href="gaji.php" class="btn btn-default pull-right">Batal</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>