<?php
    function fifo_save() {
       
        switch($_REQUEST['form']) {
            // module saldo awal
            case "saldoawal_stock_bahan_baku":
            case "saldoawal_stock_setengah_jadi":
            case "saldoawal_stock_sparepart":
            case "saldoawal_stock":
                // initialize variable for multiple tables
                $tablename = "";
                if($_REQUEST['form']=='saldoawal_stock_bahan_baku') {
                    $tablename = "_bahan_baku";
                } elseif ($_REQUEST['form']=='saldoawal_stock_setengah_jadi') {
                    $tablename = "_setengah_jadi";
                } elseif ($_REQUEST['form']=='saldoawal_stock_sparepart') {
                    $tablename = "_sparepart";
                } elseif ($_REQUEST['form']=='saldoawal_stock') {
                    $tablename = "";
                }

                switch($_REQUEST['mode']) {
                    case "simpan":
                        $ssql="select a.ftstock_in_no,a.fdtrans_date, b.fnindex,b.ftitem_code,ifnull(b.fnqty_base,0) as fnqty_base, b.ftlocation_code,
                                    b.fcprice as fcprice_cogs
                                FROM txinnitial_stock_hdr{$tablename} a inner join txinnitial_stock_det{$tablename} b ON a.ftstock_in_no=b.ftstock_in_no
                                WHERE a.ftstock_in_no = '{$_REQUEST['ftstock_in_no']}' ";

                        $rs=mysql_query($ssql) or die($ssql);
                        while($r=mysql_fetch_assoc($rs)) {
                              SaveFIFO($_SESSION['strbranch'],"txinnitial_stock_hdr{$tablename}",$r['ftstock_in_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'],  $r['fcprice_cogs'], $r['ftlocation_code'],"","","");
                        }

                        break;
                    case "batal":
                        CancelFIFO($_SESSION['strbranch'],"txinnitial_stock_hdr{$tablename}", $_REQUEST['ftstock_in_no']);
                        break;
                    case "revisi":
                        CancelFIFO($_SESSION['strbranch'],"txinnitial_stock_hdr{$tablename}", $_REQUEST['ftstock_in_no']);

                        $ssql="SELECT IFNULL(MAX(ftstock_in_no),'')  AS M
                                FROM txinnitial_stock_hdr{$tablename}
                                WHERE ftstock_in_no LIKE '{$_REQUEST['ftstock_in_no']}-%'";
                        $ftrevisi_no=RetRstField($ssql);

                        $ssql="UPDATE TXStockFIFO
                                SET ftDocument_No='{$ftrevisi_no}'
                                WHERE ftTableRef='txinnitial_stock_hdr{$tablename}' AND
                                        ftDocument_No='{$_REQUEST['ftstock_in_no']}'";
                        mysql_query($ssql) or die($ssql);
                        break;
                }
                break;

            // module pembelian
            case "pembelian_stock_masuk":
                switch($_REQUEST['mode']) {
                    case "simpan_revisi":
                        CancelFIFO($_SESSION['strbranch'],"txstock_in_det", $_REQUEST['ftstock_in_no']);
                        
                        $ssql="SELECT IFNULL(MAX(ftstock_in_no),'')  AS M
                                FROM txstock_in_hdr
                                WHERE ftstock_in_no LIKE '{$_REQUEST['ftstock_in_no']}-%'";
                        $ftstock_in_no_revisi=RetRstField($ssql);

                        $ssql="UPDATE TXStockFIFO
                                SET ftDocument_No='{$ftstock_in_no_revisi}'
                                WHERE ftTableRef='txstock_in_det' AND
                                        ftDocument_No='{$_REQUEST['ftstock_in_no']}'";
                        mysql_query($ssql) or die($ssql);
                        // coding continue with case simpan
                    case "simpan":
                        $ssql="SELECT ftstock_type_code FROM txstock_in_hdr a INNER JOIN txstock_in_det b 
                                    ON a.ftstock_in_no = b.ftstock_in_no WHERE b.ftstock_in_no = '{$_REQUEST['ftstock_in_no']}'";
                        $ftstock_type_code=RetRstField($ssql);

                        if($ftstock_type_code=='inv') {
                            $ssql="select a.ftstock_in_no AS fttrans_no,a.fdtrans_date, b.fnindex,b.ftitem_code,ROUND(ifnull(b.fnqty_base,0),2) as fnqty_base, b.ftlocation_code,
                                        IFNULL(b.fcprice,0) AS fcprice_cogs, 'txstock_in_det' AS table_name
                                    FROM txstock_in_hdr a inner join txstock_in_det b ON a.ftstock_in_no=b.ftstock_in_no
                                    WHERE a.ftstock_in_no = '{$_REQUEST['ftstock_in_no']}' ";
                                    
                            pr("sqlfifo: ".$ssql);
                            $rs=mysql_query($ssql) or die($ssql);
                            while($r=mysql_fetch_assoc($rs)) {
                                  SaveFIFO($_SESSION['strbranch'],$r['table_name'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'],  $r['fcprice_cogs'], $r['ftlocation_code'],"","","");
                            }
                        }
                        break;
                    case "batal":
                        CancelFIFO($_SESSION['strbranch'],"txstock_in_det", $_REQUEST['ftstock_in_no']);
                        break;
                    
                }
                break;
            
            
            case "pembelian_purchase_invoice":
            case "pembelian_purchase_invoice_local":
            case "pembelian_purchase_invoice_import":
                switch($_REQUEST['mode']) {
                    case "simpan_revisi":
                        CancelFIFO($_SESSION['strbranch'],"txpurchase_invoice_det", $_REQUEST['ftpurchase_invoice_no']);
                        
                        $ssql="SELECT IFNULL(MAX(ftpurchase_invoice_no),'')  AS M
                                FROM txpurchase_invoice_det
                                WHERE ftpurchase_invoice_no LIKE '{$_REQUEST['ftpurchase_invoice_no']}-%'";
                        $ftpurchase_invoice_no_revisi=RetRstField($ssql);

                        $ssql="UPDATE TXStockFIFO
                                SET ftDocument_No='{$ftpurchase_invoice_no_revisi}'
                                WHERE ftTableRef='txpurchase_invoice_det' AND
                                        ftDocument_No='{$_REQUEST['ftpurchase_invoice_no']}'";
                        mysql_query($ssql) or die($ssql);
                        // continue with case simpan
                    case "simpan":
                        $ssql="SELECT ftstock_type_code FROM txstock_in_hdr a INNER JOIN txstock_in_det b 
                                    ON a.ftstock_in_no = b.ftstock_in_no WHERE b.ftstock_in_no = '{$_REQUEST['ftstock_in_no']}'";
                        $ftstock_type_code=RetRstField($ssql);

                        if($ftstock_type_code=='inv') {
                            $ssql="select c.ftpurchase_invoice_no,a.fdtrans_date, b.fnindex,b.ftitem_code,ifnull(b.fnqty_base,0) as fnqty_base, b.ftlocation_code,
                                        d.fcprice_cogs*c.fcrate as fcprice_cogs
                                    FROM txstock_in_hdr a inner join txstock_in_det b ON a.ftstock_in_no=b.ftstock_in_no
                                    INNER JOIN txpurchase_invoice_hdr c ON a.ftstock_in_no=c.ftstock_in_no AND c.ftpurchase_invoice_no = '{$_REQUEST['ftpurchase_invoice_no']}'
                                    INNER JOIN txpurchase_invoice_det d ON d.ftpurchase_invoice_no=c.ftpurchase_invoice_no AND d.fnindex=b.fnindex  ";
                            pr("sqlfifo: ".$ssql);
                            $rs=mysql_query($ssql) or die($ssql);
                            while($r=mysql_fetch_assoc($rs)) {
                                  SaveFIFO($_SESSION['strbranch'],"txpurchase_invoice_det",$r['ftpurchase_invoice_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'],  $r['fcprice_cogs'], $r['ftlocation_code'],"","","");
                            }
                        }
                        break;
                    case "batal":
                        CancelFIFO($_SESSION['strbranch'],"txpurchase_invoice_det", $_REQUEST['ftpurchase_invoice_no']);
                        break;
                    
                }
                break;

            case "return_pembelian":
            case "return_pembelian_local":
            case "return_pembelian_import":
                switch($_REQUEST['mode']) {
                    case "simpan":
                        $ssql="select a.ftpurchase_return_no,a.fdtrans_date, b.fnindex,b.ftitem_code,ifnull(b.fnqty_base,0) as fnqty_base, b.ftlocation_code,
                                   0 AS fcprice_cogs, a.ftpurchase_invoice_no
                                FROM  txpurchase_return_hdr a
                                INNER JOIN txpurchase_return_det b ON a.ftpurchase_return_no=b.ftpurchase_return_no AND a.ftpurchase_return_no = '{$_REQUEST['ftpurchase_return_no']}' ";

                        $rs=mysql_query($ssql) or die($ssql);
                        while($r=mysql_fetch_assoc($rs)) {
                              ReturPembelianFIFO($_SESSION['strbranch'], "txpurchase_return_det", $r['ftpurchase_return_no'], $r['ftpurchase_invoice_no'] ,$r['fdtrans_date'], $r['fnindex'], $r['ftitem_code'], -1*$r['fnqty_base'], $r['ftlocation_code'],"");
                        }
                    
                        break;
                    case "batal":
                        CancelFIFO($_SESSION['strbranch'],"txpurchase_return_det", $_REQUEST['ftpurchase_return_no']);
                        break;
                    case "revisi":
                        CancelFIFO($_SESSION['strbranch'],"txpurchase_return_det", $_REQUEST['ftpurchase_return_no']);

                        $ssql="SELECT IFNULL(MAX(ftpurchase_return_no),'')  AS M
                                FROM txpurchase_return_det
                                WHERE ftpurchase_return_no LIKE '{$_REQUEST['ftpurchase_return_no']}-%'";
                        $ftpurchase_return_no_revisi=RetRstField($ssql);

                        $ssql="UPDATE TXStockFIFO
                                SET ftDocument_No='{$ftpurchase_return_no_revisi}'
                                WHERE ftTableRef='txpurchase_return_det' AND
                                        ftDocument_No='{$_REQUEST['ftpurchase_return_no']}'";
                        mysql_query($ssql) or die($ssql);
                        break;
                }
                break;

            // module inventory    
            case "inventorybb_stock_masuk":
            case "inventorysj_barang_masuk":
            case "inventorysp_barang_masuk":
            case "barang_masuk":
                switch($_REQUEST['mode']) {
                    case "simpan_revisi":
                        CancelFIFO($_SESSION['strbranch'],"txstock_in_det", $_REQUEST['ftstock_in_no']);

                        $ssql="SELECT IFNULL(MAX(ftstock_in_no),'')  AS M
                                FROM txstock_in_hdr
                                WHERE ftstock_in_no LIKE '{$_REQUEST['ftstock_in_no']}-%'";
                        $ftrevisi_no=RetRstField($ssql);

                        $ssql="UPDATE TXStockFIFO
                                SET ftDocument_No='{$ftrevisi_no}'
                                WHERE ftTableRef='txstock_in_det' AND
                                        ftDocument_No='{$_REQUEST['ftstock_in_no']}'";
                        mysql_query($ssql) or die($ssql);
                        // continue to case simpan
                    case "simpan":
                        $ssql="select a.ftstock_in_no,a.fdtrans_date, b.fnindex,b.ftitem_code,ifnull(b.fnqty_base,0) as fnqty_base, b.ftlocation_code,
                                    b.fcprice as fcprice_cogs, a.ftcategory_code, a.ftdocument_no
                                FROM txstock_in_hdr a inner join txstock_in_det b ON a.ftstock_in_no=b.ftstock_in_no
                                WHERE a.ftstock_in_no = '{$_REQUEST['ftstock_in_no']}' ";

                        $rs=mysql_query($ssql) or die($ssql);
                        while($r=mysql_fetch_assoc($rs)) {
                            if ($r['ftcategory_code']==8) {
                                $fccogs = GetFIFOPriceValue($_SESSION['strbranch'], $r['ftdocument_no'], $r['ftitem_code'],"");
                            } else {
                                $fccogs = $r['fcprice_cogs'];
                            }

                            SaveFIFO($_SESSION['strbranch'],"txstock_in_det",$r['ftstock_in_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'], $fccogs, $r['ftlocation_code'],"","","");
                        }

                        break;
                    case "batal":
                        CancelFIFO($_SESSION['strbranch'],"txstock_in_det", $_REQUEST['ftstock_in_no']);
                        break;
                    
                }
                break; 
            
            case "inventorybb_stock_keluar":
            case "inventorysj_barang_keluar":
            case "inventorysp_barang_keluar":
            case "barang_keluar":
                // initialize variable for multiple tables
                $tablename = "";
                if($_REQUEST['form']=='inventorybb_stock_keluar') {
                    $tablename = "_bahan_baku";
                } elseif ($_REQUEST['form']=='inventorysj_barang_keluar') {
                    $tablename = "_setengah_jadi";
                } elseif ($_REQUEST['form']=='inventorysp_barang_keluar') {
                    $tablename = "_sparepart";
                } elseif ($_REQUEST['form']=='barang_keluar') {
                    $tablename = "";
                }

                switch($_REQUEST['mode']) {
                    case "simpan_revisi":
                        CancelFIFO($_SESSION['strbranch'],"txstock_out_det{$tablename}", $_REQUEST['ftstock_out_no']);

                        $ssql="SELECT IFNULL(MAX(ftstock_out_no),'')  AS M
                                FROM txstock_out_hdr{$tablename}
                                WHERE ftstock_out_no LIKE '{$_REQUEST['ftstock_out_no']}-%'";
                        $ftrevisi_no=RetRstField($ssql);

                        $ssql="UPDATE TXStockFIFO
                                SET ftDocument_No='{$ftrevisi_no}'
                                WHERE ftTableRef='txstock_out_det{$tablename}' AND
                                        ftDocument_No='{$_REQUEST['ftstock_out_no']}'";
                        mysql_query($ssql) or die($ssql);
                        // continue to case simpan
                    case "simpan":
                        $ssql="select 'txstock_out_det{$tablename}' as fttable, a.ftstock_out_no,a.fdtrans_date, b.fnindex,b.ftitem_code,
                                    ifnull(b.fnqty_base,0) as fnqty_base, b.ftlocation_code, 0 as fcprice_cogs
                                FROM txstock_out_hdr{$tablename} a inner join txstock_out_det{$tablename} b ON a.ftstock_out_no=b.ftstock_out_no
                                WHERE a.ftstock_out_no = '{$_REQUEST['ftstock_out_no']}' ";

                        $rs=mysql_query($ssql) or die($ssql);
                        pr("savefifo:".$ssql);
                        while($r=mysql_fetch_assoc($rs)) {
                              SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['ftstock_out_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], -1*$r['fnqty_base'],  $r['fcprice_cogs'], $r['ftlocation_code'],"","","");
                        }

                        break;
                    case "batal":
                        CancelFIFO($_SESSION['strbranch'],"txstock_out_det{$tablename}", $_REQUEST['ftstock_out_no']);
                        break;
                    
                }
                break;

            case "inventorybb_stock_mutasi":
            case "inventorybb_stock_mutasi_barang_jadi":
            case "inventorysj_mutasi_barang":
            case "inventorysp_mutasi_barang":
            case "mutasi_barang":
                // initialize variable for multiple tables
                $tablename = "";
                if($_REQUEST['form']=='inventorybb_stock_mutasi') {
                    $tablename = "_bahan_baku";
                } elseif ($_REQUEST['form']=='inventorybb_stock_mutasi_barang_jadi') {
                    $tablename = "_bbtobj";
                } elseif ($_REQUEST['form']=='inventorysj_mutasi_barang') {
                    $tablename = "_setengah_jadi";
                } elseif ($_REQUEST['form']=='inventorysp_mutasi_barang') {
                    $tablename = "_sparepart";
                } elseif ($_REQUEST['form']=='mutasi_barang') {
                    $tablename = "";
                }

                switch($_REQUEST['mode']) {
                    case "simpan_revisi":
                        CancelFIFO($_SESSION['strbranch'],"txstock_mutation_det{$tablename}", $_REQUEST['ftstock_mutation_no']);

                        $ssql="SELECT IFNULL(MAX(ftstock_mutation_no),'')  AS M
                                FROM txstock_mutation_hdr{$tablename}
                                WHERE ftstock_mutation_no LIKE '{$_REQUEST['ftstock_mutation_no']}-%'";
                        $ftrevisi_no=RetRstField($ssql);

                        $ssql="UPDATE TXStockFIFO
                                SET ftDocument_No='{$ftrevisi_no}'
                                WHERE ftTableRef='txstock_mutation_det{$tablename}' AND
                                        ftDocument_No='{$_REQUEST['ftstock_mutation_no']}'";
                        mysql_query($ssql) or die($ssql);
                        // continue to case simpan
                    case "simpan":
                        $ssql="select 'txstock_mutation_det{$tablename}' as fttable , a.ftstock_mutation_no as fttrans_no,a.fdtrans_date,
                                b.fnindex, b.ftitem_code,b.ftwarehouse_from as ftwarehouse_code_from,b.ftlocation_from as ftlocation_code_from,
                                b.ftwarehouse_to as ftwarehouse_code_to,b.ftlocation_to as ftlocation_code_to,
                                0 as fnmasuk,(b.fnqty_base) as fnkeluar,0 as fcprice
                                from txstock_mutation_hdr{$tablename} a,txstock_mutation_det{$tablename} b
                                where a.ftstock_mutation_no=b.ftstock_mutation_no AND
                                        a.ftstock_mutation_no='{$_REQUEST['ftstock_mutation_no']}'";

                        $rs=mysql_query($ssql) or die($ssql);
                        while($r=mysql_fetch_assoc($rs)) {
                            if ($r['ftlocation_code_from'] != $r['ftlocation_code_to']) {
                                SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], -1*$r['fnkeluar'],  0, $r['ftlocation_code_from'], $r['ftlocation_code_to'],"","");
                            }
                        }
                        break;
                    case "batal":
                         CancelFIFO($_SESSION['strbranch'],"txstock_mutation_det{$tablename}", $_REQUEST['ftstock_mutation_no']);
                        break; 
                } 
                break;
            
            case "inventorybb_stock_penyesuaian":   
            case "inventorysj_stock_penyesuaian":   
            case "inventorysp_stock_penyesuaian":   
            case "penyesuaian_barang":
                // initialize variable for multiple tables
                $tablename = "";
                if($_REQUEST['form']=='inventorybb_stock_penyesuaian') {
                    $tablename = "_bahan_baku";
                } elseif ($_REQUEST['form']=='inventorysj_stock_penyesuaian') {
                    $tablename = "_setengah_jadi";
                } elseif ($_REQUEST['form']=='inventorysp_stock_penyesuaian') {
                    $tablename = "_sparepart";
                } elseif ($_REQUEST['form']=='penyesuaian_barang') {
                    $tablename = "";
                }

                 switch($_REQUEST['mode']) {
                    case "final":
                        $ssql="select 'txstock_adjustment_det{$tablename}' as fttable ,a.ftstock_adjustment_no as fttrans_no,a.fdtrans_date, b.fnindex, b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code,
                                        case when (fnqty_base > 0) then fnqty_base else 0 end as fnmasuk,
                                        case when (fnqty_base < 0) then fnqty_base else 0 end as fnkeluar,b.fcprice as fcprice
                                from txstock_adjustment_hdr{$tablename} a,txstock_adjustment_det{$tablename} b
                                where a.ftstock_adjustment_no=b.ftstock_adjustment_no AND a.fnstatus=2
                                        AND a.ftstock_adjustment_no='{$_REQUEST['ftstock_adjustment_no']}'";
        
                            $rs=mysql_query($ssql) or die($ssql);
                            while($r=mysql_fetch_assoc($rs)) {
                                  if($r['fnmasuk']>0) {
                                       SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnmasuk'],  $r['fcprice'], $r['ftlocation_code'],"","","");
                                  }
                                  else {
                                       SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnkeluar'],  $r['fcprice'], $r['ftlocation_code'],"","","");
                                  }
                             }
                        break;
                    case "batal":
                        CancelFIFO($_SESSION['strbranch'],"txstock_adjustment_det{$tablename}", $_REQUEST['ftstock_adjustment_no']);
                        break;
                }
                break;

            case "assembly":
                switch($_REQUEST['mode']) {
                    case "simpan_revisi":
                        CancelFIFO($_SESSION['strbranch'],"txassembly_hdr", $_REQUEST['ftassembly_no']);

                        $ssql="SELECT IFNULL(MAX(ftassembly_no),'')  AS M
                                FROM txassembly_hdr
                                WHERE ftassembly_no LIKE '{$_REQUEST['ftassembly_no']}-%'";
                        $ftrevisi_no=RetRstField($ssql);

                        $ssql="UPDATE TXStockFIFO
                                SET ftDocument_No='{$ftrevisi_no}'
                                WHERE ftTableRef='txassembly_hdr' AND
                                        ftDocument_No='{$_REQUEST['ftassembly_no']}'";
                        mysql_query($ssql) or die($ssql);
                        // continue to case simpan
                    case "simpan":
                        // fifo in
                        $ssql=" select 'txassembly_hdr' as fttable ,a.ftassembly_no as fttrans_no,a.fdtrans_date, '1' AS fnindex, a.ftitem_code,a.ftlocation_code,
                                        a.fnqty AS fnqty_base, a.fcnet_price as fcprice
                                from txassembly_hdr a
                                where ftassembly_no='{$_REQUEST['ftassembly_no']}'";
            
                        $rs=mysql_query($ssql) or die($ssql);
                        while($r=mysql_fetch_assoc($rs)) {
                            SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'],  $r['fcprice'], $r['ftlocation_code'],"","","");
                        }
                        
                        // fifo out
                        $ssql=" select 'txassembly_hdr' as fttable ,a.ftassembly_no as fttrans_no,a.fdtrans_date, b.fnindex, b.ftitem_code,b.ftlocation_code,
                                        (b.fnqty * -1) AS fnqty_base, b.fcnet_price as fcprice
                                from txassembly_hdr a INNER JOIN txassembly_det b on a.ftassembly_no = b.ftassembly_no
                                where a.ftassembly_no='{$_REQUEST['ftassembly_no']}'";
            
                        $rs=mysql_query($ssql) or die($ssql);
                        while($r=mysql_fetch_assoc($rs)) {
                            SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'],  $r['fcprice'], $r['ftlocation_code'],"","","");
                        }
                        break;
                    case "batal":
                        CancelFIFO($_SESSION['strbranch'],"txassembly_hdr", $_REQUEST['ftassembly_no']);
                        break;
                }
                break;
            
            case "disassembly":
                switch($_REQUEST['mode']) {
                    case "simpan_revisi":
                        CancelFIFO($_SESSION['strbranch'],"txdisassembly_hdr", $_REQUEST['ftdisassembly_no']);

                        $ssql="SELECT IFNULL(MAX(ftdisassembly_no),'')  AS M
                                FROM txdisassembly_hdr
                                WHERE ftdisassembly_no LIKE '{$_REQUEST['ftdisassembly_no']}-%'";
                        $ftrevisi_no=RetRstField($ssql);

                        $ssql="UPDATE TXStockFIFO
                                SET ftDocument_No='{$ftrevisi_no}'
                                WHERE ftTableRef='txdisassembly_hdr' AND
                                        ftDocument_No='{$_REQUEST['ftdisassembly_no']}'";
                        mysql_query($ssql) or die($ssql);
                        // continue to case simpan
                    case "simpan":
                        // fifo out
                        $ssql=" select 'txdisassembly_hdr' as fttable ,a.ftdisassembly_no as fttrans_no,a.fdtrans_date, '1' AS fnindex, a.ftitem_code,a.ftlocation_code,
                                        (a.fnqty * -1) AS fnqty_base, a.fcnet_price as fcprice
                                from txdisassembly_hdr a
                                where ftdisassembly_no='{$_REQUEST['ftdisassembly_no']}'";
            
                        $rs=mysql_query($ssql) or die($ssql);
                        while($r=mysql_fetch_assoc($rs)) {
                            SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'],  $r['fcprice'], $r['ftlocation_code'],"","","");
                        }
                        
                        // fifo in
                        $ssql=" select 'txdisassembly_hdr' as fttable ,a.ftdisassembly_no as fttrans_no,a.fdtrans_date, b.fnindex, b.ftitem_code,b.ftlocation_code,
                                        b.fnqty AS fnqty_base, b.fcnet_price as fcprice
                                from txdisassembly_hdr a INNER JOIN txdisassembly_det b on a.ftdisassembly_no = b.ftdisassembly_no
                                where a.ftdisassembly_no='{$_REQUEST['ftdisassembly_no']}'";
            
                        $rs=mysql_query($ssql) or die($ssql);
                        while($r=mysql_fetch_assoc($rs)) {
                            SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'],  $r['fcprice'], $r['ftlocation_code'],"","","");
                        }
                        break;
                    case "batal":
                        CancelFIFO($_SESSION['strbranch'],"txassembly_hdr", $_REQUEST['ftassembly_no']);
                        break;
                }
                break;
            
            // module penjualan
            case "sales_order":
                switch($_REQUEST['mode']) {
                    case "simpan_revisi":
                        $ssql="select a.ftstock_out_no
                                FROM txstock_out_hdr a INNER JOIN txstock_out_det b ON a.ftstock_out_no=b.ftstock_out_no
                                LEFT JOIN txdelivery_order_hdr c ON c.ftdo_no = a.ftdocument_no
                                LEFT JOIN txsales_order_hdr d ON d.ftso_no = c.ftso_no
                                WHERE d.ftso_no = '{$_REQUEST['ftso_no']}'";
                        $ftstock_out_no=RetRstField($ssql);
                        CancelFIFO($_SESSION['strbranch'],"txstock_out_det", $ftstock_out_no);

                        $ssql="SELECT IFNULL(MAX(ftstock_out_no),'')  AS M
                                FROM txstock_out_hdr
                                WHERE ftstock_out_no LIKE '{$ftstock_out_no}-%'";
                        $ftstock_out_no_revisi=RetRstField($ssql);

                        $ssql="UPDATE TXStockFIFO
                                SET ftDocument_No='{$ftstock_out_no_revisi}'
                                WHERE ftTableRef='txstock_out_det' AND
                                        ftDocument_No='{$ftstock_out_no}'";
                        mysql_query($ssql) or die($ssql);
                        // continue to case simpan
                    case "final":
                        $ssql="select a.ftstock_out_no as fttrans_no,a.fdtrans_date, b.fnindex,b.ftitem_code,ifnull(b.fnqty_base,0) * -1 as fnqty_base, b.ftlocation_code,
                                    '0' as fcprice_cogs, 'txstock_out_det' AS table_name
                                FROM txstock_out_hdr a INNER JOIN txstock_out_det b ON a.ftstock_out_no=b.ftstock_out_no
                                LEFT JOIN txdelivery_order_hdr c ON c.ftdo_no = a.ftdocument_no
                                LEFT JOIN txsales_order_hdr d ON d.ftso_no = c.ftso_no
                                WHERE d.ftso_no = '{$_REQUEST['ftso_no']}' ";
                        
                        pr($ssql);
                        $rs=mysql_query($ssql) or die("Save Fifo:".$ssql);
                        while($r=mysql_fetch_assoc($rs)) {
                              SaveFIFO($_SESSION['strbranch'],$r['table_name'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'],  $r['fcprice_cogs'], $r['ftlocation_code'],"","","");
                        }
                        break;
                    case "batal":
                        $ssql="select a.ftstock_out_no
                                FROM txstock_out_hdr a INNER JOIN txstock_out_det b ON a.ftstock_out_no=b.ftstock_out_no
                                LEFT JOIN txdelivery_order_hdr c ON c.ftdo_no = a.ftdocument_no
                                LEFT JOIN txsales_order_hdr d ON d.ftso_no = c.ftso_no
                                WHERE d.ftso_no = '{$_REQUEST['ftso_no']}'";
                        $ftstock_out_no=RetRstField($ssql);
                        
                        CancelFIFO($_SESSION['strbranch'],"txstock_out_det", $ftstock_out_no);
                        break;
                    
                }
                break;

            case "sales_invoice":
                switch($_REQUEST['mode']) {
                    case "update": 
                        if (!$_SESSION["SIAutoDO"]) { break;} // Auto create DO : jalankan create fifo. 
                    case "simpan_revisi":
                        $ssql="select a.ftstock_out_no
                                FROM txstock_out_hdr a INNER JOIN txstock_out_det b ON a.ftstock_out_no=b.ftstock_out_no
                                LEFT JOIN txdelivery_order_hdr c ON c.ftdo_no = a.ftdocument_no
                                LEFT JOIN txsales_invoice_hdr d ON d.ftdo_no = c.ftdo_no
                                WHERE d.ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_no']}'";
                        $ftstock_out_no=RetRstField($ssql);
                        CancelFIFO($_SESSION['strbranch'],"txstock_out_det", $ftstock_out_no);

                        $ssql="SELECT IFNULL(MAX(ftstock_out_no),'')  AS M
                                FROM txstock_out_hdr
                                WHERE ftstock_out_no LIKE '{$ftstock_out_no}-%'";
                        $ftstock_out_no_revisi=RetRstField($ssql);

                        $ssql="UPDATE TXStockFIFO
                                SET ftDocument_No='{$ftstock_out_no_revisi}'
                                WHERE ftTableRef='txstock_out_det' AND
                                        ftDocument_No='{$ftstock_out_no}'";
                        mysql_query($ssql) or die($ssql);
                        // continue to case simpan
                    case "simpan":
                        $ssql="select a.ftstock_out_no as fttrans_no,a.fdtrans_date, b.fnindex,b.ftitem_code,ifnull(b.fnqty_base,0) * -1 as fnqty_base, b.ftlocation_code,
                                    '0' as fcprice_cogs, 'txstock_out_det' AS table_name
                                FROM txstock_out_hdr a INNER JOIN txstock_out_det b ON a.ftstock_out_no=b.ftstock_out_no
                                LEFT JOIN txdelivery_order_hdr c ON c.ftdo_no = a.ftdocument_no
                                LEFT JOIN txsales_invoice_hdr d ON d.ftdo_no = c.ftdo_no
                                WHERE d.ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_no']}' ";
                        
                        pr($ssql);
                        $rs=mysql_query($ssql) or die("Save Fifo:".$ssql);
                        while($r=mysql_fetch_assoc($rs)) {
                              SaveFIFO($_SESSION['strbranch'],$r['table_name'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'],  $r['fcprice_cogs'], $r['ftlocation_code'],"","","");
                        }
                        break;
                    case "batal":
                        $ssql="select a.ftstock_out_no
                                FROM txstock_out_hdr a INNER JOIN txstock_out_det b ON a.ftstock_out_no=b.ftstock_out_no
                                LEFT JOIN txdelivery_order_hdr c ON c.ftdo_no = a.ftdocument_no
                                LEFT JOIN txsales_invoice_hdr d ON d.ftdo_no = c.ftdo_no
                                WHERE d.ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_no']}'";
                        $ftstock_out_no=RetRstField($ssql);
                        
                        CancelFIFO($_SESSION['strbranch'],"txstock_out_det", $ftstock_out_no);
                        break;
                    
                }
                break;
            
            case "delivery_order":
                switch($_REQUEST['mode']) {
                    case "final":
                        $ssql="select a.ftdo_no,a.fdtrans_date, b.fnindex,b.ftitem_code,ifnull(b.fnqty_base,0) as fnqty_base, b.ftlocation_code,
                                    '0' as fcprice_cogs
                                FROM txdelivery_order_hdr a 
                                INNER JOIN txdelivery_order_det b ON a.ftdo_no=b.ftdo_no AND a.ftdo_no = '{$_REQUEST['ftdo_no']}' ";
                        pr($ssql);
                        $rs=mysql_query($ssql) or die("Save Fifo:".$ssql);
                        while($r=mysql_fetch_assoc($rs)) {
                              SaveFIFO($_SESSION['strbranch'],"txdelivery_order_det",$r['ftdo_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], -1*$r['fnqty_base'],  $r['fcprice_cogs'], $r['ftlocation_code'],"","","");
                        }
                        break;
                    case "batal":
                        CancelFIFO($_SESSION['strbranch'],"txdelivery_order_det", $_REQUEST['ftdo_no']);
                        break;
                    case "revisi":
                        CancelFIFO($_SESSION['strbranch'],"txdelivery_order_det", $_REQUEST['ftdo_no']);

                        $ssql="SELECT IFNULL(MAX(ftdo_no),'')  AS M
                                FROM txdelivery_order_det
                                WHERE ftdo_no LIKE '{$_REQUEST['ftdo_no']}-%'";
                        $ftdo_no_revisi=RetRstField($ssql);

                        $ssql="UPDATE TXStockFIFO
                                SET ftDocument_No='{$ftdo_no_revisi}'
                                WHERE ftTableRef='txdelivery_order_det' AND
                                        ftDocument_No='{$_REQUEST['ftdo_no']}'";
                        mysql_query($ssql) or die($ssql);
                        break;
                }
                break;

            case "sales_return":
                switch($_REQUEST['mode']) {
                    case "simpan_revisi":
                        CancelFIFO($_SESSION['strbranch'],"txsales_return_det", $_REQUEST['ftsales_return_no']);

                        $ssql="SELECT IFNULL(MAX(ftsales_return_no),'')  AS M
                                FROM txsales_return_det
                                WHERE ftsales_return_no LIKE '{$_REQUEST['ftsales_return_no']}-%'";
                        $ftsales_return_no_revisi=RetRstField($ssql);

                        $ssql="UPDATE TXStockFIFO
                                SET ftDocument_No='{$ftsales_return_no_revisi}'
                                WHERE ftTableRef='txsales_return_det' AND
                                        ftDocument_No='{$_REQUEST['ftsales_return_no']}'";
                        mysql_query($ssql) or die($ssql);
                        // continue to case simpan
                    case "simpan":
                        $ssql="select a.ftsales_return_no as fttrans_no,a.fdtrans_date, b.fnindex,b.ftitem_code,ifnull(b.fnqty_base,0) as fnqty_base, b.ftlocation_code,
                                   0 AS fcprice_cogs, d.ftstock_out_no, 'txsales_return_det' AS table_name, a.ftso_no
                                FROM  txsales_return_hdr a
                                INNER JOIN txsales_return_det b ON a.ftsales_return_no=b.ftsales_return_no 
                                LEFT JOIN txdelivery_order_hdr c ON c.ftso_no = a.ftso_no
                                LEFT JOIN txstock_out_hdr d ON d.ftdocument_no = c.ftdo_no
                                WHERE a.ftsales_return_no = '{$_REQUEST['ftsales_return_no']}' ";

                        $rs=mysql_query($ssql) or die($ssql);
                        while($r=mysql_fetch_assoc($rs)) {
                            if ($r['ftso_no'] != "") { 
                                ReturFIFO($_SESSION['strbranch'], $r['table_name'], $r['fttrans_no'], $r['ftstock_out_no'] ,$r['fdtrans_date'], $r['fnindex'], $r['ftitem_code'], $r['fnqty_base'], $r['ftlocation_code'],"");
                            } else {
                                $fcprice_cogs = GetLastPriceValue($_SESSION['strbranch'], $r['ftitem_code']);
                                if ($fcprice_cogs == 0) {
                                   $fcprice_cogs = RetRstField("SELECT fcbuying_price FROM tlbuying_price WHERE ftcurrency_code = 'IDR' AND ftitem_code = '{$r['ftitem_code']}' ");
                                }
                                if ($fcprice_cogs == '') $fcprice_cogs = 0; 
                                SaveFIFO($_SESSION['strbranch'],$r['table_name'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'], $fcprice_cogs, $r['ftlocation_code'],"","","");
                            }    
                        }
    
                        break;
                    case "batal":
                        CancelFIFO($_SESSION['strbranch'],"txsales_return_det", $_REQUEST['ftsales_return_no']);
                        break;
                    
                }
                break;
        }
    }
?>
