<?php

    Getsecurity("sales_invoice_adjustment");
    
    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
                if(RetRstField("SELECT fnid FROM txsales_invoice_adjustment_hdr WHERE ftsales_invoice_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;
            
            case "tgl":
                if ($_REQUEST['termpayment'] == '') {$_REQUEST['termpayment'] = 0; }
                $ssql="select date_add('".CDate($_REQUEST['tgl'])."',INTERVAL ".intval($_REQUEST['ftterm_payment'])." DAY)";
                $date=RetRstField($ssql);
            
                ?>
                document.getElementById('fddue_date').value='<?php echo CDateRev($date); ?>';
                <?php
                
                die("");
                break;    
            
            // set tanggal jatuh tempo    
            case "term_payment":
                if ($_REQUEST['termpayment'] == '') {$_REQUEST['termpayment'] = 0; }
                $ssql="select date_add('".CDate($_REQUEST['tgl'])."',INTERVAL {$_REQUEST['termpayment']} DAY)";
                $date=RetRstField($ssql);
                ?>
                document.getElementById('fddue_date').value='<?php echo CDateRev($date); ?>';
                <?php

                die("");
                break;
                
            // Set keterangan dan data customer    
            case "customer":
                $ssql=" SELECT ftcustomer_code, ftcustomer_name, IFNULL(ftaddress,'') AS ftship_address, ftterm_payment, ftsales_code
                        FROM tlcustomer WHERE ftcustomer_code='{$_REQUEST['ftcustomer_code']}'";

                $rs=mysql_query($ssql) ;
                $r=mysql_fetch_assoc($rs);
                $term_payment = $r['ftterm_payment'];

                if ($term_payment == '') {$term_payment = 0; }
                $ssql="select date_add('".CDate($_REQUEST['tgl'])."',INTERVAL {$term_payment} DAY)";
                $date=RetRstField($ssql);

                ?>
                document.getElementById('ftship_address').value='<?php echo js_esc($r['ftship_address']); ?>';
                document.getElementById('ftterm_payment').value = <?php echo $term_payment; ?>;
                document.getElementById('fddue_date').value='<?php echo CDateRev($date); ?>';
                <?php

                die("");
                break;
           
            case "idr":
                $ssql="SELECT fcrate FROM tlcurrency_hdr WHERE ftsimbol='{$_REQUEST["ftcurrency_code"]}'";
                $mysql=mysql_query($ssql) or die ($ssql);
                $r=mysql_fetch_assoc($mysql);
                
                ?>
                document.getElementById('fcrate').value='<?php echo number_format($r["fcrate"], 2); ?>';
                ElementCurrencyALL();
                <?php
                
                die("");
                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;
        }
        
    }

    $objAxpDB->LookupMagicQuotes(True);
    
    $objAxpDB->ViewName("Daftar Sales Invoice Adjustment");
    $objAxpDB->ViewSQL("SELECT A.fnid, A.fdtrans_date, A.ftsales_invoice_no, A.ftso_no, CONCAT(A.ftcustomer_code,' - ',d.ftcustomer_name) as ftcustomer_name, 
                                A.fcamount, C.fttrans_status_name AS fnpayment_status,  B.fttrans_status_name as fnstatus
                        FROM txsales_invoice_adjustment_hdr  A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND B.fntrans_group_code=2
                        INNER JOIN tstransaction_status C ON a.fnpayment_status=C.fntrans_status_code AND C.fntrans_group_code=1
                        INNER JOIN tlcustomer d ON a.ftcustomer_code = d.ftcustomer_code
                        WHERE (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')
                        ORDER BY  A.fdtrans_date DESC,ftsales_invoice_no DESC");
    $objAxpDB->ViewCaption("ID, Tgl. Invoice, No. Invoice, No. Sales Order, Nama Customer, Total Invoice, Status Pembayaran, Status");
    $objAxpDB->ViewPeriod(TRUE);

    $ajaxinvoice="ajax_do_grid0('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=invoice&ftdo_no='+ document.getElementById('ftdo_no').value);";
    $ajaxKurs="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=idr&ftcurrency_code='+ document.getElementById('ftcurrency_code').value);";

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

                                    ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=tgl&tgl=' + document.getElementById('fdtrans_date').value + '&ftterm_payment=' + document.getElementById('ftterm_payment').value);
     
                                }
                                
                                OnChangeDate_fdtrans_date();
                                document.getElementById('ftcurrency_code').value='IDR';
                                {$ajaxKurs}
                                ElementCurrencyALL(); ";
                                
            $objAxpDB->FormJS($strAutoNumber0);
            $objAxpDB->Lookup("ftso_no", "SELECT ftso_no, fdtrans_date FROM txsales_order_hdr WHERE fnstatus!=3 and fnstatus!=9 ", "ftso_no", "fdtrans_date", -1, False, "' AND ftcustomer_code=\''+ document.getElementById('ftcustomer_code').value + '\''");
    
    } else {
            $objAxpDB->Lookup("ftso_no", "SELECT ftso_no, fdtrans_date FROM txsales_order_hdr WHERE 1 ", "ftso_no", "fdtrans_date", -1, False, "' AND ftcustomer_code=\''+ document.getElementById('ftcustomer_code').value + '\''");
            $objAxpDB->FormJS("ElementCurrencyALL();");
    }

    $objAxpDB->FormName("Sales Invoice Adjustment");
    $objAxpDB->FormSQL("SELECT ftsales_invoice_no, ftcustomer_code, ftship_address, ftso_no, ftitem_type, fdtrans_date, ftterm_payment, fddue_date, ftcurrency_code, fcrate, fntax_code, ftreff_no, fnpayment_status, fdvat_date, ftvat_no, ftnotes, fcpayment_amount, fcoutstanding_amount, fcsub_amount, ffdiscount, fcdiscount, fcother_cost, ffvat, fcvat, fcamount, fnstatus, fcreturn_amount, ftsales_code FROM txsales_invoice_adjustment_hdr");
    $objAxpDB->FormCaption("No. Invoice, Customer, Alamat,  No. SO, Tipe Invoice, Tgl. Invoice,  Syarat Pembayaran, Tgl. Jatuh Tempo, Mata Uang, Kurs, Kode Pajak, Referensi, Status Pembayaran, Tgl. Faktur Pajak, No. Faktur Pajak, Catatan, Jumlah Terbayar, Sisa Pembayaran,   Sub Total, Discount (%), Discount(Rp), Biaya Lain-lain, Pajak (%), Pajak (Rp), Grand Total, Status, Jumlah Retur, Salesman");
    $objAxpDB->FormPosition("TL, TL, TL, TL, HD, TR, TR, TR, TR, TR, HD, TL, TR, HD, HD, BL, BL, BL, BR, HD, HD, HD, BR, BR, BR, BR, HD, TR");
    
    $objAxpDB->GridSQL(0, "SELECT fnindex, ftitem_code,fnqty, ftuom_code, fcprice, ffdiscount, fcdiscount, fcamount, fnqty_base, ffformula FROM txsales_invoice_adjustment_det");
    $objAxpDB->GridCaption(0, "No., Kode Barang, Qty, Satuan, Harga, Disc(%), Disc(Rp), Jml Total, Qty Base, Formula");
    $objAxpDB->GridDelJS(0,"SalesInvoiceAdjustmentRecalQty();");
    
    $ajaxcustomer="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=customer&ftcustomer_code='+ document.getElementById('ftcustomer_code').value+'&tgl='+ document.getElementById('fdtrans_date').value);";    
    $objAxpDB->FormScript(1, "onChange=\"{$ajaxcustomer}\"");
    
    $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);";
    $objAxpDB->FormScript(3, "onChange=\"{$ajaxGrid}\"");
    
    $ajax_due="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=term_payment&ftso_no='+ document.getElementById('ftso_no').value +'&tgl='+ document.getElementById('fdtrans_date').value + '&termpayment='+ document.getElementById('ftterm_payment').value);";
    $objAxpDB->FormScript(5, "onChange=\"{$ajax_due}\"");
    $objAxpDB->FormScript(6, "onChange=\"{$ajax_due}\"");    
    $objAxpDB->FormScript(8, "OnChange=\"{$ajaxKurs}\"");
    /*$objAxpDB->FormScript(10, "onChange=\"OnChangeDate_fdtrans_date();\"");
    $objAxpDB->FormScript(19, "onChange=\"SalesInvoiceRecalDiscPercent();\"");
    $objAxpDB->FormScript(20, "onChange=\"SalesInvoiceRecalDiscRp();\"");
    $objAxpDB->FormScript(21, "onChange=\"SalesInvoiceRecalQty0();\""); */
    $objAxpDB->FormScript(22, "onChange=\"SalesInvoiceAdjustmentRecalQty();\""); 
    
    
    $objAxpDB->Lookup("ftcurrency_code", "SELECT  ftsimbol, ftdescription  FROM tlcurrency_hdr ORDER BY ftsimbol", "ftsimbol", "ftdescription", 0, True);
    $objAxpDB->Lookup("ftpayment_method", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 8", "fntrans_status_code", "fttrans_status_name", -1,true);
    $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("fntax_code", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 15", "fntrans_status_code", "fttrans_status_name", -1,true);
    $objAxpDB->Lookup("fnpayment_status", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 1", "fntrans_status_code", "fttrans_status_name", 1,true);
    $objAxpDB->Lookup("ftitem_code", "SELECT ftitem_code, ftitem_name FROM tlstock_hdr where fnstatus = 1 ", "ftitem_code", "ftitem_name", -1,false);
    // $objAxpDB->Lookup("ftitem_type","SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fttrans_group_name='tipe barang invoice'","fntrans_status_code","fttrans_status_name",1,true);
    $objAxpDB->Lookup("ftcustomer_code","SELECT ftcustomer_code, ftcustomer_name FROM tlcustomer","ftcustomer_code","ftcustomer_name", -1,false);
    $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->SetStatus("id, fnstatus");
    if ($objAxpDB->GetStatus("id") == "") {
            $objAxpDB->ButtonShow("baru");
    } elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->FormLocked("1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0");
            $objAxpDB->GridLocked(0, "1, 0, 1, 1, 1, 1, 1, 0, 1, 1");
            $objAxpDB->GridHidden(0, "0, 0, 1, 1, 1, 1, 1, 0, 1, 1");
            $objAxpDB->GridAllowAddNew(0,true);
            $objAxpDB->GridAllowDelete(0,true);
            $objAxpDB->GridAutoAdd(0,false);
            
            $ajax1=" onChange=\"SalesInvoiceAdjustmentRecalQty();\"  ";
            $objAxpDB->GridScript(0,7, $ajax1);
                
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            
    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
            // revisi
            if(isset($_REQUEST['revisi'])) 
            {   
                $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0");
                $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 0, 1, 1");
                $objAxpDB->GridHidden(0, "0, 0, 1, 1, 1, 1, 1, 0, 1, 1");
                //$objAxpDB->GridAllowAddNew(0,true);
                //$objAxpDB->GridAllowDelete(0,true);
                //$objAxpDB->GridAutoAdd(0,false);

                $ajax1=" onChange=\"SalesInvoiceAdjustmentRecalQty();\"  ";
                $objAxpDB->GridScript(0,7, $ajax1);

                $objAxpDB->ButtonShow("simpan_revisi, tutup");

            } else {    
                $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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");
                $objAxpDB->GridHidden(0, "0, 0, 1, 1, 1, 1, 1, 0, 1, 1");
                $objAxpDB->ButtonShow("baru, cetak, revisi, batal, tutup");
            }    
    } elseif (($objAxpDB->GetStatus("fnstatus") == 3) OR ($objAxpDB->GetStatus("fnstatus") == 9)) {
            $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->GridHidden(0, "0, 0, 1, 1, 1, 1, 1, 0, 1, 1");
            $objAxpDB->FormLocked("1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1, 1, 1, 1");
            $objAxpDB->ButtonShow("tutup");
    } 

    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "grid":
                ob_start();
                $ssql="SELECT b.fnindex,b.ftitem_code,b.fnqty, b.ftuom_code, b.fcprice, b.ffdiscount,'0.00' AS fcdiscount, 
                                ROUND(b.fcamount-b.fcamount,2) AS fcamount, b.fnqty_base-b.fnqty_base as fnqty_base, b.ffformula
                        FROM txsales_invoice_hdr a
                        INNER JOIN txsales_invoice_det b ON a.ftsales_invoice_no=b.ftsales_invoice_no
                        WHERE a.ftso_no= '".$_REQUEST['ftso_no']."' AND a.fnstatus!=3 and a.fnstatus!=9
                        ORDER BY b.fnindex #";
                $objAxpDB->GridSQL(0,$ssql);
                $objAxpDB->GridCaption(0, "No., Kode Barang, Qty, Satuan, Harga, Disc(%), Disc(Rp), Jml Total, Qty Base, Formula");
                $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 0, 1, 1");
                $objAxpDB->GridHidden(0, "0, 0, 1, 1, 1, 1, 1, 0, 1, 1");
            
                $ajax1=" onChange=\"SalesInvoiceAdjustmentRecalQty();\"  ";
                $objAxpDB->GridScript(0,7, $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();
                  
                <?php
/*                $query="SELECT fnindex, ftwarehouse_code, ftlocation_code, ftuom_code FROM txsales_order_det WHERE
                        ftso_no='{$_REQUEST["ftso_no"]}' ORDER BY fnindex ASC";
                $mysql=mysql_query($query);
                while($r=mysql_fetch_assoc($mysql)):
                ?>
                    document.getElementById('ftuom_codeD0<?php echo $r['fnindex']; ?>').value='<?php echo $r["ftuom_code"]; ?>';
                <?php
                endwhile;
*/
               break;

           }
           exit;
    }
    
    $objAxpDB->Process();

    $objAxpDB->WriteHtmlTable();