Search in sources :

Example 1 with TaxCriteriaNotFoundException

use of org.adempiere.exceptions.TaxCriteriaNotFoundException 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)

Aggregations

PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 DBException (org.adempiere.exceptions.DBException)1 TaxCriteriaNotFoundException (org.adempiere.exceptions.TaxCriteriaNotFoundException)1