use of org.compiere.model.MCostElement in project adempiere by adempiere.
the class CostBillOfMaterial method createLines.
/**
* createLines
*
* @param bom
* @param bomLine
*/
private void createLines(MAcctSchema accountSchema, MPPProductBOM bom, MPPProductBOMLine bomLine) {
MProduct product;
BigDecimal qty;
if (bomLine != null) {
product = MProduct.get(getCtx(), bomLine.getM_Product_ID());
qty = bomLine.getQty();
} else if (bom != null) {
product = MProduct.get(getCtx(), bom.getM_Product_ID());
qty = Env.ONE;
} else {
throw new AdempiereException("@NotFound@ @PP_Product_BOM_ID@");
}
//for (MCostElement costElement : getCostElements())
getCostElements().stream().filter(costElement -> costElement != null).forEach(costElement -> {
X_T_BOMLine reportBOMLine = new X_T_BOMLine(getCtx(), 0, get_TrxName());
reportBOMLine.setAD_Org_ID(getOrganizationId());
reportBOMLine.setM_Warehouse_ID(getWarehouseId());
reportBOMLine.setSel_Product_ID(getProductId());
reportBOMLine.setImplosion(isImplosion);
reportBOMLine.setC_AcctSchema_ID(getAccountingSchemaId());
reportBOMLine.setM_CostType_ID(getCostTypeId());
reportBOMLine.setCostingMethod(getCostingMethod());
reportBOMLine.setAD_PInstance_ID(getAD_PInstance_ID());
reportBOMLine.setM_CostElement_ID(costElement.get_ID());
reportBOMLine.setM_Product_ID(product.get_ID());
reportBOMLine.setM_Warehouse_ID(getWarehouseId());
reportBOMLine.setQtyBOM(qty);
reportBOMLine.setSeqNo(seqNo);
reportBOMLine.setLevelNo(levelNo);
reportBOMLine.setLevels(LEVELS.substring(0, levelNo) + levelNo);
BigDecimal currentCostPrice = Env.ZERO;
BigDecimal currentCostPriceLL = Env.ZERO;
BigDecimal futureCostPrice = Env.ZERO;
BigDecimal futureCostPriceLL = Env.ZERO;
final CostEngine engine = CostEngineFactory.getCostEngine(getAD_Client_ID());
List<MCost> costs = MCost.getByElement(product, accountSchema, getCostTypeId(), getOrganizationId(), getWarehouseId(), 0, costElement.getM_CostElement_ID());
boolean isCostFrozen = false;
for (MCost cost : costs) {
currentCostPrice = currentCostPrice.add(cost.getCurrentCostPrice());
currentCostPriceLL = currentCostPriceLL.add(cost.getCurrentCostPriceLL());
futureCostPrice = futureCostPrice.add(cost.getFutureCostPrice());
futureCostPriceLL = futureCostPriceLL.add(cost.getFutureCostPriceLL());
isCostFrozen = cost.isCostFrozen();
}
reportBOMLine.setCurrentCostPrice(currentCostPrice);
reportBOMLine.setCurrentCostPriceLL(currentCostPriceLL);
reportBOMLine.setFutureCostPrice(currentCostPrice);
reportBOMLine.setFutureCostPriceLL(currentCostPriceLL);
reportBOMLine.setIsCostFrozen(isCostFrozen);
if (bomLine != null) {
reportBOMLine.setPP_Product_BOM_ID(bomLine.getPP_Product_BOM_ID());
reportBOMLine.setPP_Product_BOMLine_ID(bomLine.getPP_Product_BOMLine_ID());
} else if (bom != null) {
reportBOMLine.setPP_Product_BOM_ID(bom.getPP_Product_BOM_ID());
}
reportBOMLine.saveEx();
seqNo++;
});
}
use of org.compiere.model.MCostElement in project adempiere by adempiere.
the class RollupBillOfMaterial method doIt.
// prepare
/**
* Generate Calculate Cost
* @return info
* @throws Exception
*/
protected String doIt() throws Exception {
//Get account schema
MAcctSchema acctSchema = MAcctSchema.get(getCtx(), getAccountingSchemaId());
//Get cost type
MCostType costType = MCostType.get(getCtx(), getCostTypeId());
final List<MCostElement> costElements = getCostElementId() > 0 ? Arrays.asList(MCostElement.get(getCtx(), getCostElementId())) : MCostElement.getCostElement(getCtx(), get_TrxName());
//Get max low level
int maxLowLevel = MPPMRP.getMaxLowLevel(getCtx(), get_TrxName());
// Cost Roll-up for all levels
for (int lowLevel = maxLowLevel; lowLevel >= 0; lowLevel--) {
//Iterate product based in parameters
Arrays.stream(getProductIds(lowLevel)).filter(productId -> productId > 0).forEach(productId -> {
MProduct product = MProduct.get(getCtx(), productId);
I_PP_Product_Planning productPlanning = MPPProductPlanning.find(getCtx(), getOrganizationId(), getWarehouseId(), getResourcePlantId(), productId, get_TrxName());
int bomId = 0;
if (productPlanning != null)
bomId = productPlanning.getPP_Product_BOM_ID();
else
createNotice(product, Msg.parseTranslation(getCtx(), "@NotFound@ @PP_Product_Planning_ID@"));
if (bomId <= 0)
bomId = MPPProductBOM.getBOMSearchKey(product);
MPPProductBOM bom = MPPProductBOM.get(getCtx(), bomId);
if (bom == null)
createNotice(product, Msg.parseTranslation(getCtx(), "@NotFound@ @PP_Product_BOM_ID@"));
Trx.run(new TrxRunnable() {
MAcctSchema acctSchema;
MCostType costType;
MProduct product;
MPPProductBOM bom;
public TrxRunnable setParameters(MAcctSchema acctSchema, MCostType costType, MProduct product, MPPProductBOM bom) {
this.acctSchema = acctSchema;
this.costType = costType;
this.product = product;
this.bom = bom;
return this;
}
public void run(String trxName) {
costElements.stream().filter(costElement -> costElement != null).forEach(costElement -> {
rollup(acctSchema, costType, costElement, product, bom, trxName);
});
}
}.setParameters(acctSchema, costType, product, bom));
});
// Products List
}
// for Low Lever
return "@OK@";
}
use of org.compiere.model.MCostElement in project adempiere by adempiere.
the class Doc_Production method createFacts.
// getBalance
/**
* Create Facts (the accounting logic) for
* MMP.
* <pre>
* Production
* Inventory DR CR
* </pre>
* @param as account schema
* @return Fact
*/
public ArrayList<Fact> createFacts(MAcctSchema as) {
// create Fact Header
Fact fact = new Fact(this, as, Fact.POST_Actual);
setC_Currency_ID(as.getC_Currency_ID());
// Line pointer
FactLine factLine = null;
BigDecimal total = Env.ZERO;
DocLine parentProductionLine = null;
for (DocLine line : p_lines) {
MProduct product = line.getProduct();
if (line.isProductionBOM())
parentProductionLine = line;
if (X_M_Product.PRODUCTTYPE_Item.equals(product.getProductType())) {
BigDecimal totalCosts = Env.ZERO;
BigDecimal qty = line.getQty();
BigDecimal costTransaction = Env.ZERO;
for (MCostDetail cost : line.getCostDetail(as, false)) {
if (!MCostDetail.existsCost(cost))
continue;
costTransaction = MCostDetail.getTotalCost(cost, as);
totalCosts = totalCosts.add(costTransaction);
}
if (qty.signum() < 0)
totalCosts = totalCosts.negate();
total = total.add(totalCosts);
factLine = fact.createLine(line, line.getAccount(ProductCost.ACCTTYPE_P_Asset, as), line.getAccount(ProductCost.ACCTTYPE_P_Asset, as), as.getC_Currency_ID(), totalCosts);
factLine.setM_Product_ID(line.getM_Product_ID());
factLine.setM_Locator_ID(line.getM_LocatorTo_ID());
factLine.setDescription("");
factLine.saveEx();
} else if (!X_M_Product.PRODUCTTYPE_Item.equals(product.getProductType())) {
BigDecimal totalCosts = Env.ZERO;
MAccount acct = null;
for (MCostElement costElement : MCostElement.getCostElement(line.getCtx(), line.getTrxName())) {
if (MCostElement.COSTELEMENTTYPE_BurdenMOverhead.equals(costElement.getCostElementType()))
acct = line.getAccount(ProductCost.ACCTTYPE_P_Burden, as);
else if (MCostElement.COSTELEMENTTYPE_OutsideProcessing.equals(costElement.getCostElementType()))
acct = line.getAccount(ProductCost.ACCTTYPE_P_OutsideProcessing, as);
else if (MCostElement.COSTELEMENTTYPE_Overhead.equals(costElement.getCostElementType()))
acct = line.getAccount(ProductCost.ACCTTYPE_P_Overhead, as);
else if (MCostElement.COSTELEMENTTYPE_Resource.equals(costElement.getCostElementType()))
acct = line.getAccount(ProductCost.ACCTTYPE_P_Labor, as);
else
acct = line.getAccount(ProductCost.ACCTTYPE_P_OutsideProcessing, as);
int warehouseId = DB.getSQLValue(line.getTrxName(), "SELECT M_Warehouse_ID from M_Locator WHERE M_Locator_ID=?", line.getM_Locator_ID());
BigDecimal costTransaction = Env.ZERO;
MCost costDimension = MCost.validateCostForCostType(as, (MCostType) as.getM_CostType(), costElement, product.getM_Product_ID(), line.getAD_Org_ID(), warehouseId, line.getM_AttributeSetInstance_ID(), line.getTrxName());
if (costDimension != null)
costTransaction = costDimension.getCurrentCostPrice().add(costDimension.getCurrentCostPriceLL());
totalCosts = totalCosts.add(costTransaction.multiply(line.getQty()));
}
factLine = fact.createLine(line, acct, acct, as.getC_Currency_ID(), totalCosts);
factLine.setM_Product_ID(line.getM_Product_ID());
factLine.setM_Locator_ID(line.getM_LocatorTo_ID());
if (m_DocStatus.equals(MProduction.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
// Set AmtAcctDr from Original Phys.Inventory
if (!factLine.updateReverseLine(MProduction.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), line.getQty(), Env.ONE)) {
p_Error = "Original Physical Inventory not posted yet";
return null;
}
}
factLine.setDescription("");
factLine.saveEx();
total = total.add(totalCosts);
}
}
// When total no is zero then create an adjustment cost , can happen when of resource rate was changes from original cost finish good
if (total.signum() != 0) {
factLine = fact.createLine(parentProductionLine, parentProductionLine.getAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as), as.getC_Currency_ID(), total);
factLine.setM_Product_ID(parentProductionLine.getM_Product_ID());
factLine.setM_Locator_ID(parentProductionLine.getM_LocatorTo_ID());
factLine.setDescription(" Adjustment Cost");
factLine.saveEx();
}
ArrayList<Fact> facts = new ArrayList<Fact>();
facts.add(fact);
return facts;
}
use of org.compiere.model.MCostElement in project adempiere by adempiere.
the class Doc_MatchPO method createFacts.
// getBalance
/**
* Create Facts (the accounting logic) for
* MXP.
* <pre>
* Product PPV <difference>
* PPV_Offset <difference>
* </pre>
* @param as accounting schema
* @return Fact
*/
public ArrayList<Fact> createFacts(MAcctSchema as) {
ArrayList<Fact> facts = new ArrayList<Fact>();
//
if (// Nothing to do if no Product
getM_Product_ID() == 0 || getQty().signum() == 0 || // No posting if not matched to Shipment
m_M_InOutLine_ID == 0) {
log.fine("No Product/Qty - M_Product_ID=" + getM_Product_ID() + ",Qty=" + getQty());
return facts;
}
// create Fact Header
Fact fact = new Fact(this, as, Fact.POST_Actual);
setC_Currency_ID(as.getC_Currency_ID());
boolean isInterOrg = isInterOrg(as);
// Purchase Order Line
BigDecimal poCost = m_oLine.getPriceCost();
if (poCost == null || poCost.signum() == 0)
poCost = m_oLine.getPriceActual();
MInOutLine receiptLine = new MInOutLine(getCtx(), m_M_InOutLine_ID, getTrxName());
MInOut inOut = receiptLine.getParent();
boolean isReturnTrx = inOut.getMovementType().equals(X_M_InOut.MOVEMENTTYPE_VendorReturns);
// calculate po cost
// Delivered so far
poCost = poCost.multiply(getQty());
// Different currency
if (m_oLine.getC_Currency_ID() != as.getC_Currency_ID()) {
MOrder order = m_oLine.getParent();
Timestamp dateAcct = order.getDateAcct();
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) {
p_Error = "Purchase Order not convertible - " + as.getName();
return null;
}
poCost = poCost.multiply(rate);
if (poCost.scale() > as.getCostingPrecision())
poCost = poCost.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
}
// Calculate PPV for standard costing
//get standard cost and also make sure cost for other costing method is updated
MProduct product = MProduct.get(getCtx(), getM_Product_ID());
MCostType ct = MCostType.get(as, getM_Product_ID(), getAD_Org_ID());
String costingMethod = ct.getCostingMethod();
MInOutLine ioLine = new MInOutLine(getCtx(), m_M_InOutLine_ID, getTrxName());
BigDecimal costs = Env.ZERO;
for (MTransaction transaction : MTransaction.getByInOutLine(ioLine)) {
MCostElement element = MCostElement.getByMaterialCostElementType(transaction);
MCostDetail cd = MCostDetail.getByTransaction(ioLine, transaction, as.getC_AcctSchema_ID(), ct.getM_CostType_ID(), element.getM_CostElement_ID());
if (cd != null)
costs = costs.add(MCostDetail.getTotalCost(cd, as));
}
if (MCostType.COSTINGMETHOD_StandardCosting.equals(costingMethod)) {
// No Costs yet - no PPV
if (costs == null || costs.signum() == 0) {
// TODO make these a translatable message
p_Error = "No Standard Cost information was found for " + product.getName() + ". Please add a standard cost for material and repost this document.";
log.log(Level.SEVERE, p_Error);
return null;
}
// Difference
BigDecimal difference = poCost.subtract(costs.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP));
// Nothing to post
if (difference.signum() == 0) {
log.log(Level.FINE, "No Cost Difference for M_Product_ID=" + getM_Product_ID());
return facts;
}
// Product PPV
FactLine cr = fact.createLine(null, m_pc.getAccount(ProductCost.ACCTTYPE_P_PPV, as), as.getC_Currency_ID(), isReturnTrx ? difference.negate() : difference);
if (cr != null) {
cr.setQty(isReturnTrx ? getQty().negate() : getQty());
cr.setC_BPartner_ID(m_oLine.getC_BPartner_ID());
cr.setC_Activity_ID(m_oLine.getC_Activity_ID());
cr.setC_Campaign_ID(m_oLine.getC_Campaign_ID());
cr.setC_Project_ID(m_oLine.getC_Project_ID());
cr.setC_ProjectPhase_ID(m_oLine.getC_ProjectPhase_ID());
cr.setC_ProjectTask_ID(m_oLine.getC_ProjectTask_ID());
cr.setC_UOM_ID(m_oLine.getC_UOM_ID());
cr.setUser1_ID(m_oLine.getUser1_ID());
cr.setUser2_ID(m_oLine.getUser2_ID());
cr.setUser3_ID(m_oLine.getUser3_ID());
cr.setUser4_ID(m_oLine.getUser4_ID());
}
// PPV Offset
FactLine dr = fact.createLine(null, getAccount(Doc.ACCTTYPE_PPVOffset, as), as.getC_Currency_ID(), isReturnTrx ? difference : difference.negate());
if (dr != null) {
dr.setQty(isReturnTrx ? getQty() : getQty().negate());
dr.setC_BPartner_ID(m_oLine.getC_BPartner_ID());
dr.setC_Activity_ID(m_oLine.getC_Activity_ID());
dr.setC_Campaign_ID(m_oLine.getC_Campaign_ID());
dr.setC_Project_ID(m_oLine.getC_Project_ID());
dr.setC_ProjectPhase_ID(m_oLine.getC_ProjectPhase_ID());
dr.setC_ProjectTask_ID(m_oLine.getC_ProjectTask_ID());
dr.setC_UOM_ID(m_oLine.getC_UOM_ID());
dr.setUser1_ID(m_oLine.getUser1_ID());
dr.setUser2_ID(m_oLine.getUser2_ID());
dr.setUser3_ID(m_oLine.getUser3_ID());
dr.setUser4_ID(m_oLine.getUser4_ID());
}
// Avoid usage of clearing accounts
// If both accounts Purchase Price Variance and Purchase Price Variance Offset are equal
// then remove the posting
// PPV
MAccount acct_db = dr.getAccount();
// PPV Offset
MAccount acct_cr = cr.getAccount();
if ((!as.isPostIfClearingEqual()) && acct_db.equals(acct_cr) && (!isInterOrg)) {
BigDecimal debit = dr.getAmtSourceDr();
BigDecimal credit = cr.getAmtSourceCr();
if (debit.compareTo(credit) == 0) {
fact.remove(dr);
fact.remove(cr);
}
}
// End Avoid usage of clearing accounts
//
facts.add(fact);
return facts;
} else {
return facts;
}
}
use of org.compiere.model.MCostElement in project adempiere by adempiere.
the class GenerateCostDetail method generateCostDetail.
public void generateCostDetail() {
//Generate Costdetail
KeyNamePair[] transactions = getTransactionIdsByDateAcct();
// System.out.println("Transaction to process : " + transactions.length);
Integer process = 0;
Integer productId = 0;
boolean processNewProduct = true;
Trx dbTransaction = null;
try {
//Process transaction
for (KeyNamePair keyNamePair : transactions) {
int transactionId = keyNamePair.getKey();
int transactionProductId = new Integer(keyNamePair.getName());
//Detected a new product
if (productId != transactionProductId) {
//commit last transaction by product
if (dbTransaction != null) {
dbTransaction.commit(true);
dbTransaction.close();
}
productId = transactionProductId;
processNewProduct = true;
//Create new transaction for this product
dbTransaction = Trx.get(productId.toString(), true);
}
MTransaction transaction = new MTransaction(getCtx(), transactionId, dbTransaction.getTrxName());
// for each Account Schema
for (MAcctSchema accountSchema : acctSchemas) {
// for each Cost Type
for (MCostType costType : costTypes) {
// for each Cost Element
for (MCostElement costElement : costElements) {
if (processNewProduct) {
applyCriteria(accountSchema.getC_AcctSchema_ID(), costType.getM_CostType_ID(), costElement.getM_CostElement_ID(), productId, getAccountDate(), getAccountDateTo());
deleteCostDetail(dbTransaction.getTrxName());
resetCostDimension(costType.getCostingMethod(), dbTransaction.getTrxName());
generateCostCollectorNotTransaction(accountSchema, costType, productId, dbTransaction.getTrxName());
processNewProduct = false;
}
generateCostDetail(accountSchema, costType, costElement, transaction);
}
}
}
process++;
//System.out.println("Transaction : " + transactionId + " Transaction Type :"+ transaction.getMovementType() + " record ..." + process);
}
if (dbTransaction != null) {
dbTransaction.commit(true);
dbTransaction.close();
dbTransaction = null;
}
} catch (Exception e) {
if (dbTransaction != null) {
dbTransaction.rollback();
dbTransaction.close();
dbTransaction = null;
e.printStackTrace();
addLog(e.getMessage());
}
} finally {
if (dbTransaction != null) {
dbTransaction.commit();
dbTransaction.close();
dbTransaction = null;
}
}
}
Aggregations