use of org.compiere.model.MCostElement in project adempiere by adempiere.
the class CostEngine method createCostDetailForLandedCostAllocation.
//Create cost detail for by document
public void createCostDetailForLandedCostAllocation(MLandedCostAllocation allocation) {
MInOutLine ioLine = (MInOutLine) allocation.getM_InOutLine();
for (MTransaction transaction : MTransaction.getByInOutLine(ioLine)) {
for (MAcctSchema accountSchema : MAcctSchema.getClientAcctSchema(allocation.getCtx(), allocation.getAD_Client_ID())) {
List<MCostType> costTypes = MCostType.get(allocation.getCtx(), allocation.get_TrxName());
for (MCostType costType : costTypes) {
MCostElement costElement = (MCostElement) allocation.getM_CostElement();
CostEngineFactory.getCostEngine(allocation.getAD_Client_ID()).createCostDetail(accountSchema, costType, costElement, transaction, allocation, true);
}
}
}
}
use of org.compiere.model.MCostElement in project adempiere by adempiere.
the class CostEngine method createCostDetail.
/**
* Generate by transaction
* @param transaction
*/
public void createCostDetail(MTransaction transaction, IDocumentLine model) {
MClient client = new MClient(transaction.getCtx(), transaction.getAD_Client_ID(), transaction.get_TrxName());
StringBuilder description = new StringBuilder();
if (model != null && model.getDescription() != null && !Util.isEmpty(model.getDescription(), true))
description.append(model.getDescription());
if (model != null) {
description.append(model.isSOTrx() ? "(|->)" : "(|<-)");
}
List<MAcctSchema> acctSchemas = new ArrayList(Arrays.asList(MAcctSchema.getClientAcctSchema(transaction.getCtx(), transaction.getAD_Client_ID(), transaction.get_TrxName())));
List<MCostElement> costElements = MCostElement.getCostElement(transaction.getCtx(), transaction.get_TrxName());
List<MCostType> costTypes = MCostType.get(transaction.getCtx(), transaction.get_TrxName());
for (MAcctSchema accountSchema : acctSchemas) {
for (MCostType costType : costTypes) {
if (!costType.isActive())
continue;
for (MCostElement costElement : costElements) {
createCostDetail(accountSchema, costType, costElement, transaction, model, client.isCostImmediate());
}
}
}
}
use of org.compiere.model.MCostElement in project adempiere by adempiere.
the class AveragePOCostingMethod method adjustCostDetail.
/**
* Recalculate Cost Detail
* @param costDetail
*/
public void adjustCostDetail(MCostDetail costDetail) {
Properties ctx = costDetail.getCtx();
String trxName = costDetail.get_TrxName();
int transactionId = costDetail.getM_Transaction_ID();
int clientId = costDetail.getAD_Client_ID();
MTransaction transaction = new MTransaction(ctx, transactionId, trxName);
MCostType costType = (MCostType) costDetail.getM_CostType();
MCostElement costElement = (MCostElement) costDetail.getM_CostElement();
MAcctSchema accountSchema = (MAcctSchema) costDetail.getC_AcctSchema();
CostEngineFactory.getCostEngine(accountSchema.getAD_Client_ID()).clearAccounting(accountSchema, transaction);
if (MTransaction.MOVEMENTTYPE_VendorReceipts.equals(transaction.getMovementType())) {
MInOutLine line = (MInOutLine) transaction.getDocumentLine();
if (MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
if (costDetail.getM_InOutLine_ID() > 0 && costDetail.getQty().signum() != 0) {
CostEngineFactory.getCostEngine(clientId).createCostDetail(accountSchema, costType, costElement, transaction, line, true);
} else if (costDetail.getM_InOutLine_ID() > 0 && costDetail.getQty().signum() != 0 && costDetail.getC_OrderLine_ID() > 0) {
List<MMatchPO> orderMatches = MMatchPO.getInOutLine(line);
for (MMatchPO match : orderMatches) {
if (match.getM_InOutLine_ID() == line.getM_InOutLine_ID() && match.getM_Product_ID() == transaction.getM_Product_ID()) {
CostEngineFactory.getCostEngine(clientId).createCostDetail(accountSchema, costType, costElement, transaction, match, true);
}
}
} else if (costDetail.getM_InOutLine_ID() > 0 && costDetail.getQty().signum() == 0 && costDetail.getC_OrderLine_ID() > 0) {
List<MMatchPO> poMatches = MMatchPO.getInOutLine(line);
for (MMatchPO match : poMatches) {
if (match.getM_Product_ID() == transaction.getM_Product_ID()) {
CostEngineFactory.getCostEngine(clientId).createCostDetail(accountSchema, costType, costElement, transaction, match, true);
}
}
}
}
for (MLandedCostAllocation allocation : MLandedCostAllocation.getOfInOutline(line, costElement.getM_CostElement_ID())) {
CostEngineFactory.getCostEngine(clientId).createCostDetail(accountSchema, costType, costElement, transaction, allocation, true);
}
} else
CostEngineFactory.getCostEngine(clientId).createCostDetail(accountSchema, costType, costElement, transaction, transaction.getDocumentLine(), true);
}
use of org.compiere.model.MCostElement in project adempiere by adempiere.
the class RollupWorkflow method doIt.
// prepare
protected String doIt() throws Exception {
//Get account schema
MAcctSchema accountSchema = MAcctSchema.get(getCtx(), getAccountingSchemaId());
//Get cost type
MCostType costType = MCostType.get(getCtx(), getCostTypeId());
//Get cost element to process
final List<MCostElement> costElements = getCostElementId() > 0 ? Arrays.asList(MCostElement.get(getCtx(), getCostElementId())) : MCostElement.getCostElement(getCtx(), get_TrxName());
routingService = RoutingServiceFactory.get().getRoutingService(getAD_Client_ID());
//Iterate product ids based on parameters
Arrays.stream(getProductIds()).filter(productId -> productId > 0).forEach(productId -> {
MProduct product = MProduct.get(getCtx(), productId);
log.info("Product: " + product);
int workflowId = 0;
MPPProductPlanning productPlanning = null;
if (workflowId <= 0)
workflowId = MWorkflow.getWorkflowSearchKey(product);
if (workflowId <= 0) {
productPlanning = MPPProductPlanning.find(getCtx(), getOrganizationId(), getWarehouseId(), getResourcePlantId(), product.get_ID(), get_TrxName());
if (productPlanning != null)
workflowId = productPlanning.getAD_Workflow_ID();
else
createNotice(product, "@NotFound@ @PP_Product_Planning_ID@");
}
if (workflowId <= 0)
createNotice(product, "@NotFound@ @AD_Workflow_ID@");
else {
Trx.run(new TrxRunnable() {
MAcctSchema accountSchema;
MCostType costType;
MProduct product;
MPPProductPlanning productPlanning;
int workflowId;
public TrxRunnable setParameters(MAcctSchema accountSchema, MCostType costType, MProduct product, MPPProductPlanning productPlanning, int workflowId) {
this.accountSchema = accountSchema;
this.costType = costType;
this.product = product;
this.productPlanning = productPlanning;
this.workflowId = workflowId;
return this;
}
public void run(String trxName) {
MWorkflow workflow = new MWorkflow(getCtx(), workflowId, trxName);
costElements.stream().filter(costElement -> costElement != null && CostEngine.isActivityControlElement(costElement)).forEach(costElement -> {
rollup(accountSchema, costType, costElement, product, workflow, trxName);
});
if (productPlanning != null) {
productPlanning.load(trxName);
productPlanning.setYield(workflow.getYield());
productPlanning.saveEx();
}
}
}.setParameters(accountSchema, costType, product, productPlanning, workflowId));
}
});
return "@OK@";
}
use of org.compiere.model.MCostElement in project adempiere by adempiere.
the class RollupWorkflow method rollup.
/**
* Execute rollup process
* @param accountSchema
* @param costType
* @param costElement
* @param product
* @param workflow
* @param trxName
*/
protected void rollup(MAcctSchema accountSchema, MCostType costType, MCostElement costElement, MProduct product, MWorkflow workflow, String trxName) {
log.info("Workflow: " + workflow);
workflow.setCost(Env.ZERO);
double yield = 1;
int queuingTime = 0;
int setupTime = 0;
int duration = 0;
int waitingTime = 0;
int movingTime = 0;
int workingTime = 0;
MWFNode[] nodes = workflow.getNodes(false, getAD_Client_ID());
for (MWFNode node : nodes) {
node.setCost(Env.ZERO);
if (node.getYield() != 0) {
yield = yield * ((double) node.getYield() / 100);
}
// We use node.getDuration() instead of m_routingService.estimateWorkingTime(node) because
// this will be the minimum duration of this node. So even if the node have defined units/cycle
// we consider entire duration of the node.
long nodeDuration = node.getDuration();
queuingTime += node.getQueuingTime();
setupTime += node.getSetupTime();
duration += nodeDuration;
waitingTime += node.getWaitingTime();
movingTime += node.getMovingTime();
workingTime += node.getWorkingTime();
}
workflow.setCost(Env.ZERO);
workflow.setYield((int) (yield * 100));
workflow.setQueuingTime(queuingTime);
workflow.setSetupTime(setupTime);
workflow.setDuration(duration);
workflow.setWaitingTime(waitingTime);
workflow.setMovingTime(movingTime);
workflow.setWorkingTime(workingTime);
final CostDimension costDimension = new CostDimension(product, accountSchema, costType.getM_CostType_ID(), getOrganizationId(), getWarehouseId(), 0, costElement.get_ID());
MCost cost = MCost.getOrCreate(product, 0, accountSchema, getOrganizationId(), getWarehouseId(), costType.getM_CostType_ID(), costElement.getM_CostElement_ID());
cost.setFutureCostPrice(BigDecimal.ZERO);
if (!cost.isCostFrozen())
cost.setCurrentCostPrice(BigDecimal.ZERO);
AtomicReference<BigDecimal> segmentCost = new AtomicReference<>(Env.ZERO);
Arrays.stream(nodes).filter(node -> node != null).forEach(node -> {
final CostEngine costEngine = CostEngineFactory.getCostEngine(node.getAD_Client_ID());
final BigDecimal rate = StandardCostingMethod.getResourceActualCostRate(node.getS_Resource_ID(), costDimension, trxName);
final BigDecimal baseValue = routingService.getResourceBaseValue(node.getS_Resource_ID(), node);
BigDecimal nodeCostPrecision = baseValue.multiply(rate);
BigDecimal nodeCost;
if (nodeCostPrecision.scale() > accountSchema.getCostingPrecision())
nodeCost = nodeCostPrecision.setScale(accountSchema.getCostingPrecision(), RoundingMode.HALF_UP);
else
nodeCost = nodeCostPrecision;
segmentCost.updateAndGet(costAmt -> costAmt.add(nodeCost));
log.info(Msg.parseTranslation(getCtx(), " @M_CostElement_ID@ : ") + costElement.getName() + ", Node=" + node + ", BaseValue=" + baseValue + ", rate=" + rate + ", nodeCost=" + nodeCost + " => Cost=" + segmentCost);
node.setCost(node.getCost().add(nodeCost));
});
cost.setFutureCostPrice(segmentCost.get());
if (!cost.isCostFrozen())
cost.setCurrentCostPrice(segmentCost.get());
cost.saveEx();
// Update Workflow cost
workflow.setCost(workflow.getCost().add(segmentCost.get()));
// Save Workflow & Nodes
Arrays.stream(nodes).filter(node -> node != null).forEach(node -> node.saveEx());
workflow.saveEx();
log.info("Product: " + product.getName() + " WFCost: " + workflow.getCost());
}
Aggregations