<?php
        if($_REQUEST['ajax']) { 
           switch($_REQUEST['code']) {               
             case "transno":
              if(RetRstField("SELECT fnid FROM txsales_invoice_hdr WHERE ftsales_invoice_no='{$_REQUEST['fttrans_no']}'")) {
                  ?>
                  
                  alert('No. Invoice sudah dipakai...\n System akan mengenerate No. transaksi kembali ...\n Silahkan ulangi proses penyimpanan...  ');
                  OnChangeDate_fdtrans_date();
            <?php
              }
              else {
                    ?>
                   
                    
                  submitbutton('Simpan','','','','');
                   
            <?php
              }
            break;
     
           }
           die("");
           exit;
    }
    
    Getsecurity('saldoawal_piutang');
    $objAxpDB->ViewName("Daftar Saldo Awal Piutang");
    $objAxpDB->ViewSQL("SELECT A.fnid, A.ftsales_invoice_no,  A.ftcustomer_code, d.ftcustomer_name, A.fdtrans_date,  A.fddue_date, A.fcamount,A.fcpayment_amount, A.fcoutstanding_amount, C.fttrans_status_name AS fnpayment_status,  B.fttrans_status_name
                      FROM txsales_invoice_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 
                      left join tlcustomer d on a.ftcustomer_code=d.ftcustomer_code
                        WHERE A.fnsaldo_awal=1
                      ORDER BY A.ftsales_invoice_no DESC, A.fdtrans_date DESC");
    $objAxpDB->ViewCaption("ID, No. Invoice, Kode Customer,Customer, Tgl. Invoice, Tgl. Jatuh Tempo, Jumlah Piutang, Jumlah Terbayar, Sisa Pembayaran, Status Pembayaran, Status");
    
   
     $ajaxinvoice="ajax_do('penjualan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=invoice&ftdo_no='+ document.getElementById('ftdo_no').value);";
 
    if(!$_REQUEST['id']) {
            $strAutoNumber1=" ajax_do('query2.php?code=2&ftmodule_code=TN006&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftsales_invoice_no&no_trans=ftsales_invoice_no&table_name=txsales_invoice_hdr');";
            $strAutoNumber0=" function OnChangeDate_fdtrans_date() {                                                                      
                                      {$strAutoNumber1}                                    
                                    
                                }
                                 OnChangeDate_fdtrans_date();
                                 $ajaxinvoice
                                ";  
            $objAxpDB->FormJS($strAutoNumber0);
            $objAxpDB->Lookup("ftdo_no", "SELECT ftdo_no,fdtrans_date FROM txdelivery_order_hdr WHERE fnstatus=1 ORDER BY ftdo_no DESC,fdtrans_date  DESC", "ftdo_no", "fdtrans_date", -1,true);
     }
     
    $objAxpDB->FormName("Saldo Awal Piutang");
    $objAxpDB->FormSQL("SELECT ftsales_invoice_no, fdtrans_date,   fddue_date,ftcustomer_code, fcamount, fcpayment_amount, fcoutstanding_amount, fnpayment_status, fnstatus,fnsaldo_awal FROM txsales_invoice_hdr");
    $objAxpDB->FormCaption("No. Invoice, Tgl. Invoice,   Tgl. Jatuh Tempo, Customer, Jumlah Piutang , Jumlah Terbayar, Sisa Pembayaran,  Status Pembayaran, Status,fnsaldo_awal");
    $objAxpDB->FormPosition("TL, TL, TL, TL, TL, TL, TL, TL,TR ,HD");
     
    $objAxpDB->FormScript(4, "onChange=ValSaldoAwalPiutang1()");
 
    
    $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("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("ftcustomer_code", "SELECT ftcustomer_code, ftcustomer_name FROM tlcustomer", "ftcustomer_code", "ftcustomer_name", -1,false);
   
   
    $objAxpDB->SetStatus("id, fnstatus");
    if ($objAxpDB->GetStatus("id") == "")
            $objAxpDB->ButtonShow("baru, cetak_daftar, export_excel , tutup");
    elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->ButtonShow("reset, simpan, tutup");
             $objAxpDB->FormLocked("0,0,0,0,0,1,1,1,1");             
    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
            $objAxpDB->ButtonShow(" batal, tutup"); 
               $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1");     
           #$objAxpDB->Lookup("ftcustomer_code", "SELECT ftcustomer_code, ftcustomer_name FROM tlcustomer", "ftcustomer_code", "ftcustomer_name", 1,true);
    } elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
              $objAxpDB->FormLocked("1,1,1,1,1,1,1,1,1"); 
            $objAxpDB->ButtonShow("baru, tutup");
    }
    
    $objAxpDB->Process();
    
    $objAxpDB->WriteHtmlTable();