<?php
if(!function_exists(ValAR)){
    function ValAR() {
        if(RetRstField("SELECT fnid FROM tlcustomer WHERE ftcustomer_code='{$_REQUEST['ftcustomer_code']}'")) {
            echo"<script>alert('Customer tidak valid...'); window.history.back();</script>";
            exit;
        }
    }
}

function SaveData() {
    global $objAxpDB;
    // $objAxpDB->Allow_Save(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "simpan":
            /* Case referensi menggunakan SO
            $sql_status = " SELECT CASE WHEN ((fcamount-fcpayment_amount-fcreturn_amount) <= 0)  THEN 3
                               WHEN (fcpayment_amount = 0)  OR (fcamount = fcreturn_amount) THEN 1
                               ELSE 2 END AS fnpayment_status
                        FROM txsales_invoice_hdr
                        WHERE ftso_no='{$_REQUEST['ftso_no']}'";
            $ftsales_invoice_no = RetRstField("SELECT ftsales_invoice_no FROM txsales_invoice_hdr where ftso_no = '{$_REQUEST['ftso_no']}' ");                
            */
            
            $sql_status = " SELECT CASE WHEN ((fcamount-fcpayment_amount-fcreturn_amount) <= 0)  THEN 3
                               WHEN (fcpayment_amount = 0)  OR (fcamount = fcreturn_amount) THEN 1
                               ELSE 2 END AS fnpayment_status
                        FROM txsales_invoice_hdr
                        WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";
            $ftso_no = RetRstField("SELECT ftso_no FROM txsales_invoice_hdr where ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_no']}' ");                
            
            // insert stock in hdr                
            $ftstock_in_no=getAutoNumber($_SESSION['strbranch'],'inv006','txstock_in_hdr','ftstock_in_no',$_REQUEST['fdtrans_date']);
            $dt=getDateYmd($_REQUEST["fdtrans_date"]);
            
            $strSQL['Insert Stock In Hdr'][]="
                INSERT INTO txstock_in_hdr
                (ftbranch_code, ftstock_in_no, ftcategory_code,
                ftstock_in_company, ftdocument_no, fdtrans_date,
                fntotal_qty, ftnotes, fnstatus, ftcreated_user,
                fdcreated_date, ftmodified_user, fdmodified_date, ftstock_type_code)
                SELECT
                '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftstock_in_no}' AS ftstock_in_no, '4' AS ftcategory_code,
                '{$_REQUEST['ftcustomer_code']}' AS  ftstock_in_company, '{$_REQUEST['ftsales_return_no']}' AS ftdocument_no,'{$dt}' AS  fdtrans_date,
                '0' AS fntotal_qty,'{$_REQUEST['ftnotes']}' AS  ftnotes, '1' AS fnstatus,  '{$_SESSION['username']}' AS ftcreated_user,
                now() AS fdcreated_date, '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date, 'inv' as ftstock_type_code";
            
            // transaksi tanpa no so/si otomatis tidak akan update qty retur ke so/si yg bersangkutan
            // update qty retur di so dan si
            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                if (CCur($_REQUEST['fnqtyD0'][$j]) > 0) {
                    $strSQL['Insert Stock In Det'][]="
                         INSERT INTO txstock_in_det
                         (ftbranch_code, ftstock_in_no, fnindex, ftitem_code,
                         ftwarehouse_code, ftlocation_code,fnindex_po,
                         fnqty, ftuom_code, fnqty_base,ffformula,
                         ftcreated_user, fdcreated_date, ftmodified_user)
                         SELECT
                         '{$_SESSION['strbranch']}' AS ftbranch_code,'{$ftstock_in_no}' AS ftstock_in_no, '{$j}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                         '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code, '{$j}' AS fnindex_po,
                         '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                         '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date, '{$_SESSION['username']}' AS ftmodified_user";

                    $strSQL['Update qty retur txsales_order_det'][]=
                                "UPDATE txsales_order_det
                                    SET fnqty_retur=IFNULL(fnqty_retur,0)+".CCur($_REQUEST['fnqtyD0'][$j]).",
                                        fnqty_retur_base=IFNULL(fnqty_retur_base,0)+".CCur($_REQUEST['fnqty_baseD0'][$j])."
                                    WHERE ftso_no='{$ftso_no}' AND ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND fnindex='{$_REQUEST['fnindex_siD0'][$j]}'";

                    $strSQL['Update qty retur txsales_invoice_det'][]=
                                "UPDATE txsales_invoice_det
                                    SET fnqty_retur=IFNULL(fnqty_retur,0)+".CCur($_REQUEST['fnqtyD0'][$j]).",
                                        fnqty_retur_base=IFNULL(fnqty_retur_base,0)+".CCur($_REQUEST['fnqty_baseD0'][$j])."
                                    WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}' AND ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND fnindex='{$_REQUEST['fnindex_siD0'][$j]}'";

                    $m=SaveStock('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                    $strSQL=array_merge_recursive($strSQL,$m);    
                }    
            }

            $strSQL['Update txsales_order_hdr'][]=
                        "UPDATE txsales_order_hdr
                            SET fcreturn_amount=IFNULL(fcreturn_amount,0)+".CCur($_REQUEST['fcamount'])."
                            WHERE ftso_no='{$ftso_no}'";

            $strSQL['Update txsales_invoice_hdr'][]=
                        "UPDATE txsales_invoice_hdr
                            SET fcreturn_amount=IFNULL(fcreturn_amount,0)+".CCur($_REQUEST['fcamount'])."
                            WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";

            
            break;
    }    
                        
    return $strSQL;

}

