<script type="text/javascript" src="script/ajax.js"></script>
<script type="text/javascript" src="script/ajax2.js"></script>

<?php
session_cache_expire(1440);
session_start();
session_name("insys");
require_once('include/config.php');
include "include/functions.php";
include "include/process_general_ledger.php";
include "include/fifo.php";
include "class/UctVw.php";
include "tools/update_stock_det.php";


if($_REQUEST['ajax']) {
    
    $startDate = $_REQUEST['Start_Date'] ;
    $endDate = $_REQUEST['End_Date'] ;
 
    switch($_REQUEST['code']) {
        case "update_stock_det":
            // Update Stock Detail
            UpdateStockDetail();
            pr("*** SELESAI UPDATE STOCK ***");
            die();
            break;
        
        case "check_fifo_outstanding":
            // Query FIFO Outstanding Tidak Sesuai
            $ssql= "SELECT a.fnidfifo,a.fnidfiforef,a.ftitem_code,a.ftwarehouse_code, a.fnqty, a.fnqty_outstanding, 
                            IFNULL(b.qty_out,0), ROUND(a.fnqty + IFNULL(b.qty_out,0),2) AS real_outstanding
                    FROM txstockfifo  a
                    LEFT JOIN (SELECT fnidfiforef, IFNULL(SUM(ROUND(b.fnqty,2)),0) AS qty_out, ftitem_code, ftwarehouse_code  
                                    FROM txstockfifo b
                                    WHERE b.fnstatus = 1 AND b.fnqty < 0 
                                    GROUP BY b.fnidfiforef ) b ON a.fnidfifo = b.fnidfiforef 
                    WHERE a.fnstatus = 1 AND a.fnqty > 0 
                            AND ROUND(a.fnqty_outstanding,2) != ROUND(a.fnqty + IFNULL(b.qty_out,0),2)
                    ORDER BY ftitem_code,fnidfiforef";
            echo("- FIFO Qty Outstanding TIDAK SESUAI <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("IdFifo : {$r['fnidfifo']} , IdFifoRef : {$r['fnidfiforef']}, Item Code : {$r['ftitem_code']}, Qty : {$r['fnqty']}, Qty Outstanding : {$r['fnqty_outstanding']} <br>");
            }
            echo("<br>");
            
            // query cek qty outstanding secara keseluruhan
            $ssql= "SELECT ROUND(SUM(fnqty),2) as fnqty , ROUND(SUM(fnqty_outstanding),2) as fnqty_outstanding
                    FROM txstockfifo WHERE fnstatus = 1 ";
            echo("- FIFO Qty & Qty Outstanding (TOTAL) <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Qty : {$r['fnqty']}, Qty Outstanding : {$r['fnqty_outstanding']} <br>");
            }
            echo("<br>");
            
            // query cek qty outstanding by location
            $ssql= "SELECT ftitem_code,ftwarehouse_code, SUM(fnqty) AS fnqty , SUM(fnqty_outstanding) AS fnqty_outstanding
                    FROM txstockfifo WHERE fnstatus = 1
                    GROUP BY ftitem_code,ftwarehouse_code
                    HAVING ROUND(SUM(fnqty),2) != ROUND(SUM(fnqty_outstanding),2)";
            echo("- FIFO Qty & Qty Outstanding (by Location) <br>");
                    
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Item Code : {$r['ftitem_code']}, Location : {$r['ftwarehouse_code']}, Qty : {$r['fnqty']}, Qty Outstanding : {$r['fnqty_outstanding']} <br>");
            }
            echo("<br>");
            
            // query cek harga FIFO dan Turunan FIFO yang tidak sesuai
            $ssql= "SELECT a.fnidfifo,a.fnidfiforef,a.ftitem_code,a.ftwarehouse_code, a.fcNet_Price, b.fcNet_Price AS fcNet_Price_FifoRef
                    FROM txstockfifo  a
                    LEFT JOIN (SELECT fnidfiforef, b.fcNet_Price, ftitem_code, ftwarehouse_code  
                                    FROM txstockfifo b
                                    WHERE b.fnstatus = 1 AND b.fnqty < 0 ) b ON a.fnidfifo = b.fnidfiforef 
                    WHERE a.fnstatus = 1 AND a.fnqty > 0 
                            AND a.fcNet_Price != b.fcNet_Price
                    ORDER BY ftitem_code,fnidfiforef";
            echo("- FIFO - Harga FIFO In & Fifo Out TIDAK SAMA <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("IdFifo : {$r['fnidfifo']} , IdFifoRef : {$r['fnidfiforef']}, Item Code : {$r['ftitem_code']}, Price FIFO IN : {$r['fcNet_Price']}, Price FIFO OUT : {$r['fcNet_Price_FifoRef']} <br>");
            }
            
            echo("<br><br>");
            echo("*** SELESAI ***");
            
            die();
            break;
     
        // SALES INVOICE    
        case "check_fifo_penjualan":
            // Query Stock Keluar Tidak ada di FIFO
            $ssql= "SELECT DISTINCT a.fdtrans_date,a.ftstock_out_no,a.fnstatus,c.ftdocument_no,c.fnstatus 
                    FROM txstock_out_hdr a INNER JOIN txstock_out_det b ON a.ftstock_out_no = b.ftstock_out_no
                    INNER JOIN tlstock_hdr d ON b.ftitem_code = d.ftitem_code AND d.ftstock_category_code = 'inv'	
                    LEFT JOIN txstockfifo c ON c.ftdocument_no = a.ftstock_out_no AND c.fnstatus = 1	
                    WHERE a.fnstatus = 1 AND c.ftdocument_no IS NULL";
            echo("- SALES INVOICE - FIFO TIDAK ADA <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Tgl : {$r['fdtrans_date']} , No SK : {$r['ftstock_out_no']} <br>");
            }
            echo("<br>");
            
            // query cek qty stock keluar dan FIFO
            $ssql= "SELECT DISTINCT fttrans_no, ROUND(SUM(tstock),2) AS tstock, ROUND(SUM(tfifo),2) AS tfifo
                    FROM (
                            SELECT DISTINCT a.ftstock_out_no AS fttrans_no,SUM(b.fnqty) AS tstock , 0 AS tfifo
                            FROM txstock_out_hdr a INNER JOIN txstock_out_det b ON a.ftstock_out_no = b.ftstock_out_no
                            INNER JOIN tlstock_hdr c ON b.ftitem_code = c.ftitem_code AND c.ftstock_category_code = 'inv'
                            WHERE a.fnstatus = 1 
                            GROUP BY a.ftstock_out_no
                            UNION ALL 
                            SELECT DISTINCT a.ftdocument_no AS fttrans_no,0 AS tstock, SUM(a.fnqty) * -1 AS tfifo 
                            FROM txstockfifo a 
                            WHERE a.fnstatus = 1 AND fnqty < 0  
                            GROUP BY a.ftdocument_no
                    ) X 
                    WHERE fttrans_no NOT LIKE 'AS%' AND fttrans_no NOT LIKE 'DS%' AND fttrans_no NOT LIKE 'MS%' AND fttrans_no NOT LIKE 'PS%'
                    GROUP BY fttrans_no
                    HAVING tstock != tfifo";
            echo("- SALES INVOICE - QTY FIFO TIDAK SESUAI <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No SK : {$r['fttrans_no']}, Qty Stock : {$r['tstock']}, Qty FIFO : {$r['tstock']} <br>");
            }
            
            echo("<br><br>");
            echo("*** SELESAI ***");
            
            die();
            break;
         
        case "check_sales_invoice":
            // Query cek jumlah sales invoice    
            $ssql= "SELECT count(fnid) FROM txsales_invoice_hdr a 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fnsaldo_awal = 0 AND a.fdtrans_date >= '{$startDate}'";
            $result = RetRstField($ssql);        
            echo("- Total Transaksi Sales Invoice : {$result} <br><br>");
                       
            // Query cek jumlah gl utk sales invoice        
            $ssql= "SELECT COUNT(fnid) FROM txgeneral_journal_hdr a 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.ftsource = 'sales_order' AND a.fdtrans_date >= '{$startDate}'";
            $result = RetRstField($ssql);        
            echo("- Total Jurnal Sales Invoice : {$result} <br><br>");
            
            // Query cek DO tidak terbuat SK
            $ssql= "SELECT * FROM txdelivery_order_hdr a 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' 
                        AND a.ftdo_no NOT IN 
                            (SELECT IFNULL(ftdocument_no,'') as ftdocument_no FROM txstock_out_hdr b
                             WHERE b.fnstatus != 3 AND b.fnstatus != 9 ) ";
            echo("- DO Tidak terbuat di SK <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("SO : {$r['ftso_no']} , DO : {$r['ftdo_no']} <br>");
            }
            echo("<br>");
            
            // Query cek no document sales invoice yg double di jurnal 
            $ssql= "SELECT a.ftsales_invoice_no, b.`ftjournal_no`, b.`ftdocument_no`, COUNT(a.ftsales_invoice_no)
                    FROM txsales_invoice_hdr a LEFT JOIN txgeneral_journal_hdr b ON a.ftsales_invoice_no = b.`ftdocument_no` AND b.fnstatus != 9 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fnsaldo_awal = 0 AND a.fdtrans_date >= '{$startDate}'
                    GROUP BY a.ftsales_invoice_no 
                    HAVING COUNT(a.ftsales_invoice_no) > 1
                    ORDER BY COUNT(a.ftsales_invoice_no)  DESC";
            echo("- Sales Invoice DOUBLE JURNAL <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Invoice : {$r['ftsales_invoice_no']} , Jurnal : {$r['ftjournal_no']} <br>");
            }
            echo("<br>");
            
            // query cek invoice yg tdk ada di jurnal
            $ssql= "SELECT DISTINCT ftsales_invoice_no,fnstatus FROM txsales_invoice_hdr a
                    WHERE fnstatus != 3 AND fnstatus!= 9 AND a.fnsaldo_awal = 0 AND a.fdtrans_date >= '{$startDate}'  
                            AND ftsales_invoice_no NOT IN 
                    (SELECT ftdocument_no  FROM txgeneral_journal_hdr a
                    WHERE a.fnstatus = 1 AND ftsource ='sales_order' )";
            echo("- Sales Invoice NO JURNAL <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                $ajaxFixSalesInvoice = " window.open('tools.php?ajax=1&code=fix_sales_invoice&ftsales_invoice_no={$r['ftsales_invoice_no']}','fix','height='+200+',width='+200+',top='+300+',left='+300+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
                echo("Invoice : {$r['ftsales_invoice_no']}&nbsp; <input type=\"button\" value=\"REPAIR\" onClick = \"{$ajaxFixSalesInvoice}\"><br>");
            }
            echo("<br>");
            
            // query cek jurnal yg tdk ada di invoice
            $ssql= "SELECT DISTINCT ftjournal_no, ftdocument_no,fnstatus FROM txgeneral_journal_hdr
                    WHERE fnstatus = 1 AND ftsource ='sales_order' AND ftdocument_no NOT IN 
                    (SELECT ftsales_invoice_no  FROM txsales_invoice_hdr a
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fnsaldo_awal = 0 AND a.fdtrans_date >= '{$startDate}' )";
            echo("- Sales Invoice Tidak ditemukan (Jurnal ADA) <br>");
                    
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Invoice : {$r['ftdocument_no']}, Jurnal : {$r['ftjournal_no']}<br>");
            }
            echo("<br><br>");
            echo("*** SELESAI ***");
            
            die();
            break;
            
        case "fix_sales_invoice":
            $fttrans_no = RetRstField("SELECT fnid FROM txgeneral_journal_hdr WHERE fnstatus != 9 AND ftdocument_no = '{$_REQUEST['ftsales_invoice_no']}'");
            
            if ($fttrans_no == "") {
                // Get Sales Invoice Hdr
                $ssql= "SELECT a.*
                        FROM txsales_invoice_hdr a WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_no']}'";
                $rs=mysql_query($ssql) or die($ssql);
                while($r=mysql_fetch_assoc($rs)) {
                    $_REQUEST['form'] = "sales_order";
                    $_REQUEST['mode'] = "final";
                    $_REQUEST['fnstatus'] = 2;
                    $_REQUEST['ftso_no'] = $r['ftso_no'];
                    $_REQUEST['fdtrans_date'] = CDateRev($r['fdtrans_date']); 
                    $_REQUEST['ftcustomer_code'] = $r['ftcustomer_code'];
                    $_REQUEST['ftnotes'] = $r['ftnotes'];
                    $_REQUEST['fcamount'] = $r['fcamount'];
                    $_REQUEST['fcrate'] = $r['fcrate'];
                    $_REQUEST['fcdiscount'] = $r['fcdiscount'];
                    $_REQUEST['fcsub_amount'] = $r['fcsub_amount'];
                    $_REQUEST["fcother_cost"] = $r['fcother_cost'];
                    $_REQUEST['fcvat'] = $r['fcvat'];        
                }

                // Get Sales Invoice Det
                $ssql= "SELECT a.* FROM txsales_invoice_det a WHERE a.ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_no']}' ORDER BY fnindex";
                $rs=mysql_query($ssql) or die($ssql);

                $_REQUEST['fnindexD0'][] = "";
                while($r=mysql_fetch_assoc($rs)) {
                    $_REQUEST['fnindexD0'][] = $r['fnindex'];
                    $_REQUEST['ftitem_codeD0'][$r['fnindex']] = $r['ftitem_code'];
                }

                // Call Function Process General Ledger
                if(function_exists("ProcessGL")) {
                    $m = ProcessGL();
                    Query_SqlArr($m,"1");
                }

                $varcounter = count($_REQUEST['fnindexD0']);
                echo("*** INSERT {$_REQUEST['ftsales_invoice_no']} SELESAI ***");
                
            } else {
                echo("***NO {$_REQUEST['ftsales_invoice_no']} SUDAH ADA DI JURNAL ***");
            }
            
            die();
            break;
        
        case "check_jurnal_fifo_sales_invoice":
            // Query cek jumlah sales invoice    
            $ssql= "SELECT ROUND(SUM(total_jurnal),2) AS total_jurnal,ROUND(SUM(total_transaksi),2) AS total_transaksi,ftsales_invoice_no,ftjournal_no,ftstock_out_no 
                    FROM (	-- jurnal sales invoice
                                SELECT SUM(b.fcdebit) AS total_jurnal,'' AS total_transaksi,a.ftdocument_no AS ftsales_invoice_no,a.ftjournal_no AS ftjournal_no,'' AS ftstock_out_no 
                                FROM txgeneral_journal_hdr a INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no
                                WHERE a.ftsource = 'sales_order' AND a.fnstatus !=3 AND a.fnstatus != 9 AND b.ftcoa_code != ''
                                        AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                GROUP BY a.ftjournal_no 

                                -- transaksi sales invoice
                                UNION ALL
                                SELECT '' AS total_jurnal, (fcamount + fccredit_cost) AS total_transaksi,ftsales_invoice_no,'' AS ftjournal_no,'' AS ftstock_out_no
                                FROM ( 
                                        SELECT (a.fcsub_amount*a.fcrate) + (a.fcvat*a.fcrate) AS fcamount, 
                                                IF(a.fcother_cost>0,a.fcother_cost*a.fcrate,'0') AS fccredit_cost,
                                                a.ftsales_invoice_no
                                        FROM txsales_invoice_hdr a
                                        WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                        GROUP BY a.ftsales_invoice_no 
                                        ORDER BY a.ftsales_invoice_no) X

                                -- fifo sales invoice
                                UNION ALL
                                SELECT '' AS total_jurnal, SUM(ABS(c.fnqty)*fcnet_price) AS total_transaksi,a.ftsales_invoice_no,'' AS ftjournal_no,c.ftdocument_no AS ftstock_out_no
                                FROM txsales_invoice_hdr a INNER JOIN txstock_out_hdr b ON a.ftdo_no = b.ftdocument_no
                                INNER JOIN TXStockFIFO c ON c.ftdocument_no = b.ftstock_out_no 
                                WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                        AND c.fnqty < 0 AND c.fttableref = 'txstock_out_det'
                                        AND b.fnstatus != 3 AND b.fnstatus != 9 AND c.fnstatus != 3 AND c.fnstatus != 9 
                                GROUP BY a.ftsales_invoice_no 

                        ) Z	
                    GROUP BY ftsales_invoice_no
                    HAVING total_jurnal != total_transaksi
                    ORDER BY ftsales_invoice_no";
            
            $ajaxFixJurnalFifoSalesInvoice = " window.open('tools.php?ajax=1&code=fix_jurnal_fifo_sales_invoice&Start_Date={$startDate}&End_Date={$endDate}','fix','height='+200+',width='+200+',top='+300+',left='+300+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
            echo("- Sales Invoice & Fifo TIDAK SESUAI Jurnal &nbsp; <input type=\"button\" value=\"REPAIR\" onClick = \"{$ajaxFixJurnalFifoSalesInvoice}\"><br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            
            $varcounter = 0;
            while($r=mysql_fetch_assoc($rs)) {
                $varcounter = $varcounter + 1;    
                echo("Invoice : {$r['ftsales_invoice_no']} , Jurnal : {$r['ftjournal_no']} , Total Jurnal : {$r['total_jurnal']} , Total Transaksi : {$r['total_transaksi']} <br>");
            }
            
            echo("<br><br>");
            echo("*** TOTAL {$varcounter} SALES INVOICE ***");
            
            die();
            break;
                
        case "fix_jurnal_fifo_sales_invoice":
            
            // Get Sales Invoice Yang Tidak Sesuai
            $ssql= "SELECT ROUND(SUM(total_jurnal),2) AS total_jurnal,ROUND(SUM(total_transaksi),2) AS total_transaksi,ftsales_invoice_no,ftjournal_no,ftstock_out_no 
                    FROM (	-- jurnal sales invoice
                                SELECT SUM(b.fcdebit) AS total_jurnal,'' AS total_transaksi,a.ftdocument_no AS ftsales_invoice_no,a.ftjournal_no AS ftjournal_no,'' AS ftstock_out_no 
                                FROM txgeneral_journal_hdr a INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no
                                WHERE a.ftsource = 'sales_order' AND a.fnstatus !=3 AND a.fnstatus != 9 AND b.ftcoa_code != ''
                                        AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                GROUP BY a.ftjournal_no 

                                -- transaksi sales invoice
                                UNION ALL
                                SELECT '' AS total_jurnal, (fcamount + fccredit_cost) AS total_transaksi,ftsales_invoice_no,'' AS ftjournal_no,'' AS ftstock_out_no
                                FROM ( 
                                        SELECT (a.fcsub_amount*a.fcrate) + (a.fcvat*a.fcrate) AS fcamount, 
                                                IF(a.fcother_cost>0,a.fcother_cost*a.fcrate,'0') AS fccredit_cost,
                                                a.ftsales_invoice_no
                                        FROM txsales_invoice_hdr a
                                        WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                        GROUP BY a.ftsales_invoice_no 
                                        ORDER BY a.ftsales_invoice_no) X

                                -- fifo sales invoice
                                UNION ALL
                                SELECT '' AS total_jurnal, SUM(ABS(c.fnqty)*fcnet_price) AS total_transaksi,a.ftsales_invoice_no,'' AS ftjournal_no,c.ftdocument_no AS ftstock_out_no
                                FROM txsales_invoice_hdr a INNER JOIN txstock_out_hdr b ON a.ftdo_no = b.ftdocument_no
                                INNER JOIN TXStockFIFO c ON c.ftdocument_no = b.ftstock_out_no 
                                WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                        AND c.fnqty < 0 AND c.fttableref = 'txstock_out_det'
                                        AND b.fnstatus != 3 AND b.fnstatus != 9 AND c.fnstatus != 3 AND c.fnstatus != 9 
                                GROUP BY a.ftsales_invoice_no 

                        ) Z	
                    GROUP BY ftsales_invoice_no
                    HAVING total_jurnal != total_transaksi
                    ORDER BY ftsales_invoice_no";
            $rs=mysql_query($ssql) or die($ssql);
                    
            $varcounter = 0;
            while($r=mysql_fetch_assoc($rs)) {
                ?>
                <script language='javascript'>
//                    ajax_do('tools.php?ajax=1&code=fix_jurnal_fifo_sales_invoice2&ftsales_invoice_no=<?php //echo $r['ftsales_invoice_no']; ?>');
                    window.open('tools.php?ajax=1&code=fix_jurnal_fifo_sales_invoice2&ftsales_invoice_no=<?php echo $r['ftsales_invoice_no']; ?>','result<?php echo $varcounter; ?>','height='+300+',width='+600+',top='+250+',left='+250+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen=1');
                </script>
                <?php
            
                $varcounter = $varcounter + 1;
                echo("*** INSERT {$r['ftsales_invoice_no']} ***");                
            }
            
            echo("*** PROSES {$varcounter} Jurnal SELESAI ***");

            die();
            break;
            
        case "fix_jurnal_fifo_sales_invoice2":
            $ftjournal_no = RetRstField("SELECT ftjournal_no FROM txgeneral_journal_hdr WHERE fnstatus != 9 AND ftdocument_no = '{$_REQUEST['ftsales_invoice_no']}'");

            // Batal Jurnal Header
            $ssql2= "UPDATE txgeneral_journal_hdr SET fnstatus = 9 WHERE ftjournal_no = '{$ftjournal_no}'";
            $rs2=mysql_query($ssql2) or die($ssql2);

            // Get Sales Invoice Hdr
            $ssql2= "SELECT a.*
                    FROM txsales_invoice_hdr a WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_no']}'";
            $rs2=mysql_query($ssql2) or die($ssql2);

            while($r2=mysql_fetch_assoc($rs2)) {
                $_REQUEST['form'] = "sales_order";
                $_REQUEST['mode'] = "final";
                $_REQUEST['fnstatus'] = 2;
                $_REQUEST['ftso_no'] = $r2['ftso_no'];
                $_REQUEST['fdtrans_date'] = CDateRev($r2['fdtrans_date']); 
                $_REQUEST['ftcustomer_code'] = $r2['ftcustomer_code'];
                $_REQUEST['ftnotes'] = $r2['ftnotes'];
                $_REQUEST['fcamount'] = $r2['fcamount'];
                $_REQUEST['fcrate'] = $r2['fcrate'];
                $_REQUEST['fcdiscount'] = $r2['fcdiscount'];
                $_REQUEST['fcsub_amount'] = $r2['fcsub_amount'];
                $_REQUEST["fcother_cost"] = $r2['fcother_cost'];
                $_REQUEST['fcvat'] = $r2['fcvat'];        
            }

            // Get Sales Invoice Det
            $ssql2= "SELECT a.* FROM txsales_invoice_det a WHERE a.ftsales_invoice_no = '{$_REQUEST['ftsales_invoice_no']}' ORDER BY fnindex";
            $rs2=mysql_query($ssql2) or die($ssql2);

            $_REQUEST['fnindexD0'][] = "";
            while($r2=mysql_fetch_assoc($rs2)) {
                $_REQUEST['fnindexD0'][] = $r2['fnindex'];
                $_REQUEST['ftitem_codeD0'][$r2['fnindex']] = $r2['ftitem_code'];
            }

            // Call Function Process General Ledger
            if(function_exists("ProcessGL")) {
                $m = ProcessGL();
                Query_SqlArr($m,"1");
            }
            
            echo("*** FINISH INSERT {$r2['ftsales_invoice_no']} ***");                
            die();
            break;    
            
        // SALES RETUR    
        case "check_fifo_sales_retur":
            // Query Sales Retur Tidak ada di Stock Masuk
            $ssql= "SELECT DISTINCT fdtrans_date,ftsales_return_no,fnstatus FROM txsales_return_hdr a
                    WHERE fnstatus != 3 AND fnstatus!= 9  
                            AND ftsales_return_no NOT IN 
                                (SELECT ftdocument_no FROM txstock_in_hdr a WHERE a.fnstatus = 1) ";
            echo("- SALES RETUR - STOCK MASUK TIDAK ADA <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Tgl : {$r['fdtrans_date']} , No SR : {$r['ftsales_return_no']} <br>");
            }
            echo("<br>");
            
            // Query Sales Retur Lebih dari 1 Stock Masuk
            $ssql= "SELECT a.fdtrans_date, a.ftsales_return_no, COUNT(b.ftdocument_no) AS counter FROM txsales_return_hdr a
                    INNER JOIN txstock_in_hdr b ON a.ftsales_return_no = b.ftdocument_no
                    WHERE a.fnstatus != 3 AND a.fnstatus!= 9  AND b.fnstatus = 1
                    GROUP BY a.ftsales_return_no
                    HAVING counter > 1";
            echo("- SALES RETUR - STOCK MASUK LEBIH DARI SATU <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Tgl : {$r['fdtrans_date']} , No SR : {$r['ftsales_return_no']} , Counter : {$r['counter']} <br>");
            }
            echo("<br>");
            
            // Query Stock Masuk Tidak ada di FIFO
            $ssql= "SELECT DISTINCT a.fdtrans_date,a.ftsales_return_no,a.fnstatus 
                    FROM txsales_return_hdr a INNER JOIN txsales_return_det b ON a.ftsales_return_no = b.ftsales_return_no
                    INNER JOIN tlstock_hdr c ON b.ftitem_code = c.ftitem_code AND c.ftstock_category_code = 'inv'	
                    WHERE a.fnstatus != 3 AND a.fnstatus!= 9  
                            AND a.ftsales_return_no NOT IN 
                                (SELECT ftdocument_no  FROM txstockfifo a WHERE a.fnstatus = 1 ) ";
            echo("- SALES RETUR - FIFO TIDAK ADA <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Tgl : {$r['fdtrans_date']} , No SR : {$r['ftsales_return_no']} <br>");
            }
            echo("<br>");
            
            // query cek qty stock keluar dan FIFO
            $ssql= "SELECT DISTINCT fttrans_no, ftso_no, ROUND(SUM(tstock),2) AS tstock, ROUND(SUM(tfifo),2) AS tfifo
                    FROM (
                            SELECT DISTINCT a.ftdocument_no AS fttrans_no,SUM(b.fnqty) AS tstock , 0 AS tfifo
                            FROM txstock_in_hdr a INNER JOIN txstock_in_det b ON a.ftstock_in_no = b.ftstock_in_no
                            INNER JOIN tlstock_hdr c ON b.ftitem_code = c.ftitem_code AND c.ftstock_category_code = 'inv'
                            WHERE a.fnstatus = 1 AND a.ftcategory_code = 4
                            GROUP BY a.ftstock_in_no
                            UNION ALL 
                            SELECT DISTINCT a.ftdocument_no AS fttrans_no,0 AS tstock, SUM(a.fnqty) AS tfifo 
                            FROM txstockfifo a 
                            WHERE a.fnstatus = 1 AND fnqty > 0 AND fttableref = 'txsales_return_det'
                            GROUP BY a.ftdocument_no
                    ) X 
                    INNER JOIN txsales_return_hdr z ON x.fttrans_no = z.ftsales_return_no
                    WHERE fttrans_no NOT LIKE 'AS%' AND fttrans_no NOT LIKE 'DS%' AND fttrans_no NOT LIKE 'MS%' AND fttrans_no NOT LIKE 'PS%'
                    GROUP BY fttrans_no
                    HAVING tstock != tfifo  ";
            echo("- SALES RETUR - QTY FIFO TIDAK SESUAI <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No SM : {$r['fttrans_no']}, Qty Stock : {$r['tstock']}, Qty FIFO : {$r['tstock']} <br>");
            }
            
            echo("<br><br>");
            echo("*** SELESAI ***");
            
            die();
            break;
         
        case "check_sales_retur":
            // Query cek jumlah sales retur    
            $ssql= "SELECT count(fnid) FROM txsales_return_hdr a 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}'";
            $result = RetRstField($ssql);        
            echo("- Total Transaksi Sales Retur : {$result} <br><br>");
                       
            // Query cek jumlah gl utk sales retur        
            $ssql= "SELECT COUNT(fnid) FROM txgeneral_journal_hdr a 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.ftsource = 'sales_return' AND a.fdtrans_date >= '{$startDate}'";
            $result = RetRstField($ssql);        
            echo("- Total Jurnal Sales Retur : {$result} <br><br>");
                      
            // Query cek no document sales retur yg double di jurnal 
            $ssql= "SELECT a.ftsales_return_no, b.`ftjournal_no`, b.`ftdocument_no`, COUNT(a.ftsales_return_no)
                    FROM txsales_return_hdr a LEFT JOIN txgeneral_journal_hdr b ON a.ftsales_return_no = b.`ftdocument_no` AND b.fnstatus != 9 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}'
                    GROUP BY a.ftsales_return_no 
                    HAVING COUNT(a.ftsales_return_no) > 1
                    ORDER BY COUNT(a.ftsales_return_no)  DESC";
            echo("- Sales Return DOUBLE JURNAL <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Sales Return : {$r['ftsales_return_no']} , Jurnal : {$r['ftjournal_no']} <br>");
            }
            echo("<br>");
            
            // query cek retur yg tdk ada di jurnal
            $ssql= "SELECT DISTINCT ftsales_return_no,fnstatus FROM txsales_return_hdr a
                    WHERE fnstatus != 3 AND fnstatus!= 9 AND a.fdtrans_date >= '{$startDate}'  
                            AND ftsales_return_no NOT IN 
                    (SELECT ftdocument_no  FROM txgeneral_journal_hdr a
                    WHERE a.fnstatus = 1 AND ftsource ='sales_return' )";
            echo("- Sales Return NO JURNAL <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                $ajaxFixSalesRetur = " window.open('tools.php?ajax=1&code=fix_sales_retur&ftsales_return_no={$r['ftsales_return_no']}','fix','height='+200+',width='+200+',top='+300+',left='+300+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
                echo("Sales Return : {$r['ftsales_return_no']}&nbsp; <input type=\"button\" value=\"REPAIR\" onClick = \"{$ajaxFixSalesRetur}\"><br>");
            }
            echo("<br>");
            
            // query cek jurnal yg tdk ada di retur
            $ssql= "SELECT DISTINCT ftjournal_no, ftdocument_no,fnstatus FROM txgeneral_journal_hdr
                    WHERE fnstatus = 1 AND ftsource ='sales_return' AND ftdocument_no NOT IN 
                    (SELECT ftsales_return_no  FROM txsales_return_hdr a
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' )";
            echo("- Sales Retur Tidak ditemukan (Jurnal ADA) <br>");
                    
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Sales Retur : {$r['ftdocument_no']}, Jurnal : {$r['ftjournal_no']}<br>");
            }
            echo("<br><br>");
            echo("*** SELESAI ***");
            
            die();
            break;
            
        case "fix_sales_retur":
            $fttrans_no = RetRstField("SELECT fnid FROM txgeneral_journal_hdr WHERE fnstatus != 9 AND ftdocument_no = '{$_REQUEST['ftsales_return_no']}'");
            
            if ($fttrans_no == "") {
                // Get Sales Return Hdr
                $ssql= "SELECT a.*
                        FROM txsales_return_hdr a WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.ftsales_return_no = '{$_REQUEST['ftsales_return_no']}'";
                $rs=mysql_query($ssql) or die($ssql);
                while($r=mysql_fetch_assoc($rs)) {
                    $_REQUEST['form'] = "sales_return";
                    $_REQUEST['mode'] = "simpan";
                    $_REQUEST['fnstatus'] = 1;
                    $_REQUEST['ftso_no'] = $r['ftso_no'];
                    $_REQUEST['ftsales_return_no'] = $r['ftsales_return_no'];
                    $_REQUEST['fdtrans_date'] = CDateRev($r['fdtrans_date']); 
                    $_REQUEST['ftcustomer_code'] = $r['ftcustomer_code'];
                    $_REQUEST['ftnotes'] = $r['ftnotes'];
                    $_REQUEST['fcsub_amount'] = $r['fcsub_amount'];
                    $_REQUEST["fcother_cost"] = $r['fcother_cost'];
                    $_REQUEST['fcvat'] = $r['fcvat'];     
                    $_REQUEST['fcamount'] = $r['fcamount'];
                    $_REQUEST['fcdiscount'] = $r['fcdiscount'];
                }

                // Get Sales Retur Det
                $ssql= "SELECT a.* FROM txsales_return_det a WHERE a.ftsales_return_no = '{$_REQUEST['ftsales_return_no']}' and a.fnqty > 0 ORDER BY fnindex";
                $rs=mysql_query($ssql) or die($ssql);

                $_REQUEST['fnindexD0'][] = "";
                while($r=mysql_fetch_assoc($rs)) {
                    $_REQUEST['fnindexD0'][] = $r['fnindex'];
                    $_REQUEST['ftitem_codeD0'][$r['fnindex']] = $r['ftitem_code'];
                }

                // Call Function Process General Ledger
                if(function_exists("ProcessGL")) {
                    $m = ProcessGL();
                    Query_SqlArr($m,"1");
                }

                $varcounter = count($_REQUEST['fnindexD0']);
                echo("*** INSERT {$_REQUEST['ftsales_return_no']} SELESAI ***");
                
            } else {
                echo("***NO {$_REQUEST['ftsales_return_no']} SUDAH ADA DI JURNAL ***");
            }
            
            die();
            break;
        
        case "check_jurnal_fifo_sales_retur":
            // Query cek jumlah sales retur    
            $ssql= "SELECT ROUND(SUM(total_jurnal),2) AS total_jurnal,ROUND(SUM(total_transaksi),2) AS total_transaksi,ftsales_return_no,ftjournal_no,ftstock_in_no 
                    FROM (	-- jurnal sales return
                                SELECT SUM(b.fcdebit) AS total_jurnal,'' AS total_transaksi,a.ftdocument_no AS ftsales_return_no,a.ftjournal_no AS ftjournal_no,'' AS ftstock_in_no 
                                FROM txgeneral_journal_hdr a INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no
                                WHERE a.ftsource = 'sales_return' AND a.fnstatus !=3 AND a.fnstatus != 9 
                                        AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                GROUP BY a.ftjournal_no 

                                -- transaksi sales return
                                UNION ALL
                                SELECT '' AS total_jurnal, (fcamount + fcdebit_cost) AS total_transaksi,ftsales_return_no,'' AS ftjournal_no,'' AS ftstock_in_no
                                FROM ( 
                                        SELECT (a.fcsub_amount*ifnull(b.fcrate,1)) + (a.fcvat*ifnull(b.fcrate,1)) AS fcamount, 
                                                IF(a.fcother_cost>0,a.fcother_cost*ifnull(b.fcrate,1),'0') AS fcdebit_cost,
                                                a.ftsales_return_no
                                        FROM txsales_return_hdr a
                                        LEFT JOIN txsales_order_hdr b ON a.ftso_no = b.ftso_no AND b.fnstatus != 3 AND b.fnstatus != 9 
                                        WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                        GROUP BY a.ftsales_return_no 
                                        ORDER BY a.ftsales_return_no) X

                                -- fifo sales return
                                UNION ALL
                                SELECT '' AS total_jurnal, SUM(ABS(c.fnqty)*fcnet_price) AS total_transaksi,a.ftsales_return_no,'' AS ftjournal_no,c.ftdocument_no AS ftstock_in_no
                                FROM txsales_return_hdr a INNER JOIN TXStockFIFO c ON c.ftdocument_no = a.ftsales_return_no 
                                WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                        AND c.fnqty > 0 AND c.fttableref = 'txsales_return_det'
                                        AND a.fnstatus != 3 AND a.fnstatus != 9 AND c.fnstatus != 3 AND c.fnstatus != 9 
                                GROUP BY a.ftsales_return_no 

                        ) Z	
                    GROUP BY ftsales_return_no
                    HAVING total_jurnal != total_transaksi
                    ORDER BY ftsales_return_no";
            
            $ajaxFixJurnalFifoSalesRetur = " window.open('tools.php?ajax=1&code=fix_jurnal_fifo_sales_retur&Start_Date={$startDate}&End_Date={$endDate}','fix','height='+200+',width='+200+',top='+300+',left='+300+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
            echo("- Sales Retur & Fifo TIDAK SESUAI Jurnal &nbsp; <input type=\"button\" value=\"REPAIR\" onClick = \"{$ajaxFixJurnalFifoSalesRetur}\"><br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            
            $varcounter = 0;
            while($r=mysql_fetch_assoc($rs)) {
                $varcounter = $varcounter + 1;    
                echo("Sales Retur : {$r['ftsales_return_no']} , Jurnal : {$r['ftjournal_no']} , Total Jurnal : {$r['total_jurnal']} , Total Transaksi : {$r['total_transaksi']} <br>");
            }
            
            echo("<br><br>");
            echo("*** TOTAL {$varcounter} SALES RETURN ***");
            
            die();
            break;
                
        case "fix_jurnal_fifo_sales_retur":
            
            // Get Sales Return Yang Tidak Sesuai
            $ssql= "SELECT ROUND(SUM(total_jurnal),2) AS total_jurnal,ROUND(SUM(total_transaksi),2) AS total_transaksi,ftsales_return_no,ftjournal_no,ftstock_in_no 
                    FROM (	-- jurnal sales return
                                SELECT SUM(b.fcdebit) AS total_jurnal,'' AS total_transaksi,a.ftdocument_no AS ftsales_return_no,a.ftjournal_no AS ftjournal_no,'' AS ftstock_in_no 
                                FROM txgeneral_journal_hdr a INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no
                                WHERE a.ftsource = 'sales_return' AND a.fnstatus !=3 AND a.fnstatus != 9 
                                        AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                GROUP BY a.ftjournal_no 

                                -- transaksi sales return
                                UNION ALL
                                SELECT '' AS total_jurnal, (fcamount + fcdebit_cost) AS total_transaksi,ftsales_return_no,'' AS ftjournal_no,'' AS ftstock_in_no
                                FROM ( 
                                        SELECT (a.fcsub_amount*ifnull(b.fcrate,1)) + (a.fcvat*ifnull(b.fcrate,1)) AS fcamount, 
                                                IF(a.fcother_cost>0,a.fcother_cost*ifnull(b.fcrate,1),'0') AS fcdebit_cost,
                                                a.ftsales_return_no
                                        FROM txsales_return_hdr a
                                        LEFT JOIN txsales_order_hdr b ON a.ftso_no = b.ftso_no AND b.fnstatus != 3 AND b.fnstatus != 9 
                                        WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                        GROUP BY a.ftsales_return_no 
                                        ORDER BY a.ftsales_return_no) X

                                -- fifo sales return
                                UNION ALL
                                SELECT '' AS total_jurnal, SUM(ABS(c.fnqty)*fcnet_price) AS total_transaksi,a.ftsales_return_no,'' AS ftjournal_no,c.ftdocument_no AS ftstock_in_no
                                FROM txsales_return_hdr a INNER JOIN TXStockFIFO c ON c.ftdocument_no = a.ftsales_return_no 
                                WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                        AND c.fnqty > 0 AND c.fttableref = 'txsales_return_det'
                                        AND a.fnstatus != 3 AND a.fnstatus != 9 AND c.fnstatus != 3 AND c.fnstatus != 9 
                                GROUP BY a.ftsales_return_no 

                        ) Z	
                    GROUP BY ftsales_return_no
                    HAVING total_jurnal != total_transaksi
                    ORDER BY ftsales_return_no";
            $rs=mysql_query($ssql) or die($ssql);
                    
            $varcounter = 0;
            while($r=mysql_fetch_assoc($rs)) {
                ?>
                <script language='javascript'>
//                    ajax_do('tools.php?ajax=1&code=fix_jurnal_fifo_sales_invoice2&ftsales_invoice_no=<?php //echo $r['ftsales_invoice_no']; ?>');
                    window.open('tools.php?ajax=1&code=fix_jurnal_fifo_sales_retur2&ftsales_return_no=<?php echo $r['ftsales_return_no']; ?>','result<?php echo $varcounter; ?>','height='+300+',width='+600+',top='+250+',left='+250+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen=1');
                </script>
                <?php
            
                $varcounter = $varcounter + 1;
                echo("*** INSERT {$r['ftsales_return_no']} ***");                
            }
            
            echo("*** PROSES {$varcounter} Jurnal SELESAI ***");

            die();
            break;
            
        case "fix_jurnal_fifo_sales_retur2":
            
            $ftjournal_no = RetRstField("SELECT ftjournal_no FROM txgeneral_journal_hdr WHERE fnstatus != 9 AND ftdocument_no = '{$_REQUEST['ftsales_return_no']}'");

            // Batal Jurnal Header
            $ssql2= "UPDATE txgeneral_journal_hdr SET fnstatus = 9 WHERE ftjournal_no = '{$ftjournal_no}'";
            $rs2=mysql_query($ssql2) or die($ssql2);

            // Get Sales Return Hdr
            $ssql2= "SELECT a.*,IFNULL(b.fcrate,1) as fcrate FROM txsales_return_hdr a LEFT JOIN txsales_order_hdr b ON a.ftso_no = b.ftso_no AND b.fnstatus != 3 and b.fnstatus != 9 
                     WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.ftsales_return_no = '{$_REQUEST['ftsales_return_no']}'";
            $rs2=mysql_query($ssql2) or die($ssql2);

            while($r2=mysql_fetch_assoc($rs2)) {
                $_REQUEST['form'] = "sales_return";
                $_REQUEST['mode'] = "simpan";
                $_REQUEST['fnstatus'] = 1;
                $_REQUEST['ftso_no'] = $r2['ftso_no'];
                $_REQUEST['ftsales_return_no'] = $r2['ftsales_return_no'];
                $_REQUEST['fdtrans_date'] = CDateRev($r2['fdtrans_date']); 
                $_REQUEST['ftcustomer_code'] = $r2['ftcustomer_code'];
                $_REQUEST['ftnotes'] = $r2['ftnotes'];
                $_REQUEST['fcamount'] = $r2['fcamount'];
                $_REQUEST['fcrate'] = $r2['fcrate'];
                $_REQUEST['fcdiscount'] = $r2['fcdiscount'];
                $_REQUEST['fcsub_amount'] = $r2['fcsub_amount'];
                $_REQUEST["fcother_cost"] = $r2['fcother_cost'];
                $_REQUEST['fcvat'] = $r2['fcvat'];        
            }

            // Get Sales Invoice Det
            $ssql2= "SELECT a.* FROM txsales_return_det a WHERE a.ftsales_return_no = '{$_REQUEST['ftsales_return_no']}' ORDER BY fnindex";
            $rs2=mysql_query($ssql2) or die($ssql2);

            $_REQUEST['fnindexD0'][] = "";
            while($r2=mysql_fetch_assoc($rs2)) {
                $_REQUEST['fnindexD0'][] = $r2['fnindex'];
                $_REQUEST['ftitem_codeD0'][$r2['fnindex']] = $r2['ftitem_code'];
            }

            // Call Function Process General Ledger
            if(function_exists("ProcessGL")) {
                $m = ProcessGL();
                Query_SqlArr($m,"1");
            }
            
            echo("*** FINISH INSERT {$r2['ftsales_return_no']} ***");                
            die();
            break;    
                
        // SALES ADJUSTMENT
        case "check_sales_adjustment":
            // Query cek jumlah sales adjustment    
            $ssql= "SELECT count(fnid) FROM txsales_invoice_adjustment_hdr a 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}'";
            $result = RetRstField($ssql);        
            echo("- Total Transaksi Sales Adjustment : {$result} <br><br>");
                       
            // Query cek jumlah gl utk sales adjustment        
            $ssql= "SELECT COUNT(fnid) FROM txgeneral_journal_hdr a 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.ftsource = 'sales_invoice_adjustment' AND a.fdtrans_date >= '{$startDate}'";
            $result = RetRstField($ssql);        
            echo("- Total Jurnal Sales Adjustment : {$result} <br><br>");
                      
            // Query cek no document sales adjustment yg double di jurnal 
            $ssql= "SELECT a.ftsales_invoice_no, b.`ftjournal_no`, b.`ftdocument_no`, COUNT(a.ftsales_invoice_no)
                    FROM txsales_invoice_adjustment_hdr a LEFT JOIN txgeneral_journal_hdr b ON a.ftsales_invoice_no = b.`ftdocument_no` AND b.fnstatus != 9 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}'
                    GROUP BY a.ftsales_invoice_no 
                    HAVING COUNT(a.ftsales_invoice_no) > 1
                    ORDER BY COUNT(a.ftsales_invoice_no)  DESC";
            echo("- Sales Adjustment DOUBLE JURNAL <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Sales Adjustment : {$r['ftsales_invoice_no']} , Jurnal : {$r['ftjournal_no']} <br>");
            }
            echo("<br>");
            
            // query cek adjustment yg tdk ada di jurnal
            $ssql= "SELECT DISTINCT ftsales_invoice_no,fnstatus FROM txsales_invoice_adjustment_hdr a
                    WHERE fnstatus != 3 AND fnstatus!= 9 AND a.fdtrans_date >= '{$startDate}'  
                            AND ftsales_invoice_no NOT IN 
                    (SELECT ftdocument_no  FROM txgeneral_journal_hdr a
                    WHERE a.fnstatus = 1 AND ftsource ='sales_invoice_adjustment' )";
            echo("- Sales Adjustment NO JURNAL <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                $ajaxFixSalesAdjustment = " window.open('tools.php?ajax=1&code=fix_sales_adjustment&ftsales_adjustment_no={$r['ftsales_adjustment_no']}','fix','height='+200+',width='+200+',top='+300+',left='+300+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
                echo("Sales Adjustment : {$r['ftsales_invoice_no']}&nbsp; <input type=\"button\" value=\"REPAIR\" onClick = \"{$ajaxFixSalesAdjustment}\"><br>");
            }
            echo("<br>");
            
            // query cek jurnal yg tdk ada di adjustment
            $ssql= "SELECT DISTINCT ftjournal_no, ftdocument_no,fnstatus FROM txgeneral_journal_hdr
                    WHERE fnstatus = 1 AND ftsource ='sales_invoice_adjustment' AND ftdocument_no NOT IN 
                    (SELECT ftsales_invoice_no  FROM txsales_invoice_adjustment_hdr a
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' )";
            echo("- Sales Adjustment Tidak ditemukan (Jurnal ADA) <br>");
                    
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Sales Adjustment : {$r['ftdocument_no']}, Jurnal : {$r['ftjournal_no']}<br>");
            }
            echo("<br><br>");
            echo("*** SELESAI ***");
            
            die();
            break;
            
        case "fix_sales_adjustment":
            $fttrans_no = RetRstField("SELECT fnid FROM txgeneral_journal_hdr WHERE fnstatus != 9 AND ftdocument_no = '{$_REQUEST['ftsales_adjustment_no']}'");
            
            if ($fttrans_no == "") {
                // Get Sales Adjustment Hdr
                $ssql= "SELECT a.*
                        FROM txsales_invoice_adjustment_hdr a WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.ftsales_adjustment_no = '{$_REQUEST['ftsales_adjustment_no']}'";
                $rs=mysql_query($ssql) or die($ssql);
                while($r=mysql_fetch_assoc($rs)) {
                    $_REQUEST['form'] = "sales_invoice_adjustment";
                    $_REQUEST['mode'] = "simpan";
                    $_REQUEST['fnstatus'] = 1;
                    $_REQUEST['ftso_no'] = $r['ftso_no'];
                    $_REQUEST['ftsales_invoice_no'] = $r['ftsales_invoice_no'];
                    $_REQUEST['fdtrans_date'] = CDateRev($r['fdtrans_date']); 
                    $_REQUEST['ftcustomer_code'] = $r['ftcustomer_code'];
                    $_REQUEST['fcrate'] = $r['fcrate'];
                    $_REQUEST['ftnotes'] = $r['ftnotes'];
                    $_REQUEST['fcsub_amount'] = $r['fcsub_amount'];
                    $_REQUEST["fcother_cost"] = $r['fcother_cost'];
                    $_REQUEST['fcvat'] = $r['fcvat'];     
                    $_REQUEST['fcamount'] = $r['fcamount'];
                    $_REQUEST['fcdiscount'] = $r['fcdiscount'];
                }

                // Call Function Process General Ledger
                if(function_exists("ProcessGL")) {
                    $m = ProcessGL();
                    Query_SqlArr($m,"1");
                }

                echo("*** INSERT {$_REQUEST['ftsales_invoice_no']} SELESAI ***");
                
            } else {
                echo("***NO {$_REQUEST['ftsales_invoice_no']} SUDAH ADA DI JURNAL ***");
            }
            
            die();
            break;
        
        case "check_jurnal_fifo_sales_adjustment":
            // Query cek jumlah sales adjustment    
            $ssql= "SELECT ROUND(SUM(total_jurnal),2) AS total_jurnal,ROUND(SUM(total_transaksi),2) AS total_transaksi,ftsales_invoice_no,ftjournal_no,ftstock_in_no 
                    FROM (	-- jurnal sales adjustment
                                SELECT SUM(b.fcdebit) AS total_jurnal,'' AS total_transaksi,a.ftdocument_no AS ftsales_invoice_no,a.ftjournal_no AS ftjournal_no,'' AS ftstock_in_no 
                                FROM txgeneral_journal_hdr a INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no
                                WHERE a.ftsource = 'sales_invoice_adjustment' AND a.fnstatus !=3 AND a.fnstatus != 9 
                                        AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                GROUP BY a.ftjournal_no 

                                -- transaksi sales adjustment
                                UNION ALL
                                SELECT '' AS total_jurnal, (fcamount) AS total_transaksi,ftsales_invoice_no,'' AS ftjournal_no,'' AS ftstock_in_no
                                FROM ( 
                                        SELECT abs(a.fcamount) as fcamount, a.ftsales_invoice_no
                                        FROM txsales_invoice_adjustment_hdr a 
                                        WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                        GROUP BY a.ftsales_invoice_no 
                                        ORDER BY a.ftsales_invoice_no) X


                        ) Z	
                    GROUP BY ftsales_invoice_no
                    HAVING total_jurnal != total_transaksi
                    ORDER BY ftsales_invoice_no";
            
            //$ajaxFixJurnalFifoSalesAdjustment = " window.open('tools.php?ajax=1&code=fix_jurnal_fifo_sales_adjustment&Start_Date={$startDate}&End_Date={$endDate}','fix','height='+200+',width='+200+',top='+300+',left='+300+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
            echo("- Sales Adjustment TIDAK SESUAI Jurnal &nbsp; <input type=\"button\" value=\"REPAIR\" onClick = \"{$ajaxFixJurnalFifoSalesAdjustment}\"><br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            
            $varcounter = 0;
            while($r=mysql_fetch_assoc($rs)) {
                $varcounter = $varcounter + 1;    
                echo("Sales Adjustment : {$r['ftsales_invoice_no']} , Jurnal : {$r['ftjournal_no']} , Total Jurnal : {$r['total_jurnal']} , Total Transaksi : {$r['total_transaksi']} <br>");
            }
            
            echo("<br><br>");
            echo("*** TOTAL {$varcounter} SALES ADJUSTMENT ***");
            
            die();
            break;
            
        // PURCHASE INVOICE
        case "check_fifo_pembelian":
            // Query Stock Masuk (kecuali sales retur) Tidak ada di FIFO
            $ssql= "SELECT DISTINCT a.fdtrans_date,a.ftstock_in_no,a.fnstatus,c.ftdocument_no,c.fnstatus 
                    FROM txstock_in_hdr a INNER JOIN txstock_in_det b ON a.ftstock_in_no = b.ftstock_in_no
                    INNER JOIN tlstock_hdr d ON b.ftitem_code = d.ftitem_code AND d.ftstock_category_code = 'inv'	
                    LEFT JOIN txstockfifo c ON c.ftdocument_no = a.ftstock_in_no AND c.fnstatus = 1	
                    WHERE a.fnstatus = 1 AND a.ftcategory_code != 4 AND c.ftdocument_no IS NULL ";
            echo("- STOCK MASUK (KECUALI RETUR) - FIFO TIDAK ADA <br>");
                    
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Tgl : {$r['fdtrans_date']} , No SK : {$r['ftstock_out_no']} <br>");
            }
            echo("<br>");
            
            // query cek qty stock masuk dan FIFO
            $ssql= "SELECT DISTINCT fttrans_no, ROUND(SUM(tstock),2) AS tstock, ROUND(SUM(tfifo),2) AS tfifo
                    FROM (
                            SELECT DISTINCT a.ftstock_in_no AS fttrans_no,SUM(b.fnqty) AS tstock , 0 AS tfifo
                            FROM txstock_in_hdr a INNER JOIN txstock_in_det b ON a.ftstock_in_no = b.ftstock_in_no
                            INNER JOIN tlstock_hdr c ON b.ftitem_code = c.ftitem_code AND c.ftstock_category_code = 'inv'
                            WHERE a.fnstatus = 1 AND a.ftcategory_code != 4 
                            GROUP BY a.ftstock_in_no
                            UNION ALL 
                            SELECT DISTINCT a.ftdocument_no AS fttrans_no,0 AS tstock, SUM(a.fnqty) AS tfifo 
                            FROM txstockfifo a 
                            WHERE a.fnstatus = 1 AND fnqty > 0  
                            GROUP BY a.ftdocument_no
                    ) X 
                    WHERE fttrans_no NOT LIKE 'IS%' AND fttrans_no NOT LIKE 'SR%' AND fttrans_no NOT LIKE 'MS%' AND fttrans_no NOT LIKE 'PS%'
                    GROUP BY fttrans_no
                    HAVING tstock != tfifo ";
            echo("- STOCK MASUK - QTY FIFO TIDAK SESUAI <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No SM : {$r['fttrans_no']}, Qty Stock : {$r['tstock']}, Qty FIFO : {$r['tstock']} <br>");
            }
            
            echo("<br><br>");
            echo("*** SELESAI ***");
            
            die();
            break;
            
        case "check_purchase_invoice":
            // Query cek jumlah purchase invoice    
            $ssql= "SELECT count(fnid) FROM txpurchase_invoice_hdr a 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fnsaldo_awal = 0 AND a.fdtrans_date >= '{$startDate}'";
            $result = RetRstField($ssql);        
            echo("- Total Transaksi Purchase Invoice : {$result} <br><br>");
                       
            // Query cek jumlah gl utk purchase invoice        
            $ssql= "SELECT COUNT(fnid) FROM txgeneral_journal_hdr a 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.ftsource LIKE 'pembelian_stock_masuk%' AND a.fdtrans_date >= '{$startDate}'";
            $result = RetRstField($ssql);        
            echo("- Total Jurnal Purchase Invoice : {$result} <br><br>");
                      
            // Query cek no document purchase invoice yg double di jurnal 
            $ssql= "SELECT a.ftpurchase_invoice_no, b.`ftjournal_no`, b.`ftdocument_no`, COUNT(a.ftpurchase_invoice_no)
                    FROM txpurchase_invoice_hdr a INNER JOIN txgeneral_journal_hdr b ON a.ftpurchase_invoice_no = b.`ftdocument_no` AND b.fnstatus != 9 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9  AND a.fdtrans_date >= '{$startDate}' AND b.ftsource LIKE 'pembelian_stock_masuk%' 
                    GROUP BY a.ftpurchase_invoice_no, b.ftsource
                    HAVING COUNT(a.ftpurchase_invoice_no) > 1
                    ORDER BY a.ftpurchase_invoice_no";
            echo("- Purchase Invoice DOUBLE JURNAL <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Invoice : {$r['ftpurchase_invoice_no']} , Jurnal : {$r['ftjournal_no']} <br>");
            }
            echo("<br>");
            
            // query cek invoice yg tdk ada di jurnal
            $ssql= "SELECT DISTINCT fdtrans_date,ftpurchase_invoice_no,fnstatus FROM txpurchase_invoice_hdr a
                    WHERE fnstatus != 3 AND fnstatus!= 9  AND a.fnsaldo_awal = 0  AND a.fdtrans_date >= '{$startDate}'  
                            AND ftpurchase_invoice_no NOT IN 
                    (SELECT ftdocument_no  FROM txgeneral_journal_hdr a
                    WHERE a.fnstatus = 1 AND ftsource LIKE 'pembelian_stock_masuk%'  )";
            echo("- Purchase Invoice NO JURNAL <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                $ajaxFixSalesInvoice = " window.open('tools.php?ajax=1&code=fix_purchase_invoice&ftpurchase_invoice_no={$r['ftpurchase_invoice_no']}','fix','height='+200+',width='+200+',top='+300+',left='+300+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
                echo("Invoice : {$r['ftpurchase_invoice_no']}&nbsp; <input type=\"button\" value=\"REPAIR\" onClick = \"{$ajaxFixPurchaseInvoice}\"><br>");
            }
            echo("<br>");
            
            // query cek jurnal yg tdk ada di invoice
            $ssql= "SELECT DISTINCT ftjournal_no, ftdocument_no,fnstatus FROM txgeneral_journal_hdr
                    WHERE fnstatus = 1 AND ftsource LIKE 'pembelian_stock_masuk%' AND ftdocument_no NOT IN 
                    (SELECT ftdocument_no  FROM txpurchase_invoice_hdr a
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fnsaldo_awal = 0 AND a.fdtrans_date >= '{$startDate}' )";
            echo("- Purchase Invoice Tidak ditemukan (Jurnal ADA) <br>");
                    
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Invoice : {$r['ftdocument_no']}, Jurnal : {$r['ftjournal_no']}<br>");
            }
            echo("<br>");
            
            // query cek harga stock masuk = harga pi
            $ssql= "SELECT a.ftpurchase_invoice_no,b.ftstock_in_no,b.ftitem_code,b.fcprice,b.fcprice*a.fcrate,(d.fcprice)
                    FROM txpurchase_invoice_hdr a 
                    INNER JOIN txpurchase_invoice_det b ON a.ftpurchase_invoice_no = b.ftpurchase_invoice_no 
                    LEFT JOIN txstock_in_hdr c ON c.ftdocument_no = a.ftpo_no AND ftcategory_code = 0
                    LEFT JOIN txstock_in_det d ON c.ftstock_in_no = d.ftstock_in_no AND b.ftitem_code = d.ftitem_code AND b.fnindex = d.fnindex
                    LEFT JOIN tlstock_hdr e ON b.ftitem_code = e.ftitem_code
                    WHERE  a.fnstatus = 1 AND c.fnstatus = 1 AND e.ftstock_category_code = 'inv'  AND b.fcprice * a.fcrate != d.fcprice ";
            echo("- Purchase Invoice Harga Tidak sama dengan Stock Masuk <br>");
                    
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Invoice : {$r['ftpurchase_invoice_no']}, SM : {$r['ftstock_in_no']}<br>");
            }
            echo("<br>");

            // query cek harga stock masuk = harga fifo
            $ssql= "SELECT a.ftdocument_no,b.ftstock_in_no,a.ftitem_code,a.fcnet_price,(c.fcprice)FROM txstockfifo a 
                    INNER JOIN txstock_in_hdr b ON a.ftdocument_no = b.ftstock_in_no AND ftcategory_code = 0
                    INNER JOIN txstock_in_det c ON c.ftstock_in_no = b.ftstock_in_no AND a.ftitem_code = c.ftitem_code
                    LEFT JOIN tlstock_hdr d ON c.ftitem_code = d.ftitem_code AND c.fnindex = a.fnindex
                    WHERE a.fnqty > 0 AND fcnet_price != fcprice AND d.ftstock_category_code = 'inv' AND a.fnstatus = 1 AND b.fnstatus = 1";
            echo("- Stock Masuk Harga Tidak sama dengan FIFO <br>");
                    
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("Document : {$r['ftdocument_no']}, SM : {$r['ftstock_in_no']}<br>");
            }
            echo("<br><br>");
            echo("*** SELESAI ***");

            die();
            break;
        
        case "check_jurnal_fifo_purchase_invoice":
            // Query cek jumlah purchase invoice    
            $ssql= "SELECT ROUND(SUM(total_jurnal),2) AS total_jurnal,ROUND(SUM(total_transaksi),2) AS total_transaksi,ftpurchase_invoice_no,ftjournal_no,ftstock_in_no 
                    FROM (	-- jurnal purchase invoice
                                SELECT SUM(b.fccredit) AS total_jurnal,'' AS total_transaksi,a.ftdocument_no AS ftpurchase_invoice_no,a.ftjournal_no AS ftjournal_no,'' AS ftstock_in_no 
                                FROM txgeneral_journal_hdr a INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no
                                WHERE a.ftsource LIKE 'pembelian_stock_masuk%' AND a.fnstatus !=3 AND a.fnstatus != 9 
                                        AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                GROUP BY a.ftjournal_no 

                                -- transaksi purchase invoice
                                UNION ALL
                                SELECT '' AS total_jurnal, (fcamount + fcdebit_cost) AS total_transaksi,ftpurchase_invoice_no,'' AS ftjournal_no,'' AS ftstock_in_no
                                FROM ( 
                                        SELECT (a.fcsub_amount*a.fcrate) + (a.fcvat*a.fcrate) AS fcamount, 
                                                IF(a.fcother_cost>0,a.fcother_cost*a.fcrate,'0') AS fcdebit_cost,
                                                a.ftpurchase_invoice_no
                                        FROM txpurchase_invoice_hdr a
                                        WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                        GROUP BY a.ftpurchase_invoice_no 
                                        ORDER BY a.ftpurchase_invoice_no) X 

                                -- fifo purchase invoice
                                /* UNION ALL
                                SELECT '' AS total_jurnal, SUM(ABS(c.fnqty)*fcnet_price) AS total_transaksi,a.ftpurchase_invoice_no,'' AS ftjournal_no,c.ftdocument_no AS ftstock_in_no
                                FROM txpurchase_invoice_hdr a INNER JOIN txstock_in_hdr b ON a.ftstock_in_no = b.ftstock_in_no
                                INNER JOIN TXStockFIFO c ON c.ftdocument_no = b.ftstock_in_no 
                                WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                        AND c.fnqty > 0 AND c.fttableref = 'txstock_in_det'
                                        AND b.fnstatus != 3 AND b.fnstatus != 9 AND c.fnstatus != 3 AND c.fnstatus != 9 
                                GROUP BY a.ftpurchase_invoice_no  */

                        ) Z	
                    GROUP BY ftpurchase_invoice_no
                    HAVING total_jurnal != total_transaksi
                    ORDER BY ftpurchase_invoice_no";
            
            $ajaxFixJurnalFifoPurchaseInvoice = " window.open('tools.php?ajax=1&code=fix_jurnal_fifo_purchase_invoice&Start_Date={$startDate}&End_Date={$endDate}','fix','height='+200+',width='+200+',top='+300+',left='+300+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
            echo("- Purchase Invoice & Fifo TIDAK SESUAI Jurnal &nbsp; <input type=\"button\" value=\"REPAIR\" onClick = \"{$ajaxFixJurnalFifoPurchaseInvoice}\"><br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            
            $varcounter = 0;
            while($r=mysql_fetch_assoc($rs)) {
                $varcounter = $varcounter + 1;    
                echo("Invoice : {$r['ftpurchase_invoice_no']} , Jurnal : {$r['ftjournal_no']} , Total Jurnal : {$r['total_jurnal']} , Total Transaksi : {$r['total_transaksi']} <br>");
            }
            
            echo("<br><br>");
            echo("*** TOTAL {$varcounter} PURCHASE INVOICE ***");
            
            die();
            break;
            
        case "fix_jurnal_fifo_purchase_invoice":
            
            // Get Purchase Invoice Yang Tidak Sesuai
            $ssql= "SELECT ROUND(SUM(total_jurnal),2) AS total_jurnal,ROUND(SUM(total_transaksi),2) AS total_transaksi,ftpurchase_invoice_no,ftjournal_no,ftstock_in_no 
                    FROM (	-- jurnal purchase invoice
                                SELECT SUM(b.fccredit) AS total_jurnal,'' AS total_transaksi,a.ftdocument_no AS ftpurchase_invoice_no,a.ftjournal_no AS ftjournal_no,'' AS ftstock_in_no 
                                FROM txgeneral_journal_hdr a INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no
                                WHERE a.ftsource LIKE 'pembelian_stock_masuk%' AND a.fnstatus !=3 AND a.fnstatus != 9 
                                        AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                GROUP BY a.ftjournal_no 

                                -- transaksi purchase invoice
                                UNION ALL
                                SELECT '' AS total_jurnal, (fcamount + fcdebit_cost) AS total_transaksi,ftpurchase_invoice_no,'' AS ftjournal_no,'' AS ftstock_in_no
                                FROM ( 
                                        SELECT (a.fcsub_amount*a.fcrate) + (a.fcvat*a.fcrate) AS fcamount, 
                                                IF(a.fcother_cost>0,a.fcother_cost*a.fcrate,'0') AS fcdebit_cost,
                                                a.ftpurchase_invoice_no
                                        FROM txpurchase_invoice_hdr a
                                        WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                        GROUP BY a.ftpurchase_invoice_no 
                                        ORDER BY a.ftpurchase_invoice_no) X 

                                -- fifo purchase invoice
                                /* UNION ALL
                                SELECT '' AS total_jurnal, SUM(ABS(c.fnqty)*fcnet_price) AS total_transaksi,a.ftpurchase_invoice_no,'' AS ftjournal_no,c.ftdocument_no AS ftstock_in_no
                                FROM txpurchase_invoice_hdr a INNER JOIN txstock_in_hdr b ON a.ftstock_in_no = b.ftstock_in_no
                                INNER JOIN TXStockFIFO c ON c.ftdocument_no = b.ftstock_in_no 
                                WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' AND a.fdtrans_date <= '{$endDate}'
                                        AND c.fnqty > 0 AND c.fttableref = 'txstock_in_det'
                                        AND b.fnstatus != 3 AND b.fnstatus != 9 AND c.fnstatus != 3 AND c.fnstatus != 9 
                                GROUP BY a.ftpurchase_invoice_no  */

                        ) Z	
                    GROUP BY ftpurchase_invoice_no
                    HAVING total_jurnal != total_transaksi
                    ORDER BY ftpurchase_invoice_no";
            $rs=mysql_query($ssql) or die($ssql);
                    
            $varcounter = 0;
            while($r=mysql_fetch_assoc($rs)) {
                ?>
                <script language='javascript'>
                    window.open('tools.php?ajax=1&code=fix_jurnal_fifo_purchase_invoice2&ftpurchase_invoice_no=<?php echo $r['ftpurchase_invoice_no']; ?>','result<?php echo $varcounter; ?>','height='+300+',width='+600+',top='+250+',left='+250+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen=1');
                </script>
                <?php
            
                $varcounter = $varcounter + 1;
                echo("*** INSERT {$r['ftpurchase_invoice_no']} ***");                
            }
            
            echo("*** PROSES {$varcounter} Jurnal SELESAI ***");

            die();
            break;
            
        case "fix_jurnal_fifo_purchase_invoice2":
            
            $ftjournal_no = RetRstField("SELECT ftjournal_no FROM txgeneral_journal_hdr WHERE fnstatus != 9 AND ftdocument_no = '{$_REQUEST['ftpurchase_invoice_no']}'");

            // Batal Jurnal Header
            $ssql2= "UPDATE txgeneral_journal_hdr SET fnstatus = 9 WHERE ftjournal_no = '{$ftjournal_no}'";
            $rs2=mysql_query($ssql2) or die($ssql2);

            // Get Purchase Invoice Hdr
            $ssql2= "SELECT ftpurchase_invoice_no, ROUND(SUM(fcsub_amount * fcrate),2) as fcsub_amount, ROUND(SUM(fcamount * fcrate),2) as fcamount,
                            ROUND(SUM(fcother_cost * fcrate),2) as fcother_cost, ROUND(SUM(fcvat * fcrate),2) as fcvat,
                            ROUND(SUM(fcdiscount * fcrate),2) as fcdiscount, fcrate, ftnotes, fdtrans_date, ftsupplier_code, ftstock_in_no
                     FROM txpurchase_invoice_hdr a 
                     WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.ftpurchase_invoice_no = '{$_REQUEST['ftpurchase_invoice_no']}'";
            $rs2=mysql_query($ssql2) or die($ssql2);

                
            while($r2=mysql_fetch_assoc($rs2)) {
                $_REQUEST['form'] = "pembelian_stock_masuk";
                $_REQUEST['mode'] = "simpan";
                $_REQUEST['fnstatus'] = 1;
                $_REQUEST['ftstock_in_no'] = $r2['ftstock_in_no'];
                $_REQUEST['fdtrans_date'] = CDateRev($r2['fdtrans_date']); 
                $_REQUEST['ftsupplier_code'] = $r2['ftsupplier_code'];
                $_REQUEST['ftnotes'] = $r2['ftnotes'];
                $_REQUEST['fcamount'] = $r2['fcamount'];
                $_REQUEST['fcrate'] = $r2['fcrate'];
                $_REQUEST['fcdiscount'] = $r2['fcdiscount'];
                $_REQUEST['fcsub_amount'] = $r2['fcsub_amount'];
                $_REQUEST["fcother_cost"] = $r2['fcother_cost'];
                $_REQUEST['fcvat'] = $r2['fcvat'];        
                
                $ftsupplier_name = RetRstField("SELECT ftsupplier_name FROM tlsupplier WHERE ftsupplier_code = '{$r2['ftsupplier_code']}'");
            
            }

            // Get Purchase Invoice Det
            $ssql2= "SELECT a.* FROM txpurchase_invoice_det a WHERE a.ftpurchase_invoice_no = '{$_REQUEST['ftpurchase_invoice_no']}' ORDER BY fnindex";
            $rs2=mysql_query($ssql2) or die($ssql2);

            $_REQUEST['fnindexD0'][] = "";
            while($r2=mysql_fetch_assoc($rs2)) {
                $_REQUEST['fnindexD0'][] = $r2['fnindex'];
                $_REQUEST['ftitem_codeD0'][$r2['fnindex']] = $r2['ftitem_code'];
                
                $_REQUEST['fcpriceD0'][$r2['fnindex']] = $r2['fcprice'];
                $_REQUEST['fnqty_baseD0'][$r2['fnindex']] = $r2['fnqty_base'];
            }

            // Call Function Process General Ledger
            if(function_exists("ProcessGL")) {
                $m = ProcessGL();
                Query_SqlArr($m,"1");
            }
            
            echo("*** FINISH INSERT {$r2['ftpurchase_invoice_no']} ***");                
            die();
            break;        
        
        // INVENTORY
        case "check_fifo_inventory":
            // INFO : QUERY STOCK MASUK normal sdh dicek di fifo pembelian
            
            // query cek stock adjustment tidak ada di fifo
            $ssql= "SELECT DISTINCT a.fdtrans_date,a.ftstock_adjustment_no,a.fnstatus,b.ftitem_code,b.fnqty,c.ftdocument_no,c.ftitem_code,c.fnqty 
                    FROM txstock_adjustment_hdr a INNER JOIN txstock_adjustment_det b ON a.ftstock_adjustment_no = b.ftstock_adjustment_no
                    LEFT JOIN txstockfifo c ON c.ftdocument_no = a.ftstock_adjustment_no AND b.ftitem_code = c.ftitem_code AND c.fnstatus = 1  
                    WHERE a.fnstatus = 2 AND b.fnqty != 0 AND c.ftitem_code IS NULL";
            echo("- STOCK MASUK - QTY FIFO TIDAK SESUAI <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No Adjustment : {$r['ftstock_adjustment_no']} <br>");
            }
            
            echo("<br>");
            
            // Query qty stock adjustment tdk sama di fifo
            $ssql= "SELECT DISTINCT a.ftstock_adjustment_no, COUNT(b.fnindex) AS tstock, COUNT(c.fnindex) AS tfifo 
                    FROM txstock_adjustment_hdr a INNER JOIN txstock_adjustment_det b ON a.ftstock_adjustment_no = b.ftstock_adjustment_no
                    LEFT JOIN txstockfifo c ON a.ftstock_adjustment_no = c.ftdocument_no  AND c.fnstatus = 1 AND c.fttableref = 'txstock_adjustment_det'
                    WHERE a.fnstatus = 2  
                    GROUP BY a.ftstock_adjustment_no
                    HAVING tstock != tfifo";
            echo("- STOCK ADJUSTMENT - FIFO TIDAK ADA <br>");
                    
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No Adjustment : {$r['ftstock_adjustment_no']}, Qty Stock : {$r['tstock']}, Qty FIFO : {$r['tstock']} <br>");
            }
           
            echo("<br>");
            
            // query cek item assembly hdr tdk tdk ada di fifo
            $ssql= "SELECT DISTINCT a.ftassembly_no, 1 AS tstock, COUNT(c.fnindex) AS tfifo 
                    FROM txassembly_hdr a 
                    LEFT JOIN txstockfifo c ON a.ftassembly_no = c.ftdocument_no
                            AND  c.fnstatus = 1 AND c.fttableref = 'txassembly_hdr'   AND a.ftitem_code = c.ftitem_code AND c.fnqty > 0  
                    WHERE a.fnstatus = 1
                    GROUP BY a.ftassembly_no
                    HAVING tstock != tfifo ";
            echo("- ASSEMBLY - QTY FIFO TIDAK SESUAI <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No Assembly : {$r['ftassembly_no']} <br>");
            }
            
            echo("<br>");
            
            // query cek and compare item assembly tdk sama di fifo
            $ssql= "SELECT DISTINCT a.fdtrans_date,a.ftassembly_no,a.fnstatus,a.ftitem_code,a.fnqty,c.ftdocument_no,c.ftitem_code,c.fnqty 
                    FROM txassembly_hdr a 
                    LEFT JOIN txstockfifo c ON c.ftdocument_no = a.ftassembly_no AND a.ftitem_code = c.ftitem_code AND c.fnstatus = 1 AND c.fnqty > 0  
                    WHERE a.fnstatus != 3 AND a.fnstatus!= 9 
                                    AND c.ftitem_code IS NULL";
            echo("- ASSEMBLY - FIFO TIDAK ADA <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No Assembly : {$r['ftassembly_no']} <br>");
            }
            
            echo("<br>");
        
            // query cek item assembly det tdk ada di fifo
            $ssql= "SELECT DISTINCT a.ftassembly_no, COUNT(b.fnindex) AS tstock, COUNT(c.fnindex) AS tfifo 
                    FROM txassembly_hdr a INNER JOIN txassembly_det b ON a.ftassembly_no = b.ftassembly_no
                    LEFT JOIN txstockfifo c ON a.ftassembly_no = c.ftdocument_no
                            AND  c.fnstatus = 1 AND c.fttableref = 'txassembly_hdr' AND b.ftitem_code = c.ftitem_code   
                    WHERE a.fnstatus = 1
                    GROUP BY a.ftassembly_no
                    HAVING tstock != tfifo	";
            echo("- ASSEMBLY DET - FIFO TIDAK SESUAI <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No Assembly : {$r['ftassembly_no']} <br>");
            }
            
            echo("<br>");
            
            // query cek and compare item and qty assembly tdk sama di fifo
            $ssql= "SELECT DISTINCT a.fdtrans_date,a.ftassembly_no,a.fnstatus,b.ftitem_code,a.fnqty,c.ftdocument_no,c.ftitem_code,c.fnqty 
                    FROM txassembly_hdr a INNER JOIN txassembly_det b ON a.ftassembly_no = b.ftassembly_no
                    LEFT JOIN txstockfifo c ON c.ftdocument_no = a.ftassembly_no AND b.ftitem_code = c.ftitem_code	
                            AND c.fnstatus = 1 AND c.fttableref = 'txassembly_hdr'  
                    WHERE a.fnstatus != 3 AND a.fnstatus!= 9 AND c.ftitem_code IS NULL";
            echo("- ASSEMBLY DET - FIFO TIDAK SAMA <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No Assembly : {$r['ftassembly_no']} <br>");
            }
            
            echo("<br>");
            
            // query cek item disassembly tdk ada di fifo
            $ssql= "SELECT DISTINCT a.ftdisassembly_no, 1 AS tstock, COUNT(c.fnindex) AS tfifo 
                    FROM txdisassembly_hdr a 
                    LEFT JOIN txstockfifo c ON a.ftdisassembly_no = c.ftdocument_no
                            AND  c.fnstatus = 1 AND c.fttableref = 'txdisassembly_hdr' AND a.ftitem_code = c.ftitem_code AND c.fnqty < 0  
                    WHERE a.fnstatus = 1
                    GROUP BY a.ftdisassembly_no
                    HAVING tstock != tfifo AND tfifo < 1 ";
            echo("- DISASSEMBLY HDR - FIFO TIDAK ADA <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No Disassembly : {$r['ftdisassembly_no']} <br>");
            }
            
            echo("<br>");
            
            // query cek and compare item and qty disassembly tdk sama di fifo
            $ssql= "SELECT DISTINCT a.fdtrans_date,a.ftdisassembly_no,a.fnstatus,a.ftitem_code,a.fnqty,c.ftdocument_no,c.ftitem_code,c.fnqty 
                    FROM txdisassembly_hdr a 
                    LEFT JOIN txstockfifo c ON c.ftdocument_no = a.ftdisassembly_no AND a.ftitem_code = c.ftitem_code AND c.fnstatus = 1 AND c.fnqty < 0  
                    WHERE a.fnstatus != 3 AND a.fnstatus!= 9 AND c.ftitem_code IS NULL";
            echo("- DISASSEMBLY HDR - FIFO TIDAK SAMA <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No Disassembly : {$r['ftdisassembly_no']} <br>");
            }
            
            echo("<br>");
            
            // query cek item disassembly det tdk ada di fifo
            $ssql= "SELECT DISTINCT a.ftdisassembly_no, COUNT(b.fnindex) AS tstock, COUNT(c.fnindex) AS tfifo 
                    FROM txdisassembly_hdr a INNER JOIN txdisassembly_det b ON a.ftdisassembly_no = b.ftdisassembly_no
                    LEFT JOIN txstockfifo c ON a.ftdisassembly_no = c.ftdocument_no
                            AND  c.fnstatus = 1 AND c.fttableref = 'txdisassembly_hdr' AND b.ftitem_code = c.ftitem_code 
                    WHERE a.fnstatus = 1
                    GROUP BY a.ftdisassembly_no
                    HAVING tstock != tfifo	";
            echo("- DISASSEMBLY HDR - FIFO TIDAK ADA <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No Disassembly : {$r['ftdisassembly_no']} <br>");
            }
            
            echo("<br>");
            
            // query cek and compare item and qty assembly tdk sama di fifo
            $ssql= "SELECT DISTINCT a.fdtrans_date,a.ftdisassembly_no,a.fnstatus,b.ftitem_code,a.fnqty,c.ftdocument_no,c.ftitem_code,c.fnqty 
                    FROM txdisassembly_hdr a INNER JOIN txdisassembly_det b ON a.ftdisassembly_no = b.ftdisassembly_no
                    LEFT JOIN txstockfifo c ON c.ftdocument_no = a.ftdisassembly_no AND b.ftitem_code = c.ftitem_code	
                            AND c.fnstatus = 1 AND c.fttableref = 'txdisassembly_hdr'  
                    WHERE a.fnstatus != 3 AND a.fnstatus!= 9 AND c.ftitem_code IS NULL";
            echo("- DISASSEMBLY HDR - FIFO TIDAK SAMA <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No Disassembly : {$r['ftdisassembly_no']} <br>");
            }
            
            echo("<br>");
            
            // query cek stock keluar tdk tdk ada di fifo
            $ssql= "SELECT DISTINCT a.fdtrans_date,a.ftstock_out_no,a.fnstatus,c.ftdocument_no,c.fnstatus 
                    FROM txstock_out_hdr a INNER JOIN txstock_out_det b ON a.ftstock_out_no = b.ftstock_out_no
                    INNER JOIN tlstock_hdr d ON b.ftitem_code = d.ftitem_code AND d.ftstock_category_code = 'inv'	
                    LEFT JOIN txstockfifo c ON c.ftdocument_no = a.ftstock_out_no AND c.fnstatus = 1	
                    WHERE a.fnstatus = 1 AND c.ftdocument_no IS NULL ";
            echo("- STOCK KELUAR - FIFO TIDAK ADA <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No SK : {$r['ftstock_out_no']} <br>");
            }
            
            echo("<br>");
            
            // query cek item sk tdk ada di fifo (kecuali mutasi dan adjustment) 
            $ssql= "SELECT DISTINCT fttrans_no, ROUND(SUM(tstock),2) AS tstock, ROUND(SUM(tfifo),2) AS tfifo
                    FROM (
                            SELECT DISTINCT a.ftstock_out_no AS fttrans_no,SUM(b.fnqty) AS tstock , 0 AS tfifo
                            FROM txstock_out_hdr a INNER JOIN txstock_out_det b ON a.ftstock_out_no = b.ftstock_out_no
                            INNER JOIN tlstock_hdr c ON b.ftitem_code = c.ftitem_code AND c.ftstock_category_code = 'inv'
                            WHERE a.fnstatus = 1 
                            GROUP BY a.ftstock_out_no
                            UNION ALL 
                            SELECT DISTINCT a.ftdocument_no AS fttrans_no,0 AS tstock, SUM(a.fnqty) * -1 AS tfifo 
                            FROM txstockfifo a 
                            WHERE a.fnstatus = 1 AND fnqty < 0  
                            GROUP BY a.ftdocument_no
                    ) X 
                    WHERE fttrans_no NOT LIKE 'MS%' AND fttrans_no NOT LIKE 'PS%'
                    GROUP BY fttrans_no
                    HAVING tstock != tfifo";
            echo("- STOCK KELUAR (KECUALI MS DAN ADJUSTMENT) - FIFO TIDAK ADA <br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No SK : {$r['fttrans_no']} <br>");
            }
            
            echo("<br><br>");
            echo("*** SELESAI ***");
            
            die();
            break;
            
        case "check_inventory":
            // Query cek jumlah stock masuk (kecuali retur, pembelian, assembly, dissasembly)   
            $ssql= "SELECT count(fnid) FROM txstock_in_hdr a 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.ftcategory_code != 0 AND a.ftcategory_code != 4 AND a.ftcategory_code != 5 AND a.ftcategory_code != 11 
                            AND a.fdtrans_date >= '{$startDate}'";
            $result = RetRstField($ssql);        
            echo("- Total Transaksi Stock Masuk : {$result} <br><br>");
            echo("<br>");
                       
            // Query cek jumlah gl utk stock masuk        
            $ssql= "SELECT COUNT(fnid) FROM txgeneral_journal_hdr a 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.ftsource LIKE 'barang_masuk%' AND a.fdtrans_date >= '{$startDate}'"; 
            $result = RetRstField($ssql);        
            echo("- Total Jurnal Stock Masuk : {$result} <br><br>");
            echo("<br>");

            // Query cek stock masuk tdk di jurnal        
            $ssql= "SELECT * FROM txstock_in_hdr a 
                    WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.ftcategory_code != 0 AND a.ftcategory_code != 4 AND a.ftcategory_code != 5 AND a.ftcategory_code != 11 
                            AND a.fdtrans_date >= '{$startDate}'  
                            AND a.ftstock_in_no NOT IN     
                                (SELECT ftdocument_no FROM txgeneral_journal_hdr a 
                                 WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.ftsource LIKE 'barang_masuk%' AND a.fdtrans_date >= '{$startDate}') ";
            $rs=mysql_query($ssql) or die($ssql);
            while($r=mysql_fetch_assoc($rs)) {
                echo("No SM : {$r['ftstock_in_no']} <br>");
            }
            
            echo("<br><br>");
            echo("*** SELESAI ***");
            
            die();
            break;     
            
        // Akunting
        case "check_penyusutan":
                  
            // Query cek jumlah purchase invoice    
            $ssql= "SELECT ROUND(SUM(total_jurnal),2) AS total_jurnal,ROUND(SUM(total_transaksi),2) AS total_transaksi,fttrans_code,ftjournal_no 
                    FROM (	-- jurnal penyusutan
                                SELECT SUM(b.fccredit) AS total_jurnal,'' AS total_transaksi,a.ftdocument_no AS fttrans_code,a.ftjournal_no AS ftjournal_no,'' AS ftstock_in_no 
                                FROM txgeneral_journal_hdr a INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no
                                WHERE a.ftsource LIKE 'penyusutan_asset%' AND a.fnstatus !=3 AND a.fnstatus != 9 
                                        AND a.fdtrans_date >= '{$startDate}' 
                                GROUP BY a.ftjournal_no 
 
                                -- transaksi penyusutan
                                UNION ALL
                                SELECT '' AS total_jurnal, fcmonthly_amount AS total_transaksi,fttrans_code,'' AS ftjournal_no,'' AS ftstock_in_no
                                FROM ( 
                                        SELECT SUM(b.fcmonthly_amount) AS fcmonthly_amount,a.fttrans_code
                                        FROM txdepriciation_hdr a inner join txdepriciation_det b ON a.fttrans_code = b.fttrans_code
                                        WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' 
                                        GROUP BY a.fttrans_code 
                                        ORDER BY a.fttrans_code) X 

                        ) Z	
                    GROUP BY fttrans_code
                    HAVING total_jurnal != total_transaksi
                    ORDER BY fttrans_code";
      
            $ajaxFixJurnalPenyusutan = " window.open('tools.php?ajax=1&code=fix_jurnal_penyusutan&Start_Date={$startDate}&End_Date={$endDate}','fix','height='+200+',width='+200+',top='+300+',left='+300+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
            echo("- Penyusutan TIDAK SESUAI Jurnal &nbsp; <input type=\"button\" value=\"REPAIR\" onClick = \"{$ajaxFixJurnalPenyusutan}\"><br>");
            
            $rs=mysql_query($ssql) or die($ssql);
            
            $varcounter = 0;
            while($r=mysql_fetch_assoc($rs)) {
                $varcounter = $varcounter + 1;    
                echo("Penyusutan : {$r['fttrans_code']} , Jurnal : {$r['ftjournal_no']} , Total Jurnal : {$r['total_jurnal']} , Total Transaksi : {$r['total_transaksi']} <br>");
            }
            
            echo("<br><br>");
            echo("*** TOTAL {$varcounter} PENYUSUTAN ***");
            
            die();
            break;
            
        case "fix_jurnal_penyusutan":
         
            // Query cek jumlah purchase invoice    
            $ssql= "SELECT ROUND(SUM(total_jurnal),2) AS total_jurnal,ROUND(SUM(total_transaksi),2) AS total_transaksi,fttrans_code,ftjournal_no 
                    FROM (	-- jurnal penyusutan
                                SELECT SUM(b.fccredit) AS total_jurnal,'' AS total_transaksi,a.ftdocument_no AS fttrans_code,a.ftjournal_no AS ftjournal_no,'' AS ftstock_in_no 
                                FROM txgeneral_journal_hdr a INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no
                                WHERE a.ftsource LIKE 'penyusutan_asset%' AND a.fnstatus !=3 AND a.fnstatus != 9 
                                        AND a.fdtrans_date >= '{$startDate}' 
                                GROUP BY a.ftjournal_no 
 
                                -- transaksi penyusutan
                                UNION ALL
                                SELECT '' AS total_jurnal, fcmonthly_amount AS total_transaksi,fttrans_code,'' AS ftjournal_no,'' AS ftstock_in_no
                                FROM ( 
                                        SELECT SUM(b.fcmonthly_amount) AS fcmonthly_amount,a.fttrans_code
                                        FROM txdepriciation_hdr a inner join txdepriciation_det b ON a.fttrans_code = b.fttrans_code
                                        WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fdtrans_date >= '{$startDate}' 
                                        GROUP BY a.fttrans_code 
                                        ORDER BY a.fttrans_code) X 

                        ) Z	
                    GROUP BY fttrans_code
                    HAVING total_jurnal != total_transaksi
                    ORDER BY fttrans_code";
            $rs=mysql_query($ssql) or die($ssql);
              
            $varcounter = 0;
            while($r=mysql_fetch_assoc($rs)) {
                ?>
                <script language='javascript'>
                    window.open('tools.php?ajax=1&code=fix_jurnal_penyusutan2&fttrans_code=<?php echo $r['fttrans_code']; ?>','result<?php echo $varcounter; ?>','height='+300+',width='+600+',top='+250+',left='+250+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen=1');
                </script>
                <?php
            
                $varcounter = $varcounter + 1;
                echo("*** INSERT {$r['fttrans_code']} ***");                
            }
            
            echo("*** PROSES {$varcounter} Jurnal SELESAI ***");

            die();
            break;
            
        case "fix_jurnal_penyusutan2":
            
            $ftjournal_no = RetRstField("SELECT ftjournal_no FROM txgeneral_journal_hdr WHERE fnstatus != 9 AND ftdocument_no = '{$_REQUEST['fttrans_code']}'");

            // Batal Jurnal Header
            $ssql2= "UPDATE txgeneral_journal_hdr SET fnstatus = 9 WHERE ftjournal_no = '{$ftjournal_no}'";
            $rs2=mysql_query($ssql2) or die($ssql2);

            // Get Penyusutan Hdr
            $ssql2= "SELECT fttrans_code, fdtrans_date
                     FROM txdepriciation_hdr a 
                     WHERE a.fnstatus != 3 AND a.fnstatus != 9 AND a.fttrans_code = '{$_REQUEST['fttrans_code']}'";
            $rs2=mysql_query($ssql2) or die($ssql2);
                
            while($r2=mysql_fetch_assoc($rs2)) {
                $_REQUEST['form'] = "penyusutan_asset";
                $_REQUEST['mode'] = "simpan";
                $_REQUEST['fnstatus'] = 1;
                $_REQUEST['fttrans_code'] = $r2['fttrans_code'];
                $_REQUEST['fdtrans_date'] = CDateRev($r2['fdtrans_date']); 
            }

            // Get Penyusutan Det
            $ssql2= "SELECT a.* FROM txdepriciation_det a WHERE a.fttrans_code = '{$_REQUEST['fttrans_code']}' ORDER BY fnindex";
            $rs2=mysql_query($ssql2) or die($ssql2);

            $_REQUEST['fnindexD0'][] = "";
            while($r2=mysql_fetch_assoc($rs2)) {
                $_REQUEST['fnindexD0'][] = $r2['fnindex'];
                $_REQUEST['ftasset_codeD0'][$r2['fnindex']] = $r2['ftasset_code'];
                $_REQUEST['fcmonthly_amountD0'][$r2['fnindex']] = $r2['fcmonthly_amount'];
            }

            // Call Function Process General Ledger
            if(function_exists("ProcessGL")) {
                $m = ProcessGL();
                Query_SqlArr($m,"1");
            }
            
            echo("*** FINISH INSERT {$r2['fttrans_code']} ***");                
            die();
            break;        
            
               
    }
}

