<?php
    // Prepare Insert GL
    function InsertGL ($documentno, $transdate, $source, $GL, $flag=True) {
        // Validasi GL
        $strSQL=array();
        $debit = 0;
        $credit = 0;
        $index = 1;
        
        // AutoNumber Jurnal
        if ($flag) {
                $JournalNo = getAutoNumber($_SESSION['strbranch'],'acc001','txgeneral_journal_hdr','ftjournal_no',$transdate);
        } else {
                $result = mysql_query("SELECT ftJournal_No FROM txgeneral_journal_hdr WHERE ftDocument_No = '" . $DocumentNo . "'");
                $row = mysql_fetch_array($result);
                $JournalNo = $row[0];
        }

        $jmlArray = count($GL);
        $transdate = CDate($transdate);
        // pr("GL:".$transdate." ".$jmlArray);
        // Insert GL Detail
        
        for($i=0; $i<$jmlArray; $i++) {
                pr("Flag : ".$flag." Index:".$i." ".$GL[$i]["fcdebit"]." ".$GL[$i]["fccredit"]);
                if ($GL[$i]["fcdebit"] != 0 || $GL[$i]["fccredit"] != 0) {
                        if ($flag) {
                                $strSQL[] = "insert into txgeneral_journal_det (ftbranch_code,ftJournal_No, fdTrans_Date, fnIndex, ftCOA_Code, ftDescription, fcDebit, fcCredit, ftCreated_User, fdCreated_Date, ftModified_User, fdModified_Date) values ('" . $_SESSION['strbranch'] . "' , '" . $JournalNo . "', '" . $transdate . "', '" . $index . "', '" . $GL[$i]["ftcoa_code"] . "', '" . $GL[$i]["ftdescription"] . "', '" . abs($GL[$i]["fcdebit"]) . "', '" . abs($GL[$i]["fccredit"]) . "', '" . $_SESSION["username"] . "', NOW(), '" . $_SESSION["username"] . "', NOW());";
                                pr ("insert into txgeneral_journal_det (ftbranch_code,ftJournal_No, fdTrans_Date, fnIndex, ftCOA_Code, ftDescription, fcDebit, fcCredit, ftCreated_User, fdCreated_Date, ftModified_User, fdModified_Date) values ('" . $_SESSION['strbranch'] . "' , '" . $JournalNo . "', '" . $transdate . "', '" . $index . "', '" . $GL[$i]["ftcoa_code"] . "', '" . $GL[$i]["ftdescription"] . "', '" . abs($GL[$i]["fcdebit"]) . "', '" . abs($GL[$i]["fccredit"]) . "', '" . $_SESSION["username"] . "', NOW(), '" . $_SESSION["username"] . "', NOW());");
                        } else {
                                $strSQL[] = "insert into txgeneral_journal_det (ftbranch_code,ftJournal_No, fdTrans_Date, fnIndex, ftCOA_Code, ftDescription, fcDebit, fcCredit, ftCreated_User, fdCreated_Date, ftModified_User, fdModified_Date) SELECT '" . $_SESSION['strbranch'] . "' , '" . $JournalNo . "', '" . $transdate . "', MAX(fnIndex)+1, '" . $GL[$i]["ftcoa_code"] . "', '" . $GL[$i]["ftdescription"] . "', '" . abs($GL[$i]["fcdebit"]) . "', '" . abs($GL[$i]["fccredit"]) . "', '" . $_SESSION["username"] . "', NOW(), '" . $_SESSION["username"] . "', NOW() FROM txgeneral_journal_det WHERE ftJournal_No = '" . $JournalNo . "';";
                        }
                        $index++;
                        
                }
                $debit += abs($GL[$i]["fcdebit"]);
                $credit += abs($GL[$i]["fccredit"]);
        }

        // Insert GL Header
        if (($flag) && ($jmlArray>0)) {
                $strSQL[] = "insert into txgeneral_journal_hdr (ftbranch_code, ftJournal_No, fdTrans_Date, ftDocument_No, ftsource, fcDebit, fcCredit, fntype, fnStatus, ftCreated_User, fdCreated_Date, ftModified_User, fdModified_Date) values ('" . $_SESSION['strbranch'] . "' , '" . $JournalNo . "', '" . $transdate . "', '" . $documentno . "', '" . $source . "', " . $debit . ", " . $credit . ", '0', '1', '" . $_SESSION["username"] . "', NOW(), '" . $_SESSION["username"] . "', NOW());";
                pr("GL:insert into txgeneral_journal_hdr (ftbranch_code, ftJournal_No, fdTrans_Date, ftDocument_No, ftsource, fcDebit, fcCredit, fntype, fnStatus, ftCreated_User, fdCreated_Date, ftModified_User, fdModified_Date) values ('" . $_SESSION['strbranch'] . "' , '" . $JournalNo . "', '" . $transdate . "', '" . $documentno . "', '" . $source . "', " . $debit . ", " . $credit . ", '0', '1', '" . $_SESSION["username"] . "', NOW(), '" . $_SESSION["username"] . "', NOW());");
        }
        pr("END GL");
        //die;
        return $strSQL;
    }

    // Simpan & Insert GL
    function ProcessGL() {
        $strSQL=array();
        // pr ("FORM : " . $_REQUEST['form']); 
       
        switch($_REQUEST['form']) {
            // module saldo awal
            // Tidak ada jurnal karena nominalnya harusnya sudah termasuk dalam neraca awal

            // module penjualan
            case "sales_invoice":
                if ((!$_SESSION["SOAutoDOPrint"] && !$_SESSION["SOAutoDOSave"])) {
                    // Section Insert GL
                    // Header Sales Invoice (Piutang) - Sisi Debit
                    $DocumentNo = $_REQUEST['ftsales_invoice_no'];
                    $ftcustomer_name = RetRstField("SELECT ftcustomer_name FROM tlcustomer WHERE ftcustomer_code = '{$_REQUEST['ftcustomer_code']}'");

                    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F04'");

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                    "fcdebit"  => CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']),
                                    "fccredit"  => 0 );

                    // Detail Sales Invoice (Discount) - Sisi Debit
                    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S02'");

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                    "fcdebit"  => CCur($_REQUEST['fcdiscount'])*CCur($_REQUEST['fcrate']),
                                    "fccredit"  => 0);

                    // Detail Sales Invoice (Penjualan) - Sisi Kredit
                    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S01'");

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                    "fcdebit"  => 0,
                                    "fccredit"  => CCur($_REQUEST['fcsub_amount'])*CCur($_REQUEST['fcrate']));

                    // Detail Sales Invoice (Biaya Lain) - Sisi Kredit
                    if (CCur($_REQUEST["fcother_cost"]) > 0) {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S07'");
                            $debit = 0;
                            $credit = CCur($_REQUEST['fcother_cost'])*CCur($_REQUEST['fcrate']);
                    } else {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S08'");
                            $debit = CCur($_REQUEST['fcother_cost'])*CCur($_REQUEST['fcrate']);
                            $credit = 0;
                    }

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);

                    // Detail Sales Invoice (Pajak) - Sisi Kredit
                    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S03'");

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                    "fcdebit"  => 0,
                                    "fccredit"  => CCur($_REQUEST['fcvat'])*CCur($_REQUEST['fcrate']));

                    // Create SI Auto create DO
                    if ($_SESSION["SIAutoDO"]) {
                        // Detail Persediaan Barang
                        $ftstock_out_no = RetRstField("SELECT b.ftstock_out_no FROM txsales_invoice_hdr a INNER JOIN txstock_out_hdr b ON b.ftdocument_no = a.ftdo_no WHERE ftsales_invoice_no = '{$_REQUEST["ftsales_invoice_no"]}'");
                        for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                            $fccogs = GetFIFOValue($_SESSION['strbranch'], $ftstock_out_no, $_REQUEST['ftitem_codeD0'][$i],"",$_REQUEST['fnindexD0'][$i]);

                            $debit = CCur($fccogs);
                            $credit = CCur($fccogs);

                            // Sisi Debit
                            $ftcoa_code = RetRstField("SELECT ftgl_cogs_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                            $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                        "fcdebit"  => $debit,
                                        "fccredit" => 0);

                            // Sisi Kredit
                            $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                            $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                        "fcdebit"  => 0,
                                        "fccredit" => $credit );
                        }
                    }
                }
                break; 

            case "sales_order":
                # SO auto create invoice dan DO            
                if (($_SESSION["SOAutoDOPrint"] || $_SESSION["SOAutoDOSave"])) {
        
                    if ($_REQUEST['mode']=="final" || ($_REQUEST['mode']=="simpan_revisi" && $_REQUEST["fnstatus"]==2) ) {
                        // Section Insert GL
                        // Header Sales Invoice (Piutang) - Sisi Debit
                        $DocumentNo = RetRstField("SELECT ftsales_invoice_no FROM txsales_invoice_hdr WHERE ftso_no = '{$_REQUEST["ftso_no"]}'");
                        $ftcustomer_name = RetRstField("SELECT ftcustomer_name FROM tlcustomer WHERE ftcustomer_code = '{$_REQUEST['ftcustomer_code']}'");

                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F04'");

                        $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                        "fcdebit"  => CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']),
                                        "fccredit"  => 0 );

                        // Detail Sales Invoice (Discount) - Sisi Debit
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S02'");

                        $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                        "fcdebit"  => CCur($_REQUEST['fcdiscount'])*CCur($_REQUEST['fcrate']),
                                        "fccredit"  => 0);

                        // Detail Sales Invoice (Penjualan) - Sisi Kredit
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S01'");

                        $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                        "fcdebit"  => 0,
                                        "fccredit"  => CCur($_REQUEST['fcsub_amount'])*CCur($_REQUEST['fcrate']));

                        // Detail Sales Invoice (Biaya Lain) - Sisi Kredit
                        if (CCur($_REQUEST["fcother_cost"]) > 0) {
                                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S07'");
                                $debit = 0;
                                $credit = CCur($_REQUEST['fcother_cost'])*CCur($_REQUEST['fcrate']);
                        } else {
                                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S08'");
                                $debit = CCur($_REQUEST['fcother_cost'])*CCur($_REQUEST['fcrate']);
                                $credit = 0;
                        }

                        $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                        "fcdebit"  => $debit,
                                        "fccredit"  => $credit);

                        // Detail Sales Invoice (Pajak) - Sisi Kredit
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S03'");

                        $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                        "fcdebit"  => 0,
                                        "fccredit"  => CCur($_REQUEST['fcvat'])*CCur($_REQUEST['fcrate']));

                        // Detail Persediaan Barang
                        $ftstock_out_no = RetRstField("SELECT b.ftstock_out_no FROM txdelivery_order_hdr a INNER JOIN txstock_out_hdr b ON b.ftdocument_no = a.ftdo_no WHERE ftso_no = '{$_REQUEST["ftso_no"]}'");
                        for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                            $fccogs = GetFIFOValue($_SESSION['strbranch'], $ftstock_out_no, $_REQUEST['ftitem_codeD0'][$i],"",$_REQUEST['fnindexD0'][$i]);

                            $debit = CCur($fccogs);
                            $credit = CCur($fccogs);

                            // Sisi Debit
                            $ftcoa_code = RetRstField("SELECT ftgl_cogs_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                            $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                        "fcdebit"  => $debit,
                                        "fccredit" => 0);

                            // Sisi Kredit
                            $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                            $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                        "fcdebit"  => 0,
                                        "fccredit" => $credit );
                        }
                    }    
                }    
                break;
            
            case "delivery_order":
                pr("mode:".$_REQUEST['mode']);
                if ($_REQUEST['mode']=="final") {
                    // Section Insert GL
                    $DocumentNo = $_REQUEST['ftdo_no'];
                   
                    // Detail Delivery Order
                    for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                        $fccogs = GetFIFOValue($_SESSION['strbranch'], $DocumentNo, $_REQUEST['ftitem_codeD0'][$i],"",$_REQUEST['fnindexD0'][$i]);
                        
                        $debit = CCur($fccogs);
                        $credit = CCur($fccogs);

                        // Sisi Debit
                        $ftcoa_code = RetRstField("SELECT ftgl_cogs_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                        $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                    "fcdebit"  => $debit,
                                    "fccredit" => 0);

                        // Sisi Kredit
                        $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                        $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                    "fcdebit"  => 0,
                                    "fccredit" => $credit );
                    }
                    
                    // Section Insert GL - Auto Sales Invoice
                    // Header Sales Invoice (Piutang) - Sisi Debit
                    if ($fntax_code == 0) {            
                        $fttrans_codeSI = RetRstField("SELECT ftcode FROM tstransaction_number WHERE ftmodule_code = 'TN006'");
                    } else if ($fntax_code == 1) {  
                        $fttrans_codeSI = RetRstField("SELECT ftcode FROM tstransaction_number WHERE ftmodule_code = 'TN021'");
                    } else if ($fntax_code == 2) {  
                        $fttrans_codeSI = RetRstField("SELECT ftcode FROM tstransaction_number WHERE ftmodule_code = 'TN022'");
                    }
                    // penomoran sales invoice = delivery order, kecuali prefix nya.
                    $fttrans_codeDO = RetRstField("SELECT ftcode FROM tstransaction_number WHERE ftmodule_code = 'TN005'");
                    $DocumentNo = str_replace($fttrans_codeDO, $fttrans_codeSI, $_REQUEST["ftdo_no"]);

                    $ftcustomer_name = RetRstField("SELECT ftcustomer_name FROM tlcustomer WHERE ftcustomer_code = '{$_REQUEST['ftcustomer_code']}'");

                    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F04'");

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                    "fcdebit"  => CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']),
                                    "fccredit"  => 0 );

                    // Detail Sales Invoice (Discount) - Sisi Debit
                    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S02'");

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                    "fcdebit"  => CCur($_REQUEST['fcdiscount'])*CCur($_REQUEST['fcrate']),
                                    "fccredit"  => 0);
                    
                    // Detail Sales Invoice (Penjualan) - Sisi Kredit
                    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S01'");

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                    "fcdebit"  => 0,
                                    "fccredit"  => CCur($_REQUEST['fcsub_amount'])*CCur($_REQUEST['fcrate']));
                    
                    // Detail Sales Invoice (Biaya Lain) - Sisi Kredit
                    if (CCur($_REQUEST["fcother_cost"]) > 0) {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S07'");
                            $debit = 0;
                            $credit = CCur($_REQUEST['fcother_cost'])*CCur($_REQUEST['fcrate']);
                    } else {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S08'");
                            $debit = CCur($_REQUEST['fcother_cost'])*CCur($_REQUEST['fcrate']);
                            $credit = 0;
                    }
                   
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);
                   
                    // Detail Sales Invoice (Pajak) - Sisi Kredit
                    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S03'");

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                    "fcdebit"  => 0,
                                    "fccredit"  => CCur($_REQUEST['fcvat'])*CCur($_REQUEST['fcrate']));
                 
                } else {
                    return $strSQL;
                }
            break;

            case "sales_return":
                // Section Insert GL
                // Header Sales Return (Retur) - Sisi Debit
                $DocumentNo = $_REQUEST['ftsales_return_no'];
                $ftcustomer_name = RetRstField("SELECT ftcustomer_name FROM tlcustomer WHERE ftcustomer_code = '{$_REQUEST['ftcustomer_code']}'");
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S05'");
                $fcrate = RetRstField("SELECT fcrate FROM txsales_order_hdr WHERE ftso_no = '{$_REQUEST['ftso_no']}'");
                if ($fcrate == "") { $fcrate = 1; }
                
                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => CCur($_REQUEST['fcsub_amount'])*CCur($fcrate),
                                "fccredit"  => 0);

                // Header Sales Return (Biaya Lain) - Sisi Debit
                if (CCur($_REQUEST["fcother_cost"]) > 0) {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S07'");
                        $debit = CCur($_REQUEST['fcother_cost'])*CCur($fcrate);
                        $credit = 0;
                } else {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S08'");
                        $debit = 0;
                        $credit = CCur($_REQUEST['fcother_cost'])*CCur($fcrate);
                }

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $debit,
                                "fccredit"  => $credit);

                // Header Sales Return (Pajak) - Sisi Debit
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S03'");

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => CCur($_REQUEST['fcvat'])*CCur($fcrate),
                                "fccredit"  => 0);

                // Header Sales Return (Piutang) - Sisi Kredit
                
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F04'");

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit"  => CCur($_REQUEST['fcamount'])*CCur($fcrate) );

                // Detail Sales Return (Discount) - Sisi Kredit
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S02'");

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit"  => CCur($_REQUEST['fcdiscount'])*CCur($fcrate));

                // GL Barang Masuk
                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                    $fccogs = GetFIFOValue($_SESSION['strbranch'], $DocumentNo, $_REQUEST['ftitem_codeD0'][$i],"",$_REQUEST['fnindexD0'][$i]);

                    $debit = CCur($fccogs);
                    $credit = CCur($fccogs);

                    // Sisi Debit
                    $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $debit,
                                "fccredit" => 0 );

                    // Sisi Kredit
                    $ftcoa_code = RetRstField("SELECT ftgl_cogs_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit" => $credit);                    
                }
                
            break;
            
            case "sales_invoice_adjustment":
                // Section Insert GL
                // Header Sales Invoice (Piutang) - Sisi Debit
                $DocumentNo = RetRstField("SELECT ftsales_invoice_no FROM txsales_invoice_adjustment_hdr WHERE ftsales_invoice_no = '{$_REQUEST["ftsales_invoice_no"]}'");
                $ftcustomer_name = RetRstField("SELECT ftcustomer_name FROM tlcustomer WHERE ftcustomer_code = '{$_REQUEST['ftcustomer_code']}'");

                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F04'");

                if (CCur($_REQUEST['fcamount']) > 0) {
                    $fcdebit = CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']);
                    $fccredit = 0;
                } else  {
                    $fcdebit = 0;
                    $fccredit = CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']);
                }
                    
                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $fcdebit,
                                "fccredit"  => $fccredit );

                // Detail Sales Invoice (Discount) - Sisi Debit
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S02'");

                if (CCur($_REQUEST['fcdiscount']) > 0) {
                    $fcdebit = CCur($_REQUEST['fcdiscount'])*CCur($_REQUEST['fcrate']);
                    $fccredit = 0;
                } else  {
                    $fcdebit = 0;
                    $fccredit = CCur($_REQUEST['fcdiscount'])*CCur($_REQUEST['fcrate']);
                }
                
                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $fcdebit,
                                "fccredit"  => $fccredit );

                // Detail Sales Invoice (Penjualan) - Sisi Kredit
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S01'");

                if (CCur($_REQUEST['fcsub_amount']) > 0) {
                    $fcdebit = 0;
                    $fccredit = CCur($_REQUEST['fcsub_amount'])*CCur($_REQUEST['fcrate']);
                } else  {
                    $fcdebit = CCur($_REQUEST['fcsub_amount'])*CCur($_REQUEST['fcrate']);
                    $fccredit = 0;
                }
                
                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $fcdebit,
                                "fccredit"  => $fccredit );

                // Detail Sales Invoice (Biaya Lain) - Sisi Kredit
                if (CCur($_REQUEST["fcother_cost"]) > 0) {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S07'");
                        $debit = 0;
                        $credit = CCur($_REQUEST['fcother_cost'])*CCur($_REQUEST['fcrate']);
                } else {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S08'");
                        $debit = CCur($_REQUEST['fcother_cost'])*CCur($_REQUEST['fcrate']);
                        $credit = 0;
                }

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $debit,
                                "fccredit"  => $credit);

                // Detail Sales Invoice (Pajak) - Sisi Kredit
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S03'");

                  if (CCur($_REQUEST['fcvat']) > 0) {
                    $fcdebit = 0;
                    $fccredit = CCur($_REQUEST['fcvat'])*CCur($_REQUEST['fcrate']);
                } else  {
                    $fcdebit = CCur($_REQUEST['fcvat'])*CCur($_REQUEST['fcrate']);
                    $fccredit = 0;
                }
                
                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftnotes'],
                               "fcdebit"  => $fcdebit,
                                "fccredit"  => $fccredit );

                break;            

            // module pembelian
            // pembelian stock masuk (terima barang auto purchase invoice)
            case "pembelian_stock_masuk":
                // Section Insert GL
                $ftpurchase_invoice_no = RetRstField("SELECT ftpurchase_invoice_no FROM txpurchase_invoice_hdr WHERE ftstock_in_no = '{$_REQUEST['ftstock_in_no']}' AND fnstatus = 1 ");
                $fcsub_amount = RetRstField("SELECT ROUND(SUM(fcsub_amount * fcrate),2) FROM txpurchase_invoice_hdr WHERE ftstock_in_no = '{$_REQUEST['ftstock_in_no']}' AND fnstatus = 1 ");
                $fcamount = RetRstField("SELECT ROUND(SUM(fcamount * fcrate),2) FROM txpurchase_invoice_hdr WHERE ftstock_in_no = '{$_REQUEST['ftstock_in_no']}' AND fnstatus = 1 ");
                $fcother_cost = RetRstField("SELECT ROUND(SUM(fcother_cost * fcrate),2) FROM txpurchase_invoice_hdr WHERE ftstock_in_no = '{$_REQUEST['ftstock_in_no']}' AND fnstatus = 1 ");
                $fcvat = RetRstField("SELECT ROUND(SUM(fcvat * fcrate),2) FROM txpurchase_invoice_hdr WHERE ftstock_in_no = '{$_REQUEST['ftstock_in_no']}' AND fnstatus = 1 ");
                $fcdiscount = RetRstField("SELECT ROUND(SUM(fcdiscount * fcrate),2) FROM txpurchase_invoice_hdr WHERE ftstock_in_no = '{$_REQUEST['ftstock_in_no']}' AND fnstatus = 1 ");
                
                $DocumentNo = $ftpurchase_invoice_no;
                
                $ftsupplier_name = RetRstField("SELECT ftsupplier_name FROM tlsupplier WHERE ftsupplier_code = '{$_REQUEST['ftstock_in_company']}'");
                    
                // Header Purchase Invoice (Pembelian)  - Sisi Debit
                // Persediaan from Detail Barang Masuk
                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                    $debit = CCur($_REQUEST['fcpriceD0'][$i]) * CCur($_REQUEST['fnqty_baseD0'][$i]);
                    $ftstock_category_code = RetRstField("SELECT ftstock_category_code FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}' ");
                    
                    // Sisi Debit
                    if ($ftstock_category_code == "inv") {
                        // Barang Stock
                        $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                    } else {
                        // Barang Non Stock / Biaya / Asset
                        $ftcoa_code = RetRstField("SELECT ftgl_cogs_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                    }
                        
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $debit,
                                "fccredit"  => 0 );
                    
                }
                
                // Purchase Invoice (Biaya Lain) - Sisi Debit
                if ($fcother_cost > 0) {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P07'");
                        $debit = $fcother_cost;
                        $credit = 0;
                } else {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P08'");
                        $debit = 0;
                        $credit = $fcother_cost;
                }

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $debit,
                                "fccredit"  => $credit);
                   
                // Purchase Invoice (Pajak) - Sisi Debit
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P03'");

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $fcvat,
                                "fccredit"  => 0);
                    
                 // Purchase Invoice (Hutang) - Sisi Credit

                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F01'");
                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit"  => $fcamount);
                    
                // Purchase Invoice (Discount) - Sisi Credit
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P02'");
                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit"  => $fcdiscount);
                    
                // Purchase Invoice (Biaya Ekspedisi) - Sisi Credit
                /*$GL[] = array( "ftcoa_code" => $_REQUEST['ftexpedition_code'],
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit"  => CCur($_REQUEST['fcexpedition_cost'])*CCur($_REQUEST['fcrate'])); */

                break;

            
            // purchase invoice
            case "pembelian_purchase_invoice":
            case "pembelian_purchase_invoice_local":
            case "pembelian_purchase_invoice_import":
                // Section Insert GL
                $DocumentNo = $_REQUEST['ftpurchase_invoice_no'];
                $ftsupplier_name = RetRstField("SELECT ftsupplier_name FROM tlsupplier WHERE ftsupplier_code = '{$_REQUEST['ftsupplier_code']}'");
                    
                // Header Purchase Invoice (Pembelian)  - Sisi Debit
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P01'");

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => (CCur($_REQUEST['fcsub_amount'])+CCur($_REQUEST['fcexpedition_cost']))*CCur($_REQUEST['fcrate']),
                                "fccredit"  => 0);

                // Purchase Invoice (Biaya Lain) - Sisi Debit
                if (CCur($_REQUEST["fcother_cost"]) > 0) {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P07'");
                        $debit = CCur($_REQUEST['fcother_cost'])*CCur($_REQUEST['fcrate']);
                        $credit = 0;
                } else {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P08'");
                        $debit = 0;
                        $credit = CCur($_REQUEST['fcother_cost'])*CCur($_REQUEST['fcrate']);
                }

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $debit,
                                "fccredit"  => $credit);

                // Purchase Invoice (Pajak) - Sisi Debit
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P03'");

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => CCur($_REQUEST['fcvat'])*CCur($_REQUEST['fcrate']),
                                "fccredit"  => 0);

                 // Purchase Invoice (Hutang) - Sisi Credit

                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F01'");
                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit"  => CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']));

                // Purchase Invoice (Discount) - Sisi Credit
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P02'");
                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit"  => CCur($_REQUEST['fcdiscount'])*CCur($_REQUEST['fcrate']));

                // Purchase Invoice (Biaya Ekspedisi) - Sisi Credit
                $GL[] = array( "ftcoa_code" => $_REQUEST['ftexpedition_code'],
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit"  => CCur($_REQUEST['fcexpedition_cost'])*CCur($_REQUEST['fcrate']));

                // COGS
                // Detail Barang Masuk
                $ftstock_type_code = RetRstField("SELECT ftstock_type_code FROM txstock_in_hdr WHERE ftcategory_code = 0 AND ftstock_in_no = '{$_REQUEST['ftstock_in_no']}' ");

                // initialize variable for multiple tables
                $tablename = "";
                if($ftstock_type_code=='invbb') {
                    $tablename = "_bahan_baku";
                } elseif ($ftstock_type_code=='invsj') {
                    $tablename = "_setengah_jadi";
                } elseif ($ftstock_type_code=='invsp') {
                    $tablename = "_sparepart";
                } else {
                    $tablename = "";
                }

                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {

                    $debit = CCur($_REQUEST['fcprice_cogsD0'][$i])*CCur($_REQUEST['fnqty_baseD0'][$i]);
                    $credit = CCur($_REQUEST['fcprice_cogsD0'][$i])*CCur($_REQUEST['fnqty_baseD0'][$i]);

                    // Sisi Debit
                    if ($ftstock_type_code == "") {
                        // Barang Non Stock / Biaya / Asset
                        $ftcoa_code = RetRstField("SELECT ftgl_cogs_acc FROM tlstock_hdr{$tablename} WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                    } else {
                        // Barang Stock
                        $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr{$tablename} WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                    }
                        
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $debit,
                                "fccredit"  => 0 );

                    // Sisi Kredit
                    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P01'");
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit"  => $credit );
                }

            break;

            // Purchase Return
            case "return_pembelian":
            case "return_pembelian_local":
            case "return_pembelian_import":
                // Section Insert GL
                $DocumentNo = $_REQUEST['ftpurchase_return_no'];
                $ftsupplier_name = RetRstField("SELECT ftsupplier_name FROM tlsupplier WHERE ftsupplier_code = '{$_REQUEST['ftsupplier_code']}'");

                // Purchase Return (Hutang) - Sisi Debit
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F01'");

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']),
                                "fccredit"  => 0 );

                // Purchase Return (Discount) - Sisi Debit
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P02'");

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => CCur($_REQUEST['fcdiscount'])*CCur($_REQUEST['fcrate']),
                                "fccredit"  => 0);

                // Header Purchase Return (Pembelian) - Sisi Kredit
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P06'");

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit"  => CCur($_REQUEST['fcsub_amount'])*CCur($_REQUEST['fcrate']));

                // Purchase Return (Biaya Lain) - Sisi Kredit
                if (CCur($_REQUEST["fcother_cost"]) > 0) {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P07'");
                        $debit = 0;
                        $credit = CCur($_REQUEST['fcother_cost'])*CCur($_REQUEST['fcrate']);
                } else {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P08'");
                        $debit = CCur($_REQUEST['fcother_cost'])*CCur($_REQUEST['fcrate']);
                        $credit = 0;
                }

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $debit,
                                "fccredit"  => $credit);

                // Purchase Return (Pajak) - Sisi Kredit
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P03'");

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit"  => CCur($_REQUEST['fcvat'])*CCur($_REQUEST['fcrate']));

                // COGS
                // Detail Barang Masuk
                $ftstock_type_code = RetRstField("SELECT a. ftstock_type_code FROM txstock_in_hdr a INNER JOIN txpurchase_invoice_hdr b on a.ftstock_in_no = b.ftstock_in_no WHERE a.ftcategory_code = 0 AND b.ftpurchase_invoice_no = '{$_REQUEST['ftpurchase_invoice_no']}' ");

                // initialize variable for multiple tables
                $tablename = "";
                if($ftstock_type_code=='invbb') {
                    $tablename = "_bahan_baku";
                } elseif ($ftstock_type_code=='invsj') {
                    $tablename = "_setengah_jadi";
                } elseif ($ftstock_type_code=='invsp') {
                    $tablename = "_sparepart";
                } else {
                    $tablename = "";
                }

                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                    $fccogs = GetFIFOValue($_SESSION['strbranch'], $DocumentNo, $_REQUEST['ftitem_codeD0'][$i],"",$_REQUEST['fnindexD0'][$i]);

                    $debit = CCur($fccogs);
                    $credit = CCur($fccogs);

                     // Sisi Debit
                    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P01'");
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $debit,
                                "fccredit"  => 0 );

                    // Sisi Kredit
                    if ($ftstock_type_code == "") {
                        // Barang Non Stock / Biaya / Asset
                        $ftcoa_code = RetRstField("SELECT ftgl_cogs_acc FROM tlstock_hdr{$tablename} WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                    } else {
                        // Barang Stock
                        $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr{$tablename} WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                    }

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit"  => $credit);
                }

            break;

            // module inventory
            case "inventorybb_stock_masuk":
            case "inventorysj_barang_masuk":
            case "inventorysp_barang_masuk":
            case "barang_masuk":
                // Section Insert GL
                // initialize variable for multiple tables
                $tablename = "";
                if($_REQUEST['form']=='inventorybb_stock_masuk') {
                    $tablename = "_bahan_baku";
                } elseif ($_REQUEST['form']=='inventorysj_barang_masuk') {
                    $tablename = "_setengah_jadi";
                } elseif ($_REQUEST['form']=='inventorysp_barang_masuk') {
                    $tablename = "_sparepart";
                } elseif ($_REQUEST['form']=='barang_masuk') {
                    $tablename = "";
                }

                $DocumentNo = $_REQUEST['ftstock_in_no'];

                // Detail Barang Masuk
                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                    $fccogs = GetFIFOValue($_SESSION['strbranch'], $DocumentNo, $_REQUEST['ftitem_codeD0'][$i],"",$_REQUEST['fnindexD0'][$i]);
                    
                    $debit = CCur($fccogs);
                    $credit = CCur($fccogs);
                    
                    // Sisi Debit
                    $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr{$tablename} WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $debit,
                                "fccredit"  => 0 );

                    // Sisi Kredit
                    // Kategori Free
                    if ($_REQUEST['ftcategory_code'] == 1) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB11'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ11'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP11'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I11'");
                        }
                    // Kategori Sample
                    } elseif ($_REQUEST['ftcategory_code'] == 2) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB12'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ12'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP12'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I12'");
                        }
                    // Kategori Pinjaman
                    } elseif ($_REQUEST['ftcategory_code'] == 3) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB13'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ13'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP13'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I13'");
                        }
                    // Kategori Tukar Barang
                    } elseif (($_REQUEST['ftcategory_code'] == 5)) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB16'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ16'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP16'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I16'");
                        }    
                    // Kategori Hasil Outsource 
                    } elseif (($_REQUEST['ftcategory_code'] == 6)) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB14'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ14'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP14'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I14'");
                        }
                     // Kategori Retur Outsource 
                    } elseif (($_REQUEST['ftcategory_code'] == 7)) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB25'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ25'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP25'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I25'");
                        }
                        
                    // Kategori Hasil Produksi & Penguraian 
                    } elseif (($_REQUEST['ftcategory_code'] == 8) || ($_REQUEST['ftcategory_code'] == 11)) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB15'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ15'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP15'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I15'");
                        }
                    // Kategori Retur Produksi
                    } elseif (($_REQUEST['ftcategory_code'] == 9)) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB27'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ27'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP27'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I27'");
                        }
                    // Kategori Penambahan
                    } elseif (($_REQUEST['ftcategory_code'] == 10)) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB03'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ03'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP03'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I03'");
                        }
                    // Kategori Retur Pemakaian
                    } elseif ($_REQUEST['ftcategory_code'] == 12) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB21'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ21'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP21'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I21'");
                        }
                        
                    // Kategori Retur Tanpa Invoice belum dibuat 
                    /*    
                    } elseif ($_REQUEST['ftcategory_code'] == 13) {
                        $ftcoa_code = RetRstField("SELECT ftgl_cogs_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                    */
                    } 
                    
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit"  => $credit );
                }


                break; 

            case "inventorybb_stock_keluar":
            case "inventorysj_barang_keluar":
            case "inventorysp_barang_keluar":
            case "barang_keluar":
                // Section Insert GL
                // initialize variable for multiple tables
                $tablename = "";
                if($_REQUEST['form']=='inventorybb_stock_keluar') {
                    $tablename = "_bahan_baku";
                } elseif ($_REQUEST['form']=='inventorysj_barang_keluar') {
                    $tablename = "_setengah_jadi";
                } elseif ($_REQUEST['form']=='inventorysp_barang_keluar') {
                    $tablename = "_sparepart";
                } elseif ($_REQUEST['form']=='barang_keluar') {
                    $tablename = "";
                }

                $DocumentNo = $_REQUEST['ftstock_out_no'];

                // Detail Barang Keluar
                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                    $fccogs = GetFIFOValue($_SESSION['strbranch'], $DocumentNo, $_REQUEST['ftitem_codeD0'][$i],"",$_REQUEST['fnindexD0'][$i]);

                    $debit = CCur($fccogs);
                    $credit = CCur($fccogs);
                    pr($fccogs);
                    pr("debit:".$debit);
                    //die();
                    // Sisi Debit
                    // Kategori Free
                    if ($_REQUEST['ftcategory_code'] == 1) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB22'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ22'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP22'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I22'");
                        }
                    // Kategori Sample
                    } elseif ($_REQUEST['ftcategory_code'] == 2) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB23'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ23'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP23'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I23'");
                        }
                    // Kategori Pinjaman
                    } elseif ($_REQUEST['ftcategory_code'] == 3) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB24'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ24'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP24'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I24'");
                        }
                    // Kategori Tukar Barang
                    } elseif ($_REQUEST['ftcategory_code'] == 5) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB26'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ26'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP26'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I26'");
                        }
                    // Kategori outsource
                    } elseif ($_REQUEST['ftcategory_code'] == 6) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB25'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ25'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP25'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I25'");
                        }
                    // Kategori Pemakaian
                    } elseif ($_REQUEST['ftcategory_code'] == 7) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB21'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ21'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP21'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I21'");
                        }
                    // Kategori Produksi
                    } elseif ($_REQUEST['ftcategory_code'] == 8) {
                        if ($_REQUEST['ftstock_type_code'] == 'invbb') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB27'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsj') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ27'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'invsp') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP27'");
                        } elseif ($_REQUEST['ftstock_type_code'] == 'inv') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I27'");
                        }
                    // Kategori Ganti Rugi & Pengurangan
                    } elseif (($_REQUEST['ftcategory_code'] == 9) || ($_REQUEST['ftcategory_code'] == 10)) {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I02'");
                    }

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $debit,
                                "fccredit" => 0);

                    // Sisi Kredit
                    $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr{$tablename} WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit" => $credit );
                }

            break;
            
            case "inventorybb_stock_mutasi_barang_jadi":
                // Section Insert GL
                $DocumentNo = $_REQUEST['ftstock_mutation_no'];

                // Detail Mutasi Ke Barang Jadi
                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                    $fccogs = GetFIFOValue($_SESSION['strbranch'], $DocumentNo, $_REQUEST['ftitem_codeD0'][$i],"txstock_mutation_det_bbtobj",$_REQUEST['fnindexD0'][$i]);

                    $debit = CCur($fccogs);
                    $credit = CCur($fccogs);

                    // Sisi Debit
                    $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $debit,
                                "fccredit" => 0);

                    // Sisi Kredit
                    $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr_bahan_baku WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit" => $credit );
                }

            break;

            case "inventorybb_stock_penyesuaian":
            case "inventorysj_stock_penyesuaian":
            case "inventorysp_stock_penyesuaian":
            case "penyesuaian_barang":
                if ($_REQUEST['mode']=="final") {
                    // Section Insert GL
                    // initialize variable for multiple tables
                    $tablename = "";
                    if($_REQUEST['form']=='inventorybb_stock_penyesuaian') {
                        $tablename = "_bahan_baku";
                    } elseif ($_REQUEST['form']=='inventorysj_stock_penyesuaian') {
                        $tablename = "_setengah_jadi";
                    } elseif ($_REQUEST['form']=='inventorysp_stock_penyesuaian') {
                        $tablename = "_sparepart";
                    } elseif ($_REQUEST['form']=='penyesuaian_barang') {
                        $tablename = "";
                    }

                    $DocumentNo = $_REQUEST['ftstock_adjustment_no'];

                    // Detail Stock Penyesuaian
                    for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                        $fccogs = GetFIFOValue($_SESSION['strbranch'], $DocumentNo, $_REQUEST['ftitem_codeD0'][$i],"",$_REQUEST['fnindexD0'][$i]);

                        $debit = CCur($fccogs);
                        $credit = CCur($fccogs);

                        // Stock Barang Berkurang
                        if ($_REQUEST['fnqty_baseD0'][$i] < 0) {
                            // Sisi Debit
                            if ($_REQUEST['form'] == 'inventorybb_stock_penyesuaian') {
                                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB02'");
                            } elseif ($_REQUEST['form'] == 'inventorysj_stock_penyesuaian') {
                                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ02'");
                            } elseif ($_REQUEST['form'] == 'inventorysp_stock_penyesuaian') {
                                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP02'");
                            } elseif ($_REQUEST['form'] == 'penyesuaian_barang') {
                                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I02'");
                            }

                            $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                        "fcdebit"  => $debit,
                                        "fccredit" => 0);

                            // Sisi Kredit
                            $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr{$tablename} WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                            $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                        "fcdebit"  => 0,
                                        "fccredit" => $credit );
                        }
                        // Stock Barang Bertambah
                        elseif ($_REQUEST['fnqty_baseD0'][$i] > 0) {
                            // Sisi Debit
                            $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr{$tablename} WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                            $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                        "fcdebit"  => $debit,
                                        "fccredit" => 0 );

                            // Sisi Kredit
                            if ($_REQUEST['form'] == 'inventorybb_stock_penyesuaian') {
                                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'IBB03'");
                            } elseif ($_REQUEST['form'] == 'inventorysj_stock_penyesuaian') {
                                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISJ03'");
                            } elseif ($_REQUEST['form'] == 'inventorysp_stock_penyesuaian') {
                                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'ISP03'");
                            } elseif ($_REQUEST['form'] == 'penyesuaian_barang') {
                                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I03'");
                            }

                            $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                        "fcdebit"  => 0,
                                        "fccredit" => $credit);

                        }
                    }
                } else {
                    return $strSQL;
                }
                break;
                
            case "assembly":
                // Section Insert GL
                $DocumentNo = $_REQUEST['ftassembly_no'];
                
                $fccogs = GetFIFOPriceValue($_SESSION['strbranch'], $DocumentNo, $_REQUEST['ftitem_code'],"");
                $debit = CCur($_REQUEST['fnqty']) * CCur($fccogs);
 
                // Sisi Debit (Barang Masuk)
                $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'");
                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                            "ftdescription" => $_REQUEST['ftitem_code'] . ' - ' . $_REQUEST['ftnotes'],
                            "fcdebit"  => $debit,
                            "fccredit"  => 0 );

                    
                // Detail Barang Keluar
                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                    // Sisi Credit
                    $fccogs = GetFIFOValue($_SESSION['strbranch'], $DocumentNo, $_REQUEST['ftitem_codeD0'][$i],"",$_REQUEST['fnindexD0'][$i]);
                    $credit = CCur($fccogs);
                    $credit_total = $credit_total + $credit;
                    
                    $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => 0,
                                "fccredit"  => $credit );
                }

                // Apabila ada selisih di persediaan masuk & Keluar
                if (($debit - $credit_total) > 0 ) {
                    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I15'");
                    $credit = $debit - $credit_total;
                    
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                               "ftdescription" => 'Selisih Nilai Assembly - ' . $_REQUEST['ftnotes'],
                               "fcdebit"  => 0,
                               "fccredit"  => $credit );
                } else {
                    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I21'");
                    $debit = $credit_total - $debit;
                    
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                               "ftdescription" => 'Biaya Pemakaian Assembly - ' . $_REQUEST['ftnotes'],
                               "fcdebit"  => $debit,
                               "fccredit"  => 0 );
                }

                break; 
    
            case "disassembly":
                // Section Insert GL
                $DocumentNo = $_REQUEST['ftdisassembly_no'];
                
                $fccogs = GetFIFOValue($_SESSION['strbranch'], $DocumentNo, $_REQUEST['ftitem_code'], "",$_REQUEST['fnindexD0'][$i]);
                $credit = $fccogs;
 
                // Sisi Credit (Barang Keluar)
                $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_code']}'");
                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                            "ftdescription" => $_REQUEST['ftitem_code'] . ' - ' . $_REQUEST['ftnotes'],
                            "fcdebit"  => 0,
                            "fccredit"  => $credit );
                    // $fccogs = GetFIFOPriceValue($_SESSION['strbranch'], $DocumentNo, $_REQUEST['ftitem_code'],"");
                
                // Detail Barang Masuk
                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                    // Sisi Debit
                    $fccogs = GetFIFOValue($_SESSION['strbranch'], $DocumentNo, $_REQUEST['ftitem_codeD0'][$i],"",$_REQUEST['fnindexD0'][$i]);
                    $debit = $fccogs;
                    $debit_total = $debit_total + $debit;
                    
                    $ftcoa_code = RetRstField("SELECT ftgl_inv_acc FROM tlstock_hdr WHERE ftitem_code = '{$_REQUEST['ftitem_codeD0'][$i]}'");
                
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftitem_codeD0'][$i] . ' - ' . $_REQUEST['ftnotes'],
                                "fcdebit"  => $debit,
                                "fccredit"  => 0 );
                }

                // Apabila ada selisih di persediaan masuk & Keluar
                if (($credit - $debit_total) > 0 ) {
                    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I21'");
                    $debit = $credit - $debit_total;
                    
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                               "ftdescription" => 'Biaya Pemakaian Disassembly - ' . $_REQUEST['ftnotes'],
                               "fcdebit"  => $debit,
                               "fccredit"  => 0 );
                    
                } else {
                      
                    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'I15'");
                    $credit = $debit_total - $credit;
                    
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                               "ftdescription" => 'Selisih Nilai Disassembly - ' . $_REQUEST['ftnotes'],
                               "fcdebit"  => 0,
                               "fccredit"  => $credit );
                }

                break;     
                
            // module keuangan
            case "kas_masuk":
                // Section Insert GL
                // Header Kas Masuk - Sisi Debit
                $DocumentNo = $_REQUEST['ftcashrcp_no'];

                /* $GL[] = array( "ftcoa_code" => $_REQUEST['ftacc_no1'],
                                "ftdescription" => js_esc($_REQUEST['ftreceive_name']) . ' - ' . js_esc($_REQUEST['ftremarks']),
                                "fcdebit"  => CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']),
                                "fccredit"  => 0 ); */

                $GL[] = array( "ftcoa_code" => $_REQUEST['ftacc_no1'],
                                "ftdescription" => js_esc($_REQUEST['ftremarks']),
                                "fcdebit"  => CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']),
                                "fccredit"  => 0 );

                // Detail Kas Masuk - Sisi Kredit
                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                    if (CCur($_REQUEST["fcamountD0"][$i]) > 0) {
                            $debit = 0;
                            $credit = CCur($_REQUEST['fcamountD0'][$i])*CCur($_REQUEST['fcrate']);
                    } else {
                            $debit = CCur($_REQUEST['fcamountD0'][$i])*CCur($_REQUEST['fcrate']);
                            $credit = 0;
                    }

                    /* if (js_esc($_REQUEST['ftreceive_name']) != "") {
                        $ftreceive_name = js_esc($_REQUEST['ftreceive_name']) . " - "; 
                    }
                        
                    $GL[] = array( "ftcoa_code" => $_REQUEST['ftcoa_codeD0'][$i],
                                    "ftdescription" => $ftreceive_name . js_esc($_REQUEST['ftdescriptionD0'][$i]),
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);
                    */
                    
                    $GL[] = array( "ftcoa_code" => $_REQUEST['ftcoa_codeD0'][$i],
                                    "ftdescription" => js_esc($_REQUEST['ftdescriptionD0'][$i]),
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);
                }

            break;
            case "kas_keluar":
                // Section Insert GL
                // Header Kas Keluar - Sisi Credit
                $DocumentNo = $_REQUEST['ftcashpym_no'];

                /* $GL[] = array( "ftcoa_code" => $_REQUEST['ftacc_no1'],
                                "ftdescription" => js_esc($_REQUEST['ftreceive_name']) . ' - ' . js_esc($_REQUEST['ftremarks']),
                                "fcdebit"  => 0,
                                "fccredit"  => CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate'])); */

                $GL[] = array( "ftcoa_code" => $_REQUEST['ftacc_no1'],
                                "ftdescription" => js_esc($_REQUEST['ftremarks']),
                                "fcdebit"  => 0,
                                "fccredit"  => CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']));

                // Detail Kas Keluar - Sisi Debit
                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                    if (CCur($_REQUEST["fcamountD0"][$i]) > 0) {
                            $debit = CCur($_REQUEST['fcamountD0'][$i])*CCur($_REQUEST['fcrate']);
                            $credit = 0;
                    } else {
                            $debit = 0;
                            $credit = CCur($_REQUEST['fcamountD0'][$i])*CCur($_REQUEST['fcrate']);

                    }
                    
                    /* if (js_esc($_REQUEST['ftreceive_name']) != "") {
                        $ftreceive_name = js_esc($_REQUEST['ftreceive_name']) . " - "; 
                    }

                    $GL[] = array( "ftcoa_code" => $_REQUEST['ftcoa_codeD0'][$i],
                                    "ftdescription" => $ftreceive_name . js_esc($_REQUEST['ftdescriptionD0'][$i]),
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit); */
                    
                    $GL[] = array( "ftcoa_code" => $_REQUEST['ftcoa_codeD0'][$i],
                                    "ftdescription" => js_esc($_REQUEST['ftdescriptionD0'][$i]),
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);
                }

            break;
            case "bank_masuk":
                // Section Insert GL
                // Header Bank Masuk - Sisi Debit
                $DocumentNo = $_REQUEST['ftbankrcp_no'];

                /* $GL[] = array( "ftcoa_code" => $_REQUEST['ftacc_no1'],
                                "ftdescription" => js_esc($_REQUEST['ftreceive_name']) . ' - ' . js_esc($_REQUEST['ftremarks']),
                                "fcdebit"  => CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']),
                                "fccredit"  => 0 ); */

                $GL[] = array( "ftcoa_code" => $_REQUEST['ftacc_no1'],
                                "ftdescription" => js_esc($_REQUEST['ftremarks']),
                                "fcdebit"  => CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']),
                                "fccredit"  => 0 );

                // Detail Bank Masuk - Sisi Kredit
                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                    if (CCur($_REQUEST["fcamountD0"][$i]) > 0) {
                            $debit = 0;
                            $credit = CCur($_REQUEST['fcamountD0'][$i])*CCur($_REQUEST['fcrate']);
                    } else {
                            $debit = CCur($_REQUEST['fcamountD0'][$i])*CCur($_REQUEST['fcrate']);
                            $credit = 0;
                    }

                    /* if (js_esc($_REQUEST['ftreceive_name']) != "") {
                        $ftreceive_name = js_esc($_REQUEST['ftreceive_name']) . " - "; 
                    }
                    
                    $GL[] = array( "ftcoa_code" => $_REQUEST['ftcoa_codeD0'][$i],
                                    "ftdescription" => $ftreceive_name . js_esc($_REQUEST['ftdescriptionD0'][$i]),
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);
                    */
                    
                    $GL[] = array( "ftcoa_code" => $_REQUEST['ftcoa_codeD0'][$i],
                                    "ftdescription" => js_esc($_REQUEST['ftdescriptionD0'][$i]),
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);
                     
                }
    
            break;
            case "bank_keluar":
                // Section Insert GL
                // Header Bank Keluar - Sisi Credit
                $DocumentNo = $_REQUEST['ftbankpym_no'];

                /* $GL[] = array( "ftcoa_code" => $_REQUEST['ftacc_no1'],
                                "ftdescription" => js_esc($_REQUEST['ftreceive_name']) . ' - ' . js_esc($_REQUEST['ftremarks']),
                                "fcdebit"  => 0,
                                "fccredit"  => CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate'])); */

                $GL[] = array( "ftcoa_code" => $_REQUEST['ftacc_no1'],
                                "ftdescription" => js_esc($_REQUEST['ftremarks']),
                                "fcdebit"  => 0,
                                "fccredit"  => CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']));

                
                // Detail Bank Keluar - Sisi Debit
                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                    if (CCur($_REQUEST["fcamountD0"][$i]) > 0) {
                            $debit = CCur($_REQUEST['fcamountD0'][$i])*CCur($_REQUEST['fcrate']);
                            $credit = 0;
                    } else {
                            $debit = 0;
                            $credit = CCur($_REQUEST['fcamountD0'][$i])*CCur($_REQUEST['fcrate']);

                    }

                    /* if (js_esc($_REQUEST['ftreceive_name']) != "") {
                        $ftreceive_name = js_esc($_REQUEST['ftreceive_name']) . " - "; 
                    }
                    
                    $GL[] = array( "ftcoa_code" => $_REQUEST['ftcoa_codeD0'][$i],
                                    "ftdescription" => $ftreceive_name . js_esc($_REQUEST['ftdescriptionD0'][$i]),
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);
                    */
                    
                    $GL[] = array( "ftcoa_code" => $_REQUEST['ftcoa_codeD0'][$i],
                                    "ftdescription" => js_esc($_REQUEST['ftdescriptionD0'][$i]),
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);
                }

            break;
            case "penerimaan_piutang":
                // Section Insert GL
                // Detail Penerimaan Piutang - Sisi Debit - Pembayaran
                $DocumentNo = $_REQUEST['ftar_no'];
                $ftcustomer_name = RetRstField("SELECT ftcustomer_name FROM tlcustomer WHERE ftcustomer_code = '{$_REQUEST['ftcustomer_code']}' ");

                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                    // amount
                    if (CCur($_REQUEST["fcamountD0"][$i]) > 0) {
                            $debit = CCur($_REQUEST['fcamountD0'][$i])*CCur($_REQUEST['fcrate']);
                            $credit = 0;
                    }


                    if (($_REQUEST['fnpayment_methodD0'][$i] == 0) || ($_REQUEST['fnpayment_methodD0'][$i] == 1)) {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F07'");
                    } else if ($_REQUEST['fnpayment_methodD0'][$i] == 9) {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F09'");
                    } else {
                            $ftcoa_code = $_REQUEST['ftacc_noD0'][$i];
                    }

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftremarks'],
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);

                    // Other Cost
                    if (CCur($_REQUEST["fcother_costD0"][$i]) != 0) {

                        if (CCur($_REQUEST["fcother_costD0"][$i]) > 0) {
                                $debit = CCur($_REQUEST['fcother_costD0'][$i])*CCur($_REQUEST['fcrate']);
                                $credit = 0;
                        } else  if (CCur($_REQUEST["fcother_costD0"][$i]) < 0) {
                                $debit = 0;
                                $credit = CCur($_REQUEST['fcother_costD0'][$i])*CCur($_REQUEST['fcrate']);
                        }

                        $ftcoa_code = $_REQUEST['ftother_coa_codeD0'][$i];

                        $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftremarks'],
                                        "fcdebit"  => $debit,
                                        "fccredit"  => $credit);
                    }
                }

                // Detail Penerimaan Piutang - Sisi Kredit - Sales Invoice
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F04'");

                for($i=0; $i<count($_REQUEST['fnindexD1']); $i++) {
                    if (CCur($_REQUEST["fcpayment_amount_baseD1"][$i]) > 0) {
                            $debit = 0;
                            $credit = CCur($_REQUEST['fcpayment_amount_baseD1'][$i]);
                    } else {
                            $debit = CCur($_REQUEST['fcpayment_amount_baseD1'][$i]);
                            $credit = 0;
                    }
                        
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                            "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftsales_invoice_noD1'][$i],
                            "fcdebit"  => $debit,
                            "fccredit"  => $credit);
                }

                // Detail Penerimaan Piutang - Biaya
                for($i=1; $i<count($_REQUEST['fnindexD3']); $i++) {
                    if (CCur($_REQUEST["fcamountD3"][$i]) != 0) {

                        if (CCur($_REQUEST["fcamountD3"][$i]) > 0) {
                                $debit = CCur($_REQUEST['fcamountD3'][$i])*CCur($_REQUEST['fcrate']);
                                $credit = 0;
                        } else {
                                $debit = 0;
                                $credit = CCur($_REQUEST['fcamountD3'][$i])*CCur($_REQUEST['fcrate']);

                        }

                        $GL[] = array( "ftcoa_code" => $_REQUEST['ftcoa_codeD3'][$i],
                                        "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftdescriptionD3'][$i],
                                        "fcdebit"  => $debit,
                                        "fccredit"  => $credit);
                
                    }
                }

                // Detail Penerimaan Piutang - Selisih Kurs
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F21'");

                if (CCur($_REQUEST["fcrate_adjustment"]) != 0) {
                    if (CCur($_REQUEST["fcrate_adjustment"]) > 0) {
                        $debit = CCur($_REQUEST['fcrate_adjustment']);
                        $credit = 0;
                    } else {
                        $debit = 0;
                        $credit = CCur($_REQUEST['fcrate_adjustment']);
                    }        

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $ftcustomer_name . ' - ' . $_REQUEST['ftremarks'],
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);
                }

            break;
            case "rekap_bank_voucher":
            case "rekap_bank_voucher_import":
                // Section Insert GL
                // Detail Pembayaran Hutang - Sisi Kredit - Pembayaran
                $DocumentNo = $_REQUEST['ftcvs_no'];
                $ftsupplier_name = RetRstField("SELECT ftsupplier_name FROM tlsupplier WHERE ftsupplier_code = '{$_REQUEST['ftsupplier_code']}' ");

                for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                    // amount
                    if (CCur($_REQUEST["fcamountD0"][$i]) > 0) {
                            $credit = CCur($_REQUEST['fcamountD0'][$i]);
                            $debit = 0;
                    } else {
                            $debit = CCur($_REQUEST['fcamountD0'][$i]);
                            $credit = 0;
                    }

                    if (($_REQUEST['fnpayment_methodD0'][$i] == 0) || ($_REQUEST['fnpayment_methodD0'][$i] == 1)) {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F08'");
                    } else if ($_REQUEST['fnpayment_methodD0'][$i] == 9) {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F10'");
                    } else {
                            $ftcoa_code = $_REQUEST['ftacc_noD0'][$i];
                    }

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftremarks'],
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);

                    // Other Cost
                    if (CCur($_REQUEST["fcother_costD0"][$i]) != 0) {

                        if (CCur($_REQUEST["fcother_costD0"][$i]) > 0) {
                                $credit = CCur($_REQUEST['fcother_costD0'][$i]);
                                $debit = 0;
                        } else  if (CCur($_REQUEST["fcother_costD0"][$i]) < 0) {
                                $debit = CCur($_REQUEST['fcother_costD0'][$i]);
                                $credit = 0;
                        }

                        $ftcoa_code = $_REQUEST['ftother_coa_codeD0'][$i];

                        $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftremarks'],
                                        "fcdebit"  => $debit,
                                        "fccredit"  => $credit);
                    }
                }

                // Detail Pembayaran Hutang - Sisi Debit - Purchase Invoice
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F01'");

                for($i=0; $i<count($_REQUEST['fnindexD1']); $i++) {
                    if (CCur($_REQUEST["fcpayment_amountD1"][$i]) > 0) {
                        $debit = CCur($_REQUEST['fcpayment_amountD1'][$i]);
                        $credit = 0;
                    } else {
                        $credit = CCur($_REQUEST['fcpayment_amountD1'][$i]);
                        $debit = 0;
                    }        
                    
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                            "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftpi_noD1'][$i],
                            "fcdebit"  => $debit,
                            "fccredit"  => $credit);
                    
                }

                // Detail Pembayaran Hutang - Biaya
                for($i=1; $i<count($_REQUEST['fnindexD2']); $i++) {
                    if (CCur($_REQUEST["fcamountD2"][$i]) != 0) {

                        if (CCur($_REQUEST["fcamountD2"][$i]) > 0) {
                                $debit = 0;
                                $credit = CCur($_REQUEST['fcamountD2'][$i])*CCur($_REQUEST['fckurs']);
                        } else {
                                $debit = CCur($_REQUEST['fcamountD2'][$i])*CCur($_REQUEST['fckurs']);
                                $credit = 0;

                        }

                        $GL[] = array( "ftcoa_code" => $_REQUEST['ftcoa_codeD2'][$i],
                                        "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftdescriptionD2'][$i],
                                        "fcdebit"  => $debit,
                                        "fccredit"  => $credit);

                    }
                }

                // Detail Pembayaran Hutang - Selisih Kurs
                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F21'");

                if (CCur($_REQUEST["fcrate_adjustment"]) != 0) {
                    if (CCur($_REQUEST["fcrate_adjustment"]) > 0) {
                        $debit = 0;
                        $credit = CCur($_REQUEST['fcrate_adjustment']);
                    } else {
                        $debit = CCur($_REQUEST['fcrate_adjustment']);
                        $credit = 0;
                    }

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $ftsupplier_name . ' - ' . $_REQUEST['ftremarks'],
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);
                }

            break;
            case "credit_memo":
                // Section Insert GL
                $DocumentNo = $_REQUEST['ftcm_no'];

                // Credit Memo kategori retur tidak perlu jurnal karena sudah terbuat di sales retur
                if ($_REQUEST['fntrans_type'] == '1') {
                    return $strSQL;
                    
                } else {

                    // Detail Credit Memo - Sisi Debit
                    for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                        $debit = CCur($_REQUEST['fccredit_amountD0'][$i]);
                        $credit = 0;

                        if ($_REQUEST['fntrans_type'] == '0') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S02'");
                        //} else if ($_REQUEST['fntrans_type'] == '1') {
                        //    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S05'");
                        } else if ($_REQUEST['fntrans_type'] == '2') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'S01'");
                        } else if ($_REQUEST['fntrans_type'] == '3') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F09'");
                        }

                        $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $_REQUEST['ftsales_invoice_noD0'][$i] . ' - ' . $_REQUEST['ftdescriptionD0'][$i],
                                        "fcdebit"  => $debit,
                                        "fccredit"  => $credit);
                    }

                    // Header Credit Memo - Sisi Credit
                    if ($_REQUEST['fntrans_type'] == '0') {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F14'");
                    //} else if ($_REQUEST['fntrans_type'] == '1') {
                    //    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F15'");
                    } else if ($_REQUEST['fntrans_type'] == '2') {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F16'");
                    } else if ($_REQUEST['fntrans_type'] == '3') {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F24'");
                    }

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $_REQUEST['ftnotes'],
                                    "fcdebit"  => 0,
                                    "fccredit"  => CCur($_REQUEST['fcinvoice_amount']) );

                }

            break;
            case "debit_memo":
                // Section Insert GL
                $DocumentNo = $_REQUEST['ftdm_no'];

                // Debit Memo kategori retur tidak perlu jurnal karena sudah terbuat di purchase retur
                if ($_REQUEST['fntrans_type'] == '1') {
                    return $strSQL;

                } else {

                    // Header Debit Memo - Sisi Debit
                    if ($_REQUEST['fntrans_type'] == '0') {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F11'");
                    //} else if ($_REQUEST['fntrans_type'] == '1') {
                    //    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F12'");
                    } else if ($_REQUEST['fntrans_type'] == '2') {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F13'");
                    } else if ($_REQUEST['fntrans_type'] == '3') {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F22'");
                    } else if ($_REQUEST['fntrans_type'] == '4') {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F23'");
                    }

                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $_REQUEST['ftnotes'],
                                    "fcdebit"  => CCur($_REQUEST['fcinvoice_amount']),
                                    "fccredit"  => 0 );

                    // Detail Debit Memo - Sisi Credit
                    for($i=1; $i<count($_REQUEST['fnindexD0']); $i++) {
                        $debit = 0;
                        $credit = CCur($_REQUEST['fcdebit_amountD0'][$i]);

                        if ($_REQUEST['fntrans_type'] == '0') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P02'");
                        //} else if ($_REQUEST['fntrans_type'] == '1') {
                        //    $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P06'");
                        } else if ($_REQUEST['fntrans_type'] == '2') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P01'");
                        } else if ($_REQUEST['fntrans_type'] == '3') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'P01'");
                        } else if ($_REQUEST['fntrans_type'] == '4') {
                            $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F10'");
                        }

                        $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                        "ftdescription" => $_REQUEST['ftcv_noD0'][$i] . ' - ' . $_REQUEST['ftdescriptionD0'][$i],
                                        "fcdebit"  => $debit,
                                        "fccredit"  => $credit);
                    }
                }

            break;
            case "giro_mundur_piutang":
                // Section Insert GL
                // Detail Giro Mundur Piutang - Sisi Debit
                $DocumentNo = $_REQUEST['ftdocument_no'];

                $debit = CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']);
                $credit = 0;
                
                if ($_REQUEST['fnstatus_giro'] == 1) {
                        $ftcoa_code = $_REQUEST['ftacc_no'];
                } else if ($_REQUEST['fnstatus_giro'] == 2) {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F04'");
                } 

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftgiro_no'],
                                "fcdebit"  => $debit,
                                "fccredit"  => $credit);

                // Detail Giro Mundur Piutang - Sisi Kredit 
                $debit = 0;
                $credit = CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']);

                $fttype = RetRstField("SELECT fttype FROM txgiro_hist WHERE ftdocument_no = '{$DocumentNo}' AND ftgiro_no = '{$_REQUEST['ftgiro_no']}' ");

                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F07'");
                
                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftgiro_no'],
                                "fcdebit"  => $debit,
                                "fccredit"  => $credit);


            break;
            case "giro_mundur_hutang":
                // Section Insert GL
                // Detail Giro Mundur Hutang - Sisi Debit
                $DocumentNo = $_REQUEST['ftdocument_no'];

                $debit = CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']);
                $credit = 0;

                $fttype = RetRstField("SELECT fttype FROM txgiro_hist WHERE ftdocument_no = '{$DocumentNo}' AND ftgiro_no = '{$_REQUEST['ftgiro_no']}' ");

                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F08'");
 
                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftgiro_no'],
                                "fcdebit"  => $debit,
                                "fccredit"  => $credit);

                // Detail Giro Mundur Hutang - Sisi Credit
                $debit = 0;
                $credit = CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']);

                if ($_REQUEST['fnstatus_giro'] == 1) {
                        $ftcoa_code = $_REQUEST['ftacc_no'];
                } else if ($_REQUEST['fnstatus_giro'] == 2) {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F01'");
                }

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftgiro_no'],
                                "fcdebit"  => $debit,
                                "fccredit"  => $credit);
   
            break;
            case "uang_muka_penjualan":
                // Section Insert GL
                // Detail UM Penjualan - Sisi Debit
                $DocumentNo = $_REQUEST['fttrans_no'];

                $debit = CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']);
                $credit = 0;

                if (($_REQUEST['fnpayment_method'] == 0) || ($_REQUEST['fnpayment_method'] == 1)) {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F07'");
                } else {
                        $ftcoa_code = $_REQUEST['ftacc_no'];
                }

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftremarks'],
                                "fcdebit"  => $debit,
                                "fccredit"  => $credit);

                // Detail UM Penjualan - Sisi Kredit
                $debit = 0;
                $credit = CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']);

                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F09'");

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftremarks'],
                                "fcdebit"  => $debit,
                                "fccredit"  => $credit);
            break;
            case "uang_muka_pembelian":
                // Section Insert GL
                // Detail UM Pembelian - Sisi Debit
                $DocumentNo = $_REQUEST['fttrans_no'];

                $debit = CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']);
                $credit = 0;

                $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F10'");

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftremarks'],
                                "fcdebit"  => $debit,
                                "fccredit"  => $credit);

                // Detail UM Pembelian - Sisi Credit
                $debit = 0;
                $credit = CCur($_REQUEST['fcamount'])*CCur($_REQUEST['fcrate']);

                if (($_REQUEST['fnpayment_method'] == 0) || ($_REQUEST['fnpayment_method'] == 1)) {
                        $ftcoa_code = RetRstField("SELECT ftcoa_code FROM tlcoa_setting WHERE ftcoa_no = 'F08'");
                } else {
                        $ftcoa_code = $_REQUEST['ftacc_no'];
                }

                $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                "ftdescription" => $_REQUEST['ftremarks'],
                                "fcdebit"  => $debit,
                                "fccredit"  => $credit);
            break;    
            
            // modul akunting
            case "penyusutan_asset":
                // Section Insert GL
                $DocumentNo = $_REQUEST['fttrans_code'];
                
                for($i=0; $i<count($_REQUEST['fnindexD0']); $i++) {
                    // Detail Penyusutan - Sisi Debit (Biaya)
                    $debit = CCur($_REQUEST['fcmonthly_amountD0'][$i]);
                    $credit = 0;

                    $ftcoa_code = RetRstField("SELECT ftgl_cost_acc,ftasset_code FROM tlasset a INNER JOIN tlasset_category b ON a.ftcategory_code= b.ftcategory_code WHERE a.ftasset_code = '{$_REQUEST['ftasset_codeD0'][$i]}' ");
                    
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $_REQUEST['ftasset_codeD0'][$i],
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);
                    
                    // Detail Penyusutan - Sisi Credit (Akumulasi))
                    $debit = 0;
                    $credit = CCur($_REQUEST['fcmonthly_amountD0'][$i]);
                    
                    $ftcoa_code = RetRstField("SELECT ftgl_accumulate_acc,ftasset_code FROM tlasset a INNER JOIN tlasset_category b ON a.ftcategory_code= b.ftcategory_code WHERE a.ftasset_code = '{$_REQUEST['ftasset_codeD0'][$i]}' ");
                    
                    $GL[] = array( "ftcoa_code" => $ftcoa_code,
                                    "ftdescription" => $_REQUEST['ftasset_codeD0'][$i],
                                    "fcdebit"  => $debit,
                                    "fccredit"  => $credit);
                }

                break;    
            
            default :
                return $strSQL;
            break;
        } // end switch

        // Proses Insert GL
        if (($_REQUEST['form'] == 'giro_mundur_piutang') || ($_REQUEST['form'] == 'giro_mundur_hutang')) {
            $arrGL = InsertGL($DocumentNo, $_REQUEST['fdgiro_cliring_date'], $_REQUEST['form'], $GL);
        } else {
            $arrGL = InsertGL($DocumentNo, $_REQUEST['fdtrans_date'], $_REQUEST['form'], $GL);
        }
        $strSQL["Insert GL"] =$arrGL;

        return $strSQL;
    }
 

    function RevisiGL($trans_no_new) {
        $strSQL=array();
        switch($_REQUEST['form']) {
            // module penjualan
            case "sales_order" :
                $DocumentNo = RetRstField("SELECT ftsales_invoice_no FROM txsales_invoice_hdr WHERE ftso_no = '{$_REQUEST["ftso_no"]}'");
            break;    
            case "sales_invoice" :
                $DocumentNo = $_REQUEST['ftsales_invoice_no'];
            break;
            case "delivery_order" :
                $DocumentNo = $_REQUEST['ftdo_no'];
            break;
            case "sales_return" :
                $DocumentNo = $_REQUEST['ftsales_return_no'];
            break;
            case "sales_invoice_adjustment" :
                $DocumentNo = $_REQUEST['ftsales_invoice_no'];
            break;
        
            // module pembelian
            case "pembelian_stock_masuk" :
                $ftpurchase_invoice_no = RetRstField("SELECT ftpurchase_invoice_no FROM txpurchase_invoice_hdr WHERE ftstock_in_no = '{$_REQUEST['ftstock_in_no']}' AND fnstatus = 1 ");
                $DocumentNo = $ftpurchase_invoice_no;
                break;
            case "pembelian_purchase_invoice" :
            case "pembelian_purchase_invoice_local" :
            case "pembelian_purchase_invoice_import" :
               $DocumentNo = $_REQUEST['ftpurchase_invoice_no'];
                break;
            case "return_pembelian":
            case "return_pembelian_local":
            case "return_pembelian_import":
               $DocumentNo = $_REQUEST['ftpurchase_return_no'];
                break;

            // module inventory
            case "inventorybb_stock_masuk":
            case "inventorysj_barang_masuk":
            case "inventorysp_barang_masuk":
            case "barang_masuk":
                $DocumentNo = $_REQUEST['ftstock_in_no'];
            break;
            case "inventorybb_stock_keluar":
            case "inventorysj_barang_keluar":
            case "inventorysp_barang_keluar":
            case "barang_keluar":
                $DocumentNo = $_REQUEST['ftstock_out_no'];
            break;
            case "inventorybb_stock_mutasi_barang_jadi":
                $DocumentNo = $_REQUEST['ftstock_mutation_no'];
            break;
            case "inventorybb_stock_penyesuaian":
            case "inventorysj_stock_penyesuaian":
            case "inventorysp_stock_penyesuaian":
            case "penyesuaian_barang":
                $DocumentNo = $_REQUEST['ftstock_adjustment_no'];
            case "assembly":
                $DocumentNo = $_REQUEST['ftassembly_no'];
            case "disassembly":
                $DocumentNo = $_REQUEST['ftdisassembly_no'];
            break;

            // module keuangan
            case "kas_masuk" :
                $DocumentNo = $_REQUEST['ftcashrcp_no'];
            break;
            case "kas_keluar" :
                $DocumentNo = $_REQUEST['ftcashpym_no'];
            break;
            case "bank_masuk" :
                $DocumentNo = $_REQUEST['ftbankrcp_no'];
            break;
            case "bank_keluar" :
                $DocumentNo = $_REQUEST['ftbankpym_no'];
            break;
            case "penerimaan_piutang" :
                $DocumentNo = $_REQUEST['ftar_no'];
            break;
            case "rekap_bank_voucher" :
            case "rekap_bank_voucher_import" :
                $DocumentNo = $_REQUEST['ftcvs_no'];
            break;
            case "credit_memo" :
                $DocumentNo = $_REQUEST['ftcm_no'];
            break;
            case "debit_memo" :
                $DocumentNo = $_REQUEST['ftdm_no'];
            break;
            case "giro_mundur_piutang" :
            case "giro_mundur_hutang" :
                $DocumentNo = $_REQUEST['ftdocument_no'];
            break;
            case "uang_muka_penjualan" :
            case "uang_muka_pembelian" :
                $DocumentNo = $_REQUEST['fttrans_no'];
            break;
            case "penyusutan_asset" :
                $DocumentNo = $_REQUEST['fttrans_code'];
            break;
            default :
                $strSQL;
            break;
        }
            
        if (($_REQUEST['form'] == 'giro_mundur_piutang') || ($_REQUEST['form'] == 'giro_mundur_hutang')) {
            // Revisi Old General Ledger
            $arrGL[] = "UPDATE txgeneral_journal_hdr SET fnStatus = 9, ftDocument_No = '" . $trans_no_new . "',
                                ftModified_User = '" . $_SESSION["username"] . "', fdModified_Date = NOW()
                        WHERE ftDocument_No = '" . $DocumentNo . "' AND ftgiro_no = '" . $_REQUEST['ftgiro_no'] . "' and fntype=0 and ftsource= '" . $_REQUEST['form'] . "' ";

        } else if (($_REQUEST['form'] == 'penerimaan_piutang') || ($_REQUEST['form'] == 'rekap_bank_voucher') || ($_REQUEST['form'] == 'rekap_bank_voucher_import') || ($_REQUEST['form'] == 'uang_muka_penjualan') || ($_REQUEST['form'] == 'uang_muka_pembelian')) {
            // Revisi Old General Ledger
            $arrGL[] = "UPDATE txgeneral_journal_hdr SET fnStatus = 9, ftDocument_No = '" . $trans_no_new . "',
                                ftModified_User = '" . $_SESSION["username"] . "', fdModified_Date = NOW()
                        WHERE ftDocument_No = '" . $DocumentNo . "' and fntype=0 and (ftsource= '" . $_REQUEST['form'] . "' OR ftsource LIKE 'giro_mundur%') ";
           
        } else {
            // Revisi Old General Ledger
            $arrGL[] = "UPDATE txgeneral_journal_hdr SET fnStatus = 9, ftDocument_No = '" . $trans_no_new . "',
                                ftModified_User = '" . $_SESSION["username"] . "', fdModified_Date = NOW()
                        WHERE ftDocument_No = '" . $DocumentNo . "' and fntype=0 and ftsource= '" . $_REQUEST['form'] . "' ";
            
        }

        $strSQL["Revisi GL"] =$arrGL;
            
        return $strSQL;
    }

    // Batal GL
    function CancelGL() {
        $strSQL=array();
        switch($_REQUEST['form']) {
            // module penjualan
            case "sales_order" :
                $DocumentNo = RetRstField("SELECT ftsales_invoice_no FROM txsales_invoice_hdr WHERE ftso_no = '{$_REQUEST["ftso_no"]}'");
            break;
            case "sales_invoice" :
                $DocumentNo = $_REQUEST['ftsales_invoice_no'];
            break;
            case "delivery_order" :
                $DocumentNo = $_REQUEST['ftdo_no'];
            break;
            case "sales_return" :
                $DocumentNo = $_REQUEST['ftsales_return_no'];
            break;
            case "sales_invoice_adjustment" :
                $DocumentNo = $_REQUEST['ftsales_invoice_no'];
            break;
        
            // module pembelian
            case "pembelian_stock_masuk" :
                $ftpurchase_invoice_no = RetRstField("SELECT ftpurchase_invoice_no FROM txpurchase_invoice_hdr WHERE ftstock_in_no = '{$_REQUEST['ftstock_in_no']}' AND fnstatus = 1 ");
                $DocumentNo = $ftpurchase_invoice_no;
                break;
            case "pembelian_purchase_invoice" :
            case "pembelian_purchase_invoice_local" :
            case "pembelian_purchase_invoice_import" :
               $DocumentNo = $_REQUEST['ftpurchase_invoice_no'];
            break;
            case "return_pembelian":
            case "return_pembelian_local":
            case "return_pembelian_import":
               $DocumentNo = $_REQUEST['ftpurchase_return_no'];
            break;

            // module inventory
            case "inventorybb_stock_masuk":
            case "inventorysj_barang_masuk":
            case "inventorysp_barang_masuk":
            case "barang_masuk":
                $DocumentNo = $_REQUEST['ftstock_in_no'];
            break;

            case "inventorybb_stock_keluar":
            case "inventorysj_barang_keluar":
            case "inventorysp_barang_keluar":
            case "barang_keluar":
                $DocumentNo = $_REQUEST['ftstock_out_no'];
            break;

            case "inventorybb_stock_mutasi_barang_jadi":
                $DocumentNo = $_REQUEST['ftstock_mutation_no'];
            break;

            case "inventorybb_stock_penyesuaian":
            case "inventorysj_stock_penyesuaian":
            case "inventorysp_stock_penyesuaian":
            case "penyesuaian_barang":
                $DocumentNo = $_REQUEST['ftstock_adjustment_no'];
            break;

            // module keuangan
            case "kas_masuk" :
                $DocumentNo = $_REQUEST['ftcashrcp_no'];
            break;
            case "kas_keluar" :
                $DocumentNo = $_REQUEST['ftcashpym_no'];
            break;
            case "bank_masuk" :
                $DocumentNo = $_REQUEST['ftbankrcp_no'];
            break;
            case "bank_keluar" :
                $DocumentNo = $_REQUEST['ftbankpym_no'];
            break;
            case "penerimaan_piutang" :
                $DocumentNo = $_REQUEST['ftar_no'];
            break;
            case "rekap_bank_voucher" :
            case "rekap_bank_voucher_import" :
                $DocumentNo = $_REQUEST['ftcvs_no'];
            break;
            case "credit_memo" :
                $DocumentNo = $_REQUEST['ftcm_no'];
            break;
            case "debit_memo" :
                $DocumentNo = $_REQUEST['ftdm_no'];
            break;
            case "giro_mundur_piutang" :
            case "giro_mundur_hutang" :
                $DocumentNo = $_REQUEST['ftdocument_no'];
            break;
            case "uang_muka_penjualan" :
            case "uang_muka_pembelian" :
                $DocumentNo = $_REQUEST['fttrans_no'];
            break;
            case "penyusutan_asset" :
                $DocumentNo = $_REQUEST['fttrans_code'];
            break;
            default :
                return $strSQL;
            break;    
        }

        if (($_REQUEST['form'] == 'giro_mundur_piutang') || ($_REQUEST['form'] == 'giro_mundur_hutang')) {
            // Batal General Ledger
            //$arrGL[] = "UPDATE txgeneral_journal_hdr SET fnStatus = 9, ftModified_User = '" . $_SESSION["username"] . "', fdModified_Date = NOW() WHERE ftDocument_No = '" . $DocumentNo . "' AND ftDocument_No = '" . $_REQUEST['ftgiro_no'] . "' and fntype=0 and ftsource= '" . $_REQUEST['form'] . "' ";
            $arrGL[] = "UPDATE txgeneral_journal_hdr SET fnStatus = 9, ftModified_User = '" . $_SESSION["username"] . "', fdModified_Date = NOW() WHERE ftDocument_No = '" . $DocumentNo . "' and fntype=0 and ftsource= '" . $_REQUEST['form'] . "' ";

        } else if (($_REQUEST['form'] == 'penerimaan_piutang') || ($_REQUEST['form'] == 'rekap_bank_voucher') || ($_REQUEST['form'] == 'rekap_bank_voucher_import') || ($_REQUEST['form'] == 'uang_muka_penjualan') || ($_REQUEST['form'] == 'uang_muka_pembelian')) {
            // Revisi Old General Ledger
            $arrGL[] = "UPDATE txgeneral_journal_hdr SET fnStatus = 9, ftModified_User = '" . $_SESSION["username"] . "', fdModified_Date = NOW() WHERE ftDocument_No = '" . $DocumentNo . "' and fntype=0 and (ftsource= '" . $_REQUEST['form'] . "' OR ftsource LIKE 'giro_mundur%') ";

        } else {
            // Batal General Ledger
            $arrGL[] = "UPDATE txgeneral_journal_hdr SET fnStatus = 9, ftModified_User = '" . $_SESSION["username"] . "', fdModified_Date = NOW() WHERE ftDocument_No = '" . $DocumentNo . "' and fntype=0 and ftsource= '" . $_REQUEST['form'] . "' ";
        }

        $strSQL["Cancel GL"] =$arrGL;

        return $strSQL;
    }

?>
