<?php
     function ValAR() {
           for($j=1; $j<count($_REQUEST['ftacc_noD0']); $j++) {
                
                  if($_REQUEST['fnpayment_methodD0'][$j]==6) {

                        $ssql="SELECT fnid
                                FROM txcredit_memo_hdr
                                WHERE fcoutstanding_amount>=".CCur($_REQUEST['fctotalD0'][$j])." AND
                                      ftcm_no='".$_REQUEST['ftgiro_noD0'][$j]."' and
                                        ftcustomer_code='".$_REQUEST['ftcustomer_code']."'";

                        $rs=mysql_query($ssql);
                        $r=mysql_fetch_assoc($rs);
                        if(!$r['fnid']) {
                            echo "<script>
                                      alert('Inputan credit memo tidak valid ...');
                                      window.history.back();
                                 </script>";
                                die("Inputan credit memo tidak valid ...");
                        }

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

                        $ssql="SELECT a.fttrans_no, IFNULL(a.fcoutstanding_amount,0) AS fcamount_ready
                                FROM txadvanced_receive 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);

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

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

                        if (str_replace(',','',$_REQUEST['fcamountD0'][$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 txgiro_hist
                                where   fnstatus_giro<>9 and
                                        ftdocument_no<>'".$_REQUEST['ftar_no']."'
                                        and ftgiro_no='".$_REQUEST['ftgiro_noD0'][$j]."'";

                        $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() {
        global $objAxpDB;
        $objAxpDB->Allow_Save(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
            case "simpan":
                ValAR();
                $_REQUEST['fdtrans_date']=CDate($_REQUEST['fdtrans_date']);
               
                $strSQL['Insert AR_HDR'][]=
                            "INSERT INTO txar_hdr  (fnid,ftar_no,ftbranch_code,
                                    fdtrans_date, ftcurrency_code, fcrate, ftcustomer_code,ftaddress,
                                    ftnpwp,fcsub_amount,fcamount, fcrate_adjustment,
                                    fcinvoice_amount,fcoutstanding_amount,ftremarks,
                                    fnstatus2,fnstatus,ftprint_counter,fnprint_status,
                                    ftcreated_user,fdcreated_date,ftmodified_user,
                                    fdmodified_date,fccredit_memo,fcother_cost,ftother_coa_code,fcother_cost2,
                                    fccm_min,fccm_add, ftRef_No)
                                SELECT '' AS fnid,'{$_REQUEST['ftar_no']}' AS ftar_no,'{$_SESSION['strbranch']}' AS ftbranch_code,
                                    '{$_REQUEST['fdtrans_date']}' AS fdtrans_date, '{$_REQUEST['ftcurrency_code']}' AS ftcurrency_code, '".CCur($_REQUEST['fcrate'])."' AS fcrate, '{$_REQUEST['ftcustomer_code']}' AS ftcustomer_code, '{$_REQUEST['ftaddress']}' AS ftaddress,
                                    '{$_REQUEST['ftnpwp']}' AS ftnpwp, '".CCur($_REQUEST['fcsub_amount'])."' AS fcsub_amount,'".CCur($_REQUEST['fcamount'])."' AS fcamount,'".CCur($_REQUEST['fcrate_adjustment'])."' AS fcrate_adjustment,
                                    '".CCur($_REQUEST['fcinvoice_amount'])."' AS fcinvoice_amount,'".CCur($_REQUEST['fcoutstanding_amount'])."' AS fcoutstanding_amount,'{$_REQUEST['ftremarks']}' AS ftremarks,
                                    '0' as fnstatus2,'1' AS fnstatus,'' AS ftprint_counter,'' AS fnprint_status,
                                    '{$_SESSION['username']}' AS ftcreated_user,NOW()  AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,
                                    NOW() AS fdmodified_date , '".CCur($_REQUEST['fccredit_memo'])."' AS fccredit_memo,'".CCur($_REQUEST['fcother_cost'])."' AS fcother_cost,'{$_REQUEST['ftother2_coa_code']}' AS ftother_coa_code,'".CCur($_REQUEST['fcother_cost2'])."' AS fcother_cost2,
                                    '".CCur($_REQUEST['fccm_min'])."','".CCur($_REQUEST['fccm_add'])."','{$_REQUEST['ftRef_No']}' AS ftRef_No";

                  $fnstatus="1";

                  for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                      $_REQUEST['fdgiro_dateD0'][$j]=getDateYmd($_REQUEST['fdgiro_dateD0'][$j]);
                      $_REQUEST['fdtrans_dateD0'][$j]=getDateYmd($_REQUEST['fdtrans_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";
                          $strSQL['Insert txgiro_hist'][]="
                            INSERT INTO txgiro_hist (
                                fnid,ftbranch_code,ftacc_no,ftacc_desc,
                                ftdocument_no,ftcustomer_code,ftcustomer_name,
                                ftCredit_NO,fnpayment_method,ftgiro_no,
                                fdgiro_date,fdgiro_cliring_date,fdtrans_date,
                                ftremarks,fnstatus_giro,fncategory,
                                fncategory2,fcamount,fccredit_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['ftar_no']}' AS ftdocument_no,'{$_REQUEST['ftcustomer_code']}' AS ftcustomer_code,'' AS ftcustomer_name,
                                '' AS ftCredit_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['fcamountD0'][$j])."' AS fcamount,'' AS fccredit_memo,'".CCur($_REQUEST['fcother_costD0'][$j])."' AS fcother_cost,
                                '' AS ftother_sign,'".CCur($_REQUEST['fctotalD0'][$j])."' AS fctotal,'".CCur($_REQUEST['fcrate'])."' 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 ,
                                'AR' AS fttype";

                      } else if ($_REQUEST['fnpayment_methodD0'][$j]==6) {
                               $hit = (float)CCur($_REQUEST['fcamountD0'][$j]);
                               $strSQL['Update txcredit_memo_hdr'][]="UPDATE txcredit_memo_hdr SET
                                            fcoutstanding_amount=fcoutstanding_amount - {$hit},
                                            fccredit_amount=IFNULL(fccredit_amount,0) + {$hit}
                                            WHERE ftcm_no='{$_REQUEST['ftgiro_noD0'][$j]}'";

                      } else if ($_REQUEST['fnpayment_methodD0'][$j]==9) {
                               $hit = (float)CCur($_REQUEST['fcamountD0'][$j]);
                               $strSQL['Update txadvanced_payment'][]="UPDATE txadvanced_receive 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 txar_det1'][]="INSERT INTO txar_det1(
                                                        fnid,ftbranch_code,ftar_no,
                                                        fnindex,fdtrans_date,ftacc_no,
                                                        ftacc_desc,ftcm_no,ftDP_No,
                                                        fnpayment_method,ftgiro_no,fdgiro_date,
                                                        fdgiro_cliring_date,ftremarks,fnstatus_giro,
                                                        fnstatus_giro2,fcamount,fcDP_Amount,
                                                        fccredit_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['ftar_no']}' AS ftar_no,
                                                        '{$j}' AS fnindex,'{$_REQUEST['fdtrans_dateD0'][$j]}' AS fdtrans_date,'{$_REQUEST['ftacc_noD0'][$j]}' AS ftacc_no,
                                                        '' AS ftacc_desc,'{$_REQUEST['ftcm_noD0'][$j]}' AS ftcm_no,'' AS ftDP_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['ftremarksD0'][$j]}' AS ftremarks,'{$fnstatus}' AS fnstatus_giro,
                                                        '' AS fnstatus_giro2,'".CCur($_REQUEST['fcamountD0'][$j])."' AS fcamount,'' AS fcDP_Amount,
                                                        '' AS fccredit_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
                                                        ";


                  }

                for($j=1; $j<=count($_REQUEST['fnindexD1']); $j++) {
                    //$_REQUEST['fdtrans_dateD1'][$j - 1]=getDateYmd($_REQUEST['fdtrans_dateD1'][$j - 1]);
                    if($_REQUEST['fcpayment_amountD1'][$j - 1]!=0) {
                        $strSQL['Insert txar_det'][]="INSERT INTO txar_det(
                                                        fnid,ftbranch_code,ftar_no,ftsales_type,
                                                        fnindex, ftsales_invoice_no, ftdo_no, fdtrans_date,
                                                        fdinvoice_date, fcamount,
                                                        fcoutstanding_amount, fcpayment_amount,
                                                        ftcurrency_code, fcrate_invoice, fcpayment_amount_base,
                                                        ftcreated_user,fdcreated_date,
                                                        ftmodified_user,fdmodified_date
                                                        )
                                                        SELECT
                                                        '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftar_no']}' AS ftar_no,'{$_REQUEST['ftsales_typeD1'][$j - 1]}' AS ftsales_type,
                                                        '$j' AS fnindex,'{$_REQUEST['ftsales_invoice_noD1'][$j - 1]}' AS ftsales_invoice_no,
                                                        '{$_REQUEST['ftdo_noD1'][$j - 1]}' AS ftdo_no,'{$_REQUEST['fdtrans_dateD1'][$j - 1]}' AS fdtrans_date,
                                                        '{$_REQUEST['fdtrans_dateD1'][$j - 1]}' AS fdinvoice_date, 
                                                        '".CCur($_REQUEST['fcamountD1'][$j - 1])."' AS fcamount,
                                                        '".CCur($_REQUEST['fcoutstanding_amountD1'][$j - 1])."' AS fcoutstanding_amount,'".CCur($_REQUEST['fcpayment_amountD1'][$j - 1])."' AS fcpayment_amount,
                                                        '{$_REQUEST['ftcurrency_code']}' AS ftcurrency_code,'".CCur($_REQUEST['fcrate_invoiceD1'][$j - 1])."' AS fcrate_invoice,'".CCur($_REQUEST['fcpayment_amount_baseD1'][$j - 1])."' AS fcpayment_amount_base,
                                                        '{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
                                                        '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date
                                                        ";

                            $hit = (float)CCur($_REQUEST['fcpayment_amountD1'][$j - 1]);
                            
                            // update invoice/retur payment and outstanding  
                            if((float)CCur($_REQUEST['fcoutstanding_amountD1'][$j - 1])==(float)CCur($_REQUEST['fcpayment_amountD1'][$j - 1])) {
                                $fnpayment_status = 3;
                            } else { 
                                $fnpayment_status = 2;
                            }    
                            
                            if ($_REQUEST['ftsales_typeD1'][$j - 1] == "PIUTANG AWAL") {
                                $strSQL['Update txsales_invoice_hdr'][]="UPDATE txsales_invoice_hdr SET fnpayment_status = {$fnpayment_status}, fcoutstanding_amount = ifnull(fcoutstanding_amount,0) - {$hit}, fcpayment_amount = IFNULL(fcpayment_amount,0) + ".CCur($_REQUEST['fcpayment_amountD1'][$j - 1]).", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE   ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_noD1'][$j - 1]}'";
                            
                            } else if ($_REQUEST['ftsales_typeD1'][$j - 1] == "FAKTUR") {
                                $ftso_no=RetRstField("SELECT ftso_no FROM txsales_invoice_hdr WHERE ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_noD1'][$j - 1]}' ");
                                $strSQL['Update txsales_order_hdr'][]="UPDATE txsales_order_hdr SET fnpayment_status = {$fnpayment_status}, fcoutstanding_amount = ifnull(fcoutstanding_amount,0) - {$hit}, fcpayment_amount = IFNULL(fcpayment_amount,0) + ".CCur($_REQUEST['fcpayment_amountD1'][$j - 1]).", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE ftso_no = '{$ftso_no}' ";
                                $strSQL['Update txsales_invoice_hdr'][]="UPDATE txsales_invoice_hdr SET fnpayment_status = {$fnpayment_status}, fcoutstanding_amount = ifnull(fcoutstanding_amount,0) - {$hit}, fcpayment_amount = IFNULL(fcpayment_amount,0) + ".CCur($_REQUEST['fcpayment_amountD1'][$j - 1]).", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE   ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_noD1'][$j - 1]}'";
                                
                            } else if ($_REQUEST['ftsales_typeD1'][$j - 1] == "ADJUSTMENT") {
                                $strSQL['Update txsales_invoice_adjustment_hdr'][]="UPDATE txsales_invoice_adjustment_hdr SET fnpayment_status = {$fnpayment_status}, fcoutstanding_amount = ifnull(fcoutstanding_amount,0) - {$hit}, fcpayment_amount = IFNULL(fcpayment_amount,0) + ".CCur($_REQUEST['fcpayment_amountD1'][$j - 1]).", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE  ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_noD1'][$j - 1]}'";
                                    
                            } else if ($_REQUEST['ftsales_typeD1'][$j - 1] == "RETUR") {
                                $strSQL['Update txsales_retur_hdr'][]="UPDATE txsales_return_hdr SET fnpayment_status = {$fnpayment_status}, fcoutstanding_amount = ifnull(fcoutstanding_amount,fcamount) - ({$hit} * -1), fcpayment_amount = IFNULL(fcpayment_amount,0) + ". (CCur($_REQUEST['fcpayment_amountD1'][$j - 1]) * -1 ) . ", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE  ftsales_return_no = '{$_REQUEST['ftsales_invoice_noD1'][$j - 1]}'";
                            }
                                
                    }
                }

                  $jj=0;
                  for($j=0; $j<count($_REQUEST['fnindexD3']); $j++) {
                      if((floatval($_REQUEST['fcamountD3'][$j])!=0)) {
                          $jj++;
                          $strSQL['Insert txar_cost'][]="
                                  INSERT INTO txar_cost(
                                      ftbranch_code, ftar_no,
                                      fnindex, ftcoa_code, ftdescription,
                                      fcamount,
                                      ftcreated_user, fdcreated_date,
                                      ftmodified_user, fdmodified_date)
                                  SELECT
                                      '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftar_no']}' AS ftar_no,
                                      '{$jj}' AS fnindex, '{$_REQUEST['ftcoa_codeD3'][$j]}' AS ftcoa_code, '{$_REQUEST['ftdescriptionD3'][$j]}' AS ftdescription,
                                      '".CCur($_REQUEST['fcamountD3'][$j])."' AS fcamount,
                                      '{$_SESSION['username']}' AS ftcreated_user, NOW() AS fdcreated_date,
                                      '{$_SESSION['username']}' AS ftmodified_user, NOW() AS fdmodified_date";
                      }
                  }

                  $strSQL['Update txar_hdr'][]="UPDATE txar_hdr SET fnstatus2='{$fnstatus}' WHERE ftar_no='{$_REQUEST['ftar_no']}'";

                  $_REQUEST['fdtrans_date']=CDateRev($_REQUEST['fdtrans_date']);
                
                  break;
        /*          
	case "simpan_penerimaan":


                            $_REQUEST['fdtrans_date']=CDate($_REQUEST['fdtrans_date']);
			      $strSQL['Insert AR_HDR'][]="UPDATE  txar_hdr
                                                                SET fdtrans_date='{$_REQUEST['fdtrans_date']}',
								ftcustomer_code='{$_REQUEST['ftcustomer_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'])."',
                                                                fcrate_adjustment='".CCur($_REQUEST['fcrate_adjustment'])."',
                                                                ftremarks='{$_REQUEST['ftremarks']}',
                                                                fnstatus2='1',fnstatus='2',ftprint_counter='',fnprint_status='',
                                                                ftmodified_user='{$_SESSION['username']}',
                                                                fdmodified_date=NOW(),fccredit_memo='".CCur($_REQUEST['fccredit_memo'])."',
                                                                fcother_cost='".CCur($_REQUEST['fcother_cost'])."',
                                                                fcother_cost2='".CCur($_REQUEST['fcother_cost2'])."',
                                                                fccm_min='".CCur($_REQUEST['fccm_min'])."',
                                                                fccm_add='".CCur($_REQUEST['fccm_add'])."'
                                                            WHERE ftar_no='{$_REQUEST['ftar_no']}'";


                for($j=0; $j<count($_REQUEST['ftsales_invoice_noD1']); $j++) {
                    $_REQUEST['fdtrans_dateD1']=getDateYmd($_REQUEST['fdtrans_dateD1']);
                    if($_REQUEST['fcpayment_amountD1'][$j]!=0) {
                             $jj=$j+1;
                             $strSQL['Insert txar_det'][]="INSERT INTO txar_det(
                                                                fnid,ftbranch_code,ftar_no,
                                                                fnindex,ftsales_invoice_no,fdinvoice_date,
                                                                ftdo_no,fdtrans_date,fcamount,
                                                                fcoutstanding_amount,fcpayment_amount,
                                                                ftcurrency_code, fcpayment_amount_base, ftcreated_user,
                                                                fdcreated_date,ftmodified_user,fdmodified_date
                                                                )
                                                                SELECT
                                                                '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftar_no']}' AS ftar_no,
                                                                '{$jj}' AS fnindex,'{$_REQUEST['ftsales_invoice_noD1'][$j]}' AS ftsales_invoice_no,'{$_REQUEST['fdtrans_dateD1'][$j]}' AS fdinvoice_date,
                                                                '{$_REQUEST['ftdo_noD1'][$j]}' AS ftdo_no,'{$_REQUEST['fdtrans_dateD1'][$j]}' AS fdtrans_date,'".CCur($_REQUEST['fcamountD1'][$j])."' AS fcamount,
                                                                '".CCur($_REQUEST['fcoutstanding_amountD1'][$j])."' AS fcoutstanding_amount,'".CCur($_REQUEST['fcpayment_amountD1'][$j])."' AS fcpayment_amount,
                                                                '{$_REQUEST['ftcurrency_code']}' AS ftcurrency_code,'".CCur($_REQUEST['fcpayment_amount_baseD1'][$j])."' AS fcpayment_amount_base,'{$_SESSION['username']}' AS ftcreated_user,
                                                                NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date
                                                            ";
                    } 

                }
                break; */
        }

         $strSQL['Insert Update Giro'][]="update txar_hdr set
                                                ftdescription=ff06_desc_giro(ftar_no)
                                                where ftar_no='{$_REQUEST['ftar_no']}' ";

