<?php

session_cache_expire(1440);
session_start();
session_name("insys");
include "include/config.php";
include "include/functions.php";
include "include/fifo.php";
include "class/UctVw.php";


pr("*** INSERTING STOCK MASUK (BAHAN BAKU) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'STOCK MASUK' as fttype, 'txstock_in_hdr' as fttable , a.ftstock_in_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, ifnull(b.fnqty_base,0) as fnmasuk, 0 as fnkeluar,
            ifnull(b.fcprice,0) as fcprice
            FROM txstock_in_hdr a inner join txstock_in_det b ON a.ftstock_in_no=b.ftstock_in_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2) and a.ftstock_type_code = 'invbb' and a.ftcategory_code <> 0  and a.ftcategory_code <> 4
        ) x
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

mysql_query("BEGIN");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    #echo $i."<br>";
    if($r['fnmasuk']>0) {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'],$r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnmasuk'],  $r['fcprice'], $r['ftwarehouse_code'],"","","");
    }
    else {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'],$r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'],-1*$r['fnkeluar'],  0, $r['ftwarehouse_code'],"","","");
    }

    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;

}

mysql_query("COMMIT");


pr("*** INSERTING STOCK MASUK (SETENGAH JADI) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'STOCK MASUK' as fttype, 'txstock_in_hdr' as fttable , a.ftstock_in_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, ifnull(b.fnqty_base,0) as fnmasuk, 0 as fnkeluar,
            ifnull(b.fcprice,0) as fcprice
            FROM txstock_in_hdr a inner join txstock_in_det b ON a.ftstock_in_no=b.ftstock_in_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2) and a.ftstock_type_code = 'invsj' and a.ftcategory_code <> 0  and a.ftcategory_code <> 4
        ) x
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

mysql_query("BEGIN");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    #echo $i."<br>";
    if($r['fnmasuk']>0) {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'],$r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnmasuk'],  $r['fcprice'], $r['ftwarehouse_code'],"","","");
    }
    else {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'],$r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'],-1*$r['fnkeluar'],  0, $r['ftwarehouse_code'],"","","");
    }

    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;

}

mysql_query("COMMIT");


pr("*** INSERTING STOCK MASUK (SPAREPART) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'STOCK MASUK' as fttype, 'txstock_in_hdr' as fttable , a.ftstock_in_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, ifnull(b.fnqty_base,0) as fnmasuk, 0 as fnkeluar,
            ifnull(b.fcprice,0) as fcprice
            FROM txstock_in_hdr a inner join txstock_in_det b ON a.ftstock_in_no=b.ftstock_in_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2) and a.ftstock_type_code = 'invsp' and a.ftcategory_code <> 0  and a.ftcategory_code <> 4
        ) x
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

mysql_query("BEGIN");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    #echo $i."<br>";
    if($r['fnmasuk']>0) {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'],$r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnmasuk'],  $r['fcprice'], $r['ftwarehouse_code'],"","","");
    }
    else {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'],$r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'],-1*$r['fnkeluar'],  0, $r['ftwarehouse_code'],"","","");
    }

    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;

}

mysql_query("COMMIT");


pr("*** INSERTING STOCK MASUK (BARANG JADI) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'STOCK MASUK' as fttype, 'txstock_in_hdr' as fttable , a.ftstock_in_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, ifnull(b.fnqty_base,0) as fnmasuk, 0 as fnkeluar,
            ifnull(b.fcprice,0) as fcprice
            FROM txstock_in_hdr a inner join txstock_in_det b ON a.ftstock_in_no=b.ftstock_in_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2) and a.ftstock_type_code = 'inv' and a.ftcategory_code <> 0  and a.ftcategory_code <> 4
        ) x
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

mysql_query("BEGIN");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    #echo $i."<br>";
    if($r['fnmasuk']>0) {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'],$r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnmasuk'],  $r['fcprice'], $r['ftwarehouse_code'],"","","");
    }
    else {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'],$r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'],-1*$r['fnkeluar'],  0, $r['ftwarehouse_code'],"","","");
    }

    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;

}

