<?php
function ValBankMasuk() {
    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("");
        }
        
        if($_REQUEST['ftgiro_noD0'][$j]!="") {
            $ssql= "select fnid
                    from txgiro_hist
                    where ftdocument_no<>'".$_REQUEST['ftbankrcp_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() {
        ValBankMasuk();
        global $objAxpDB;
        //$objAxpDB->Allow_Save(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "simpan":
           
            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                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,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['ftbankrcp_no']}' AS ftdocument_no,'' AS ftcustomer_code,'' AS ftcustomer_name,
                                '' AS ftCredit_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 fccredit_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 ,
                                'BM' AS fttype";
                }

            }
    break;
    }  //pr($strSQL);die("mm")
    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(ftbankrcp_no),'')  AS M
                      FROM txbankrcp_hdr
                      WHERE ftbankrcp_no LIKE '{$_REQUEST['ftbankrcp_no']}-%'";
            $no=RetRstField($ssql1);
            if($no!="") {
                $no1=explode("-",$no);
                $no=(int)$no1[1]+1;
                $str1="-{$no}";
            }
            else {
                $str1="-1";
            }

            $ftbankrcp_no_new="{$_REQUEST['ftbankrcp_no']}{$str1}";

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

            $strSQL["Revisi"][]="update txbankrcp_det
                    set ftbankrcp_no='{$ftbankrcp_no_new}'
                    where ftbankrcp_no='{$_REQUEST['ftbankrcp_no']}'";

            $strSQL["Revisi"][]="update txgiro_hist
                    set ftdocument_no='{$ftftbankpym_no_new}', fnstatus_giro='9'
                    where ftdocument_no='{$_REQUEST['ftbankrcp_no']}' and fttype='BM'";

            // Insert untuk giro mundur piutang
            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                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,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['ftbankrcp_no']}' AS ftdocument_no,'' AS ftcustomer_code,'' AS ftcustomer_name,
                                '' AS ftCredit_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 fccredit_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 ,
                                'BM' AS fttype";
                }
            }

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

      return $strSQL;
   }

   


    function FinalData() {
        ValBankMasuk();
        global $objAxpDB;
        $objAxpDB->Allow_Final(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "final":

            $strSQL["Insert txbankrcp_hdr"][] =
                    "update txbankrcp_hdr
					set ftfinal_user='{$_SESSION['username']}',fdfinal_date=NOW(),
                    ftjournal_no='{$_REQUEST['ftjournal_no']}',
                    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 ftbankrcp_no='{$_REQUEST['ftbankrcp_no']}'";


           $jj=0;
		    $strSQL["Insert txbankrcp_det"][] ="delete from txbankrcp_det where ftbankrcp_no='{$_REQUEST['ftbankrcp_no']}'";
			 $strSQL["Insert txgiro_hist"][] ="delete from txgiro_hist where ftdocument_no='{$_REQUEST['ftbankrcp_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 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_no1']}' AS ftacc_no,'' AS ftacc_desc,
                                '{$_REQUEST['ftbankrcp_no']}' AS ftdocument_no,'' AS ftcustomer_code,'' AS ftcustomer_name,
                                '' AS ftCredit_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 fccredit_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 ,
                                'BM' AS fttype";
                      }

                $strSQL["Insert txbankrcp_det"][] ="
                    INSERT INTO txbankrcp_det
                    (
                    fnid,ftbranch_code,fnindex,
                    ftbankrcp_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['ftbankrcp_no']}' AS ftbankrcp_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 txgiro_hist'][]="
                                  UPDATE txgiro_hist SET fnstatus_giro=9 WHERE ftgiro_no='{$_REQUEST['ftgiro_noD0'][$j]}' AND ftdocument_no='{$_REQUEST['ftbankrcp_no']}'";

                      }

           }


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

             $ftbankrcp_no_new="{$_REQUEST['ftbankrcp_no']}{$str1}";
             $ftjournal_no_new="{$_REQUEST['ftjournal_no']}{$str1}";
              $strSQL["batal"][]="update txbankrcp_hdr
                                            set fnstatus=9,
                                               ftjournal_no='{$ftjournal_no_new}'
                                            where ftbankrcp_no='{$_REQUEST['ftbankrcp_no']}'";

//             $strSQL["batal"][]="update txbankrcp_hdr
//                                set fnstatus=9,ftbankrcp_no='{$ftbankrcp_no_new}' ,
//                                   ftjournal_no='{$ftjournal_no_new}'
//                                where ftbankrcp_no='{$_REQUEST['ftbankrcp_no']}'";
//
//             $strSQL["batal"][]="update txbankrcp_det
//                                set ftbankrcp_no='{$ftbankrcp_no_new}'
//                                where ftbankrcp_no='{$_REQUEST['ftbankrcp_no']}'";
         //--------------------------------------------

        break;
        }
        return  $strSQL;
      }