IDEMPIERE-1398 Tax lookups not transaction aware

This commit is contained in:
dantam 2013-09-27 14:17:14 +02:00
parent f249fd2327
commit 9e5cd134dc
4 changed files with 99 additions and 28 deletions

View File

@ -435,7 +435,7 @@ public class MInvoiceLine extends X_C_InvoiceLine
int C_Tax_ID = Tax.get(getCtx(), getM_Product_ID(), getC_Charge_ID() , m_DateInvoiced, m_DateInvoiced, int C_Tax_ID = Tax.get(getCtx(), getM_Product_ID(), getC_Charge_ID() , m_DateInvoiced, m_DateInvoiced,
getAD_Org_ID(), M_Warehouse_ID, getAD_Org_ID(), M_Warehouse_ID,
m_C_BPartner_Location_ID, // should be bill to m_C_BPartner_Location_ID, // should be bill to
m_C_BPartner_Location_ID, m_IsSOTrx); m_C_BPartner_Location_ID, m_IsSOTrx, get_TrxName());
if (C_Tax_ID == 0) if (C_Tax_ID == 0)
{ {
log.log(Level.SEVERE, "No Tax found"); log.log(Level.SEVERE, "No Tax found");

View File

@ -338,7 +338,7 @@ public class MOrderLine extends X_C_OrderLine
int ii = Tax.get(getCtx(), getM_Product_ID(), getC_Charge_ID(), getDateOrdered(), getDateOrdered(), int ii = Tax.get(getCtx(), getM_Product_ID(), getC_Charge_ID(), getDateOrdered(), getDateOrdered(),
getAD_Org_ID(), getM_Warehouse_ID(), getAD_Org_ID(), getM_Warehouse_ID(),
getC_BPartner_Location_ID(), // should be bill to getC_BPartner_Location_ID(), // should be bill to
getC_BPartner_Location_ID(), m_IsSOTrx); getC_BPartner_Location_ID(), m_IsSOTrx, get_TrxName());
if (ii == 0) if (ii == 0)
{ {
log.log(Level.SEVERE, "No Tax found"); log.log(Level.SEVERE, "No Tax found");

View File

@ -156,7 +156,7 @@ public class MRMALine extends X_M_RMALine
taxId = Tax.get(getCtx(), getM_Product_ID(), getC_Charge_ID(), invoice.getDateInvoiced(), invoice.getDateInvoiced(), taxId = Tax.get(getCtx(), getM_Product_ID(), getC_Charge_ID(), invoice.getDateInvoiced(), invoice.getDateInvoiced(),
getAD_Org_ID(), getParent().getShipment().getM_Warehouse_ID(), getAD_Org_ID(), getParent().getShipment().getM_Warehouse_ID(),
invoice.getC_BPartner_Location_ID(), // should be bill to invoice.getC_BPartner_Location_ID(), // should be bill to
invoice.getC_BPartner_Location_ID(), getParent().isSOTrx()); invoice.getC_BPartner_Location_ID(), getParent().isSOTrx(), get_TrxName());
} }
else else
{ {
@ -170,7 +170,7 @@ public class MRMALine extends X_M_RMALine
taxId = Tax.get(getCtx(), getM_Product_ID(), getC_Charge_ID(), order.getDateOrdered(), order.getDateOrdered(), taxId = Tax.get(getCtx(), getM_Product_ID(), getC_Charge_ID(), order.getDateOrdered(), order.getDateOrdered(),
getAD_Org_ID(), order.getM_Warehouse_ID(), getAD_Org_ID(), order.getM_Warehouse_ID(),
order.getC_BPartner_Location_ID(), // should be bill to order.getC_BPartner_Location_ID(), // should be bill to
order.getC_BPartner_Location_ID(), getParent().isSOTrx()); order.getC_BPartner_Location_ID(), getParent().isSOTrx(), get_TrxName());
} }
else else
throw new IllegalStateException("No Invoice/Order found the Shipment/Receipt associated"); throw new IllegalStateException("No Invoice/Order found the Shipment/Receipt associated");

View File

@ -46,6 +46,30 @@ public class Tax
/** Logger */ /** Logger */
static private CLogger log = CLogger.getCLogger (Tax.class); static private CLogger log = CLogger.getCLogger (Tax.class);
/**
*
* @param ctx
* @param M_Product_ID
* @param C_Charge_ID
* @param billDate
* @param shipDate
* @param AD_Org_ID
* @param M_Warehouse_ID
* @param billC_BPartner_Location_ID
* @param shipC_BPartner_Location_ID
* @param IsSOTrx
* @deprecated
* @return
*/
public static int get (Properties ctx, int M_Product_ID, 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) {
return get(ctx, M_Product_ID, C_Charge_ID, billDate, shipDate, AD_Org_ID, M_Warehouse_ID, billC_BPartner_Location_ID, shipC_BPartner_Location_ID, IsSOTrx, null);
}
/************************************************************************** /**************************************************************************
* Get Tax ID - converts parameters to call Get Tax. * Get Tax ID - converts parameters to call Get Tax.
@ -76,18 +100,41 @@ public class Tax
Timestamp billDate, Timestamp shipDate, Timestamp billDate, Timestamp shipDate,
int AD_Org_ID, int M_Warehouse_ID, int AD_Org_ID, int M_Warehouse_ID,
int billC_BPartner_Location_ID, int shipC_BPartner_Location_ID, int billC_BPartner_Location_ID, int shipC_BPartner_Location_ID,
boolean IsSOTrx) boolean IsSOTrx, String trxName)
{ {
if (M_Product_ID != 0) if (M_Product_ID != 0)
return getProduct (ctx, M_Product_ID, billDate, shipDate, AD_Org_ID, M_Warehouse_ID, return getProduct (ctx, M_Product_ID, billDate, shipDate, AD_Org_ID, M_Warehouse_ID,
billC_BPartner_Location_ID, shipC_BPartner_Location_ID, IsSOTrx); billC_BPartner_Location_ID, shipC_BPartner_Location_ID, IsSOTrx, trxName);
else if (C_Charge_ID != 0) else if (C_Charge_ID != 0)
return getCharge (ctx, C_Charge_ID, billDate, shipDate, AD_Org_ID, M_Warehouse_ID, return getCharge (ctx, C_Charge_ID, billDate, shipDate, AD_Org_ID, M_Warehouse_ID,
billC_BPartner_Location_ID, shipC_BPartner_Location_ID, IsSOTrx); billC_BPartner_Location_ID, shipC_BPartner_Location_ID, IsSOTrx, trxName);
else else
return getExemptTax (ctx, AD_Org_ID); return getExemptTax (ctx, AD_Org_ID, trxName);
} // get } // get
/**
*
* @param ctx
* @param C_Charge_ID
* @param billDate
* @param shipDate
* @param AD_Org_ID
* @param M_Warehouse_ID
* @param billC_BPartner_Location_ID
* @param shipC_BPartner_Location_ID
* @param IsSOTrx
* @return
* @deprecated
*/
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) {
return getCharge(ctx, C_Charge_ID, billDate, shipDate, AD_Org_ID, M_Warehouse_ID, billC_BPartner_Location_ID, shipC_BPartner_Location_ID, IsSOTrx, null);
}
/** /**
* Get Tax ID - converts parameters to call Get Tax. * Get Tax ID - converts parameters to call Get Tax.
* <pre> * <pre>
@ -118,7 +165,7 @@ public class Tax
Timestamp billDate, Timestamp shipDate, Timestamp billDate, Timestamp shipDate,
int AD_Org_ID, int M_Warehouse_ID, int AD_Org_ID, int M_Warehouse_ID,
int billC_BPartner_Location_ID, int shipC_BPartner_Location_ID, int billC_BPartner_Location_ID, int shipC_BPartner_Location_ID,
boolean IsSOTrx) boolean IsSOTrx, String trxName)
{ {
/* ship location from warehouse is plainly ignored below */ /* ship location from warehouse is plainly ignored below */
// if (M_Warehouse_ID <= 0) // if (M_Warehouse_ID <= 0)
@ -152,7 +199,7 @@ public class Tax
ResultSet rs = null; ResultSet rs = null;
try try
{ {
pstmt = DB.prepareStatement (sql, null); pstmt = DB.prepareStatement (sql, trxName);
pstmt.setInt (1, M_Warehouse_ID); pstmt.setInt (1, M_Warehouse_ID);
pstmt.setInt (2, C_Charge_ID); pstmt.setInt (2, C_Charge_ID);
pstmt.setInt (3, AD_Org_ID); pstmt.setInt (3, AD_Org_ID);
@ -182,7 +229,7 @@ public class Tax
} }
else if ("Y".equals (IsTaxExempt)) else if ("Y".equals (IsTaxExempt))
{ {
return getExemptTax (ctx, AD_Org_ID); return getExemptTax (ctx, AD_Org_ID, trxName);
} }
} }
catch (SQLException e) catch (SQLException e)
@ -213,10 +260,32 @@ public class Tax
+ ", shipToC_Location_ID=" + shipToC_Location_ID); + ", shipToC_Location_ID=" + shipToC_Location_ID);
return get (ctx, C_TaxCategory_ID, IsSOTrx, return get (ctx, C_TaxCategory_ID, IsSOTrx,
shipDate, shipFromC_Location_ID, shipToC_Location_ID, shipDate, shipFromC_Location_ID, shipToC_Location_ID,
billDate, billFromC_Location_ID, billToC_Location_ID); billDate, billFromC_Location_ID, billToC_Location_ID, trxName);
} // getCharge } // getCharge
/**
*
* @param ctx
* @param M_Product_ID
* @param billDate
* @param shipDate
* @param AD_Org_ID
* @param M_Warehouse_ID
* @param billC_BPartner_Location_ID
* @param shipC_BPartner_Location_ID
* @param IsSOTrx
* @return
* @deprecated
*/
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) {
return getProduct(ctx, M_Product_ID, billDate, shipDate, AD_Org_ID, M_Warehouse_ID, billC_BPartner_Location_ID, shipC_BPartner_Location_ID, IsSOTrx, null);
}
/** /**
* Get Tax ID - converts parameters to call Get Tax. * Get Tax ID - converts parameters to call Get Tax.
* <pre> * <pre>
@ -246,7 +315,7 @@ public class Tax
Timestamp billDate, Timestamp shipDate, Timestamp billDate, Timestamp shipDate,
int AD_Org_ID, int M_Warehouse_ID, int AD_Org_ID, int M_Warehouse_ID,
int billC_BPartner_Location_ID, int shipC_BPartner_Location_ID, int billC_BPartner_Location_ID, int shipC_BPartner_Location_ID,
boolean IsSOTrx) boolean IsSOTrx, String trxName)
{ {
String variable = ""; String variable = "";
int C_TaxCategory_ID = 0; int C_TaxCategory_ID = 0;
@ -273,7 +342,7 @@ public class Tax
+ " AND o.AD_Org_ID=?" + " AND o.AD_Org_ID=?"
+ " AND il.C_BPartner_Location_ID=?" + " AND il.C_BPartner_Location_ID=?"
+ " AND sl.C_BPartner_Location_ID=?"; + " AND sl.C_BPartner_Location_ID=?";
pstmt = DB.prepareStatement(sql, null); pstmt = DB.prepareStatement(sql, trxName);
pstmt.setInt(1, M_Warehouse_ID); pstmt.setInt(1, M_Warehouse_ID);
pstmt.setInt(2, M_Product_ID); pstmt.setInt(2, M_Product_ID);
pstmt.setInt(3, AD_Org_ID); pstmt.setInt(3, AD_Org_ID);
@ -298,7 +367,7 @@ public class Tax
if (found && "Y".equals(IsTaxExempt)) if (found && "Y".equals(IsTaxExempt))
{ {
if (log.isLoggable(Level.FINE)) log.fine("getProduct - Business Partner is Tax exempt"); if (log.isLoggable(Level.FINE)) log.fine("getProduct - Business Partner is Tax exempt");
return getExemptTax(ctx, AD_Org_ID); return getExemptTax(ctx, AD_Org_ID, trxName);
} }
else if (found) else if (found)
{ {
@ -318,7 +387,7 @@ public class Tax
+ ", shipToC_Location_ID=" + shipToC_Location_ID); + ", shipToC_Location_ID=" + shipToC_Location_ID);
return get(ctx, C_TaxCategory_ID, IsSOTrx, return get(ctx, C_TaxCategory_ID, IsSOTrx,
shipDate, shipFromC_Location_ID, shipToC_Location_ID, shipDate, shipFromC_Location_ID, shipToC_Location_ID,
billDate, billFromC_Location_ID, billToC_Location_ID); billDate, billFromC_Location_ID, billToC_Location_ID, trxName);
} }
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -328,7 +397,7 @@ public class Tax
// M_Product_ID -> C_TaxCategory_ID // M_Product_ID -> C_TaxCategory_ID
variable = "M_Product_ID"; variable = "M_Product_ID";
sql = "SELECT C_TaxCategory_ID FROM M_Product WHERE 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); C_TaxCategory_ID = DB.getSQLValueEx(trxName, sql, M_Product_ID);
found = C_TaxCategory_ID != -1; found = C_TaxCategory_ID != -1;
if (C_TaxCategory_ID <= 0) if (C_TaxCategory_ID <= 0)
{ {
@ -339,7 +408,7 @@ public class Tax
// AD_Org_ID -> billFromC_Location_ID // AD_Org_ID -> billFromC_Location_ID
variable = "AD_Org_ID"; variable = "AD_Org_ID";
sql = "SELECT C_Location_ID FROM AD_OrgInfo WHERE 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); billFromC_Location_ID = DB.getSQLValueEx(trxName, sql, AD_Org_ID);
found = billFromC_Location_ID != -1; found = billFromC_Location_ID != -1;
if (billFromC_Location_ID <= 0) if (billFromC_Location_ID <= 0)
{ {
@ -352,7 +421,7 @@ public class Tax
+ " FROM C_BPartner_Location l" + " FROM C_BPartner_Location l"
+ " INNER JOIN C_BPartner b ON (l.C_BPartner_ID=b.C_BPartner_ID) " + " INNER JOIN C_BPartner b ON (l.C_BPartner_ID=b.C_BPartner_ID) "
+ " WHERE C_BPartner_Location_ID=?"; + " WHERE C_BPartner_Location_ID=?";
pstmt = DB.prepareStatement(sql, null); pstmt = DB.prepareStatement(sql, trxName);
pstmt.setInt(1, billC_BPartner_Location_ID); pstmt.setInt(1, billC_BPartner_Location_ID);
rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
found = false; found = false;
@ -370,7 +439,7 @@ public class Tax
throw new TaxCriteriaNotFoundException(variable, billC_BPartner_Location_ID); throw new TaxCriteriaNotFoundException(variable, billC_BPartner_Location_ID);
} }
if ("Y".equals(IsTaxExempt)) if ("Y".equals(IsTaxExempt))
return getExemptTax(ctx, AD_Org_ID); return getExemptTax(ctx, AD_Org_ID, trxName);
// Reverse for PO // Reverse for PO
if (!IsSOTrx) if (!IsSOTrx)
@ -389,7 +458,7 @@ public class Tax
// M_Warehouse_ID -> shipFromC_Location_ID // M_Warehouse_ID -> shipFromC_Location_ID
variable = "M_Warehouse_ID"; variable = "M_Warehouse_ID";
sql = "SELECT C_Location_ID FROM M_Warehouse WHERE 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); shipFromC_Location_ID = DB.getSQLValueEx(trxName, sql, M_Warehouse_ID);
found = shipFromC_Location_ID != -1; found = shipFromC_Location_ID != -1;
if (shipFromC_Location_ID <= 0) if (shipFromC_Location_ID <= 0)
{ {
@ -399,7 +468,7 @@ public class Tax
// shipC_BPartner_Location_ID -> shipToC_Location_ID // shipC_BPartner_Location_ID -> shipToC_Location_ID
variable = "C_BPartner_Location_ID"; variable = "C_BPartner_Location_ID";
sql = "SELECT C_Location_ID FROM C_BPartner_Location WHERE 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); shipToC_Location_ID = DB.getSQLValueEx(trxName, sql, shipC_BPartner_Location_ID);
found = shipToC_Location_ID != -1; found = shipToC_Location_ID != -1;
if (shipToC_Location_ID <= 0) if (shipToC_Location_ID <= 0)
{ {
@ -428,24 +497,25 @@ public class Tax
return get (ctx, C_TaxCategory_ID, IsSOTrx, return get (ctx, C_TaxCategory_ID, IsSOTrx,
shipDate, shipFromC_Location_ID, shipToC_Location_ID, shipDate, shipFromC_Location_ID, shipToC_Location_ID,
billDate, billFromC_Location_ID, billToC_Location_ID); billDate, billFromC_Location_ID, billToC_Location_ID, trxName);
} // getProduct } // getProduct
/** /**
* Get Exempt Tax Code * Get Exempt Tax Code
* @param ctx context * @param ctx context
* @param AD_Org_ID org to find client * @param AD_Org_ID org to find client
* @param trxName Transaction
* @return C_Tax_ID * @return C_Tax_ID
* @throws TaxNoExemptFoundException if no tax exempt found * @throws TaxNoExemptFoundException if no tax exempt found
*/ */
private static int getExemptTax (Properties ctx, int AD_Org_ID) private static int getExemptTax (Properties ctx, int AD_Org_ID, String trxName)
{ {
final String sql = "SELECT t.C_Tax_ID " final String sql = "SELECT t.C_Tax_ID "
+ "FROM C_Tax t" + "FROM C_Tax t"
+ " INNER JOIN AD_Org o ON (t.AD_Client_ID=o.AD_Client_ID) " + " INNER JOIN AD_Org o ON (t.AD_Client_ID=o.AD_Client_ID) "
+ "WHERE t.IsTaxExempt='Y' AND o.AD_Org_ID=? " + "WHERE t.IsTaxExempt='Y' AND o.AD_Org_ID=? "
+ "ORDER BY t.Rate DESC"; + "ORDER BY t.Rate DESC";
int C_Tax_ID = DB.getSQLValueEx(null, sql, AD_Org_ID); int C_Tax_ID = DB.getSQLValueEx(trxName, sql, AD_Org_ID);
if (log.isLoggable(Level.FINE)) log.fine("getExemptTax - TaxExempt=Y - C_Tax_ID=" + C_Tax_ID); if (log.isLoggable(Level.FINE)) log.fine("getExemptTax - TaxExempt=Y - C_Tax_ID=" + C_Tax_ID);
if (C_Tax_ID <= 0) if (C_Tax_ID <= 0)
{ {
@ -469,13 +539,14 @@ public class Tax
* @param billDate invoice date * @param billDate invoice date
* @param billFromC_Location_ID invoice from * @param billFromC_Location_ID invoice from
* @param billToC_Location_ID invoice to * @param billToC_Location_ID invoice to
* @param trxName Transaction
* @return C_Tax_ID * @return C_Tax_ID
* @throws TaxNotFoundException if no tax found for given criteria * @throws TaxNotFoundException if no tax found for given criteria
*/ */
protected static int get (Properties ctx, protected static int get (Properties ctx,
int C_TaxCategory_ID, boolean IsSOTrx, int C_TaxCategory_ID, boolean IsSOTrx,
Timestamp shipDate, int shipFromC_Location_ID, int shipToC_Location_ID, Timestamp shipDate, int shipFromC_Location_ID, int shipToC_Location_ID,
Timestamp billDate, int billFromC_Location_ID, int billToC_Location_ID) Timestamp billDate, int billFromC_Location_ID, int billToC_Location_ID, String trxName)
{ {
// C_TaxCategory contains CommodityCode // C_TaxCategory contains CommodityCode
@ -490,8 +561,8 @@ public class Tax
} }
MTax[] taxes = MTax.getAll (ctx); MTax[] taxes = MTax.getAll (ctx);
MLocation lFrom = new MLocation (ctx, billFromC_Location_ID, null); MLocation lFrom = new MLocation (ctx, billFromC_Location_ID, trxName);
MLocation lTo = new MLocation (ctx, billToC_Location_ID, null); MLocation lTo = new MLocation (ctx, billToC_Location_ID, trxName);
if (log.isLoggable(Level.FINER)){ if (log.isLoggable(Level.FINER)){
log.finer("From=" + lFrom); log.finer("From=" + lFrom);
log.finer("To=" + lTo); log.finer("To=" + lTo);