Search in sources :

Example 1 with I_M_MovementLine

use of org.compiere.model.I_M_MovementLine in project adempiere by adempiere.

the class InventoryUtil method getLineCosts.

private static BigDecimal getLineCosts(PO line, BigDecimal lineQty, boolean isSOTrx) {
    ArrayList<Object> params = new ArrayList<Object>();
    String sql = "SELECT" + " SUM(" + MCostDetail.COLUMNNAME_Amt + ")" + ",SUM(" + MCostDetail.COLUMNNAME_Qty + ")" + " FROM " + MCostDetail.Table_Name + " WHERE " + line.get_TableName() + "_ID=?";
    params.add(line.get_ID());
    if (line instanceof I_M_MovementLine) {
        sql += " AND " + MCostDetail.COLUMNNAME_IsSOTrx + "=?";
        params.add(isSOTrx);
    }
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    BigDecimal costs = null;
    BigDecimal qty = null;
    try {
        pstmt = DB.prepareStatement(sql, line.get_TrxName());
        DB.setParameters(pstmt, params);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            costs = rs.getBigDecimal(1);
            qty = rs.getBigDecimal(2);
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    if (costs == null)
        costs = Env.ZERO;
    if (qty == null)
        qty = Env.ZERO;
    // Convert Qty sign from Absolute to Relative (for comparation purposes)
    if (isSOTrx)
        qty = qty.negate();
    // Check Line Qty (if specified)
    if (lineQty != null && qty.compareTo(lineQty) != 0) {
        throw new RuntimeException("CostDetail Qty Not Match - TargetQty=" + lineQty + ", ActualQty=" + qty + ", Costs=" + costs);
    }
    return costs;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) I_M_MovementLine(org.compiere.model.I_M_MovementLine) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal)

Aggregations

BigDecimal (java.math.BigDecimal)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 DBException (org.adempiere.exceptions.DBException)1 I_M_MovementLine (org.compiere.model.I_M_MovementLine)1