<?php

function SaveData() {
    global $objAxpDB;
    $strSQL=array();

    switch($_REQUEST[mode]) {
        case "simpan":     
            
            break;
    }

 return $strSQL;
}


function SaveDataAfter() {
    global $objAxpDB;
    $strSQL=array();

    switch($_REQUEST[mode]) {
        case "simpan":
            /*
            $strSQL['Update tlstock_det1'][]= "
                UPDATE tlstock_det a
                INNER JOIN (SELECT ftitem_code,ftwarehouse_from,ftlocation_from, fdexp_date, SUM(fnqty) AS fnqty
                                FROM txstock_mutation_det
                                WHERE ftstock_mutation_no = '".$_REQUEST['ftstock_mutation_no']."'
                                GROUP BY ftitem_code,ftwarehouse_from,ftlocation_from,fdexp_date) b
                       ON a.ftitem_code=b.ftitem_code AND
                          a.ftWarehouse_Code=b.ftwarehouse_from AND
                          a.ftlocation_code=b.ftlocation_from AND
                          a.fdexp_date=b.fdexp_date
                SET a.fnqty_available=a.fnqty_available - b.fnqty";

            $strSQL['Update tlstock_det2'][]= "
                UPDATE tlstock_det a
		INNER JOIN (SELECT ftitem_code,ftwarehouse_to,ftlocation_to,fdexp_date, SUM(fnqty) AS fnqty
				FROM txstock_mutation_det
				WHERE ftstock_mutation_no = '".$_REQUEST['ftstock_mutation_no']."'
				GROUP BY ftitem_code,ftwarehouse_to,ftlocation_to,fdexp_date) b
		       ON a.ftitem_code=b.ftitem_code AND
			  a.ftWarehouse_Code=b.ftwarehouse_to AND
			  a.ftlocation_code=b.ftlocation_to AND
                          a.fdexp_date=b.fdexp_date
                SET a.fnqty_available=a.fnqty_available + b.fnqty";
            */    
                                
            for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_fromD0'][$j],$_REQUEST['ftlocation_fromD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);
                $m=SaveStock('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_toD0'][$j],$_REQUEST['ftlocation_toD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);
            }
        break;
    }

 return $strSQL;
}

function RevisiData() {
    global $objAxpDB;
    // $objAxpDB->Allow_Revisi(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "simpan_revisi":
            
            $ssql1="SELECT IFNULL(MAX(ftstock_mutation_no),'')  AS M
                                    FROM txstock_mutation_hdr
                            WHERE ftstock_mutation_no LIKE '{$_REQUEST['ftstock_mutation_no']}-%'";
            $no=RetRstField($ssql1);
            if($no!="") 
            {
                    $no1=explode("-",$no);
                    $no=(int)$no1[1]+1;
                    $str1="-{$no}";
            }
            else 
            {
                    $str1="-1";
            }
            $fttrans_no_new="{$_REQUEST['ftstock_mutation_no']}{$str1}";

            // Pengembalian Stock transaksi lama              
            $rs=mysql_query("SELECT * FROM txstock_mutation_det WHERE ftstock_mutation_no='{$_REQUEST['ftstock_mutation_no']}'");
            while($r=mysql_fetch_assoc($rs)) {
                $m=SaveStock('+',$r['ftitem_code'],'',$r['ftwarehouse_from'],$r['ftlocation_from'],'',$r['fnqty_base'],$r['fnqty_base']);
                $strSQL=array_merge_recursive($strSQL,$m);
                $m=SaveStock('-',$r['ftitem_code'],'',$r['ftwarehouse_to'],$r['ftlocation_to'],'',$r['fnqty_base'],$r['fnqty_base']);
                $strSQL=array_merge_recursive($strSQL,$m);
            }
            
            $strSQL["Revisi"][]="UPDATE txstock_mutation_hdr
                                    SET fdmodified_date=now(),ftmodified_user='{$_SESSION['username']}',
                                    fnstatus=3,ftstock_mutation_no='{$fttrans_no_new}'
                                    WHERE ftstock_mutation_no='{$_REQUEST['ftstock_mutation_no']}'";
            $strSQL["Revisi"][]="UPDATE txstock_mutation_det
                                    SET ftstock_mutation_no='{$fttrans_no_new}'
                                    where ftstock_mutation_no='{$_REQUEST['ftstock_mutation_no']}'";

            // Save Stock transaksi baru                        
            /*for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_fromD0'][$j],$_REQUEST['ftlocation_fromD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);
                $m=SaveStock('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_toD0'][$j],$_REQUEST['ftlocation_toD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);
            }*/
            
            // Call Function Revisi General Ledger
            if(function_exists("RevisiGL")) {
                $m = RevisiGL($fttrans_no_new);
                if ($m != '') { $strSQL=array_merge_recursive($strSQL,$m); }
            }
            
            break;
    }
    return $strSQL;
}
  

function RevisiDataAfter() {
    global $objAxpDB;
    // $objAxpDB->Allow_Revisi(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "simpan_revisi":
            
            // Save Stock transaksi baru                        
            for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_fromD0'][$j],$_REQUEST['ftlocation_fromD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);
                $m=SaveStock('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_toD0'][$j],$_REQUEST['ftlocation_toD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);
            }
            
            break;
    }
    return $strSQL;
}
  

function CancelData() {
    global $objAxpDB;
    // $objAxpDB->Allow_Cancel(false);
    $strSQL=array();

    switch($_REQUEST[mode]) {
    case "batal":
        if($_REQUEST['fnstatus']=='1') {
            for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                $m=SaveStock('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_fromD0'][$j],$_REQUEST['ftlocation_fromD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);
                $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_toD0'][$j],$_REQUEST['ftlocation_toD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);
            }
        }
        break;
    }

     return $strSQL;
}

?>