public static BigDecimal available(int p_C_Payment_ID) throws SQLException {
  if (p_C_Payment_ID == 0)   return null;
  int C_Currency_ID=0;
  int C_ConversionType_ID=0;
  BigDecimal PayAmt=null;
  int C_Charge_ID=0;
  String sql="SELECT C_Currency_ID, C_ConversionType_ID, PayAmt, C_Charge_ID " + "FROM C_Payment_v " + "WHERE C_Payment_ID=?";
  PreparedStatement pstmt=Adempiere.prepareStatement(sql);
  pstmt.setInt(1,p_C_Payment_ID);
  ResultSet rs=pstmt.executeQuery();
  if (rs.next()) {
    C_Currency_ID=rs.getInt(1);
    C_ConversionType_ID=rs.getInt(2);
    PayAmt=rs.getBigDecimal(3);
    C_Charge_ID=rs.getInt(4);
  }
  rs.close();
  pstmt.close();
  if (PayAmt == null)   return null;
  if (C_Charge_ID != 0)   return Adempiere.ZERO;
  BigDecimal allocatedAmt=getAllocatedAmt(p_C_Payment_ID,C_Currency_ID,C_ConversionType_ID);
  BigDecimal available=PayAmt.subtract(allocatedAmt);
  return Currency.round(available,C_Currency_ID,null);
}

public static BigDecimal available ( int p_C_Payment_ID ) throws SQLException { if ( p_C_Payment_ID == 0 ) return null ; int C_Currency_ID = 0 ; int C_ConversionType_ID = 0 ; BigDecimal PayAmt = null ; int C_Charge_ID = 0 ; String sql = "SELECT C_Currency_ID, C_ConversionType_ID, PayAmt, C_Charge_ID " + "FROM C_Payment_v " + "WHERE C_Payment_ID=?" ; PreparedStatement pstmt = Adempiere . prepareStatement ( sql ) ; pstmt . setInt ( 1 , p_C_Payment_ID ) ; ResultSet rs = pstmt . executeQuery ( ) ; if ( rs . next ( ) ) { C_Currency_ID = rs . getInt ( 1 ) ; C_ConversionType_ID = rs . getInt ( 2 ) ; PayAmt = rs . getBigDecimal ( 3 ) ; C_Charge_ID = rs . getInt ( 4 ) ; } rs . close ( ) ; pstmt . close ( ) ; if ( PayAmt == null ) return null ; if ( C_Charge_ID != 0 ) return Adempiere . ZERO ; BigDecimal allocatedAmt = getAllocatedAmt ( p_C_Payment_ID , C_Currency_ID , C_ConversionType_ID ) ; BigDecimal available = PayAmt . subtract ( allocatedAmt ) ; return Currency . round ( available , C_Currency_ID , null ) ; }
get available payment amount in payment currency
