Search in sources :

Example 1 with MMatchPO

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

the class CostEngine method clearAccounting.

/**
     * Clear Accounting
     * @param accountSchema
     * @param costType
     * @param model
	 * @param productId
     * @param dateAcct
     * @return true clean
     */
public boolean clearAccounting(MAcctSchema accountSchema, I_M_CostType costType, PO model, int productId, Timestamp dateAcct) {
    // check if costing type need reset accounting 
    if (!accountSchema.getCostingMethod().equals(costType.getCostingMethod())) {
        MProduct product = MProduct.get(accountSchema.getCtx(), productId);
        MProductCategoryAcct productCategoryAcct = MProductCategoryAcct.get(accountSchema.getCtx(), product.getM_Product_Category_ID(), accountSchema.get_ID(), model.get_TrxName());
        if (productCategoryAcct == null || !costType.getCostingMethod().equals(productCategoryAcct.getCostingMethod()))
            return false;
    }
    final String docBaseType;
    // check if account period is open
    if (model instanceof MMatchInv)
        docBaseType = MPeriodControl.DOCBASETYPE_MatchInvoice;
    else if (model instanceof MMatchPO)
        docBaseType = MPeriodControl.DOCBASETYPE_MatchPO;
    else if (model instanceof MProduction)
        docBaseType = MPeriodControl.DOCBASETYPE_MaterialProduction;
    else {
        MDocType docType = MDocType.get(model.getCtx(), model.get_ValueAsInt(MDocType.COLUMNNAME_C_DocType_ID));
        docBaseType = docType.getDocBaseType();
    }
    Boolean openPeriod = MPeriod.isOpen(model.getCtx(), dateAcct, docBaseType, model.getAD_Org_ID());
    if (!openPeriod) {
        System.out.println("Period closed.");
        return false;
    }
    final String sqlUpdate = "UPDATE " + model.get_TableName() + " SET Posted = 'N' WHERE " + model.get_TableName() + "_ID=?";
    DB.executeUpdate(sqlUpdate, new Object[] { model.get_ID() }, false, model.get_TrxName());
    //Delete account
    final String sqldelete = "DELETE FROM Fact_Acct WHERE Record_ID =? AND AD_Table_ID=?";
    DB.executeUpdate(sqldelete, new Object[] { model.get_ID(), model.get_Table_ID() }, false, model.get_TrxName());
    return true;
}
Also used : MProduct(org.compiere.model.MProduct) MDocType(org.compiere.model.MDocType) MMatchPO(org.compiere.model.MMatchPO) MMatchInv(org.compiere.model.MMatchInv) MProductCategoryAcct(org.compiere.model.MProductCategoryAcct) MProduction(org.compiere.model.MProduction)

Example 2 with MMatchPO

use of org.compiere.model.MMatchPO 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 3 with MMatchPO

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

the class AbstractCostingMethod method createCostDetails.

protected List<MCostDetail> createCostDetails() {
    final String idColumnName;
    if (model instanceof MMatchPO) {
        idColumnName = I_C_OrderLine.COLUMNNAME_C_OrderLine_ID;
    } else if (model instanceof MMatchInv) {
        idColumnName = I_C_InvoiceLine.COLUMNNAME_C_InvoiceLine_ID;
    } else {
        idColumnName = model.get_TableName() + "_ID";
    }
    List<MCostDetail> list = new ArrayList<MCostDetail>();
    if (model.isSOTrx() == true || model instanceof MInventoryLine || model instanceof MMovementLine) {
        List<CostComponent> costComponents = getCalculatedCosts();
        for (CostComponent costComponent : costComponents) {
            MCostDetail cost = new MCostDetail(transaction, accountSchema.getC_AcctSchema_ID(), dimension.getM_CostType_ID(), dimension.getM_CostElement_ID(), costComponent.getAmount(), Env.ZERO, costComponent.getQty(), model.get_TrxName());
            if (!cost.set_ValueOfColumnReturningBoolean(idColumnName, model.get_ID()))
                throw new AdempiereException("Cannot set " + idColumnName);
            StringBuilder description = new StringBuilder();
            if (!Util.isEmpty(model.getDescription(), true))
                description.append(model.getDescription());
            if (model.isSOTrx() != false) {
                description.append(model.isSOTrx() ? "(|->)" : "(|<-)");
            }
            if (// TODO: need evaluate anca
            model.isSOTrx() != false)
                cost.setIsSOTrx(model.isSOTrx());
            else
                cost.setIsSOTrx(model.isSOTrx());
            cost.setM_Transaction_ID(transaction.get_ID());
            cost.setDescription(description.toString());
            cost.saveEx();
            list.add(cost);
        }
    } else // qty and amt is take from documentline
    {
        MCostDetail cost = new MCostDetail(transaction, accountSchema.getC_AcctSchema_ID(), dimension.getM_CostType_ID(), dimension.getM_CostElement_ID(), costThisLevel.multiply(model.getMovementQty()), Env.ZERO, model.getMovementQty(), model.get_TrxName());
        int id;
        if (model instanceof MMatchPO) {
            I_M_InOutLine inOutLine = transaction.getM_InOutLine();
            I_C_OrderLine orderLine = inOutLine.getC_OrderLine();
            id = orderLine.getC_OrderLine_ID();
        } else {
            id = model.get_ID();
        }
        if (!cost.set_ValueOfColumnReturningBoolean(idColumnName, id))
            throw new AdempiereException("Cannot set " + idColumnName);
        if (model.isSOTrx() != false)
            cost.setIsSOTrx(model.isSOTrx());
        else
            cost.setIsSOTrx(model.isSOTrx());
        cost.setM_Transaction_ID(transaction.get_ID());
        cost.saveEx();
        list.add(cost);
    }
    return list;
}
Also used : MMatchPO(org.compiere.model.MMatchPO) MInventoryLine(org.compiere.model.MInventoryLine) I_M_InOutLine(org.compiere.model.I_M_InOutLine) ArrayList(java.util.ArrayList) I_C_OrderLine(org.compiere.model.I_C_OrderLine) AdempiereException(org.adempiere.exceptions.AdempiereException) MMatchInv(org.compiere.model.MMatchInv) MCostDetail(org.compiere.model.MCostDetail) MMovementLine(org.compiere.model.MMovementLine)

Example 4 with MMatchPO

use of org.compiere.model.MMatchPO 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 5 with MMatchPO

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

Aggregations

MMatchPO (org.compiere.model.MMatchPO)14 MInOutLine (org.compiere.model.MInOutLine)9 MMatchInv (org.compiere.model.MMatchInv)8 MLandedCostAllocation (org.compiere.model.MLandedCostAllocation)7 MCostDetail (org.compiere.model.MCostDetail)3 MInventoryLine (org.compiere.model.MInventoryLine)3 MMovementLine (org.compiere.model.MMovementLine)3 MOrderLine (org.compiere.model.MOrderLine)3 MTransaction (org.compiere.model.MTransaction)3 BigDecimal (java.math.BigDecimal)2 List (java.util.List)2 Properties (java.util.Properties)2 MAcctSchema (org.compiere.model.MAcctSchema)2 MCostElement (org.compiere.model.MCostElement)2 MCostType (org.compiere.model.MCostType)2 MProduction (org.compiere.model.MProduction)2 MProductionLine (org.compiere.model.MProductionLine)2 MPPCostCollector (org.eevolution.model.MPPCostCollector)2 ArrayList (java.util.ArrayList)1 AdempiereException (org.adempiere.exceptions.AdempiereException)1