mysql_query("COMMIT");


pr("*** INSERTING STOCK MUTASI (BAHAN BAKU) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'STOCK MUTASI' as fttype, 'txstock_mutation_det_bahan_baku' 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.ftwarehouse_to as ftwarehouse_code_to,
            0 as fnmasuk, ifnull(b.fnqty_base,0) as fnkeluar, 0 as fcprice
            FROM txstock_mutation_hdr_bahan_baku a inner join txstock_mutation_det_bahan_baku b ON a.ftstock_mutation_no=b.ftstock_mutation_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2) AND (b.ftwarehouse_from <> b.ftwarehouse_to) and ifnull(b.fnqty_base,0) <> 0
        ) x
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], -1*$r['fnkeluar'],  0, $r['ftwarehouse_code_from'], $r['ftwarehouse_code_to'],"","");
    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;

}


pr("*** INSERTING STOCK MUTASI (BBTOBJ) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'STOCK MUTASI' as fttype, 'txstock_mutation_det_bbtobj' 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.ftwarehouse_to as ftwarehouse_code_to,
            0 as fnmasuk, ifnull(b.fnqty_base,0) as fnkeluar, 0 as fcprice
            FROM txstock_mutation_hdr_bbtobj a inner join txstock_mutation_det_bbtobj b ON a.ftstock_mutation_no=b.ftstock_mutation_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2) AND (b.ftwarehouse_from <> b.ftwarehouse_to) and ifnull(b.fnqty_base,0) <> 0
        ) x
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], -1*$r['fnkeluar'],  0, $r['ftwarehouse_code_from'], $r['ftwarehouse_code_to'],"","");
    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;

}

pr("*** INSERTING STOCK MUTASI (SETENGAH JADI) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'STOCK MUTASI' as fttype, 'txstock_mutation_det_setengah_jadi' 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.ftwarehouse_to as ftwarehouse_code_to,
            0 as fnmasuk, ifnull(b.fnqty_base,0) as fnkeluar, 0 as fcprice
            FROM txstock_mutation_hdr_setengah_jadi a inner join txstock_mutation_det_setengah_jadi b ON a.ftstock_mutation_no=b.ftstock_mutation_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2) AND (b.ftwarehouse_from <> b.ftwarehouse_to) and ifnull(b.fnqty_base,0) <> 0
        ) x
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], -1*$r['fnkeluar'],  0, $r['ftwarehouse_code_from'], $r['ftwarehouse_code_to'],"","");
    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;
}


pr("*** INSERTING STOCK MUTASI (SPAREPART) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'STOCK MUTASI' as fttype, 'txstock_mutation_det_sparepart' 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.ftwarehouse_to as ftwarehouse_code_to,
            0 as fnmasuk, ifnull(b.fnqty_base,0) as fnkeluar, 0 as fcprice
            FROM txstock_mutation_hdr_sparepart a inner join txstock_mutation_det_sparepart b ON a.ftstock_mutation_no=b.ftstock_mutation_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2) AND (b.ftwarehouse_from <> b.ftwarehouse_to) and ifnull(b.fnqty_base,0) <> 0
        ) x
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], -1*$r['fnkeluar'],  0, $r['ftwarehouse_code_from'], $r['ftwarehouse_code_to'],"","");
    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;
}


pr("*** INSERTING STOCK MUTASI (BARANG JADI) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'STOCK MUTASI' as fttype, 'txstock_mutation_det' 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.ftwarehouse_to as ftwarehouse_code_to,
            0 as fnmasuk, ifnull(b.fnqty_base,0) as fnkeluar, 0 as fcprice
            FROM txstock_mutation_hdr a inner join txstock_mutation_det b ON a.ftstock_mutation_no=b.ftstock_mutation_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2) AND (b.ftwarehouse_from <> b.ftwarehouse_to) and ifnull(b.fnqty_base,0) <> 0
        ) x
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], -1*$r['fnkeluar'],  0, $r['ftwarehouse_code_from'], $r['ftwarehouse_code_to'],"","");
    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;
}


