Search in sources :

Example 81 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class Tax method getProduct.

//	getCharge
/**
	 *	Get Tax ID - converts parameters to call Get Tax.
	 *  <pre>
	 *		M_Product_ID				->	C_TaxCategory_ID
	 *		billDate					->	billDate
	 *		shipDate					->	shipDate (ignored)
	 *		AD_Org_ID					->	billFromC_Location_ID
	 *		M_Warehouse_ID				->	shipFromC_Location_ID (ignored)
	 *		billC_BPartner_Location_ID  ->	billToC_Location_ID
	 *		shipC_BPartner_Location_ID 	->	shipToC_Location_ID (ignored)
	 *
	 *  if IsSOTrx is false, bill and ship are reversed
	 *  </pre>
	 * 	@param ctx	context
	 * 	@param M_Product_ID product
	 * 	@param billDate invoice date
	 * 	@param shipDate ship date (ignored)
	 * 	@param AD_Org_ID org
	 * 	@param M_Warehouse_ID warehouse (ignored)
	 * 	@param billC_BPartner_Location_ID invoice location
	 * 	@param shipC_BPartner_Location_ID ship location (ignored)
	 * 	@param IsSOTrx is a sales trx
	 * 	@return C_Tax_ID
	 *  If error it returns 0 and sets error log (TaxCriteriaNotFound)
	 */
