<?php
    #pr($_REQUEST); die("mm");
     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['ftgiro_noD0'][$j]!="") {
                      $ssql="select fnid
                            from txgiro_hist
                            where ftdocument_no<>'".$_REQUEST['ftar_no']."' and  fnstatus_giro<>9 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']=getDateYmd($_REQUEST['fdtrans_date']);
          $strSQL['Insert AR_HDR'][]="REPLACE INTO txar_hdr  (fnid,ftar_no,ftbranch_code,
                                      fdtrans_date,ftcustomer_code,ftaddress,
                                      ftnpwp,fcsub_amount,fcamount,
                                      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)
                                      SELECT '' AS fnid,'{$_REQUEST['ftar_no']}' AS ftar_no,'{$_SESSION['strbranch']}' AS ftbranch_code,
                                      '{$_REQUEST['fdtrans_date']}' AS fdtrans_date,'{$_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['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";


                  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 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,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,'' 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";
                      }
                      //credit memo
                      elseif($_REQUEST['fnpayment_methodD0'][$j]==6) {



                      }

                      $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=0; $j<count($_REQUEST['ftsales_invoice_noD']); $j++) {

                     if($_REQUEST['fcpayment_amountD'][$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,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_noD'][$j]}' AS ftsales_invoice_no,'{$_REQUEST['fdtrans_dateD'][$j]}' AS fdinvoice_date,
                                                                '{$_REQUEST['ftdo_noD'][$j]}' AS ftdo_no,'{$_REQUEST['fdtrans_dateD'][$j]}' AS fdtrans_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
                                                            ";

                                $hit = (float)CCur($_REQUEST['fcoutstanding_amountD'][$j]) - (float)CCur($_REQUEST['fcpayment_amountD'][$j]);
                                /*if((float)CCur($_REQUEST['fcoutstanding_amountD'][$j])==(float)CCur($_REQUEST['fcpayment_amountD'][$j])) {
                                    $strSQL['Update txsales_invoice_hdr'][]="UPDATE txsales_invoice_hdr SET fnpayment_status = 3, fcoutstanding_amount = {$hit}, fcpayment_amount = IFNULL(fcpayment_amount,0) + ".CCur($_REQUEST['fcpayment_amountD'][$j]).", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE   ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_noD'][$j]}'";
                                }
                                else {
                                    $strSQL['Update txsales_invoice_hdr'][]="UPDATE txsales_invoice_hdr SET fnpayment_status = 2, fcoutstanding_amount = {$hit}, fcpayment_amount = IFNULL(fcpayment_amount,0) + ".CCur($_REQUEST['fcpayment_amountD'][$j]).", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE   ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_noD'][$j]}'";
                                }*/

                    }

                for($j=1; $j<=count($_REQUEST['fnindexD2']); $j++) {
                    if($_REQUEST['fccm_paymentD2'][$j]!=0) {
                        $strSQL['Insert txar_cm'][]="INSERT INTO txar_cm(
                                                        fnid,ftbranch_code,ftar_no,
                                                        fnindex,ftcm_no,fccm_amount,
                                                        fccm_outstanding,fccm_payment,
                                                        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['ftcm_noD2'][$j]}' AS ftcm_no,'".CCur($_REQUEST['fccm_amountD2'][$j])."' AS fccm_amount,
                                                        '".CCur($_REQUEST['fccm_outstandingD2'][$j])."' AS fccm_outstanding,
                                                        '".CCur($_REQUEST['fccm_paymentD2'][$j])."' AS fccm_payment,
                                                         '{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
                                                         '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date
                                                        ";
                    }

                  }



                  }
                  break;
			case "simpan_bank":

                ValAR();
                $_REQUEST['fdtrans_date']=getDateYmd($_REQUEST['fdtrans_date']);


                $strSQL['Insert AR_HDR'][]="REPLACE INTO txar_hdr  (fnid,ftar_no,ftbranch_code,
                                                    fdtrans_date,ftcustomer_code,ftaddress,
                                                    ftnpwp,fcsub_amount,fcamount,
                                                    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)
                                                SELECT '' AS fnid,'{$_REQUEST['ftar_no']}' AS ftar_no,'{$_SESSION['strbranch']}' AS ftbranch_code,
                                                    '{$_REQUEST['fdtrans_date']}' AS fdtrans_date,'{$_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['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'])."'";