function SaveDataAfter() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan":
        $status_payment = 0;
        
        $sql_status = " SELECT CASE WHEN ((fcamount-fcpayment_amount-fcreturn_amount) <= 0)  THEN 3
                               WHEN (fcpayment_amount = 0)  OR (fcamount = fcreturn_amount) THEN 1
                               ELSE 2 END AS fnpayment_status
                        FROM txsales_invoice_hdr
                        WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";
        $status_payment = RetRstField($sql_status);                
        
        $strSQL["Update Status Payment SO"][]="UPDATE txsales_order_hdr SET fnpayment_status = '{$status_payment}' WHERE ftso_no='{$ftso_no}'";
        $strSQL["Update Status Payment SI"][]="UPDATE txsales_invoice_hdr SET fnpayment_status = '{$status_payment}' WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";
        
        $stock_in_no=RetRstField("SELECT ftstock_in_no FROM txstock_in_hdr WHERE ftdocument_no = '{$_REQUEST['ftsales_return_no']}' AND fnstatus !=3 AND fnstatus!=9 ");
        $total_qty=RetRstField("SELECT sum(fnqty) as fnqty FROM txstock_in_det WHERE ftstock_in_no = '{$stock_in_no}' ");
        $strSQL['Update txstock_in_hdr after'][] =  "UPDATE txstock_in_hdr SET fntotal_qty = '{$total_qty}' WHERE ftstock_in_no = '{$stock_in_no}'  ";

    break;

    }

    return $strSQL;
}


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

            $strSQL["Revisi"][]="UPDATE txsales_return_hdr
                                    SET fnstatus=3,ftsales_return_no='{$ftsr_no_new}',
                                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                    WHERE ftsales_return_no='{$_REQUEST['ftsales_return_no']}'";
            $strSQL["Revisi"][]="UPDATE txsales_return_det
                                    SET ftsales_return_no='{$ftsr_no_new}',
                                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                    where ftsales_return_no='{$_REQUEST['ftsales_return_no']}'";
                                    
            // Auto Revisi Stock In, Get No Revisi
            $ftstock_in_no=RetRstField("SELECT ftstock_in_no FROM txstock_in_hdr WHERE ftdocument_no = '{$_REQUEST['ftsales_return_no']}' AND ftcategory_code = 4 ");      
            $ftso_no = RetRstField("SELECT ftso_no FROM txsales_invoice_hdr where ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_no']}' ");                
                    
            // Pengembalian Stock transaksi lama              
            $rs=mysql_query("SELECT * FROM txstock_in_det WHERE ftstock_in_no='{$ftstock_in_no}'");
            while($r=mysql_fetch_assoc($rs)) {
                $strSQL['Update qty retur txsales_order_det'][]=
                            "UPDATE txsales_order_det
                                SET fnqty_retur=IFNULL(fnqty_retur,0)-".$r['fnqty_base'].",
                                    fnqty_retur_base=IFNULL(fnqty_retur_base,0)+".$r['fnqty_base']."
                                WHERE ftso_no='{$ftso_no}' AND ftitem_code='{$r['ftitem_code']}' AND fnindex='{$r['fnindex_po']}'";

                $strSQL['Update qty retur txsales_invoice_det'][]=
                            "UPDATE txsales_invoice_det
                                SET fnqty_retur=IFNULL(fnqty_retur,0)+".$r['fnqty_base'].",
                                    fnqty_retur_base=IFNULL(fnqty_retur_base,0)+".$r['fnqty_base']."
                                WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}' AND ftitem_code='{$r['ftitem_code']}' AND fnindex='{$r['fnindex_po']}'";
                                    
                $m=SaveStock('-',$r['ftitem_code'],'',$r['ftwarehouse_code'],$r['ftlocation_code'],'',$r['fnqty_base'],$r['fnqty_base']);
                $strSQL=array_merge_recursive($strSQL,$m);
            }

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

            $ftstock_in_no_new="{$ftstock_in_no}{$str1}";
                
            $strSQL["Revisi"][]="UPDATE txstock_in_hdr
                                    SET fnstatus=3,ftstock_in_no='{$ftstock_in_no_new}',ftdocument_no = '{$ftsr_no_new}',
                                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                    WHERE ftstock_in_no='{$ftstock_in_no}'"; 
            $strSQL["Revisi"][]="UPDATE txstock_in_det
                                    SET ftstock_in_no = '{$ftstock_in_no_new}',
                                        ftmodified_user='{$_SESSION['username']}',fdmodified_date=now()
                                    WHERE ftstock_in_no='{$ftstock_in_no}'";        

            $fdtrans_date=getDateYmd($_REQUEST["fdtrans_date"]);

            $ssql = mysql_query("SELECT ftcustomer_name FROM tlcustomer WHERE ftcustomer_code='{$_REQUEST["ftcustomer_code"]}'");
            $r = mysql_fetch_assoc($ssql);

            $strSQL['Insert Stock In Hdr'][]="
                INSERT INTO txstock_in_hdr
                    (ftbranch_code, ftstock_in_no, ftcategory_code,
                    ftstock_in_company, ftdocument_no, fdtrans_date,
                    fntotal_qty, ftnotes, fnstatus, ftcreated_user,
                    fdcreated_date, ftmodified_user, fdmodified_date, ftstock_type_code)
                SELECT
                    '{$_SESSION['strbranch']}' AS ftbranch_code, '{$ftstock_in_no}' AS ftstock_in_no, '4' AS ftcategory_code,
                    '{$_REQUEST['ftcustomer_code']}' AS  ftstock_in_company, '{$_REQUEST['ftsales_return_no']}' AS ftdocument_no,'{$fdtrans_date}' AS  fdtrans_date,
                    '0' AS fntotal_qty,'{$_REQUEST['ftnotes']}' AS  ftnotes, '1' AS fnstatus,  '{$_SESSION['username']}' AS ftcreated_user,
                    now() AS fdcreated_date, '{$_SESSION['username']}' AS ftmodified_user, now() AS fdmodified_date, 'inv' as ftstock_type_code";
            
            // transaksi tanpa no so/si otomatis tidak akan update qty retur ke so/si yg bersangkutan
            // update qty retur di so dan si
            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                if (CCur($_REQUEST['fnqtyD0'][$j]) > 0) {
                    $strSQL['Insert Stock In Det'][]="
                         INSERT INTO txstock_in_det
                         (ftbranch_code, ftstock_in_no, fnindex, ftitem_code,
                         ftwarehouse_code, ftlocation_code,fnindex_po,
                         fnqty, ftuom_code, fnqty_base,ffformula,
                         ftcreated_user, fdcreated_date, ftmodified_user)
                         SELECT
                         '{$_SESSION['strbranch']}' AS ftbranch_code,'{$ftstock_in_no}' AS ftstock_in_no, '{$j}' AS fnindex, '{$_REQUEST['ftitem_codeD0'][$j]}' AS ftitem_code,
                         '{$_REQUEST['ftwarehouse_codeD0'][$j]}' AS ftwarehouse_code, '{$_REQUEST['ftlocation_codeD0'][$j]}' AS ftlocation_code, '{$j}' AS fnindex_po,
                         '".CCur($_REQUEST['fnqtyD0'][$j])."' AS fnqty, '{$_REQUEST['ftuom_codeD0'][$j]}' AS ftuom_code, '".CCur($_REQUEST['fnqty_baseD0'][$j])."' AS fnqty_base, '".CCur($_REQUEST['ffformulaD0'][$j])."' AS ffformula,
                         '{$_SESSION['username']}' AS ftcreated_user, now() AS fdcreated_date, '{$_SESSION['username']}' AS ftmodified_user";

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

    }

    return $strSQL;
}    


