BF [ 1693997 ] Calculation of cost should happen when transaction complete (fix for MatchPO to Invoice)
This commit is contained in:
parent
60f34d42ce
commit
f2a7d111fd
|
@ -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,
|
boolean skip = false;
|
||||||
as.getAD_OrgOnly_ID()));
|
if (as.getAD_OrgOnly_ID() != 0)
|
||||||
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())
|
|
||||||
{
|
{
|
||||||
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
|
|
||||||
tAmt = tAmt.add(isReturnTrx ? poCost.negate() : poCost);
|
// Purchase Order Line
|
||||||
tQty = tQty.add(isReturnTrx ? getQty().negate() : getQty());
|
BigDecimal poCost = oLine.getPriceCost();
|
||||||
|
if (poCost == null || poCost.signum() == 0)
|
||||||
// Different currency
|
poCost = oLine.getPriceActual();
|
||||||
String costingMethod = as.getCostingMethod();
|
|
||||||
if (oLine.getC_Currency_ID() != as.getC_Currency_ID())
|
// Source from Doc_MatchPO.createFacts(MAcctSchema)
|
||||||
{
|
MInOutLine receiptLine = new MInOutLine (getCtx(), getM_InOutLine_ID(), get_TrxName());
|
||||||
MOrder order = oLine.getParent();
|
MInOut inOut = receiptLine.getParent();
|
||||||
Timestamp dateAcct = order.getDateAcct();
|
boolean isReturnTrx = inOut.getMovementType().equals(X_M_InOut.MOVEMENTTYPE_VendorReturns);
|
||||||
if (MAcctSchema.COSTINGMETHOD_AveragePO.equals(costingMethod) ||
|
|
||||||
MAcctSchema.COSTINGMETHOD_LastPOPrice.equals(costingMethod) )
|
// Create PO Cost Detail Record first
|
||||||
dateAcct = inOut.getDateAcct(); //Movement Date
|
// MZ Goodwill
|
||||||
BigDecimal rate = MConversionRate.getRate(
|
// Create Cost Detail Matched PO using Total Amount and Total Qty based on OrderLine
|
||||||
order.getC_Currency_ID(), as.getC_Currency_ID(),
|
MMatchPO[] mPO = MMatchPO.getOrderLine(getCtx(), oLine.getC_OrderLine_ID(), get_TrxName());
|
||||||
dateAcct, order.getC_ConversionType_ID(),
|
BigDecimal tQty = Env.ZERO;
|
||||||
oLine.getAD_Client_ID(), oLine.getAD_Org_ID());
|
BigDecimal tAmt = Env.ZERO;
|
||||||
if (rate == null)
|
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.multiply(getQty()); // Delivered so far
|
||||||
poCost = poCost.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
|
tAmt = tAmt.add(isReturnTrx ? poCost.negate() : poCost);
|
||||||
tAmt = tAmt.multiply(rate);
|
tQty = tQty.add(isReturnTrx ? getQty().negate() : getQty());
|
||||||
if (tAmt.scale() > as.getCostingPrecision())
|
|
||||||
tAmt = tAmt.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
|
// 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 "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue