<?php
    if(!function_exists(ValBatalPR)){
      function ValBatalPR() {
          $ssql="select ftpr_no
                  from txpurchase_request_det
                  where fnstatus<>9 and ftpr_no='{$_REQUEST['ftpr_no']}'";

          $rs=mysql_query($ssql) or die ("query error {$ssql}");
          $ftpr_no="";
          while($r=mysql_fetch_assoc($rs)) {
             $ftpr_no.="- {$r['ftpr_no']}\\n";

          }
          if($ftstock_in_no!="") {
               $massage=" Transaksi Purchase Request berikut harus dibatalkan terlebih dahulu \\n";
               $massage.="---------------------------------------------------------\\n";
               $massage.=$ftpr_no;
          }

          if($massage!="") {
            echo "<script>
                  alert('{$massage}');
                  window.history.back();
              </script>";
              die("");
          }
      }
    }

    function SaveData() {
        global $objAxpDB;
        $strSQL=array();
        switch($_REQUEST[mode]) {
			case "simpan":
			
			break;
		}
        return $strSQL;
    }

    function SaveDataLast() {
        global $objAxpDB;

        $strSQL=array();
        switch($_REQUEST[mode]) {
			case "simpan":
	
				$strSQL["Update txpurchase_request_det 1"][] ="update txpurchase_request_det set fdclosing_date='".CDate($_REQUEST['fdtrans_date'])."',fnstatus=1 where ftpr_no='{$_REQUEST['ftpr_no']}'";
				
        }
        return $strSQL;
    }
  
    function CancelData() {
        ValBatalPR();

        global $objAxpDB;
        $objAxpDB->Allow_Cancel(true);
        $strSQL=array();
        switch($_REQUEST[mode]) {
			case "batal":
              $strSQL["Final txpurchase_request_hdr"][] ="
						UPDATE txpurchase_request_hdr
							SET ftmodified_user='{$_SESSION['username']}',fdmodified_date=now(),
								fnreceive_status=9,
								fnstatus=9 ,
								fdcancel_date='".CDate($_REQUEST['fdcancel_date'])."'
							WHERE ftpr_no='{$_REQUEST['ftpr_no']}'";
			break;
        }
        return $strSQL;
    }

    function RevisiData() {
		ValBatalPR();
		global $objAxpDB;
		$objAxpDB->Allow_Revisi(true);
		$strSQL=array();
		switch($_REQUEST[mode]) {
			case "simpan_revisi":
				$ssql1="SELECT IFNULL(MAX(ftpr_no),'')  AS M
							FROM txpurchase_request_hdr
						WHERE ftpr_no LIKE '{$_REQUEST['ftpr_no']}-%'";
				$no=RetRstField($ssql1);
				if($no!="") 
				{
					$no1=explode("-",$no);
					$no=(int)$no1[1]+1;
					$str1="-{$no}";
				}
				else 
				{
					$str1="-1";
				}
                                
				$ftpr_no_new="{$_REQUEST['ftpr_no']}{$str1}";
				$strSQL["Revisi"][]="UPDATE txpurchase_request_hdr
										SET fdrevisi_date=now(),ftrevisi_user='{$_SESSION['username']}',
										fnstatus=3,ftpr_no='{$ftpr_no_new}'
									 WHERE ftpr_no='{$_REQUEST['ftpr_no']}'";
				$strSQL["Revisi"][]="UPDATE txpurchase_request_det
										SET ftpr_no='{$ftpr_no_new}'
									 where ftpr_no='{$_REQUEST['ftpr_no']}'";

				if($_REQUEST['fnreceipt_status']!=1) {
					$strSQL["Revisi"][]="
						INSERT INTO txpurchase_request_hdr
						(
							ftpr_no, ftreference, fdtrans_date, ftnotes, fdcancel_date, fnstatus,fnpr_type
						)
                                                SELECT '{$_REQUEST['ftpr_no']}' AS ftpr_no, ftreference, fdtrans_date, ftnotes, fdcancel_date,
                                                        '1' as fnstatus, fnpr_type
                                                FROM  txpurchase_request_hdr
                                                WHERE ftpr_no='{$ftpr_no_new}'";
                                                            $strSQL["Revisi"][]="INSERT INTO txpurchase_request_det
                                                (
                                                    ftpr_no, fnindex, ftitem_code,fnqty,ffformula,fnqty_base, fnqty_accept,fnqty_accept_base, ftuom_code
                                                    fdclosing_date,fnstatus
                                                )
                                                SELECT '{$_REQUEST['ftpr_no']}' AS ftpr_no, fnindex, ftitem_code,fnqty,ffformula,fnqty_base, fnqty_accept,fnqty_accept_base, ftuom_code
                                                    fdclosing_date,fnstatus
                                                FROM txpurchase_request_det
                                                WHERE ftpr_no='{$ftpr_no_new}'";
                    
				}	
				else {
				
					$strSQL["Revisi"][]="
							UPDATE txpurchase_request_hdr
								SET ftpr_no='{$ftpr_no_new}'
							WHERE ftpr_no='{$_REQUEST['ftpr_no']}'";
				}
			break;
		}
		return $strSQL;
	}

	function RevisiDataLast() {
        global $objAxpDB;
        $strSQL=array();
        switch($_REQUEST[mode]) {
            case "simpan_revisi":
            if($_REQUEST['fnreceipt_status']==1) {
                $strSQL["Update txpurchase_request_det 1"][] ="update txpurchase_request_det set fdclosing_date='".CDate($_REQUEST['fdtrans_date'])."',fnstatus=1 where ftpr_no='{$_REQUEST['ftpr_no']}'";
                
				
            }
        }
        return $strSQL;
    }

    function FinalData() {
        global $objAxpDB;
        $strSQL=array();
        switch($_REQUEST[mode]) {
          case "final":
          $strSQL['Delete txpurchase_request_det'][]="DELETE FROM txpurchase_request_det WHERE ftpr_no='{$_REQUEST['ftpr_no']}'";

          for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
            $dt=getDateYmd($_REQUEST['fdclosing_dateD0'][$j]);
            $strSQL['Insert txpurchase_request_det'][]="
                   INSERT INTO txpurchase_request_det
                        (ftbranch_code,ftpr_no, fnindex, ftitem_code,fnqty,
                         ftuom_code,ffformula, fnqty_base, ftuom_code_base,fnqty_accept,
                         fnqty_accept_base,fdclosing_date, fnstatus,
                         ftcreated_user,fdcreated_date, ftmodified_user,fdmodified_date
                        )
                        SELECT
                        '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftpr_no']}' AS ftpr_no, '{$j}' AS fnindex,
                        '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code, '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty,
                        '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                        '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '{$_REQUEST['ftuom_code_baseD0'][$j]}' AS ftuom_code_base,
                        '".CCur($_REQUEST['fnqty_acceptD0'][$j])."' AS fnqty_accept, '".CCur($_REQUEST['fnqty_accept_baseD0'][$j])."' AS fnqty_accept_base,
                        '{$dt}' AS fdclosing_date, '{$_REQUEST['fnstatusD0'][$j]}' AS fnstatus,
                        '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                        '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";
          }

          $strSQL['Update txpurchase_request_hdr'][]="UPDATE txpurchase_request_hdr SET fnstatus=1 WHERE ftpr_no='{$_REQUEST["ftpr_no"]}'";

          break;
	}

        return $strSQL;
    }

    function UpdateData() {
        global $objAxpDB;
        $strSQL=array();
        switch($_REQUEST[mode]) {
          case "update":
            $strSQL['Delete txpurchase_request_det'][]="DELETE FROM txpurchase_request_det WHERE ftpr_no='{$_REQUEST['ftpr_no']}'";

            for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                $dt=getDateYmd($_REQUEST['fdclosing_dateD0'][$j]);
                $fnstatus=(intval($_REQUEST['fnstatusD0'][$j]) == 0)? 1: $_REQUEST['fnstatusD0'][$j];
                $strSQL['Insert txpurchase_request_det'][]="
                        INSERT INTO txpurchase_request_det
                             (ftbranch_code,ftpr_no, fnindex, ftitem_code,fnqty,
                              ftuom_code,ffformula, fnqty_base, ftuom_code_base,fnqty_accept,
                              fnqty_accept_base,fdclosing_date, fnstatus,
                              ftcreated_user,fdcreated_date, ftmodified_user,fdmodified_date
                             )
                             SELECT
                             '{$_SESSION['strbranch']}' AS ftbranch_code, '{$_REQUEST['ftpr_no']}' AS ftpr_no, '{$j}' AS fnindex,
                             '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code, '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty,
                             '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                             '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '{$_REQUEST['ftuom_code_baseD0'][$j]}' AS ftuom_code_base,
                             '".CCur($_REQUEST['fnqty_acceptD0'][$j])."' AS fnqty_accept, '".CCur($_REQUEST['fnqty_accept_baseD0'][$j])."' AS fnqty_accept_base,
                             '{$dt}' AS fdclosing_date, '{$fnstatus}' AS fnstatus,
                             '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date,
                             '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date";
            }
            break;
           
	}

        return $strSQL;
    }
    
    function UpdateDataLast() {
        global $objAxpDB;
        $strSQL=array();
        switch($_REQUEST[mode]) {
          case "close_pr":
            // set status item = close
            $strSQL["Update txpurchase_request_det"][] =
                        "UPDATE txpurchase_request_det
                            SET fnstatus = 5, ftmodified_user = '{$_SESSION['username']}', fdmodified_date = now()
                            WHERE ftpr_no='{$_REQUEST['ftpr_no']}' and fnstatus = 1";

            // set status hdr = approved                
            $strSQL['Update txpurchase_request_hdr'][] =
                        "UPDATE txpurchase_request_hdr SET fnstatus=5 WHERE ftpr_no='{$_REQUEST["ftpr_no"]}'";

            break;
	}

        return $strSQL;
    }