IDEMPIERE-1620 Improve ITaxProvider / integrate patch from Murilo Habermann Torquato (muriloht)

This commit is contained in:
Carlos Ruiz 2014-03-05 08:43:17 -05:00
parent e0a5c43285
commit 772fca9f09
5 changed files with 189 additions and 111 deletions

View File

@ -26,23 +26,35 @@ import org.compiere.process.ProcessInfo;
* Tax provider interface
* @author Elaine
*
* @contributor Murilo H. Torquato <muriloht@devcoffee.com.br>
*
*/
public interface ITaxProvider {
public boolean calculateOrderTaxTotal(MTaxProvider provider, MOrder order);
public boolean updateOrderTax(MTaxProvider provider, MOrderLine line);
public boolean recalculateTax(MTaxProvider provider, MOrderLine line, boolean newRecord);
public boolean updateHeaderTax(MTaxProvider provider, MOrderLine line);
public boolean calculateInvoiceTaxTotal(MTaxProvider provider, MInvoice invoice);
public boolean updateInvoiceTax(MTaxProvider provider, MInvoiceLine line);
public boolean recalculateTax(MTaxProvider provider, MInvoiceLine line, boolean newRecord);
public boolean updateHeaderTax(MTaxProvider provider, MInvoiceLine line);
public boolean calculateRMATaxTotal(MTaxProvider provider, MRMA rma);
public boolean updateRMATax(MTaxProvider provider, MRMALine line);
public boolean recalculateTax(MTaxProvider provider, MRMALine line, boolean newRecord);
public boolean updateHeaderTax(MTaxProvider provider, MRMALine line);
public String validateConnection(MTaxProvider provider, ProcessInfo pi) throws Exception;
}

View File

@ -54,7 +54,7 @@ public class MInvoiceLine extends X_C_InvoiceLine
/**
*
*/
private static final long serialVersionUID = -8065144330439104363L;
private static final long serialVersionUID = -6174490999732876285L;
/**
* Get Invoice Line referencing InOut Line
@ -989,7 +989,7 @@ public class MInvoiceLine extends X_C_InvoiceLine
* Update Tax & Header
* @return true if header updated with tax
*/
protected boolean updateHeaderTax()
public boolean updateHeaderTax()
{
// Update header only if the document is not processed - teo_sarca BF [ 2317305 ]
if (isProcessed() && !is_ValueChanged(COLUMNNAME_Processed))
@ -1004,30 +1004,7 @@ public class MInvoiceLine extends X_C_InvoiceLine
if (!calculator.updateInvoiceTax(provider, this))
return false;
// Update Invoice Header
String sql = "UPDATE C_Invoice i"
+ " SET TotalLines="
+ "(SELECT COALESCE(SUM(LineNetAmt),0) FROM C_InvoiceLine il WHERE i.C_Invoice_ID=il.C_Invoice_ID) "
+ "WHERE C_Invoice_ID=?";
int no = DB.executeUpdateEx(sql, new Object[]{getC_Invoice_ID()}, get_TrxName());
if (no != 1)
log.warning("(1) #" + no);
if (isTaxIncluded())
sql = "UPDATE C_Invoice i "
+ " SET GrandTotal=TotalLines "
+ "WHERE C_Invoice_ID=?";
else
sql = "UPDATE C_Invoice i "
+ " SET GrandTotal=TotalLines+"
+ "(SELECT COALESCE(SUM(TaxAmt),0) FROM C_InvoiceTax it WHERE i.C_Invoice_ID=it.C_Invoice_ID) "
+ "WHERE C_Invoice_ID=?";
no = DB.executeUpdateEx(sql, new Object[]{getC_Invoice_ID()}, get_TrxName());
if (no != 1)
log.warning("(2) #" + no);
m_parent = null;
return no == 1;
return calculator.updateHeaderTax(provider, this);
} // updateHeaderTax
@ -1376,4 +1353,10 @@ public class MInvoiceLine extends X_C_InvoiceLine
+" AND "+MMatchInv.COLUMNNAME_Processed+"=?";
return DB.getSQLValueBDEx(get_TrxName(), sql, getC_InvoiceLine_ID(), true);
}
public void clearParent()
{
this.m_parent = null;
}
} // MInvoiceLine

View File

