<?php

         function ValRBVoucher() {
           for($j=1; $j<count($_REQUEST['ftacc_noD0']); $j++) {
                  if($_REQUEST['fnpayment_methodD0'][$j]==5) {

                        $ssql="SELECT fnid, IFNULL(a.fcoutstanding_amount,0) AS fcamount_ready, ftcurrency_code
                                FROM txdebit_memo_hdr a
                                WHERE ftdm_no='".$_REQUEST['ftgiro_noD0'][$j]."' and ftsupplier_code='".$_REQUEST['ftsupplier_code']."'";
                        $rs=mysql_query($ssql);
                        $r=mysql_fetch_assoc($rs);

                        $fcoutstanding = 0;
                        if ($_REQUEST[mode] == "simpan_revisi") {
                            $fcoutstanding = RetRstField("SELECT IFNULL(fcamount,0) FROM txrecap_bank_voucher_det1 WHERE ftcvs_no='{$_REQUEST['ftcvs_no']}' and fnpayment_method=5 and ftgiro_no='{$_REQUEST['ftgiro_noD0'][$j]}' ");
                        }

                        $fcoutstanding = $fcoutstanding + $r['fcamount_ready'];

                        if ($_REQUEST['ftcurrency_code'] != $r['ftcurrency_code']) {
                            echo "<script>
                                  alert('Mata Uang Debit Memo yang digunakan harus sama dengan Mata Uang Pembayaran Hutang. ');
                                  window.history.back();
                                 </script>";
                            die("");
                        } else if ($_REQUEST['fcamountUSDD0'][$j] > $fcoutstanding) {
                            echo "<script>
                                  alert('Nominal lebih dari Debit Memo yang tersedia. Debit Memo yang tersedia sebesar {$fcoutstanding} . ');
                                  window.history.back();
                                 </script>";
                            die("");
                        }

                  } elseif($_REQUEST['fnpayment_methodD0'][$j]==9) {

                        $ssql="SELECT a.fttrans_no, IFNULL(a.fcoutstanding_amount,0) AS fcamount_ready
                                FROM txadvanced_payment a
                                WHERE a.fttrans_no = '{$_REQUEST['ftgiro_noD0'][$j]}' and a.fnstatus <> 3 and a.fnstatus <> 9 ";

                        $rs=mysql_query($ssql);
                        $r=mysql_fetch_assoc($rs);

                        $fcoutstanding = 0;
                        if ($_REQUEST[mode] == "simpan_revisi") {
                            $fcoutstanding = RetRstField("SELECT IFNULL(fcamount,0) FROM txrecap_bank_voucher_det1 WHERE ftcvs_no='{$_REQUEST['ftcvs_no']}' and fnpayment_method=9 and ftgiro_no='{$_REQUEST['ftgiro_noD0'][$j]}' ");
                        }

                        $fcoutstanding = $fcoutstanding + $r['fcamount_ready'];

                        if ($_REQUEST['fcamountUSDD0'][$j] > $fcoutstanding) {
                            echo "<script>
                                  alert('Nominal lebih dari Uang Muka yang tersedia. Uang Muka yang tersedia sebesar {$fcoutstanding} . ');
                                  window.history.back();
                                 </script>";
                            die("");
                        }
                  }
                  elseif($_REQUEST['ftgiro_noD0'][$j]!="") {
                       $ssql="select fnid
                            from txgiro2_hist
                            where   fnstatus_giro<>9 and
                                        ftdocument_no<>'".$_REQUEST['ftcvs_no']."'
                                    and ftgiro_no='".$_REQUEST['ftgiro_noD0'][$j]."'";
                     
                        //die($ssql);
                      $rs=mysql_query($ssql);
                        $r=mysql_fetch_assoc($rs);
                        if($r['fnid']) {
                            echo "<script>
                                      alert('No. Reff sudah pernah dipakai ...');
                                      window.history.back();
                                 </script>";
                                die("No. Reff sudah pernah dipakai ...");
                        }
                  }
           }

     }



   function SaveData() {
        ValRBVoucher();
        global $objAxpDB;
        $objAxpDB->Allow_Save(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "simpan":
          $_REQUEST['fdtrans_date']=getDateYmd($_REQUEST['fdtrans_date']);

          $strSQL['Insert txrecap_bank_voucher_hdr'][]="
                  INSERT INTO txrecap_bank_voucher_hdr (
                      fnid,ftcvs_no, ftbranch_code, fdtrans_date,
                      ftcurrency_code, fckurs, 
                      ftsupplier_code, ftaddress, ftnpwp,
                      fcsub_amount, fcinvoice_amount, fcother_cost,
                      fcother_costIDR, fcamount, fcsub_amountUSD, 
                      fcinvoice_amountUSD, fcother_costUSD, fcother_costUSDx,
                      fcamountUSD, fcrate_adjustment, ftremarks, fnstatus,
                      ftprint_counter, fnprint_status, fntype,
                      ftother_coa_code,
                      ftcreated_user,fdcreated_date,ftmodified_user,
                      fdmodified_date, ftRef_No)
                  SELECT 
                      '' AS fnid, '{$_REQUEST['ftcvs_no']}' AS ftcvs_no, '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['fdtrans_date']}' AS fdtrans_date,
                      '{$_REQUEST['ftcurrency_code']}', '".CCur($_REQUEST['fckurs'])."' AS fckurs, 
                      '{$_REQUEST['ftsupplier_code']}' AS ftsupplier_code,'{$_REQUEST['ftaddress']}' AS ftaddress, '{$_REQUEST['ftnpwp']}' AS ftnpwp, 
                      '".CCur($_REQUEST['fcsub_amount'])."' AS fcsub_amount, '".CCur($_REQUEST['fcinvoice_amount'])."' AS fcinvoice_amount, '".CCur($_REQUEST['fcother_cost'])."' AS fcother_cost,
                      '".CCur($_REQUEST['fcother_costIDR'])."' AS fcother_costIDR, '".CCur($_REQUEST['fcamount'])."' AS fcamount, '".CCur($_REQUEST['fcsub_amountUSD'])."' AS fcsub_amountUSD,
                      '".CCur($_REQUEST['fcinvoice_amountUSD'])."' AS fcinvoice_amountUSD, '".CCur($_REQUEST['fcother_costUSD'])."' AS fcother_costUSD, '".CCur($_REQUEST['fcother_costUSDx'])."' AS fcother_costUSDx,
                      '".CCur($_REQUEST['fcamountUSD'])."' AS fcamountUSD, '".CCur($_REQUEST['fcrate_adjustment'])."' AS fcrate_adjustment, '{$_REQUEST['ftremarks']}' AS ftremarks, '1' AS fnstatus,
                      '' AS ftprint_counter, '' AS fnprint_status, '0' AS fntype,
                      '{$_REQUEST['ftother2_coa_code']}' AS ftother_coa_code,
                      '{$_SESSION['username']}' AS ftcreated_user,NOW()  AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,
                      NOW() AS fdmodified_date, '{$_REQUEST['ftRef_No']}' AS ftRef_No";

          for($j=1; $j<count($_REQUEST['ftacc_noD0']); $j++) {
            $_REQUEST['fdgiro_dateD0'][$j]=getDateYmd($_REQUEST['fdgiro_dateD0'][$j]);
            $_REQUEST['fdgiro_cliring_dateD0'][$j]=getDateYmd($_REQUEST['fdgiro_cliring_dateD0'][$j]);

            if(($_REQUEST['fnpayment_methodD0'][$j]==0)||($_REQUEST['fnpayment_methodD0'][$j]==1))
              $fnstatus="0";
            else
              $fnstatus="1";

            if(($_REQUEST['fnpayment_methodD0'][$j]==0)||($_REQUEST['fnpayment_methodD0'][$j]==1)) {
               $strSQL['Insert txgiro2_hist'][]="
                INSERT INTO txgiro2_hist (
                    fnid,ftbranch_code,ftacc_no,ftacc_desc,
                    ftdocument_no,ftsupplier_code,ftsupplier_name,
                    ftdebit_no,fnpayment_method,ftgiro_no,
                    fdgiro_date,fdgiro_cliring_date,fdtrans_date,
                    ftremarks,fnstatus_giro,fncategory,
                    fncategory2,fcamount,fcdebit_memo,fcother_cost,
                    ftother_sign,fctotal,fcrate,ftprint_counter,
                    fnprint_status,ftcreated_user,fdcreated_date,
                    ftmodified_user,fdmodified_date ,fttype
                    )
                    SELECT
                    '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftacc_noD0'][$j]}' AS ftacc_no,'' AS ftacc_desc,
                    '{$_REQUEST['ftcvs_no']}' AS ftdocument_no,'{$_REQUEST['ftsupplier_code']}' AS ftsupplier_code,'' AS ftsupplier_name,
                    '' AS ftdebit_no,'{$_REQUEST['fnpayment_methodD0'][$j]}' AS fnpayment_method,'{$_REQUEST['ftgiro_noD0'][$j]}' AS ftgiro_no,
                    '{$_REQUEST['fdgiro_dateD0'][$j]}' AS fdgiro_date,'{$_REQUEST['fdgiro_cliring_dateD0'][$j]}' AS fdgiro_cliring_date,'{$_REQUEST['fdtrans_date']}' AS fdtrans_date,
                    '' AS ftremarks,'0' AS fnstatus_giro,'' AS fncategory,
                    '' AS fncategory2,'".CCur($_REQUEST['fcamountUSDD0'][$j])."' AS fcamount,'' AS fccredit_memo,'".CCur($_REQUEST['fcother_costUSDD0'][$j])."' AS fcother_cost,
                    '' AS ftother_sign,'".CCur($_REQUEST['fctotalUSDD0'][$j])."' AS fctotal,'".CCur($_REQUEST['fckurs'])."' AS fcrate,'' AS ftprint_counter,
                    '' AS fnprint_status,'{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
                    '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date ,
                    'AP' AS fttype";

            } else if ($_REQUEST['fnpayment_methodD0'][$j]==5) {
                   $hit = (float)CCur($_REQUEST['fcamountUSDD0'][$j]);
                   $strSQL['Update txdebit_memo_hdr'][]="UPDATE txdebit_memo_hdr SET
                                fcoutstanding_amount=fcoutstanding_amount - {$hit},
                                fcdebit_amount=IFNULL(fcdebit_amount,0) + {$hit}
                                WHERE ftdm_no='{$_REQUEST['ftgiro_noD0'][$j]}'";

            } else if ($_REQUEST['fnpayment_methodD0'][$j]==9) {
                   $hit = (float)CCur($_REQUEST['fcamountUSDD0'][$j]);
                   $strSQL['Update txadvanced_payment'][]="UPDATE txadvanced_payment SET
                                fcused_amount=ifnull(fcused_amount,0) + {$hit},
                                fcoutstanding_amount=IFNULL(fcoutstanding_amount,0) - {$hit},
                                fnstatus = 2
                                WHERE fttrans_no='{$_REQUEST['ftgiro_noD0'][$j]}'";
            }

            $strSQL['Insert txrecap_bank_voucher_det1'][]="
                    INSERT INTO txrecap_bank_voucher_det1(
                        fnid,ftbranch_code,ftcvs_no,
                        fnindex,fdtrans_date,ftacc_no,
                        ftacc_desc,ftdm_no,ftDP_No,
                        fnpayment_method,ftgiro_no,fttf_no,fdgiro_date,
                        fdgiro_cliring_date,ftremarks,fnstatus_giro,
                        fnstatus_giro2,fcamount,fcdp_amount,
                        fcamountUSD, fcother_costUSD, fctotalUSD,
                        fcother_costIDR, 
                        fcdebit_memo,ftother_coa_code,fcother_cost,ftother_sign,
                        fctotal,ftcreated_user,fdcreated_date,
                        ftmodified_user,fdmodified_date)
                    SELECT
                        '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftcvs_no']}' AS ftcvs_no,
                        '$j' AS fnindex,'{$_REQUEST['fdtrans_date'][$j]}' AS fdtrans_date,'{$_REQUEST['ftacc_noD0'][$j]}' AS ftacc_no,
                        '' AS ftacc_desc,'{$_REQUEST['ftdm_noD0'][$j]}' AS ftdm_no,'' AS ftDP_No,
                        '{$_REQUEST['fnpayment_methodD0'][$j]}' AS fnpayment_method,'{$_REQUEST['ftgiro_noD0'][$j]}' AS ftgiro_no,'{$_REQUEST['fttf_noD0'][$j]}' AS fttf_no,'{$_REQUEST['fdgiro_dateD0'][$j]}' AS fdgiro_date,
                        '{$_REQUEST['fdgiro_cliring_dateD0'][$j]}' AS fdgiro_cliring_date,'{$_REQUEST['ftremarksD0'][$j]}' AS ftremarks,'{$fnstatus}' AS fnstatus_giro,
                        '' AS fnstatus_giro2,'".CCur($_REQUEST['fcamountD0'][$j])."' AS fcamount,'' AS fcdp_Amount,
                        '".CCur($_REQUEST['fcamountUSDD0'][$j])."' AS fcamountUSD,'".CCur($_REQUEST['fcother_costUSDD0'][$j])."' AS fcother_costUSD,'".CCur($_REQUEST['fctotalUSDD0'][$j])."' AS fctotalUSD,
                        '".CCur($_REQUEST['fcother_costIDRD0'][$j])."' AS fcother_costIDR,
                        '' AS fcdebit_memo,'{$_REQUEST['ftother_coa_codeD0'][$j]}' AS ftother_coa_code,'".CCur($_REQUEST['fcother_costD0'][$j])."'  AS fcother_cost,'+' AS ftother_sign,
                        '".CCur($_REQUEST['fctotalD0'][$j])."' AS fctotal, '{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
                         '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";
          }

          $jj=0;
          for($j=0; $j<count($_REQUEST['fnindexD1']); $j++) {
            $_REQUEST['ftdue_dateD1'][$j]=getDateYmd($_REQUEST['ftdue_dateD1'][$j]);
            if((floatval($_REQUEST['fcpayment_amountD1'][$j])!=0)||(floatval($_REQUEST['fcpayment_amountUSDD1'][$j])!=0) ) {
              $jj++;
              $strSQL['Insert txrecap_bank_voucher_det'][]="
                      INSERT INTO txrecap_bank_voucher_det(
                          fnid, ftbranch_code, ftcvs_no,ftpi_type,
                          fnindex, ftpp_no, ftpo_no,
                          ftcv_no, ftreceipt_no, ftdue_date,
                          fcadjustment, ftpi_no, ftcurrency_code,
                          fcamount, fcoutstanding_amount, fcpayment_amount,
                          fcamountUSD, fcoutstanding_amountUSD, fcpayment_amountUSD,
                          ftcreated_user, fdcreated_date,
                          ftmodified_user, fdmodified_date)
                      SELECT
                          '' AS fnid, '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftcvs_no']}' AS ftcvs_no, '{$_REQUEST['ftpi_typeD1'][$j]}' AS ftpi_type,
                          '{$jj}' AS fnindex, '{$_REQUEST['ftpp_noD1'][$j]}' AS ftpp_no, '{$_REQUEST['ftpo_noD1'][$j]}' AS ftpo_no,
                          '{$_REQUEST['ftcv_noD1'][$j]}' as ftcv_no, '{$_REQUEST['ftreceipt_noD1'][$j]}' AS ftreceipt_no, '{$_REQUEST['ftdue_dateD1'][$j]}' AS ftdue_date,
                          '".CCur($_REQUEST['fcadjustmentD1'][$j])."' AS fcadjustment, '{$_REQUEST['ftpi_noD1'][$j]}' AS ftpi_no, '{$_REQUEST['ftcurrency_codeD1'][$j]}' AS ftcurrency_code,
                          '".CCur($_REQUEST['fcamountD1'][$j])."' AS fcamount, '".CCur($_REQUEST['fcoutstanding_amountD1'][$j])."' AS fcoutstanding_amount, '".CCur($_REQUEST['fcpayment_amountD1'][$j])."' AS fcpayment_amount,
                          '".CCur($_REQUEST['fcamountUSDD1'][$j])."' AS fcamountUSD, '".CCur($_REQUEST['fcoutstanding_amountUSDD1'][$j])."' AS fcoutstanding_amountUSD, '".CCur($_REQUEST['fcpayment_amountUSDD1'][$j])."' AS fcpayment_amountUSD,
                          '{$_SESSION['username']}' AS ftcreated_user, NOW() AS fdcreated_date,
                          '{$_SESSION['username']}' AS ftmodified_user, NOW() AS fdmodified_date";

                $hit = (float)CCur($_REQUEST['fcpayment_amountUSDD1'][$j]);
                if((float)CCur($_REQUEST['fcoutstanding_amountUSDD1'][$j])==(float)CCur($_REQUEST['fcpayment_amountUSDD1'][$j])) {
                    $fnpayment_status = 3;
    
                } else {
                    $fnpayment_status = 2;
    
                }                                      
                
                if (($_REQUEST['ftpi_typeD1'][$j] == "HUTANG AWAL") OR ($_REQUEST['ftpi_typeD1'][$j] == "FAKTUR")) {
                    $strSQL['Update txpurchase_invoice_hdr'][]="UPDATE txpurchase_invoice_hdr SET fnpayment_status = {$fnpayment_status}, fcoutstanding_amount = ifnull(fcoutstanding_amount,0) - {$hit}, fcpayment_amount = IFNULL(fcpayment_amount,0) + ".CCur($_REQUEST['fcpayment_amountUSDD1'][$j]).", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE   ftpurchase_invoice_no = '{$_REQUEST['ftpi_noD1'][$j]}'";

                } else if ($_REQUEST['ftpi_typeD1'][$j] == "RETUR") {
                    $strSQL['Update txpurchase_retur_hdr'][]="UPDATE txpurchase_return_hdr SET fnpayment_status = {$fnpayment_status}, fcoutstanding_amount = ifnull(fcoutstanding_amount,fcamount) - ({$hit} * -1), fcpayment_amount = IFNULL(fcpayment_amount,0) + ". (CCur($_REQUEST['fcpayment_amountUSDD1'][$j]) * -1 ) . ", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE  ftsales_return_no = '{$_REQUEST['ftpi_noD1'][$j]}'";
                }

            }
          }

          // Save other cost
          $jj=0;
          for($j=0; $j<count($_REQUEST['fnindexD2']); $j++) {
              if((floatval($_REQUEST['fcamountD2'][$j])!=0)) {
                  $jj++;
                  $strSQL['Insert txrecap_bank_voucher_cost'][]="
                          INSERT INTO txrecap_bank_voucher_cost(
                              ftbranch_code, ftcvs_no,
                              fnindex, ftcoa_code, ftdescription,
                              fcamount,
                              ftcreated_user, fdcreated_date,
                              ftmodified_user, fdmodified_date)
                          SELECT
                              '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftcvs_no']}' AS ftcvs_no,
                              '{$jj}' AS fnindex, '{$_REQUEST['ftcoa_codeD2'][$j]}' AS ftcoa_code, '{$_REQUEST['ftdescriptionD2'][$j]}' AS ftdescription,
                              '".CCur($_REQUEST['fcamountD2'][$j])."' AS fcamount,
                              '{$_SESSION['username']}' AS ftcreated_user, NOW() AS fdcreated_date,
                              '{$_SESSION['username']}' AS ftmodified_user, NOW() AS fdmodified_date";
              }
          }

          $_REQUEST['fdtrans_date']=CDateRev($_REQUEST['fdtrans_date']);
          
          break;
        }
        //pr($strSQL);die("mm");
        return $strSQL;
   }