pr("*** INSERTING PURCHASE RETURN TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'PURCHASE RETURN' as fttype, 'txpurchase_return_det' as fttable , a.ftpurchase_return_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, ifnull(b.fnqty_base,0) as fnqty_base,
            0 as fcprice, a.ftpurchase_invoice_no
            FROM txpurchase_return_hdr a inner join txpurchase_return_det b ON a.ftpurchase_return_no=b.ftpurchase_return_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2)
        ) x
        WHERE fnqty_base <> 0
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

mysql_query("BEGIN");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    #echo $i."<br>";
    ReturPembelianFIFO($_SESSION['strbranch'], "txpurchase_return_det", $r['fttrans_no'], $r['ftpurchase_invoice_no'] ,$r['fdtrans_date'], $r['fnindex'], $r['ftitem_code'], -1*$r['fnqty_base'], $r['ftwarehouse_code'],"");

    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;
}

mysql_query("COMMIT");


pr("*** INSERTING STOCK KELUAR & PENYESUAIAN (BAHAN BAKU) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'STOCK KELUAR' as fttype, 'txstock_out_det_bahan_baku' as fttable , a.ftstock_out_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, -1*ifnull(b.fnqty_base,0) as fnqty_base,
            0 as fcprice
            FROM txstock_out_hdr_bahan_baku a inner join txstock_out_det_bahan_baku b ON a.ftstock_out_no=b.ftstock_out_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2) and a.ftcategory_code <> 0  and a.ftcategory_code <> 4
            UNION ALL
            select 'STOCK PENYESUAIAN' as fttype, 'txstock_adjustment_det_bahan_baku' as fttable , a.ftstock_adjustment_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, ifnull(b.fnqty_base,0) as fnqty_base,
            b.fcprice as fcprice
            FROM txstock_adjustment_hdr_bahan_baku a inner join txstock_adjustment_det_bahan_baku b ON a.ftstock_adjustment_no=b.ftstock_adjustment_no
            WHERE a.fnstatus = 2
        ) x
        WHERE fnqty_base <> 0
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

mysql_query("BEGIN");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    #echo $i."<br>";

    $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'],  $r['fcprice'], $r['ftwarehouse_code'],"","","");

    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;

}

mysql_query("COMMIT");

pr("*** INSERTING STOCK KELUAR & PENYESUAIAN (SETENGAH JADI) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'STOCK KELUAR' as fttype, 'txstock_out_det_setengah_jadi' as fttable , a.ftstock_out_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, -1*ifnull(b.fnqty_base,0) as fnqty_base,
            0 as fcprice
            FROM txstock_out_hdr_setengah_jadi a inner join txstock_out_det_setengah_jadi b ON a.ftstock_out_no=b.ftstock_out_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2) and a.ftcategory_code <> 0  and a.ftcategory_code <> 4
            UNION ALL
            select 'STOCK PENYESUAIAN' as fttype, 'txstock_adjustment_det_setengah_jadi' as fttable , a.ftstock_adjustment_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, ifnull(b.fnqty_base,0) as fnqty_base,
            b.fcprice as fcprice
            FROM txstock_adjustment_hdr_setengah_jadi a inner join txstock_adjustment_det_setengah_jadi b ON a.ftstock_adjustment_no=b.ftstock_adjustment_no
            WHERE a.fnstatus = 2
        ) x
        WHERE fnqty_base <> 0
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

mysql_query("BEGIN");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    #echo $i."<br>";

    $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'],  $r['fcprice'], $r['ftwarehouse_code'],"","","");

    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;

}

mysql_query("COMMIT");