public static int getProduct(Properties ctx, int M_Product_ID, Timestamp billDate, Timestamp shipDate, int AD_Org_ID, int M_Warehouse_ID, int billC_BPartner_Location_ID, int shipC_BPartner_Location_ID, boolean IsSOTrx) {
    String variable = "";
    int C_TaxCategory_ID = 0;
    int shipFromC_Location_ID = 0;
    int shipToC_Location_ID = 0;
    int billFromC_Location_ID = 0;
    int billToC_Location_ID = 0;
    String IsTaxExempt = null;
    String IsSOTaxExempt = null;
    String IsPOTaxExempt = null;
    String sql = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        //	Get all at once
        sql = "SELECT p.C_TaxCategory_ID, o.C_Location_ID, il.C_Location_ID, b.IsTaxExempt, b.IsPOTaxExempt, " + " w.C_Location_ID, sl.C_Location_ID " + "FROM M_Product p, AD_OrgInfo o," + " C_BPartner_Location il INNER JOIN C_BPartner b ON (il.C_BPartner_ID=b.C_BPartner_ID) " + " LEFT OUTER JOIN M_Warehouse w ON (w.M_Warehouse_ID=?), C_BPartner_Location sl " + "WHERE p.M_Product_ID=?" + " AND o.AD_Org_ID=?" + " AND il.C_BPartner_Location_ID=?" + " AND sl.C_BPartner_Location_ID=?";
        pstmt = DB.prepareStatement(sql, null);
        pstmt.setInt(1, M_Warehouse_ID);
        pstmt.setInt(2, M_Product_ID);
        pstmt.setInt(3, AD_Org_ID);
        pstmt.setInt(4, billC_BPartner_Location_ID);
        pstmt.setInt(5, shipC_BPartner_Location_ID);
        rs = pstmt.executeQuery();
        boolean found = false;
        if (rs.next()) {
            C_TaxCategory_ID = rs.getInt(1);
            billFromC_Location_ID = rs.getInt(2);
            billToC_Location_ID = rs.getInt(3);
            IsSOTaxExempt = rs.getString(4);
            IsPOTaxExempt = rs.getString(5);
            IsTaxExempt = IsSOTrx ? IsSOTaxExempt : IsPOTaxExempt;
            shipFromC_Location_ID = rs.getInt(6);
            shipToC_Location_ID = rs.getInt(7);
            found = true;
        }
        DB.close(rs, pstmt);
        //
        if (found && "Y".equals(IsTaxExempt)) {
            log.fine("getProduct - Business Partner is Tax exempt");
            return getExemptTax(ctx, AD_Org_ID);
        } else if (found) {
            if (!IsSOTrx) {
                int temp = billFromC_Location_ID;
                billFromC_Location_ID = billToC_Location_ID;
                billToC_Location_ID = temp;
                temp = shipFromC_Location_ID;
                shipFromC_Location_ID = shipToC_Location_ID;
                shipToC_Location_ID = temp;
            }
            log.fine("getProduct - C_TaxCategory_ID=" + C_TaxCategory_ID + ", billFromC_Location_ID=" + billFromC_Location_ID + ", billToC_Location_ID=" + billToC_Location_ID + ", shipFromC_Location_ID=" + shipFromC_Location_ID + ", shipToC_Location_ID=" + shipToC_Location_ID);
            return get(ctx, C_TaxCategory_ID, IsSOTrx, shipDate, shipFromC_Location_ID, shipToC_Location_ID, billDate, billFromC_Location_ID, billToC_Location_ID);
        }
        // ----------------------------------------------------------------
        //	Detail for error isolation
        //	M_Product_ID				->	C_TaxCategory_ID
        variable = "M_Product_ID";
        sql = "SELECT C_TaxCategory_ID FROM M_Product WHERE M_Product_ID=?";
        C_TaxCategory_ID = DB.getSQLValueEx(null, sql, M_Product_ID);
        found = C_TaxCategory_ID != -1;
        if (C_TaxCategory_ID <= 0) {
            throw new TaxCriteriaNotFoundException(variable, M_Product_ID);
        }
        log.fine("getProduct - C_TaxCategory_ID=" + C_TaxCategory_ID);
        //	AD_Org_ID					->	billFromC_Location_ID
        variable = "AD_Org_ID";
        sql = "SELECT C_Location_ID FROM AD_OrgInfo WHERE AD_Org_ID=?";
        billFromC_Location_ID = DB.getSQLValueEx(null, sql, AD_Org_ID);
        found = billFromC_Location_ID != -1;
        if (billFromC_Location_ID <= 0) {
            throw new TaxCriteriaNotFoundException(variable, AD_Org_ID);
        }
        //	billC_BPartner_Location_ID  ->	billToC_Location_ID
        variable = "BillTo_ID";
        sql = "SELECT l.C_Location_ID, b.IsTaxExempt, b.IsPOTaxExempt " + " FROM C_BPartner_Location l" + " INNER JOIN C_BPartner b ON (l.C_BPartner_ID=b.C_BPartner_ID) " + " WHERE C_BPartner_Location_ID=?";
        pstmt = DB.prepareStatement(sql, null);
        pstmt.setInt(1, billC_BPartner_Location_ID);
        rs = pstmt.executeQuery();
        found = false;
        if (rs.next()) {
            billToC_Location_ID = rs.getInt(1);
            IsSOTaxExempt = rs.getString(2);
            IsPOTaxExempt = rs.getString(3);
            IsTaxExempt = IsSOTrx ? IsSOTaxExempt : IsPOTaxExempt;
            found = true;
        }
        DB.close(rs, pstmt);
        if (billToC_Location_ID <= 0) {
            throw new TaxCriteriaNotFoundException(variable, billC_BPartner_Location_ID);
        }
        if ("Y".equals(IsTaxExempt))
            return getExemptTax(ctx, AD_Org_ID);
        //  Reverse for PO
        if (!IsSOTrx) {
            int temp = billFromC_Location_ID;
            billFromC_Location_ID = billToC_Location_ID;
            billToC_Location_ID = temp;
        }
        log.fine("getProduct - billFromC_Location_ID = " + billFromC_Location_ID);
        log.fine("getProduct - billToC_Location_ID = " + billToC_Location_ID);
        //-----------------------------------------------------------------
        //	M_Warehouse_ID				->	shipFromC_Location_ID
        variable = "M_Warehouse_ID";
        sql = "SELECT C_Location_ID FROM M_Warehouse WHERE M_Warehouse_ID=?";
        shipFromC_Location_ID = DB.getSQLValueEx(null, sql, M_Warehouse_ID);
        found = shipFromC_Location_ID != -1;
        if (shipFromC_Location_ID <= 0) {
            throw new TaxCriteriaNotFoundException(variable, M_Warehouse_ID);
        }
        //	shipC_BPartner_Location_ID 	->	shipToC_Location_ID
        variable = "C_BPartner_Location_ID";
        sql = "SELECT C_Location_ID FROM C_BPartner_Location WHERE C_BPartner_Location_ID=?";
        shipToC_Location_ID = DB.getSQLValueEx(null, sql, shipC_BPartner_Location_ID);
        found = shipToC_Location_ID != -1;
        if (shipToC_Location_ID <= 0) {
            throw new TaxCriteriaNotFoundException(variable, shipC_BPartner_Location_ID);
        }
        //  Reverse for PO
        if (!IsSOTrx) {
            int temp = shipFromC_Location_ID;
            shipFromC_Location_ID = shipToC_Location_ID;
            shipToC_Location_ID = temp;
        }
        log.fine("getProduct - shipFromC_Location_ID = " + shipFromC_Location_ID);
        log.fine("getProduct - shipToC_Location_ID = " + shipToC_Location_ID);
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    return get(ctx, C_TaxCategory_ID, IsSOTrx, shipDate, shipFromC_Location_ID, shipToC_Location_ID, billDate, billFromC_Location_ID, billToC_Location_ID);
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) TaxCriteriaNotFoundException(org.adempiere.exceptions.TaxCriteriaNotFoundException) PreparedStatement(java.sql.PreparedStatement)

