Search in sources :

Example 1 with MLandedCostAllocation

use of org.compiere.model.MLandedCostAllocation 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 MLandedCostAllocation

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

the class AverageInvoiceCostingMethod method updateAmountCost.

/**
	 * Update Cost Amt
	 */
public void updateAmountCost() {
    if (movementQuantity.signum() > 0) {
        costDetail.setCostAmt(costDetail.getAmt().subtract(costDetail.getCostAdjustment()));
        costDetail.setCostAmtLL(costDetail.getAmtLL().subtract(costDetail.getCostAdjustmentLL()));
    } else if (movementQuantity.signum() < 0) {
        costDetail.setCostAmt(costDetail.getAmt().add(adjustCost));
        costDetail.setCostAmtLL(costDetail.getAmtLL().add(adjustCostLowerLevel));
    }
    costDetail.setCumulatedQty(getNewAccumulatedQuantity(lastCostDetail));
    costDetail.setCumulatedAmt(getNewAccumulatedAmount(lastCostDetail));
    costDetail.setCumulatedAmtLL(getNewAccumulatedAmountLowerLevel(lastCostDetail));
    costDetail.setCurrentCostPrice(currentCostPrice);
    costDetail.setCurrentCostPriceLL(currentCostPriceLowerLevel);
    // set the id for model
    final String idColumnName = CostEngine.getIDColumnName(model);
    costDetail.set_ValueOfColumn(idColumnName, CostEngine.getIDColumn(model));
    if (model instanceof MInOutLine) {
        MInOutLine ioLine = (MInOutLine) model;
        costDetail.setC_OrderLine_ID(ioLine.getC_OrderLine_ID());
        // IMPORTANT : reset possible provision purchase cost processed
        costDetail.setC_InvoiceLine_ID(0);
    }
    if (model instanceof MMatchInv && costDetail.getM_InOutLine_ID() == 0) {
        MMatchInv iMatch = (MMatchInv) model;
        costDetail.setM_InOutLine_ID(iMatch.getM_InOutLine_ID());
    }
    if (model instanceof MMatchPO && costDetail.getM_InOutLine_ID() == 0) {
        MMatchPO poMatch = (MMatchPO) model;
        costDetail.setM_InOutLine_ID(poMatch.getM_InOutLine_ID());
    }
    if (model instanceof MLandedCostAllocation) {
        MLandedCostAllocation allocation = (MLandedCostAllocation) model;
        costDetail.setM_InOutLine_ID(allocation.getM_InOutLine_ID());
        costDetail.setC_InvoiceLine_ID(allocation.getC_InvoiceLine_ID());
        costDetail.setC_LandedCostAllocation_ID(allocation.getC_LandedCostAllocation_ID());
        costDetail.setProcessed(false);
    }
    costDetail.saveEx();
}
Also used : MLandedCostAllocation(org.compiere.model.MLandedCostAllocation) MInOutLine(org.compiere.model.MInOutLine) MMatchPO(org.compiere.model.MMatchPO) MMatchInv(org.compiere.model.MMatchInv)

Example 3 with MLandedCostAllocation

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

the class AveragePOCostingMethod method updateAmountCost.

/**
	 * Update Cost Amt
	 */
public void updateAmountCost() {
    if (movementQuantity.signum() > 0) {
        costDetail.setCostAmt(costDetail.getAmt().subtract(costDetail.getCostAdjustment()));
        costDetail.setCostAmtLL(costDetail.getAmtLL().subtract(costDetail.getCostAdjustmentLL()));
    } else if (movementQuantity.signum() < 0) {
        costDetail.setCostAmt(costDetail.getAmt().add(adjustCost));
        costDetail.setCostAmtLL(costDetail.getAmtLL().add(adjustCostLowerLevel));
    }
    costDetail.setCumulatedQty(getNewAccumulatedQuantity(lastCostDetail));
    costDetail.setCumulatedAmt(getNewAccumulatedAmount(lastCostDetail));
    costDetail.setCumulatedAmtLL(getNewAccumulatedAmountLowerLevel(lastCostDetail));
    costDetail.setCurrentCostPrice(currentCostPrice);
    costDetail.setCurrentCostPriceLL(currentCostPriceLowerLevel);
    // set the id for model
    final String idColumnName = CostEngine.getIDColumnName(model);
    costDetail.set_ValueOfColumn(idColumnName, CostEngine.getIDColumn(model));
    if (model instanceof MInOutLine) {
        MInOutLine ioLine = (MInOutLine) model;
        costDetail.setC_OrderLine_ID(ioLine.getC_OrderLine_ID());
        // IMPORTANT : reset possible provision purchase cost processed
        costDetail.setC_InvoiceLine_ID(0);
    }
    if (model instanceof MMatchPO && costDetail.getM_InOutLine_ID() == 0) {
        MMatchPO poMatch = (MMatchPO) model;
        costDetail.setM_InOutLine_ID(poMatch.getM_InOutLine_ID());
    }
    if (model instanceof MLandedCostAllocation) {
        MLandedCostAllocation allocation = (MLandedCostAllocation) model;
        costDetail.setM_InOutLine_ID(allocation.getM_InOutLine_ID());
        costDetail.setC_InvoiceLine_ID(allocation.getC_InvoiceLine_ID());
        costDetail.setProcessed(false);
    }
    costDetail.saveEx();
}
Also used : MLandedCostAllocation(org.compiere.model.MLandedCostAllocation) MInOutLine(org.compiere.model.MInOutLine) MMatchPO(org.compiere.model.MMatchPO)

