<?php

    $fnsalary_type="3";
     if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            //generate
            case "grid":
              $ssqlm="SELECT MAX(a.fdend_date) AS fdend_date
                        FROM txpayroll_slip_hdr a
                        WHERE a.ftpayroll_type_code='{$_REQUEST['ftpayroll_type_code']}'
                        ";

              if(RetRstField("SELECT fnid FROM txpayroll_slip_hdr WHERE ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."'")&&($_REQUEST['x']!='1')) {
                  ?>
                   alert('No. transaksi sudah dipakai...\n System akan mengenerate No. transaksi kembali ...\n Silahkan ulangi proses penyimpanan...  ');
                  OnChangeDate_fdattendance_date();
            <?php
                die("");
              }


               ?>
               document.getElementById('txtHint0').innerHTML = '<img src=images/loading.gif>';
             <?php

            break;
            //save per employee
           case "save_employee":
                $ssql="UPDATE txpayroll_slip_emp
                            SET
                            fcincome_amount='{$_REQUEST['fcincome_amount']}',
                            fcdeduction_amount='{$_REQUEST['fcdeduction_amount']}',
                            fcamount='{$_REQUEST['fcamount']}',
                            fnattendance_days='{$_REQUEST['fnattendance_days']}',
                            fnleave_days='{$_REQUEST['fnleave_days']}'
                        WHERE ftemployee_code='{$_REQUEST['ftemployee_code']}' AND
                            ftpayroll_slip_no='{$_REQUEST['ftpayroll_slip_no']}'
                        ";
                $strSQL["Insert txpayroll_slip_emp"][]=$ssql;
                $arr1=explode("*",base64_decode($_REQUEST['strgrid1']));
                for($j=0; $j<count($arr1); $j++) {
                    if($arr1[$j]!="") {
                        $arr2=explode("|",$arr1[$j]);
                        $ssql="UPDATE txpayroll_slip_inc_plus
                                SET
                                    fnindex='{$arr2[0]}',fcamount='{$arr2[2]}'
                               WHERE  ftemployee_code='{$_REQUEST['ftemployee_code']}' AND
                                        ftpayroll_slip_no='{$_REQUEST['ftpayroll_slip_no']}' AND
                                       ftsalary_code='{$arr2[1]}'";
                         $strSQL["Insert txpayroll_slip_inc_plus"][]=$ssql;
                    }
                }

                 $arr1=explode("*",base64_decode($_REQUEST['strgrid2']));
                for($j=0; $j<count($arr1); $j++) {
                    if($arr1[$j]!="") {
                        $arr2=explode("|",$arr1[$j]);
                        $ssql="UPDATE txpayroll_slip_dec
                                SET
                                    fnindex='{$arr2[0]}',fcamount='{$arr2[2]}'
                               WHERE  ftemployee_code='{$_REQUEST['ftemployee_code']}' AND
                                        ftpayroll_slip_no='{$_REQUEST['ftpayroll_slip_no']}' AND
                                       ftsalary_code='{$arr2[1]}'";
                       $strSQL["Insert txpayroll_slip_dec"][]=$ssql;
                    }
                }

                $ssql="UPDATE
                        txpayroll_slip_hdr ,(SELECT ftpayroll_slip_no,
                                                SUM(fcincome_amount) AS  fcincome_amount,
                                                SUM(fcdeduction_amount) AS fcdeduction_amount,
                                                SUM(fcamount) AS fcamount
                                                FROM txpayroll_slip_emp
                                                WHERE ftpayroll_slip_no='{$_REQUEST['ftpayroll_slip_no']}'
                                                GROUP BY ftpayroll_slip_no
                                                ) txpayroll_slip_emp
                        SET txpayroll_slip_hdr.fcgrand_income_amount=txpayroll_slip_emp.fcincome_amount,
                                txpayroll_slip_hdr.fcgrand_deduction_amount=txpayroll_slip_emp.fcdeduction_amount,
                                        txpayroll_slip_hdr.fcgrand_total=txpayroll_slip_emp.fcamount
                        WHERE txpayroll_slip_hdr.ftpayroll_slip_no=txpayroll_slip_emp.ftpayroll_slip_no AND
                                   txpayroll_slip_hdr.ftpayroll_slip_no='{$_REQUEST['ftpayroll_slip_no']}'
                                                ";
               $strSQL["Update txpayroll_slip_hdr"][]=$ssql;

               Query_SqlArr($strSQL);

               $ssql="SELECT fcgrand_income_amount,
                            fcgrand_deduction_amount,
                               fcgrand_total
                        FROM txpayroll_slip_hdr
                        WHERE ftpayroll_slip_no='{$_REQUEST['ftpayroll_slip_no']}'  ";
               $rs=mysql_query($ssql);
               $r=mysql_fetch_assoc($rs);

               ?>
                document.getElementById('fcgrand_income_amount').value='<?php echo $r['fcgrand_income_amount']; ?>';
                 document.getElementById('fcgrand_deduction_amount').value='<?php echo $r['fcgrand_deduction_amount']; ?>';
                  document.getElementById('fcgrand_total').value='<?php echo $r['fcgrand_total']; ?>';
                  <?php


            die("");

           break;
           //del employee
            case "delete_employee":
                $ssql="DELETE FROM txpayroll_slip_emp
                        WHERE ftemployee_code='{$_REQUEST['ftemployee_code']}' AND
                            ftpayroll_slip_no='{$_REQUEST['ftpayroll_slip_no']}'
                        ";
                $strSQL["Delete txpayroll_slip_emp"][]=$ssql;

                 $ssql="DELETE FROM txpayroll_slip_inc_plus
                        WHERE ftemployee_code='{$_REQUEST['ftemployee_code']}' AND
                            ftpayroll_slip_no='{$_REQUEST['ftpayroll_slip_no']}'
                        ";

                $strSQL["Delete txpayroll_slip_inc_plus"][]=$ssql;

                 $ssql="DELETE FROM txpayroll_slip_dec
                        WHERE ftemployee_code='{$_REQUEST['ftemployee_code']}' AND
                            ftpayroll_slip_no='{$_REQUEST['ftpayroll_slip_no']}'
                        ";
                $strSQL["Delete txpayroll_slip_dec"][]=$ssql;
                #pr($strSQL);
                #die("ss");
                 Query_SqlArr($strSQL);

                  $ssql="SELECT fcgrand_income_amount,
                            fcgrand_deduction_amount,
                               fcgrand_total
                        FROM txpayroll_slip_hdr
                        WHERE ftpayroll_slip_no='{$_REQUEST['ftpayroll_slip_no']}'  ";
               $rs=mysql_query($ssql);
               $r=mysql_fetch_assoc($rs);
               ?>
                document.getElementById('fcgrand_income_amount').value='<?php echo $r['fcgrand_income_amount']; ?>';
                 document.getElementById('fcgrand_deduction_amount').value='<?php echo $r['fcgrand_deduction_amount']; ?>';
                  document.getElementById('fcgrand_total').value='<?php echo $r['fcgrand_total']; ?>';

                  var index_delete=0;
                var obj=document.getElementById("fcamountD0<?php echo $_REQUEST['index']; ?>").parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;

                index_delete=obj.rowIndex;
                document.getElementById("TransactionDetail0").deleteRow(index_delete);


               for (var ix=3; ix <  document.getElementById("TransactionDetail1").rows.length; i++) {
                     document.getElementById("TransactionDetail1").deleteRow(ix);

                }

                for ( ix=3; ix <  document.getElementById("TransactionDetail2").rows.length; i++) {
                     document.getElementById("TransactionDetail2").deleteRow(ix);

                }
                 RowIndex0();
                 reSize();
               <?php
            die("");
            break;
        }

    }

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

            $strAutoNumber0="

                                function ValInclude() {
                                  if(document.getElementById('chk_include').checked == true) {
                                             document.getElementById('hd_include').value='1';
                                    }
                                    else {
                                            document.getElementById('hd_include').value='0';
                                    }
                             }

                                function OnChangeDate_fdtrans_date() {
                                           {$strAutoNumber1}


                                     }
                                     OnChangeDate_fdtrans_date();
                                  var i=document.getElementsByName('fnindexD0[]').length;
                                        document.getElementById(\"TransactionDetail0\").deleteRow(3);
                                      
                                ";
     }
     else {
                           $strAutoNumber0="// var i=document.getElementsByName('fnindexD0[]').length;
                                        //document.getElementById(\"TransactionDetail0\").deleteRow(3);
                                       
                                        RowIndex0();";


     }

        $objAxpDB->FormJS($strAutoNumber0);
  // Deklarasi UctVw
	$objAxpDB->ViewName("Daftar Perhitungan Tunjangan Tambahan");
        $objAxpDB->ViewTable("vHRPerhitunganTunjanganTambahan");
	$objAxpDB->ViewSQL("SELECT a.fnid, a.ftpayroll_slip_no, a.fdtrans_date,
                                   ifnull(d.ftdivision_name,'SELURUH') AS ftdivision_name,IFNULL(e.ftdepartment_name,'SELURUH') AS ftdepartment_name, IFNULL(f.ftposition_name,'SELURUH') AS ftposition_name,
                                        c.fttrans_status_name AS ftpayroll_type_code,  a.fdstart_date, a.fdend_date,                                        
                                        a.ftnotes, b.fttrans_status_name AS fnstatus
                            FROM txpayroll_slip_hdr a
                                    LEFT JOIN tstransaction_status b ON b.fntrans_status_code=a.fnstatus AND b.fntrans_group_code = 2
                                    LEFT JOIN tstransaction_status c ON a.ftpayroll_type_code=c.fntrans_status_code  AND c.fntrans_group_code = 25
                                     left join tldivision d on a.ftdivision_code=d.ftdivision_code
                               left join tldepartment e on a.ftdepartment_code=e.ftdepartment_code
                                left join tlposition f on a.ftposition_code=f.ftposition_code
                            WHERE a.fnsalary_type={$fnsalary_type} 
                            order by a.ftpayroll_slip_no desc, a.fdtrans_date desc");

	$objAxpDB->ViewCaption("ID, No. Slip Gaji,
                                 Tgl. Transaksi,Divisi, Departement, Jabatan,
                                Type Pengajian, Periode Absensi 1, Periode Absensi 2,
                                Catatan, Status");
	$objAxpDB->ViewPeriod(TRUE);

	// Deklarasi Header & Footer
	$objAxpDB->FormName("Perhitungan Tunjangan Tambahan");
	$objAxpDB->FormSQL("SELECT ftpayroll_slip_no, fdtrans_date,
                            ftdivision_code, ftdepartment_code, ftposition_code,
                            ftpayroll_type_code, fdstart_date, fdend_date,fdstart_date_overtime, fdend_date_overtime,fdjht_date,
                            ftnotes, fcgrand_income_amount, fcgrand_deduction_amount,fcgrand_total, fnstatus FROM txpayroll_slip_hdr");
	$objAxpDB->FormCaption("No. Slip Gaji, Tgl. Transaksi,Divisi, Departement, Jabatan, Type Gaji, Periode Absensi 1, Periode Absensi 2, Periode Lembur 1, Periode Lembur 2, Periode JHT,  Catatan, Grand Total Pendapatan, Grand Total Potongan, Total, Status");
	$objAxpDB->FormPosition("TL, TL,TR, TR,TR,TR, TR,TR, HD,HD,HD, BL, HD, HD, BR, BR");
    
         $objAxpDB->SetStatus("id,fnstatus");
	if(!$_REQUEST['id']) {
		$ajax1=" OnClick=\"if(ValDaftarPegawai()==false) { return false; } ajax_do_grid0('hrd.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 +'&ftpayroll_type_code='+ document.getElementById('ftpayroll_type_code').value +'&fdstart_date='+ document.getElementById('fdstart_date').value +'&fdend_date='+ document.getElementById('fdend_date').value+'&fdstart_date_overtime='+ document.getElementById('fdstart_date_overtime').value +'&fdend_date_overtime='+ document.getElementById('fdend_date_overtime').value+'&fdjht_date='+ document.getElementById('fdjht_date').value+'&hd_include='+ document.getElementById('hd_include').value+ValStrSlipGajiUrlHdr());\"";
                $button="<input type='button' id='cmd_employee_list' name='cmd_employee_list' value='Daftar Pegawai' {$ajax1}>";
                $check="&nbsp;<input type='hidden' id='hd_include' name='hd_include' value='0'>";
                
//                $check.="<input type='checkbox'  id='chk_include' name='chk_include' onclick='ValInclude();'> Plus JHT";
	}
        else {
             
             $ssql="SELECT fnstatus
                        FROM txpayroll_slip_hdr
                        WHERE fnid='{$_REQUEST['id']}'";
            if(RetRstField($ssql)==0) {
		$ajax1=" OnClick=\"if(ValDaftarPegawai()==false) { return false; } ajax_do_grid0('hrd.php?x=1&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 +'&ftpayroll_type_code='+ document.getElementById('ftpayroll_type_code').value +'&fdstart_date='+ document.getElementById('fdstart_date').value +'&fdend_date='+ document.getElementById('fdend_date').value+'&fdstart_date_overtime='+ document.getElementById('fdstart_date_overtime').value +'&fdend_date_overtime='+ document.getElementById('fdend_date_overtime').value+'&fdjht_date='+ document.getElementById('fdjht_date').value+'&hd_include='+ document.getElementById('hd_include').value+ValStrSlipGajiUrlHdr());\"";
                $button="<input type='button' id='cmd_employee_list' name='cmd_employee_list' value='Daftar Pegawai' {$ajax1}>";
                $check="&nbsp;<input type='hidden' id='hd_include' name='hd_include' value='0'>"; 
            }
        }
        
     $objAxpDB->FormLayout(9,$check);
      $objAxpDB->FormLayout(7,"<input type='hidden' id='fnindex_grid1' name='fnindex_grid1'> <input type='hidden' id='fnindex_grid0' name='fnindex_grid0'> {$button}");


	// Deklarasi Detail
    $objAxpDB->GridName(0, "Daftar Pegawai");
    $objAxpDB->GridSQL(0, "select fnindex, ftemployee_code, fcamount, fnleave_days,fnalfaizin_days from txpayroll_slip_emp ");
    $objAxpDB->GridCaption(0, "No., Pegawai, Total, Cuti (hr) , Sakit/Srt.Dr ");
    $ajax1="&ftpayroll_slip_no='+ document.getElementById('ftpayroll_slip_no').value +'&ftemployee_code='+ document.getElementById('ftemployee_codeD0_N_').value";
    $ajax1="document.getElementById('fnindex_grid0').value='_N_';open('?form=slip_gaji4_x&mode=edit&id=0{$ajax1},'mywindow','width=900,height=800,resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no,screenX=100,screenY=2')";
    $ajax1=" OnClick=\"{$ajax1}\"";

    $objAxpDB->GridLayout(0,1,"<table colspacing=0 colpadding=0>
                    <tr>
                    <td>
                    <input type='button' id='cmd_grid_list' name='cmd_grid_list' value='DETAIL' {$ajax1}>
                    </td>
                     {$simpan}
                     {$hapus}
                    </tr>
                    </table>
                    ");

	$objAxpDB->GridAllowDelete(0,true);
	$objAxpDB->GridName(1, "Daftar Pendapatan");
        if($_REQUEST['id']) {
            $m=" where 1=0 #";
        }


	$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("ftpayroll_type_code", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 25", "fntrans_status_code", "fttrans_status_name", 3, 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("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);


	$objAxpDB->GridScroll(0,true,"200");

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,0,  0,0, 0, 0, 0, 1, 1, 1, 1");
		$objAxpDB->GridLocked(0, "1, 1, 1, 1,1,1,1,1");
		
                 $objAxpDB->GridAllowDelete(0,false);

	}
	 elseif ($objAxpDB->GetStatus("fnstatus") == 0) {
		$objAxpDB->ButtonShow("cetak, cetak_slip_crosstab,final,batal, tutup");
		$objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1, 0, 1, 1, 1, 1");
		$objAxpDB->GridLocked(0, "1, 1, 1, 1,1,1,1,1");
		
                $objAxpDB->GridAllowDelete(0,false);

	} elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
                $objAxpDB->ButtonShow("cetak,cetak_slip_crosstab, batal, tutup");
		$objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1");
		$objAxpDB->GridLocked(0, "1, 1, 1, 1,1,1,1,1");
		
		$objAxpDB->GridAllowAddNew(0,false);
                $objAxpDB->GridAllowDelete(0,false);
		$objAxpDB->GridAllowAddNew(1,false);
                $objAxpDB->GridAllowDelete(1,false);
		$objAxpDB->GridAllowAddNew(2,false);
                $objAxpDB->GridAllowDelete(2,false);

	} elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
		$objAxpDB->ButtonShow("tutup");
		$objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1");
		$objAxpDB->GridLocked(0, "1, 1, 1, 1,1,1,1,1");
	
		$objAxpDB->GridAllowAddNew(0,false);
        $objAxpDB->GridAllowDelete(0,false);
		
	}
//     $objAxpDB->GridHidden(0,"0, 0, 1, 1, 0, 1,,0");
   

	if(($_REQUEST['fnstatus']=='1')||($_REQUEST['fnstatus']=='9')) {
		
	}

	if($_REQUEST['ajax']) {
           switch($_REQUEST['code']) {
           case "grid":
                    if($_REQUEST['ftdivision_code']!="SELURUH") {
                        $ftdivision_code=" AND a1.ftdivision_code='{$_REQUEST['ftdivision_code']}'";
                    }
                    if($_REQUEST['ftdepartment_code']!="SELURUH") {
                        $ftdepartment_code=" AND a1.ftdepartment_code='{$_REQUEST['ftdepartment_code']}'";
                    }
                    if($_REQUEST['ftposition_code']!="SELURUH") {
                         $ftposition_code=" AND a1.ftposition_code='{$_REQUEST['ftposition_code']}'";
                    }


                    $ftpayroll_type_codex=" WHERE  b.fnstatus<>4  AND  a.fnsalary_period='{$_REQUEST['ftpayroll_type_code']}' {$ftdivision_code} {$ftdepartment_code} {$ftposition_code}";
                    
                    $ssql_1=ListEmployeeTunjanganTambahan($ftpayroll_type_codex,$include_jhtx);
                    $ftpayroll_type_codex2=" WHERE ifnull(a.fcamount,0)>0 and b.fnstatus<>4  AND  a.fnsalary_period='{$_REQUEST['ftpayroll_type_code']}' {$ftdivision_code} {$ftdepartment_code} {$ftposition_code}";
                    ?>

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

                   <?
//                   die("");
                    $ssql_2=ListIncTunjanganTambahan($ftpayroll_type_codex2,$include_jhtx);
                      
//                    $ssql_2plus=ListIncPlus($ftpayroll_type_codex,$include_jhtx);
//                    $ssql_3=ListDec($ftpayroll_type_codex,$include_jhtx);

                    ob_start();
                    mysql_query("BEGIN");
                    $ssql="UPDATE tlemployee_salary_inc_plus,tlsalary
                            SET tlemployee_salary_inc_plus.ftsalary_category_code=tlsalary.ftsalary_category_code
                            WHERE tlemployee_salary_inc_plus.ftsalary_code=tlsalary.ftsalary_code";

                    $strSQL["Insert txpayroll_slip_hdr"][]=$ssql;
                    mysql_query($ssql) or die($ssql);

//                    $ssql="UPDATE tlemployee_salary_dec,tlsalary
//                        SET tlemployee_salary_dec.ftsalary_category_code=tlsalary.ftsalary_category_code
//                        WHERE tlemployee_salary_dec.ftsalary_code=tlsalary.ftsalary_code";
//                     $strSQL["Insert txpayroll_slip_hdr"][]=$ssql;
//                    mysql_query($ssql);
                     $ssql="SELECT ftpayroll_slip_no
                            FROM txpayroll_slip_hdr
                            WHERE ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."'";

                    if(RetRstField($ssql)=="") {
                            $ssql="INSERT INTO  txpayroll_slip_hdr
                                            (
                                                    ftpayroll_slip_no,fdtrans_date,fdstart_date,
                                                    fdend_date,ftpayroll_type,ftpayroll_type_code,
                                                    ftnotes,fnstatus,fcgrand_income_amount,
                                                    fcgrand_deduction_amount,fcgrand_total,
                                                    ftcreated_user,fdcreated_date,
                                                    ftmodified_user,fdmodified_date,
                                                    ftdepartment_code,
                                                    ftdivision_code,
                                                    ftposition_code,
                                                    fdstart_date_overtime,
                                                    fdend_date_overtime,
                                                    fdjht_date,
                                                    fnsalary_type
                                            )
                                            SELECT
                                                    '".base64_decode($_REQUEST['ftpayroll_slip_nox'])."' AS ftpayroll_slip_no, '".CDate(base64_decode($_REQUEST['fdtrans_datex']))."' AS fdtrans_date, '".CDate(base64_decode($_REQUEST['fdstart_datex']))."' AS fdstart_date,
                                                    '".CDate(base64_decode($_REQUEST['fdend_datex']))."' AS fdend_date,'".base64_decode($_REQUEST['ftpayroll_typex'])."'  AS ftpayroll_type, '".base64_decode($_REQUEST['ftpayroll_type_codex'])."' AS ftpayroll_type_code,
                                                    '".($_REQUEST['ftnotesx'])."' AS ftnotes, '".base64_decode($_REQUEST['fnstatusx'])."' AS fnstatus,'".CCur(base64_decode($_REQUEST['fcgrand_income_amountx']))."'  AS fcgrand_income_amount,
                                                    '".CCur(base64_decode($_REQUEST['fcgrand_deduction_amountx']))."' AS fcgrand_deduction_amount,'".CCur(base64_decode($_REQUEST['fcgrand_totalx']))."' as fcgrand_total,
                                                    '{$_SESSION['username']}' AS ftcreated_user,now() AS  fdcreated_date,
                                                    '{$_SESSION['username']}' AS ftmodified_user,now() as fdmodified_date,
                                                     '{$_REQUEST['ftdepartment_code']}',
                                                     '{$_REQUEST['ftdivision_code']}',
                                                     '{$_REQUEST['ftposition_code']}',
                                                     '".CDate($_REQUEST['fdstart_date_overtime'])."' AS fdstart_date_overtime,
                                                     '".CDate($_REQUEST['fdend_date_overtime'])."'  AS fdend_date_overtime,
                                                     '".CDate($_REQUEST['fdjht_date'])."'  AS fdjht_date,
                                                     {$fnsalary_type} as fnsalary_type
                                                        ";

                            $strSQL["Insert txpayroll_slip_hdr"][]=$ssql;
                            mysql_query($ssql)  or die($ssql);
                    }
                    else {
                                $ssql="DELETE FROM txpayroll_slip_emp WHERE ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."' ";
                                $strSQL["Insert txpayroll_slip_emp"][]=$ssql;
                                mysql_query($ssql) or die($ssql);

                                $ssql="DELETE FROM txpayroll_slip_inc_plus WHERE ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."' ";
                                $strSQL["Insert txpayroll_slip_inc_plus"][]=$ssql;
                                mysql_query($ssql) or die($ssql); 

                    } 

                     $ssql="INSERT INTO  txpayroll_slip_emp
                                    (
                                            fnindex,ftemployee_code,
                                            fcincome_amount,fcdeduction_amount,fcamount,
                                            fnleave_days, fnalfaizin_days,
                                            ftpayroll_slip_no,
                                            fnstatus_tunda
                                    )
                                    SELECT fnindex,ftemployee_code,
                                            fcincome_amount-fcdeduction_amount_1 as fcincome_amount,
                                            fcdeduction_amount,fcamount-fcdeduction_amount_1 as fcamount,
                                            fnleave_days,fnalfaizin_days,
                                            '".base64_decode($_REQUEST['ftpayroll_slip_nox'])."' AS ftpayroll_slip_no,
                                            0 as fnstatus_tunda  
                                    FROM ($ssql_1) XX";

                    $strSQL["Insert txpayroll_slip_hdr"][]=$ssql;
                     mysql_query($ssql)  or die($ssql);

                     $ssql="INSERT INTO  txpayroll_slip_inc_plus
                                    (
                                            fnindex, ftsalary_code,fcamount,ftemployee_code,ftpayroll_slip_no
                                    )
                                    SELECT fnindex, ftsalary_code,fcamount,ftemployee_code,
                                            '".base64_decode($_REQUEST['ftpayroll_slip_nox'])."' AS ftpayroll_slip_no
                                    FROM ($ssql_2) XX";

                      $strSQL["Insert txpayroll_slip_inc_plus"][]=$ssql;
                      mysql_query($ssql)  or die($ssql);

//                       $ssql="INSERT INTO  txpayroll_slip_inc_plus_plus
//                                    (
//                                            fnindex, ftsalary_code,fcamount,ftemployee_code,ftpayroll_slip_no
//                                    )
//                                    SELECT fnindex, ftsalary_code,fcamount,ftemployee_code,
//                                            '".base64_decode($_REQUEST['ftpayroll_slip_nox'])."' AS ftpayroll_slip_no
//                                    FROM ($ssql_2plus) XX";
//
//                      $strSQL["Insert txpayroll_slip_inc_plus_plus"][]=$ssql;
//                      mysql_query($ssql);


//                     $ssql="INSERT INTO  txpayroll_slip_dec
//                                    (
//                                            fnindex, ftsalary_code,fcamount,ftemployee_code,ftpayroll_slip_no
//                                    )
//                                    SELECT fnindex, ftsalary_code,fcamount,ftemployee_code,
//                                            '".base64_decode($_REQUEST['ftpayroll_slip_nox'])."' AS ftpayroll_slip_no
//                                    FROM ($ssql_3) XX";
//
//                    $strSQL["Insert txpayroll_slip_dec"][]=$ssql;
//                     mysql_query($ssql);

                     $ssql="UPDATE
                                txpayroll_slip_emp a,
                                (SELECT c.fnid,(SELECT IFNULL(COUNT(d.fnid),0)+1
                                                        FROM txpayroll_slip_emp d
                                                        WHERE d.fnid<c.fnid AND
                                                                d.ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."'
                                                )
                                        AS m
                                        FROM  txpayroll_slip_inc_plus c
                                        WHERE c.ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."'

                                        ) b

                                SET a.fnindex=b.m
                                WHERE a.ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."'  AND
                                         a.fnid=b.fnid";
                          $strSQL["Update txpayroll_slip_emp"][]=$ssql;
                          mysql_query($ssql)  or die($ssql);

                     $ssql="select ftpayroll_slip_no,ftemployee_code
                            FROM txpayroll_slip_emp
                            WHERE  ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."'";
                    # die($ssql);
                     $rs=mysql_query($ssql)  or die($ssql);
                     while($r=mysql_fetch_assoc($rs)) {
                         $ssql="UPDATE
                                txpayroll_slip_inc_plus a,
                                (SELECT c.fnid,(SELECT IFNULL(COUNT(d.fnid),0)+1
                                                        FROM txpayroll_slip_inc_plus d
                                                        WHERE d.fnid<c.fnid AND
                                                                d.ftpayroll_slip_no='{$r['ftpayroll_slip_no']}' AND
                                                                d.ftemployee_code='{$r['ftemployee_code']}'
                                                )
                                        AS m
                                        FROM  txpayroll_slip_inc_plus c
                                        WHERE c.ftpayroll_slip_no='{$r['ftpayroll_slip_no']}'  AND
                                                c.ftemployee_code='{$r['ftemployee_code']}'
                                        ) b

                                SET a.fnindex=b.m
                                WHERE a.ftpayroll_slip_no='{$r['ftpayroll_slip_no']}'  AND
                                        a.ftemployee_code='{$r['ftemployee_code']}' AND
                                         a.fnid=b.fnid";
                          $strSQL["Update index txpayroll_slip_inc_plus"][]=$ssql;
                          #die($ssql);
                          mysql_query($ssql)  or die($ssql);
//                         $ssql="UPDATE
//                                txpayroll_slip_dec a,
//                                (SELECT c.fnid,(SELECT IFNULL(COUNT(d.fnid),0)+1
//                                                        FROM txpayroll_slip_dec d
//                                                        WHERE d.fnid<c.fnid AND
//                                                                d.ftpayroll_slip_no='{$r['ftpayroll_slip_no']}' AND
//                                                                d.ftemployee_code='{$r['ftemployee_code']}'
//                                                )
//                                        AS m
//                                        FROM  txpayroll_slip_dec c
//                                        WHERE c.ftpayroll_slip_no='{$r['ftpayroll_slip_no']}'  AND
//                                                c.ftemployee_code='{$r['ftemployee_code']}'
//                                        ) b
//
//                                SET a.fnindex=b.m
//                                WHERE a.ftpayroll_slip_no='{$r['ftpayroll_slip_no']}'  AND
//                                        a.ftemployee_code='{$r['ftemployee_code']}' AND
//                                         a.fnid=b.fnid";
//                              $strSQL["Update index txpayroll_slip_dec"][]=$ssql;
//                              mysql_query($ssql);
                     }


                     $ssql="UPDATE
                        txpayroll_slip_hdr ,(SELECT ftpayroll_slip_no,
                                                SUM(fcincome_amount) AS  fcincome_amount,
                                                SUM(fcdeduction_amount) AS fcdeduction_amount,
                                                SUM(fcamount) AS fcamount
                                                FROM txpayroll_slip_emp
                                                WHERE ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."'
                                                GROUP BY ftpayroll_slip_no
                                                ) txpayroll_slip_emp
                        SET txpayroll_slip_hdr.fcgrand_income_amount=txpayroll_slip_emp.fcincome_amount,
                                txpayroll_slip_hdr.fcgrand_deduction_amount=txpayroll_slip_emp.fcdeduction_amount,
                                        txpayroll_slip_hdr.fcgrand_total=txpayroll_slip_emp.fcamount
                        WHERE txpayroll_slip_hdr.ftpayroll_slip_no=txpayroll_slip_emp.ftpayroll_slip_no AND
                                   txpayroll_slip_hdr.ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."'
                                                ";

                           mysql_query($ssql)  or die($ssql);

                           if($_REQUEST['hd_include']==1) {
                                   $ssql="SELECT distinct b.ftemployee_code
                                        FROM txpayroll_slip_hdr a,
                                            txpayroll_slip_dec b,
                                            tlemployee_salary_dec c
                                        WHERE a.ftpayroll_slip_no=b.ftpayroll_slip_no AND
                                            b.ftemployee_code=c.ftemployee_code and
                                            b.ftsalary_code=c.ftsalary_code and
                                            c.ftsalary_category_code='B01' and
                                            a.fnstatus<>9  and
                                            month(a.fdjht_date)=month('".CDate($_REQUEST['fdjht_date'])."') and
                                            year(a.fdjht_date)=year('".CDate($_REQUEST['fdjht_date'])."') and
                                            a.ftpayroll_slip_no<>'".base64_decode($_REQUEST['ftpayroll_slip_nox'])."'";
                                    $rs=mysql_query($ssql);
                                    #echo $ssql;
                                    while($r=mysql_fetch_assoc($rs)) {
                                        $ssql_1="select ifnull(max(fcamount),0) as fcamount
                                                from txpayroll_slip_dec
                                                where ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."' AND
                                                    ftemployee_code='{$r['ftemployee_code']}' and
                                                    exists(select a.fnid
                                                            from tlemployee_salary_dec a
                                                            where txpayroll_slip_dec.ftsalary_code=a.ftsalary_code and
                                                                    ftsalary_category_code='B01')";
                                        $rs_1=mysql_query($ssql_1);
                                        $r_1=mysql_fetch_assoc($rs_1);

//                                        $ssql="delete
//                                                from txpayroll_slip_dec
//                                                where ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."' AND
//                                                    ftemployee_code='{$r['ftemployee_code']}' and
//                                                    exists(select a.fnid
//                                                            from tlemployee_salary_dec a
//                                                            where txpayroll_slip_dec.ftsalary_code=a.ftsalary_code and
//                                                                    a.ftsalary_category_code='B01')";
                                       $ssql="update
                                                 txpayroll_slip_dec
                                                  set fcamount=0
                                                where ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."' AND
                                                    ftemployee_code='{$r['ftemployee_code']}' and
                                                    exists(select a.fnid
                                                            from tlemployee_salary_dec a
                                                            where txpayroll_slip_dec.ftsalary_code=a.ftsalary_code and
                                                                    a.ftsalary_category_code='B01')";

                                                mysql_query($ssql);

                                          $ssql="UPDATE txpayroll_slip_emp
                                                    SET
                                                    fcdeduction_amount=fcdeduction_amount-{$r_1['fcamount']},
                                                    fcamount=fcamount+{$r_1['fcamount']}
                                                WHERE ftemployee_code='{$r['ftemployee_code']}' AND
                                                    ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."'
                                                ";
                            #echo $ssql."***";
                                                 mysql_query($ssql);
                                    }


                            }

                     mysql_query("COMMIT");

               $id=RetRstField("SELECT fnid FROM txpayroll_slip_hdr WHERE ftpayroll_slip_no='".base64_decode($_REQUEST['ftpayroll_slip_nox'])."' ");
               ?>
                    window.location= 'hrd.php?form=slip_gaji4&mode=edit&id=<?php echo $id; ?>';
                  <?php
              break;
              case "grid0":
                $ssql1x="select sum(fcamount) as fcamount from txpayroll_slip_inc_plus where ftemployee_code='".($_REQUEST['ftemployee_code'])."' AND  ftpayroll_slip_no='".($_REQUEST['ftpayroll_slip_no'])."'";
                $ssql2x="select sum(fcamount) as fcamount from txpayroll_slip_dec where ftemployee_code='".($_REQUEST['ftemployee_code'])."' AND  ftpayroll_slip_no='".($_REQUEST['ftpayroll_slip_no'])."'";
                $fcincome_amountD0=RetRstField($ssql1x);
                $fcdeduction_amountD0=RetRstField($ssql2x);
                $fcamountD0=$fcincome_amountD0-$fcdeduction_amountD0;
               ?>

                   document.getElementById('fcincome_amountD0<?php echo $_REQUEST['index'];?>').value='<?php echo $fcincome_amountD0;?>';
                   document.getElementById('fcdeduction_amountD0<?php echo $_REQUEST['index'];?>').value='<?php echo $fcdeduction_amountD0;?>';
                   document.getElementById('fcamountD0<?php echo $_REQUEST['index'];?>').value='<?php echo $fcamountD0;?>';
                   ElementCurrencyOnBlur(document.getElementById('fcincome_amountD0<?php echo $_REQUEST['index'];?>'));
                   ElementCurrencyOnBlur(document.getElementById('fcdeduction_amountD0<?php echo $_REQUEST['index'];?>'));
                   ElementCurrencyOnBlur(document.getElementById('fcamountD0<?php echo $_REQUEST['index'];?>'));

               <?

               break;

              case "grid1":
               ob_start();


				$ssql_2="select fnindex, ftsalary_code,fcamount from txpayroll_slip_inc_plus where ftemployee_code='".($_REQUEST['ftemployee_code'])."' AND  ftpayroll_slip_no='".($_REQUEST['ftpayroll_slip_no'])."'";
                #die($ssql_2);
				$objAxpDB->GridSQL(1,$ssql_2."#");

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

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

                    div.innerHTML =m;
                    <?php
                    if(!RetRstField($ssql_2)) {
                    ?>
                       document.getElementById("TransactionDetail1").deleteRow(3);
                    <?
                    }
                    ?>

                    RowIndex1();
                    reSize();

               <?

               break;
                case "grid2":
               ob_start();

                        $ssql_3="select fnindex, ftsalary_code,fcamount from txpayroll_slip_dec where ftemployee_code='".($_REQUEST['ftemployee_code'])."' AND  ftpayroll_slip_no='".($_REQUEST['ftpayroll_slip_no'])."'";
                        $objAxpDB->GridSQL(2,$ssql_3."#");



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

               ?>

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

                    div.innerHTML =m;
                    <?php
                    if(!RetRstField($ssql_3)) {
                    ?>
                       document.getElementById("TransactionDetail2").deleteRow(3);
                    <?
                    }
                    ?>

                    RowIndex2();
                    reSize();

               <?


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


?>