<?php
     if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
                $ssql="SELECT fnid
                    FROM txattendance_hdr
                    WHERE ftdepartment_code='{$_REQUEST['ftdepartment_code']}' AND
                        ftdivision_code='{$_REQUEST['ftdivision_code']}' AND
                        ftposition_code='{$_REQUEST['ftposition_code']}' AND
                        ftshift_code='{$_REQUEST['ftshift_code']}' AND
                        fdattendance_date='".CDate($_REQUEST['fdattendance_date'])."' AND
                        fnstatus<>9
                    ";

              if(RetRstField($ssql)) {
                    ?>
                    alert('Jabatan dan Shift tersebut pada tanggal tersebut sudah di absensi.. ');
                    <?php
              }
              else if(RetRstField("SELECT fnid FROM txattendance_hdr WHERE ftattendance_no='{$_REQUEST['fttrans_no']}'")) {
                  ?>

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


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

            <?php
              }
                die("");
            break;
            case "time":
                  $date_from=CDate($_REQUEST['fdattendance_date'])." ".$_REQUEST['fdworking_enter_time'];
                  $date_end=CDate($_REQUEST['fdworking_exit_date'])." ".$_REQUEST['fdworking_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('ftworking_hoursD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $hourx ?>';
                 <?
                   die("");
            break;
            case "holiday":
                  $ssql="select fnid  from tlholiday where fdholiday_date='".CDate($_REQUEST['fdattendance_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=hrd001&thbln=' + document.getElementById(\"fdattendance_date\").value + '&field_name=ftattendance_no&no_trans=ftattendance_no&table_name=txattendance_hdr');";
            $ajax_OnChangeDate="ajax_do('absensi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=holiday&fdattendance_date='+ document.getElementById('fdattendance_date').value);";

            $strAutoNumber0=" function OnChangeDate_fdattendance_date() {
                                           {$strAutoNumber1}
                                           {$ajax_OnChangeDate}

                                     }
                                     OnChangeDate_fdattendance_date();

                                ";
            $objAxpDB->FormJS($strAutoNumber0);


     }
     // Deklarasi UctVw
    $objAxpDB->ViewSQL("select a.fnid, a.ftattendance_no, a.fdattendance_date as fdtrans_date,
                              d.ftdivision_name,
                              c.ftdepartment_name,
                              e.ftposition_name,
                              f.ftshift_name,
                             a.ftnotes,b.fttrans_status_name
                            from txattendance_hdr a
                                left join tstransaction_status b on a.fnstatus=b.fntrans_status_code and b.fntrans_group_code=2
                               left join tldivision d on a.ftdivision_code=d.ftdivision_code
                                        left join tldepartment c on a.ftdepartment_code=c.ftdepartment_code
                                left join tlposition e on a.ftposition_code=e.ftposition_code
                                left join (SELECT distinct ftshift_code, ftshift_name FROM tlshift) f on a.ftshift_code=f.ftshift_code
                                order by a.ftattendance_no desc, a.fdattendance_date desc");
    $objAxpDB->ViewCaption("ID, No. Absensi, Tgl. Absensi,Divisi, Departemen,Jabatan,Shift, Catatan,Status");
    $objAxpDB->ViewPeriod(TRUE);


    // Deklarasi Header & Footer
    $objAxpDB->FormName("Absensi");
    $objAxpDB->FormSQL("select  ftattendance_no, fdattendance_date, ftdivision_code, ftdepartment_code, ftposition_code,  ftshift_code, ftnotes, fnstatus,ftworking_enter_time,ftdesciption  from txattendance_hdr");
    $objAxpDB->FormCaption(" No. Absensi,Tgl. Transaksi,Divisi, Departement, Jabatan, Shift, Catatan, Status, Jam Masuk, Ket. Hari Libur");
    $objAxpDB->FormPosition("TL, TL,TL, TL,TL, TL, BL, BR, TL, TL");


    $ajax1="ajax_do_grid0('absensi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftdivision_code='+ document.getElementById('ftdivision_code').value+'&ftdepartment_code='+ document.getElementById('ftdepartment_code').value+'&ftposition_code='+ document.getElementById('ftposition_code').value+'&ftshift_code='+ document.getElementById('ftshift_code').value+'&fdattendance_date='+ document.getElementById('fdattendance_date').value);";
//    $objAxpDB->FormScript(2, "onChange=\"{$ajax1}\"");
//    $objAxpDB->FormScript(3, "onChange=\"{$ajax1}\"");
    if($_REQUEST['id']<=0) {
        $ajax1=" OnClick=\"{$ajax1}\"";
        $button="<input type='button' id='cmd_employee_list' name='cmd_employee_list' value='LIHAT' {$ajax1}>";
        $objAxpDB->FormLayout(5,"{$button}");
    }
    // Deklarasi Detail
    $objAxpDB->GridSQL(0, "select fnindex, ftemployee_code, ftshift_code,
                            fnemployee_status,ftabsen_code, fdworking_enter_time, fdworking_exit_date,
                            fdworking_exit_time, ftworking_hours, ftnotes
                            from txattendance_det");
    $objAxpDB->GridCaption(0, "No., Pegawai, Shift, Status Kehadiran, Keterlambatan, Jam Masuk, Tgl. Keluar, Jam Keluar,Total, Keterangan");
    $objAxpDB->GridAutoAdd(0, False);

    $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 order by ftemployee_code", "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 'SELURUH' AS ftposition_code,'SELURUH' AS ftposition_name UNION ALL (SELECT ftposition_code, ftposition_name FROM tlposition   order by ftposition_code) ", "ftposition_code", "ftposition_name", -1, False);
    $objAxpDB->Lookup("ftabsen_code", "SELECT ftabsen_code, ftabsen_name FROM tlabsen   order by ftabsen_code  ", "ftabsen_code", "ftabsen_name", -1, True);
    $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("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);

//    $ajax1="OnChange=\"ajax_do('absensi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=time&fdattendance_date='+ document.getElementById('fdattendance_date').value+'&fdworking_enter_time='+ document.getElementById('fdworking_enter_timeD0_N_').value);\"";

    $ajax1="OnChange=\"ajax_do('absensi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=time&index=_N_&fdattendance_date='+ document.getElementById('fdattendance_date').value +'&fdworking_enter_time='+ document.getElementById('fdworking_enter_timeD0_N_').value +'&fdworking_exit_date='+ document.getElementById('fdworking_exit_dateD0_N_').value +'&fdworking_exit_time='+ document.getElementById('fdworking_exit_timeD0_N_').value);\"";
//    $objAxpDB->FormScript(1,$ajax1);
    $objAxpDB->GridScript(0,5, $ajax1);
    $objAxpDB->GridScript(0,6, $ajax1);
    $objAxpDB->GridScript(0,7, $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, 0,1,1,1");
             $objAxpDB->GridLocked(0, "1, 1, 1,0, 0,0,0,0,1,0");

    } elseif ($objAxpDB->GetStatus("fnstatus") == 0) {

            $objAxpDB->FormLocked("1, 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);
            $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");
            $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");
            $objAxpDB->GridLocked(0, "1, 1,1, 1,1,1,1,1,1");
            $objAxpDB->ButtonShow("tutup");
    }

if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           case "grid":
               $_REQUEST['id']=0.5;
               ob_start();
                      if($_REQUEST['ftposition_code']!='SELURUH') {
                           $ftposition_code=" AND a.ftposition_code='{$_REQUEST['ftposition_code']}' ";
                       }
                        if($_REQUEST['ftdepartment_code']!='SELURUH') {
                           $ftdepartment_code=" AND a.ftdepartment_code='{$_REQUEST['ftdepartment_code']}' ";
                       }
                        if($_REQUEST['ftdivision_code']!='SELURUH') {
                           $ftdivision_code=" AND a.ftdivision_code='{$_REQUEST['ftdivision_code']}' ";
                       }
                        if($_REQUEST['ftshift_code']!='SELURUH') {
                           $ftshift_code=" AND a.ftshift_code='{$_REQUEST['ftshift_code']}' ";
                       }
                       $where="{$ftposition_code} {$ftdepartment_code} {$ftdivision_code} {$ftshift_code}";

                       $ssql="SELECT c.fnindex AS fnindex,a.ftemployee_code,a.ftshift_code,
                                    ifnull(c.fnemployee_status,0) AS fnemployee_status,c.ftabsen_code,
                                    b.fdshift_in AS fdworking_enter_time,
                                   CAST( CASE WHEN b.fdshift_in<=b.fdshift_out THEN
                                                 '".CDate($_REQUEST['fdattendance_date'])."' 
                                                 ELSE
                                                        DATE_ADD( '".CDate($_REQUEST['fdattendance_date'])."', INTERVAL 1 DAY) 
                                                        END AS DATE)   AS fdworking_exit_date,

                                     b.fdshift_out AS fdworking_exit_time,
                                       
                                        TIMEDIFF(CONCAT(
                                            CASE WHEN b.fdshift_in<=b.fdshift_out THEN
                                                 '".CDate($_REQUEST['fdattendance_date'])."' 
                                                 ELSE
                                                        DATE_ADD( '".CDate($_REQUEST['fdattendance_date'])."', INTERVAL 1 DAY) 
                                                        END
                                            ,' ',b.fdshift_out),CONCAT('".CDate($_REQUEST['fdattendance_date'])." ',b.fdshift_in)) as ftworking_hours,
                                    c.ftnotes
                            FROM tlemployee a INNER JOIN
                                    tlshift  b ON a.ftshift_code=b.ftshift_code
                                    LEFT JOIN txattendance_det c ON 1=0
                            WHERE  b.ftshift_day=DATE_FORMAT('".CDate($_REQUEST['fdattendance_date'])."','%W')
                                   {$where} 
                            ORDER BY a.ftemployee_name 
                                   #";
//                  die($ssql);
                        $objAxpDB->GridSQL(0,$ssql);



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

               $objAxpDB->Process();
               $objAxpDB->setGrid2(0);
               $html = ob_get_contents();
               ob_end_clean();
                $ssql2="SELECT b.fdshift_in AS fdworking_enter_time
                                FROM
                                        tlshift  b
                                        LEFT JOIN txattendance_det c ON 1=0
                                WHERE
                                        b.ftshift_code='{$_REQUEST['ftshift_code']}' AND
                                        b.ftshift_day=DATE_FORMAT('".CDate($_REQUEST['fdattendance_date'])."','%W')

                               ";
               ?>
                    document.getElementById('ftworking_enter_time').value='<?php echo RetRstField($ssql2); ?>';

                    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();

?>
