Search in sources :

Example 1 with MProductionLine

use of org.compiere.model.MProductionLine in project adempiere by adempiere.

the class CostEngine method createCostDetail.

/**
	 * Create Cost Detail
	 * @param accountSchema Account Schema
	 * @param transaction Material Transaction
	 * @param model IDocumentLine
	 * @param costType Cost Type
	 * @param costElement Cost Element
	 */
public void createCostDetail(MAcctSchema accountSchema, MCostType costType, MCostElement costElement, MTransaction transaction, IDocumentLine model, boolean force) {
    if (!force)
        return;
    BigDecimal costThisLevel = Env.ZERO;
    BigDecimal costLowLevel = Env.ZERO;
    String costingLevel = MProduct.get(transaction.getCtx(), transaction.getM_Product_ID()).getCostingLevel(accountSchema, transaction.getAD_Org_ID());
    // adjustment for Average PO Costing method
    if (model instanceof MMatchInv && MCostType.COSTINGMETHOD_AveragePO.equals(costType.getCostingMethod()))
        return;
    // adjustment for Average PO Costing method
    if (model instanceof MMatchPO && MCostType.COSTINGMETHOD_AverageInvoice.equals(costType.getCostingMethod()))
        return;
    if (model instanceof MLandedCostAllocation) {
        MLandedCostAllocation allocation = (MLandedCostAllocation) model;
        costThisLevel = convertCostToSchemaCurrency(accountSchema, model, model.getPriceActualCurrency());
    }
    MCost cost = MCost.validateCostForCostType(accountSchema, costType, costElement, transaction.getM_Product_ID(), transaction.getAD_Org_ID(), transaction.getM_Warehouse_ID(), transaction.getM_AttributeSetInstance_ID(), transaction.get_TrxName());
    // get the cost for positive transaction
    if ((MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType()) || MCostElement.COSTELEMENTTYPE_LandedCost.equals(costElement.getCostElementType())) && transaction.getMovementType().contains("+") && !MCostType.COSTINGMETHOD_StandardCosting.equals(costType.getCostingMethod())) {
        if (model instanceof MMovementLine || model instanceof MInventoryLine || (model instanceof MInOutLine && MTransaction.MOVEMENTTYPE_CustomerReturns.equals(transaction.getMovementType()))) {
            costThisLevel = getCostThisLevel(accountSchema, costType, costElement, transaction, model, costingLevel);
            if (model instanceof MInventoryLine) {
                MInventoryLine inventoryLine = (MInventoryLine) model;
                // try get cost from physical inventory
                if (costThisLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
                    // Use the current cost only for Physical Inventory
                    if (inventoryLine.getQtyInternalUse().signum() == 0 && inventoryLine.getCurrentCostPrice() != null && inventoryLine.getCurrentCostPrice().signum() > 0) {
                        costThisLevel = convertCostToSchemaCurrency(accountSchema, model, model.getPriceActualCurrency());
                    }
                    if (costThisLevel.signum() == 0)
                        costThisLevel = getCostThisLevel(accountSchema, costType, costElement, transaction, model, costingLevel);
                }
                costLowLevel = getCostLowLevel(accountSchema, costType, costElement, transaction, model, costingLevel);
                // try get cost low level from physical inventory
                if (costLowLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
                    // Use the current cost only for Physical Inventory
                    if (inventoryLine.getQtyInternalUse().signum() == 0 && inventoryLine.getCurrentCostPriceLL() != null && inventoryLine.getCurrentCostPriceLL().signum() > 0) {
                        costLowLevel = convertCostToSchemaCurrency(accountSchema, model, inventoryLine.getCurrentCostPriceLL());
                    }
                    if (costLowLevel.signum() == 0)
                        costLowLevel = getCostLowLevel(accountSchema, costType, costElement, transaction, model, costingLevel);
                }
            }
            //Get cost from movement from if it > that zero replace cost This Level
            if (model instanceof MMovementLine) {
                MTransaction transactionFrom = MTransaction.getByDocumentLine(model, MTransaction.MOVEMENTTYPE_MovementFrom);
                BigDecimal costMovementFrom = getCostThisLevel(accountSchema, costType, costElement, transactionFrom == null ? transaction : transactionFrom, model, costingLevel);
                if (costMovementFrom.signum() > 0)
                    costThisLevel = costMovementFrom;
                BigDecimal costMovementFromLL = getCostLowLevel(accountSchema, costType, costElement, transactionFrom == null ? transaction : transactionFrom, model, costingLevel);
                if (costMovementFromLL.signum() > 0)
                    costLowLevel = costMovementFromLL;
            }
        } else if (MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
            costThisLevel = convertCostToSchemaCurrency(accountSchema, model, model.getPriceActualCurrency());
        }
    }
    if (!MCostType.COSTINGMETHOD_StandardCosting.equals(costType.getCostingMethod())) {
        if (model instanceof MPPCostCollector) {
            MPPCostCollector costCollector = (MPPCostCollector) model;
            if (MPPCostCollector.COSTCOLLECTORTYPE_MaterialReceipt.equals(costCollector.getCostCollectorType())) {
                // get Actual Cost for Cost Type and Cost Element
                costThisLevel = getCostThisLevel(accountSchema, costType, costElement, transaction, model, costingLevel);
                costLowLevel = CostEngine.getParentActualCostByCostType(accountSchema, costType.getM_CostType_ID(), costElement.getM_CostElement_ID(), costCollector);
            }
        }
        if (model instanceof MProductionLine) {
            MProductionLine productionLine = (MProductionLine) model;
            if (productionLine.isParent())
                costThisLevel = CostEngine.getParentActualCostByCostType(accountSchema, costType, costElement, productionLine.getM_Production());
            if (costThisLevel.signum() == 0)
                costThisLevel = cost.getCurrentCostPrice();
            if (costThisLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType()))
                costThisLevel = getSeedCost(transaction.getCtx(), transaction.getM_Product_ID(), transaction.get_TrxName());
            // Material Receipt for Production light
            if (productionLine.isParent()) {
                // if the product is purchase then no use low level 
                if (!productionLine.getM_Product().isPurchased()) {
                    costLowLevel = costThisLevel;
                    costThisLevel = Env.ZERO;
                }
            } else if (productionLine.getMovementQty().signum() < 0)
                costLowLevel = Env.ZERO;
        }
    } else if (MCostType.COSTINGMETHOD_StandardCosting.equals(costType.getCostingMethod())) {
        costThisLevel = cost.getCurrentCostPrice();
        costLowLevel = cost.getCurrentCostPriceLL();
        //Define Cost Inventory Line
        if (model instanceof MInventoryLine) {
            MInventoryLine inventoryLine = (MInventoryLine) model;
            //Define Current Cost Level
            if (costThisLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
                // Use the current cost only for Physical Inventory
                if (inventoryLine.getQtyInternalUse().signum() == 0 && inventoryLine.getCurrentCostPrice() != null && inventoryLine.getCurrentCostPrice().signum() > 0) {
                    costThisLevel = convertCostToSchemaCurrency(accountSchema, model, model.getPriceActualCurrency());
                    cost.setCurrentCostPrice(costThisLevel);
                    cost.saveEx();
                }
                if (costThisLevel.signum() == 0)
                    costThisLevel = getCostThisLevel(accountSchema, costType, costElement, transaction, model, costingLevel);
            }
            //Define Current Cost Low Level
            if (costLowLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
                // Use the cost only for Physical Inventory
                if (inventoryLine.getQtyInternalUse().signum() == 0 && inventoryLine.getCurrentCostPriceLL() != null && inventoryLine.getCurrentCostPriceLL().signum() > 0) {
                    costLowLevel = convertCostToSchemaCurrency(accountSchema, model, inventoryLine.getCurrentCostPriceLL());
                    cost.setCurrentCostPriceLL(costLowLevel);
                    cost.saveEx();
                }
                if (costLowLevel.signum() == 0)
                    costLowLevel = getCostLowLevel(accountSchema, costType, costElement, transaction, model, costingLevel);
            }
        }
        if (model instanceof MMovementLine) {
            MTransaction transactionFrom = MTransaction.getByDocumentLine(model, MTransaction.MOVEMENTTYPE_MovementFrom);
            BigDecimal costMovementFrom = getCostThisLevel(accountSchema, costType, costElement, transactionFrom == null ? transaction : transactionFrom, model, costingLevel);
            if (costThisLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
                if (costMovementFrom.signum() > 0)
                    costThisLevel = costMovementFrom;
            }
            if (costLowLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
                BigDecimal costMovementFromLL = getCostLowLevel(accountSchema, costType, costElement, transactionFrom == null ? transaction : transactionFrom, model, costingLevel);
                if (costMovementFromLL.signum() > 0)
                    costLowLevel = costMovementFromLL;
            }
        }
        if (costThisLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
            costThisLevel = getSeedCost(transaction.getCtx(), transaction.getM_Product_ID(), transaction.get_TrxName());
            if (costThisLevel.signum() == 0)
                if (model instanceof MInOutLine && !model.isSOTrx()) {
                    costThisLevel = convertCostToSchemaCurrency(accountSchema, model, model.getPriceActualCurrency());
                }
            if (costThisLevel.signum() != 0) {
                cost.setCurrentCostPrice(costThisLevel);
                cost.saveEx();
            }
        }
        if (costLowLevel.signum() != 0) {
            cost.setCurrentCostPriceLL(costLowLevel);
            cost.saveEx();
        }
    }
    final ICostingMethod method = CostingMethodFactory.get().getCostingMethod(costType.getCostingMethod());
    method.setCostingMethod(accountSchema, transaction, model, cost, costThisLevel, costLowLevel, model.isSOTrx());
    method.process();
}
Also used : MLandedCostAllocation(org.compiere.model.MLandedCostAllocation) MMatchPO(org.compiere.model.MMatchPO) MInventoryLine(org.compiere.model.MInventoryLine) MInOutLine(org.compiere.model.MInOutLine) MCost(org.compiere.model.MCost) MProductionLine(org.compiere.model.MProductionLine) MTransaction(org.compiere.model.MTransaction) BigDecimal(java.math.BigDecimal) MMatchInv(org.compiere.model.MMatchInv) MPPCostCollector(org.eevolution.model.MPPCostCollector) MMovementLine(org.compiere.model.MMovementLine)

