public String invoice(Properties ctx,int WindowNo,GridTab mTab,GridField mField,Object value){
  Integer C_Invoice_ID=(Integer)value;
  if (isCalloutActive() || C_Invoice_ID == null || C_Invoice_ID.intValue() == 0)   return "";
  int C_Payment_ID=Env.getContextAsInt(ctx,WindowNo,"C_Payment_ID");
  MPayment payment=new MPayment(ctx,C_Payment_ID,null);
  if (payment.getC_Charge_ID() != 0 || payment.getC_Invoice_ID() != 0 || payment.getC_Order_ID() != 0)   return Msg.getMsg(ctx,"PaymentIsAllocated");
  mTab.setValue("DiscountAmt",Env.ZERO);
  mTab.setValue("WriteOffAmt",Env.ZERO);
  mTab.setValue("OverUnderAmt",Env.ZERO);
  int C_InvoicePaySchedule_ID=0;
  if (Env.getContextAsInt(ctx,WindowNo,Env.TAB_INFO,"C_Invoice_ID") == C_Invoice_ID.intValue() && Env.getContextAsInt(ctx,WindowNo,Env.TAB_INFO,"C_InvoicePaySchedule_ID") != 0) {
    C_InvoicePaySchedule_ID=Env.getContextAsInt(ctx,WindowNo,Env.TAB_INFO,"C_InvoicePaySchedule_ID");
  }
  Timestamp ts=Env.getContextAsDate(ctx,WindowNo,"DateTrx");
  String sql="SELECT C_BPartner_ID,C_Currency_ID," + " invoiceOpen(C_Invoice_ID, ?)," + " invoiceDiscount(C_Invoice_ID,?,?), IsSOTrx "+ "FROM C_Invoice WHERE C_Invoice_ID=?";
  PreparedStatement pstmt=null;
  ResultSet rs=null;
  try {
    pstmt=DB.prepareStatement(sql,null);
    pstmt.setInt(1,C_InvoicePaySchedule_ID);
    pstmt.setTimestamp(2,ts);
    pstmt.setInt(3,C_InvoicePaySchedule_ID);
    pstmt.setInt(4,C_Invoice_ID.intValue());
    rs=pstmt.executeQuery();
    if (rs.next()) {
      BigDecimal InvoiceOpen=rs.getBigDecimal(3);
      if (InvoiceOpen == null)       InvoiceOpen=Env.ZERO;
      BigDecimal DiscountAmt=rs.getBigDecimal(4);
      if (DiscountAmt == null)       DiscountAmt=Env.ZERO;
      mTab.setValue("InvoiceAmt",InvoiceOpen);
      mTab.setValue("Amount",InvoiceOpen.subtract(DiscountAmt));
      mTab.setValue("DiscountAmt",DiscountAmt);
      Env.setContext(ctx,WindowNo,"C_Invoice_ID",C_Invoice_ID.toString());
      mTab.setValue("C_Invoice_ID",C_Invoice_ID);
    }
  }
 catch (  SQLException e) {
    log.log(Level.SEVERE,sql,e);
    return e.getLocalizedMessage();
  }
 finally {
    DB.close(rs,pstmt);
    rs=null;
    pstmt=null;
  }
  return "";
}

public String invoice ( Properties ctx , int WindowNo , GridTab mTab , GridField mField , Object value ) { Integer C_Invoice_ID = ( Integer ) value ; if ( isCalloutActive ( ) || C_Invoice_ID == null || C_Invoice_ID . intValue ( ) == 0 ) return "" ; int C_Payment_ID = Env . getContextAsInt ( ctx , WindowNo , "C_Payment_ID" ) ; MPayment payment = new MPayment ( ctx , C_Payment_ID , null ) ; if ( payment . getC_Charge_ID ( ) != 0 || payment . getC_Invoice_ID ( ) != 0 || payment . getC_Order_ID ( ) != 0 ) return Msg . getMsg ( ctx , "PaymentIsAllocated" ) ; mTab . setValue ( "DiscountAmt" , Env . ZERO ) ; mTab . setValue ( "WriteOffAmt" , Env . ZERO ) ; mTab . setValue ( "OverUnderAmt" , Env . ZERO ) ; int C_InvoicePaySchedule_ID = 0 ; if ( Env . getContextAsInt ( ctx , WindowNo , Env . TAB_INFO , "C_Invoice_ID" ) == C_Invoice_ID . intValue ( ) && Env . getContextAsInt ( ctx , WindowNo , Env . TAB_INFO , "C_InvoicePaySchedule_ID" ) != 0 ) { C_InvoicePaySchedule_ID = Env . getContextAsInt ( ctx , WindowNo , Env . TAB_INFO , "C_InvoicePaySchedule_ID" ) ; } Timestamp ts = Env . getContextAsDate ( ctx , WindowNo , "DateTrx" ) ; String sql = "SELECT C_BPartner_ID,C_Currency_ID," + " invoiceOpen(C_Invoice_ID, ?)," + " invoiceDiscount(C_Invoice_ID,?,?), IsSOTrx " + "FROM C_Invoice WHERE C_Invoice_ID=?" ; PreparedStatement pstmt = null ; ResultSet rs = null ; try { pstmt = DB . prepareStatement ( sql , null ) ; pstmt . setInt ( 1 , C_InvoicePaySchedule_ID ) ; pstmt . setTimestamp ( 2 , ts ) ; pstmt . setInt ( 3 , C_InvoicePaySchedule_ID ) ; pstmt . setInt ( 4 , C_Invoice_ID . intValue ( ) ) ; rs = pstmt . executeQuery ( ) ; if ( rs . next ( ) ) { BigDecimal InvoiceOpen = rs . getBigDecimal ( 3 ) ; if ( InvoiceOpen == null ) InvoiceOpen = Env . ZERO ; BigDecimal DiscountAmt = rs . getBigDecimal ( 4 ) ; if ( DiscountAmt == null ) DiscountAmt = Env . ZERO ; mTab . setValue ( "InvoiceAmt" , InvoiceOpen ) ; mTab . setValue ( "Amount" , InvoiceOpen . subtract ( DiscountAmt ) ) ; mTab . setValue ( "DiscountAmt" , DiscountAmt ) ; Env . setContext ( ctx , WindowNo , "C_Invoice_ID" , C_Invoice_ID . toString ( ) ) ; mTab . setValue ( "C_Invoice_ID" , C_Invoice_ID ) ; } } catch ( SQLException e ) { log . log ( Level . SEVERE , sql , e ) ; return e . getLocalizedMessage ( ) ; } finally { DB . close ( rs , pstmt ) ; rs = null ; pstmt = null ; } return "" ; }
payment_invoice . when invoice selected - set invoiceamt = invoiceopen - discountamt = c_invoice_discount ( id , datetrx ) - amount = invoiceopen ( id ) - discount - writeoffamt , overunderamt = 0
