<?php
     if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
            if(RetRstField("SELECT fnid FROM txthr_hdr WHERE ftthr_no='{$_REQUEST['fttrans_no']}'")) {
            ?>
                  alert('No. transaksi sudah dipakai...\n System akan mengenerate No. transaksi kembali ...\n Silahkan ulangi proses penyimpanan...  ');
                  OnChangeDate_fdtrans_date();
            <?php
            } else {
            ?>
                  submitbutton('Simpan','','','','');
            <?php
            }
            die("");
            break;
        }
       
    }

    if(!$_REQUEST['id']) {
            $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=hrd005&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftthr_no&no_trans=ftthr_no&table_name=txthr_hdr');";

            $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                           {$strAutoNumber1}
                              }
                              OnChangeDate_fdtrans_date();
                            ";

            $objAxpDB->FormJS($strAutoNumber0);
     }
    // Deklarasi UctVw
    $objAxpDB->ViewName("Daftar THR");
    $objAxpDB->ViewTable("vHRTHR");
    $objAxpDB->ViewSQL("select a.fnid, a.ftthr_no, a.fdtrans_date, a.ftdivision_code, a.ftgroup_pengajian_code, a.ftreligion, a.ftnotes, b.fttrans_status_name from txthr_hdr  a left join tstransaction_status b on a.fnstatus=b.fntrans_status_code and b.fntrans_group_code=2 left join tlposition c on a.ftposition_code=c.ftposition_code order by  a.ftthr_no desc, a.fdtrans_date desc");
    $objAxpDB->ViewCaption("ID, No. THR, Tgl. Transaksi,Kode Divisi,Group Penggajian, Agama, Catatan, Status");
    $objAxpDB->ViewPeriod(TRUE);

    // Deklarasi Header & Footer
    $objAxpDB->FormName("THR");
    $objAxpDB->FormSQL("select  ftthr_no, fdtrans_date, ftdepartment_code, ftdivision_code,ftgroup_pengajian_code, ftreligion,  ftnotes, fnstatus from txthr_hdr ");
    $objAxpDB->FormCaption("No. THR, Tgl. Transaksi, Department, Divisi, Group Penggajian,Agama, Catatan, Status");
    $objAxpDB->FormPosition("TL, TL,TL,TL,TL,TL,  BL, BR");
    $objAxpDB->Lookup("ftdivision_code", "select 'SELURUH' AS ftdivision_code,'SELURUH' AS ftdivision_name UNION ALL (select ftdivision_code, ftdivision_name from tldivision order by ftdivision_code) ", "ftdivision_code", "ftdivision_name", -1, False);
    $objAxpDB->Lookup("ftdepartment_code", "select 'SELURUH' AS ftdepartment_code,'SELURUH' AS ftdepartment_name UNION ALL (select ftdepartment_code, ftdepartment_name from tldepartment order by ftdepartment_code) ", "ftdepartment_code", "ftdepartment_name", -1, False);	
    $objAxpDB->Lookup("ftgroup_pengajian_code", "select 'SELURUH' AS ftgroup_pengajian_code,'SELURUH' AS ftgroup_pengajian_name UNION ALL (select ftgroup_pengajian_code, ftgroup_pengajian_name from tlgroup_pengajian order by  ftgroup_pengajian_code)", "ftgroup_pengajian_code", "ftgroup_pengajian_name", -1, False);

    if(!$_REQUEST['id']) {
            $ajax1=" OnClick=\"ajax_do('hrd.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&fdtrans_date='+ document.getElementById('fdtrans_date').value+'&ftdepartment_code='+ document.getElementById('ftdepartment_code').value +'&ftdivision_code='+ document.getElementById('ftdivision_code').value +'&ftgroup_pengajian_code='+ document.getElementById('ftgroup_pengajian_code').value+'&ftreligion='+ document.getElementById('ftreligion').value);\"";

            $objAxpDB->FormLayout(5,"<input type='button' id='cmd_process' name='cmd_process' value='Prosess' {$ajax1}>  &nbsp;&nbsp;&nbsp;*** Hanya untuk Slip Gaji Tipe 1 *** ");
    }
	
    // Deklarasi Detail
    $objAxpDB->GridSQL(0, "select  fnindex, ftemployee_code,fdstart_date, fnmonth, fcbasic_thr, fcthr, fcamount, ftdescription from txthr_det");
    $objAxpDB->GridCaption(0, "No., Pegawai,Tgl. Mulai Kontrak, Lama Kerja(Bln), Gaji Pokok, THR, Penyesuaian, Catatan");
    $objAxpDB->GridAutoAdd(0, False);

    $objAxpDB->Lookup("fnstatus", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 2", "fntrans_status_code", "fttrans_status_name", 1, True);
    $objAxpDB->Lookup("ftemployee_code", "SELECT ftemployee_code, ftemployee_name FROM tlemployee", "ftemployee_code", "ftemployee_name", -1, False);
    $objAxpDB->Lookup("ftposition_code", "SELECT ftposition_code, ftposition_name FROM tlposition", "ftposition_code", "ftposition_name", -1, False);
    $objAxpDB->Lookup("ftreligion", "select 'SELURUH' AS ftreligion_code,'SELURUH' AS ftreligion_name UNION ALL (select ftreligion_code, ftreligion_name from tlreligion order by fnid) ", "ftreligion_code", "ftreligion_name", -1, True);
  
    $objAxpDB->SetStatus("id, fnstatus");
    if ($objAxpDB->GetStatus("id") == "") {
            $objAxpDB->ButtonShow("baru, cetak_daftar,  export_excel , tutup");
    } elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->FormLocked("0, 0, 0,0, 0, 0, 1, 1, 0, 1, 1, 1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1,1,1,0,0");
            $objAxpDB->GridAllowDelete(0, TRUE);
    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {         
            $objAxpDB->ButtonShow("cetak, batal, tutup");
            $objAxpDB->FormLocked("1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1,1,1,1,1");
    } elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
            $objAxpDB->ButtonShow("tutup");
            $objAxpDB->FormLocked("1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1,1,1,1,1");
    }
    $objAxpDB->GridHidden(0,"0, 0, 0, 0,0,0,0,0");
	if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           case "grid":
               $where="";
               if($_REQUEST['ftdepartment_code']!="SELURUH") {
                   $where.=" AND e.ftdepartment_code='{$_REQUEST['ftdepartment_code']}'";
               }
               if($_REQUEST['ftdivision_code']!="SELURUH") {
                   $where.=" AND e.ftdivision_code='{$_REQUEST['ftdivision_code']}'";
               }
               if($_REQUEST['ftgroup_pengajian_code']!="SELURUH") {
                    $where.=" AND e.ftgroup_pengajian_code='{$_REQUEST['ftgroup_pengajian_code']}'";
               }
               if($_REQUEST['ftreligion']!="SELURUH") {
                    $where.=" AND e.ftreligion='{$_REQUEST['ftreligion']}'";
               }
              
               ob_start();

                       // Aturan THR : a) di bawah 1 tahun, perhitungan thr = lama kerja dibagi 12 bulan
                       //              b) Pas 12 bulan, tgl 1 - 15 masuk 1 tahun, tgl 16-30 belum setahun
                       //              c) Perhitungan dari tgl kontrak dimulai
                       // kode lama : period_diff(date_format('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'), date_format(e.fdstart_contract_date, '%Y%m')) as fnmonth,
                       
                $ssql="select d.fnindex as fnindex,e.ftemployee_code, e.fdstart_contract_date as fdstart_date,
                                CASE WHEN (DATE_FORMAT(e.fdstart_contract_date, '%d') <= 15)
                                THEN PERIOD_DIFF(DATE_FORMAT('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'), DATE_FORMAT(e.fdstart_contract_date, '%Y%m'))
                                ELSE PERIOD_DIFF(DATE_FORMAT('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'), DATE_FORMAT(e.fdstart_contract_date, '%Y%m'))-1
                                END AS fnmonth,
                                c.fcamount as fcbasic_thr,

                                case when period_diff(date_format('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'), date_format(e.fdstart_contract_date, '%Y%m'))>12 then c.fcamount
                                when period_diff(date_format('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'), date_format(e.fdstart_contract_date, '%Y%m'))=12 and (date_format(e.fdstart_contract_date, '%d') <= 15) then c.fcamount
                                when period_diff(date_format('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'), date_format(e.fdstart_contract_date, '%Y%m'))=12 and (date_format(e.fdstart_contract_date, '%d') > 15) then (11/12) * c.fcamount
                                WHEN PERIOD_DIFF(DATE_FORMAT('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'), DATE_FORMAT(e.fdstart_contract_date, '%Y%m'))<12 AND (DATE_FORMAT(e.fdstart_contract_date, '%d') <= 15)
                                    THEN ROUND((PERIOD_DIFF(DATE_FORMAT('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'),DATE_FORMAT(e.fdstart_contract_date, '%Y%m'))/12) * c.fcamount,0)
                                WHEN PERIOD_DIFF(DATE_FORMAT('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'), DATE_FORMAT(e.fdstart_contract_date, '%Y%m'))<12 AND (DATE_FORMAT(e.fdstart_contract_date, '%d') > 15)
                                    THEN ROUND(((PERIOD_DIFF(DATE_FORMAT('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'),DATE_FORMAT(e.fdstart_contract_date, '%Y%m'))-1)/12) * c.fcamount,0) END AS fcthr,

                                case when period_diff(date_format('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'), date_format(e.fdstart_contract_date, '%Y%m'))>12 then c.fcamount
                                when   period_diff(date_format('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'), date_format(e.fdstart_contract_date, '%Y%m'))=12 and (date_format(e.fdstart_contract_date, '%d') <= 15) then c.fcamount
                                when   period_diff(date_format('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'), date_format(e.fdstart_contract_date, '%Y%m'))=12 and (date_format(e.fdstart_contract_date, '%d') > 15) then (11/12) * c.fcamount
                                WHEN PERIOD_DIFF(DATE_FORMAT('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'), DATE_FORMAT(e.fdstart_contract_date, '%Y%m'))<12 AND (DATE_FORMAT(e.fdstart_contract_date, '%d') <= 15)
                                    THEN ROUND((PERIOD_DIFF(DATE_FORMAT('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'),DATE_FORMAT(e.fdstart_contract_date, '%Y%m'))/12) * c.fcamount,0)
                                WHEN PERIOD_DIFF(DATE_FORMAT('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'), DATE_FORMAT(e.fdstart_contract_date, '%Y%m'))<12 AND (DATE_FORMAT(e.fdstart_contract_date, '%d') > 15)
                                    THEN ROUND(((PERIOD_DIFF(DATE_FORMAT('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'),DATE_FORMAT(e.fdstart_contract_date, '%Y%m'))-1)/12)*c.fcamount,0) END AS fcamount,
                                d.ftdescription
                        from
                                tlemployee e
                                left join tlemployee_salary_inc c on c.ftsalary_category_code='A04' and e.ftemployee_code=c.ftemployee_code
                                left join txovertime_money_det d on 1=0
                        where   e.fnstatus <> 4
                                and (period_diff(date_format('".CDate($_REQUEST['fdtrans_date'])."', '%Y%m'), date_format(e.fdstart_contract_date, '%Y%m')) >= 4
                                AND (DATE_FORMAT(e.fdstart_contract_date, '%d') <= 15))
                                AND e.fnsalary_type=0
                                {$where}
                        group by e.ftemployee_code
                        order by e.ftemployee_name
                        #   	";
                 
               $objAxpDB->GridSQL(0,$ssql);
               // echo $ssql;

               $objAxpDB->Process();
               $objAxpDB->setGrid2(0);
               $html = ob_get_contents();
               ob_end_clean();
               ?>

                div = document.getElementById('txtHint0');
                var m='<?php echo js_esc($html); ?>';
                m= m.replace(/^\s*|\s*$/g,"");

                div.innerHTML =m;
                RowIndex0();
                reSize();
                   
               <?

               break;

           }
           die("");
           exit;
    }
	
    $objAxpDB->Process();
    $objAxpDB->WriteHtmlTable();


?>