@ -58,7 +58,7 @@ public class MOrderLine extends X_C_OrderLine
/**
*
*/
private static final long serialVersionUID = -5740772832684028526L;
private static final long serialVersionUID = -6972864309223293705L;
/**
* Get Order Unreserved Qty
@ -1019,14 +1019,12 @@ public class MOrderLine extends X_C_OrderLine
{
if (!success)
return success;
if (!newRecord && is_ValueChanged("C_Tax_ID"))
{
// Recalculate Tax for old Tax
if (!getParent().isProcessed())
if (!updateOrderTax(true))
return false;
}
return updateHeaderTax();
MTax tax = new MTax(getCtx(), getC_Tax_ID(), get_TrxName());
MTaxProvider provider = new MTaxProvider(tax.getCtx(), tax.getC_TaxProvider_ID(), tax.get_TrxName());
ITaxProvider calculator = Core.getTaxProvider(provider);
if (calculator == null)
throw new AdempiereException(Msg.getMsg(getCtx(), "TaxNoProvider"));
return calculator.recalculateTax(provider, this, newRecord);
} // afterSave
/**
@ -1054,7 +1052,7 @@ public class MOrderLine extends X_C_OrderLine
*
* @author teo_sarca [ 1583825 ]
*/
protected boolean updateOrderTax(boolean oldTax) {
public boolean updateOrderTax(boolean oldTax) {
MOrderTax tax = MOrderTax.get (this, getPrecision(), oldTax, get_TrxName());
if (tax != null) {
if (!tax.calculateTaxFromLines())
@ -1075,43 +1073,27 @@ public class MOrderLine extends X_C_OrderLine
* Update Tax & Header
* @return true if header updated
*/
private boolean updateHeaderTax()
public boolean updateHeaderTax()
{
// Recalculate Tax for this Tax
if (!getParent().isProcessed())
{
MTax tax = new MTax(getCtx(), getC_Tax_ID(), get_TrxName());
MTaxProvider provider = new MTaxProvider(tax.getCtx(), tax.getC_TaxProvider_ID(), tax.get_TrxName());
ITaxProvider calculator = Core.getTaxProvider(provider);
if (calculator == null)
throw new AdempiereException(Msg.getMsg(getCtx(), "TaxNoProvider"));
if (!calculator.updateOrderTax(provider, this))
return false;
}
// Update Order Header
String sql = "UPDATE C_Order i"
+ " SET TotalLines="
+ "(SELECT COALESCE(SUM(LineNetAmt),0) FROM C_OrderLine il WHERE i.C_Order_ID=il.C_Order_ID) "
+ "WHERE C_Order_ID=" + getC_Order_ID();
int no = DB.executeUpdate(sql, get_TrxName());
if (no != 1)
log.warning("(1) #" + no);
if (isTaxIncluded())
sql = "UPDATE C_Order i "
+ " SET GrandTotal=TotalLines "
+ "WHERE C_Order_ID=" + getC_Order_ID();
else
sql = "UPDATE C_Order i "
+ " SET GrandTotal=TotalLines+"
+ "(SELECT COALESCE(SUM(TaxAmt),0) FROM C_OrderTax it WHERE i.C_Order_ID=it.C_Order_ID) "
+ "WHERE C_Order_ID=" + getC_Order_ID();
no = DB.executeUpdate(sql, get_TrxName());
if (no != 1)
log.warning("(2) #" + no);
m_parent = null;
return no == 1;
// Update header only if the document is not processed
if (isProcessed() && !is_ValueChanged(COLUMNNAME_Processed))
return true;
MTax tax = new MTax(getCtx(), getC_Tax_ID(), get_TrxName());
MTaxProvider provider = new MTaxProvider(tax.getCtx(), tax.getC_TaxProvider_ID(), tax.get_TrxName());
ITaxProvider calculator = Core.getTaxProvider(provider);
if (calculator == null)
throw new AdempiereException(Msg.getMsg(getCtx(), "TaxNoProvider"));
if (!calculator.updateOrderTax(provider, this))
return false;
return calculator.updateHeaderTax(provider, this);
} // updateHeaderTax
public void clearParent()
{
this.m_parent = null;
}
} // MOrderLine

View File

@ -40,7 +40,7 @@ public class MRMALine extends X_M_RMALine
/**
*
*/
private static final long serialVersionUID = 4358572588500923170L;
private static final long serialVersionUID = -3459158383642518763L;
/**
* Standard Constructor
@ -439,15 +439,12 @@ public class MRMALine extends X_M_RMALine
{
if (!success)
return success;
if (!newRecord && is_ValueChanged("C_Tax_ID"))
{
// Recalculate Tax for old Tax
if (!getParent().isProcessed())
if (!updateOrderTax(true))
return false;
}
return updateHeaderAmt();
MTax tax = new MTax(getCtx(), getC_Tax_ID(), get_TrxName());
MTaxProvider provider = new MTaxProvider(tax.getCtx(), tax.getC_TaxProvider_ID(), tax.get_TrxName());
ITaxProvider calculator = Core.getTaxProvider(provider);
if (calculator == null)
throw new AdempiereException(Msg.getMsg(getCtx(), "TaxNoProvider"));
return calculator.recalculateTax(provider, this, newRecord);
}
@Override
@ -462,36 +459,21 @@ public class MRMALine extends X_M_RMALine
* Update Amount on Header
* @return true if header updated
*/
private boolean updateHeaderAmt()
public boolean updateHeaderAmt()
{
// Update header only if the document is not processed
if (isProcessed() && !is_ValueChanged(COLUMNNAME_Processed))
return true;
// Recalculate Tax for this Tax
if (!getParent().isProcessed())
{
MTax tax = new MTax(getCtx(), getC_Tax_ID(), get_TrxName());
MTaxProvider provider = new MTaxProvider(tax.getCtx(), tax.getC_TaxProvider_ID(), tax.get_TrxName());
ITaxProvider calculator = Core.getTaxProvider(provider);
if (calculator == null)
throw new AdempiereException(Msg.getMsg(getCtx(), "TaxNoProvider"));
if (!calculator.updateRMATax(provider, this))
return false;
}
// Update RMA Header
String sql = "UPDATE M_RMA "
+ " SET Amt="
+ "(SELECT COALESCE(SUM(LineNetAmt),0) FROM M_RMALine WHERE M_RMA.M_RMA_ID=M_RMALine.M_RMA_ID) "
+ "WHERE M_RMA_ID=?";
int no = DB.executeUpdateEx(sql, new Object[]{getM_RMA_ID()}, get_TrxName());
if (no != 1)
log.warning("(1) #" + no);
MTax tax = new MTax(getCtx(), getC_Tax_ID(), get_TrxName());
MTaxProvider provider = new MTaxProvider(tax.getCtx(), tax.getC_TaxProvider_ID(), tax.get_TrxName());
ITaxProvider calculator = Core.getTaxProvider(provider);
if (calculator == null)
throw new AdempiereException(Msg.getMsg(getCtx(), "TaxNoProvider"));
if (!calculator.updateRMATax(provider, this))
return false;
m_parent = null;
return no == 1;
return calculator.updateHeaderTax(provider, this);
} // updateHeaderTax
/**
@ -683,5 +665,9 @@ public class MRMALine extends X_M_RMALine
}
return m_ioLine.getM_Locator_ID();
}
public void clearParent()
{
this.m_parent = null;
}
} // MRMALine

View File

@ -18,6 +18,8 @@ import java.util.ArrayList;
import org.adempiere.model.ITaxProvider;
import org.compiere.process.ProcessInfo;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
@ -25,9 +27,14 @@ import org.compiere.util.Msg;
* Standard tax provider
* @author Elaine
*
* @contributor Murilo H. Torquato <muriloht@devcoffee.com.br>
*
*/
public class StandardTaxProvider implements ITaxProvider {
/** Logger */
protected transient CLogger log = CLogger.getCLogger (getClass());
@Override
public boolean calculateOrderTaxTotal(MTaxProvider provider, MOrder order) {
// Lines
@ -113,6 +120,51 @@ public class StandardTaxProvider implements ITaxProvider {
return true;
}
@Override
public boolean recalculateTax(MTaxProvider provider, MOrderLine line, boolean newRecord)
{
if (!newRecord && line.is_ValueChanged(MOrderLine.COLUMNNAME_C_Tax_ID) && !line.getParent().isProcessed())
{
MTax mtax = new MTax(line.getCtx(), line.getC_Tax_ID(), line.get_TrxName());
if (mtax.getC_TaxProvider_ID() == 0)
{
// Recalculate Tax for old Tax
if (!line.updateOrderTax(true))
return false;
}
}
return line.updateHeaderTax();
}
@Override
public boolean updateHeaderTax(MTaxProvider provider, MOrderLine line)
{
// Update Order Header
String sql = "UPDATE C_Order i"
+ " SET TotalLines="
+ "(SELECT COALESCE(SUM(LineNetAmt),0) FROM C_OrderLine il WHERE i.C_Order_ID=il.C_Order_ID) "
+ "WHERE C_Order_ID=" + line.getC_Order_ID();
int no = DB.executeUpdate(sql, line.get_TrxName());
if (no != 1)
log.warning("(1) #" + no);
if (line.isTaxIncluded())
sql = "UPDATE C_Order i "
+ " SET GrandTotal=TotalLines "
+ "WHERE C_Order_ID=" + line.getC_Order_ID();
else
sql = "UPDATE C_Order i "
+ " SET GrandTotal=TotalLines+"
+ "(SELECT COALESCE(SUM(TaxAmt),0) FROM C_OrderTax it WHERE i.C_Order_ID=it.C_Order_ID) "
+ "WHERE C_Order_ID=" + line.getC_Order_ID();
no = DB.executeUpdate(sql, line.get_TrxName());
if (no != 1)
log.warning("(2) #" + no);
line.clearParent();
return no == 1;
}
@Override
public boolean calculateInvoiceTaxTotal(MTaxProvider provider, MInvoice invoice) {
// Lines
@ -211,6 +263,35 @@ public class StandardTaxProvider implements ITaxProvider {
return line.updateHeaderTax();
}
@Override
public boolean updateHeaderTax(MTaxProvider provider, MInvoiceLine line)
{
// Update Invoice Header
String sql = "UPDATE C_Invoice i"
+ " SET TotalLines="
+ "(SELECT COALESCE(SUM(LineNetAmt),0) FROM C_InvoiceLine il WHERE i.C_Invoice_ID=il.C_Invoice_ID) "
+ "WHERE C_Invoice_ID=?";
int no = DB.executeUpdateEx(sql, new Object[]{line.getC_Invoice_ID()}, line.get_TrxName());
if (no != 1)
log.warning("(1) #" + no);
if (line.isTaxIncluded())
sql = "UPDATE C_Invoice i "
+ " SET GrandTotal=TotalLines "
+ "WHERE C_Invoice_ID=?";
else
sql = "UPDATE C_Invoice i "
+ " SET GrandTotal=TotalLines+"
+ "(SELECT COALESCE(SUM(TaxAmt),0) FROM C_InvoiceTax it WHERE i.C_Invoice_ID=it.C_Invoice_ID) "
+ "WHERE C_Invoice_ID=?";
no = DB.executeUpdateEx(sql, new Object[]{line.getC_Invoice_ID()}, line.get_TrxName());
if (no != 1)
log.warning("(2) #" + no);
line.clearParent();
return no == 1;
}
@Override
public boolean calculateRMATaxTotal(MTaxProvider provider, MRMA rma) {
// Lines
@ -295,6 +376,40 @@ public class StandardTaxProvider implements ITaxProvider {
return true;
}
@Override
public boolean recalculateTax(MTaxProvider provider, MRMALine line, boolean newRecord)
{
if (!newRecord && line.is_ValueChanged(MRMALine.COLUMNNAME_C_Tax_ID) && !line.getParent().isProcessed())
{
MTax mtax = new MTax(line.getCtx(), line.getC_Tax_ID(), line.get_TrxName());
if (mtax.getC_TaxProvider_ID() == 0)
{
// Recalculate Tax for old Tax
if (!line.updateOrderTax(true))
return false;
}
}
return line.updateHeaderAmt();
}
@Override
public boolean updateHeaderTax(MTaxProvider provider, MRMALine line)
{
// Update RMA Header
String sql = "UPDATE M_RMA "
+ " SET Amt="
+ "(SELECT COALESCE(SUM(LineNetAmt),0) FROM M_RMALine WHERE M_RMA.M_RMA_ID=M_RMALine.M_RMA_ID) "
+ "WHERE M_RMA_ID=?";
int no = DB.executeUpdateEx(sql, new Object[]{line.getM_RMA_ID()}, line.get_TrxName());
if (no != 1)
log.warning("(1) #" + no);
line.clearParent();
return no == 1;
}
@Override
public String validateConnection(MTaxProvider provider, ProcessInfo pi) throws Exception {
throw new IllegalStateException(Msg.getMsg(provider.getCtx(), "ActionNotSupported"));