<?php

function SaveData() {
    global $objAxpDB;
    $strSQL=array();
    //$objAxpDB->Allow_Save(false);

    switch($_REQUEST[mode]) {
    case "simpan":
        
        $_REQUEST['ftstock_type_code'] = "inv";
        
        // auto insert stock out 
        $ssql="INSERT INTO txstock_out_hdr
                (
                    ftbranch_code,ftstock_out_no, ftstock_type_code, ftcategory_code,
                    fdtrans_date, ftreff_no, 
                    ftnotes,  fntotal_qty, fnstatus,ftcreated_user,
                    fdcreated_date,ftmodified_user,fdmodified_date
                )
                    SELECT
                    '{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftdisassembly_no']}' AS ftstock_out_no,'{$_REQUEST['ftstock_type_code']}' AS ftstock_type_code,'11' AS ftcategory_code,
                    '".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,'{$_REQUEST['ftreff_no']}' AS ftreff_no,
                    '{$_REQUEST['ftnotes']}' AS ftnotes,'".CCur($_REQUEST['fnqty'])."' AS fntotal_qty,'{$_REQUEST['fnstatus']}'  AS fnstatus,'{$_SESSION['username']}' AS ftcreated_user,
                    NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date
                ";
        $strSQL['INSERT txstock_out_hdr'][]=$ssql;
        
        $ssql="INSERT INTO txstock_out_det
                    (
                        ftbranch_code,ftstock_out_no,fnindex, ftitem_code,
                        ftwarehouse_code, ftlocation_code, 
                        fnqty,fnqty_base, ftuom_code,ffformula,
                        ftcreated_user,fdcreated_date,
                        ftmodified_user,fdmodified_date
                    )
                        SELECT  '{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftdisassembly_no']}' AS ftstock_out_no,'1' AS fnindex,'{$_REQUEST['ftitem_code']}' AS ftitem_code,
                        '{$_REQUEST['ftwarehouse_code']}' AS ftwarehouse_code,'{$_REQUEST['ftlocation_code']}' AS ftlocation_code,
                        '".CCur($_REQUEST['fnqty'])."' AS fnqty,'".CCur($_REQUEST['fnqty'])."' AS fnqty_base,'{$_REQUEST['ftuom_code']}' AS ftuom_code,'1' AS ffformula,
                        '{$_SESSION['username']}' AS ftcreated_user,now() AS fdcreated_date,
                        '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";
        $strSQL['INSERT txstock_out_det'][]=$ssql;
               		
         // auto insert stock in
        $ssql="INSERT INTO txstock_in_hdr
                (
                    ftbranch_code,ftstock_in_no, ftstock_type_code, ftcategory_code,
                    fdtrans_date, ftreff_no, 
                    ftnotes,  fntotal_qty, fnstatus,ftcreated_user,
                    fdcreated_date,ftmodified_user,fdmodified_date
                )
                    SELECT
                    '{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftdisassembly_no']}' AS ftstock_in_no,'{$_REQUEST['ftstock_type_code']}' AS ftstock_type_code,'11' AS ftcategory_code,
                    '".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,'{$_REQUEST['ftreff_no']}' AS ftreff_no,
                    '{$_REQUEST['ftnotes']}' AS ftnotes,'".CCur($_REQUEST['fnqty'])."' AS fntotal_qty,'{$_REQUEST['fnstatus']}'  AS fnstatus,'{$_SESSION['username']}' AS ftcreated_user,
                    NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date
                ";
        $strSQL['INSERT txstock_in_hdr'][]=$ssql;

        for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
            $ssql="INSERT INTO txstock_in_det
                    (
                        ftbranch_code,ftstock_in_no,fnindex, ftitem_code,
                        ftwarehouse_code, ftlocation_code, 
                        fnqty,fnqty_base, ftuom_code,ffformula,
                        ftcreated_user,fdcreated_date,
                        ftmodified_user,fdmodified_date
                    )
                        SELECT  '{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftdisassembly_no']}' AS ftstock_in_no,'{$_REQUEST['fnindexD0'][$j]}' AS fnindex,'{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                        '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code,'{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code,
                        '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty,'".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty_base,'{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code,'1' AS ffformula,
                        '{$_SESSION['username']}' AS ftcreated_user,now() AS fdcreated_date,
                        '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";
            $strSQL['INSERT txstock_out_det'][]=$ssql;
        }		

        
        $m=SaveStock('-',$_REQUEST['ftitem_code'],'',$_REQUEST['ftwarehouse_code'],$_REQUEST['ftlocation_code'],$_REQUEST['fdexp_date'],CCur($_REQUEST['fnqty']),CCur($_REQUEST['fnqty']));
        $strSQL=array_merge_recursive($strSQL,$m);
              
        for($j=1; $j < count($_REQUEST['fnindexD0']); $j++){
            $m=SaveStock('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],$_REQUEST['fdexp_dateD0'][$j],CCur($_REQUEST['fnqtyD0'][$j]),CCur($_REQUEST['fnqtyD0'][$j]));
            $strSQL=array_merge_recursive($strSQL,$m);
        }
        
    }

    return $strSQL;
 }
 
 