pr("*** INSERTING STOCK KELUAR & PENYESUAIAN (SPAREPART) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'STOCK KELUAR' as fttype, 'txstock_out_det_sparepart' as fttable , a.ftstock_out_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, -1*ifnull(b.fnqty_base,0) as fnqty_base,
            0 as fcprice
            FROM txstock_out_hdr_sparepart a inner join txstock_out_det_sparepart b ON a.ftstock_out_no=b.ftstock_out_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2) and a.ftcategory_code <> 0  and a.ftcategory_code <> 4
            UNION ALL
            select 'STOCK PENYESUAIAN' as fttype, 'txstock_adjustment_det_sparepart' as fttable , a.ftstock_adjustment_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, ifnull(b.fnqty_base,0) as fnqty_base,
            b.fcprice as fcprice
            FROM txstock_adjustment_hdr_sparepart a inner join txstock_adjustment_det_sparepart b ON a.ftstock_adjustment_no=b.ftstock_adjustment_no
            WHERE a.fnstatus = 2
        ) x
        WHERE fnqty_base <> 0
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

mysql_query("BEGIN");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    #echo $i."<br>";

    $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'],  $r['fcprice'], $r['ftwarehouse_code'],"","","");

    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;

}

mysql_query("COMMIT");

pr("*** INSERTING STOCK KELUAR & PENYESUAIAN & DO & SALES RETUR (BARANG JADI) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'STOCK KELUAR' as fttype, 'txstock_out_det' as fttable , a.ftstock_out_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, -1*ifnull(b.fnqty_base,0) as fnqty_base,
            0 as fcprice, '' as reff_no
            FROM txstock_out_hdr a inner join txstock_out_det b ON a.ftstock_out_no=b.ftstock_out_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2)  and a.ftcategory_code <> 0  and a.ftcategory_code <> 4
            UNION ALL
            select 'STOCK PENYESUAIAN' as fttype, 'txstock_adjustment_det' as fttable , a.ftstock_adjustment_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, ifnull(b.fnqty_base,0) as fnqty_base,
            b.fcprice as fcprice, '' as reff_no
            FROM txstock_adjustment_hdr a inner join txstock_adjustment_det b ON a.ftstock_adjustment_no=b.ftstock_adjustment_no
            WHERE a.fnstatus = 2
            UNION ALL
            select 'DELIVERY ORDER' as fttype, 'txdelivery_order_det' as fttable , a.ftdo_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, -1*ifnull(b.fnqty_base,0) as fnqty_base,
            0 as fcprice, '' as reff_no
            FROM txdelivery_order_hdr a inner join txdelivery_order_det b ON a.ftdo_no=b.ftdo_no
            WHERE a.fnstatus = 2
            UNION ALL
            select 'SALES RETUR' as fttype, 'txsales_return_det' as fttable , a.ftsales_return_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, ifnull(b.fnqty_base,0) as fnqty_base,
            0 as fcprice, c.ftdo_no as reff_no
            FROM txsales_return_hdr a inner join txsales_return_det b ON a.ftsales_return_no=b.ftsales_return_no
            INNER JOIN txsales_invoice_hdr c ON a.ftsales_invoice_no=c.ftsales_invoice_no
            WHERE (a.fnstatus=1 OR a.fnstatus = 2)
        ) x
        WHERE fnqty_base <> 0
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

mysql_query("BEGIN");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    pr($r['fttrans_no']);
    if ($r['fttype'] == 'SALES RETUR') {
        $m=ReturFIFO($_SESSION['strbranch'],$r['fttable'], $r['fttrans_no'], $r['reff_no'] ,$r['fdtrans_date'], $r['fnindex'], $r['ftitem_code'], $r['fnqty_base'], $r['ftwarehouse_code'],"");
    } else {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'], $r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnqty_base'],  $r['fcprice'], $r['ftwarehouse_code'],"","","");
    }

    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;

}

mysql_query("COMMIT");

