<?php
     if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
              if(RetRstField("SELECT fnid FROM txovertime_hdr WHERE ftovertime_no='{$_REQUEST['fttrans_no']}'")) {
                  ?>

                   alert('No. transaksi sudah dipakai...\n System akan mengenerate No. transaksi kembali ...\n Silahkan ulangi proses penyimpanan...  ');
                  OnChangeDate_fdovertime_date();
            <?php
              }
              else {
                    ?>

                  submitbutton('Simpan','','','','');

            <?php
              }
               die("");
            break;
            case "time":
                  $date_from=CDate($_REQUEST['fdovertime_date'])." ".$_REQUEST['fdovertime_enter_time'];
                  $date_end=CDate($_REQUEST['fdovertime_exit_date'])." ".$_REQUEST['fdovertime_exit_time'];
                  $second=datediff('s', $date_from, $date_end, false);
                    $hour=floor((double)$second/3600);
                  if(!($hour>9)) {
                     $hour="0{$hour}";
                  }
                  $second=$second-(double)$hour*3600;
                  $minute=floor((double)$second/60);
                  if(!($minute>9)) {
                     $minute="0{$minute}";
                  }
                  $second=$second-(double)$minute*60;
                  if(!($second>9)) {
                     $second="0{$second}";
                  }
                  $hourx="{$hour}:{$minute}:{$second}";
                ?>


                  document.getElementById('ftovertime_hoursD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $hourx ?>';
                 <?
                  die("");
            break;
            case "holiday":
                  $ssql="select fnid  from tlholiday where fdholiday_date='".CDate($_REQUEST['fdovertime_date'])."'";
                  #die($ssql);
                  if(RetRstField($ssql)) {
                   ?>
                       document.getElementById('ftdesciption').value='Hari Libur';
                   <?
                  }
                  else {
                       ?>
                       document.getElementById('ftdesciption').value='Hari Biasa';
                   <?
                  }
                   die("");
            break;
            
        }
       
    }
     if(!$_REQUEST['id']) {
            $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=hrd007&thbln=' + document.getElementById(\"fdovertime_date\").value + '&field_name=ftovertime_no&no_trans=ftovertime_no&table_name=txovertime_hdr');";
             $ajax_OnChangeDate="ajax_do('absensi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=holiday&fdovertime_date='+ document.getElementById('fdovertime_date').value);";

            $strAutoNumber0=" function OnChangeDate_fdovertime_date() {
                                           {$strAutoNumber1}
                                            {$ajax_OnChangeDate}
                                           
                                     }
                                     OnChangeDate_fdovertime_date();
                                var i=document.getElementsByName('fnindexD0[]').length;
                                 document.getElementById(\"TransactionDetail0\").deleteRow(2);

                                function get_tgl() {
                                        return  \" and a.fdattendance_date='\"+ CDate(document.getElementById('fdovertime_date').value) + \"'\";
                                }

                                ";
             $mm="  '+ get_tgl() +'";
            $objAxpDB->FormJS($strAutoNumber0);

             $ajaxGrid="ajax_do_grid0('absensi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftattendance_no='+ document.getElementById('ftattendance_no').value);";
             $objAxpDB->FormScript(2, "onChange=\"{$ajaxGrid}\"");
     }
     // Deklarasi UctVw
    $objAxpDB->ViewName("Daftar Perhitungan Lembur");
    $objAxpDB->ViewSQL("select a.fnid, a.ftovertime_no, a.fdovertime_date as fdtrans_date,a.ftattendance_no, a.ftnotes,b.fttrans_status_name from txovertime_hdr a left join tstransaction_status b on a.fnstatus=b.fntrans_status_code and b.fntrans_group_code=2 order by a.ftovertime_no desc, a.fdovertime_date desc");
    $objAxpDB->ViewCaption("ID, No. Lembur, Tgl. Lembur, No. Absensi,Catatan,Status");
	$objAxpDB->ViewPeriod(TRUE);
    $objAxpDB->LookupMagicQuotes(False);

    // Deklarasi Header & Footer
    $objAxpDB->FormName("Perhitungan Lembur");
    $objAxpDB->FormSQL("select  ftovertime_no, fdovertime_date,ftattendance_no,ftposition_code,ftshift_code, ftnotes, fnstatus,ftdesciption from txovertime_hdr");
    $objAxpDB->FormCaption(" No. Lembur,Tgl. Transaksi,No. Absensi, Jabatan, Shift, Catatan, Status, Ket. Hari Libur");
    $objAxpDB->FormPosition("TL, TL, TL,HD, HD, BL, BR, TL");

    // Deklarasi Detail
    $objAxpDB->GridSQL(0, "select fnindex, ftemployee_code, ftshift_code,
                            fnemployee_status, fdovertime_enter_time, fdovertime_exit_date,
                            fdovertime_exit_time, ftovertime_hours, ftnotes
                            from txovertime_det");
    $objAxpDB->GridCaption(0, "No., Pegawai, Shift, Status, Jam Masuk Lembur, Tgl. Keluar, Jam Keluar Lembur,Total, Keterangan");
//    $objAxpDB->GridScript(0, 2, "onChange='RecalculateGrid_PO();'");

    $objAxpDB->Lookup("fnemployee_status", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 22", "fntrans_status_code", "fttrans_status_name", 0, True);
    $objAxpDB->Lookup("ftemployee_code", "SELECT ftemployee_code, ftemployee_name FROM tlemployee", "ftemployee_code", "ftemployee_name", -1, 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", 0, True);
    $objAxpDB->Lookup("ftshift_code", "SELECT distinct ftshift_code, ftshift_name FROM tlshift", "ftshift_code", "ftshift_name", 0, True);
    $objAxpDB->Lookup("ftposition_code", "SELECT ftposition_code, ftposition_name FROM tlposition", "ftposition_code", "ftposition_name", -1, False);

     if(!$_REQUEST['id']) {
        $ssql_ftattendance_no.=" SELECT a.ftattendance_no, c.ftposition_name " ;
        $ssql_ftattendance_no.=" FROM txattendance_hdr  a " ;
        $ssql_ftattendance_no.=" LEFT JOIN tlposition c ON a.ftposition_code=c.ftposition_code " ;
        $ssql_ftattendance_no.=" WHERE a.fnstatus=1 AND " ;
        $ssql_ftattendance_no.=" NOT EXISTS(SELECT b.fnid FROM txovertime_hdr b  WHERE b.fnstatus<>9 AND a.ftattendance_no=b.ftattendance_no) ";
        $ssql_ftattendance_no.=" $mm";
        
     }
     else {       
        $ssql_ftattendance_no.=" SELECT a.ftattendance_no, c.ftposition_name " ;
        $ssql_ftattendance_no.=" FROM txattendance_hdr  a " ;
        $ssql_ftattendance_no.=" LEFT JOIN tlposition c ON a.ftposition_code=c.ftposition_code " ;
      
     }
      $objAxpDB->LookupMagicQuotes(false);
    $objAxpDB->Lookup("ftattendance_no",$ssql_ftattendance_no , "ftattendance_no", "fdattendance_date", -1, False);


    $ajax1="OnChange=\"ajax_do('absensi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=time&index=_N_&fdovertime_date='+ document.getElementById('fdovertime_date').value +'&fdovertime_enter_time='+ document.getElementById('fdovertime_enter_timeD0_N_').value +'&fdovertime_exit_date='+ document.getElementById('fdovertime_exit_dateD0_N_').value +'&fdovertime_exit_time='+ document.getElementById('fdovertime_exit_timeD0_N_').value);\"";
//    $objAxpDB->FormScript(1,$ajax1);
    $objAxpDB->GridScript(0,4, $ajax1);
    $objAxpDB->GridScript(0,5, $ajax1);
    $objAxpDB->GridScript(0,6, $ajax1);

    $objAxpDB->SetStatus("id, fnstatus");
    if ($objAxpDB->GetStatus("id") == "") {
            $objAxpDB->ButtonShow("baru,cetak_daftar,  tutup");
    } elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->FormLocked("0,0,0,0,0,0,1,1 ");
           $objAxpDB->GridLocked(0, "1, 0, 0, 0,0,0,0,1,0");
            $objAxpDB->GridAllowAddNew(0, TRUE);
            $objAxpDB->GridAllowDelete(0, TRUE);
    } elseif ($objAxpDB->GetStatus("fnstatus") == 0) {
           
            $objAxpDB->FormLocked("1, 1, 1,1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 0,0,0,0,1,0");
        
            $objAxpDB->ButtonShow("cetak,update, final, batal, tutup");

     } 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,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,1");
    }

    if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           case "grid":
               ob_start();
                       $_REQUEST['id']=0.5;
                       $ssql="SELECT
                                b.fnindex, b.ftemployee_code, b.ftshift_code,
                                b.fnemployee_status, b.fdworking_exit_time AS fdovertime_enter_time, b.fdworking_exit_date AS fdovertime_exit_date,
                                b.fdworking_exit_time AS fdovertime_exit_time,
                                TIMEDIFF(
                                        CONCAT(b.fdworking_exit_date,' ',b.fdworking_exit_time),
                                        CONCAT(b.fdworking_exit_date,' ',b.fdworking_exit_time)
                                        ) AS  ftovertime_hours
                                FROM txattendance_hdr a, txattendance_det b
                                WHERE a.ftattendance_no=b.ftattendance_no AND 
                                        a.ftattendance_no='".$_REQUEST['ftattendance_no']."'
                                ORDER BY  b.fnindex
                                   #";
                        $objAxpDB->GridSQL(0,$ssql);
                        $objAxpDB->GridLocked(0, "1, 1, 1, 1,0,0,0,1,0");
                        $objAxpDB->GridAllowAddNew(0, false);
                        $objAxpDB->GridAllowDelete(0, false);

                       $objAxpDB->GridCaption(0, "No., Pegawai, Shift, Status, Jam Masuk Lembur, Tgl. Keluar, Jam Keluar Lembur,Total, Keterangan");


               $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;
                     TransactionDetailRowID0 =document.getElementsByName('fnindexD0[]').length;  
                    RowIndex0();
                    reSize();

               <?

               break;

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



?>