Example 2 with MProductionLine

use of org.compiere.model.MProductionLine in project adempiere by adempiere.

the class CostEngine method getParentActualCostByCostType.

public static BigDecimal getParentActualCostByCostType(MAcctSchema accountSchema, MCostType costType, MCostElement costElement, I_M_Production production) {
    //	Get BOM Cost - Sum of individual lines
    BigDecimal totalCost = Env.ZERO;
    for (MProductionLine productionLine : ((MProduction) production).getLines()) {
        if (productionLine.isParent())
            continue;
        String productType = productionLine.getM_Product().getProductType();
        BigDecimal cost = BigDecimal.ZERO;
        if (X_M_Product.PRODUCTTYPE_Item.equals(productType)) {
            cost = MCostDetail.getCostByModel(accountSchema.getC_AcctSchema_ID(), costType.getM_CostType_ID(), costElement.getM_CostElement_ID(), productionLine);
        } else if (X_M_Product.PRODUCTTYPE_Resource.equals(productType)) {
            MCost costDimension = MCost.validateCostForCostType(accountSchema, costType, costElement, productionLine.getM_Product_ID(), productionLine.getAD_Org_ID(), productionLine.getM_Locator().getM_Warehouse_ID(), productionLine.getM_AttributeSetInstance_ID(), productionLine.get_TrxName());
            if (costDimension != null && costDimension.getCurrentCostPrice().signum() != 0)
                cost = costDimension.getCurrentCostPrice().multiply(productionLine.getMovementQty().negate());
        }
        if (cost != null && cost.signum() != 0)
            totalCost = totalCost.add(cost);
    }
    BigDecimal unitCost = Env.ZERO;
    if (production.getProductionQty().signum() != 0 && totalCost.signum() != 0)
        unitCost = totalCost.divide(production.getProductionQty(), accountSchema.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
    return unitCost;
}
Also used : MProductionLine(org.compiere.model.MProductionLine) MCost(org.compiere.model.MCost) BigDecimal(java.math.BigDecimal) MProduction(org.compiere.model.MProduction)

Example 3 with MProductionLine

use of org.compiere.model.MProductionLine in project adempiere by adempiere.

the class CostEngine method clearAccounting.

/**
     * clear Accounting
     * @param accountSchema
     * @param transaction
     */
public void clearAccounting(MAcctSchema accountSchema, MTransaction transaction) {
    if (transaction.getM_InOutLine_ID() > 0) {
        MInOutLine line = (MInOutLine) transaction.getM_InOutLine();
        if (!clearAccounting(accountSchema, accountSchema.getM_CostType(), line.getParent(), transaction.getM_Product_ID(), line.getDateAcct()))
            return;
        // get Purchase matches
        List<MMatchPO> orderMatches = MMatchPO.getInOutLine(line);
        for (MMatchPO match : orderMatches) {
            if (!clearAccounting(accountSchema, accountSchema.getM_CostType(), match, transaction.getM_Product_ID(), line.getDateAcct()))
                return;
        }
        // get invoice matches
        List<MMatchInv> invoiceMatches = MMatchInv.getInOutLine(line);
        for (MMatchInv match : invoiceMatches) {
            if (!clearAccounting(accountSchema, accountSchema.getM_CostType(), match, transaction.getM_Product_ID(), line.getDateAcct()))
                return;
        }
    } else if (transaction.getC_ProjectIssue_ID() > 0) {
        MProjectIssue line = (MProjectIssue) transaction.getC_ProjectIssue();
        if (!clearAccounting(accountSchema, accountSchema.getM_CostType(), line.getParent(), transaction.getM_Product_ID(), line.getMovementDate()))
            return;
    } else if (transaction.getM_InventoryLine_ID() > 0) {
        MInventoryLine line = (MInventoryLine) transaction.getM_InventoryLine();
        if (!clearAccounting(accountSchema, accountSchema.getM_CostType(), line.getParent(), transaction.getM_Product_ID(), line.getDateAcct()))
            return;
    } else if (transaction.getM_MovementLine_ID() > 0) {
        MMovementLine line = (MMovementLine) transaction.getM_MovementLine();
        if (!clearAccounting(accountSchema, accountSchema.getM_CostType(), line.getParent(), transaction.getM_Product_ID(), line.getDateAcct()))
            return;
    } else if (transaction.getM_ProductionLine_ID() > 0) {
        MProductionLine line = (MProductionLine) transaction.getM_ProductionLine();
        MProduction production = (MProduction) line.getM_ProductionPlan().getM_Production();
        if (!clearAccounting(accountSchema, accountSchema.getM_CostType(), production, transaction.getM_Product_ID(), production.getMovementDate()))
            return;
    } else if (transaction.getPP_Cost_Collector_ID() > 0) {
        MPPCostCollector costCollector = (MPPCostCollector) transaction.getPP_Cost_Collector();
        if (!clearAccounting(accountSchema, accountSchema.getM_CostType(), costCollector, costCollector.getM_Product_ID(), costCollector.getDateAcct()))
            ;
        return;
    } else {
        log.info("Document does not exist :" + transaction);
    }
}
Also used : MProjectIssue(org.compiere.model.MProjectIssue) MInOutLine(org.compiere.model.MInOutLine) MMatchPO(org.compiere.model.MMatchPO) MInventoryLine(org.compiere.model.MInventoryLine) MProductionLine(org.compiere.model.MProductionLine) MMatchInv(org.compiere.model.MMatchInv) MPPCostCollector(org.eevolution.model.MPPCostCollector) MMovementLine(org.compiere.model.MMovementLine) MProduction(org.compiere.model.MProduction)

Aggregations

MProductionLine (org.compiere.model.MProductionLine)3 BigDecimal (java.math.BigDecimal)2 MCost (org.compiere.model.MCost)2 MInOutLine (org.compiere.model.MInOutLine)2 MInventoryLine (org.compiere.model.MInventoryLine)2 MMatchInv (org.compiere.model.MMatchInv)2 MMatchPO (org.compiere.model.MMatchPO)2 MMovementLine (org.compiere.model.MMovementLine)2 MProduction (org.compiere.model.MProduction)2 MPPCostCollector (org.eevolution.model.MPPCostCollector)2 MLandedCostAllocation (org.compiere.model.MLandedCostAllocation)1 MProjectIssue (org.compiere.model.MProjectIssue)1 MTransaction (org.compiere.model.MTransaction)1