/*
pr("*** INSERTING INITIAL STOCK (NON BARANG JADI) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'INITIAL STOCK BAHAN BAKU' as fttype, 'txinnitial_stock_hdr_bahan_baku' as fttable , a.ftstock_in_no as fttrans_no,a.fdtrans_date, b.fnindex, b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code,b.fnqty_base as fnmasuk,0 as fnkeluar, b.fcprice
            from txinnitial_stock_hdr_bahan_baku a,txinnitial_stock_det_bahan_baku b
            where a.ftstock_in_no=b.ftstock_in_no and a.fnstatus=1
            union all
            select 'INITIAL STOCK SETENGAH JADI' as fttype, 'txinnitial_stock_hdr_setengah_jadi' as fttable , a.ftstock_in_no as fttrans_no,a.fdtrans_date, b.fnindex, b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code,b.fnqty_base as fnmasuk,0 as fnkeluar, b.fcprice
            from txinnitial_stock_hdr_setengah_jadi a,txinnitial_stock_det_setengah_jadi b
            where a.ftstock_in_no=b.ftstock_in_no and a.fnstatus=1
            union all
            select 'INITIAL STOCK SPAREPART' as fttype, 'txinnitial_stock_hdr_sparepart' as fttable , a.ftstock_in_no as fttrans_no,a.fdtrans_date, b.fnindex, b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code,b.fnqty_base as fnmasuk,0 as fnkeluar, b.fcprice
            from txinnitial_stock_hdr_sparepart a,txinnitial_stock_det_sparepart b
            where a.ftstock_in_no=b.ftstock_in_no and a.fnstatus=1
        ) x
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

mysql_query("BEGIN");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    #echo $i."<br>";
    if($r['fnmasuk']>0) {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'],$r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnmasuk'],  $r['fcprice'], $r['ftwarehouse_code'],"","","");
    }
    else {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'],$r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'],-1*$r['fnkeluar'],  0, $r['ftwarehouse_code'],"","","");
    }

    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;

}

mysql_query("COMMIT");


pr("*** INSERTING INITIAL STOCK (BARANG JADI) TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'INITIAL STOCK BARANG JADI' as fttype, 'txinnitial_stock_hdr' as fttable , a.ftstock_in_no as fttrans_no,a.fdtrans_date, b.fnindex, b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code,b.fnqty_base as fnmasuk,0 as fnkeluar, b.fcprice
            from txinnitial_stock_hdr a INNER JOIN txinnitial_stock_det b ON a.ftstock_in_no=b.ftstock_in_no
            where a.fnstatus=1
        ) x
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

mysql_query("BEGIN");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    #echo $i."<br>";
    if($r['fnmasuk']>0) {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'],$r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnmasuk'],  $r['fcprice'], $r['ftwarehouse_code'],"","","");
    }
    else {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'],$r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'],-1*$r['fnkeluar'],  0, $r['ftwarehouse_code'],"","","");
    }

    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;

}

mysql_query("COMMIT");

pr("*** INSERTING PURCHASE INVOICE TO FIFO ***");

$ssql=" SELECT x.*
        FROM
        (
            select 'PURCHASE INVOICE' as fttype, 'txpurchase_invoice_hdr' as fttable , c.ftpurchase_invoice_no as fttrans_no,
            a.fdtrans_date, b.fnindex,b.ftitem_code, b.ftwarehouse_code, ifnull(b.fnqty_base,0) as fnmasuk, 0 as fnkeluar,
            d.fcprice_cogs*c.fcrate as fcprice
            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 (a.fnstatus=1 or a.fnstatus = 2)
            INNER JOIN txpurchase_invoice_det d ON d.ftpurchase_invoice_no=c.ftpurchase_invoice_no AND d.fnindex=b.fnindex
        ) x
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        ";

pr("$ssql");

mysql_query("BEGIN");

$strSQL=array();

$rs=mysql_query($ssql);
while($r=mysql_fetch_assoc($rs)) {
    $i++;
    #echo $i."<br>";
    if($r['fnmasuk']>0) {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'],$r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'], $r['fnmasuk'],  $r['fcprice'], $r['ftwarehouse_code'],"","","");
    }
    else {
        $m=SaveFIFO($_SESSION['strbranch'],$r['fttable'],$r['fttrans_no'],$r['fdtrans_date'], $r['fnindex'],$r['ftitem_code'],-1*$r['fnkeluar'],  0, $r['ftwarehouse_code'],"","","");
    }

    if($m=="") continue;
    $strSQL["fifo {$i}"]=$m;

}

mysql_query("COMMIT");
*/