Example 82 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class Tax method getCharge.

//	get
/**
	 *	Get Tax ID - converts parameters to call Get Tax.
	 *  <pre>
	 *		C_Charge_ID					->	C_TaxCategory_ID
	 *		billDate					->	billDate
	 *		shipDate					->	shipDate (ignored)
	 *		AD_Org_ID					->	billFromC_Location_ID
	 *		M_Warehouse_ID				->	shipFromC_Location_ID (ignored)
	 *		billC_BPartner_Location_ID  ->	billToC_Location_ID
	 *		shipC_BPartner_Location_ID 	->	shipToC_Location_ID (ignored)
	 *
	 *  if IsSOTrx is false, bill and ship are reversed
	 *  </pre>
	 * 	@param ctx	context
	 * 	@param C_Charge_ID product
	 * 	@param billDate invoice date
	 * 	@param shipDate ship date (ignored)
	 * 	@param AD_Org_ID org
	 * 	@param M_Warehouse_ID warehouse (ignored)
	 * 	@param billC_BPartner_Location_ID invoice location
	 * 	@param shipC_BPartner_Location_ID ship location (ignored)
	 * 	@param IsSOTrx is a sales trx
	 * 	@return C_Tax_ID
	 *  @throws TaxForChangeNotFoundException if criteria not found for given change
	 *  @throws TaxCriteriaNotFoundException if a criteria was not found
	 */
