<?php  

    Getsecurity('credit_memo');

    if($_REQUEST['ajax']) {
        switch($_REQUEST['code']) {
            case "transno":
              if(RetRstField("SELECT fnid FROM txcredit_memo_hdr WHERE ftcm_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;

        }       
       
    }
    
    $objAxpDB->ViewName("Daftar Credit Memo");
    #$objAxpDB->ViewTable("vKUCreditMemo");
    $objAxpDB->ViewSQL("SELECT  A.fnid, A.ftcm_no, A.fdtrans_date, c.ftcustomer_name, D.fttrans_status_name AS fntrans_type, A.fcinvoice_amount, A.fcoutstanding_amount, A.fccredit_amount, A.ftnotes,  B.fttrans_status_name FROM txcredit_memo_hdr 
                      A INNER JOIN tstransaction_status B ON a.fnstatus=b.fntrans_status_code AND fntrans_group_code=2  
                      INNER JOIN tlcustomer C ON A.ftcustomer_code=c.ftcustomer_code 
                      INNER JOIN tstransaction_status D ON  A.fntrans_type=D.fntrans_status_code AND D.fntrans_group_code = 5
                      WHERE (('{$_SESSION['strbranch']}' <> 'PUSAT' AND a.ftbranch_code = '{$_SESSION['strbranch']}') OR '{$_SESSION['strbranch']}' = 'PUSAT')
                      ORDER BY  A.ftcm_no DESC, A.fdtrans_date DESC");
    $objAxpDB->ViewCaption("ID, No. transaksi, Tgl. transaksi, Customer, Jenis transaksi, Total Credit Memo, Sisa Credit Memo, Total Pemb. Credit Memo, Catatan,  Status");
    $objAxpDB->ViewPeriod(TRUE);
    $objAxpDB->FormName("Credit Memo");
    $objAxpDB->FormSQL("SELECT ftcm_no, ftcustomer_code, fdtrans_date, fntrans_type, ftnotes,  fcinvoice_amount, fcoutstanding_amount, fccredit_amount, fnstatus FROM txcredit_memo_hdr");	
    $objAxpDB->FormCaption("No. transaksi, Customer, Tgl. transaksi, Jenis transaksi, Catatan,  Total Credit Memo, Sisa Credit Memo, Pemakaian Credit Memo, Status");
    $objAxpDB->FormPosition("TL, TL, TR, TR, BL, BR, BR, BR, BR");
   
   
    if(!$_REQUEST['id']) {
            $strAutoNumber1="ajax_do('query2.php?code=2&ftmodule_code=TN017&thbln=' + document.getElementById(\"fdtrans_date\").value + '&field_name=ftcm_no&no_trans=ftcm_no&table_name=txcredit_memo_hdr&ftbranch_code={$_SESSION['strbranch']}');";
             
            $strAutoNumber0=" function OnChangeDate_fdtrans_date() {                                                    
                                           {$strAutoNumber1}                                            
                                     } 
                                 OnChangeDate_fdtrans_date();  
                               
                                ";   
          
            $objAxpDB->FormJS($strAutoNumber0);
            $ajaxGrid="ajax_do('keuangan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id=0&ajax=1&code=grid&ftcustomer_code='+ document.getElementById('ftcustomer_code').value +'&fntrans_type='+  document.getElementById('fntrans_type').value );";
            
            $objAxpDB->FormScript(1, "onChange=\"{$ajaxGrid}\"");
            $objAxpDB->FormScript(3, "onChange=\"{$ajaxGrid}\"");
         
            $ajaxinvoice="ajax_do('keuangan.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&ajax=1&code=invoice&ftsales_invoice_no='+ document.getElementById('ftsales_invoice_noD0_N_').value +'&fdsales_invoice_date=fdsales_invoice_dateD0_N_&fcinvoice_amount=fcinvoice_amountD0_N_&fcoutstanding_amount=fcoutstanding_amountD0_N_&fcoutstanding_credit_memo=fcoutstanding_credit_memoD0_N_');";
            $objAxpDB->GridScript(0,1,"OnChange=\"$ajaxinvoice\"");
            //---------------
   }

    $objAxpDB->GridSQL(0, "SELECT fnindex, ftsales_invoice_no, fdsales_invoice_date, ftdescription, fcinvoice_amount, fcoutstanding_amount, fccredit_amount, fcoutstanding_credit_memo FROM txcredit_memo_det");
    $objAxpDB->GridCaption(0, "No., No. Invoice, Tgl. Invoice, Keterangan, Jumlah Tagihan, Sisa Pembayaran, Credit Memo, Sisa Credit Memo");
    
    $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( "ftcustomer_code", "SELECT ftcustomer_code, ftcustomer_name FROM tlcustomer", "ftcustomer_code", "ftcustomer_name", -1,false);
    $objAxpDB->Lookup( "fntrans_type", "SELECT fntrans_status_code, fttrans_status_name FROM tstransaction_status WHERE fntrans_group_code = 5", "fntrans_status_code", "fttrans_status_name", -1,true);
    $ssql="SELECT ftsales_invoice_no,ftdo_no FROM txsales_invoice_hdr x WHERE fnstatus<>9 AND ftcustomer_code='{$_REQUEST['ftcustomer_code']}'";
    $ssql.=" and exists (select a.ftsales_invoice_no,fcamount-ifnull(b.fccredit_amount,0) as fcoutstanding_credit_memo
                from txsales_invoice_hdr a
                left join (
                                select a2.ftsales_invoice_no, sum(a2.fccredit_amount) as fccredit_amount
                                from txcredit_memo_hdr a1,
                                        txcredit_memo_det a2
                                where a1.ftcm_no=a2.ftcm_no and a1.fnstatus<>9
                                group by a2.ftsales_invoice_no
                        ) b
                on a.ftsales_invoice_no=b.ftsales_invoice_no 
                where fcamount-ifnull(b.fccredit_amount,0)>0
                  and x.ftsales_invoice_no=a.ftsales_invoice_no 
                  AND a.ftbranch_code='{$_SESSION['strbranch']}'
                ) ";

    $objAxpDB->Lookup("ftsales_invoice_no",$ssql,"ftsales_invoice_no","ftdo_no",-1,true);
    
    $objAxpDB->SetStatus("id, fnstatus,fntrans_type");
    if ($objAxpDB->GetStatus("id") == "") {
            $objAxpDB->ButtonShow("baru");
            
    } elseif ($objAxpDB->GetStatus("id") == 0) {
            $objAxpDB->GridAllowAddNew(0,true);
            $objAxpDB->GridAllowDelete(0,true);
            $objAxpDB->GridLocked(0, "1, 0, 1, 0, 1, 1, 0, 1");
            $objAxpDB->GridHidden(0, "0, 0, 0, 0, 0, 0, 0, 1");
            $objAxpDB->ButtonShow("reset, simpan, tutup");
            $objAxpDB->FormLocked("1, 0, 0, 0, 0,  1, 1, 1, 1");
            $objAxpDB->GridScript(0,6, "onChange=\"CreditMemoRecalQty();\"");
            $objAxpDB->GridDelJS(0,"CreditMemoRecalQty();");

    } elseif ($objAxpDB->GetStatus("fnstatus") == 1) {
            $objAxpDB->ButtonShow("baru, cetak, batal, tutup");
            $objAxpDB->FormLocked("1, 1, 1, 1,1,  1, 1, 1, 1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->GridHidden(0, "0, 0, 0, 0, 0, 0, 0, 1");
            unset($objAxpDB->lookup["ftsales_invoice_no"]);

    } elseif ($objAxpDB->GetStatus("fnstatus") == 9) {
            $objAxpDB->FormLocked("1, 1, 1, 1, 1,  1, 1, 1, 1");
            $objAxpDB->GridLocked(0, "1, 1, 1, 1, 1, 1, 1, 1");
            $objAxpDB->GridHidden(0, "0, 0, 0, 0, 0, 0, 0, 1");
            $objAxpDB->ButtonShow("tutup");
            unset($objAxpDB->lookup["ftsales_invoice_no"]);
    }
    if($objAxpDB->GetStatus("id")&&$objAxpDB->GetStatus("fntrans_type")=='3') {
          $objAxpDB->GridSQL(0, "SELECT fnindex,  ftdescription, fccredit_amount FROM txcredit_memo_det");
          $objAxpDB->GridCaption(0, "No.,  Keterangan, Credit Memo");      
          $objAxpDB->GridLocked(0, "1, 1, 1");
    }
    
    if($_REQUEST['ajax']) { 
           switch($_REQUEST['code']) {
           case "grid":
               ob_start();    
               if($_REQUEST['fntrans_type']==3) {
                      $objAxpDB->GridSQL(0, "SELECT fnindex,  ftdescription, fccredit_amount FROM txcredit_memo_det");
                      $objAxpDB->GridCaption(0, "No.,  Keterangan, Credit Memo");      
                      $objAxpDB->GridLocked(0, "1, 0, 0");
                      $objAxpDB->GridScript(0,1,"");
                      $objAxpDB->GridScript(0,2, "onChange=\"CreditMemoRecalQty();\"");
               }
               $objAxpDB->Process();
               $objAxpDB->setGrid1(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;                                   
                    RowBuild0();
               <?php 
             
               break;
           case "invoice":
                $ssql="SELECT fdtrans_date,fcamount,fcoutstanding_amount 
                        FROM txsales_invoice_hdr
                        WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";
                $rs=mysql_query($ssql);
                $r=mysql_fetch_assoc($rs);       
                ?>                        
                    document.getElementById('<?php echo $_REQUEST['fdsales_invoice_date']; ?>').value='<?php echo CDateRev($r['fdtrans_date']); ?>';
                    document.getElementById('<?php echo $_REQUEST['fcinvoice_amount']; ?>').value='<?php echo number_format($r['fcamount'],2) ;  ?>';
                    document.getElementById('<?php echo $_REQUEST['fcoutstanding_amount']; ?>').value='<?php echo number_format($r['fcoutstanding_amount'],2); ?>';                   
                <?php
                $sql="select a.ftsales_invoice_no,fcamount-ifnull(b.fccredit_amount,0) as fcoutstanding_credit_memo
                        from txsales_invoice_hdr a
                        left join (
                                        select a2.ftsales_invoice_no, sum(a2.fccredit_amount) as fccredit_amount
                                        from txcredit_memo_hdr a1,
                                                txcredit_memo_det a2
                                        where a1.ftcm_no=a2.ftcm_no and a1.fnstatus<>9
                                        group by a2.ftsales_invoice_no
                                ) b
                        on a.ftsales_invoice_no=b.ftsales_invoice_no 
                        where a.ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";
                $rs=mysql_query($sql);
                $r=mysql_fetch_assoc($rs);
                $fcamount=$r['fcamount'];
                $fcoutstanding_amount=$r['fcoutstanding_amount'];
                $fcoutstanding_credit_memo=$r['fcoutstanding_credit_memo'];

                $sql="select SUM(b.fccredit_amount) FROM txcredit_memo_hdr a INNER JOIN txcredit_memo_det b ON a.ftcm_no=b.ftcm_no WHERE b.ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}' AND a.fnstatus <> '9'";
                $rs=mysql_query($sql);
                $r=mysql_fetch_assoc($rs);
                $fccredit_amount=$r['fccredit_amount'];

                //$fcamount_total=$fcamount - $fcoutstanding_amount - $fccredit_amount;
                $fcamount_total=$fcoutstanding_credit_memo;
                if($fctotal_amount < 0) $fctotal_amount=0;

                ?>
                    document.getElementById('<?php echo $_REQUEST['fcoutstanding_credit_memo']; ?>').value='<?php echo  number_format($fcamount_total, 2); ?>';
                <?php
               break;
           }
           die("");
           exit;
    }
    
    $objAxpDB->Process();
    
    $objAxpDB->WriteHtmlTable();
    
?>    