//              $strSQL['Insert AR_HDR'][]="UPDATE  txar_hdr
//												SET ftfinal_bank_user='{$_SESSION['username']}',fdfinal_bank_date=NOW(),
//													ftjournal_no='{$_REQUEST['ftjournal_no']}',
//                                                    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'])."',
//													ftremarks='{$_REQUEST['ftremarks']}',
//                                                    fnstatus2='0',fnstatus='1',ftprint_counter='',fnprint_status='',
//													ftmodified_user='{$_SESSION['username']}',
//                                                    fdmodified_date=NOW(),fccredit_memo='".CCur($_REQUEST['fccredit_memo'])."',
//													fcother_cost='".CCur($_REQUEST['fcother_cost'])."',
//                                                    fccm_min='".CCur($_REQUEST['fccm_min'])."',
//                                                    fccm_add='".CCur($_REQUEST['fccm_add'])."'
//												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";
                      }

                      $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++) {
                   if($_REQUEST['fcpayment_amountD1'][$j - 1]!=0) {
                        $strSQL['Insert txar_det'][]="INSERT INTO txar_det(
                                                        fnid,ftbranch_code,ftar_no,
                                                        fnindex, ftsales_invoice_no, ftdo_no, fdtrans_date,
                                                        fdinvoice_date, fcamount,
                                                        fcoutstanding_amount, fcpayment_amount,
                                                        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['ftsales_invoice_noD1'][$j - 1]}' AS ftsales_invoice_no,
                                                        '{$_REQUEST['ftdo_noD1'][$j - 1]}' AS ftdo_no,'{$_REQUEST['fdtrans_dateD1'][$j]}' 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,
                                                         '{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
                                                         '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date
                                                        ";

                            $hit = (float)CCur($_REQUEST['fcoutstanding_amountD1'][$j - 1]) - (float)CCur($_REQUEST['fcpayment_amountD1'][$j - 1]);
                                    if((float)CCur($_REQUEST['fcoutstanding_amountD1'][$j - 1])==(float)CCur($_REQUEST['fcpayment_amountD1'][$j - 1])) {
                                        $strSQL['Update txsales_invoice_hdr'][]="UPDATE txsales_invoice_hdr SET fnpayment_status = 3, fcoutstanding_amount = {$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 {
                                        $strSQL['Update txsales_invoice_hdr'][]="UPDATE txsales_invoice_hdr SET fnpayment_status = 2, fcoutstanding_amount = {$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]}'";
                                    }
                   }
                  }

                for($j=1; $j<=count($_REQUEST['fnindexD2']); $j++) {
                   if($_REQUEST['fccm_paymentD2'][$j - 1]!=0) {
                        $strSQL['Insert txar_cm'][]="INSERT INTO txar_cm(
                                fnid,ftbranch_code,ftar_no,
                                fnindex,ftcm_no,fccm_amount,
                                fccm_outstanding,fccm_payment,
                                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['ftcm_noD2'][$j - 1]}' AS ftcm_no,'".CCur($_REQUEST['fccm_amountD2'][$j - 1])."' AS fccm_amount,
                                '".CCur($_REQUEST['fccm_outstandingD2'][$j - 1])."' AS fccm_outstanding,
                                '".CCur($_REQUEST['fccm_paymentD2'][$j - 1])."' AS fccm_payment,
                                '{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
                                '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";

                       $hit = (float)CCur($_REQUEST['fccm_outstandingD2'][$j - 1]) - (float)CCur($_REQUEST['fccm_paymentD2'][$j - 1]);
                       $strSQL['Update txcredit_memo_hdr'][]="UPDATE txcredit_memo_hdr SET
                               fcoutstanding_amount={$hit},
                               fccredit_amount=IFNULL(fccredit_amount,0) + ".CCur($_REQUEST['fccm_paymentD2'][$j - 1])."
                               WHERE
                               ftcm_no='{$_REQUEST['ftcm_noD2'][$j - 1]}'";
                   }
                  }

                  break;
			case "simpan_penerimaan":


                   $_REQUEST['fdtrans_date']=getDateYmd($_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'])."',
													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++) {

                     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,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,'{$_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 FinalData() {
        global $objAxpDB;
        $objAxpDB->Allow_Final(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
			case "final_bank":
                $_REQUEST['fdtrans_date']=getDateYmd($_REQUEST['fdtrans_date']);
                 for($j=1; $j<count($_REQUEST['fnindexD2']); $j++) {
                        $strSQL['Insert txar_cm'][]="INSERT INTO txar_cm (
                                fnid,ftbranch_code,fnindex,
                                ftar_no,ftcm_no,fccm_amount,
                                fccm_outstanding,fccm_payment,
                                ftcreated_user,fdcreated_date,
                                ftmodified_user,fdmodified_date)
                                SELECT  '' AS  fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'".$_REQUEST['fnindexD2'][$j]."' as fnindex,
                                '{$_REQUEST['ftar_no']}' as ftar_no,'".$_REQUEST['ftcm_noD2'][$j]."' as ftcm_no,'".CCur($_REQUEST['fccm_amountD2'][$j])."' as fccm_amount,
                                '".CCur($_REQUEST['fccm_outstandingD2'][$j])."' as  fccm_outstanding, '".CCur($_REQUEST['fccm_paymentD2'][$j])."' as fccm_payment,
                               '{$_SESSION['username']}' AS ftcreated_user,
                                NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";



                     }

               $strSQL['Insert AR_HDR'][]="UPDATE  txar_hdr
												SET ftfinal_bank_user='{$_SESSION['username']}',fdfinal_bank_date=NOW(),
                                                    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'])."',
													ftremarks='{$_REQUEST['ftremarks']}',
                                                    fnstatus2='0',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']}'";

					$strSQL['Insert txgiro_hist'][]="DELETE  FROM  txgiro_hist
															WHERE ftdocument_no='{$_REQUEST['ftar_no']}'";

					$strSQL['Insert txar_det1'][]="DELETE  FROM  txar_det1
															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['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,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,'' 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";
                      }
                      //credit memo
                      elseif($_REQUEST['fnpayment_methodD0'][$j]==6) {
                              $strSQL['Update txcredit_memo_hist'][]="
                                    UPDATE txcredit_memo_hist SET fccm_amount=fccm_amount-".CCur($_REQUEST['fcamountD0'][$j])." WHERE ftcustomer_code='{$_REQUEST['ftcustomer_code']}'
                              ";
                              $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]}'";


                      }

                      $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,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,'".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
                                                        ";


                  }

                  break;
			case "final_penerimaan":

                $ftcm_no=getAutoNumber('tn017','txcredit_memo_hdr','ftcm_no', $_REQUEST['fdtrans_date']);
                $_REQUEST['fdtrans_date']=getDateYmd($_REQUEST['fdtrans_date']);

                    #==============================
                    #CREDIT MEMO
                    #==============================
                    if(CCur($_REQUEST['fccm_add'])>0) {

                         $strSQL['Insert txcredit_memo_hdr'][]="INSERT INTO txcredit_memo_hdr (
                                    fnid,ftbranch_code,ftcm_no, ftreff_no,
                                    fdtrans_date,ftcustomer_code,
                                    fntrans_type,fcinvoice_amount,fcoutstanding_amount,
                                    fccredit_amount,ffvat,fcvat,
                                    ftnotes,fnstatus,ftprint_counter,
                                    fnprint_status,
                                    ftcreated_user,fdcreated_date,
                                    ftmodified_user,fdmodified_date)
                                   SELECT '' AS  fnid,'{$_SESSION['strbranch']}' AS  ftbranch_code,'{$ftcm_no}' AS ftcm_no,'{$_REQUEST['ftar_no']}' as ftreff_no,
                                   '{$_REQUEST['fdtrans_date']}' AS  fdtrans_date,'{$_REQUEST['ftcustomer_code']}' AS ftcustomer_code,
                                      '2'  AS fntrans_type,'".CCur($_REQUEST['fccm_add'])."' AS fcinvoice_amount,'".CCur($_REQUEST['fccm_add'])."' AS fcoutstanding_amount,
                                    0 AS fccredit_amount,0 AS ffvat,0 AS fcvat,
                                    'Credit Memo dari Penerimaan Piutang' as ftnotes,'1' as fnstatus,'0' as ftprint_counter,
                                    '0' as fnprint_status,
                                   '{$_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['ftsales_invoice_noD']); $j++) {
                                 if($_REQUEST['fcpayment_amountD'][$j]!=0) {
                                         $jj=$jj+1;
                                          $strSQL['Insert txcredit_memo_det'][]="INSERT INTO txcredit_memo_det (
                                                        fnid,ftbranch_code,ftcm_no,
                                                        fdtrans_date,fnindex,
                                                        ftsales_invoice_no,fdsales_invoice_date,
                                                        fcinvoice_amount,fcoutstanding_amount,fccredit_amount,
                                                        fcoutstanding_credit_memo,ftdescription,
                                                        ftcreated_user,
                                                        fdcreated_date,ftmodified_user,fdmodified_date)
                                                  SELECT
                                                        '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$ftcm_no}' AS ftcm_no,
                                                        null fdtrans_date,{$jj} as fnindex,
                                                        '{$_REQUEST['ftsales_invoice_noD'][$j]}' as ftsales_invoice_no,'{$_REQUEST['fdtrans_dateD'][$j]}' AS  fdsales_invoice_date,
                                                        '".CCur($_REQUEST['fcamount1D'][$j])."' AS fcinvoice_amount,'".CCur($_REQUEST['fcoutstanding_amountD'][$j])."' AS fcoutstanding_amount,'0' as fccredit_amount,
                                                        '".CCur($_REQUEST['fcoutstanding_amountD'][$j])."' as fcoutstanding_credit_memo,'Credit Memo dari Penerimaan Piutang' as ftdescription,
                                                        '{$_SESSION['username']}' AS ftcreated_user,NOW() AS fdcreated_date,
                                                        '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";



                                     }



                              }

                    }



                        $strSQL['Insert txar_det'][]="DELETE FROM txar_cm WHERE ftar_no='{$_REQUEST['ftar_no']}'";

                        for($j=1; $j<count($_REQUEST['fnindexD2']); $j++) {
                        $strSQL['Insert txar_cm'][]="INSERT INTO txar_cm (
                                fnid,ftbranch_code,fnindex,
                                ftar_no,ftcm_no,fccm_amount,
                                fccm_outstanding,fccm_payment,
                                ftcreated_user,fdcreated_date,
                                ftmodified_user,fdmodified_date)
                                SELECT  '' AS  fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'".$_REQUEST['fnindexD2'][$j]."' as fnindex,
                                '{$_REQUEST['ftar_no']}' as ftar_no,'".$_REQUEST['ftcm_noD2'][$j]."' as ftcm_no,'".CCur($_REQUEST['fccm_amountD2'][$j])."' as fccm_amount,
                                '".CCur($_REQUEST['fccm_outstandingD2'][$j])."' as  fccm_outstanding, '".CCur($_REQUEST['fccm_paymentD2'][$j])."' as fccm_payment,
                               '{$_SESSION['username']}' AS ftcreated_user,
                                NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";

                               $strSQL['Update txcredit_memo_hist'][]="
                                            UPDATE txcredit_memo_hist SET fccm_amount=fccm_amount-".CCur($_REQUEST['fccm_paymentD2'][$j])." WHERE ftcustomer_code='{$_REQUEST['ftcustomer_code']}'
                                        ";
                              $strSQL['Update txcredit_memo_hdr'][]="
                                        UPDATE txcredit_memo_hdr
                                               SET fcoutstanding_amount=fcoutstanding_amount-".CCur($_REQUEST['fccm_paymentD2'][$j])." ,
                                                    fccredit_amount=fccredit_amount+".CCur($_REQUEST['fccm_paymentD2'][$j])."
                                            WHERE ftcm_no='".$_REQUEST['ftcm_noD2'][$j]."'";

                     }
                        #===========================================



                    $strSQL['Insert AR_HDR'][]="UPDATE  txar_hdr
                                                    SET ftfinal_penerimaan_user='{$_SESSION['username']}',fdfinal_penerimaan_date=NOW(),
                                                        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'])."',
                                                        ftremarks='{$_REQUEST['ftremarks']}',
                                                        fnstatus2='2',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'])."'
                                                    WHERE ftar_no='{$_REQUEST['ftar_no']}'";


                      $strSQL['Insert txar_det'][]="DELETE FROM txar_det WHERE ftar_no='{$_REQUEST['ftar_no']}'";
                     $strSQL['Insert txar_det'][]="DELETE FROM txar_det WHERE ftar_no='{$_REQUEST['ftar_no']}'";
                     for($j=0; $j<count($_REQUEST['ftsales_invoice_noD']); $j++) {

                         if($_REQUEST['fcpayment_amountD'][$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,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_noD'][$j]}' AS ftsales_invoice_no,'{$_REQUEST['fdtrans_dateD'][$j]}' AS fdinvoice_date,
                                                                    '{$_REQUEST['ftdo_noD'][$j]}' AS ftdo_no,'{$_REQUEST['fdtrans_dateD'][$j]}' AS fdtrans_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
                                                                ";
                                    $hit = (float)CCur($_REQUEST['fcoutstanding_amountD'][$j]) - (float)CCur($_REQUEST['fcpayment_amountD'][$j]);
                                    if((float)CCur($_REQUEST['fcoutstanding_amountD'][$j])==(float)CCur($_REQUEST['fcpayment_amountD'][$j])) {
                                        $strSQL['Update txsales_invoice_hdr'][]="UPDATE txsales_invoice_hdr SET fnpayment_status = 3, fcoutstanding_amount = {$hit}, fcpayment_amount = IFNULL(fcpayment_amount,0) + ".CCur($_REQUEST['fcpayment_amountD'][$j]).", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE   ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_noD'][$j]}'";
                                    }
                                    else {
                                        $strSQL['Update txsales_invoice_hdr'][]="UPDATE txsales_invoice_hdr SET fnpayment_status = 2, fcoutstanding_amount = {$hit}, fcpayment_amount = IFNULL(fcpayment_amount,0) + ".CCur($_REQUEST['fcpayment_amountD'][$j]).", ftmodified_user =  '{$_SESSION['username']}', fdmodified_date = NOW() WHERE   ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_noD'][$j]}'";
                                    }

                        }



                      }

                  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 CancelData() {
        global $objAxpDB;
        $objAxpDB->Allow_Cancel(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
          case "batal":
                if($_REQUEST['fnstatus2']==2) {
                       #==============================
                        #CREDIT MEMO
                        #==============================
                        if(CCur($_REQUEST['fccm_add'])>0) {

                             $strSQL['Update Batal txcredit_memo_hdr'][]="update txcredit_memo_hdr
                                                                set fnstatus=9,fnstatus2=9
                                                                where ftreff_no='{$_REQUEST['ftar_no']}'";
                                       $jj=0;
                                for($j=0; $j<count($_REQUEST['ftsales_invoice_noD1']); $j++) {
                                     if($_REQUEST['fcpayment_amountD1'][$j]!=0) {
                                             $jj=$jj+1;

                                                     $strSQL['Update txcredit_memo_hist'][]="
                                                                    UPDATE txcredit_memo_hist  SET
                                                                    fccm_amount=fccm_amount-".CCur($_REQUEST['fccm_add'])."
                                                                    WHERE ftcustomer_code='{$_REQUEST['ftcustomer_code']}'";

                                         }



                                  }

                        }



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

                                   $strSQL['Update txcredit_memo_hist'][]="
                                                UPDATE txcredit_memo_hist SET fccm_amount=fccm_amount+".CCur($_REQUEST['fccm_paymentD2'][$j])." WHERE ftcustomer_code='{$_REQUEST['ftcustomer_code']}'
                                            ";
                                  $strSQL['Update txcredit_memo_hdr'][]="
                                            UPDATE txcredit_memo_hdr
                                                   SET fcoutstanding_amount=fcoutstanding_amount+".CCur($_REQUEST['fccm_paymentD2'][$j])." ,
                                                        fccredit_amount=fccredit_amount-".CCur($_REQUEST['fccm_paymentD2'][$j])."
                                                WHERE ftcm_no='".$_REQUEST['ftcm_noD2'][$j]."'";

                         }
                            #===========================================
                }

                $_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']}'";

                  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
                      elseif($_REQUEST['fnpayment_methodD0'][$j]==6) {
                           if($_REQUEST['fnstatus2']==2) {
                                   $strSQL['Update txcredit_memo_hist'][]="
                                        UPDATE txcredit_memo_hist SET fccm_amount=fccm_amount+".CCur($_REQUEST['fcamountD0'][$j])." WHERE ftcustomer_code='{$_REQUEST['ftcustomer_code']}'
                                  ";
                                  $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]}'";
                           }

                      }



                  }
                  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=1; $j<=count($_REQUEST['fnindexD2']); $j++) {
                   if($_REQUEST['fccm_paymentD2'][$j - 1]!=0) {
                        $hit = (float)CCur($_REQUEST['fccm_outstandingD2'][$j - 1]) + (float)CCur($_REQUEST['fccm_paymentD2'][$j - 1]);
                       $strSQL['Update txcredit_memo_hdr'][]="UPDATE txcredit_memo_hdr SET
                               fcoutstanding_amount={$hit},
                               fccredit_amount=IFNULL(fccredit_amount,0) - ".CCur($_REQUEST['fccm_paymentD2'][$j - 1])."
                               WHERE
                               ftcm_no='{$_REQUEST['ftcm_noD2'][$j - 1]}'";
                   }
                  }
                  

                  /*// batal -%
                  $ssql1="SELECT IFNULL(MAX(ftjournal_no),'')  AS M
                              FROM txar_hdr
                              WHERE ftjournal_no LIKE '{$_REQUEST['ftjournal_no']}-%'";
                    $no=RetRstField($ssql1);
                    if($no!="") {
                        $no=$no+1;
                        $str1="-{$no}";
                    }
                    else {
                        $str1="-1";
                    }

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

                    $ftjournal_no_new="{$_REQUEST['ftjournal_no']}{$str1}";

                     $strSQL["batal"][]="update txar_hdr
                                        set fnstatus=9,ftjournal_no='{$ftjournal_no_new}'
                                        where ftar_no='{$_REQUEST['ftar_no']}'";
*/
        //             $strSQL["batal"][]="update txar_det
        //                                set ftar_no='{$ftar_no_new}'
        //                                where ftar_no='{$_REQUEST['ftar_no']}'";
        //             $strSQL["batal"][]="update txar_det1
        //                                set ftar_no='{$ftar_no_new}'
        //                                where ftar_no='{$_REQUEST['ftar_no']}'";


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