//       pr($strSQL);
//        die("mn");
        return  $strSQL;
     }

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

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

                $ftar_no_new="{$_REQUEST['ftar_no']}{$str1}";

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

                $strSQL["Revisi"][]="update txar_det
                        set ftar_no='{$ftar_no_new}'
                        where ftar_no='{$_REQUEST['ftar_no']}'";

                $strSQL["Revisi"][]="update txar_det1
                        set ftar_no='{$ftar_no_new}'
                        where ftar_no='{$_REQUEST['ftar_no']}'";

                $strSQL["Revisi"][]="update txar_det2
                        set ftar_no='{$ftar_no_new}'
                        where ftar_no='{$_REQUEST['ftar_no']}'";

                $strSQL['Revisi'][]="update txar_cost
                        set ftar_no='{$ftar_no_new}'
                        where ftar_no='{$_REQUEST['ftar_no']}'";

                $strSQL["Revisi"][]="update txgiro_hist
                        set ftdocument_no='{$ftar_no_new}', fnstatus_giro='9'
                        where ftdocument_no='{$_REQUEST['ftar_no']}' and fttype='AR'";

                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";
                    }

                    //credit memo
                    if($_REQUEST['fnpayment_methodD0'][$j]==6) {
                            $strSQL['Update txcredit_memo_hdr'][]="
                                        UPDATE txcredit_memo_hdr
                                        SET fcoutstanding_amount=fcoutstanding_amount+".CCur($_REQUEST['fcamountD0'][$j])." ,
                                            fccredit_amount=fccredit_amount-".CCur($_REQUEST['fcamountD0'][$j])."
                                        WHERE ftcm_no='{$_REQUEST['ftgiro_noD0'][$j]}'";

                    } else if ($_REQUEST['fnpayment_methodD0'][$j]==9) {
                            // query update ada di revisi last
                    }
                }
          
                // Simpan ulang
                ValAR();
                $_REQUEST['fdtrans_date']=getDateYmd($_REQUEST['fdtrans_date']);

                $strSQL['Insert AR_HDR'][]=
                            "REPLACE INTO txar_hdr  (fnid,ftar_no,ftbranch_code,
                                    fdtrans_date, ftcurrency_code, fcrate, ftcustomer_code,ftaddress,
                                    ftnpwp,fcsub_amount,fcamount, fcrate_adjustment,
                                    fcinvoice_amount,fcoutstanding_amount,ftremarks,
                                    fnstatus2,fnstatus,ftprint_counter,fnprint_status,
                                    ftcreated_user,fdcreated_date,ftmodified_user,
                                    fdmodified_date,fccredit_memo,fcother_cost,ftother_coa_code,fcother_cost2,
                                    fccm_min,fccm_add, ftRef_No)
                            SELECT '' AS fnid,'{$_REQUEST['ftar_no']}' AS ftar_no,'{$_SESSION['strbranch']}' AS ftbranch_code,
                                    '{$_REQUEST['fdtrans_date']}' AS fdtrans_date, '{$_REQUEST['ftcurrency_code']}' AS ftcurrency_code, '".CCur($_REQUEST['fcrate'])."' AS fcrate, '{$_REQUEST['ftcustomer_code']}' AS ftcustomer_code, '{$_REQUEST['ftaddress']}' AS ftaddress,
                                    '{$_REQUEST['ftnpwp']}' AS ftnpwp, '".CCur($_REQUEST['fcsub_amount'])."' AS fcsub_amount,'".CCur($_REQUEST['fcamount'])."' AS fcamount,'".CCur($_REQUEST['fcrate_adjustment'])."' AS fcrate_adjustment,
                                    '".CCur($_REQUEST['fcinvoice_amount'])."' AS fcinvoice_amount,'".CCur($_REQUEST['fcoutstanding_amount'])."' AS fcoutstanding_amount,'{$_REQUEST['ftremarks']}' AS ftremarks,
                                    '0' as fnstatus2,'1' AS fnstatus,'' AS ftprint_counter,'' AS fnprint_status,
                                    '{$_SESSION['username']}' AS ftcreated_user,NOW()  AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,
                                    NOW() AS fdmodified_date , '".CCur($_REQUEST['fccredit_memo'])."' AS fccredit_memo,'".CCur($_REQUEST['fcother_cost'])."' AS fcother_cost,'{$_REQUEST['ftother2_coa_code']}' AS ftother_coa_code,'".CCur($_REQUEST['fcother_cost2'])."' AS fcother_cost2,
                                    '".CCur($_REQUEST['fccm_min'])."','".CCur($_REQUEST['fccm_add'])."', '{$_REQUEST['ftRef_No']}' AS ftRef_No";

                $fnstatus="1";

                for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                    //$_REQUEST['fdgiro_dateD0'][$j]=getDateYmd($_REQUEST['fdgiro_dateD0'][$j]);
                    //$_REQUEST['fdtrans_dateD0'][$j]=getDateYmd($_REQUEST['fdtrans_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";
                        $strSQL['Insert txgiro_hist'][]="
                          INSERT INTO txgiro_hist (
                              fnid,ftbranch_code,ftacc_no,ftacc_desc,
                              ftdocument_no,ftcustomer_code,ftcustomer_name,
                              ftCredit_NO,fnpayment_method,ftgiro_no,
                              fdgiro_date,fdgiro_cliring_date,fdtrans_date,
                              ftremarks,fnstatus_giro,fncategory,
                              fncategory2,fcamount,fccredit_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['ftar_no']}' AS ftdocument_no,'{$_REQUEST['ftcustomer_code']}' AS ftcustomer_code,'' AS ftcustomer_name,
                              '' AS ftCredit_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['fcamountD0'][$j])."' AS fcamount,'' AS fccredit_memo,'".CCur($_REQUEST['fcother_costD0'][$j])."' AS fcother_cost,
                              '' AS ftother_sign,'".CCur($_REQUEST['fctotalD0'][$j])."' AS fctotal,'".CCur($_REQUEST['fcrate'])."' 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 ,
                              'AR' AS fttype";

                    }  else if ($_REQUEST['fnpayment_methodD0'][$j]==6) {
                                 $hit = (float)CCur($_REQUEST['fcamountD0'][$j]);
                                 $strSQL['Update txcredit_memo_hdr'][]="UPDATE txcredit_memo_hdr SET
                                              fcoutstanding_amount=fcoutstanding_amount - {$hit},
                                              fccredit_amount=IFNULL(fccredit_amount,0) + {$hit}
                                              WHERE ftcm_no='{$_REQUEST['ftgiro_noD0'][$j]}'";

                    } else if ($_REQUEST['fnpayment_methodD0'][$j]==9) {
                             $hit = (float)CCur($_REQUEST['fcamountD0'][$j]);
                             $strSQL['Update txadvanced_payment'][]="UPDATE txadvanced_receive 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 txar_det1'][]="INSERT INTO txar_det1(
                                                      fnid,ftbranch_code,ftar_no,
                                                      fnindex,fdtrans_date,ftacc_no,
                                                      ftacc_desc,ftcm_no,ftDP_No,
                                                      fnpayment_method,ftgiro_no,fdgiro_date,
                                                      fdgiro_cliring_date,ftremarks,fnstatus_giro,
                                                      fnstatus_giro2,fcamount,fcDP_Amount,
                                                      fccredit_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['ftar_no']}' AS ftar_no,
                                                      '{$j}' AS fnindex,'{$_REQUEST['fdtrans_dateD0'][$j]}' AS fdtrans_date,'{$_REQUEST['ftacc_noD0'][$j]}' AS ftacc_no,
                                                      '' AS ftacc_desc,'{$_REQUEST['ftcm_noD0'][$j]}' AS ftcm_no,'' AS ftDP_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['ftremarksD0'][$j]}' AS ftremarks,'{$fnstatus}' AS fnstatus_giro,
                                                      '' AS fnstatus_giro2,'".CCur($_REQUEST['fcamountD0'][$j])."' AS fcamount,'' AS fcDP_Amount,
                                                      '' AS fccredit_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
                                                      ";
                }

                for($j=1; $j<=count($_REQUEST['fnindexD1']); $j++) {
                    //$_REQUEST['fdtrans_dateD1'][$j - 1]=getDateYmd($_REQUEST['fdtrans_dateD1'][$j - 1]);
                    if($_REQUEST['fcpayment_amountD1'][$j - 1]!=0) {
                        $strSQL['Insert txar_det'][]="INSERT INTO txar_det(
                                                          fnid,ftbranch_code,ftar_no, ftsales_type,
                                                          fnindex, ftsales_invoice_no, ftdo_no, fdtrans_date,
                                                          fdinvoice_date, fcamount,
                                                          fcoutstanding_amount, fcpayment_amount,
                                                          ftcurrency_code, fcpayment_amount_base,
                                                          ftcreated_user,fdcreated_date,
                                                          ftmodified_user,fdmodified_date
                                                          )
                                                          SELECT
                                                          '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftar_no']}' AS ftar_no,'{$_REQUEST['ftsales_typeD1'][$j - 1]}' AS ftsales_type,
                                                          '$j' AS fnindex,'{$_REQUEST['ftsales_invoice_noD1'][$j - 1]}' AS ftsales_invoice_no,
                                                          '{$_REQUEST['ftdo_noD1'][$j - 1]}' AS ftdo_no,'{$_REQUEST['fdtrans_dateD1'][$j - 1]}' AS fdtrans_date,
                                                          '{$_REQUEST['fdtrans_dateD1'][$j - 1]}' AS fdinvoice_date, '"
                                                          .CCur($_REQUEST['fcamountD1'][$j - 1])."' AS fcamount,
                                                          '".CCur($_REQUEST['fcoutstanding_amountD1'][$j - 1])."' AS fcoutstanding_amount,'".CCur($_REQUEST['fcpayment_amountD1'][$j - 1])."' AS fcpayment_amount,
                                                          '{$_REQUEST['ftcurrency_code']}' AS ftcurrency_code,'".CCur($_REQUEST['fcpayment_amount_baseD1'][$j - 1])."' AS fcpayment_amount_base,
                                                          '{$_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['fnindexD3']); $j++) {
                    if((floatval($_REQUEST['fcamountD3'][$j])!=0)) {
                        $jj++;
                        $strSQL['Insert txar_cost'][]="
                                INSERT INTO txar_cost(
                                    ftbranch_code, ftar_no,
                                    fnindex, ftcoa_code, ftdescription,
                                    fcamount,
                                    ftcreated_user, fdcreated_date,
                                    ftmodified_user, fdmodified_date)
                                SELECT
                                    '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftar_no']}' AS ftar_no,
                                    '{$jj}' AS fnindex, '{$_REQUEST['ftcoa_codeD3'][$j]}' AS ftcoa_code, '{$_REQUEST['ftdescriptionD3'][$j]}' AS ftdescription,
                                    '".CCur($_REQUEST['fcamountD3'][$j])."' AS fcamount,
                                    '{$_SESSION['username']}' AS ftcreated_user, NOW() AS fdcreated_date,
                                    '{$_SESSION['username']}' AS ftmodified_user, NOW() AS fdmodified_date";
                    }
                }

                $strSQL['Update txar_hdr'][]="UPDATE txar_hdr SET fnstatus2='{$fnstatus}' WHERE ftar_no='{$_REQUEST['ftar_no']}'";

                $strSQL['Insert Update Giro'][]="update txar_hdr set
                                                  ftdescription=ff06_desc_giro(ftar_no)
                                                  where ftar_no='{$_REQUEST['ftar_no']}' ";


                // update amount payment and outstanding at sales order        
                $strSQL["Update txsales_order_hdr "][] = "
                                UPDATE txsales_order_hdr e, txsales_invoice_hdr a, txar_det d
                                SET e.fcpayment_amount = (SELECT IFNULL(SUM(b.fcpayment_amount),0) AS totalpayment
                                                      FROM txar_det b INNER JOIN txar_hdr c
                                                      ON b.ftar_no = c.ftar_no AND c.fnstatus = 1
                                                      WHERE a.ftsales_invoice_no = b.ftsales_invoice_no
                                                      ) ,
                                    e.fcoutstanding_amount = a.fcamount - (SELECT IFNULL(SUM(b.fcpayment_amount),0) AS totalpayment
                                                      FROM txar_det b INNER JOIN txar_hdr c
                                                      ON b.ftar_no = c.ftar_no AND c.fnstatus = 1
                                                      WHERE a.ftsales_invoice_no = b.ftsales_invoice_no
                                                      )
                                WHERE a.ftso_no = e.ftso_no AND a.ftsales_invoice_no = d.ftsales_invoice_no 
                                        AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'";

                // update amount payment and outstanding at sales invoice        
                $strSQL["Update txsales_invoice_hdr "][] = "
                              UPDATE txsales_invoice_hdr a, txar_det d
                              SET a.fcpayment_amount = (SELECT IFNULL(SUM(b.fcpayment_amount),0) AS totalpayment
                                                      FROM txar_det b INNER JOIN txar_hdr c
                                                      ON b.ftar_no = c.ftar_no AND c.fnstatus = 1
                                                      WHERE a.ftsales_invoice_no = b.ftsales_invoice_no
                                                      ) ,
                              a.fcoutstanding_amount = a.fcamount - (SELECT IFNULL(SUM(b.fcpayment_amount),0) AS totalpayment
                                                      FROM txar_det b INNER JOIN txar_hdr c
                                                      ON b.ftar_no = c.ftar_no AND c.fnstatus = 1
                                                      WHERE a.ftsales_invoice_no = b.ftsales_invoice_no
                                                      )
                              WHERE a.ftsales_invoice_no = d.ftsales_invoice_no AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'";
              
                // update amount payment and outstanding at sales invoice adjustment      
                $strSQL["Update txsales_invoice_adjustment_hdr "][] = "
                              UPDATE txsales_invoice_adjustment_hdr a, txar_det d
                              SET a.fcpayment_amount = (SELECT IFNULL(SUM(b.fcpayment_amount),0) AS totalpayment
                                                      FROM txar_det b INNER JOIN txar_hdr c
                                                      ON b.ftar_no = c.ftar_no AND c.fnstatus = 1
                                                      WHERE a.ftsales_invoice_no = b.ftsales_invoice_no
                                                      ) ,
                              a.fcoutstanding_amount = a.fcamount - (SELECT IFNULL(SUM(b.fcpayment_amount),0) AS totalpayment
                                                      FROM txar_det b INNER JOIN txar_hdr c
                                                      ON b.ftar_no = c.ftar_no AND c.fnstatus = 1
                                                      WHERE a.ftsales_invoice_no = b.ftsales_invoice_no
                                                      )
                              WHERE a.ftsales_invoice_no = d.ftsales_invoice_no AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'";

                // update amount payment and outstanding at sales return      
                $strSQL["Update txsales_retur_hdr "][] = "
                              UPDATE txsales_return_hdr a, txar_det d
                              SET a.fcpayment_amount = (SELECT (IFNULL(SUM(b.fcpayment_amount),0) * -1) AS totalpayment
                                                      FROM txar_det b INNER JOIN txar_hdr c
                                                      ON b.ftar_no = c.ftar_no AND c.fnstatus = 1
                                                      WHERE a.ftsales_return_no = b.ftsales_invoice_no
                                                      ) ,
                              a.fcoutstanding_amount = a.fcamount - (SELECT (IFNULL(SUM(b.fcpayment_amount),0) * -1) AS totalpayment
                                                      FROM txar_det b INNER JOIN txar_hdr c
                                                      ON b.ftar_no = c.ftar_no AND c.fnstatus = 1
                                                      WHERE a.ftsales_return_no = b.ftsales_invoice_no
                                                      )
                              WHERE a.ftsales_return_no = d.ftsales_invoice_no AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'";

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

                // Call Function Revisi General Ledger
                if(function_exists("RevisiGL")) {
                    $m = RevisiGL($ftar_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 sales order
                $strSQL["Update txsales_invoice_hdr"][] ="UPDATE txsales_order_hdr e, txsales_invoice_hdr a, txar_det d
                            SET e.fnpayment_status = 1
                            WHERE a.ftso_no = e.ftso_no AND a.ftsales_invoice_no = d.ftsales_invoice_no 
                                    AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'
                                    AND a.fcoutstanding_amount = a.fcamount ";

               $strSQL["Update txsales_invoice_hdr"][] ="UPDATE txsales_order_hdr e, txsales_invoice_hdr a, txar_det d
                            SET e.fnpayment_status = 2
                            WHERE a.ftso_no = e.ftso_no AND a.ftsales_invoice_no = d.ftsales_invoice_no 
                                    AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'
                                    AND a.fcoutstanding_amount > 0 and a.fcoutstanding_amount <> a.fcamount ";

               $strSQL["Update txsales_invoice_hdr"][] ="UPDATE txsales_order_hdr e, txsales_invoice_hdr a, txar_det d
                            SET e.fnpayment_status = 3
                            WHERE a.ftso_no = e.ftso_no AND a.ftsales_invoice_no = d.ftsales_invoice_no 
                                    AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'
                                    AND a.fcoutstanding_amount = 0 ";
                            
                // update status pembayaran sales invoice
                $strSQL["Update txsales_invoice_hdr"][] ="UPDATE txsales_invoice_hdr a, txar_det d
                            SET a.fnpayment_status = 1
                            WHERE a.ftsales_invoice_no = d.ftsales_invoice_no AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'
                                    AND a.fcoutstanding_amount = a.fcamount ";

               $strSQL["Update txsales_invoice_hdr"][] ="UPDATE txsales_invoice_hdr a, txar_det d
                            SET a.fnpayment_status = 2
                            WHERE a.ftsales_invoice_no = d.ftsales_invoice_no AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'
                                    AND a.fcoutstanding_amount > 0 and a.fcoutstanding_amount <> a.fcamount ";

               $strSQL["Update txsales_invoice_hdr"][] ="UPDATE txsales_invoice_hdr a, txar_det d
                            SET a.fnpayment_status = 3
                            WHERE a.ftsales_invoice_no = d.ftsales_invoice_no AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'
                                    AND a.fcoutstanding_amount = 0 ";
                            
                // update status pembayaran sales invoice adjustment
                $strSQL["Update txsales_invoice_hdr"][] ="UPDATE txsales_invoice_adjustment_hdr a, txar_det d
                            SET a.fnpayment_status = 1
                            WHERE a.ftsales_invoice_no = d.ftsales_invoice_no AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'
                                    AND a.fcoutstanding_amount = a.fcamount ";

               $strSQL["Update txsales_invoice_hdr"][] ="UPDATE txsales_invoice_adjustment_hdr a, txar_det d
                            SET a.fnpayment_status = 2
                            WHERE a.ftsales_invoice_no = d.ftsales_invoice_no AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'
                                    AND a.fcoutstanding_amount > 0 and a.fcoutstanding_amount <> a.fcamount ";

               $strSQL["Update txsales_invoice_hdr"][] ="UPDATE txsales_invoice_adjustment_hdr a, txar_det d
                            SET a.fnpayment_status = 3
                            WHERE a.ftsales_invoice_no = d.ftsales_invoice_no AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'
                                    AND a.fcoutstanding_amount = 0 ";       
                            
                // update status pembayaran sales return
                $strSQL["Update txsales_return_hdr"][] ="UPDATE txsales_return_hdr a, txar_det d
                            SET a.fnpayment_status = 1
                            WHERE a.ftsales_return_no = d.ftsales_invoice_no AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'
                                    AND a.fcoutstanding_amount = a.fcamount ";

               $strSQL["Update txsales_return_hdr"][] ="UPDATE txsales_return_hdr a, txar_det d
                            SET a.fnpayment_status = 2
                            WHERE a.ftsales_return_no = d.ftsales_invoice_no AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'
                                    AND a.fcoutstanding_amount > 0 and a.fcoutstanding_amount <> a.fcamount ";

               $strSQL["Update txsales_return_hdr"][] ="UPDATE txsales_return_hdr a, txar_det d
                            SET a.fnpayment_status = 3
                            WHERE a.ftsales_return_no = d.ftsales_invoice_no AND d.ftar_no LIKE '{$_REQUEST['ftar_no']}%'
                                    AND a.fcoutstanding_amount = 0 ";                

               $strSQL["Update txadvanced_receive"][] ="
                            UPDATE txadvanced_receive a LEFT JOIN
                            (
                            SELECT SUM(b.fcamount) AS fcamount,ftgiro_no
                            FROM txar_hdr a INNER JOIN txar_det1 b ON a.ftar_no = b.ftar_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 txar_det1 c on a.fttrans_no = c.ftgiro_no AND c.ftar_no LIKE '{$_REQUEST['ftar_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 FinalData() {
        global $objAxpDB;
        $objAxpDB->Allow_Final(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
            
        }
    }

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

                $_REQUEST['fdtrans_date']=getDateYmd($_REQUEST['fdtrans_date']);
                $strSQL['Update AR_HDR'][]="UPDATE txar_hdr SET fnstatus=9,ftmodified_user='{$_SESSION['username']}',fdmodified_date=NOW() WHERE fnid='{$_REQUEST['id']}'";
                $strSQL['Update AR_DET1'][]="UPDATE txar_det1 SET fnstatus_giro=9,ftmodified_user='{$_SESSION['username']}',fdmodified_date=NOW() WHERE ftar_no ='{$_REQUEST['ftar_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 txgiro_hist'][]="
                                      UPDATE txgiro_hist SET fnstatus_giro=9
                                      WHERE ftgiro_no='{$_REQUEST['ftgiro_noD0'][$j]}' AND ftdocument_no='{$_REQUEST['ftar_no']}'";
                    //credit memo
                    } else if ($_REQUEST['fnpayment_methodD0'][$j]==6) {
                          $strSQL['Update txcredit_memo_hdr'][]="
                                      UPDATE txcredit_memo_hdr
                                      SET fcoutstanding_amount=fcoutstanding_amount+".CCur($_REQUEST['fcamountD0'][$j])." ,
                                      fccredit_amount=fccredit_amount-".CCur($_REQUEST['fcamountD0'][$j])."
                                              WHERE ftcm_no='{$_REQUEST['ftgiro_noD0'][$j]}'";

                    } else if ($_REQUEST['fnpayment_methodD0'][$j]==9) {
                             $hit = (float)CCur($_REQUEST['fcamountD0'][$j]);
                             $strSQL['Update txadvanced_receive'][]="UPDATE txadvanced_receive 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]}'";
                    }

                }
              
                if($_REQUEST['fnstatus2']==2 || $_REQUEST['fnstatus']==2) {
                    for($j=0; $j<count($_REQUEST['ftsales_invoice_noD1']); $j++) {
                        if((float)CCur($_REQUEST['fcpayment_amountD1'][$j])!=0) {
                            $jj=$j+1;
                            $hit = (float)CCur($_REQUEST['fcoutstanding_amountD1'][$j]) + (float)CCur($_REQUEST['fcpayment_amountD1'][$j]);
                            $strSQL['Update txsales_invoice_hdr'][]="UPDATE txsales_invoice_hdr SET fnpayment_status = 2, fcoutstanding_amount =fcoutstanding_amount+".CCur($_REQUEST['fcpayment_amountD1'][$j]).", fcpayment_amount = IFNULL(fcpayment_amount,0) - ".CCur($_REQUEST['fcpayment_amountD1'][$j]).", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_noD1'][$j]}'";                                    
                            $strSQL['Update txsales_invoice_hdr'][]="UPDATE txsales_invoice_hdr SET fnpayment_status=1 WHERE  fcpayment_amount=0 AND  ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_noD1'][$j]}'";
                        }
                    }
                }

                for($j=0; $j<count($_REQUEST['fnindexD1']); $j++) {
                    //$strSQL['Update txsales_invoice_hdr'][]="UPDATE txsales_invoice_hdr SET fnpayment_status = CASE WHEN (IFNULL(fcoutstanding_amount,0) + ".CCur($_REQUEST['fcpayment_amountD1'][$j])." = fcamount) THEN 1 ELSE 2 END, fcoutstanding_amount = IFNULL(fcoutstanding_amount,0) + ".CCur($_REQUEST['fcpayment_amountD1'][$j]).", fcpayment_amount = IFNULL(fcpayment_amount,0) - ".CCur($_REQUEST['fcpayment_amountD1'][$j]).", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE   ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_noD1'][$j]}'";
                
                    if (($_REQUEST['ftsales_typeD1'][$j] == "PIUTANG AWAL") OR ($_REQUEST['ftsales_typeD1'][$j] == "FAKTUR")) {
                        $strSQL['Update txsales_invoice_hdr'][]="UPDATE txsales_invoice_hdr SET fnpayment_status = CASE WHEN (IFNULL(fcoutstanding_amount,0) + ".CCur($_REQUEST['fcpayment_amountD1'][$j])." = fcamount) THEN 1 ELSE 2 END, fcoutstanding_amount = ifnull(fcoutstanding_amount,0) + ".CCur($_REQUEST['fcpayment_amountD1'][$j]).", fcpayment_amount = IFNULL(fcpayment_amount,0) - ".CCur($_REQUEST['fcpayment_amountD1'][$j]).", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_noD1'][$j]}'";

                    } else if ($_REQUEST['ftsales_typeD1'][$j] == "ADJUSTMENT") {
                        $strSQL['Update txsales_invoice_adjustment_hdr'][]="UPDATE txsales_invoice_adjustment_hdr SET fnpayment_status = CASE WHEN (IFNULL(fcoutstanding_amount,0) + ".CCur($_REQUEST['fcpayment_amountD1'][$j])." = fcamount) THEN 1 ELSE 2 END, fcoutstanding_amount = ifnull(fcoutstanding_amount,0) + ".CCur($_REQUEST['fcpayment_amountD1'][$j]).", fcpayment_amount = IFNULL(fcpayment_amount,0) - ".CCur($_REQUEST['fcpayment_amountD1'][$j]).", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_noD1'][$j]}'";
                            
                    } else if ($_REQUEST['ftsales_typeD1'][$j] == "RETUR") {
                        $strSQL['Update txsales_retur_hdr'][]="UPDATE txsales_return_hdr SET fnpayment_status = CASE WHEN (IFNULL(fcoutstanding_amount,0) + ".CCur($_REQUEST['fcpayment_amountD1'][$j])." = fcamount) THEN 1 ELSE 2 END, fcoutstanding_amount = ifnull(fcoutstanding_amount,0) + ". (CCur($_REQUEST['fcpayment_amountD1'][$j]) * -1) .", fcpayment_amount = IFNULL(fcpayment_amount,0) - ". (CCur($_REQUEST['fcpayment_amountD1'][$j]) * -1) .", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE ftsales_return_no = '{$_REQUEST['ftsales_invoice_noD1'][$j]}'";
                    }      

                }

                $_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_receive last'][]="UPDATE txadvanced_receive SET
                                            fnstatus = IF(((IFNULL(fcused_amount,0))=0),1,2)
                                            WHERE fttrans_no='{$_REQUEST['ftgiro_noD0'][$j]}'";
                      }
                }
              
                break;
        }

        return $strSQL;
    }