function SaveDataAfter(){
    global $objAxpDB;
    $objAxpDB->Allow_Save(true);
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "simpan":
            $strSQL['Update txdisassembly_hdr after'][] =  "UPDATE txdisassembly_hdr a,tlstock_hdr b SET a.ftitem_name = b.ftitem_name WHERE a.ftitem_code = b.ftitem_code AND ftdisassembly_no='{$_REQUEST['ftdisassembly_no']}' ";
            $strSQL['Update txdisassembly_det after'][] =  "UPDATE txdisassembly_det a,tlstock_hdr b SET a.ftitem_name = b.ftitem_name WHERE a.ftitem_code = b.ftitem_code AND ftdisassembly_no='{$_REQUEST['ftdisassembly_no']}' ";

            $total_qty=RetRstField("SELECT sum(fnqty) as fnqty FROM txstock_in_det WHERE ftstock_in_no = '{$_REQUEST['ftdisassembly_no']}' ");
            $strSQL['Update txstock_in_hdr after'][] =  "UPDATE txstock_in_hdr SET fntotal_qty = '{$total_qty}' WHERE ftstock_in_no = '{$_REQUEST['ftdisassembly_no']}'  ";

            break;
    }
    return $strSQL;
}

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

            // Pengembalian Stock transaksi lama     
            $rs0=mysql_query("SELECT * FROM txdisassembly_hdr WHERE ftdisassembly_no='{$_REQUEST['ftdisassembly_no']}'");
            while($r0=mysql_fetch_assoc($rs0)) {
                $m0=SaveStock('+',$r0['ftitem_code'],'',$r0['ftwarehouse_code'],$r0['ftlocation_code'],'',$r0['fnqty'],$r0['fnqty']);
                $strSQL=array_merge_recursive($strSQL,$m0);
            }
            
            $rs=mysql_query("SELECT * FROM txdisassembly_det WHERE ftdisassembly_no='{$_REQUEST['ftdisassembly_no']}'");
            while($r=mysql_fetch_assoc($rs)) {
                $m=SaveStock('-',$r['ftitem_code'],'',$r['ftwarehouse_code'],$r['ftlocation_code'],'',$r['fnqty'],$r['fnqty']);
                $strSQL=array_merge_recursive($strSQL,$m);
            }
            
            $strSQL["Revisi"][]="UPDATE txdisassembly_hdr
                                    SET fdmodified_date=now(),ftmodified_user='{$_SESSION['username']}',
                                    fnstatus=9,ftdisassembly_no='{$fttrans_no_new}'
                                    WHERE ftdisassembly_no='{$_REQUEST['ftdisassembly_no']}'";
            $strSQL["Revisi"][]="UPDATE txdisassembly_det
                                    SET ftdisassembly_no='{$fttrans_no_new}'
                                    where ftdisassembly_no='{$_REQUEST['ftdisassembly_no']}'";

            $strSQL["Revisi"][]="UPDATE txgeneral_journal_hdr
                                    SET fdmodified_date=now(),ftmodified_user='{$_SESSION['username']}',
                                    fnstatus=9,ftdocument_no='{$fttrans_no_new}'
                                    WHERE fnstatus <> 9 and ftdocument_no='{$_REQUEST['ftdisassembly_no']}'";

            // Auto revisi Stock Keluar          
            $_REQUEST['ftstock_type_code'] = "inv";
            $ftsk_no="{$_REQUEST['ftdisassembly_no']}";                     

            $ssql1="SELECT IFNULL(MAX(ftstock_out_no),'')  AS M
                    FROM txstock_out_hdr
                    WHERE ftstock_out_no LIKE '{$ftsk_no}-%'";
            $no=RetRstField($ssql1);
            if($no!="") 
            {
                    $no1=explode("-",$no);
                    $no=(int)$no1[1]+1;
                    $str1="-{$no}";
            }
            else 
            {
                    $str1="-1";
            }

            $ftsk_no_new="{$ftsk_no}{$str1}";
            
            $strSQL["Revisi"][]="UPDATE txstock_out_hdr
                                    SET fnstatus=3,ftdocument_no='{$ftsk_no_new}',ftstock_out_no = '{$ftsk_no_new}',
                                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                    WHERE ftstock_out_no='{$ftsk_no}'"; 
            $strSQL["Revisi"][]="UPDATE txstock_out_det
                                    SET ftstock_out_no = '{$ftsk_no_new}',
                                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                    WHERE ftstock_out_no='{$ftsk_no}'";        
                                    
            // Auto revisi Stock Masuk
            $ftsm_no="{$_REQUEST['ftdisassembly_no']}";                     

            $ssql1="SELECT IFNULL(MAX(ftstock_in_no),'')  AS M
                    FROM txstock_in_hdr
                    WHERE ftstock_in_no LIKE '{$ftsm_no}-%'";
            $no=RetRstField($ssql1);
            if($no!="") 
            {
                    $no1=explode("-",$no);
                    $no=(int)$no1[1]+1;
                    $str1="-{$no}";
            }
            else 
            {
                    $str1="-1";
            }

            $ftsm_no_new="{$ftsm_no}{$str1}";
                         
            $strSQL["Revisi"][]="UPDATE txstock_in_hdr
                                    SET fnstatus=3,ftdocument_no='{$ftsm_no_new}',ftstock_in_no = '{$ftsm_no_new}',
                                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                    WHERE ftstock_in_no='{$ftsm_no}'"; 
            $strSQL["Revisi"][]="UPDATE txstock_in_det
                                    SET ftstock_in_no = '{$ftsm_no_new}',
                                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                    WHERE ftstock_in_no='{$ftsm_no}'";                                
                                    
            // auto insert stock out 
            $ssql="INSERT INTO txstock_out_hdr
                    (
                        ftbranch_code,ftstock_out_no, ftstock_type_code, ftcategory_code,
                        fdtrans_date, ftreff_no, 
                        ftnotes,  fntotal_qty, fnstatus,ftcreated_user,
                        fdcreated_date,ftmodified_user,fdmodified_date
                    )
                        SELECT
                        '{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftdisassembly_no']}' AS ftstock_out_no,'{$_REQUEST['ftstock_type_code']}' AS ftstock_type_code,'11' AS ftcategory_code,
                        '".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,'{$_REQUEST['ftreff_no']}' AS ftreff_no,
                        '{$_REQUEST['ftnotes']}' AS ftnotes,'".CCur($_REQUEST['fnqty'])."' AS fntotal_qty,'{$_REQUEST['fnstatus']}'  AS fnstatus,'{$_SESSION['username']}' AS ftcreated_user,
                        NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date
                    ";
            $strSQL['INSERT txstock_out_hdr'][]=$ssql;

            $ssql="INSERT INTO txstock_out_det
                        (
                            ftbranch_code,ftstock_out_no,fnindex, ftitem_code,
                            ftwarehouse_code, ftlocation_code, 
                            fnqty,fnqty_base, ftuom_code,ffformula,
                            ftcreated_user,fdcreated_date,
                            ftmodified_user,fdmodified_date
                        )
                            SELECT  '{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftdisassembly_no']}' AS ftstock_out_no,'1' AS fnindex,'{$_REQUEST['ftitem_code']}' AS ftitem_code,
                            '{$_REQUEST['ftwarehouse_code']}' AS ftwarehouse_code,'{$_REQUEST['ftlocation_code']}' AS ftlocation_code,
                            '".CCur($_REQUEST['fnqty'])."' AS fnqty,'".CCur($_REQUEST['fnqty'])."' AS fnqty_base,'{$_REQUEST['ftuom_code']}' AS ftuom_code,'1' AS ffformula,
                            '{$_SESSION['username']}' AS ftcreated_user,now() AS fdcreated_date,
                            '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";
            $strSQL['INSERT txstock_out_det'][]=$ssql;

             // auto insert stock in
            $ssql="INSERT INTO txstock_in_hdr
                    (
                        ftbranch_code,ftstock_in_no, ftstock_type_code, ftcategory_code,
                        fdtrans_date, ftreff_no, 
                        ftnotes,  fntotal_qty, fnstatus,ftcreated_user,
                        fdcreated_date,ftmodified_user,fdmodified_date
                    )
                        SELECT
                        '{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftdisassembly_no']}' AS ftstock_in_no,'{$_REQUEST['ftstock_type_code']}' AS ftstock_type_code,'11' AS ftcategory_code,
                        '".CDate($_REQUEST['fdtrans_date'])."' AS fdtrans_date,'{$_REQUEST['ftreff_no']}' AS ftreff_no,
                        '{$_REQUEST['ftnotes']}' AS ftnotes,'".CCur($_REQUEST['fnqty'])."' AS fntotal_qty,'{$_REQUEST['fnstatus']}'  AS fnstatus,'{$_SESSION['username']}' AS ftcreated_user,
                        NOW() AS fdcreated_date,'{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date
                    ";
            $strSQL['INSERT txstock_in_hdr'][]=$ssql;

            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                $ssql="INSERT INTO txstock_in_det
                        (
                            ftbranch_code,ftstock_in_no,fnindex, ftitem_code,
                            ftwarehouse_code, ftlocation_code, 
                            fnqty,fnqty_base, ftuom_code,ffformula,
                            ftcreated_user,fdcreated_date,
                            ftmodified_user,fdmodified_date
                        )
                            SELECT  '{$_SESSION['strbranch']}' AS ftbranch_code,'{$_REQUEST['ftdisassembly_no']}' AS ftstock_in_no,'{$_REQUEST['fnindexD0'][$j]}' AS fnindex,'{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                            '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code,'{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code,
                            '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty,'".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty_base,'{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code,'1' AS ffformula,
                            '{$_SESSION['username']}' AS ftcreated_user,now() AS fdcreated_date,
                            '{$_SESSION['username']}' AS ftmodified_user,NOW() AS fdmodified_date";
                $strSQL['INSERT txstock_out_det'][]=$ssql;
            }		

            // 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;
    $strSQL=array();
    
    switch($_REQUEST[mode]) {
        case "simpan_revisi":
            // Save Stock transaksi baru                        
            $m=SaveStock('-',$_REQUEST['ftitem_code'],'',$_REQUEST['ftwarehouse_code'],$_REQUEST['ftlocation_code'],'',CCur($_REQUEST['fnqty']),CCur($_REQUEST['fnqty']));
            $strSQL=array_merge_recursive($strSQL,$m);
            
            for($j=1; $j<count($_REQUEST['ftitem_codeD0']); $j++) {
                $m=SaveStock('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqtyD0'][$j]),CCur($_REQUEST['fnqtyD0'][$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') {
        
            $m=SaveStock('+',$_REQUEST['ftitem_code'],'',$_REQUEST['ftwarehouse_code'],$_REQUEST['ftlocation_code'],$_REQUEST['fdexp_date'],CCur($_REQUEST['fnqty']),CCur($_REQUEST['fnqty']));
            $strSQL=array_merge_recursive($strSQL,$m);

            for($j=1; $j < count($_REQUEST['fnindexD0']); $j++){
                $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],$_REQUEST['fdexp_dateD0'][$j],CCur($_REQUEST['fnqtyD0'][$j]),CCur($_REQUEST['fnqtyD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);
            }
            
            $strSQL['Update txstock_in_hdr'][] =  "UPDATE txstock_in_hdr SET fnstatus = 9 WHERE ftstock_in_no = '{$_REQUEST['ftdisassembly_no']}'  ";
            $strSQL['Update txstock_out_hdr'][] =  "UPDATE txstock_out_hdr SET fnstatus = 9 WHERE ftstock_out_no = '{$_REQUEST['ftdisassembly_no']}'  ";

            $strSQL['Update txgeneral_journal_hdr'][] =  "UPDATE txgeneral_journal_hdr SET fnstatus = 9 WHERE ftdocument_no = '{$_REQUEST['ftdisassembly_no']}'  ";
        
        }
        break;
    }

     return $strSQL;
}


