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
private String createMatchPOCostDetail()
{
MOrderLine oLine = getOrderLine();
// Get Account Schemas to create MCostDetail
MAcctSchema[] acctschemas = MAcctSchema.getClientAcctSchema(getCtx(), getAD_Client_ID());
for(int asn = 0; asn < acctschemas.length; asn++)
if (getM_InOutLine_ID() != 0)
{
MAcctSchema as = acctschemas[asn];
MOrderLine oLine = getOrderLine();
boolean skip = false;
if (as.getAD_OrgOnly_ID() != 0)
// Get Account Schemas to create MCostDetail
MAcctSchema[] acctschemas = MAcctSchema.getClientAcctSchema(getCtx(), getAD_Client_ID());
for(int asn = 0; asn < acctschemas.length; asn++)
{
if (as.getOnlyOrgs() == null)
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
BigDecimal poCost = oLine.getPriceCost();
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())
MAcctSchema as = acctschemas[asn];
boolean skip = false;
if (as.getAD_OrgOnly_ID() != 0)
{
BigDecimal qty = (isReturnTrx ? mPO[i].getQty().negate() : mPO[i].getQty());
tQty = tQty.add(qty);
tAmt = tAmt.add(poCost.multiply(qty));
if (as.getOnlyOrgs() == null)
as.setOnlyOrgs(MReportTree.getChildIDs(getCtx(),
0, MAcctSchemaElement.ELEMENTTYPE_Organization,
as.getAD_OrgOnly_ID()));
skip = as.isSkipOrg(getAD_Org_ID());
}
}
poCost = poCost.multiply(getQty()); // Delivered so far
tAmt = tAmt.add(isReturnTrx ? poCost.negate() : poCost);
tQty = tQty.add(isReturnTrx ? getQty().negate() : getQty());
// Different currency
String costingMethod = as.getCostingMethod();
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)
if (skip)
continue;
// Purchase Order Line
BigDecimal poCost = oLine.getPriceCost();
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++)
{
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);
poCost = poCost.multiply(getQty()); // Delivered so far
tAmt = tAmt.add(isReturnTrx ? poCost.negate() : poCost);
tQty = tQty.add(isReturnTrx ? getQty().negate() : getQty());
// Different currency
String costingMethod = as.getCostingMethod();
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
}
// 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 "";
}