$ajaxUpdateStock = " window.open('tools.php?ajax=1&code=update_stock_det','tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
$ajaxCheckFifoOutstanding = " window.open('tools.php?ajax=1&code=check_fifo_outstanding','tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";

// Penjualan
$ajaxCheckFifoPenjualan = " window.open('tools.php?ajax=1&code=check_fifo_penjualan','tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
$ajaxCheckSalesInvoice = " window.open('tools.php?ajax=1&code=check_sales_invoice&Start_Date='+document.getElementById('Start_Date').value,'tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
$ajaxCheckJurnalFifoSalesInvoice = " window.open('tools.php?ajax=1&code=check_jurnal_fifo_sales_invoice&Start_Date='+document.getElementById('Start_Date').value+'&End_Date='+document.getElementById('End_Date').value,'tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
$ajaxCheckFifoSalesRetur = " window.open('tools.php?ajax=1&code=check_fifo_sales_retur','tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
$ajaxCheckSalesRetur = " window.open('tools.php?ajax=1&code=check_sales_retur&Start_Date='+document.getElementById('Start_Date').value,'tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
$ajaxCheckJurnalFifoSalesRetur = " window.open('tools.php?ajax=1&code=check_jurnal_fifo_sales_retur&Start_Date='+document.getElementById('Start_Date').value+'&End_Date='+document.getElementById('End_Date').value,'tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
$ajaxCheckSalesAdjustment = " window.open('tools.php?ajax=1&code=check_sales_adjustment&Start_Date='+document.getElementById('Start_Date').value,'tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
$ajaxCheckJurnalFifoSalesAdjustment = " window.open('tools.php?ajax=1&code=check_jurnal_fifo_sales_adjustment&Start_Date='+document.getElementById('Start_Date').value+'&End_Date='+document.getElementById('End_Date').value,'tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";

