[ 1851417 ] Problem with AveragePO Costing

[ 1693997 ] Calculation of cost should happen when transaction complete
[ 1991574 ] Average Cost Error
This commit is contained in:
Heng Sin Low 2008-08-08 06:53:33 +00:00
parent 22df2fec30
commit 1759d9d4a7
8 changed files with 379 additions and 33 deletions

View File

@ -285,6 +285,9 @@ public class Doc_InOut extends Doc
{
for (int i = 0; i < p_lines.length; i++)
{
// Elaine 2008/06/26
int C_Currency_ID = as.getC_Currency_ID();
//
DocLine line = p_lines[i];
BigDecimal costs = null;
MProduct product = line.getProduct();
@ -298,11 +301,23 @@ public class Doc_InOut extends Doc
MAcctSchema.COSTINGMETHOD_LastPOPrice.equals(costingMethod) )
{
int C_OrderLine_ID = line.getC_OrderLine_ID();
MOrderLine orderLine = new MOrderLine (getCtx(), C_OrderLine_ID, getTrxName());
costs = orderLine.getPriceCost();
if (costs == null || costs.signum() == 0)
costs = orderLine.getPriceActual();
costs = costs.multiply(line.getQty());
// Low - check if c_orderline_id is valid
if (C_OrderLine_ID > 0)
{
MOrderLine orderLine = new MOrderLine (getCtx(), C_OrderLine_ID, getTrxName());
costs = orderLine.getPriceCost();
if (costs == null || costs.signum() == 0)
costs = orderLine.getPriceActual();
costs = costs.multiply(line.getQty());
// Elaine 2008/06/26
C_Currency_ID = orderLine.getC_Currency_ID();
//
}
else
{
costs = line.getProductCosts(as, line.getAD_Org_ID(), false); // current costs
}
//
}
else
{
@ -318,8 +333,13 @@ public class Doc_InOut extends Doc
MAccount assets = line.getAccount(ProductCost.ACCTTYPE_P_Asset, as);
if (product.isService())
assets = line.getAccount(ProductCost.ACCTTYPE_P_Expense, as);
// Elaine 2008/06/26
/*dr = fact.createLine(line, assets,
as.getC_Currency_ID(), costs, null);*/
dr = fact.createLine(line, assets,
as.getC_Currency_ID(), costs, null);
C_Currency_ID, costs, null);
//
if (dr == null)
{
p_Error = "DR not created: " + line;
@ -330,9 +350,14 @@ public class Doc_InOut extends Doc
dr.setLocationFromBPartner(getC_BPartner_Location_ID(), true); // from Loc
dr.setLocationFromLocator(line.getM_Locator_ID(), false); // to Loc
// NotInvoicedReceipt CR
// Elaine 2008/06/26
/*cr = fact.createLine(line,
getAccount(Doc.ACCTTYPE_NotInvoicedReceipts, as),
as.getC_Currency_ID(), null, costs);*/
cr = fact.createLine(line,
getAccount(Doc.ACCTTYPE_NotInvoicedReceipts, as),
as.getC_Currency_ID(), null, costs);
C_Currency_ID, null, costs);
//
if (cr == null)
{
p_Error = "CR not created: " + line;
@ -350,6 +375,9 @@ public class Doc_InOut extends Doc
{
for (int i = 0; i < p_lines.length; i++)
{
// Elaine 2008/06/26
int C_Currency_ID = as.getC_Currency_ID();
//
DocLine line = p_lines[i];
BigDecimal costs = null;
MProduct product = line.getProduct();
@ -368,6 +396,9 @@ public class Doc_InOut extends Doc
if (costs == null || costs.signum() == 0)
costs = orderLine.getPriceActual();
costs = costs.multiply(line.getQty());
// Elaine 2008/06/26
C_Currency_ID = orderLine.getC_Currency_ID();
//
}
else
{
@ -380,9 +411,14 @@ public class Doc_InOut extends Doc
return null;
}
// NotInvoicedReceipt DR
// Elaine 2008/06/26
/*dr = fact.createLine(line,
getAccount(Doc.ACCTTYPE_NotInvoicedReceipts, as),
as.getC_Currency_ID(), costs , null);*/
dr = fact.createLine(line,
getAccount(Doc.ACCTTYPE_NotInvoicedReceipts, as),
as.getC_Currency_ID(), costs , null);
C_Currency_ID, costs , null);
//
if (dr == null)
{
p_Error = "CR not created: " + line;
@ -398,8 +434,12 @@ public class Doc_InOut extends Doc
MAccount assets = line.getAccount(ProductCost.ACCTTYPE_P_Asset, as);
if (product.isService())
assets = line.getAccount(ProductCost.ACCTTYPE_P_Expense, as);
// Elaine 2008/06/26
/*cr = fact.createLine(line, assets,
as.getC_Currency_ID(), null, costs);*/
cr = fact.createLine(line, assets,
as.getC_Currency_ID(), null, costs);
C_Currency_ID, null, costs);
//
if (cr == null)
{
p_Error = "DR not created: " + line;

View File

@ -16,11 +16,22 @@
*****************************************************************************/
package org.compiere.acct;
import java.math.*;
import java.sql.*;
import java.util.*;
import org.compiere.model.*;
import org.compiere.util.*;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.util.ArrayList;
import org.compiere.model.MAccount;
import org.compiere.model.MAcctSchema;
import org.compiere.model.MAcctSchemaElement;
import org.compiere.model.MConversionRate;
import org.compiere.model.MInOut;
import org.compiere.model.MInOutLine;
import org.compiere.model.MInvoice;
import org.compiere.model.MInvoiceLine;
import org.compiere.model.MMatchInv;
import org.compiere.model.ProductCost;
import org.compiere.util.DB;
import org.compiere.util.Env;
/**
* Post MatchInv Documents.
@ -57,7 +68,7 @@ public class Doc_MatchInv extends Doc
private ProductCost m_pc = null;
/** Commitments */
private DocLine[] m_commitments = null;
// private DocLine[] m_commitments = null;
/**
* Load Specific Document Details
@ -124,7 +135,7 @@ public class Doc_MatchInv extends Doc
+ ",Qty=" + getQty() + ",InOutQty=" + m_receiptLine.getMovementQty());
return facts;
}
MMatchInv matchInv = (MMatchInv)getPO();
// MMatchInv matchInv = (MMatchInv)getPO();
// create Fact Header
Fact fact = new Fact(this, as, Fact.POST_Actual);
@ -262,7 +273,8 @@ public class Doc_MatchInv extends Doc
pv.setUser2_ID(m_invoiceLine.getUser2_ID());
}
log.fine("IPV=" + ipv + "; Balance=" + fact.getSourceBalance());
// Elaine 2008/6/20
/* Source move to MInvoice.createMatchInvCostDetail()
// Cost Detail Record - data from Expense/IncClearing (CR) record
// MZ Goodwill
// Create Cost Detail Matched Invoice using Total Amount and Total Qty based on InvoiceLine
@ -309,7 +321,7 @@ public class Doc_MatchInv extends Doc
m_invoiceLine.getC_InvoiceLine_ID(), 0, // No cost element
tAmt, tQty, getDescription(), getTrxName());
// end MZ
*/
// Update Costing
updateProductInfo(as.getC_AcctSchema_ID(),
MAcctSchema.COSTINGMETHOD_StandardCosting.equals(as.getCostingMethod()));

View File

@ -126,6 +126,8 @@ public class Doc_MatchPO extends Doc
MInOut inOut = receiptLine.getParent();
boolean isReturnTrx = inOut.getMovementType().equals(X_M_InOut.MOVEMENTTYPE_VendorReturns);
// Elaine 2008/6/20
/* Source move to MInOut.createMatchPOCostDetail()
// Create PO Cost Detail Record first
// MZ Goodwill
// Create Cost Detail Matched PO using Total Amount and Total Qty based on OrderLine
@ -143,9 +145,9 @@ public class Doc_MatchPO extends Doc
tAmt = tAmt.add(poCost.multiply(qty));
}
}
poCost = poCost.multiply(getQty()); // Delivered so far
tAmt = tAmt.add(isReturnTrx ? poCost.negate() : poCost);
tQty = tQty.add(isReturnTrx ? getQty().negate() : getQty());
poCost = poCost.multiply(getQty()); // Delivered so far
// Different currency
String costingMethod = as.getCostingMethod();
@ -180,8 +182,9 @@ public class Doc_MatchPO extends Doc
tAmt, tQty, // Delivered
m_oLine.getDescription(), getTrxName());
// end MZ
*/
// Calculate PPV for standard costing
String costingMethod = as.getCostingMethod();
MProduct product = MProduct.get(getCtx(), getM_Product_ID());
MProductCategoryAcct pca = MProductCategoryAcct.get(getCtx(),
product.getM_Product_Category_ID(), as.getC_AcctSchema_ID(), getTrxName());

View File

@ -16,12 +16,15 @@
*****************************************************************************/
package org.compiere.model;
import java.math.*;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.logging.Level;
import org.compiere.util.*;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
/**
* Cost Detail Model
@ -31,6 +34,8 @@ import org.compiere.util.*;
*/
public class MCostDetail extends X_M_CostDetail
{
private static final long serialVersionUID = 1L;
/**
* Create New Order Cost Detail for Purchase Orders.
* Called from Doc_MatchPO
@ -1071,4 +1076,57 @@ public class MCostDetail extends X_M_CostDetail
return cost.save();
} // process
// Elaine 2008/6/20
protected boolean afterDelete (boolean success)
{
if(success)
{
// recalculate MCost
boolean ok = false;
// get costing level for product
MAcctSchema as = new MAcctSchema (getCtx(), getC_AcctSchema_ID(), null);
String CostingLevel = as.getCostingLevel();
MProduct product = MProduct.get(getCtx(), getM_Product_ID());
MProductCategoryAcct pca = MProductCategoryAcct.get (getCtx(),
product.getM_Product_Category_ID(), getC_AcctSchema_ID(), null);
if (pca.getCostingLevel() != null)
CostingLevel = pca.getCostingLevel();
// Org Element
int Org_ID = getAD_Org_ID();
int M_ASI_ID = getM_AttributeSetInstance_ID();
if (MAcctSchema.COSTINGLEVEL_Client.equals(CostingLevel))
{
Org_ID = 0;
M_ASI_ID = 0;
}
else if (MAcctSchema.COSTINGLEVEL_Organization.equals(CostingLevel))
M_ASI_ID = 0;
else if (MAcctSchema.COSTINGLEVEL_BatchLot.equals(CostingLevel))
Org_ID = 0;
// Create Material Cost elements
if (getM_CostElement_ID() == 0)
{
MCostElement[] ces = MCostElement.getCostingMethods(this);
for (int i = 0; i < ces.length; i++)
{
MCostElement ce = ces[i];
ok = process (as, product, ce, Org_ID, M_ASI_ID);
if (!ok)
break;
}
} // Material Cost elements
else
{
MCostElement ce = MCostElement.get(getCtx(), getM_CostElement_ID());
ok = process (as, product, ce, Org_ID, M_ASI_ID);
}
return ok;
}
return super.afterDelete(success);
}
//
} // MCostDetail

View File

@ -29,6 +29,7 @@ import java.util.logging.Level;
import org.compiere.print.ReportEngine;
import org.compiere.process.DocAction;
import org.compiere.process.DocumentEngine;
import org.compiere.report.MReportTree;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
@ -1473,6 +1474,11 @@ public class MInOut extends X_M_InOut implements DocAction
oLine.setM_AttributeSetInstance_ID(sLine.getM_AttributeSetInstance_ID());
oLine.save(get_TrxName());
}
// Elaine 2008/6/20
String err = createMatchPOCostDetail(po, oLine);
if(err != null && err.length() > 0) return err;
//
}
else // No Order - Try finding links via Invoice
{
@ -1497,6 +1503,11 @@ public class MInOut extends X_M_InOut implements DocAction
oLine.setM_AttributeSetInstance_ID(sLine.getM_AttributeSetInstance_ID());
oLine.save(get_TrxName());
}
// Elaine 2008/6/20
String err = createMatchPOCostDetail(po, oLine);
if(err != null && err.length() > 0) return err;
//
}
} // No Order
} // PO Matching
@ -1523,7 +1534,119 @@ public class MInOut extends X_M_InOut implements DocAction
setDocAction(DOCACTION_Close);
return DocAction.STATUS_Completed;
} // completeIt
// Elaine 2008/6/20
private String createMatchPOCostDetail(MMatchPO po, MOrderLine m_oLine)
{
// Get Account Schemas to create MCostDetail
MAcctSchema[] acctschemas = MAcctSchema.getClientAcctSchema(getCtx(), getAD_Client_ID());
for(int asn = 0; asn < acctschemas.length; asn++)
{
MAcctSchema as = acctschemas[asn];
boolean skip = false;
if (as.getAD_OrgOnly_ID() != 0)
{
if (as.getOnlyOrgs() == null)
as.setOnlyOrgs(MReportTree.getChildIDs(getCtx(),
0, MAcctSchemaElement.ELEMENTTYPE_Organization,
as.getAD_OrgOnly_ID()));
// Header Level Org
skip = as.isSkipOrg(getAD_Org_ID());
// Line Level Org
skip = as.isSkipOrg(m_oLine.getAD_Org_ID());
}
if (skip)
continue;
// Purchase Order Line
BigDecimal poCost = m_oLine.getPriceCost();
if (poCost == null || poCost.signum() == 0)
poCost = m_oLine.getPriceActual();
poCost = poCost.multiply(po.getQty()); // Delivered so far
// Different currency
if (m_oLine.getC_Currency_ID() != as.getC_Currency_ID())
{
MOrder order = m_oLine.getParent();
BigDecimal rate = MConversionRate.getRate(
order.getC_Currency_ID(), as.getC_Currency_ID(),
order.getDateAcct(), order.getC_ConversionType_ID(),
m_oLine.getAD_Client_ID(), m_oLine.getAD_Org_ID());
if (rate == null)
{
return "Purchase Order not convertible - " + as.getName();
}
poCost = poCost.multiply(rate);
if (poCost.scale() > as.getCostingPrecision())
poCost = poCost.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
}
// Source from Doc_MatchPO.createFacts(MAcctSchema)
MInOutLine receiptLine = new MInOutLine (getCtx(), po.getM_InOutLine_ID(), po.get_TrxName());
MInOut inOut = receiptLine.getParent();
boolean isReturnTrx = inOut.getMovementType().equals(X_M_InOut.MOVEMENTTYPE_VendorReturns);
// Create PO Cost Detail Record first
// MZ Goodwill
// Create Cost Detail Matched PO using Total Amount and Total Qty based on OrderLine
MMatchPO[] mPO = MMatchPO.getOrderLine(getCtx(), m_oLine.getC_OrderLine_ID(), po.get_TrxName());
BigDecimal tQty = Env.ZERO;
BigDecimal tAmt = Env.ZERO;
for (int i = 0 ; i < mPO.length ; i++)
{
if (mPO[i].isPosted()
&& mPO[i].getM_AttributeSetInstance_ID() == po.getM_AttributeSetInstance_ID()
&& mPO[i].getM_MatchPO_ID() != get_ID())
{
BigDecimal qty = (isReturnTrx ? mPO[i].getQty().negate() : mPO[i].getQty());
tQty = tQty.add(qty);
tAmt = tAmt.add(poCost.multiply(qty));
}
}
tAmt = tAmt.add(isReturnTrx ? poCost.negate() : poCost);
tQty = tQty.add(isReturnTrx ? po.getQty().negate() : po.getQty());
poCost = poCost.multiply(po.getQty()); // Delivered so far
// Different currency
String costingMethod = as.getCostingMethod();
if (m_oLine.getC_Currency_ID() != as.getC_Currency_ID())
{
MOrder order = m_oLine.getParent();
Timestamp dateAcct = order.getDateAcct();
if (MAcctSchema.COSTINGMETHOD_AveragePO.equals(costingMethod) ||
MAcctSchema.COSTINGMETHOD_LastPOPrice.equals(costingMethod) )
dateAcct = po.getDateAcct(); //Movement Date
BigDecimal rate = MConversionRate.getRate(
order.getC_Currency_ID(), as.getC_Currency_ID(),
dateAcct, order.getC_ConversionType_ID(),
m_oLine.getAD_Client_ID(), m_oLine.getAD_Org_ID());
if (rate == null)
{
return "Purchase Order not convertible - " + as.getName();
}
poCost = poCost.multiply(rate);
if (poCost.scale() > as.getCostingPrecision())
poCost = poCost.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
tAmt = tAmt.multiply(rate);
if (tAmt.scale() > as.getCostingPrecision())
tAmt = tAmt.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
}
// Set Total Amount and Total Quantity from Matched PO
MCostDetail.createOrder(as, m_oLine.getAD_Org_ID(),
po.getM_Product_ID(), po.getM_AttributeSetInstance_ID(),
m_oLine.getC_OrderLine_ID(), 0, // no cost element
tAmt, tQty, // Delivered
m_oLine.getDescription(), po.get_TrxName());
// end MZ
// end
}
return "";
}
//
/**
* Set the definite document number after completed
*/

View File

@ -16,14 +16,24 @@
*****************************************************************************/
package org.compiere.model;
import java.io.*;
import java.math.*;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import org.compiere.print.*;
import org.compiere.process.*;
import org.compiere.util.*;
import java.io.File;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import org.compiere.print.ReportEngine;
import org.compiere.process.DocAction;
import org.compiere.process.DocumentEngine;
import org.compiere.report.MReportTree;
import org.compiere.util.CCache;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.eevolution.model.MPPProductBOM;
import org.eevolution.model.MPPProductBOMLine;
@ -42,6 +52,8 @@ import org.eevolution.model.MPPProductBOMLine;
*/
public class MInvoice extends X_C_Invoice implements DocAction
{
private static final long serialVersionUID = 1L;
/**
* Get Payments Of BPartner
* @param ctx context
@ -1779,6 +1791,11 @@ public class MInvoice extends X_C_Invoice implements DocAction
}
else
matchInv++;
// Elaine 2008/6/20
String err = createMatchInvCostDetail(inv, line, receiptLine);
if(err != null && err.length() > 0) return err;
//
}
} // for all lines
if (matchInv > 0)
@ -1906,6 +1923,91 @@ public class MInvoice extends X_C_Invoice implements DocAction
setDocAction(DOCACTION_Close);
return DocAction.STATUS_Completed;
} // completeIt
// Elaine 2008/6/20
private String createMatchInvCostDetail(MMatchInv inv, MInvoiceLine m_invoiceLine, MInOutLine m_receiptLine)
{
// Get Account Schemas to create MCostDetail
MAcctSchema[] acctschemas = MAcctSchema.getClientAcctSchema(getCtx(), getAD_Client_ID());
for(int asn = 0; asn < acctschemas.length; asn++)
{
MAcctSchema as = acctschemas[asn];
boolean skip = false;
if (as.getAD_OrgOnly_ID() != 0)
{
if (as.getOnlyOrgs() == null)
as.setOnlyOrgs(MReportTree.getChildIDs(getCtx(),
0, MAcctSchemaElement.ELEMENTTYPE_Organization,
as.getAD_OrgOnly_ID()));
// Header Level Org
skip = as.isSkipOrg(getAD_Org_ID());
// Line Level Org
skip = as.isSkipOrg(m_invoiceLine.getAD_Org_ID());
}
if (skip)
continue;
BigDecimal LineNetAmt = m_invoiceLine.getLineNetAmt();
BigDecimal multiplier = inv.getQty()
.divide(m_invoiceLine.getQtyInvoiced(), 12, BigDecimal.ROUND_HALF_UP)
.abs();
if (multiplier.compareTo(Env.ONE) != 0)
LineNetAmt = LineNetAmt.multiply(multiplier);
// Source from Doc_MatchInv.createFacts(MAcctSchema)
// Cost Detail Record - data from Expense/IncClearing (CR) record
// MZ Goodwill
// Create Cost Detail Matched Invoice using Total Amount and Total Qty based on InvoiceLine
MMatchInv[] mInv = MMatchInv.getInvoiceLine(getCtx(), m_invoiceLine.getC_InvoiceLine_ID(), inv.get_TrxName());
BigDecimal tQty = Env.ZERO;
BigDecimal tAmt = Env.ZERO;
for (int i = 0 ; i < mInv.length ; i++)
{
if (mInv[i].isPosted() && mInv[i].getM_MatchInv_ID() != get_ID())
{
tQty = tQty.add(mInv[i].getQty());
multiplier = mInv[i].getQty()
.divide(m_invoiceLine.getQtyInvoiced(), 12, BigDecimal.ROUND_HALF_UP).abs();
tAmt = tAmt.add(m_invoiceLine.getLineNetAmt().multiply(multiplier));
}
}
// Different currency
MInvoice invoice = m_invoiceLine.getParent();
if (as.getC_Currency_ID() != invoice.getC_Currency_ID())
{
tAmt = MConversionRate.convert(getCtx(), tAmt,
invoice.getC_Currency_ID(), as.getC_Currency_ID(),
invoice.getDateAcct(), invoice.getC_ConversionType_ID(),
invoice.getAD_Client_ID(), invoice.getAD_Org_ID());
if (tAmt == null)
{
return "AP Invoice not convertible - " + as.getName();
}
}
tAmt = tAmt.add(LineNetAmt); //Invoice Price
// set Qty to negative value when MovementType is Vendor Returns
MInOut receipt = m_receiptLine.getParent();
if (receipt.getMovementType().equals(MInOut.MOVEMENTTYPE_VendorReturns))
tQty = tQty.add(inv.getQty().negate()); // Qty is set to negative value
else
tQty = tQty.add(inv.getQty());
// Set Total Amount and Total Quantity from Matched Invoice
MCostDetail.createInvoice(as, getAD_Org_ID(),
inv.getM_Product_ID(), inv.getM_AttributeSetInstance_ID(),
m_invoiceLine.getC_InvoiceLine_ID(), 0, // No cost element
tAmt, tQty, getDescription(), inv.get_TrxName());
// end MZ
// end
}
return "";
}
//
/**
* Set the definite document number after completed

View File

@ -0,0 +1,4 @@
UPDATE AD_Column SET IsParent='Y', IsUpdateable='N',Updated=TO_DATE('2008-06-28 22:10:02','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Column_ID=13468
;

View File

@ -0,0 +1,4 @@
UPDATE AD_Column SET IsParent='Y', IsUpdateable='N',Updated=TO_DATE('2008-06-28 22:10:02','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Column_ID=13468
;