Example 4 with MLandedCostAllocation

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

the class AveragePOCostingMethod method createCostDetail.

private void createCostDetail() {
    //Validate if model have a reverses and processing of reverse
    if (model.getReversalLine_ID() > 0 && costDetail == null) {
        createReversalCostDetail();
        return;
    } else if (model.getReversalLine_ID() > 0)
        return;
    int seqNo = lastCostDetail.getSeqNo() + 10;
    // adjustment
    if (transaction.getM_Transaction_ID() != lastCostDetail.getM_Transaction_ID() && costDetail == null || adjustCost.add(adjustCostLowerLevel).signum() != 0 && costDetail == null) {
        // if exist adjustment cost for Landed Cost Allocation or Match Inv then set the movement qty to zero
        if ((adjustCost.add(adjustCostLowerLevel).signum() != 0 && costDetail != null) || (model instanceof MLandedCostAllocation || model instanceof MMatchPO))
            movementQuantity = Env.ZERO;
        // create new cost detail
        costDetail = new MCostDetail(transaction, accountSchema.getC_AcctSchema_ID(), dimension.getM_CostType_ID(), dimension.getM_CostElement_ID(), currentCostPrice.multiply(movementQuantity).abs(), currentCostPriceLowerLevel.multiply(movementQuantity).abs(), movementQuantity, transaction.get_TrxName());
        // set account date for this cost detail
        costDetail.setDateAcct(dateAccounting);
        costDetail.setSeqNo(seqNo);
        // set transaction id
        if (transaction != null)
            costDetail.setM_Transaction_ID(transaction.getM_Transaction_ID());
        // set if transaction is sales order type or not
        if (isSalesTransaction != null)
            costDetail.setIsSOTrx(isSalesTransaction);
        else
            costDetail.setIsSOTrx(model.isSOTrx());
        if (adjustCost.signum() != 0 || adjustCostLowerLevel.signum() != 0) {
            String description = costDetail.getDescription() != null ? costDetail.getDescription() : "";
            // update adjustment cost this level
            if (adjustCost.signum() != 0) {
                costDetail.setCostAdjustmentDate(model.getDateAcct());
                costDetail.setCostAdjustment(adjustCost);
                costDetail.setCostAmt(BigDecimal.ZERO);
                costDetail.setAmt(costDetail.getAmt().add(costDetail.getCostAdjustment()));
                costDetail.setDescription(description + " Adjust Cost:" + adjustCost);
            }
            // update adjustment cost lower level
            if (adjustCostLowerLevel.signum() != 0) {
                description = costDetail.getDescription() != null ? costDetail.getDescription() : "";
                costDetail.setCostAdjustmentDateLL(model.getDateAcct());
                costDetail.setCostAdjustmentLL(adjustCostLowerLevel);
                costDetail.setCostAmtLL(BigDecimal.ZERO);
                costDetail.setAmt(costDetail.getCostAmtLL().add(costDetail.getCostAdjustmentLL()));
                costDetail.setDescription(description + " Adjust Cost LL:" + adjustCost);
            }
        }
        updateAmountCost();
        return;
    }
}
Also used : MLandedCostAllocation(org.compiere.model.MLandedCostAllocation) MMatchPO(org.compiere.model.MMatchPO) MCostDetail(org.compiere.model.MCostDetail)

Example 5 with MLandedCostAllocation

use of org.compiere.model.MLandedCostAllocation 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);
}
Also used : MCostElement(org.compiere.model.MCostElement) MAcctSchema(org.compiere.model.MAcctSchema) MLandedCostAllocation(org.compiere.model.MLandedCostAllocation) MInOutLine(org.compiere.model.MInOutLine) MMatchPO(org.compiere.model.MMatchPO) MTransaction(org.compiere.model.MTransaction) List(java.util.List) MCostType(org.compiere.model.MCostType) Properties(java.util.Properties)

Aggregations

MLandedCostAllocation (org.compiere.model.MLandedCostAllocation)10 MMatchPO (org.compiere.model.MMatchPO)7 MCostDetail (org.compiere.model.MCostDetail)5 MInOutLine (org.compiere.model.MInOutLine)5 MMatchInv (org.compiere.model.MMatchInv)5 BigDecimal (java.math.BigDecimal)4 MAcctSchema (org.compiere.model.MAcctSchema)3 MCostType (org.compiere.model.MCostType)3 MTransaction (org.compiere.model.MTransaction)3 List (java.util.List)2 Properties (java.util.Properties)2 MCostElement (org.compiere.model.MCostElement)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Optional (java.util.Optional)1 Level (java.util.logging.Level)1 MAccount (org.compiere.model.MAccount)1