<?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";

        /*
        // KAS MASUK HEADER
        
        $ssql="SELECT fdtrans_date, ftcashrcp_no AS ftdocument_no, '1' AS fnstatus, '0' AS fntype,
                'kas_masuk' AS ftsource, fcamount, fcrate
                FROM txcashrcp_hdr a
                WHERE a.fnstatus = 1 OR a.fnstatus = 2
                ORDER BY a.fdtrans_date, a.ftcashrcp_no";

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

        pr("*** INSERTING KAS MASUK 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,
                        'kas_masuk' AS ftsource, 'Admin' AS ftcreated_user, '2013-11-20' AS fdcreated_date,
                        'Admin' AS ftmodified_user,  '2013-11-20' AS fdmodified_date; ";

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

        // KAS MASUK DETAIL
        pr("*** INSERTING KAS MASUK 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,
                    a.ftacc_no1 AS ftcoa_code, CONCAT(ftcashrcp_no,' - ',ftreceive_name,' - ',ftremarks) AS ftdescription,
                    a.fcamount*a.fcrate AS fcdebit, '0' AS fccredit, 'Admin' AS ftcreated_user, '2013-11-20' AS fdcreated_date,
                    'Admin' AS ftmodified_user,  '2013-11-20' AS fdmodified_date
                    FROM txcashrcp_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftcashrcp_no = c.ftdocument_no AND c.ftsource='kas_masuk' AND c.fnstatus = 1
                    WHERE a.fnstatus = 1 OR a.fnstatus = 2
                    UNION ALL
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,b.fnindex+1 AS  fnindex, a.fdtrans_date,
                    b.ftcoa_code AS ftcoa_code, CONCAT(a.ftcashrcp_no,' - ',a.ftreceive_name,' - ',b.ftdescription) AS ftdescription,
                    IF(b.fcamount<0,ABS(b.fcamount*a.fcrate),'0') AS fcdebit, IF(b.fcamount>0,b.fcamount*a.fcrate,'0')  AS fccredit,
                    'Admin' AS ftcreated_user, '2013-11-20' AS fdcreated_date,
                    'Admin' AS ftmodified_user,  '2013-11-20' AS fdmodified_date
                    FROM txcashrcp_hdr a INNER JOIN txcashrcp_det b ON a.ftcashrcp_no = b.ftcashrcp_no
                    INNER JOIN txgeneral_journal_hdr c
                    ON a.ftcashrcp_no = c.ftdocument_no AND c.ftsource='kas_masuk' AND c.fnstatus = 1
                    WHERE a.fnstatus = 1 OR a.fnstatus = 2
                    ) X
                    ORDER BY fdtrans_date, ftjournal_no, fnindex";

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

        // KAS KELUAR HEADER

        $ssql="SELECT fdtrans_date, ftcashpym_no AS ftdocument_no, '1' AS fnstatus, '0' AS fntype,
                'kas_keluar' AS ftsource, fcamount, fcrate
                FROM txcashpym_hdr a
                WHERE a.fnstatus = 1 OR a.fnstatus = 2
                ORDER BY a.fdtrans_date, a.ftcashpym_no";

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

        pr("*** INSERTING KAS KELUAR 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,
                        'kas_keluar' AS ftsource, 'Admin' AS ftcreated_user, '2013-11-20' AS fdcreated_date,
                        'Admin' AS ftmodified_user,  '2013-11-20' AS fdmodified_date; ";

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


        }

        // KAS KELUAR DETAIL
        pr("*** INSERTING KAS KELUAR 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,
                    a.ftacc_no1 AS ftcoa_code, CONCAT(ftcashpym_no,' - ',ftreceive_name,' - ',ftremarks) AS ftdescription,
                    '0' AS fcdebit, a.fcamount*a.fcrate AS fccredit, 'Admin' AS ftcreated_user, '2013-11-20' AS fdcreated_date,
                    'Admin' AS ftmodified_user,  '2013-11-20' AS fdmodified_date
                    FROM txcashpym_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftcashpym_no = c.ftdocument_no AND c.ftsource='kas_keluar' AND c.fnstatus = 1
                    WHERE a.fnstatus = 1 OR a.fnstatus = 2
                    UNION ALL
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,b.fnindex+1 AS  fnindex, a.fdtrans_date,
                    b.ftcoa_code AS ftcoa_code, CONCAT(a.ftcashpym_no,' - ',a.ftreceive_name,' - ',b.ftdescription) AS ftdescription,
                    IF(b.fcamount>0,b.fcamount*a.fcrate,'0') AS fcdebit, IF(b.fcamount<0,ABS(b.fcamount*a.fcrate),'0')  AS fccredit,
                    'Admin' AS ftcreated_user, '2013-11-20' AS fdcreated_date,
                    'Admin' AS ftmodified_user,  '2013-11-20' AS fdmodified_date
                    FROM txcashpym_hdr a INNER JOIN txcashpym_det b ON a.ftcashpym_no = b.ftcashpym_no
                    INNER JOIN txgeneral_journal_hdr c
                    ON a.ftcashpym_no = c.ftdocument_no AND c.ftsource='kas_keluar' AND c.fnstatus = 1
                    WHERE a.fnstatus = 1 OR a.fnstatus = 2
                    ) X
                    ORDER BY fdtrans_date, ftjournal_no, fnindex";

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

        
        // BANK MASUK HEADER

        $ssql="SELECT fdtrans_date, ftbankrcp_no AS ftdocument_no, '1' AS fnstatus, '0' AS fntype,
                'bank_masuk' AS ftsource, fcamount, fcrate
                FROM txbankrcp_hdr a
                WHERE a.fnstatus = 1 OR a.fnstatus = 2
                ORDER BY a.fdtrans_date, a.ftbankrcp_no";

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

        pr("*** INSERTING BANK MASUK 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,
                        'bank_masuk' AS ftsource, 'Admin' AS ftcreated_user, '2014-03-08' AS fdcreated_date,
                        'Admin' AS ftmodified_user,  '2014-03-08' AS fdmodified_date; ";

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


        }

        // BANK MASUK DETAIL
        pr("*** INSERTING BANK MASUK 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,
                    a.ftacc_no1 AS ftcoa_code, CONCAT(ftbankrcp_no,' - ',ftreceive_name,' - ',ftremarks) AS ftdescription,
                    a.fcamount*a.fcrate AS fcdebit, '0' AS fccredit, 'Admin' AS ftcreated_user, '2014-03-08' AS fdcreated_date,
                    'Admin' AS ftmodified_user,  '2014-03-08' AS fdmodified_date
                    FROM txbankrcp_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftbankrcp_no = c.ftdocument_no AND c.ftsource='bank_masuk' AND c.fnstatus = 1
                    WHERE a.fnstatus = 1 OR a.fnstatus = 2
                    UNION ALL
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,b.fnindex+1 AS  fnindex, a.fdtrans_date,
		    CASE WHEN b.fnpayment_method IN ('0','1') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F07')
			 ELSE b.ftcoa_code END AS ftcoa_code,
                    CONCAT(a.ftbankrcp_no,' - ',a.ftreceive_name,' - ',b.ftdescription) AS ftdescription,
                    IF(b.fcamount<0,ABS(b.fcamount*a.fcrate),'0') AS fcdebit, IF(b.fcamount>0,b.fcamount*a.fcrate,'0')  AS fccredit,
                    'Admin' AS ftcreated_user, '2014-03-08' AS fdcreated_date,
                    'Admin' AS ftmodified_user,  '2014-03-08' AS fdmodified_date
                    FROM txbankrcp_hdr a INNER JOIN txbankrcp_det b ON a.ftbankrcp_no = b.ftbankrcp_no
                    INNER JOIN txgeneral_journal_hdr c
                    ON a.ftbankrcp_no = c.ftdocument_no AND c.ftsource='bank_masuk' AND c.fnstatus = 1
                    WHERE a.fnstatus = 1 OR a.fnstatus = 2
                    ) X
                    ORDER BY fdtrans_date, ftjournal_no, fnindex";

        mysql_query("BEGIN");
        $rs=mysql_query($ssql);
        mysql_query("COMMIT");
        
        // BANK KELUAR HEADER

        $ssql="SELECT fdtrans_date, ftbankpym_no AS ftdocument_no, '1' AS fnstatus, '0' AS fntype,
                'bank_keluar' AS ftsource, fcamount, fcrate
                FROM txbankpym_hdr a
                WHERE a.fnstatus = 1 and ftbankpym_no >= 'BO190400065'
                ORDER BY a.fdtrans_date, a.ftbankpym_no";

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

        pr("*** INSERTING BANK KELUAR TO JURNAL SYSTEM HDR ***");

        while($r=mysql_fetch_assoc($rs)) {
            mysql_query("BEGIN");
            $ftjournal_no=getAutoNumber("PUSAT","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,
                        'bank_keluar' 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");


        }

        // BANK KELUAR DETAIL
        pr("*** INSERTING BANK KELUAR 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,
                    a.ftacc_no1 AS ftcoa_code, CONCAT(ftbankpym_no,' - ',ftremarks) 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 txbankpym_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftbankpym_no = c.ftdocument_no AND c.ftsource='bank_keluar' AND c.fnstatus = 1
                    WHERE a.fnstatus = 1  and a.ftbankpym_no >= 'BO190400065'
                    UNION ALL
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,b.fnindex+1 AS  fnindex, a.fdtrans_date,
                    CASE WHEN b.fnpayment_method IN ('0','1') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F08')
			 ELSE b.ftcoa_code END AS ftcoa_code,
                    CONCAT(a.ftbankpym_no,' - ',b.ftdescription) AS ftdescription,
                    IF(b.fcamount>0,b.fcamount*a.fcrate,'0') AS fcdebit, IF(b.fcamount<0,ABS(b.fcamount*a.fcrate),'0')  AS fccredit,
                    'Admin' AS ftcreated_user, now() AS fdcreated_date,
                    'Admin' AS ftmodified_user,  now() AS fdmodified_date
                    FROM txbankpym_hdr a INNER JOIN txbankpym_det b ON a.ftbankpym_no = b.ftbankpym_no
                    INNER JOIN txgeneral_journal_hdr c
                    ON a.ftbankpym_no = c.ftdocument_no AND c.ftsource='bank_keluar' AND c.fnstatus = 1
                    WHERE a.fnstatus = 1  and a.ftbankpym_no >= 'BO190400065'
                    ) X
                    ORDER BY fdtrans_date, ftjournal_no, fnindex";

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

        */
        // PEMBAYARAN HUTANG HEADER

        $ssql="SELECT fdtrans_date, ftcvs_no AS ftdocument_no, '1' AS fnstatus, '0' AS fntype,
                IF(a.fntype=0,'rekap_bank_voucher','rekap_bank_voucher_impor') AS ftsource, fcamount, fckurs as fcrate
                FROM txrecap_bank_voucher_hdr a
                WHERE (a.fnstatus = 1 OR a.fnstatus = 2) and a.ftcvs_no = 'AP190500001'
                ORDER BY a.fdtrans_date, a.ftcvs_no";

        $rs=mysql_query($ssql);
        pr($ssql);

        pr("*** INSERTING PEMBAYARAN HUTANG TO JURNAL SYSTEM HDR ***");

        while($r=mysql_fetch_assoc($rs)) {
            mysql_query("BEGIN");
            $ftjournal_no=getAutoNumber("PUSAT","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,
                        '{$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);
            mysql_query("COMMIT");
            // pr($ssql);

             //  PEMBAYARAN HUTANG DETAIL
            // pr("*** INSERTING PEMBAYARAN HUTANG (PEMBAYARAN) 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, '{$ftjournal_no}' as ftjournal_no ,b.fnindex AS  fnindex, a.fdtrans_date,
                        CASE WHEN b.fnpayment_method IN ('0','1') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F08')
                             WHEN b.fnpayment_method IN ('9') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F10')
                             ELSE b.ftacc_no END AS ftcoa_code,
                        CONCAT(a.ftcvs_no,' - ',a.ftremarks) AS ftdescription,
                        '0' AS fcdebit, b.fcamount AS fccredit, 'Admin' AS ftcreated_user, now() AS fdcreated_date,
                        'Admin' AS ftmodified_user,  now() AS fdmodified_date
                        FROM txrecap_bank_voucher_hdr a INNER JOIN txrecap_bank_voucher_det1 b ON a.ftcvs_no = b.ftcvs_no
                        WHERE (a.fnstatus = 1 OR a.fnstatus = 2) and a.ftcvs_no = '{$r['ftdocument_no']}'
                        ) X
                        ORDER BY fdtrans_date, ftjournal_no, fnindex";

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

            //  PEMBAYARAN HUTANG DETAIL 1 - PEMBAYARAN OTHER COST
            // pr("*** INSERTING PEMBAYARAN HUTANG (OTHER COST PEMBAYARAN) TO JURNAL SYSTEM DET ***");

            $ssql="SELECT * FROM (
                        SELECT a.fdtrans_date, b.fnindex,b.ftother_coa_code as ftcoa_code,
                        CONCAT(a.ftcvs_no,' - ',a.ftremarks) AS ftdescription,
                        IF(b.fcother_cost<0,ABS(b.fcother_cost),'0') AS fcdebit, IF(b.fcother_cost>0,b.fcother_cost,'0')  AS fccredit
                        FROM txrecap_bank_voucher_hdr a INNER JOIN txrecap_bank_voucher_det1 b ON a.ftcvs_no = b.ftcvs_no
                        WHERE (a.fnstatus = 1 OR a.fnstatus = 2) and a.ftcvs_no = '{$r['ftdocument_no']}'
                        ) X
                        WHERE fcdebit <> 0 or fccredit <> 0
                        ORDER BY fnindex";

            $rs1=mysql_query($ssql);
            // pr($ssql);

            $fnindex = RetRstField("SELECT count(*) + 1 FROM txgeneral_journal_hdr a INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no WHERE a.ftdocument_no = '{$r['ftdocument_no']}'");

            while($r1=mysql_fetch_assoc($rs1)) {
                mysql_query("BEGIN");

                $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 'PUSAT' AS ftbranch_code, '{$ftjournal_no}' as ftjournal_no, '{$fnindex}' as fnindex, '{$r1['fdtrans_date']}' AS fdtrans_date,
                            '{$r1['ftcoa_code']}' AS ftcoa_code, '{$r1['ftdescription']}' AS ftdescription,
                            '{$r1['fcdebit']}' AS fcdebit, '{$r1['fccredit']}' AS fccredit,
                            'Admin' AS ftcreated_user, now() AS fdcreated_date,
                            'Admin' AS ftmodified_user,  now() AS fdmodified_date";

                $strSQL["insert txgeneral_journal_det"]=$ssql;
                mysql_query($ssql);
                mysql_query("COMMIT");
                $fnindex = $fnindex + 1;
                // pr($ssql);
            }


            //  PEMBAYARAN HUTANG DETAIL 2
            // pr("*** INSERTING PEMBAYARAN HUTANG (INVOICE) TO JURNAL SYSTEM DET ***");

            $ssql="SELECT * FROM (
                        SELECT a.fdtrans_date, b.fnindex,
                        (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F01') AS ftcoa_code,
                        CONCAT(a.ftcvs_no,' - ',b.ftpi_no) AS ftdescription,
                        b.fcpayment_amount AS fcdebit
                        FROM txrecap_bank_voucher_hdr a INNER JOIN txrecap_bank_voucher_det b ON a.ftcvs_no = b.ftcvs_no
                        WHERE (a.fnstatus = 1 OR a.fnstatus = 2) and a.ftcvs_no = '{$r['ftdocument_no']}'
                        ) X
                        ORDER BY fnindex";

            $rs2=mysql_query($ssql);
            // pr($ssql);

            while($r2=mysql_fetch_assoc($rs2)) {
                mysql_query("BEGIN");

                $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 'PUSAT' AS ftbranch_code, '{$ftjournal_no}' as ftjournal_no, '{$fnindex}' as fnindex, '{$r2['fdtrans_date']}' AS fdtrans_date,
                            '{$r2['ftcoa_code']}' AS ftcoa_code, '{$r2['ftdescription']}' AS ftdescription,
                            '{$r2['fcdebit']}' AS fcdebit, '0' AS fccredit,
                            'Admin' AS ftcreated_user, now() AS fdcreated_date,
                            'Admin' AS ftmodified_user,  now() AS fdmodified_date";

                $strSQL["insert txgeneral_journal_det"]=$ssql;
                mysql_query($ssql);
                mysql_query("COMMIT");
                $fnindex = $fnindex + 1;
                // pr($ssql);
            }

            //  PEMBAYARAN HUTANG DETAIL 3 - BIAYA
            // pr("*** INSERTING PEMBAYARAN HUTANG (BIAYA) TO JURNAL SYSTEM DET ***");

            $ssql="SELECT * FROM (
                        SELECT a.fdtrans_date, b.fnindex, b.ftcoa_code,
                        CONCAT(a.ftcvs_no,' - ',b.ftdescription) AS ftdescription,
                        IF(b.fcamount>0,b.fcamount * a.fckurs,'0') AS fccredit , IF(b.fcamount<0,ABS(b.fcamount * a.fckurs),'0')  AS fcdebit
                        FROM txrecap_bank_voucher_hdr a INNER JOIN txrecap_bank_voucher_cost b ON a.ftcvs_no = b.ftcvs_no AND b.fcamount <> 0
                        WHERE (a.fnstatus = 1 OR a.fnstatus = 2) and a.ftcvs_no = '{$r['ftdocument_no']}'
                        ) X
                        WHERE fcdebit <> 0 OR fccredit <> 0
                        ORDER BY fnindex";

            $rs3=mysql_query($ssql);
            // pr($ssql);

            while($r3=mysql_fetch_assoc($rs3)) {
                mysql_query("BEGIN");

                $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 'PUSAT' AS ftbranch_code, '{$ftjournal_no}' as ftjournal_no, '{$fnindex}' as fnindex, '{$r3['fdtrans_date']}' AS fdtrans_date,
                            '{$r3['ftcoa_code']}' AS ftcoa_code, '{$r3['ftdescription']}' AS ftdescription,
                            '{$r3['fcdebit']}' AS fcdebit, '{$r3['fccredit']}' AS fccredit,
                            'Admin' AS ftcreated_user, now() AS fdcreated_date,
                            'Admin' AS ftmodified_user,  now() AS fdmodified_date";

                $strSQL["insert txgeneral_journal_det"]=$ssql;
                mysql_query($ssql);
                mysql_query("COMMIT");
                $fnindex = $fnindex + 1;
                // pr($ssql);
            }

            //  PEMBAYARAN HUTANG DETAIL 4 - SELISIH KURS
            // pr("*** INSERTING PEMBAYARAN HUTANG (SELISIH KURS) TO JURNAL SYSTEM DET ***");

            $ssql="SELECT * FROM (
                    SELECT a.fdtrans_date,
		    (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F21') AS ftcoa_code,
                    CONCAT(a.ftcvs_no,' - ',a.ftremarks) AS ftdescription,
                    IF(a.fcrate_adjustment>0,a.fcrate_adjustment,'0') AS fccredit, IF(a.fcrate_adjustment<0,ABS(a.fcrate_adjustment),'0')  AS fcdebit
                    FROM txrecap_bank_voucher_hdr a
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2) AND a.fcrate_adjustment <> 0 and a.ftcvs_no = '{$r['ftdocument_no']}'
                    ) X
                    WHERE fcdebit <> 0 OR fccredit <> 0 ";

            $rs4=mysql_query($ssql);
            // pr($ssql);

            while($r4=mysql_fetch_assoc($rs4)) {
                mysql_query("BEGIN");

                $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 'PUSAT' AS ftbranch_code, '{$ftjournal_no}' as ftjournal_no, '{$fnindex}' as fnindex, '{$r4['fdtrans_date']}' AS fdtrans_date,
                            '{$r4['ftcoa_code']}' AS ftcoa_code, '{$r4['ftdescription']}' AS ftdescription,
                            '{$r4['fcdebit']}' AS fcdebit, '{$r4['fccredit']}' AS fccredit,
                            'Admin' AS ftcreated_user, now() AS fdcreated_date,
                            'Admin' AS ftmodified_user,  now() AS fdmodified_date";

                $strSQL["insert txgeneral_journal_det"]=$ssql;
                mysql_query($ssql);
                mysql_query("COMMIT");
                $fnindex = $fnindex + 1;
                // pr($ssql);
            }

        }
        
        
        // PENERIMAAN PIUTANG HEADER

        $ssql="SELECT fdtrans_date, ftar_no AS ftdocument_no, '1' AS fnstatus, '0' AS fntype,
                'penerimaan_piutang' AS ftsource, fcamount, fcrate
                FROM txar_hdr a
                WHERE (a.fnstatus = 1 OR a.fnstatus = 2)
                        AND (ftar_no = 'AR190100265' OR ftar_no = 'AR190100266' OR ftar_no = 'AR190400203' OR ftar_no = 'AR190400204'
				OR ftar_no = 'AR190500001' OR ftar_no = 'AR190500002' OR ftar_no = 'AR190500003' OR ftar_no = 'AR190500004'
				OR ftar_no = 'AR190500005' OR ftar_no = 'AR190500006' )
                ORDER BY a.fdtrans_date, a.ftar_no";

        $rs=mysql_query($ssql);
        pr($ssql);

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

        while($r=mysql_fetch_assoc($rs)) {
            mysql_query("BEGIN");
            $ftjournal_no=getAutoNumber("PUSAT","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,
                        'penerimaan_piutang' 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);
            mysql_query("COMMIT");
            // pr($ssql);

             //  PENERIMAAN PIUTANG DETAIL
            // pr("*** INSERTING PENERIMAAN PIUTANG (PEMBAYARAN) 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, '{$ftjournal_no}' as ftjournal_no ,b.fnindex AS  fnindex, a.fdtrans_date,
                        CASE WHEN b.fnpayment_method IN ('0','1') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F07')
                             WHEN b.fnpayment_method IN ('9') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F09')
                             ELSE b.ftacc_no END AS ftcoa_code,
                        CONCAT(a.ftar_no,' - ',a.ftremarks) 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 txar_hdr a INNER JOIN txar_det1 b ON a.ftar_no = b.ftar_no
                        WHERE (a.fnstatus = 1 OR a.fnstatus = 2) and a.ftar_no = '{$r['ftdocument_no']}'                           
                        ) X
                        ORDER BY fdtrans_date, ftjournal_no, fnindex";

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

            //  PENERIMAAN PIUTANG DETAIL 1 - PEMBAYARAN OTHER COST
            // pr("*** INSERTING PENERIMAAN PIUTANG (OTHER COST PEMBAYARAN) TO JURNAL SYSTEM DET ***");

            $ssql="SELECT * FROM (
                        SELECT a.fdtrans_date, b.fnindex,b.ftother_coa_code as ftcoa_code,
                        CONCAT(a.ftar_no,' - ',a.ftremarks) AS ftdescription,
                        IF(b.fcother_cost>0,b.fcother_cost * a.fcrate,'0') AS fcdebit, IF(b.fcother_cost<0,ABS(b.fcother_cost * a.fcrate),'0')  AS fccredit
                        FROM txar_hdr a INNER JOIN txar_det1 b ON a.ftar_no = b.ftar_no
                        WHERE (a.fnstatus = 1 OR a.fnstatus = 2) and a.ftar_no = '{$r['ftdocument_no']}'
                        ) X
                        WHERE fcdebit <> 0 or fccredit <> 0
                        ORDER BY fnindex";

            $rs1=mysql_query($ssql);
            // pr($ssql);

            $fnindex = RetRstField("SELECT count(*) + 1 FROM txgeneral_journal_hdr a INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no WHERE a.ftdocument_no = '{$r['ftdocument_no']}'");

            while($r1=mysql_fetch_assoc($rs1)) {
                mysql_query("BEGIN");

                $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 'PUSAT' AS ftbranch_code, '{$ftjournal_no}' as ftjournal_no, '{$fnindex}' as fnindex, '{$r1['fdtrans_date']}' AS fdtrans_date,
                            '{$r1['ftcoa_code']}' AS ftcoa_code, '{$r1['ftdescription']}' AS ftdescription,
                            '{$r1['fcdebit']}' AS fcdebit, '{$r1['fccredit']}' AS fccredit,
                            'Admin' AS ftcreated_user, now() AS fdcreated_date,
                            'Admin' AS ftmodified_user,  now() AS fdmodified_date";

                $strSQL["insert txgeneral_journal_det"]=$ssql;
                mysql_query($ssql);
                mysql_query("COMMIT");
                $fnindex = $fnindex + 1;
                // pr($ssql);
            }


            //  PENERIMAAN PIUTANG DETAIL 2
            // pr("*** INSERTING PENERIMAAN PIUTANG (INVOICE) TO JURNAL SYSTEM DET ***");

            $ssql="SELECT * FROM (
                        SELECT a.fdtrans_date, b.fnindex,
                        (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F04') AS ftcoa_code,
                        CONCAT(a.ftar_no,' - ',b.ftsales_invoice_no) AS ftdescription,
                        case when b.fcpayment_amount_base < 0 then b.fcpayment_amount_base * -1 else 0 end as fcdebit,
                        case when b.fcpayment_amount_base > 0 then b.fcpayment_amount_base  else 0 end as fccredit
                        FROM txar_hdr a INNER JOIN txar_det b ON a.ftar_no = b.ftar_no
                        WHERE (a.fnstatus = 1 OR a.fnstatus = 2) and a.ftar_no = '{$r['ftdocument_no']}'
                        ) X
                        ORDER BY fnindex";

            $rs2=mysql_query($ssql);
            // pr($ssql);

            $fnindex = RetRstField("SELECT count(*) + 1 FROM txgeneral_journal_hdr a INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no WHERE a.ftdocument_no = '{$r['ftdocument_no']}'");

            while($r2=mysql_fetch_assoc($rs2)) {
                mysql_query("BEGIN");

                $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 'PUSAT' AS ftbranch_code, '{$ftjournal_no}' as ftjournal_no, '{$fnindex}' as fnindex, '{$r2['fdtrans_date']}' AS fdtrans_date,
                            '{$r2['ftcoa_code']}' AS ftcoa_code, '{$r2['ftdescription']}' AS ftdescription,
                            '0' AS fcdebit, '{$r2['fccredit']}' AS fccredit,
                            'Admin' AS ftcreated_user, now() AS fdcreated_date,
                            'Admin' AS ftmodified_user,  now() AS fdmodified_date";

                $strSQL["insert txgeneral_journal_det"]=$ssql;
                mysql_query($ssql);
                mysql_query("COMMIT");
                $fnindex = $fnindex + 1;
                // pr($ssql);
            }

            //  PENERIMAAN PIUTANG DETAIL 3 - BIAYA
            // pr("*** INSERTING PENERIMAAN PIUTANG (BIAYA) TO JURNAL SYSTEM DET ***");

            $ssql="SELECT * FROM (
                        SELECT a.fdtrans_date, b.fnindex, b.ftcoa_code,
                        CONCAT(a.ftar_no,' - ',b.ftdescription) AS ftdescription,
                        IF(b.fcamount>0,b.fcamount * a.fcrate,'0') AS fcdebit, IF(b.fcamount<0,ABS(b.fcamount * a.fcrate),'0')  AS fccredit
                        FROM txar_hdr a INNER JOIN txar_cost b ON a.ftar_no = b.ftar_no AND b.fcamount <> 0
                        WHERE (a.fnstatus = 1 OR a.fnstatus = 2) and a.ftar_no = '{$r['ftdocument_no']}'
                        ) X
                        WHERE fcdebit <> 0 OR fccredit <> 0
                        ORDER BY fnindex";

            $rs3=mysql_query($ssql);
            // pr($ssql);

            $fnindex = RetRstField("SELECT count(*) + 1 FROM txgeneral_journal_hdr a INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no WHERE a.ftdocument_no = '{$r['ftdocument_no']}'");

            while($r3=mysql_fetch_assoc($rs3)) {
                mysql_query("BEGIN");

                $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 'PUSAT' AS ftbranch_code, '{$ftjournal_no}' as ftjournal_no, '{$fnindex}' as fnindex, '{$r3['fdtrans_date']}' AS fdtrans_date,
                            '{$r3['ftcoa_code']}' AS ftcoa_code, '{$r3['ftdescription']}' AS ftdescription,
                            '{$r3['fcdebit']}' AS fcdebit, '{$r3['fccredit']}' AS fccredit,
                            'Admin' AS ftcreated_user, now() AS fdcreated_date,
                            'Admin' AS ftmodified_user,  now() AS fdmodified_date";

                $strSQL["insert txgeneral_journal_det"]=$ssql;
                mysql_query($ssql);
                mysql_query("COMMIT");
                $fnindex = $fnindex + 1;
                // pr($ssql);
            }

            //  PENERIMAAN PIUTANG DETAIL 4 - SELISIH KURS
            // pr("*** INSERTING PENERIMAAN PIUTANG (SELISIH KURS) TO JURNAL SYSTEM DET ***");

            $ssql="SELECT * FROM (
                    SELECT a.fdtrans_date,
		    (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F21') AS ftcoa_code,
                    CONCAT(a.ftar_no,' - ',a.ftremarks) AS ftdescription,
                    IF(a.fcrate_adjustment>0,a.fcrate_adjustment,'0') AS fcdebit, IF(a.fcrate_adjustment<0,ABS(a.fcrate_adjustment),'0')  AS fccredit
                    FROM txar_hdr a
                    WHERE (a.fnstatus = 1 OR a.fnstatus = 2) AND a.fcrate_adjustment <> 0 and a.ftar_no = '{$r['ftdocument_no']}'
                    ) X
                    WHERE fcdebit <> 0 OR fccredit <> 0 ";

            $rs4=mysql_query($ssql);
            // pr($ssql);

            $fnindex = RetRstField("SELECT count(*) + 1 FROM txgeneral_journal_hdr a INNER JOIN txgeneral_journal_det b ON a.ftjournal_no = b.ftjournal_no WHERE a.ftdocument_no = '{$r['ftdocument_no']}'");

            while($r4=mysql_fetch_assoc($rs4)) {
                mysql_query("BEGIN");

                $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 'PUSAT' AS ftbranch_code, '{$ftjournal_no}' as ftjournal_no, '{$fnindex}' as fnindex, '{$r4['fdtrans_date']}' AS fdtrans_date,
                            '{$r4['ftcoa_code']}' AS ftcoa_code, '{$r4['ftdescription']}' AS ftdescription,
                            '{$r4['fcdebit']}' AS fcdebit, '{$r4['fccredit']}' AS fccredit,
                            'Admin' AS ftcreated_user, now() AS fdcreated_date,
                            'Admin' AS ftmodified_user,  now() AS fdmodified_date";

                $strSQL["insert txgeneral_journal_det"]=$ssql;
                mysql_query($ssql);
                mysql_query("COMMIT");
                $fnindex = $fnindex + 1;
                // pr($ssql);
            }

        }

        /*
        // CREDIT MEMO HEADER
        // credit memo di posisi kredit

        $ssql="SELECT fdtrans_date, ftcm_no AS ftdocument_no, '1' AS fnstatus, '0' AS fntype,
                'credit_memo' AS ftsource, fcinvoice_amount as fcamount, '1' as fcrate
                FROM txcredit_memo_hdr a
                WHERE a.fnstatus = 1 OR a.fnstatus = 2
                ORDER BY a.fdtrans_date, a.ftcm_no";

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

        pr("*** INSERTING CREDIT MEMO 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,
                        'credit_memo' AS ftsource, 'Admin' AS ftcreated_user, '2014-03-25' AS fdcreated_date,
                        'Admin' AS ftmodified_user,  '2014-03-25' AS fdmodified_date; ";

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

        }

        // CREDIT MEMO  DETAIL
        pr("*** INSERTING CREDIT MEMO  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,
                    CASE WHEN a.fntrans_type IN ('0') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F14')
			 WHEN a.fntrans_type IN ('1') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F15')
                         WHEN a.fntrans_type IN ('2') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F16')
                         WHEN a.fntrans_type IN ('3') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F24')
                    END AS ftcoa_code,
                    CONCAT(a.ftcm_no,' - ',a.ftnotes) AS ftdescription,
                    '0' AS fcdebit, fcinvoice_amount AS fccredit, 'Admin' AS ftcreated_user, '2014-03-25' AS fdcreated_date,
                    'Admin' AS ftmodified_user,  '2014-03-25' AS fdmodified_date
                    FROM txcredit_memo_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftcm_no = c.ftdocument_no AND c.ftsource='credit_memo' AND c.fnstatus = 1
                    WHERE a.fnstatus = 1 OR a.fnstatus = 2

                    UNION ALL
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,b.fnindex+1 AS  fnindex, a.fdtrans_date,
                    CASE WHEN a.fntrans_type IN ('0') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S02')
			 WHEN a.fntrans_type IN ('1') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S05')
                         WHEN a.fntrans_type IN ('2') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S01')
                         WHEN a.fntrans_type IN ('3') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F09')
                    END AS ftcoa_code,
                    CONCAT(a.ftcm_no,' - ',b.ftsales_invoice_no,' - ',b.ftdescription) AS ftdescription,
                    b.fccredit_amount AS fcdebit, 0 AS fccredit,
                    'Admin' AS ftcreated_user, '2014-03-25' AS fdcreated_date,
                    'Admin' AS ftmodified_user,  '2014-03-25' AS fdmodified_date
                    FROM txcredit_memo_hdr a INNER JOIN txcredit_memo_det b ON a.ftcm_no = b.ftcm_no
                    INNER JOIN txgeneral_journal_hdr c
                    ON a.ftcm_no = c.ftdocument_no AND c.ftsource='credit_memo' AND c.fnstatus = 1
                    WHERE a.fnstatus = 1 OR a.fnstatus = 2
                    ) X
                    ORDER BY fdtrans_date, ftjournal_no, fnindex";

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


        // DEBIT MEMO HEADER
        // debit memo di posisi debit

        $ssql="SELECT fdtrans_date, ftdm_no AS ftdocument_no, '1' AS fnstatus, '0' AS fntype,
                'debit_memo' AS ftsource, fcinvoice_amount as fcamount, '1' as fcrate
                FROM txdebit_memo_hdr a
                WHERE a.fnstatus = 1 OR a.fnstatus = 2
                ORDER BY a.fdtrans_date, a.ftdm_no";

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

        pr("*** INSERTING DEBIT MEMO 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,
                        'debit_memo' AS ftsource, 'Admin' AS ftcreated_user, '2014-03-25' AS fdcreated_date,
                        'Admin' AS ftmodified_user,  '2014-03-25' AS fdmodified_date; ";

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

        }

        // DEBIT MEMO  DETAIL
        pr("*** INSERTING DEBIT MEMO  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,
                    CASE WHEN a.fntrans_type IN ('0') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F11')
			 WHEN a.fntrans_type IN ('1') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F12')
                         WHEN a.fntrans_type IN ('2') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F13')
                         WHEN a.fntrans_type IN ('3') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F22')
                         WHEN a.fntrans_type IN ('4') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F23')
                    END AS ftcoa_code,
                    CONCAT(a.ftdm_no,' - ',a.ftnotes) AS ftdescription,
                    fcinvoice_amount AS fcdebit, '0' AS fccredit, 'Admin' AS ftcreated_user, '2014-03-25' AS fdcreated_date,
                    'Admin' AS ftmodified_user,  '2014-03-25' AS fdmodified_date
                    FROM txdebit_memo_hdr a INNER JOIN txgeneral_journal_hdr c
                    ON a.ftdm_no = c.ftdocument_no AND c.ftsource='debit_memo' AND c.fnstatus = 1
                    WHERE a.fnstatus = 1 OR a.fnstatus = 2

                    UNION ALL
                    SELECT 'PUSAT' AS ftbranch_code, c.ftjournal_no,b.fnindex+1 AS  fnindex, a.fdtrans_date,
                    CASE WHEN a.fntrans_type IN ('0') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P02')
			 WHEN a.fntrans_type IN ('1') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P06')
                         WHEN a.fntrans_type IN ('2') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P01')
                         WHEN a.fntrans_type IN ('3') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P01')
                         WHEN a.fntrans_type IN ('4') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F10')
                    END AS ftcoa_code,
                    CONCAT(a.ftdm_no,' - ',b.ftcv_no,' - ',b.ftdescription) AS ftdescription,
                    '0' AS fcdebit, b.fcdebit_amount AS fccredit,
                    'Admin' AS ftcreated_user, '2014-03-25' AS fdcreated_date,
                    'Admin' AS ftmodified_user,  '2014-03-25' AS fdmodified_date
                    FROM txdebit_memo_hdr a INNER JOIN txdebit_memo_det b ON a.ftdm_no = b.ftdm_no
                    INNER JOIN txgeneral_journal_hdr c
                    ON a.ftdm_no = c.ftdocument_no AND c.ftsource='debit_memo' AND c.fnstatus = 1
                    WHERE a.fnstatus = 1 OR a.fnstatus = 2
                    ) X
                    ORDER BY fdtrans_date, ftjournal_no, fnindex";

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

        
        // GIRO MUNDUR PIUTANG HEADER
        $ssql="SELECT fdgiro_cliring_date AS fdtrans_date, ftdocument_no, '1' AS fnstatus, '0' AS fntype,
                'giro_mundur_piutang' AS ftsource, fcamount, fcrate, ftgiro_no
                FROM txgiro_hist a
                WHERE (a.fnstatus_giro = 1 OR a.fnstatus_giro = 2)
                ORDER BY a.fdtrans_date, a.ftdocument_no";

        $rs=mysql_query($ssql);
        pr($ssql);

        pr("*** INSERTING GIRO MUNDUR PIUTANG 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,
                        'giro_mundur_piutang' 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);
            mysql_query("COMMIT");
            // pr($ssql);

            //  GIRO MUNDUR PIUTANG - DEBIT
            // pr("*** INSERTING GIRO MUNDUR PIUTANG - DEBIT 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, '{$ftjournal_no}' as ftjournal_no ,'1' AS  fnindex, a.fdtrans_date,
                        CASE WHEN a.fnstatus_giro IN ('1') THEN a.ftacc_no
                             WHEN a.fnstatus_giro IN ('2') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F04')
                        END AS ftcoa_code,
                        CONCAT(a.ftdocument_no,' - ',a.ftgiro_no) 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 txgiro_hist a
                        WHERE ((a.fnstatus_giro = 1) OR (a.fnstatus_giro = 2)) and a.ftdocument_no = '{$r['ftdocument_no']}' and a.ftgiro_no = '{$r['ftgiro_no']}'
                        ) X
                        ORDER BY fdtrans_date, ftjournal_no, fnindex";

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

            //  GIRO MUNDUR PIUTANG - CREDIT
            // pr("*** INSERTING GIRO MUNDUR PIUTANG - CREDIT 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, '{$ftjournal_no}' as ftjournal_no ,'2' AS  fnindex, a.fdtrans_date,
                    CASE WHEN b.fnpayment_method IN ('0','1') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F07')
                         WHEN b.fnpayment_method IN ('9') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F09')
                    END AS ftcoa_code,
                    CONCAT(a.ftdocument_no,' - ',a.ftgiro_no) 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 txgiro_hist a INNER JOIN txar_det1 b ON a.ftdocument_no = b.ftar_no AND a.ftgiro_no = b.ftgiro_no
                    WHERE ((a.fnstatus_giro = 1) OR (a.fnstatus_giro = 2)) AND a.ftdocument_no = '{$r['ftdocument_no']}'  and a.ftgiro_no = '{$r['ftgiro_no']}'
                    ) X
                    ORDER BY fdtrans_date, ftjournal_no, fnindex";

            mysql_query("BEGIN");
            mysql_query($ssql);
            mysql_query("COMMIT");

        }

        // GIRO MUNDUR HUTANG HEADER
        $ssql="SELECT fdgiro_cliring_date AS fdtrans_date, ftdocument_no, '1' AS fnstatus, '0' AS fntype,
                'giro_mundur_hutang' AS ftsource, fcamount, fcrate, ftgiro_no
                FROM txgiro2_hist a
                WHERE (a.fnstatus_giro = 1 OR a.fnstatus_giro = 2)
                ORDER BY a.fdtrans_date, a.ftdocument_no";

        $rs=mysql_query($ssql);
        pr($ssql);

        pr("*** INSERTING GIRO MUNDUR HUTANG 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,
                        'giro_mundur_hutang' 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);
            mysql_query("COMMIT");
            // pr($ssql);

            //  GIRO MUNDUR HUTANG - DEBIT
            // pr("*** INSERTING GIRO MUNDUR HUTANG - DEBIT 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, '{$ftjournal_no}' as ftjournal_no ,'1' AS  fnindex, a.fdtrans_date,
                    CASE WHEN b.fnpayment_method IN ('0','1') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F08')
                         WHEN b.fnpayment_method IN ('9') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F10')
                    END AS ftcoa_code,
                    CONCAT(a.ftdocument_no,' - ',a.ftgiro_no) 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 txgiro2_hist a INNER JOIN txrecap_bank_voucher_det1 b ON a.ftdocument_no = b.ftcvs_no AND a.ftgiro_no = b.ftgiro_no
                    WHERE ((a.fnstatus_giro = 1) OR (a.fnstatus_giro = 2)) AND a.ftdocument_no = '{$r['ftdocument_no']}'  and a.ftgiro_no = '{$r['ftgiro_no']}'
                    ) X
                    ORDER BY fdtrans_date, ftjournal_no, fnindex";


            mysql_query("BEGIN");
            mysql_query($ssql);
            mysql_query("COMMIT");


            //  GIRO MUNDUR HUTANG - CREDIT
            // pr("*** INSERTING GIRO MUNDUR HUTANG - CREDIT 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, '{$ftjournal_no}' as ftjournal_no ,'2' AS  fnindex, a.fdtrans_date,
                        CASE WHEN a.fnstatus_giro IN ('1') THEN a.ftacc_no
                             WHEN a.fnstatus_giro IN ('2') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F01')
                        END AS ftcoa_code,
                        CONCAT(a.ftdocument_no,' - ',a.ftgiro_no) 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 txgiro_hist a
                        WHERE ((a.fnstatus_giro = 1) OR (a.fnstatus_giro = 2)) and a.ftdocument_no = '{$r['ftdocument_no']}' and a.ftgiro_no = '{$r['ftgiro_no']}'
                        ) X
                        ORDER BY fdtrans_date, ftjournal_no, fnindex";

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



        }

        /*
        // UM PENJUALAN HEADER
        $ssql="SELECT fdtrans_date, fttrans_no AS ftdocument_no, '1' AS fnstatus, '0' AS fntype,
                'uang_muka_penjualan' AS ftsource, fcamount, fcrate
                FROM txadvanced_receive a
                WHERE (a.fnstatus = 1 OR a.fnstatus = 2)
                ORDER BY a.fdtrans_date, a.fttrans_no";

        $rs=mysql_query($ssql);
        pr($ssql);

        pr("*** INSERTING UM PENJUALAN 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,
                        'uang_muka_penjualan' AS ftsource, 'Admin' AS ftcreated_user, '2014-03-13' AS fdcreated_date,
                        'Admin' AS ftmodified_user,  '2014-03-13' AS fdmodified_date ";

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

            //  UM PENJUALAN - DEBIT
            // pr("*** INSERTING UM PENJUALAN - DEBIT 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, '{$ftjournal_no}' as ftjournal_no ,'1' AS  fnindex, a.fdtrans_date,
                        CASE WHEN a.fnpayment_method IN ('0','1') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F07')
                             ELSE ftacc_no END AS ftcoa_code,
                        CONCAT(a.fttrans_no,' - ',a.ftremarks) AS ftdescription,
                        a.fcamount*a.fcrate AS fcdebit, '0' AS fccredit, 'Admin' AS ftcreated_user, '2014-03-13' AS fdcreated_date,
                        'Admin' AS ftmodified_user,  '2014-03-13' AS fdmodified_date
                        FROM txadvanced_receive a
                        WHERE ((a.fnstatus = 1) OR (a.fnstatus = 2)) and a.fttrans_no = '{$r['ftdocument_no']}'
                        ) X
                        ORDER BY fdtrans_date, ftjournal_no, fnindex";

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

            // UM PENJUALAN - CREDIT
            // pr("*** INSERTING UM PENJUALAN - CREDIT 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, '{$ftjournal_no}' as ftjournal_no ,'2' AS  fnindex, a.fdtrans_date,
                    (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F09') AS ftcoa_code,
                    CONCAT(a.fttrans_no,' - ',a.ftremarks) AS ftdescription,
                    '0' AS fcdebit, a.fcamount*a.fcrate AS fccredit, 'Admin' AS ftcreated_user, '2014-03-13' AS fdcreated_date,
                    'Admin' AS ftmodified_user,  '2014-03-13' AS fdmodified_date
                    FROM txadvanced_receive a
                    WHERE ((a.fnstatus = 1) OR (a.fnstatus = 2)) and a.fttrans_no = '{$r['ftdocument_no']}'
                    ) X
                    ORDER BY fdtrans_date, ftjournal_no, fnindex";

            mysql_query("BEGIN");
            mysql_query($ssql);
            mysql_query("COMMIT");

        }

        // UM PEMBELIAN HEADER
        $ssql="SELECT fdtrans_date, fttrans_no AS ftdocument_no, '1' AS fnstatus, '0' AS fntype,
                'uang_muka_pembelian' AS ftsource, fcamount, fcrate
                FROM txadvanced_payment a
                WHERE (a.fnstatus = 1 OR a.fnstatus = 2)
                ORDER BY a.fdtrans_date, a.fttrans_no";

        $rs=mysql_query($ssql);
        pr($ssql);

        pr("*** INSERTING UM PEMBELIAN 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,
                        'uang_muka_pembelian' AS ftsource, 'Admin' AS ftcreated_user, '2014-03-13' AS fdcreated_date,
                        'Admin' AS ftmodified_user,  '2014-03-13' AS fdmodified_date ";

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

            // UM PEMBELIAN - DEBIT
            // pr("*** INSERTING UM PENJUALAN - DEBIT 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, '{$ftjournal_no}' as ftjournal_no ,'1' AS  fnindex, a.fdtrans_date,
                    (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F10') AS ftcoa_code,
                    CONCAT(a.fttrans_no,' - ',a.ftremarks) AS ftdescription,
                    a.fcamount*a.fcrate AS fcdebit, '0' AS fccredit, 'Admin' AS ftcreated_user, '2014-03-13' AS fdcreated_date,
                    'Admin' AS ftmodified_user,  '2014-03-13' AS fdmodified_date
                    FROM txadvanced_payment a
                    WHERE ((a.fnstatus = 1) OR (a.fnstatus = 2)) and a.fttrans_no = '{$r['ftdocument_no']}'
                    ) X
                    ORDER BY fdtrans_date, ftjournal_no, fnindex";

            mysql_query("BEGIN");
            mysql_query($ssql);
            mysql_query("COMMIT");

            // UM PEMBELIAN - CREDIT
            // pr("*** INSERTING UM PEMBELIAN - CREDIT 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, '{$ftjournal_no}' as ftjournal_no ,'2' AS  fnindex, a.fdtrans_date,
                        CASE WHEN a.fnpayment_method IN ('0','1') THEN (SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F08')
                             ELSE ftacc_no END AS ftcoa_code,
                        CONCAT(a.fttrans_no,' - ',a.ftremarks) AS ftdescription,
                        '0' AS fcdebit, a.fcamount*a.fcrate AS fccredit, 'Admin' AS ftcreated_user, '2014-03-13' AS fdcreated_date,
                        'Admin' AS ftmodified_user,  '2014-03-13' AS fdmodified_date
                        FROM txadvanced_payment a
                        WHERE ((a.fnstatus = 1) OR (a.fnstatus = 2)) and a.fttrans_no = '{$r['ftdocument_no']}'
                        ) X
                        ORDER BY fdtrans_date, ftjournal_no, fnindex";

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

        }

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

?>