function RevisiDataLast() {
    global $objAxpDB;
    //$objAxpDB->Allow_Revisi(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "simpan_revisi":
             
            break;

    }

    return $strSQL;
}        


function RevisiDataAfter() {
    global $objAxpDB;
    $strSQL=array();
    
    switch($_REQUEST[mode]) {
        case "simpan_revisi":
            
            // Penambahan stock karena retur
            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                if((float)CCur($_REQUEST['fnqty_baseD0'][$j])>0){

                    $m=SaveStock('+',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                    $strSQL=array_merge_recursive($strSQL,$m);    

                }
            }

            // Update Status Payment
            $status_payment = 0;
        
            $sql_status = " SELECT CASE WHEN ((fcamount-fcpayment_amount-fcreturn_amount) <= 0)  THEN 3
                                   WHEN (fcpayment_amount = 0)  OR (fcamount = fcreturn_amount) THEN 1
                                   ELSE 2 END AS fnpayment_status
                            FROM txsales_invoice_hdr
                            WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";
            $status_payment = RetRstField($sql_status);                

            $strSQL["Update Status Payment SO"][]="UPDATE txsales_order_hdr SET fnpayment_status = '{$status_payment}' WHERE ftso_no='{$ftso_no}'";
            $strSQL["Update Status Payment SI"][]="UPDATE txsales_invoice_hdr SET fnpayment_status = '{$status_payment}' WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";

            $stock_in_no=RetRstField("SELECT ftstock_in_no FROM txstock_in_hdr WHERE ftdocument_no = '{$_REQUEST['ftsales_return_no']}' AND fnstatus !=3 AND fnstatus!=9 ");
            $total_qty=RetRstField("SELECT sum(fnqty) as fnqty FROM txstock_in_det WHERE ftstock_in_no = '{$stock_in_no}' ");
            $strSQL['Update txstock_in_hdr after'][] =  "UPDATE txstock_in_hdr SET fntotal_qty = '{$total_qty}' WHERE ftstock_in_no = '{$stock_in_no}'  ";

            break;

    }

    return $strSQL;
}


