<?php

function ValBankMasuk() {
           for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                 if($_REQUEST['ftgiro_no']!="") {
                      $ssql="select fnid
                            from txgiro_hist
                            where ftdocument_no<>'".$_REQUEST['ftbankrcp_no']."' and  fnstatus_giro<>9 and
                                   ftgiro_no='".$_REQUEST['ftgiro_no']."'";


                      $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":

//
            $strSQL["Insert txbankrcp_hdr"][] =
                    "INSERT INTO txbankrcp_hdr
                    (fnid,ftbankrcp_no,ftjournal_no,
                    ftbranch_code,fdtrans_date,ftacc_no1,
                    ftacc_desc,ftreference,fcamount,
                    fccomparison_amount,ftremarks,fnstatus,
                    ftprint_counter,fnprint_status,
                    ftcreated_user,fdcreated_date,ftmodified_user,
                    fdmodified_date
                    )
                    SELECT
                    '' AS fnid,'{$_REQUEST['ftbankrcp_no']}' AS ftbankrcp_no,'{$_REQUEST['ftjournal_no']}' AS ftjournal_no,
                    '{$_SESSION['strbranch']}' AS ftbranch_code,'".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,'{$_REQUEST['ftacc_no']}' AS ftacc_no1,
                    '' AS ftacc_desc,'{$_REQUEST['ftloan_payment_no']}' AS ftreference,'".CCur($_REQUEST['fcloan_payment_amount'])."' AS fcamount,
                   '".CCur($_REQUEST['fcloan_payment_amount'])."'  AS fccomparison_amount,'{$_REQUEST['ftnotes']}' 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";

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

                $strSQL["Insert txbankrcp_det"][] ="
                    INSERT INTO txbankrcp_det
                    (
                    fnid,ftbranch_code,fnindex,
                    ftbankrcp_no,fdtrans_date,ftacc_no,
                    ftacc_desc,fnpayment_method,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_no']}' AS ftacc_no,
                    '{$_REQUEST['ftacc_desc']}' AS ftacc_desc,'{$_REQUEST['fnpayment_method']}' AS fnpayment_method,'{$_REQUEST['ftgiro_no']}' AS ftgiro_no,
                    '".CDate($_REQUEST['fdgiro_date'])."' AS fdgiro_date,'".CDate($_REQUEST['fdgiro_cliring_date'])."' AS fdgiro_cliring_date,'{$fnstatus}' AS fnstatus_giro,
                    '{$_REQUEST['ftnotes']}' AS ftdescription, '".CCur($_REQUEST['fcloan_payment_amount'])."'  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 SaveDataLast() {
        global $objAxpDB;
//        $objAxpDB->Allow_Save(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "simpan":


                break;
         }
    return $strSQL;
   }


   function FinalData() {
         ValBankMasuk();
        global $objAxpDB;
        $objAxpDB->Allow_Final(false);
        $strSQL=array();
        switch($_REQUEST[mode]) {
        case "final":
            $strSQL['Update txloan_payment_hdr'][]="update txloan_payment_hdr set
                                             fdtrans_date='".CDate($_REQUEST['fdtrans_date'])."',
                                            ftemployee_code='{$_REQUEST['ftemployee_code']}',
                                            fcloan_payment_amount='".CCur($_REQUEST['fcloan_payment_amount'])."',
                                            ftloan_payment_description='{$_REQUEST['ftloan_payment_description']}',
                                            ftnotes='{$_REQUEST['ftnotes']}',
                                            fnstatus='2',
                                             ftbankrcp_no='{$_REQUEST['ftbankrcp_no']}',
                                             ftjournal_no='{$_REQUEST['ftjournal_no']}',
                                             ftacc_no='{$_REQUEST['ftacc_no']}',
                                             fnpayment_method='{$_REQUEST['fnpayment_method']}',
                                             ftgiro_no='{$_REQUEST['ftgiro_no']}',
                                             fdgiro_date='".CDate($_REQUEST['fdgiro_date'])."',
                                             fdgiro_cliring_date='".CDate($_REQUEST['fdgiro_cliring_date'])."',
                                             fnstatus_giro='{$_REQUEST['fnstatus_giro']}'
												where ftloan_payment_no= '{$_REQUEST['ftloan_payment_no']}'
												";

             for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
						$strSQL['Update txloan'][]="update txloan set
												fcloan_payment=ifnull(fcloan_payment,0)+".CCur($_REQUEST['fcloan_paymentD0'][$j])." ,
												fcloan_outstanding=ifnull(fcloan_outstanding,0)-".CCur($_REQUEST['fcloan_paymentD0'][$j])."
												where ftloan_no= '{$_REQUEST['ftloan_noD0'][$j]}'
												";
							$strSQL['Update txloan'][]="update txloan set
												fnloan_payment_status=1
												where ftloan_no='{$_REQUEST['ftloan_noD0'][$j]}' and
													fcloan_payment=0
														";
							$strSQL['Update txloan'][]="update txloan set
												fnloan_payment_status=3
												where ftloan_no='{$_REQUEST['ftloan_noD0'][$j]}' and
													fcloan_outstanding=0
																			";

							$strSQL['Update txloan'][]="update txloan set
										fnloan_payment_status=2
										where ftloan_no='{$_REQUEST['ftloan_noD0'][$j]}' and
											fcloan_payment>0 and
											fcloan_outstanding>0
																	";

				}
				$strSQL['Update txloan'][]="update txloan_payment_det,txloan
											set txloan_payment_det.fdloan_date=txloan.fdtrans_date
											where txloan_payment_det.ftloan_no=txloan.ftloan_no and
												txloan_payment_det.ftloan_payment_no='{$_REQUEST['ftloan_payment_no']}'";



            $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_no']}',
                    ftacc_desc='',
                    ftreference='{$_REQUEST['ftloan_payment_no']}',
                    fcamount='".CCur($_REQUEST['fcloan_payment_amount'])."',
                    fccomparison_amount='".CCur($_REQUEST['fcloan_payment_amount'])."',
					ftremarks='{$_REQUEST['ftnotes']}',
                    ftprint_counter='',fnprint_status='',
                    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']}'";


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

                   if(($_REQUEST['fnpayment_method']==0)||($_REQUEST['fnpayment_method']==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_no']}' AS ftacc_no,'' AS ftacc_desc,
                                '{$_REQUEST['ftbankrcp_no']}' AS ftdocument_no,'' AS ftcustomer_code,'' AS ftcustomer_name,
                                '' AS ftCredit_NO,'{$_REQUEST['fnpayment_method']}' AS fnpayment_method,'{$_REQUEST['ftgiro_no']}' AS ftgiro_no,
                                '".CDate($_REQUEST['fdgiro_date'])."' AS fdgiro_date,'".CDate($_REQUEST['fdgiro_cliring_date'])."' AS fdgiro_cliring_date,'".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,
                               '{$_REQUEST['ftnotes']}' AS ftremarks,'0' AS fnstatus_giro,'' AS fncategory,
                                '' AS fncategory2,'".CCur($_REQUEST['fcloan_payment_amount'])."' AS fcamount,'' AS fccredit_memo,0 AS fcother_cost,
                                '' AS ftother_sign,'".CCur($_REQUEST['fcloan_payment_amount'])."' 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 ,
                                '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,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_no']}' AS ftacc_no,
                    '{$_REQUEST['ftacc_desc']}' AS ftacc_desc,'{$_REQUEST['fnpayment_method']}' AS fnpayment_method,'{$_REQUEST['ftgiro_no']}' AS ftgiro_no,
                    '".CDate($_REQUEST['fdgiro_date'])."' AS fdgiro_date,'".CDate($_REQUEST['fdgiro_cliring_date'])."' AS fdgiro_cliring_date,'{$fnstatus}' AS fnstatus_giro,
                     '{$_REQUEST['ftnotes']}' AS ftdescription, '".CCur($_REQUEST['fcloan_payment_amount'])."'  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":
            if($_REQUEST['fnstatus']==2) {
             for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
						$strSQL['Update txloan'][]="update txloan set
												fcloan_payment=ifnull(fcloan_payment,0)-".CCur($_REQUEST['fcloan_paymentD0'][$j])." ,
												fcloan_outstanding=ifnull(fcloan_outstanding,0)+".CCur($_REQUEST['fcloan_paymentD0'][$j])."
												where ftloan_no= '{$_REQUEST['ftloan_noD0'][$j]}'
												";
							$strSQL['Update txloan'][]="update txloan set
												fnloan_payment_status=1
												where ftloan_no='{$_REQUEST['ftloan_noD0'][$j]}' and
													fcloan_payment=0
														";
							$strSQL['Update txloan'][]="update txloan set
												fnloan_payment_status=3
												where ftloan_no='{$_REQUEST['ftloan_noD0'][$j]}' and
													fcloan_outstanding=0
																			";

							$strSQL['Update txloan'][]="update txloan set
										fnloan_payment_status=2
										where ftloan_no='{$_REQUEST['ftloan_noD0'][$j]}' and
											fcloan_payment>0 and
											fcloan_outstanding>0
																	";

				}

            }

           if(($_REQUEST['fnpayment_method']==0)||($_REQUEST['fnpayment_method']==1)) {
                   $strSQL['Update txgiro_hist'][]="
                          UPDATE txgiro_hist SET fnstatus_giro=9 WHERE ftgiro_no='{$_REQUEST['ftgiro_no']}' 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']}'";
        break;
        }

        return $strSQL;
      }


?>
