<?php   // tools to update Jurnal System
        session_cache_expire(1440);
        session_start();
        session_name("insys");
        include "include/config.php";
        include "include/functions.php";
        include "class/UctVw.php";

        // PURCHASE  PENERIMAAN BARANG  HEADER
        $ftsource = "pembelian_stock_masuk";
        
        $ssql="SELECT fdtrans_date, ftpurchase_invoice_no AS ftdocument_no, '1' AS fnstatus, '0' AS fntype, round((fcamount * fcrate),2) as fcamount
                FROM txpurchase_invoice_hdr a
                WHERE (a.fnstatus = 1 OR a.fnstatus = 2) and fdtrans_date >= '2019-05-03' 
                ORDER BY a.fdtrans_date, a.ftpurchase_invoice_no";

        $rs=mysql_query($ssql);
        $ssql1=array();

        
        pr("*** INSERTING PENERIMAAN BARANG  TO JURNAL SYSTEM HDR ***");

        while($r=mysql_fetch_assoc($rs)) {
            mysql_query("BEGIN");
            $ftjournal_no=getAutoNumber($_SESSION['strbranch'],"acc001","txgeneral_journal_hdr","ftjournal_no",CDateRev($r['fdtrans_date']));

            $ssql="INSERT INTO txgeneral_journal_hdr(ftbranch_code,ftjournal_no,fdtrans_date,ftdocument_no, fcdebit, fccredit,
                        fnstatus,fntype,ftsource,ftcreated_user,fdcreated_date,ftmodified_user,fdmodified_date)
                        SELECT 'PUSAT' AS ftbranch_code, '{$ftjournal_no}' AS ftjournal_no, '{$r['fdtrans_date']}' as fdtrans_date,
                        '{$r['ftdocument_no']}' AS ftdocument_no, {$r['fcamount']} AS fcdebit, {$r['fcamount']} AS fccredit,
                        '1' AS fnstatus, '0' AS fntype,
                        '{$ftsource}' AS ftsource, 'Admin' AS ftcreated_user, now() AS fdcreated_date,
                        'Admin' AS ftmodified_user,  now() AS fdmodified_date; ";

            $strSQL["insert txgeneral_journal_hdr"]=$ssql;
            mysql_query($ssql);
            //pr($ssql);
            mysql_query("COMMIT");


        }
        
        // PURCHASE INVOICE DETAIL
        pr("*** INSERTING PENERIMAAN BARANG  TO JURNAL SYSTEM DET ***");

        $ssql="INSERT INTO txgeneral_journal_det(ftbranch_code,ftjournal_no,fnindex,fdtrans_date,
                    ftcoa_code,ftdescription,fcdebit,fccredit,ftcreated_user,fdcreated_date,ftmodified_user,fdmodified_date)
                    SELECT * FROM (
                    -- hutang (credit) 
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,'1' AS fnindex, a.fdtrans_date,
                    (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F01') AS ftcoa_code,
                    CONCAT(IFNULL(a.ftsupplier_name,''),' - ',IFNULL(a.ftnotes,'')) AS ftdescription,
                    '0' AS fcdebit, a.fcamount*a.fcrate AS fccredit, 'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txpurchase_invoice_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftpurchase_invoice_no = c.ftdocument_no AND c.ftsource='{$ftsource}' AND c.fnstatus = 1
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2)
                    
                    UNION ALL
                    -- discount (credit)
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,'2' AS fnindex, a.fdtrans_date,
                    (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P02') AS ftcoa_code,
                    CONCAT(IFNULL(a.ftsupplier_name,''),' - ',IFNULL(a.ftnotes,'')) AS ftdescription,
                    '0'AS fcdebit, a.fcdiscount*a.fcrate AS fccredit,
                    'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txpurchase_invoice_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftpurchase_invoice_no = c.ftdocument_no AND c.ftsource='{$ftsource}' AND c.fnstatus = 1
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2) AND a.fcdiscount > 0
                    
                    UNION ALL
                    -- biaya lain (debit/credit)
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,'3' AS fnindex, a.fdtrans_date,
                    IF(a.fcother_cost>0,(SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P07'),(SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P08')) AS ftcoa_code,
                    CONCAT(IFNULL(a.ftsupplier_name,''),' - ',IFNULL(a.ftnotes,'')) AS ftdescription,
                    IF(a.fcother_cost>0,a.fcother_cost*a.fcrate,'0') AS fcdebit, IF(a.fcother_cost<0,ABS(a.fcother_cost*a.fcrate),'0') AS fccredit,
                    'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txpurchase_invoice_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftpurchase_invoice_no = c.ftdocument_no AND c.ftsource='{$ftsource}' AND c.fnstatus = 1
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2) AND a.fcother_cost > 0
                    
                    UNION ALL
                    -- pajak (debit)    
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,'4' AS fnindex, a.fdtrans_date,
                    (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P03') AS ftcoa_code,
                    CONCAT(IFNULL(a.ftsupplier_name,''),' - ',IFNULL(a.ftnotes,'')) AS ftdescription,
                    a.fcvat*a.fcrate AS fcdebit, '0' AS fccredit,
                    'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txpurchase_invoice_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftpurchase_invoice_no = c.ftdocument_no AND c.ftsource='{$ftsource}' AND c.fnstatus = 1
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2) AND a.fcvat > 0
                    
                    UNION ALL
                    -- persediaan (debit)
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,b.fnindex + 4 AS fnindex, a.fdtrans_date,
                    -- (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P01') AS ftcoa_code,
                    CASE WHEN (d.ftstock_category_code = 'inv') THEN d.ftgl_inv_acc ELSE d.ftgl_cogs_acc END AS ftcoa_code,
                    CONCAT(b.ftitem_code,' - ',IFNULL(a.ftsupplier_name,''),' - ',IFNULL(a.ftnotes,'')) AS ftdescription,
                    b.fcamount*a.fcrate AS fcdebit, '0' AS fccredit,
                    'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txpurchase_invoice_hdr a INNER JOIN txpurchase_invoice_det b ON a.ftpurchase_invoice_no = b.ftpurchase_invoice_no 
                    INNER JOIN txgeneral_journal_hdr c ON a.ftpurchase_invoice_no = c.ftdocument_no AND c.ftsource='{$ftsource}' AND c.fnstatus = 1
                    LEFT JOIN tlstock_hdr d ON b.ftitem_code = d.ftitem_code    
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2)
                    
                ) X
                ORDER BY fdtrans_date, ftjournal_no, fnindex";

        pr($ssql);
        mysql_query("BEGIN");
        $rs=mysql_query($ssql);
        mysql_query("COMMIT");

        pr("*** SELESAI PENERIMAAN BARANG ***");
        
        /*    
        // INSERT PURCHASE INVOICE COGS BARANG JADI DETAIL
        pr("*** INSERTING PURCHASE INVOICE COGS - BARANG JADI TO JURNAL SYSTEM DET ***");

        $ssql="INSERT INTO txgeneral_journal_det(ftbranch_code,ftjournal_no,fnindex,fdtrans_date,
                    ftcoa_code,ftdescription,fcdebit,fccredit,ftcreated_user,fdcreated_date,ftmodified_user,fdmodified_date)
                    SELECT * FROM (
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,'6' AS fnindex, a.fdtrans_date,
                    (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I01') AS ftcoa_code,
                    CONCAT(a.ftpurchase_invoice_no,' - ',IFNULL(a.ftsupplier_name,''),' - ',IFNULL(a.ftnotes,'')) AS ftdescription,
                    ifnull(sum(e.fcNet_Price*ABS(e.fnqty)),0) AS fcdebit, '0' AS fccredit,
                    'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txpurchase_invoice_hdr a
                    INNER JOIN txpurchase_invoice_det b ON a.ftpurchase_invoice_no = b.ftpurchase_invoice_no
                    INNER JOIN txstock_in_hdr f ON a.ftstock_in_no = f.ftstock_in_no AND f.ftstock_type_code = 'inv'
                    INNER JOIN txgeneral_journal_hdr c
                    ON a.ftpurchase_invoice_no = c.ftdocument_no AND c.fnstatus = 1
                    INNER JOIN tlstock_hdr d ON b.ftitem_code = d.ftitem_code
                    LEFT JOIN TXStockFIFO e on e.ftDocument_No=a.ftpurchase_invoice_no and e.ftItem_Code = b.ftItem_Code
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2)
                    GROUP BY a.fdtrans_date, a.ftpurchase_invoice_no

                    UNION ALL
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,'7' AS fnindex, a.fdtrans_date,
                    (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P01') AS ftcoa_code,
                    CONCAT(a.ftpurchase_invoice_no,' - ',IFNULL(a.ftsupplier_name,''),' - ',IFNULL(a.ftnotes,'')) AS ftdescription,
                    '0' AS fcdebit, ifnull(sum(e.fcNet_Price*ABS(e.fnqty)),0) AS fccredit,
                    'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txpurchase_invoice_hdr a
                    INNER JOIN txpurchase_invoice_det b ON a.ftpurchase_invoice_no = b.ftpurchase_invoice_no
                    INNER JOIN txstock_in_hdr f ON a.ftstock_in_no = f.ftstock_in_no AND f.ftstock_type_code = 'inv'
                    INNER JOIN txgeneral_journal_hdr c
                    ON a.ftpurchase_invoice_no = c.ftdocument_no AND c.fnstatus = 1
                    INNER JOIN tlstock_hdr d ON b.ftitem_code = d.ftitem_code
                    LEFT JOIN TXStockFIFO e on e.ftDocument_No=a.ftpurchase_invoice_no and e.ftItem_Code = b.ftItem_Code
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2)
                    GROUP BY a.fdtrans_date, a.ftpurchase_invoice_no

                ) X
                ORDER BY fdtrans_date, ftjournal_no, fnindex";

        pr($ssql);
        mysql_query("BEGIN");
        $rs=mysql_query($ssql);
        mysql_query("COMMIT");

        pr("*** UPDATE TO JURNAL SYSTEM HDR ***");

        // UPDATE COGS PURCHASE INVOICE HEADER
        $ssql="SELECT a.ftjournal_no, a.ftsource, a.ftdocument_no, SUM(b.fcdebit) AS fcamount
                FROM txgeneral_journal_hdr a
                INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no
                WHERE a.ftsource LIKE '%purchase_invoice%' AND a.fnstatus = 1
                GROUP BY a.ftjournal_no
                ORDER BY a.ftjournal_no";

        $rs=mysql_query($ssql);
        $ssql1=array();

        while($r=mysql_fetch_assoc($rs)) {
            mysql_query("BEGIN");

            $ssql="UPDATE txgeneral_journal_hdr
                    SET fcdebit = '{$r['fcamount']}', fccredit = '{$r['fcamount']}'
                    WHERE ftjournal_no = '{$r['ftjournal_no']}'  ";

            mysql_query($ssql);
            //pr($ssql);
            mysql_query("COMMIT");


        }
        */
        /*
        // PURCHASE RETUR HEADER
        $ssql="SELECT fdtrans_date, ftpurchase_return_no AS ftdocument_no, '1' AS fnstatus, '0' AS fntype,
                if(a.fnpo_type=0,'return_pembelian_local','return_pembelian_import') AS ftsource,
                fcamount, fcrate
                FROM txpurchase_return_hdr a
                WHERE (a.fnstatus = 1 OR a.fnstatus = 2)
                ORDER BY a.fdtrans_date, a.ftpurchase_return_no";

        $rs=mysql_query($ssql);
        $ssql1=array();

        pr("*** INSERTING PURCHASE RETURN TO JURNAL SYSTEM HDR ***");

        while($r=mysql_fetch_assoc($rs)) {
            mysql_query("BEGIN");
            $ftjournal_no=getAutoNumber("acc001","txgeneral_journal_hdr","ftjournal_no",CDateRev($r['fdtrans_date']));

            $ssql="INSERT INTO txgeneral_journal_hdr(ftbranch_code,ftjournal_no,fdtrans_date,ftdocument_no, fcdebit, fccredit,
                        fnstatus,fntype,ftsource,ftcreated_user,fdcreated_date,ftmodified_user,fdmodified_date)
                        SELECT 'PUSAT' AS ftbranch_code, '{$ftjournal_no}' AS ftjournal_no, '{$r['fdtrans_date']}' as fdtrans_date,
                        '{$r['ftdocument_no']}' AS ftdocument_no, {$r['fcamount']}*{$r['fcrate']} as fcdebit, {$r['fcamount']}*{$r['fcrate']}  as fccredit,
                        '1' AS fnstatus, '0' AS fntype,
                        '{$r['ftsource']}' AS ftsource, 'Admin' AS ftcreated_user, now() AS fdcreated_date,
                        'Admin' AS ftmodified_user,  now() AS fdmodified_date; ";

            $strSQL["insert txgeneral_journal_hdr"]=$ssql;
            mysql_query($ssql);
            //pr($ssql);
            mysql_query("COMMIT");


        }

        // PURCHASE RETURN DETAIL
        pr("*** INSERTING PURCHASE RETURN TO JURNAL SYSTEM DET ***");

        $ssql="INSERT INTO txgeneral_journal_det(ftbranch_code,ftjournal_no,fnindex,fdtrans_date,
                    ftcoa_code,ftdescription,fcdebit,fccredit,ftcreated_user,fdcreated_date,ftmodified_user,fdmodified_date)
                    SELECT * FROM (

                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,'1' AS fnindex, a.fdtrans_date,
                    (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F01') AS ftcoa_code,
                    CONCAT(a.ftpurchase_return_no,' - ',IFNULL(a.ftsupplier_code,''),' - ',IFNULL(a.ftnotes,'')) AS ftdescription,
                    a.fcamount*a.fcrate AS fcdebit, '0' AS fccredit, 'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txpurchase_return_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftpurchase_return_no = c.ftdocument_no AND c.fnstatus = 1
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2)

                    UNION ALL
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,'2' AS fnindex, a.fdtrans_date,
                    (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P02') AS ftcoa_code,
                    CONCAT(a.ftpurchase_return_no,' - ',IFNULL(a.ftsupplier_code,''),' - ',IFNULL(a.ftnotes,'')) AS ftdescription,
                    a.fcdiscount*a.fcrate AS fcdebit, '0' AS fccredit,
                    'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txpurchase_return_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftpurchase_return_no = c.ftdocument_no AND c.fnstatus = 1
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2) AND a.fcdiscount > 0

                    UNION ALL
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,'3' AS fnindex, a.fdtrans_date,
                    (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P06') AS ftcoa_code,
                    CONCAT(a.ftpurchase_return_no,' - ',IFNULL(a.ftsupplier_code,''),' - ',IFNULL(a.ftnotes,'')) AS ftdescription,
                    '0' AS fcdebit, a.fcsub_amount*a.fcrate AS fccredit,
                    'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txpurchase_return_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftpurchase_return_no = c.ftdocument_no AND c.fnstatus = 1
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2)

                    UNION ALL
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,'4' AS fnindex, a.fdtrans_date,
                    IF(a.fcother_cost>0,(SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P07'),(SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P08')) AS ftcoa_code,
                    CONCAT(a.ftpurchase_return_no,' - ',IFNULL(a.ftsupplier_code,''),' - ',IFNULL(a.ftnotes,'')) AS ftdescription,
                    IF(a.fcother_cost<0,a.fcother_cost*a.fcrate,'0') AS fcdebit, IF(a.fcother_cost>0,ABS(a.fcother_cost*a.fcrate),'0') AS fccredit,
                    'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txpurchase_return_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftpurchase_return_no = c.ftdocument_no AND c.fnstatus = 1
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2) AND a.fcother_cost <> 0

                    UNION ALL
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,'5' AS fnindex, a.fdtrans_date,
                    (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P03') AS ftcoa_code,
                    CONCAT(a.ftpurchase_return_no,' - ',IFNULL(a.ftsupplier_code,''),' - ',IFNULL(a.ftnotes,'')) AS ftdescription,
                    '0' AS fcdebit, a.fcvat*a.fcrate AS fccredit,
                    'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txpurchase_return_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftpurchase_return_no = c.ftdocument_no AND c.fnstatus = 1
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2) AND a.fcvat > 0

                    UNION ALL
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,'6' AS fnindex, a.fdtrans_date,
                    (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P01') AS ftcoa_code,
                    CONCAT(a.ftpurchase_return_no,' - ',IFNULL(a.ftsupplier_code,''),' - ',IFNULL(a.ftnotes,'')) AS ftdescription,
                    ifnull(sum(e.fcNet_Price*ABS(e.fnqty)),0) AS fcdebit, '0' AS fccredit, 'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txpurchase_return_hdr a
                    INNER JOIN txpurchase_return_det b ON a.ftpurchase_return_no = b.ftpurchase_return_no
                    INNER JOIN txgeneral_journal_hdr c ON a.ftpurchase_return_no = c.ftdocument_no AND c.fnstatus = 1
                    LEFT JOIN TXStockFIFO e on e.ftDocument_No=a.ftpurchase_return_no and e.ftItem_Code = b.ftItem_Code AND e.fnstatus = 1
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2)
                    GROUP BY a.fdtrans_date, a.ftpurchase_return_no

                    UNION ALL
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,'7' AS fnindex, a.fdtrans_date,
                    CASE WHEN f.fnpo_type3 = 0 THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB01')
			 WHEN f.fnpo_type3 = 1 THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ01')
                         WHEN f.fnpo_type3 = 2 THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP01')
                         WHEN f.fnpo_type3 = 3 THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I01')
                    END AS ftcoa_code,
                    CONCAT(a.ftpurchase_return_no,' - ',IFNULL(a.ftsupplier_code,''),' - ',IFNULL(a.ftnotes,'')) AS ftdescription,
                    '0' AS fcdebit, ifnull(sum(e.fcNet_Price*ABS(e.fnqty)),0) AS fccredit, 'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txpurchase_return_hdr a
                    INNER JOIN txpurchase_return_det b ON a.ftpurchase_return_no = b.ftpurchase_return_no
                    INNER JOIN txgeneral_journal_hdr c ON a.ftpurchase_return_no = c.ftdocument_no AND c.fnstatus = 1
                    INNER JOIN txpurchase_invoice_hdr d ON a.ftpurchase_invoice_no = d.ftpurchase_invoice_no
                    INNER JOIN txpurchase_order_hdr f ON d.ftpo_no = f.ftpo_no
                    LEFT JOIN TXStockFIFO e on e.ftDocument_No=a.ftpurchase_return_no and e.ftItem_Code = b.ftItem_Code AND e.fnstatus = 1
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2)
                    GROUP BY a.fdtrans_date, a.ftpurchase_return_no
                ) X
                ORDER BY fdtrans_date, ftjournal_no, fnindex";

        pr($ssql);
        mysql_query("BEGIN");
        $rs=mysql_query($ssql);
        mysql_query("COMMIT");

        pr("*** UPDATE TO JURNAL SYSTEM HDR ***");

        // UPDATE COGS PURCHASE RETURN HEADER
        $ssql="SELECT a.ftjournal_no, a.ftsource, a.ftdocument_no, SUM(b.fcdebit) AS fcamount
                FROM txgeneral_journal_hdr a
                INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no
                WHERE a.ftsource LIKE '%return_pembelian%' AND a.fnstatus = 1
                GROUP BY a.ftjournal_no
                ORDER BY a.ftjournal_no";

        $rs=mysql_query($ssql);
        $ssql1=array();

        while($r=mysql_fetch_assoc($rs)) {
            mysql_query("BEGIN");

            $ssql="UPDATE txgeneral_journal_hdr
                    SET fcdebit = '{$r['fcamount']}', fccredit = '{$r['fcamount']}'
                    WHERE ftjournal_no = '{$r['ftjournal_no']}'  ";

            mysql_query($ssql);
            //pr($ssql);
            mysql_query("COMMIT");
        }

        */
        

?>