From 397f6fb4eefc7758d282f24b6043ae9921621a3c Mon Sep 17 00:00:00 2001 From: trifonnt Date: Sat, 12 Dec 2009 13:59:13 +0000 Subject: [PATCH] BF [2913276] - Allow only one Default Tax Rate per Tax Category https://sourceforge.net/tracker/?func=detail&aid=2913276&group_id=176962&atid=879332 --- base/src/org/compiere/model/MTax.java | 57 ++++++++++++++++++--------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/base/src/org/compiere/model/MTax.java b/base/src/org/compiere/model/MTax.java index 083b02b912..ab949e8d42 100644 --- a/base/src/org/compiere/model/MTax.java +++ b/base/src/org/compiere/model/MTax.java @@ -23,6 +23,7 @@ import java.util.Properties; import org.compiere.util.CCache; import org.compiere.util.Env; +import org.compiere.util.Msg; import org.compiere.util.TimeUtil; /** @@ -39,6 +40,18 @@ public class MTax extends X_C_Tax */ private static final long serialVersionUID = 4140382472528327237L; + /** Cache */ + private static CCache s_cache = new CCache(Table_Name, 5); + /** Cache of Client */ + private static CCache s_cacheAll = new CCache(Table_Name, 5); + + /** 100 */ + private static BigDecimal ONEHUNDRED = new BigDecimal(100); + /** Child Taxes */ + private MTax[] m_childTaxes = null; + /** Postal Codes */ + private MTaxPostal[] m_postals = null; + /** * Get All Tax codes (for AD_Client) @@ -58,6 +71,7 @@ public class MTax extends X_C_Tax List list = new Query(ctx, MTax.Table_Name, whereClause, null) .setParameters(new Object[]{AD_Client_ID}) .setOrderBy("C_Country_ID, C_Region_ID, To_Country_ID, To_Region_ID") + .setOnlyActiveRecords(true) .list(); for (MTax tax : list) { @@ -87,11 +101,6 @@ public class MTax extends X_C_Tax return retValue; } // get - /** Cache */ - private static CCache s_cache = new CCache(Table_Name, 5); - /** Cache of Client */ - private static CCache s_cacheAll = new CCache(Table_Name, 5); - /************************************************************************** * Standard Constructor * @param ctx context @@ -145,14 +154,6 @@ public class MTax extends X_C_Tax setC_TaxCategory_ID (C_TaxCategory_ID); // FK } // MTax - /** 100 */ - private static BigDecimal ONEHUNDRED = new BigDecimal(100); - /** Child Taxes */ - private MTax[] m_childTaxes = null; - /** Postal Codes */ - private MTaxPostal[] m_postals = null; - - /** * Get Child Taxes * @param requery reload @@ -168,8 +169,9 @@ public class MTax extends X_C_Tax //FR: [ 2214883 ] Remove SQL code and Replace for Query - red1 String whereClause = COLUMNNAME_Parent_Tax_ID+"=?"; List list = new Query(getCtx(), MTax.Table_Name, whereClause, get_TrxName()) - .setParameters(new Object[]{getC_Tax_ID()}) - .list(); + .setParameters(new Object[]{getC_Tax_ID()}) + .setOnlyActiveRecords(true) + .list(); //red1 - end - m_childTaxes = new MTax[list.size ()]; @@ -190,10 +192,10 @@ public class MTax extends X_C_Tax //FR: [ 2214883 ] Remove SQL code and Replace for Query - red1 String whereClause = MTaxPostal.COLUMNNAME_C_Tax_ID+"=?"; List list = new Query(getCtx(), MTaxPostal.Table_Name, whereClause, get_TrxName()) - .setParameters(new Object[]{getC_Tax_ID()}) - .setOnlyActiveRecords(true) - .setOrderBy(MTaxPostal.COLUMNNAME_Postal+", "+MTaxPostal.COLUMNNAME_Postal_To) - .list(); + .setParameters(new Object[]{getC_Tax_ID()}) + .setOnlyActiveRecords(true) + .setOrderBy(MTaxPostal.COLUMNNAME_Postal+", "+MTaxPostal.COLUMNNAME_Postal_To) + .list(); //red1 - end - if (list.size() > 0) { @@ -274,6 +276,23 @@ public class MTax extends X_C_Tax return finalTax; } // calculateTax + @Override + protected boolean beforeSave(boolean newRecord) { + if (isDefault()) { + // @Trifon - Ensure that only one tax rate is set as Default! + String whereClause = MTax.COLUMNNAME_C_TaxCategory_ID+"=? AND IsDefault='Y'"; + List list = new Query(getCtx(), MTax.Table_Name, whereClause, get_TrxName()) + .setParameters(new Object[]{getC_TaxCategory_ID()}) + .setOnlyActiveRecords(true) + .list(); + if (list.size() >= 1) { + log.saveError("Error", Msg.parseTranslation(getCtx(), "Only one @C_Tax_ID@ per @C_TaxCategory_ID@ can be marked as Default!")); + return false; + } + } + return super.beforeSave(newRecord); + } + /** * After Save * @param newRecord new