public static int getCharge(Properties ctx, int C_Charge_ID, Timestamp billDate, Timestamp shipDate, int AD_Org_ID, int M_Warehouse_ID, int billC_BPartner_Location_ID, int shipC_BPartner_Location_ID, boolean IsSOTrx) {
    /* ship location from warehouse is plainly ignored below */
    // if (M_Warehouse_ID <= 0)
    // M_Warehouse_ID = Env.getContextAsInt(ctx, "M_Warehouse_ID");
    // if (M_Warehouse_ID <= 0)
    // {
    // throw new TaxForChangeNotFoundException(C_Charge_ID, AD_Org_ID, M_Warehouse_ID,
    // billC_BPartner_Location_ID, shipC_BPartner_Location_ID,
    // "@NotFound@ @M_Warehouse_ID@");
    // }
    int C_TaxCategory_ID = 0;
    int shipFromC_Location_ID = 0;
    int shipToC_Location_ID = 0;
    int billFromC_Location_ID = 0;
    int billToC_Location_ID = 0;
    String IsTaxExempt = null;
    String IsSOTaxExempt = null;
    String IsPOTaxExempt = null;
    //	Get all at once
    String sql = "SELECT c.C_TaxCategory_ID, o.C_Location_ID, il.C_Location_ID, b.IsTaxExempt, b.IsPOTaxExempt," + " w.C_Location_ID, sl.C_Location_ID " + "FROM C_Charge c, AD_OrgInfo o," + " C_BPartner_Location il INNER JOIN C_BPartner b ON (il.C_BPartner_ID=b.C_BPartner_ID) " + " LEFT OUTER JOIN M_Warehouse w ON (w.M_Warehouse_ID=?), C_BPartner_Location sl " + "WHERE c.C_Charge_ID=?" + " AND o.AD_Org_ID=?" + " AND il.C_BPartner_Location_ID=?" + " AND sl.C_BPartner_Location_ID=?";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        pstmt.setInt(1, M_Warehouse_ID);
        pstmt.setInt(2, C_Charge_ID);
        pstmt.setInt(3, AD_Org_ID);
        pstmt.setInt(4, billC_BPartner_Location_ID);
        pstmt.setInt(5, shipC_BPartner_Location_ID);
        rs = pstmt.executeQuery();
        boolean found = false;
        if (rs.next()) {
            C_TaxCategory_ID = rs.getInt(1);
            billFromC_Location_ID = rs.getInt(2);
            billToC_Location_ID = rs.getInt(3);
            IsSOTaxExempt = rs.getString(4);
            IsPOTaxExempt = rs.getString(5);
            IsTaxExempt = IsSOTrx ? IsSOTaxExempt : IsPOTaxExempt;
            shipFromC_Location_ID = rs.getInt(6);
            shipToC_Location_ID = rs.getInt(7);
            found = true;
        }
        DB.close(rs, pstmt);
        //
        if (!found) {
            throw new TaxForChangeNotFoundException(C_Charge_ID, AD_Org_ID, M_Warehouse_ID, billC_BPartner_Location_ID, shipC_BPartner_Location_ID, null);
        } else if ("Y".equals(IsTaxExempt)) {
            return getExemptTax(ctx, AD_Org_ID);
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    //	Reverese for PO
    if (!IsSOTrx) {
        int temp = billFromC_Location_ID;
        billFromC_Location_ID = billToC_Location_ID;
        billToC_Location_ID = temp;
        temp = shipFromC_Location_ID;
        shipFromC_Location_ID = shipToC_Location_ID;
        shipToC_Location_ID = temp;
    }
    //
    log.fine("getCharge - C_TaxCategory_ID=" + C_TaxCategory_ID + ", billFromC_Location_ID=" + billFromC_Location_ID + ", billToC_Location_ID=" + billToC_Location_ID + ", shipFromC_Location_ID=" + shipFromC_Location_ID + ", shipToC_Location_ID=" + shipToC_Location_ID);
    return get(ctx, C_TaxCategory_ID, IsSOTrx, shipDate, shipFromC_Location_ID, shipToC_Location_ID, billDate, billFromC_Location_ID, billToC_Location_ID);
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) TaxForChangeNotFoundException(org.adempiere.exceptions.TaxForChangeNotFoundException)

Example 83 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class Query method list.

/**
	 * Return a list of all po that match the query criteria.
	 * @param clazz all resulting POs will be converted to this interface
	 * @return List
	 * @throws DBException 
	 */
@SuppressWarnings("unchecked")
public <T> List<T> list(Class<T> clazz) throws DBException {
    final List<T> list;
    if (limit > 0) {
        list = new ArrayList<T>(limit);
    } else {
        list = new ArrayList<T>();
    }
    String sql = buildSQL(null, true);
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, trxName);
        rs = createResultSet(pstmt);
        while (rs.next()) {
            PO o = table.getPO(rs, trxName);
            T po;
            if (clazz != null && !o.getClass().isAssignableFrom(clazz))
                po = POWrapper.create(o, clazz);
            else
                po = (T) o;
            list.add(po);
            if (limit > 0 && list.size() >= limit) {
                log.fine("Limit of " + limit + " reached. Stop.");
                break;
            }
        }
    } catch (SQLException e) {
        log.log(Level.SEVERE, sql, e);
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    return list;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 84 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class Query method firstOnly.

/**
	 * Return first PO that match query criteria.
	 * If there are more records that match criteria an exception will be throwed 
	 * @return first PO
	 * @throws DBException
	 * @see {@link #first()}
	 */
@SuppressWarnings("unchecked")
public <T extends PO> T firstOnly() throws DBException {
    T po = null;
    String sql = buildSQL(null, true);
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, trxName);
        rs = createResultSet(pstmt);
        if (rs.next()) {
            po = (T) table.getPO(rs, trxName);
        }
        if (rs.next()) {
            // TODO : translate
            throw new DBException("QueryMoreThanOneRecordsFound");
        }
    } catch (SQLException e) {
        log.log(Level.SEVERE, sql, e);
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    return po;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 85 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class Query method aggregate.

/**
	 * Aggregate given expression on this criteria
	 * @param <T>
	 * @param sqlExpression
	 * @param sqlFunction
	 * @param returnType
	 * @return aggregated value
	 * @throws DBException
	 */
@SuppressWarnings("unchecked")
public <T> T aggregate(String sqlExpression, String sqlFunction, Class<T> returnType) throws DBException {
    if (Util.isEmpty(sqlFunction, true)) {
        throw new DBException("No Aggregate Function defined");
    }
    if (Util.isEmpty(sqlExpression, true)) {
        if (AGGREGATE_COUNT == sqlFunction) {
            sqlExpression = "*";
        } else {
            throw new DBException("No Expression defined");
        }
    }
    StringBuffer sqlSelect = new StringBuffer("SELECT ").append(sqlFunction).append("(").append(sqlExpression).append(")").append(" FROM ").append(table.getTableName());
    T value = null;
    T defaultValue = null;
    String sql = buildSQL(sqlSelect, false);
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, this.trxName);
        rs = createResultSet(pstmt);
        if (rs.next()) {
            if (returnType.isAssignableFrom(BigDecimal.class)) {
                value = (T) rs.getBigDecimal(1);
                defaultValue = (T) Env.ZERO;
            } else if (returnType.isAssignableFrom(Double.class)) {
                value = (T) Double.valueOf(rs.getDouble(1));
                defaultValue = (T) Double.valueOf(0.00);
            } else if (returnType.isAssignableFrom(Integer.class)) {
                value = (T) Integer.valueOf(rs.getInt(1));
                defaultValue = (T) Integer.valueOf(0);
            } else if (returnType.isAssignableFrom(Timestamp.class)) {
                value = (T) rs.getTimestamp(1);
            } else if (returnType.isAssignableFrom(Boolean.class)) {
                value = (T) Boolean.valueOf("Y".equals(rs.getString(1)));
                defaultValue = (T) Boolean.FALSE;
            } else {
                value = (T) rs.getObject(1);
            }
        }
        if (rs.next()) {
            // TODO : translate
            throw new DBException("QueryMoreThanOneRecordsFound");
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    //
    if (value == null) {
        value = defaultValue;
    }
    return value;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Aggregations

DBException (org.adempiere.exceptions.DBException)89 SQLException (java.sql.SQLException)82 PreparedStatement (java.sql.PreparedStatement)75 ResultSet (java.sql.ResultSet)75 BigDecimal (java.math.BigDecimal)27 ArrayList (java.util.ArrayList)23 Timestamp (java.sql.Timestamp)15 POResultSet (org.compiere.model.POResultSet)8 AdempiereException (org.adempiere.exceptions.AdempiereException)6 KeyNamePair (org.compiere.util.KeyNamePair)5 Connection (java.sql.Connection)4 Savepoint (java.sql.Savepoint)4 MProduct (org.compiere.model.MProduct)4 Trx (org.compiere.util.Trx)4 Date (java.util.Date)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 IDColumn (org.compiere.minigrid.IDColumn)2 MLocator (org.compiere.model.MLocator)2 MTable (org.compiere.model.MTable)2 MUOM (org.compiere.model.MUOM)2