// Pembelian
$ajaxCheckFifoPembelian = " window.open('tools.php?ajax=1&code=check_fifo_pembelian','tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
$ajaxCheckPurchaseInvoice = " window.open('tools.php?ajax=1&code=check_purchase_invoice&Start_Date='+document.getElementById('Start_Date').value,'tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
$ajaxCheckJurnalFifoPurchaseInvoice = " window.open('tools.php?ajax=1&code=check_jurnal_fifo_purchase_invoice&Start_Date='+document.getElementById('Start_Date').value+'&End_Date='+document.getElementById('End_Date').value,'tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";

// Inventory
$ajaxCheckFifoInventory = " window.open('tools.php?ajax=1&code=check_fifo_inventory','tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";
$ajaxCheckInventory = " window.open('tools.php?ajax=1&code=check_inventory','tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";

// Akunting
$ajaxCheckPenyusutan = " window.open('tools.php?ajax=1&code=check_penyusutan&Start_Date='+document.getElementById('Start_Date').value,'tools','height='+300+',width='+500+',top='+200+',left='+200+',scrollbars=0,resizable=1,menubar=0,status=0,fullscreen  =1');";


?>        

<br>
<table cellspacing = "10" cellpadding = "10">
    <tr><td colspan = "3">SESSION BRANCH : <?php echo("{$_SESSION['strbranch']}") ?> --- SESSION USER : <?php echo($_SESSION['username']) ?></td></tr>
    <tr><td>START DATE : <input id="Start_Date" name="Start_Date" type = "text" VALUE = "2019-01-01" SIZE="10"></td>
        <td colspan = "2">END DATE : <input id="End_Date" name="End_Date" type = "text" VALUE = "2019-12-31" SIZE = "10"></td></tr>
    
    <tr><td>UPDATE QTY STOCK DETAIL = KARTU STOCK</td><td width="50"></td>
        <td><input type="button" value="UPDATE STOCK DETAIL" onClick = "<?php echo("{$ajaxUpdateStock}") ?>"></td>
        <td><div id="stock_detail"></div></td></tr>
    
    <tr><td>CEK FIFO - QTY OUTSTANDING </td><td width="50"></td>
        <td><input type="button" value="CHECK FIFO OUTSTANDING" onClick = "<?php echo("{$ajaxCheckFifoOutstanding}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>---------------------------------------------------------------- </td><td width="50"></td>
        <td></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>CEK FIFO - TRANSAKSI PENJUALAN TIDAK SESUAI </td><td width="50"></td>
        <td><input type="button" value="CHECK FIFO PENJUALAN" onClick = "<?php echo("{$ajaxCheckFifoPenjualan}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>CEK SALES INVOICE - JURNAL TIDAK ADA</td><td width="50"></td>
        <td><input type="button" value="CHECK JURNAL SALES INVOICE" onClick = "<?php echo("{$ajaxCheckSalesInvoice}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>CEK SALES INVOICE - FIFO - JURNAL TIDAK SESUAI</td><td width="50"></td>
        <td><input type="button" value="CHECK JURNAL FIFO SALES INVOICE" onClick = "<?php echo("{$ajaxCheckJurnalFifoSalesInvoice}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>CEK FIFO - TRANSAKSI SALES RETUR TIDAK SESUAI </td><td width="50"></td>
        <td><input type="button" value="CHECK FIFO SALES RETUR" onClick = "<?php echo("{$ajaxCheckFifoSalesRetur}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>CEK SALES RETUR - JURNAL TIDAK ADA</td><td width="50"></td>
        <td><input type="button" value="CHECK JURNAL SALES RETUR" onClick = "<?php echo("{$ajaxCheckSalesRetur}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>CEK SALES RETUR - FIFO - JURNAL TIDAK SESUAI</td><td width="50"></td>
        <td><input type="button" value="CHECK JURNAL FIFO SALES RETUR" onClick = "<?php echo("{$ajaxCheckJurnalFifoSalesRetur}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>CEK SALES ADJUSTMENT - JURNAL TIDAK ADA</td><td width="50"></td>
        <td><input type="button" value="CHECK JURNAL SALES ADJUSTMENT" onClick = "<?php echo("{$ajaxCheckSalesAdjustment}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>CEK SALES ADJUSTMENT - JURNAL TIDAK SESUAI</td><td width="50"></td>
        <td><input type="button" value="CHECK JURNAL FIFO SALES ADJUSTMENT" onClick = "<?php echo("{$ajaxCheckJurnalFifoSalesAdjustment}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>---------------------------------------------------------------- </td><td width="50"></td>
        <td></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>CEK FIFO - TRANSAKSI PEMBELIAN TIDAK SESUAI </td><td width="50"></td>
        <td><input type="button" value="CHECK FIFO PEMBELIAN" onClick = "<?php echo("{$ajaxCheckFifoPembelian}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>CEK PURCHASE INVOICE - JURNAL TIDAK ADA</td><td width="50"></td>
        <td><input type="button" value="CHECK JURNAL PURCHASE INVOICE" onClick = "<?php echo("{$ajaxCheckPurchaseInvoice}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>CEK PURCHASE INVOICE - FIFO - JURNAL TIDAK SESUAI</td><td width="50"></td>
        <td><input type="button" value="CHECK JURNAL FIFO PURCHASE INVOICE" onClick = "<?php echo("{$ajaxCheckJurnalFifoPurchaseInvoice}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>---------------------------------------------------------------- </td><td width="50"></td>
        <td></td>
        <td><div id="sales_invoice"></div></td></tr>
     
    <tr><td>CEK FIFO - TRANSAKSI INVENTORY TIDAK SESUAI </td><td width="50"></td>
        <td><input type="button" value="CHECK FIFO INVENTORY" onClick = "<?php echo("{$ajaxCheckFifoInventory}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>CEK INVENTORY - JURNAL TIDAK ADA </td><td width="50"></td>
        <td><input type="button" value="CHECK JURNAL INVENTORY" onClick = "<?php echo("{$ajaxCheckInventory}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
     
    
    
    <tr><td>---------------------------------------------------------------- </td><td width="50"></td>
        <td></td>
        <td><div id="sales_invoice"></div></td></tr>
    
    <tr><td>CEK PENYUSUTAN - JURNAL TIDAK ADA </td><td width="50"></td>
        <td><input type="button" value="CHECK JURNAL PENYUSUTAN" onClick = "<?php echo("{$ajaxCheckPenyusutan}") ?>"></td>
        <td><div id="sales_invoice"></div></td></tr>
     
    
</table>        
        
        