<?php
    Getsecurity("sales_return");

    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
                if(RetRstField("SELECT fnid FROM txsales_return_hdr WHERE ftsales_return_no='{$_REQUEST['fttrans_no']}'")) {
                      ?>
                        alert('No. transaksi sudah dipakai...\n System akan mengenerate No. transaksi kembali ...\n Silahkan ulangi proses penyimpanan...  ');
                        OnChangeDate_fdtrans_date();
                      <?php
                }
                else {
                      ?>
                      submitbutton('Simpan','','','','');
                      <?php
                }
                  die("");
                  break;

            // Set Satuan dan Harga Jual
            case "item":
                ?>
                var ObjSelect=document.getElementById('<?php echo $_REQUEST['ObjLokasi']; ?>');
                var len = ObjSelect.length;
                for (var idx=0; idx < len; idx++)
                {
                        if (ObjSelect.options[ObjSelect.length-1].value!='') {
                                  ObjSelect.remove(ObjSelect.length-1);
                        }
                }
                <?php

                $ftUOM_Code1=RetRstField("SELECT UCASE(ftUOM_Code) FROM tlstock_hdr WHERE ftitem_code='{$_REQUEST['ftitem_code']}'");
                $ftUOM_Code="SELECT UCASE(a.ftuom_code) AS ftuom_code,UCASE(b.ftuom_name) AS ftuom_name FROM tlstock_hdr a
                                    INNER JOIN tluom b ON a.ftuom_code=b.ftuom_code
                                    WHERE a.ftitem_code='{$_REQUEST['ftitem_code']}'
                                    GROUP BY a.ftuom_code
                                    UNION ALL
                                    SELECT UCASE(a.ftuom_code_multi) AS ftuom_code,UCASE(b.ftuom_name) AS ftuom_name FROM tlstock_multi_det a
                                            INNER JOIN tluom b ON a.ftuom_code_multi=b.ftuom_code
                                    WHERE a.ftitem_code='{$_REQUEST['ftitem_code']}'
                                    GROUP BY a.ftuom_code_multi
                                        ";
                $rs=mysql_query($ftUOM_Code);
                $i=0;
                while($r=mysql_fetch_assoc($rs))
                {
                        $i++;
                        echo " try {
                                         ObjSelect.add(new Option('{$r['ftuom_code']}--{$r['ftuom_name']}','{$r['ftuom_code']}'),null,null);
                                  } catch(ex) {
                                         ObjSelect.add(new Option('{$r['ftuom_code']}--{$r['ftuom_name']}','{$r['ftuom_code']}'),$i,$i);
                                  } ";
                }

                $fcprice=RetRstField("SELECT fcselling_price FROM tlselling_price WHERE ftitem_code='{$_REQUEST['ftitem_code']}'");
                $ffdefault='1';
                ?>
                    document.getElementById('ftuom_codeD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftUOM_Code1; ?>';
                    document.getElementById('fcpriceD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $fcprice; ?>';
                    <?php
                break;

            case "formula":
                $ssql="SELECT IFNULL(SUM(ffformula),1) as ffformula
                        FROM tlstock_multi_det
                        WHERE ftitem_code='{$_REQUEST['ftitem_code']}'
                        and ftuom_code_multi='{$_REQUEST['ftuom_code']}'
                        ";
                $ffformula=RetRstField($ssql);
                $ffQtyMulti=(double)$ffformula*(double)CCur($_REQUEST['fnqty']);
                ?>
                        document.getElementById('ffformulaD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ffformula; ?>';
                        document.getElementById('fnqty_baseD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ffQtyMulti; ?>';
                <?php

                die("");
                break;

            case "lokasi":
                $ssql="SELECT ftwarehouse_code FROM tllocation 
                        WHERE ftlocation_code='{$_REQUEST['ftlocation_code']}'
                        ";
                $ftwarehouse_code=RetRstField($ssql);

                ?>
                document.getElementById('ftwarehouse_codeD0<?php echo $_REQUEST['index']; ?>').value='<?php echo $ftwarehouse_code; ?>';
                <?php

                die("");
                break;  
        }
    }

    if(!$_REQUEST['id']) {
            $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=tn123&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftsales_return_no&no_trans=ftsales_return_no&table_name=txsales_return_hdr&ftbranch_code={$_SESSION['strbranch']}');";

            $strAutoNumber0=" function OnChangeDate_fdtrans_date() {
                                           {$strAutoNumber1}
                                     }
                                     OnChangeDate_fdtrans_date(); 
                                     ElementCurrencyALL();
                            ";
            $objAxpDB->FormJS($strAutoNumber0);

    } else {
            $objAxpDB->FormJS("ElementCurrencyALL();");
    }

    $objAxpDB->LookupMagicQuotes(True);

    $objAxpDB->ViewName("Daftar Sales Return");
    //$objAxpDB->ViewTable("vsalesreturn");
    $objAxpDB->ViewSQL("SELECT
                        `a`.`fnid`                AS `fnid`,
                        `a`.`fdtrans_date`        AS `fdtrans_date`,
                        `a`.`ftsales_return_no`   AS `ftsales_return_no`,
                        `a`.`ftso_no`             AS `ftso_no`,
                        `a`.`ftsales_invoice_no`  AS `ftsales_invoice_no`,
                        `a`.`ftcustomer_code`     AS `ftcustomer_code`,
                        `b`.`ftcustomer_name`     AS `ftcustomer_name`,
                        `a`.`fcamount`            AS `fcamount`,
                        `c`.`fttrans_status_name` AS `ftstatus`
                        FROM ((`txsales_return_hdr` `a`
                        LEFT JOIN `tlcustomer` `b` ON ((`a`.`ftcustomer_code` = `b`.`ftcustomer_code`)))
                        LEFT JOIN `tstransaction_status` `c` ON (((`a`.`fnstatus` = `c`.`fntrans_status_code`) AND (`c`.`fntrans_group_code` = 2))))
                        WHERE (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')
                        ORDER BY `a`.`ftsales_return_no` DESC, `a`.`fdtrans_date`");
    $objAxpDB->ViewCaption("ID, Tgl. Return, No. Return, No. SO, No. Invoice, Kode Customer, Nama Customer, Jumlah, Status");
    $objAxpDB->ViewPeriod(TRUE);

    // Deklarasi Header & Footer
    $objAxpDB->FormName("Retur Penjualan");
    $objAxpDB->FormSQL("SELECT ftsales_return_no, ftcustomer_code, ftso_no, ftsales_invoice_no, 
                            fdtrans_date,ftsales_code,fnreturn_type,ftnotes,
                            fcsub_amount,ffdiscount,fcdiscount,
                            fcother_cost,ffvat,fcvat,
                            fcamount,fnstatus
                            FROM txsales_return_hdr");
    $objAxpDB->FormCaption("No. Return, Customer, No. SO, No Invoice,
                                Tgl. Return, Sales, Jenis Retur, Catatan,
                                Sub Total, Disc (%), Disc,Biaya Lain-Lain,Pajak (%), Pajak,Grand Total, Status");
    $objAxpDB->FormPosition("TL,TL,HD,TL,TR,TR,TR,BL,HD,HD,HD,HD,HD,HD,BR,BR");

    // Deklarasi Detail
    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, fnqty_si, fnqty_outstanding_si,fnqty, ftuom_code,ftlocation_code,ftwarehouse_code, fcprice, ffdiscount, fcdiscount, fcamount, fnqty_base, ffformula, fnindex_si, ftprint_name FROM txsales_return_det");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Qty SO, Qty Sisa SO, Qty, Satuan, Lokasi, Gudang, Harga, Discount(%),Discount,Jumlah, Qty Base, Formula, Index SI, Keterangan Barang");
    $objAxpDB->GridSize(0, "2,10,5,5,5,5,5,5,10,5,10,10,,,");
     
    $ajaxGrid="ajax_do_grid0('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftso_no='+ document.getElementById('ftso_no').value + '&ftsales_invoice_no='+ document.getElementById('ftsales_invoice_no').value);";
    $objAxpDB->FormScript(2, "onChange=\"{$ajaxGrid}\"");
    $objAxpDB->FormScript(3, "onChange=\"{$ajaxGrid}\"");
    $objAxpDB->FormScript(9, "onChange=\"SalesReturnRecalDiscPercent();\"");
    $objAxpDB->FormScript(10, "onChange=\"SalesReturnRecalDiscRp();\"");
    $objAxpDB->FormScript(11, "onChange=\"SalesReturnRecalQty0();\"");
    $objAxpDB->FormScript(12, "onChange=\"SalesReturnRecalQty0();\"");

    $ajaxItem="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=item&ObjLokasi=ftuom_codeD0_N_&ftitem_code=' + this.value + '&index=_N_');";
    $objAxpDB->GridScript(0,1," onChange=\"{$ajaxItem}SalesReturnDiscDetailPercent(_N_);SalesReturnRecalDiscPercent();\"");
    $ajaxFormula="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=formula&index=_N_&index=_N_&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value+'&ftuom_code='+ document.getElementById('ftuom_codeD0_N_').value+'&fnqty='+ document.getElementById('fnqtyD0_N_').value);";
    $ajax1=" onChange=\"SalesReturnDiscDetailPercent(_N_);SalesReturnRecalDiscPercent();{$ajaxFormula}\"  ";
    $objAxpDB->GridScript(0,4, $ajax1);
    $ajaxLokasi="OnChange=\"ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=lokasi&index=_N_&ftlocation_code='+this.value);\"";
    $objAxpDB->GridScript(0,6, $ajaxLokasi);
    $ajaxFormula="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=formula&index=_N_&index=_N_&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value+'&ftuom_code='+ document.getElementById('ftuom_codeD0_N_').value+'&fnqty='+ document.getElementById('fnqtyD0_N_').value);";
    $ajax1=" onChange=\"{$ajaxFormula}SalesReturnDiscDetailPercent(_N_);SalesReturnRecalDiscPercent();\"  ";
    $objAxpDB->GridScript(0,8, $ajax1);
    $ajax1=" onChange=\"SalesReturnDiscDetailPercent(_N_);SalesReturnRecalDiscPercent();\"   ";
    $objAxpDB->GridScript(0,9, $ajax1);
    $ajax1="   onChange=\"SalesReturnDiscDetailRp(_N_);SalesReturnRecalDiscPercent();\"  ";
    $objAxpDB->GridScript(0,11, $ajax1);

    if($_REQUEST['id']=='0') {
        $objAxpDB->Lookup("ftsales_invoice_no", "SELECT ftsales_invoice_no, fdtrans_date FROM txsales_invoice_hdr WHERE fnstatus=1 ", "ftsales_invoice_no", "fdtrans_date", -1, False, "' AND ftcustomer_code=\''+ document.getElementById('ftcustomer_code').value + '\''");
        $objAxpDB->Lookup("ftso_no", "SELECT ftso_no, fdtrans_date FROM txsales_order_hdr WHERE fnstatus=2 ", "ftso_no", "fdtrans_date", -1, False, "' AND ftcustomer_code=\''+ document.getElementById('ftcustomer_code').value + '\''");
    } else {
        $objAxpDB->Lookup("ftsales_invoice_no", "SELECT ftsales_invoice_no, fdtrans_date FROM txsales_invoice_hdr WHERE 1=1 ", "ftsales_invoice_no", "fdtrans_date", -1, False, "' AND ftcustomer_code=\''+ document.getElementById('ftcustomer_code').value + '\''");
        $objAxpDB->Lookup("ftso_no", "SELECT ftso_no, fdtrans_date FROM txsales_order_hdr WHERE 1=1 ", "ftso_no", "fdtrans_date", -1, False, "' AND ftcustomer_code=\''+ document.getElementById('ftcustomer_code').value + '\''");
    }
    
    $objAxpDB->Lookup("ftcustomer_code", "SELECT ftcustomer_Code, ftcustomer_name FROM tlcustomer ORDER BY ftcustomer_Code", "ftcustomer_Code", "ftcustomer_name", -1, False);
    $objAxpDB->Lookup("fnstatus", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 2", "fntrans_status_code", "fttrans_status_name",1, True);
//    $objAxpDB->Lookup("ftcurrency_code", "SELECT  ftsimbol, ftdescription  FROM tlcurrency_hdr ORDER BY ftsimbol", "ftsimbol", "ftdescription", 0, True);
    // $objAxpDB->Lookup("ftwarehouse_code", "SELECT ftwarehouse_code, ftwarehouse_name FROM tlwarehouse", "ftwarehouse_code", "ftwarehouse_name", -1,true);
    $objAxpDB->Lookup("ftlocation_code", "SELECT ftlocation_code, ftlocation_name FROM tllocation", "ftlocation_code", "ftlocation_name", -1,true);
    $objAxpDB->Lookup("ftuom_code", "SELECT ftuom_code, ftuom_name FROM tluom", "ftuom_code", "ftuom_name", -1,true);
    $objAxpDB->Lookup("ftsales_code", "SELECT  ftsales_code, ftsales_name  FROM tlsales WHERE fnstatus=1 ORDER BY ftsales_code", "ftsales_code", "ftsales_name", -1, true);
    $objAxpDB->Lookup("fnreturn_type", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 74", "fntrans_status_code", "fttrans_status_name",-1, True);
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, CONCAT(ftitem_name,' PART NO: ',ifnull(ftpart_no,'')) AS ftitem_name FROM tlstock_hdr WHERE fnstatus = 1  ORDER BY ftitem_name,ftpart_no,ftitem_code  ", "ftitem_code", "ftitem_name", -1,false);
    
    $objAxpDB->SetStatus("id, fnreceipt_status, fnstatus");
      
    if ($objAxpDB->GetStatus("id") == "") {
            $objAxpDB->ButtonShow("baru");
    } elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->FormLocked("1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1");
            $objAxpDB->GridAllowAddNew(0,true);
            $objAxpDB->GridAllowDelete(0,true);
            $objAxpDB->GridDelJS(0,"SalesReturnRecalPrice();");
            $objAxpDB->GridLocked(0,"1,0,1,1,0,0,0,1,0,1,1,1,1,1,1,0");
            $objAxpDB->GridHidden(0,"0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,0");
    
    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
        // revisi
        if(isset($_REQUEST['revisi'])) 
        {
            $objAxpDB->FormLocked("1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,1");
            $objAxpDB->GridLocked(0,"1,0,1,1,0,0,0,1,0,1,1,1,1,1,1,0");
            $objAxpDB->GridHidden(0,"0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,0");
            $objAxpDB->GridAllowAddNew(0,true);
            $objAxpDB->GridAllowDelete(0,true);
            $objAxpDB->GridDelJS(0,"SalesReturnRecalPrice();");
            
            $objAxpDB->ButtonShow("simpan_revisi,tutup");
            
        } else {    
            $objAxpDB->ButtonShow("baru, cetak, revisi, batal, tutup");
            $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1");
            $objAxpDB->GridLocked(0,"1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1");
            $objAxpDB->GridHidden(0,"0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,0");
        }
        
    } elseif (($objAxpDB->GetStatus("fnstatus") == 3) || ($objAxpDB->GetStatus("fnstatus") == 9)) {
            $objAxpDB->ButtonShow("tutup");
            $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1");
            $objAxpDB->GridLocked(0,"1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1");
            $objAxpDB->GridHidden(0,"0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,0");
    }

    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
           case "grid":
                ob_start();
                // if ($_REQUEST['ftso_no']=='') {
                if ($_REQUEST['ftsales_invoice_no']=='') {
                    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code, fnqty_si, fnqty_outstanding_si, fnqty, ftuom_code,ftlocation_code,ftwarehouse_code, fcprice, ffdiscount, fcdiscount, fcamount, fnqty_base, ffformula, fnindex_si, ftprint_name FROM txsales_return_det");
                    $objAxpDB->GridCaption(0, "No., Kode Barang, Qty SO, Qty Sisa SO, Qty, Satuan, Lokasi, Gudang, Harga, Discount(%),Discount,Jumlah, Qty Base, Formula, Index SO, Keterangan Barang");
                    $objAxpDB->GridLocked(0, "1,0,1,1,0,0,0,0,0,1,1,1,1,1,1,0");
                    $objAxpDB->GridHidden(0, "0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1");
                    
                    $objAxpDB->GridAllowAddNew(0,true);
                    $objAxpDB->GridAllowDelete(0,true);
                    $objAxpDB->GridDelJS(0,"SalesReturnRecalPrice();");
                }
                else {                    
                    $ssql=" SELECT fnindex,ftitem_code,fnqty_si,fnqty_si-fnqty_retur AS fnqty_outstanding_si, fnqty, ftuom_code, ftlocation_code,ftwarehouse_code,
                                    fcprice, ffdiscount, fcdiscount, fcamount, fnqty_base, ffformula, fnindex_si, ftprint_name 
                            FROM (  SELECT b.fnindex AS fnindex,b.ftitem_code,b.fnqty AS fnqty_si,COALESCE(SUM(e.fnqty_base),0) AS fnqty_retur,
                                            b.fnqty-b.fnqty AS fnqty, b.fnqty_base-b.fnqty_base AS fnqty_base, b.ftuom_code,
                                            b.ftlocation_code,b.ftwarehouse_code, b.fcprice, b.ffdiscount,
                                            '0.00' AS fcdiscount, '0.00' AS fcamount, b.ffformula, b.fnindex AS fnindex_si, b.ftprint_name
                                    FROM txsales_invoice_hdr a
                                    INNER JOIN txsales_invoice_det b ON a.ftsales_invoice_no=b.ftsales_invoice_no
                                    LEFT JOIN txsales_return_hdr d ON d.ftsales_invoice_no = a.ftsales_invoice_no AND d.fnstatus != 3 AND d.fnstatus != 9 
                                    LEFT JOIN txsales_return_det e ON b.ftitem_code=e.ftitem_code AND d.ftsales_return_no = e.ftsales_return_no
                                    WHERE a.fnstatus!=3 AND a.fnstatus<>9 AND a.ftbranch_code='{$_SESSION['strbranch']}'  
                                                AND a.ftsales_invoice_no= '".$_REQUEST['ftsales_invoice_no']."'  
                                    GROUP BY a.ftsales_invoice_no,b.fnindex,b.ftitem_code
                                    HAVING  fnqty_retur < fnqty_si) X
                            ORDER BY fnindex_si  #";

                    /* $ssql="SELECT b.fnindex,b.ftitem_code,b.fnqty AS fnqty_si,b.fnqty-IFNULL(b.fnqty_retur,0) AS fnqty_outstanding_si,
                            b.fnqty-b.fnqty AS fnqty, b.ftuom_code,b.ftlocation_code,b.ftwarehouse_code, b.fcprice, b.ffdiscount,
                            '0.00' AS fcdiscount, '0.00' AS fcamount, b.fnqty_base-b.fnqty_base as fnqty_base, b.ffformula, b.fnindex AS fnindex_si,
                            b.ftprint_name
                            FROM txsales_invoice_hdr a
                            INNER JOIN txsales_invoice_det b ON a.ftsales_invoice_no=b.ftsales_invoice_no
                            INNER JOIN tlstock_det c ON b.ftitem_code=c.ftitem_code 
                            -- LEFT JOIN txsales_return_hdr d ON d.ftso_no = a.ftso_no AND d.fnstatus != 3 AND d.fnstatus != 9 
                            LEFT JOIN txsales_return_hdr d ON d.ftsales_invoice_no = a.ftsales_invoice_no AND d.fnstatus != 3 AND d.fnstatus != 9 
                            LEFT JOIN txsales_return_det e ON c.ftitem_code=e.ftitem_code AND d.ftsales_return_no = e.ftsales_return_no
                            WHERE a.fnstatus!=3 AND a.fnstatus<>9 AND a.ftsales_invoice_no= '".$_REQUEST['ftsales_invoice_no']."'  
                                    -- AND a.ftso_no= '".$_REQUEST['ftso_no']."' 
                            GROUP BY b.fnindex #"; */
                    $objAxpDB->GridSQL(0,$ssql);
                    $objAxpDB->GridCaption(0, "No., Kode Barang, Qty SI, Qty Sisa SI, Qty Retur, Satuan, Lokasi, Gudang, Harga, Discount(%),Discount,Jumlah,Qty Base, Formula, Index SI, Keterangan Barang");
                    $objAxpDB->GridLocked(0, "1,1,1,1,0,1,0,1,0,1,1,1,1,1,1,0");
                    $objAxpDB->GridHidden(0, "0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,0");
                    
                    $ajaxFormula="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=formula&index=_N_&index=_N_&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value+'&ftuom_code='+ document.getElementById('ftuom_codeD0_N_').value+'&fnqty='+ document.getElementById('fnqtyD0_N_').value);";
                    $ajax1=" onChange=\"SalesReturnDiscDetailPercent(_N_);SalesReturnRecalDiscPercent();{$ajaxFormula}\"  ";
                    $objAxpDB->GridScript(0,4, $ajax1);
                    $ajaxLokasi="OnChange=\"ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=lokasi&index=_N_&ftlocation_code='+this.value);\"";
                    $objAxpDB->GridScript(0,6, $ajaxLokasi);
                    $ajaxFormula="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=formula&index=_N_&index=_N_&ftitem_code='+ document.getElementById('ftitem_codeD0_N_').value+'&ftuom_code='+ document.getElementById('ftuom_codeD0_N_').value+'&fnqty='+ document.getElementById('fnqtyD0_N_').value);";
                    $ajax1=" onChange=\"{$ajaxFormula}SalesReturnDiscDetailPercent(_N_);SalesReturnRecalDiscPercent();\"  ";
                    $objAxpDB->GridScript(0,8, $ajax1);
                    $ajax1=" onChange=\"SalesReturnDiscDetailPercent(_N_);SalesReturnRecalDiscPercent();\"   ";
                    $objAxpDB->GridScript(0,9, $ajax1);
                    $ajax1="   onChange=\"SalesReturnDiscDetailRp(_N_);SalesReturnRecalDiscPercent();\"  ";
                    $objAxpDB->GridScript(0,11, $ajax1);

                    $objAxpDB->GridAllowAddNew(0,false);
                    $objAxpDB->GridAllowDelete(0,false);
                }
                $objAxpDB->Process();
                $objAxpDB->setGrid2(0);
                $html = ob_get_contents();
                ob_end_clean();

                ?>
                    div = document.getElementById('txtHint0');
                    var m='<?php echo js_esc($html); ?>';
                    m= m.replace(/^\s*|\s*$/g,"");

                    div.innerHTML =m;
                    RowIndex0();
                    <!--reSize();-->
                <?php
                /* $query="SELECT fnindex, ftwarehouse_code, ftlocation_code, ftuom_code FROM txsales_order_det WHERE
                        ftso_no='{$_REQUEST["ftso_no"]}' ORDER BY fnindex ASC"; */
                $query="SELECT fnindex, ftwarehouse_code, ftlocation_code, ftuom_code FROM txsales_invoice_det WHERE
                        ftsales_invoice_no='{$_REQUEST["ftsales_invoice_no"]}' ORDER BY fnindex ASC";
                $mysql=mysql_query($query);
                while($r=mysql_fetch_assoc($mysql)):
                ?>
                    document.getElementById('ftwarehouse_codeD0<?php echo $r['fnindex']; ?>').value='<?php echo $r["ftwarehouse_code"];?>';
                    document.getElementById('ftlocation_codeD0<?php echo $r['fnindex']; ?>').value='<?php echo $r["ftlocation_code"];?>';
                    document.getElementById('ftuom_codeD0<?php echo $r['fnindex']; ?>').value='<?php echo $r["ftuom_code"]; ?>';
                <?php
                endwhile;

                //$ftcustomer_code=RetRstField("SELECT ftcustomer_code FROM txsales_invoice_hdr  WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}' and fnstatus<>9");
                //$ftcustomer_name=RetRstField("SELECT ftcustomer_name FROM tlcustomer  WHERE ftcustomer_code='{$ftcustomer_code}'");
                // $ssql="SELECT ffdiscount,ffvat,ftsales_code FROM txsales_order_hdr WHERE ftso_no='{$_REQUEST['ftso_no']}'";
                $ssql="SELECT ffdiscount,ffvat,ftsales_code FROM txsales_invoice_hdr WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";
                $rs=mysql_query($ssql) or die($ssql);
                $r=mysql_fetch_assoc($rs);
                ?>
                    document.getElementById('ffvat').value='<?php echo number_format($r['ffvat'],2) ?>';
                    <!-- document.getElementById('ffdiscount').value='<?php // echo number_format($r['ffdiscount'],2) ?>'; -->
                    document.getElementById('ftsales_code').value='<?php echo $r['ftsales_code'] ?>';

                    SalesReturnRecalPrice();
                <?php
                
               break;

           }
           exit;
    }

    $objAxpDB->Process();
    $objAxpDB->WriteHtmlTable();
    