function FinalData() {
        /*
        ValRBVoucher();
        global $objAxpDB;
        $objAxpDB->Allow_Final(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "final":
            $_REQUEST['fdtrans_date']=getDateYmd($_REQUEST['fdtrans_date']);

            $strSQL['Insert txrecap_bank_voucher_hdr'][]="
                  UPDATE  txrecap_bank_voucher_hdr
                                SET ftfinal_user='{$_SESSION['username']}',fdfinal_date=now(), fckurs='".CCur($_REQUEST['fckurs'])."',
                                fdtrans_date='{$_REQUEST['fdtrans_date']}',ftsupplier_code='{$_REQUEST['ftsupplier_code']}',
                                ftaddress='{$_REQUEST['ftaddress']}',
                                ftnpwp='{$_REQUEST['ftnpwp']}',fcsub_amount='".CCur($_REQUEST['fcsub_amount'])."',
                                fcamount='".CCur($_REQUEST['fcamount'])."',
                                fcinvoice_amount='".CCur($_REQUEST['fcinvoice_amount'])."' ,
                                fcoutstanding_amount='".CCur($_REQUEST['fcoutstanding_amount'])."',ftremarks='{$_REQUEST['ftremarks']}',
                                fnstatus='2',ftprint_counter='',fnprint_status='',
                                ftmodified_user='{$_SESSION['username']}',
                                fdmodified_date=now(),fcother_cost='".CCur($_REQUEST['fcother_cost'])."', ftcurrency_code ='{$_REQUEST['ftcurrency_code']}'
                                WHERE ftcvs_no='{$_REQUEST['ftcvs_no']}'";

              $strSQL['del txgiro_hist'][]="delete from txgiro2_hist where ftdocument_no='{$_REQUEST['ftcvs_no']}'";
              $strSQL['del txrecap_bank_voucher_det1'][]="delete from txrecap_bank_voucher_det1 where ftcvs_no='{$_REQUEST['ftcvs_no']}'";
              $strSQL['del txrecap_bank_voucher_det'][]="delete from txrecap_bank_voucher_det where ftcvs_no='{$_REQUEST['ftcvs_no']}'";


              for($j=1; $j<count($_REQUEST['ftacc_noD0']); $j++) {
                  $_REQUEST['fdgiro_dateD0'][$j]=getDateYmd($_REQUEST['fdgiro_dateD0'][$j]);
                  $_REQUEST['fdgiro_cliring_dateD0'][$j]=getDateYmd($_REQUEST['fdgiro_cliring_dateD0'][$j]);

                  if(($_REQUEST['fnpayment_methodD0'][$j]==0)||($_REQUEST['fnpayment_methodD0'][$j]==1)) {
                      $fnstatus="0";
                  }
                  else {
                      $fnstatus="1";
                  }
				  //cheque,giro
                  if(($_REQUEST['fnpayment_methodD0'][$j]==0)||($_REQUEST['fnpayment_methodD0'][$j]==1)) {
                       $strSQL['Insert txgiro2_hist'][]="
                        INSERT INTO txgiro2_hist (
                            fttype, fnid,ftbranch_code,ftacc_no,ftacc_desc,
                            ftdocument_no,ftsupplier_code,ftsupplier_name,
                            ftdebit_no,fnpayment_method,ftgiro_no,
                            fdgiro_date,fdgiro_cliring_date,fdtrans_date,
                            ftremarks,fnstatus_giro,fncategory,
                            fncategory2,fcamount,fcdebit_memo,fcother_cost,
                            ftother_sign,fctotal,fcrate,ftprint_counter,
                            fnprint_status,ftcreated_user,fdcreated_date,
                            ftmodified_user,fdmodified_date,ftpf_no 
                            )
                            SELECT
                            'AP' as fttype, '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftacc_noD0'][$j]}' AS ftacc_no,'' AS ftacc_desc,
                            '{$_REQUEST['ftcvs_no']}' AS ftdocument_no,'{$_REQUEST['ftsupplier_code']}' AS ftsupplier_code,'' AS ftsupplier_name,
                            '' AS ftdebit_no,'{$_REQUEST['fnpayment_methodD0'][$j]}' AS fnpayment_method,'{$_REQUEST['ftgiro_noD0'][$j]}' AS ftgiro_no,
                            '{$_REQUEST['fdgiro_dateD0'][$j]}' AS fdgiro_date,'{$_REQUEST['fdgiro_cliring_dateD0'][$j]}' AS fdgiro_cliring_date,'{$_REQUEST['fdtrans_date']}' AS fdtrans_date,
                            '' AS ftremarks,'0' AS fnstatus_giro,'' AS fncategory,
                            '' AS fncategory2,'".CCur($_REQUEST['fcamountUSDD0'][$j])."' AS fcamount,'' AS fcdebit_memo,'".CCur($_REQUEST['fcother_costUSDD0'][$j])."' AS fcother_cost,
                            '' AS ftother_sign,'".CCur($_REQUEST['fctotalUSDD0'][$j])."' AS fctotal,'".CCur($_REQUEST['fckurs'])."' AS fcrate,'' AS ftprint_counter,
                            '' AS fnprint_status,'{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
                            '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date , '{$_REQUEST['fttf_noD0'][$j]}' AS fttf_no ";
                  }
                  //Debit memo
                  elseif($_REQUEST['fnpayment_methodD0'][$j]==5) {
                         $strSQL['Update txdebit_memo_hdr'][]="
                                UPDATE txdebit_memo_hdr SET fcoutstanding_amount=fcoutstanding_amount-".CCur($_REQUEST['fcamountUSDD0'][$j]).",
                                                fcdebit_amount=fcdebit_amount+".CCur($_REQUEST['fcamountUSDD0'][$j])."
                                                WHERE ftdm_no='{$_REQUEST['ftgiro_noD0'][$j]}'";

                  }

                  $strSQL['Insert txrecap_bank_voucher_det1'][]="INSERT INTO txrecap_bank_voucher_det1(
                                                    fnid,ftbranch_code,ftcvs_no,
                                                    fnindex,fdtrans_date,ftacc_no,
                                                    ftacc_desc,ftdm_no,ftDP_No,
                                                    fnpayment_method,ftgiro_no,fttf_no,fdgiro_date,
                                                    fdgiro_cliring_date,ftremarks,fnstatus_giro,
                                                    fnstatus_giro2,fcamount,fcDP_Amount,
                                                    fcdebit_memo,ftother_coa_code,fcother_cost,ftother_sign,
                                                    fctotal,ftcreated_user,fdcreated_date,
                                                    ftmodified_user,fdmodified_date
                                                    )
                                                    SELECT
                                                    '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftcvs_no']}' AS ftcvs_no,
                                                    '$j' AS fnindex,'{$_REQUEST['fdtrans_dateD0'][$j]}' AS fdtrans_date,'{$_REQUEST['ftacc_noD0'][$j]}' AS ftacc_no,
                                                    '' AS ftacc_desc,'{$_REQUEST['ftdm_noD0'][$j]}' AS ftdm_no,'' AS ftDP_No,
                                                    '{$_REQUEST['fnpayment_methodD0'][$j]}' AS fnpayment_method,'{$_REQUEST['ftgiro_noD0'][$j]}' AS ftgiro_no,'{$_REQUEST['fttf_noD0'][$j]}' AS fttf_no,'{$_REQUEST['fdgiro_dateD0'][$j]}' AS fdgiro_date,
                                                    '{$_REQUEST['fdgiro_cliring_dateD0'][$j]}' AS fdgiro_cliring_date,'{$_REQUEST['ftremarksD0'][$j]}' AS ftremarks,'{$fnstatus}' AS fnstatus_giro,
                                                    '' AS fnstatus_giro2,'".CCur($_REQUEST['fcamountD0'][$j])."' AS fcamount,'' AS fcDP_Amount,
                                                    '' AS fcdebit_memo,'{$_REQUEST['ftother_coa_codeD0'][$j]}' AS ftother_coa_code,'".CCur($_REQUEST['fcother_costD0'][$j])."'  AS fcother_cost,'+' AS ftother_sign,
                                                    '".CCur($_REQUEST['fctotalD0'][$j])."' AS fctotal, '{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
                                                     '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date
                                                    ";


              }
              $jj=0;
             for($j=0; $j<count($_REQUEST['fcpayment_amountD']); $j++) {
                 $_REQUEST['ftdue_date'][$j]=getDateYmd($_REQUEST['ftdue_date'][$j]);
                  if(((float)CCur($_REQUEST['fcpayment_amountD'][$j])!=0)||((float)CCur($_REQUEST['fcpayment_amountUSDD'][$j])!=0) ) {
                         $jj=$jj+1;
                         $strSQL['Insert txrecap_bank_voucher_det'][]="INSERT INTO txrecap_bank_voucher_det(
                                                            fnid,ftbranch_code,ftcvs_no,
                                                            fnindex,ftpp_no,ftpo_no,ftcv_no,
                                                            ftreceipt_no,ftdue_date,fcamount,
                                                            fcoutstanding_amount,fcpayment_amount,ftcreated_user,
                                                            fdcreated_date,ftmodified_user,fdmodified_date,
                                                            fcamountUSD,
                                                            fcoutstanding_amountUSD,fcpayment_amountUSD, ftcurrency_code 
                                                            )
                                                            SELECT
                                                            '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftcvs_no']}' AS ftcvs_no,
                                                            '{$jj}' AS fnindex,'{$_REQUEST['ftpp_noD'][$j]}' AS ftpp_no,'{$_REQUEST['ftpo_noD'][$j]}' AS ftpo_no,'{$_REQUEST['ftcv_noD'][$j]}' as ftcv_no,
                                                            '{$_REQUEST['ftreceipt_noD'][$j]}' AS ftreceipt_no,'{$_REQUEST['ftdue_dateD'][$j]}' AS ftdue_date,'".CCur($_REQUEST['fcamount1D'][$j])."' AS fcamount,
                                                            '".CCur($_REQUEST['fcoutstanding_amountD'][$j])."' AS fcoutstanding_amount,'".CCur($_REQUEST['fcpayment_amountD'][$j])."' AS fcpayment_amount,'{$_SESSION['username']}' AS ftcreated_user,
                                                            NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date,
                                                            '".CCur($_REQUEST['fcamount1USDD'][$j])."' AS fcamountUSD,
                                                            '".CCur($_REQUEST['fcoutstanding_amountUSDD'][$j])."' AS fcoutstanding_amountUSD,'".CCur($_REQUEST['fcpayment_amountUSDD'][$j])."' AS fcpayment_amountUSD,'{$_REQUEST['ftcurrency_codeD'][$j]}' as ftcurrency_code
                                                        ";

                            $hit = (float)CCur($_REQUEST['fcoutstanding_amountD'][$j]) - (float)CCur($_REQUEST['fcpayment_amountD'][$j]);
                            $hit2 = (float)CCur($_REQUEST['fcoutstanding_amountUSDD'][$j]) - (float)CCur($_REQUEST['fcpayment_amountUSDD'][$j]);

                }

              }

            $_REQUEST['fdtrans_date']=CDateRev($_REQUEST['fdtrans_date']);

            break;
        }
       
        return $strSQL;
         
         */
   }

 function RevisiData(){
        global $objAxpDB;
        $objAxpDB->Allow_Revisi(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
            case "simpan_revisi":
            ValRBVoucher();
            // Data lama penonomoran ditambah format '-'
            $ssql1="SELECT IFNULL(MAX(ftcvs_no),'')  AS M
                      FROM txrecap_bank_voucher_hdr
                      WHERE ftcvs_no LIKE '{$_REQUEST['ftcvs_no']}-%'";
            $no=RetRstField($ssql1);

            if($no!="") {
                $no1=explode("-",$no);
                $no=(int)$no1[1]+1;
                $str1="-{$no}";
            }
            else {
                $str1="-1";
            }

            $ftcvs_no_new="{$_REQUEST['ftcvs_no']}{$str1}";

            $strSQL["Revisi"][]="update txrecap_bank_voucher_hdr
                    set fdmodified_date=now(), ftmodified_user='{$_SESSION['username']}',
                    fnstatus=3, ftcvs_no='{$ftcvs_no_new}'
                    where ftcvs_no='{$_REQUEST['ftcvs_no']}'";

            $strSQL["Revisi"][]="update txrecap_bank_voucher_det
                    set ftcvs_no='{$ftcvs_no_new}'
                    where ftcvs_no='{$_REQUEST['ftcvs_no']}'";

            $strSQL["Revisi"][]="update txrecap_bank_voucher_det1
                    set ftcvs_no='{$ftcvs_no_new}', fnstatus_giro=9
                    where ftcvs_no='{$_REQUEST['ftcvs_no']}'";

            $strSQL['Revisi'][]="update txrecap_bank_voucher_cost
                    set ftcvs_no='{$ftcvs_no_new}'
                    where ftcvs_no='{$_REQUEST['ftcvs_no']}'";

            $strSQL["Revisi"][]="update txgiro2_hist
                    set ftdocument_no='{$ftcvs_no_new}', fnstatus_giro='9'
                    where ftdocument_no='{$_REQUEST['ftcvs_no']}' and fttype='AP'";

            for($j=1; $j<count($_REQUEST['ftacc_noD0']); $j++) {
                
                // debit memo
                if($_REQUEST['fnpayment_methodD0'][$j]==5) {
                         $strSQL['Update txdebit_memo_hdr'][]="
                                UPDATE txdebit_memo_hdr SET fcoutstanding_amount=fcoutstanding_amount+".CCur($_REQUEST['fcamountUSDD0'][$j]).",
                                                fcdebit_amount=fcdebit_amount-".CCur($_REQUEST['fcamountUSDD0'][$j])."
                                                WHERE ftdm_no='{$_REQUEST['ftgiro_noD0'][$j]}'";
                
                } else if ($_REQUEST['fnpayment_methodD0'][$j]==9) {
                       // query update ada di revisi last
                }

            }

          // Simpan ulang
          $_REQUEST['fdtrans_date']=getDateYmd($_REQUEST['fdtrans_date']);

          $strSQL['Insert txrecap_bank_voucher_hdr'][]="
                  INSERT INTO txrecap_bank_voucher_hdr (
                      fnid,ftcvs_no, ftbranch_code, fdtrans_date,
                      ftcurrency_code, fckurs,
                      ftsupplier_code, ftaddress, ftnpwp,
                      fcsub_amount, fcinvoice_amount, fcother_cost,
                      fcother_costIDR, fcamount, fcsub_amountUSD,
                      fcinvoice_amountUSD, fcother_costUSD, fcother_costUSDx,
                      fcamountUSD, fcrate_adjustment, ftremarks, fnstatus,
                      ftprint_counter, fnprint_status, fntype,
                      ftother_coa_code,
                      ftcreated_user,fdcreated_date,ftmodified_user,
                      fdmodified_date, ftRef_No)
                  SELECT
                      '' AS fnid, '{$_REQUEST['ftcvs_no']}' AS ftcvs_no, '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['fdtrans_date']}' AS fdtrans_date,
                      '{$_REQUEST['ftcurrency_code']}', '".CCur($_REQUEST['fckurs'])."' AS fckurs,
                      '{$_REQUEST['ftsupplier_code']}' AS ftsupplier_code,'{$_REQUEST['ftaddress']}' AS ftaddress, '{$_REQUEST['ftnpwp']}' AS ftnpwp,
                      '".CCur($_REQUEST['fcsub_amount'])."' AS fcsub_amount, '".CCur($_REQUEST['fcinvoice_amount'])."' AS fcinvoice_amount, '".CCur($_REQUEST['fcother_cost'])."' AS fcother_cost,
                      '".CCur($_REQUEST['fcother_costIDR'])."' AS fcother_costIDR, '".CCur($_REQUEST['fcamount'])."' AS fcamount, '".CCur($_REQUEST['fcsub_amountUSD'])."' AS fcsub_amountUSD,
                      '".CCur($_REQUEST['fcinvoice_amountUSD'])."' AS fcinvoice_amountUSD, '".CCur($_REQUEST['fcother_costUSD'])."' AS fcother_costUSD, '".CCur($_REQUEST['fcother_costUSDx'])."' AS fcother_costUSDx,
                      '".CCur($_REQUEST['fcamountUSD'])."' AS fcamountUSD, '".CCur($_REQUEST['fcrate_adjustment'])."' AS fcrate_adjustment, '{$_REQUEST['ftremarks']}' AS ftremarks, '1' AS fnstatus,
                      '' AS ftprint_counter, '' AS fnprint_status, '0' AS fntype,
                      '{$_REQUEST['ftother2_coa_code']}' AS ftother_coa_code,
                      '{$_SESSION['username']}' AS ftcreated_user,NOW()  AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,
                      NOW() AS fdmodified_date, '{$_REQUEST['ftRef_No']}' AS ftRef_No";

          for($j=1; $j<count($_REQUEST['ftacc_noD0']); $j++) {
            $_REQUEST['fdgiro_dateD0'][$j]=getDateYmd($_REQUEST['fdgiro_dateD0'][$j]);
            $_REQUEST['fdgiro_cliring_dateD0'][$j]=getDateYmd($_REQUEST['fdgiro_cliring_dateD0'][$j]);

            if(($_REQUEST['fnpayment_methodD0'][$j]==0)||($_REQUEST['fnpayment_methodD0'][$j]==1))
              $fnstatus="0";
            else
              $fnstatus="1";

            if(($_REQUEST['fnpayment_methodD0'][$j]==0)||($_REQUEST['fnpayment_methodD0'][$j]==1)) {
               $strSQL['Insert txgiro2_hist'][]="
                INSERT INTO txgiro2_hist (
                    fnid,ftbranch_code,ftacc_no,ftacc_desc,
                    ftdocument_no,ftsupplier_code,ftsupplier_name,
                    ftdebit_no,fnpayment_method,ftgiro_no,
                    fdgiro_date,fdgiro_cliring_date,fdtrans_date,
                    ftremarks,fnstatus_giro,fncategory,
                    fncategory2,fcamount,fcdebit_memo,fcother_cost,
                    ftother_sign,fctotal,fcrate,ftprint_counter,
                    fnprint_status,ftcreated_user,fdcreated_date,
                    ftmodified_user,fdmodified_date ,fttype
                    )
                    SELECT
                    '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftacc_noD0'][$j]}' AS ftacc_no,'' AS ftacc_desc,
                    '{$_REQUEST['ftcvs_no']}' AS ftdocument_no,'{$_REQUEST['ftsupplier_code']}' AS ftsupplier_code,'' AS ftsupplier_name,
                    '' AS ftdebit_no,'{$_REQUEST['fnpayment_methodD0'][$j]}' AS fnpayment_method,'{$_REQUEST['ftgiro_noD0'][$j]}' AS ftgiro_no,
                    '{$_REQUEST['fdgiro_dateD0'][$j]}' AS fdgiro_date,'{$_REQUEST['fdgiro_cliring_dateD0'][$j]}' AS fdgiro_cliring_date,'{$_REQUEST['fdtrans_date']}' AS fdtrans_date,
                    '' AS ftremarks,'0' AS fnstatus_giro,'' AS fncategory,
                    '' AS fncategory2,'".CCur($_REQUEST['fcamountUSDD0'][$j])."' AS fcamount,'' AS fccredit_memo,'".CCur($_REQUEST['fcother_costUSDD0'][$j])."' AS fcother_cost,
                    '' AS ftother_sign,'".CCur($_REQUEST['fctotalUSDD0'][$j])."' AS fctotal,'".CCur($_REQUEST['fckurs'])."' AS fcrate,'' AS ftprint_counter,
                    '' AS fnprint_status,'{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
                    '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date ,
                    'AP' AS fttype";

            }  else if ($_REQUEST['fnpayment_methodD0'][$j]==5) {
                    $hit = (float)CCur($_REQUEST['fcamountUSDD0'][$j]);
                    $strSQL['Update txdebit_memo_hdr'][]="UPDATE txdebit_memo_hdr SET
                                fcoutstanding_amount=fcoutstanding_amount - {$hit},
                                fcdebit_amount=IFNULL(fcdebit_amount,0) + {$hit}
                                WHERE ftdm_no='{$_REQUEST['ftgiro_noD0'][$j]}'";

            } else if ($_REQUEST['fnpayment_methodD0'][$j]==9) {
                   $hit = (float)CCur($_REQUEST['fcamountUSDD0'][$j]);
                   $strSQL['Update txadvanced_payment'][]="UPDATE txadvanced_payment SET
                                fcused_amount=ifnull(fcused_amount,0) + {$hit},
                                fcoutstanding_amount=IFNULL(fcoutstanding_amount,0) - {$hit},
                                fnstatus = 2
                                WHERE fttrans_no='{$_REQUEST['ftgiro_noD0'][$j]}'";
            }

            $strSQL['Insert txrecap_bank_voucher_det1'][]="
                    INSERT INTO txrecap_bank_voucher_det1(
                        fnid,ftbranch_code,ftcvs_no,
                        fnindex,fdtrans_date,ftacc_no,
                        ftacc_desc,ftdm_no,ftDP_No,
                        fnpayment_method,ftgiro_no,fttf_no,fdgiro_date,
                        fdgiro_cliring_date,ftremarks,fnstatus_giro,
                        fnstatus_giro2,fcamount,fcdp_amount,
                        fcamountUSD, fcother_costUSD, fctotalUSD,
                        fcother_costIDR,
                        fcdebit_memo,ftother_coa_code,fcother_cost,ftother_sign,
                        fctotal,ftcreated_user,fdcreated_date,
                        ftmodified_user,fdmodified_date)
                    SELECT
                        '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftcvs_no']}' AS ftcvs_no,
                        '$j' AS fnindex,'{$_REQUEST['fdtrans_date'][$j]}' AS fdtrans_date,'{$_REQUEST['ftacc_noD0'][$j]}' AS ftacc_no,
                        '' AS ftacc_desc,'{$_REQUEST['ftdm_noD0'][$j]}' AS ftdm_no,'' AS ftDP_No,
                        '{$_REQUEST['fnpayment_methodD0'][$j]}' AS fnpayment_method,'{$_REQUEST['ftgiro_noD0'][$j]}' AS ftgiro_no,'{$_REQUEST['fttf_noD0'][$j]}' AS fttf_no,'{$_REQUEST['fdgiro_dateD0'][$j]}' AS fdgiro_date,
                        '{$_REQUEST['fdgiro_cliring_dateD0'][$j]}' AS fdgiro_cliring_date,'{$_REQUEST['ftremarksD0'][$j]}' AS ftremarks,'{$fnstatus}' AS fnstatus_giro,
                        '' AS fnstatus_giro2,'".CCur($_REQUEST['fcamountD0'][$j])."' AS fcamount,'' AS fcdp_Amount,
                        '".CCur($_REQUEST['fcamountUSDD0'][$j])."' AS fcamountUSD,'".CCur($_REQUEST['fcother_costUSDD0'][$j])."' AS fcother_costUSD,'".CCur($_REQUEST['fctotalUSDD0'][$j])."' AS fctotalUSD,
                        '".CCur($_REQUEST['fcother_costIDRD0'][$j])."' AS fcother_costIDR,
                        '' AS fcdebit_memo,'{$_REQUEST['ftother_coa_codeD0'][$j]}' AS ftother_coa_code,'".CCur($_REQUEST['fcother_costD0'][$j])."'  AS fcother_cost,'+' AS ftother_sign,
                        '".CCur($_REQUEST['fctotalD0'][$j])."' AS fctotal, '{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
                         '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";
          }

          $jj=0;
          for($j=0; $j<count($_REQUEST['fnindexD1']); $j++) {
            $_REQUEST['ftdue_dateD1'][$j]=getDateYmd($_REQUEST['ftdue_dateD1'][$j]);

            if((floatval($_REQUEST['fcpayment_amountD1'][$j])!=0)||(floatval($_REQUEST['fcpayment_amountUSDD1'][$j])!=0) ) {
              $jj++;
              $strSQL['Insert txrecap_bank_voucher_det'][]="
                      INSERT INTO txrecap_bank_voucher_det(
                          fnid, ftbranch_code, ftcvs_no,
                          fnindex, ftpp_no, ftpo_no,
                          ftcv_no, ftreceipt_no, ftdue_date,
                          fcadjustment, ftpi_no, ftcurrency_code,
                          fcamount, fcoutstanding_amount, fcpayment_amount,
                          fcamountUSD, fcoutstanding_amountUSD, fcpayment_amountUSD,
                          ftcreated_user, fdcreated_date,
                          ftmodified_user, fdmodified_date)
                      SELECT
                          '' AS fnid, '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftcvs_no']}' AS ftcvs_no,
                          '{$jj}' AS fnindex, '{$_REQUEST['ftpp_noD1'][$j]}' AS ftpp_no, '{$_REQUEST['ftpo_noD1'][$j]}' AS ftpo_no,
                          '{$_REQUEST['ftcv_noD1'][$j]}' as ftcv_no, '{$_REQUEST['ftreceipt_noD1'][$j]}' AS ftreceipt_no, '{$_REQUEST['ftdue_dateD1'][$j]}' AS ftdue_date,
                          '".CCur($_REQUEST['fcadjustmentD1'][$j])."' AS fcadjustment, '{$_REQUEST['ftpi_noD1'][$j]}' AS ftpi_no, '{$_REQUEST['ftcurrency_codeD1'][$j]}' AS ftcurrency_code,
                          '".CCur($_REQUEST['fcamountD1'][$j])."' AS fcamount, '".CCur($_REQUEST['fcoutstanding_amountD1'][$j])."' AS fcoutstanding_amount, '".CCur($_REQUEST['fcpayment_amountD1'][$j])."' AS fcpayment_amount,
                          '".CCur($_REQUEST['fcamountUSDD1'][$j])."' AS fcamountUSD, '".CCur($_REQUEST['fcoutstanding_amountUSDD1'][$j])."' AS fcoutstanding_amountUSD, '".CCur($_REQUEST['fcpayment_amountUSDD1'][$j])."' AS fcpayment_amountUSD,
                          '{$_SESSION['username']}' AS ftcreated_user, NOW() AS fdcreated_date,
                          '{$_SESSION['username']}' AS ftmodified_user, NOW() AS fdmodified_date";

            }
          }

          // Save other cost
          $jj=0;
          for($j=0; $j<count($_REQUEST['fnindexD2']); $j++) {
              if((floatval($_REQUEST['fcamountD2'][$j])!=0)) {
                  $jj++;
                  $strSQL['Insert txrecap_bank_voucher_cost'][]="
                          INSERT INTO txrecap_bank_voucher_cost(
                              ftbranch_code, ftcvs_no,
                              fnindex, ftcoa_code, ftdescription,
                              fcamount,
                              ftcreated_user, fdcreated_date,
                              ftmodified_user, fdmodified_date)
                          SELECT
                              '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftcvs_no']}' AS ftcvs_no,
                              '{$jj}' AS fnindex, '{$_REQUEST['ftcoa_codeD2'][$j]}' AS ftcoa_code, '{$_REQUEST['ftdescriptionD2'][$j]}' AS ftdescription,
                              '".CCur($_REQUEST['fcamountD2'][$j])."' AS fcamount,
                              '{$_SESSION['username']}' AS ftcreated_user, NOW() AS fdcreated_date,
                              '{$_SESSION['username']}' AS ftmodified_user, NOW() AS fdmodified_date";
              }
          }

          $strSQL["Update txpurchase_invoice_hdr "][] ="UPDATE txpurchase_invoice_hdr a, txrecap_bank_voucher_det d
                            SET a.fcpayment_amount = (SELECT IFNULL(SUM(b.fcpayment_amountUSD),0) AS totalpayment
                                                    FROM txrecap_bank_voucher_det b INNER JOIN txrecap_bank_voucher_hdr c
                                                    ON b.ftcvs_no = c.ftcvs_no AND c.fnstatus = 1
                                                    WHERE a.ftpurchase_invoice_no = b.ftpi_no
                                               ) ,
                            a.fcoutstanding_amount = a.fcamount - (SELECT IFNULL(SUM(b.fcpayment_amountUSD),0) AS totalpayment
                                                    FROM txrecap_bank_voucher_det b INNER JOIN txrecap_bank_voucher_hdr c
                                                    ON b.ftcvs_no = c.ftcvs_no AND c.fnstatus = 1
                                                    WHERE a.ftpurchase_invoice_no = b.ftpi_no
                                               )
                            WHERE a.ftpurchase_invoice_no = d.ftpi_no AND d.ftcvs_no LIKE '{$_REQUEST['ftcvs_no']}%'";

            $_REQUEST['fdtrans_date']=CDateRev($_REQUEST['fdtrans_date']);

            // Call Function Revisi General Ledger
            if(function_exists("RevisiGL")) {
                $m = RevisiGL($ftcvs_no_new);
                if ($m != '') { $strSQL=array_merge_recursive($strSQL,$m); }
            }

            break;
      }

      return $strSQL;
   }

    function RevisiDataLast() {
        global $objAxpDB;
        $strSQL=array();
        switch($_REQUEST[mode]) {
            case "simpan_revisi":
               // update status pembayaran purchase invoice
               $strSQL["Update txpurchase_invoice_hdr"][] ="UPDATE txpurchase_invoice_hdr a, txrecap_bank_voucher_det d
                            SET a.fnpayment_status = 1
                            WHERE a.ftpurchase_invoice_no = d.ftpi_no AND d.ftcvs_no LIKE '{$_REQUEST['ftcvs_no']}%'
                                    AND a.fcoutstanding_amount = a.fcamount ";

               $strSQL["Update txpurchase_invoice_hdr"][] ="UPDATE txpurchase_invoice_hdr a, txrecap_bank_voucher_det d
                            SET a.fnpayment_status = 2
                            WHERE a.ftpurchase_invoice_no = d.ftpi_no AND d.ftcvs_no LIKE '{$_REQUEST['ftcvs_no']}%'
                                    AND a.fcoutstanding_amount > 0 and a.fcoutstanding_amount <> a.fcamount ";

               $strSQL["Update txpurchase_invoice_hdr"][] ="UPDATE txpurchase_invoice_hdr a, txrecap_bank_voucher_det d
                            SET a.fnpayment_status = 3
                            WHERE a.ftpurchase_invoice_no = d.ftpi_no AND d.ftcvs_no LIKE '{$_REQUEST['ftcvs_no']}%'
                                    AND a.fcoutstanding_amount = 0 ";
                            
               $strSQL["Update txadvanced_payment last"][] ="
                            UPDATE txadvanced_payment a LEFT JOIN
                            (
                            SELECT SUM(b.fcamount) AS fcamount,ftgiro_no
                            FROM txrecap_bank_voucher_hdr a INNER JOIN txrecap_bank_voucher_det1 b ON a.ftcvs_no = b.ftcvs_no
                            WHERE a.fnstatus <> 3 AND a.fnstatus <> 9 AND b.fnpayment_method = 9
                            GROUP BY ftgiro_no
                            ) b ON a.fttrans_no = b.ftgiro_no
                            INNER JOIN txrecap_bank_voucher_det1 c on a.fttrans_no = c.ftgiro_no AND c.ftcvs_no LIKE '{$_REQUEST['ftcvs_no']}%'
                            SET a.fcused_amount = IFNULL(b.fcamount,0), a.fcoutstanding_amount = a.fcamount - IFNULL(b.fcamount,0),
                                fnstatus = IF((IFNULL(b.fcamount,0)=0),1,2) ";


        }
        return $strSQL;
    }

 function CancelData() {
    global $objAxpDB;
    $objAxpDB->Allow_Cancel(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
      case "batal":
        $strSQL['update txrecap_bank_voucher_hdr'][]="UPDATE txrecap_bank_voucher_hdr SET fnstatus=9 WHERE ftcvs_no='{$_REQUEST['ftcvs_no']}'";
        $strSQL['update txrecap_bank_voucher_det1'][]="UPDATE txrecap_bank_voucher_det1 SET fnstatus_giro=9 WHERE ftcvs_no='{$_REQUEST['ftcvs_no']}'";

        for($j=1; $j<count($_REQUEST['ftacc_noD0']); $j++) {

            $_REQUEST['fdgiro_dateD0'][$j]=getDateYmd($_REQUEST['fdgiro_dateD0'][$j]);
            $_REQUEST['fdgiro_cliring_dateD0'][$j]=getDateYmd($_REQUEST['fdgiro_cliring_dateD0'][$j]);

            if(($_REQUEST['fnpayment_methodD0'][$j]==0)||($_REQUEST['fnpayment_methodD0'][$j]==1)) {
                $fnstatus="0";
            } else {
                $fnstatus="1";
            }

            //cheque,giro
            if(($_REQUEST['fnpayment_methodD0'][$j]==0)||($_REQUEST['fnpayment_methodD0'][$j]==1)) {
                    $strSQL['Update txgiro2_hist'][]="
                                UPDATE txgiro2_hist SET fnstatus_giro=9
                                WHERE ftgiro_no='{$_REQUEST['ftgiro_noD0'][$j]}' AND ftdocument_no='{$_REQUEST['ftcvs_no']}'";
            //debit memo
            } else if ($_REQUEST['fnpayment_methodD0'][$j]==5) {
                    $strSQL['Update txdebit_memo_hdr'][]="
                                UPDATE txdebit_memo_hdr
                                SET fcoutstanding_amount=fcoutstanding_amount+".CCur($_REQUEST['fcamountUSDD0'][$j])." ,
                                fcdebit_amount=fcdebit_amount-".CCur($_REQUEST['fcamountUSDD0'][$j])."
                                        WHERE ftdm_no='{$_REQUEST['ftgiro_noD0'][$j]}'";

            } else if ($_REQUEST['fnpayment_methodD0'][$j]==9) {
                   $hit = (float)CCur($_REQUEST['fcamountUSDD0'][$j]);
                   $strSQL['Update txadvanced_payment'][]="UPDATE txadvanced_payment SET
                                fcused_amount=ifnull(fcused_amount,0) - {$hit},
                                fcoutstanding_amount=IFNULL(fcoutstanding_amount,0) + {$hit},
                                fnstatus = IF(((IFNULL(fcused_amount,0)- {$hit})=0),1,2)
                                WHERE fttrans_no='{$_REQUEST['ftgiro_noD0'][$j]}'";
            }
        }

        for($j=0; $j<count($_REQUEST['fnindexD1']); $j++) {
            
            if (($_REQUEST['ftpi_typeD1'][$j] == "HUTANG AWAL") OR ($_REQUEST['ftpi_typeD1'][$j] == "FAKTUR")) {
                $strSQL['Update txpurchase_invoice_hdr'][]="UPDATE txpurchase_invoice_hdr SET fnpayment_status = CASE WHEN (IFNULL(fcoutstanding_amount,0) + ".CCur($_REQUEST['fcpayment_amountUSDD1'][$j])." = fcamount) THEN 1 ELSE 2 END, fcoutstanding_amount = ifnull(fcoutstanding_amount,0) + ".CCur($_REQUEST['fcpayment_amountUSDD1'][$j]).", fcpayment_amount = IFNULL(fcpayment_amount,0) - ".CCur($_REQUEST['fcpayment_amountUSDD1'][$j]).", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE ftpurchase_invoice_no = '{$_REQUEST['ftpi_noD1'][$j]}'";

            } else if ($_REQUEST['ftpi_typeD1'][$j] == "RETUR") {
                $strSQL['Update txpurchase_retur_hdr'][]="UPDATE txpurchase_return_hdr SET fnpayment_status = CASE WHEN (IFNULL(fcoutstanding_amount,0) + ".CCur($_REQUEST['fcpayment_amountUSDD1'][$j])." = fcamount) THEN 1 ELSE 2 END, fcoutstanding_amount = ifnull(fcoutstanding_amount,0) + ". (CCur($_REQUEST['fcpayment_amountUSDD1'][$j]) * -1) .", fcpayment_amount = IFNULL(fcpayment_amount,0) - ". (CCur($_REQUEST['fcpayment_amountUSDD1'][$j]) * -1) .", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE ftpurchase_return_no = '{$_REQUEST['ftpi_noD1'][$j]}'";
            }      
                
        }

        // cek dan update status payment yang belum ada pembayaran
        $strSQL['Update txpurchase_invoice_hdr no payment status'][]=
                        "UPDATE txpurchase_invoice_hdr
                            SET fnpayment_status=1
                            WHERE fcpayment_amount=0 and fnstatus <> 9 and fnpayment_status <> 1 ";

        $_REQUEST['fdtrans_date']=CDateRev($_REQUEST['fdtrans_date']);

        break;
    }

    return $strSQL;
 }

 function CancelDataLast() {
        global $objAxpDB;
        $objAxpDB->Allow_Cancel(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
          case "batal":

                for($j=1; $j<count($_REQUEST['ftacc_noD0']); $j++) {
                      if ($_REQUEST['fnpayment_methodD0'][$j]==9) {
                               $hit = (float)CCur($_REQUEST['fcamountD0'][$j]);
                               $strSQL['Update txadvanced_payment last'][]="UPDATE txadvanced_payment SET
                                            fnstatus = IF(((IFNULL(fcused_amount,0))=0),1,2)
                                            WHERE fttrans_no='{$_REQUEST['ftgiro_noD0'][$j]}'";
                      }
                }

                break;
        }

        return $strSQL;
}