BF [ 1693997 ] Calculation of cost should happen when transaction complete

This commit is contained in:
armenrz 2008-10-07 04:35:29 +00:00
parent 9bafff9969
commit b421080724
2 changed files with 108 additions and 106 deletions

View File

@ -1476,11 +1476,6 @@ 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
{
@ -1505,11 +1500,6 @@ 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
@ -1537,102 +1527,6 @@ public class MInOut extends X_M_InOut implements DocAction
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();
// 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));
}
}
poCost = poCost.multiply(po.getQty()); // Delivered so far
tAmt = tAmt.add(isReturnTrx ? poCost.negate() : poCost);
tQty = tQty.add(isReturnTrx ? po.getQty().negate() : po.getQty());
// 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

@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import org.compiere.report.MReportTree;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
@ -611,6 +612,17 @@ public class MMatchPO extends X_M_MatchPO
}
return orderLine.save();
}
//
if (newRecord)
{
// Elaine 2008/6/20
String err = createMatchPOCostDetail(this, getOrderLine());
if(err != null && err.length() > 0)
{
s_log.warning(err);
return false;
}
}
return success;
} // afterSave
@ -808,4 +820,100 @@ public class MMatchPO extends X_M_MatchPO
s_log.info("Success #" + success + " - Error #" + errors);
} // consolidate
// 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();
// 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));
}
}
poCost = poCost.multiply(po.getQty()); // Delivered so far
tAmt = tAmt.add(isReturnTrx ? poCost.negate() : poCost);
tQty = tQty.add(isReturnTrx ? po.getQty().negate() : po.getQty());
// 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 "";
}
//
} // MMatchPO