pr("*** SELESAI ***");

 // code sql lama
/*
$ssql="
        SELECT x.*
        FROM
        (
        select 'INITIAL STOCK' as fttype, 'txinnitial_stock_hdr_bahan_baku' as fttable , a.ftstock_in_no as fttrans_no,a.fdtrans_date, b.fnindex, b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code,(fnqty) as fnmasuk,0 as fnkeluar,0 as fcprice
        from txinnitial_stock_hdr_bahan_baku a,txinnitial_stock_det_bahan_baku b
        where a.ftstock_in_no=b.ftstock_in_no
                and a.fnstatus=1
        union all
        //stock masuk
        select
        CASE 	WHEN ftcategory_code='0' THEN 'STOCK MASUK PEMBELIAN'
                WHEN ftcategory_code='1' THEN 'STOCK MASUK SERVICE'
                WHEN ftcategory_code='2' THEN 'STOCK MASUK RETURN'
                WHEN ftcategory_code='3' THEN 'STOCK MASUK SURAT JALAN PINJAMAN KEMBALI'
                WHEN ftcategory_code='4' THEN 'STOCK MASUK SURAT JALAN SAMPLE'
                END AS  fttype,
        CASE 	WHEN ftcategory_code='0' THEN 'txstock_in_det_bahan_baku'
                WHEN ftcategory_code='1' THEN 'txstock_in_return_det_bahan_baku'
                WHEN ftcategory_code='2' THEN 'txstock_in_return_det_bahan_baku'
                WHEN ftcategory_code='3' THEN 'txdelivery_order_det_pinjaman'
                WHEN ftcategory_code='4' THEN 'txdelivery_order_det_sample'
                END AS  fttable,
        CASE WHEN ifnull(a.ftdocument_no,'')<>'' THEN ifnull(a.ftdocument_no,'') ELSE a.ftstock_in_no  END  as fttrans_no,
        a.fdtrans_date, b.fnindex, b.ftitem_code,
        b.ftwarehouse_code,b.ftlocation_code,(b.fnqty) as fnmasuk,0 as fnkeluar,e.fcprice*d.fcrate as fcprice
        from txstock_in_hdr_bahan_baku a
                inner join txstock_in_det_bahan_baku b ON a.ftstock_in_no=b.ftstock_in_no
                inner join tlstock_hdr_bahan_baku c ON b.ftitem_code=c.ftitem_code
                LEFT JOIN txpurchase_order_hdr d ON a.ftdocument_no=d.ftpo_no
                LEFT JOIN txpurchase_order_det e ON d.ftpo_no =e.ftpo_no AND e.fnindex=b.fnindex_po
        where
                 a.fnstatus=1  and
                (
                        (a.fnstatus<>9 and a.ftcategory_code<>0)
                        or
                        (a.fnstatus<>9 and a.fnpo_type3<>1 and a.ftcategory_code=0)
                )

        //stock keluar
        union all

        select

        CASE 	WHEN ftcategory_code='0' THEN 'SURAT JALAN RETURN'
                WHEN ftcategory_code='1' THEN 'SURAT JALAN SUBCON'
                WHEN ftcategory_code='2' THEN 'SURAT JALAN SERVICE'
                WHEN ftcategory_code='3' THEN 'SURAT JALAN FREE'
                WHEN ftcategory_code='4' THEN 'SURAT JALAN KOMPONEN PON POTONG'
                WHEN ftcategory_code='5' THEN ''
                WHEN ftcategory_code='6' THEN 'SURAT JALAN SAMPLE'
                WHEN ftcategory_code='7' THEN 'SURAT JALAN MESIN KE PEMBORONG'
                WHEN ftcategory_code='8' THEN 'SURAT JALAN JUAL SAMPAH DAN BAHAN'
                WHEN ftcategory_code='9' THEN 'SURAT JALAN PINJAMAN'
                WHEN ftcategory_code='10' THEN 'SURAT JALAN ALAT-ALAT MAINTENANCE'
                WHEN ftcategory_code='11' THEN 'SURAT JALAN INTERNAL'


                END AS  fttype,
        CASE 	WHEN ftcategory_code='0' THEN 'txdelivery_order_return_det_bahan_baku'
                WHEN ftcategory_code='1' THEN 'txdelivery_order_det_bahan_baku'
                WHEN ftcategory_code='2' THEN 'txdelivery_order_return_det_bahan_baku'
                WHEN ftcategory_code='3' THEN 'txdelivery_order_det_free'
                WHEN ftcategory_code='4' THEN ''
                WHEN ftcategory_code='5' THEN ''
                WHEN ftcategory_code='6' THEN 'txdelivery_order_det_sample'
                WHEN ftcategory_code='7' THEN ''
                WHEN ftcategory_code='8' THEN 'txdelivery_order_det_jual'
                WHEN ftcategory_code='9' THEN 'txdelivery_order_det_pinjaman'
                WHEN ftcategory_code='10' THEN 'txdelivery_order_det_alat'
                WHEN ftcategory_code='11' THEN 'txdelivery_order_det_internal'
                END AS  fttable,

        CASE WHEN ifnull(a.ftdocument_no,'')<>'' THEN ifnull(a.ftdocument_no,'') ELSE a.ftstock_out_no  END  as fttrans_no,a.fdtrans_date, b.fnindex,b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code,0 as fnmasuk,(fnqty)  as fnkeluar
        ,0 as fcprice
        from txstock_out_hdr_bahan_baku a,txstock_out_det_bahan_baku b,tlstock_hdr_bahan_baku c
        where a.ftstock_out_no=b.ftstock_out_no
                and a.fnstatus=1 and
                b.ftitem_code=c.ftitem_code
        //mutasi
        //keluar
        union all
        select  'MUTASI' as fttype, 'txstock_mutation_det_bahan_baku' as fttable , a.ftstock_mutation_no as fttrans_no,a.fdtrans_date, b.fnindex, b.ftitem_code,b.ftwarehouse_from as ftwarehouse_code,b.ftlocation_from as ftlocation_code,0 as fnmasuk,(fnqty) as fnkeluar,0 as fcprice
        from txstock_mutation_hdr_bahan_baku a,txstock_mutation_det_bahan_baku b,tlstock_hdr_bahan_baku c
        where a.ftstock_mutation_no=b.ftstock_mutation_no
                and a.fnstatus=1 and
                b.ftitem_code=c.ftitem_code
        //masuk
        union all
        select 'MUTASI' as fttype, 'txstock_mutation_det_bahan_baku' as fttable , a.ftstock_mutation_no as fttrans_no,a.fdtrans_date, b.fnindex, b.ftitem_code,b.ftwarehouse_to as ftwarehouse_code,b.ftlocation_to as ftlocation_code,(fnqty)  as fnmasuk,0 as fnkeluar,0 as fcprice
        from txstock_mutation_hdr_bahan_baku a,txstock_mutation_det_bahan_baku b,tlstock_hdr_bahan_baku c
        where a.ftstock_mutation_no=b.ftstock_mutation_no
                and a.fnstatus=1  and
                b.ftitem_code=c.ftitem_code
        //penyesuaian
        union all
        select 'PENYESUAIAN' as fttype, 'txstock_adjustment_det_bahan_baku' 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_opname)>(fnqty_available) then (fnqty_opname)-(fnqty_available) else 0 end as fnmasuk,
                case when (fnqty_opname)<(fnqty_available) then (fnqty_available)-(fnqty_opname) else 0 end as fnkeluar,0 as fcprice
        from txstock_adjustment_hdr_bahan_baku a,txstock_adjustment_det_bahan_baku b,tlstock_hdr_bahan_baku c
        where a.ftstock_adjustment_no=b.ftstock_adjustment_no  and  a.fnstatus=2  and
                b.ftitem_code=c.ftitem_code
        ) x
       #where x.ftitem_code='BSCABSGILDMXX.D013'
        ORDER BY x.fdtrans_date,x.fttrans_no,x.fnindex
        LIMIT 27001,1000
        ";
*/