function CancelData() {

    global $objAxpDB;
    // $objAxpDB->Allow_Cancel(false);
    $strSQL=array();
    switch($_REQUEST[mode]) {
        case "batal":

            // $ftsales_invoice_no = RetRstField("SELECT ftsales_invoice_no FROM txsales_invoice_hdr WHERE ftso_no = '{$_REQUEST['ftso_no']}' ");                
    
            //update qty retur di so dan si
            for($j=1; $j<count($_REQUEST['fnindexD0']); $j++) {
                $strSQL['Update qty retur txsales_order_det'][]="
                        UPDATE txsales_order_det
                        SET fnqty_retur=IFNULL(fnqty_retur,0)-".CCur($_REQUEST['fnqtyD0'][$j]).",
                            fnqty_retur_base=IFNULL(fnqty_retur_base,0)-".CCur($_REQUEST['fnqty_baseD0'][$j])."
                        WHERE ftso_no='{$ftso_no}' AND
                                ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND fnindex='{$_REQUEST['fnindex_siD0'][$j]}'";
                                
                $strSQL['Update qty retur txsales_invoice_det'][]="
                        UPDATE txsales_invoice_det
                        SET fnqty_retur=IFNULL(fnqty_retur,0)-".CCur($_REQUEST['fnqtyD0'][$j]).",
                            fnqty_retur_base=IFNULL(fnqty_retur_base,0)-".CCur($_REQUEST['fnqty_baseD0'][$j])."
                        WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}' AND
                                ftitem_code='{$_REQUEST['ftitem_codeD0'][$j]}' AND fnindex='{$_REQUEST['fnindex_siD0'][$j]}'";         
                                
                $m=SaveStock('-',$_REQUEST['ftitem_codeD0'][$j],'',$_REQUEST['ftwarehouse_codeD0'][$j],$_REQUEST['ftlocation_codeD0'][$j],'',CCur($_REQUEST['fnqty_baseD0'][$j]),CCur($_REQUEST['fnqty_baseD0'][$j]));
                $strSQL=array_merge_recursive($strSQL,$m);                
            }

            $strSQL['Update txsales_order_hdr'][]=
                    "UPDATE txsales_order_hdr
                        SET fcreturn_amount=IFNULL(fcreturn_amount,0)-".CCur($_REQUEST['fcamount'])."
                        WHERE ftso_no='{$ftso_no}'";
                        
            $strSQL['Update txsales_invoice_hdr'][]=
                    "UPDATE txsales_invoice_hdr
                        SET fcreturn_amount=IFNULL(fcreturn_amount,0)-".CCur($_REQUEST['fcamount'])."
                        WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";            

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

    return $strSQL;
}
      

function CancelDataAfter() {
    global $objAxpDB;
    $strSQL=array();
    switch($_REQUEST[mode]) {
    case "simpan":
        $status_payment = 0;
        
        $sql_status = " SELECT CASE WHEN ((fcamount-fcpayment_amount-fcreturn_amount) <= 0)  THEN 3
                               WHEN (fcpayment_amount = 0)  OR (fcamount = fcreturn_amount) THEN 1
                               ELSE 2 END AS fnpayment_status
                        FROM txsales_invoice_hdr
                        WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";
                   
        $status_payment = RetRstField($sql_status);                
        
        $strSQL["Update Status Payment SO"][]="UPDATE txsales_order_hdr SET fnpayment_status = {$status_payment} WHERE ftso_no='{$ftso_no}'";
        $strSQL["Update Status Payment SI"][]="UPDATE txsales_invoice_hdr SET fnpayment_status = {$status_payment} WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";

    break;

    }

    return $strSQL;
}
      

