<?php

 function ValBankKeluar() {
    for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
        // check no perkiraan
        $ftcoa_code=RetRstField("select ftcoa_code from tlcoa where ftcoa_code='".$_REQUEST['ftcoa_codeD0'][$j]."'");

        if ($ftcoa_code == "") {

            echo "  <script>
                        alert('No Perkiraan tidak valid ...');
                        window.history.back();
                    </script>";
            die("");
        }

        // check no giro
        if($_REQUEST['ftgiro_noD0'][$j]!="") {
            $ssql= "select fnid
                    from txgiro2_hist
                    where ftdocument_no<>'".$_REQUEST['ftbankpym_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. Giro sudah pernah dipakai ...');
                          window.history.back();
                     </script>";
                die("No. Reff sudah pernah dipakai ...");
            }
        }
    }

}

    function SaveData() {
        ValBankKeluar();
        global $objAxpDB;
        //$objAxpDB->Allow_Save(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "simpan":
            /*
            if($_REQUEST['fnstatus']=='0') {
                 $strSQL["Insert txbankpym_hdr"][] ="DELETE
                                                        FROM txbankpym_hdr
                                                        WHERE  ftbankpym_no='{$_REQUEST['ftbankpym_no']}'";
                 $strSQL["Insert txbankpym_hdr"][] ="DELETE
                                                        FROM txbankpym_det
                                                        WHERE  ftbankpym_no='{$_REQUEST['ftbankpym_no']}'";
            }
            $strSQL["Insert txbankpym_hdr"][] =
                    "INSERT INTO txbankpym_hdr
                    (fnid,ftbankpym_no,
                    ftbranch_code,fdtrans_date,ftacc_no1,
                    ftacc_desc, ftreceive_name, ftcurrency_code, fcrate, ftreference, fcamount,
                    fccomparison_amount,ftremarks,fnstatus,
                    ftprint_counter,fnprint_status,
                    ftcreated_user,fdcreated_date,ftmodified_user,
                    fdmodified_date
                    )
                    SELECT
                    '' AS fnid,'{$_REQUEST['ftbankpym_no']}' AS ftbankpym_no,
                    '{$_SESSION['strbranch']}' AS ftbranch_code,'".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,'{$_REQUEST['ftacc_no1']}' AS ftacc_no1,
                    '' AS ftacc_desc,'{$_REQUEST['ftreceive_name']}' AS ftreceive_name, '{$_REQUEST['ftcurrency_code']}' AS ftcurrency_code, '".CCur($_REQUEST['fcrate'])."' AS fcrate,'{$_REQUEST['ftreference']}' AS ftreference,'".CCur($_REQUEST['fcamount'])."' AS fcamount,
                   '".CCur($_REQUEST['fccomparison_amount'])."'  AS fccomparison_amount,'{$_REQUEST['ftremarks']}' AS ftremarks,'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";
             *
             *
             */
                
            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                if(($_REQUEST['fnpayment_methodD0'][$j]==0)||($_REQUEST['fnpayment_methodD0'][$j]==1)) {
                    $strSQL['Insert txgiro2_hist'][]="
                            INSERT INTO txgiro2_hist (
                                fnid,ftbranch_code,ftacc_no,ftacc_desc,
                                ftdocument_no,ftsupplier_code,ftsupplier_name,
                                ftdebit_no,fnpayment_method,ftgiro_no,
                                fdgiro_date,fdgiro_cliring_date,fdtrans_date,
                                ftremarks,fnstatus_giro,fncategory,
                                fncategory2,fcamount,fcdebit_memo,fcother_cost,
                                ftother_sign,fctotal,fcrate,ftprint_counter,
                                fnprint_status,ftcreated_user,fdcreated_date,
                                ftmodified_user,fdmodified_date,fttype
                                )
                            SELECT
                                '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftcoa_codeD0'][$j]}' AS ftacc_no,'' AS ftacc_desc,
                                '{$_REQUEST['ftbankpym_no']}' AS ftdocument_no,'' AS ftsupplier_code,'' AS ftsupplier_name,
                                '' AS ftdebit_no,'{$_REQUEST['fnpayment_methodD0'][$j]}' AS fnpayment_method,'{$_REQUEST['ftgiro_noD0'][$j]}' AS ftgiro_no,
                                '".CDate($_REQUEST['fdgiro_dateD0'][$j])."' AS fdgiro_date,'".CDate($_REQUEST['fdgiro_cliring_dateD0'][$j])."' AS fdgiro_cliring_date,'".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,
                                '' AS ftremarks,'0' AS fnstatus_giro,'' AS fncategory,
                                '' AS fncategory2,'".CCur($_REQUEST['fcamountD0'][$j])."' AS fcamount,'' AS fcdebit_memo,0 AS fcother_cost,
                                '' AS ftother_sign,'".CCur($_REQUEST['fcamountD0'][$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 ,
                                'BK' AS fttype";
                }
				
                  /*
                $strSQL["Insert txbankpym_det"][] ="
                    INSERT INTO txbankpym_det
                    (
                    fnid,ftbranch_code,fnindex,
                    ftbankpym_no,fdtrans_date,ftacc_no,
                    ftacc_desc,fnpayment_method,ftcoa_code,ftgiro_no,
                    fdgiro_date,fdgiro_cliring_date,fnstatus_giro,
                    ftdescription,fcamount,ftcreated_user,
                    fdcreated_date,ftmodified_user,fdmodified_date
                    )
                    SELECT
                    '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$j}' AS fnindex,
                    '{$_REQUEST['ftbankpym_no']}' AS ftbankpym_no,'".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date, '{$_REQUEST['ftacc_noD0'][$j]}' AS ftacc_no,
                    '{$_REQUEST['ftacc_descD0'][$j]}' AS ftacc_desc,'{$_REQUEST['fnpayment_methodD0'][$j]}' AS fnpayment_method,'{$_REQUEST['ftcoa_codeD0'][$j]}' AS ftcode_code,'{$_REQUEST['ftgiro_noD0'][$j]}' AS ftgiro_no,
                    '".CDate($_REQUEST['fdgiro_dateD0'][$j])."' AS fdgiro_date,'".CDate($_REQUEST['fdgiro_cliring_dateD0'][$j])."' AS fdgiro_cliring_date,'{$fnstatus}' AS fnstatus_giro,
                     '{$_REQUEST['ftdescriptionD0'][$j]}' AS ftdescription, '".CCur($_REQUEST['fcamountD0'][$j])."'  AS fcamount,'{$_SESSION['username']}' AS ftcreated_user,
                    NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";
*/
            }

        break;
        }

        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(ftbankpym_no),'')  AS M
                      FROM txbankpym_hdr
                      WHERE ftbankpym_no LIKE '{$_REQUEST['ftbankpym_no']}-%'";
            $no=RetRstField($ssql1);

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

            $ftbankpym_no_new="{$_REQUEST['ftbankpym_no']}{$str1}";

            $strSQL["Revisi"][]="update txbankpym_hdr
                    set fdrevisi_date=now(), ftrevisi_user='{$_SESSION['username']}',
                    fnstatus=3, ftbankpym_no='{$ftbankpym_no_new}'
                    where ftbankpym_no='{$_REQUEST['ftbankpym_no']}'";

            $strSQL["Revisi"][]="update txbankpym_det
                    set ftbankpym_no='{$ftbankpym_no_new}'
                    where ftbankpym_no='{$_REQUEST['ftbankpym_no']}'";

            $strSQL["Revisi"][]="update txgiro2_hist
                    set ftdocument_no='{$ftbankpym_no_new}', fnstatus_giro='9'
                    where ftdocument_no='{$_REQUEST['ftbankpym_no']}' and fttype='BK'";

            // Insert utk giro mundur hutang
            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                if(($_REQUEST['fnpayment_methodD0'][$j]==0)||($_REQUEST['fnpayment_methodD0'][$j]==1)) {
                    $strSQL['Insert txgiro2_hist'][]="
                            INSERT INTO txgiro2_hist (
                                fnid,ftbranch_code,ftacc_no,ftacc_desc,
                                ftdocument_no,ftsupplier_code,ftsupplier_name,
                                ftdebit_no,fnpayment_method,ftgiro_no,
                                fdgiro_date,fdgiro_cliring_date,fdtrans_date,
                                ftremarks,fnstatus_giro,fncategory,
                                fncategory2,fcamount,fcdebit_memo,fcother_cost,
                                ftother_sign,fctotal,fcrate,ftprint_counter,
                                fnprint_status,ftcreated_user,fdcreated_date,
                                ftmodified_user,fdmodified_date,fttype
                                )
                            SELECT
                                '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftcoa_codeD0'][$j]}' AS ftacc_no,'' AS ftacc_desc,
                                '{$_REQUEST['ftbankpym_no']}' AS ftdocument_no,'' AS ftsupplier_code,'' AS ftsupplier_name,
                                '' AS ftdebit_no,'{$_REQUEST['fnpayment_methodD0'][$j]}' AS fnpayment_method,'{$_REQUEST['ftgiro_noD0'][$j]}' AS ftgiro_no,
                                '".CDate($_REQUEST['fdgiro_dateD0'][$j])."' AS fdgiro_date,'".CDate($_REQUEST['fdgiro_cliring_dateD0'][$j])."' AS fdgiro_cliring_date,'".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,
                                '' AS ftremarks,'0' AS fnstatus_giro,'' AS fncategory,
                                '' AS fncategory2,'".CCur($_REQUEST['fcamountD0'][$j])."' AS fcamount,'' AS fcdebit_memo,0 AS fcother_cost,
                                '' AS ftother_sign,'".CCur($_REQUEST['fcamountD0'][$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 ,
                                'BK' AS fttype";
                }
            }

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

      return $strSQL;
   }

   
   function FinalData() {
//        ValBankKeluar();
        global $objAxpDB;
        $objAxpDB->Allow_Final(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "final":
	       $strSQL["Insert txbankpym_hdr"][] =
                    "update txbankpym_hdr
                    set ftfinal_user='{$_SESSION['username']}',fdfinal_date=NOW() ,
                    fdtrans_date='".CDate($_REQUEST['fdtrans_date'])."',ftacc_no1='{$_REQUEST['ftacc_no1']}',
                    ftacc_desc='',ftreference='{$_REQUEST['ftreference']}',fcamount='".CCur($_REQUEST['fcamount'])."',
                    ftcurrency_code='{$_REQUEST['ftcurrency_code']}', fcrate='".CCur($_REQUEST['fcrate'])."',
                    fccomparison_amount='".CCur($_REQUEST['fccomparison_amount'])."',
					ftremarks='{$_REQUEST['ftremarks']}',
					fnstatus='2',
                    ftprint_counter='',fnprint_status='',
                    ftreceive_name='{$_REQUEST['ftreceive_name']}',
                    ftmodified_user='{$_SESSION['username']}',
                    fdmodified_date=NOW()
					where ftbankpym_no='{$_REQUEST['ftbankpym_no']}'";

           $jj=0;

		   	 $strSQL["Insert txbankpym_det"][] ="delete from txbankpym_det where ftbankpym_no ='{$_REQUEST['ftbankpym_no']}'";
           	 $strSQL["Insert txgiro_hist"][] ="delete from txgiro2_hist where ftdocument_no ='{$_REQUEST['ftbankpym_no']}'";

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

                   if(($_REQUEST['fnpayment_methodD0'][$j]==0)||($_REQUEST['fnpayment_methodD0'][$j]==1)) {
                           $strSQL['Insert txgiro_hist'][]="
                            INSERT INTO txgiro2_hist (
                                fnid,ftbranch_code,ftacc_no,ftacc_desc,
                                ftdocument_no,ftsupplier_code,ftsupplier_name,
                                ftdebit_no,fnpayment_method,ftgiro_no,
                                fdgiro_date,fdgiro_cliring_date,fdtrans_date,
                                ftremarks,fnstatus_giro,fncategory,
                                fncategory2,fcamount,fcdebit_memo,fcother_cost,
                                ftother_sign,fctotal,fcrate,ftprint_counter,
                                fnprint_status,ftcreated_user,fdcreated_date,
                                ftmodified_user,fdmodified_date,fttype,ftpf_no 
                                )
                                SELECT
                                '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftacc_no1']}' AS ftacc_no,'' AS ftacc_desc,
                                '{$_REQUEST['ftbankpym_no']}' AS ftdocument_no,'' AS ftsupplier_code,'' AS ftsupplier_name,
                                '' AS ftdebit_no,'{$_REQUEST['fnpayment_methodD0'][$j]}' AS fnpayment_method,'{$_REQUEST['ftgiro_noD0'][$j]}' AS ftgiro_no,
                                '".CDate($_REQUEST['fdgiro_dateD0'][$j])."' AS fdgiro_date,'".CDate($_REQUEST['fdgiro_cliring_dateD0'][$j])."' AS fdgiro_cliring_date,'".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,
                                '' AS ftremarks,'0' AS fnstatus_giro,'' AS fncategory,
                                '' AS fncategory2,'".CCur($_REQUEST['fcamountD0'][$j])."' AS fcamount,'' AS fcdebit_memo,0 AS fcother_cost,
                                '' AS ftother_sign,'".CCur($_REQUEST['fcamountD0'][$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 ,
                                'BK' AS fttype, '{$_REQUEST['fttf_noD0'][$j]}' AS fttf_no ";
                      }

                $strSQL["Insert txbankpym_det"][] ="
                    INSERT INTO txbankpym_det
                    (
                    fnid,ftbranch_code,fnindex,
                    ftbankpym_no,fdtrans_date,ftacc_no,
                    ftacc_desc,fnpayment_method,ftcoa_code,ftgiro_no,
                    fdgiro_date,fdgiro_cliring_date,fnstatus_giro,
                    ftdescription,fcamount,ftcreated_user,
                    fdcreated_date,ftmodified_user,fdmodified_date
                    )
                    SELECT
                    '' AS fnid,'{$_SESSION['strbranch']}' AS ftbranch_code,'{$j}' AS fnindex,
                    '{$_REQUEST['ftbankpym_no']}' AS ftbankpym_no,'".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date, '{$_REQUEST['ftacc_noD0'][$j]}' AS ftacc_no,
                    '{$_REQUEST['ftacc_descD0'][$j]}' AS ftacc_desc,'{$_REQUEST['fnpayment_methodD0'][$j]}' AS fnpayment_method,'{$_REQUEST['ftcoa_codeD0'][$j]}' AS ftcoa_code,'{$_REQUEST['ftgiro_noD0'][$j]}' AS ftgiro_no,
                    '".CDate($_REQUEST['fdgiro_dateD0'][$j])."' AS fdgiro_date,'".CDate($_REQUEST['fdgiro_cliring_dateD0'][$j])."' AS fdgiro_cliring_date,'{$fnstatus}' AS fnstatus_giro,
                     '{$_REQUEST['ftdescriptionD0'][$j]}' AS ftdescription, '".CCur($_REQUEST['fcamountD0'][$j])."'  AS fcamount,'{$_SESSION['username']}' AS ftcreated_user,
                    NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";

           }


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

   }


      function CancelData() {
        global $objAxpDB;
       // $objAxpDB->Allow_Cancel(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
          case "batal":
          for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                   if(($_REQUEST['fnpayment_methodD0'][$j]==0)||($_REQUEST['fnpayment_methodD0'][$j]==1)) {
                           $strSQL['Update txgiro2_hist'][]="
                                  UPDATE txgiro2_hist SET fnstatus_giro=9 WHERE ftgiro_no='{$_REQUEST['ftgiro_noD0'][$j]}' AND ftdocument_no='{$_REQUEST['ftbankpym_no']}'";
                      }
           }

                                                    
        break;
        }

        return $strSQL;
      }

    