BF [ 1693997 ] Calculation of cost should happen when transaction complete (fix for MatchPO to Invoice)

This commit is contained in:
armenrz 2008-10-16 12:28:40 +00:00
parent 60f34d42ce
commit f2a7d111fd
1 changed files with 79 additions and 77 deletions

View File

@ -800,92 +800,94 @@ public class MMatchPO extends X_M_MatchPO
// Elaine 2008/6/20 // Elaine 2008/6/20
private String createMatchPOCostDetail() private String createMatchPOCostDetail()
{ {
MOrderLine oLine = getOrderLine(); if (getM_InOutLine_ID() != 0)
// 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]; MOrderLine oLine = getOrderLine();
boolean skip = false; // Get Account Schemas to create MCostDetail
if (as.getAD_OrgOnly_ID() != 0) MAcctSchema[] acctschemas = MAcctSchema.getClientAcctSchema(getCtx(), getAD_Client_ID());
for(int asn = 0; asn < acctschemas.length; asn++)
{ {
if (as.getOnlyOrgs() == null) MAcctSchema as = acctschemas[asn];
as.setOnlyOrgs(MReportTree.getChildIDs(getCtx(),
0, MAcctSchemaElement.ELEMENTTYPE_Organization,
as.getAD_OrgOnly_ID()));
skip = as.isSkipOrg(getAD_Org_ID());
}
if (skip)
continue;
// Purchase Order Line boolean skip = false;
BigDecimal poCost = oLine.getPriceCost(); if (as.getAD_OrgOnly_ID() != 0)
if (poCost == null || poCost.signum() == 0)
poCost = oLine.getPriceActual();
// Source from Doc_MatchPO.createFacts(MAcctSchema)
MInOutLine receiptLine = new MInOutLine (getCtx(), getM_InOutLine_ID(), 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(), oLine.getC_OrderLine_ID(), 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() == getM_AttributeSetInstance_ID()
&& mPO[i].getM_MatchPO_ID() != get_ID())
{ {
BigDecimal qty = (isReturnTrx ? mPO[i].getQty().negate() : mPO[i].getQty()); if (as.getOnlyOrgs() == null)
tQty = tQty.add(qty); as.setOnlyOrgs(MReportTree.getChildIDs(getCtx(),
tAmt = tAmt.add(poCost.multiply(qty)); 0, MAcctSchemaElement.ELEMENTTYPE_Organization,
as.getAD_OrgOnly_ID()));
skip = as.isSkipOrg(getAD_Org_ID());
} }
} if (skip)
continue;
poCost = poCost.multiply(getQty()); // Delivered so far // Purchase Order Line
tAmt = tAmt.add(isReturnTrx ? poCost.negate() : poCost); BigDecimal poCost = oLine.getPriceCost();
tQty = tQty.add(isReturnTrx ? getQty().negate() : getQty()); if (poCost == null || poCost.signum() == 0)
poCost = oLine.getPriceActual();
// Different currency // Source from Doc_MatchPO.createFacts(MAcctSchema)
String costingMethod = as.getCostingMethod(); MInOutLine receiptLine = new MInOutLine (getCtx(), getM_InOutLine_ID(), get_TrxName());
if (oLine.getC_Currency_ID() != as.getC_Currency_ID()) MInOut inOut = receiptLine.getParent();
{ boolean isReturnTrx = inOut.getMovementType().equals(X_M_InOut.MOVEMENTTYPE_VendorReturns);
MOrder order = oLine.getParent();
Timestamp dateAcct = order.getDateAcct(); // Create PO Cost Detail Record first
if (MAcctSchema.COSTINGMETHOD_AveragePO.equals(costingMethod) || // MZ Goodwill
MAcctSchema.COSTINGMETHOD_LastPOPrice.equals(costingMethod) ) // Create Cost Detail Matched PO using Total Amount and Total Qty based on OrderLine
dateAcct = inOut.getDateAcct(); //Movement Date MMatchPO[] mPO = MMatchPO.getOrderLine(getCtx(), oLine.getC_OrderLine_ID(), get_TrxName());
BigDecimal rate = MConversionRate.getRate( BigDecimal tQty = Env.ZERO;
order.getC_Currency_ID(), as.getC_Currency_ID(), BigDecimal tAmt = Env.ZERO;
dateAcct, order.getC_ConversionType_ID(), for (int i = 0 ; i < mPO.length ; i++)
oLine.getAD_Client_ID(), oLine.getAD_Org_ID());
if (rate == null)
{ {
return "Purchase Order not convertible - " + as.getName(); if (mPO[i].isPosted()
&& mPO[i].getM_AttributeSetInstance_ID() == 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));
}
} }
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 poCost = poCost.multiply(getQty()); // Delivered so far
MCostDetail.createOrder(as, oLine.getAD_Org_ID(), tAmt = tAmt.add(isReturnTrx ? poCost.negate() : poCost);
getM_Product_ID(), getM_AttributeSetInstance_ID(), tQty = tQty.add(isReturnTrx ? getQty().negate() : getQty());
oLine.getC_OrderLine_ID(), 0, // no cost element
tAmt, tQty, // Delivered // Different currency
oLine.getDescription(), get_TrxName()); String costingMethod = as.getCostingMethod();
// end MZ if (oLine.getC_Currency_ID() != as.getC_Currency_ID())
{
MOrder order = oLine.getParent();
Timestamp dateAcct = order.getDateAcct();
if (MAcctSchema.COSTINGMETHOD_AveragePO.equals(costingMethod) ||
MAcctSchema.COSTINGMETHOD_LastPOPrice.equals(costingMethod) )
dateAcct = inOut.getDateAcct(); //Movement Date
BigDecimal rate = MConversionRate.getRate(
order.getC_Currency_ID(), as.getC_Currency_ID(),
dateAcct, order.getC_ConversionType_ID(),
oLine.getAD_Client_ID(), 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, oLine.getAD_Org_ID(),
getM_Product_ID(), getM_AttributeSetInstance_ID(),
oLine.getC_OrderLine_ID(), 0, // no cost element
tAmt, tQty, // Delivered
oLine.getDescription(), get_TrxName());
// end MZ
}
} }
return ""; return "";
} }