Search in sources :

Example 1 with MCost

use of org.compiere.model.MCost 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;
}
Also used : MCostElement(org.compiere.model.MCostElement) MProduct(org.compiere.model.MProduct) MAccount(org.compiere.model.MAccount) MCost(org.compiere.model.MCost) ArrayList(java.util.ArrayList) MCostType(org.compiere.model.MCostType) MCostDetail(org.compiere.model.MCostDetail) BigDecimal(java.math.BigDecimal)

Example 2 with MCost

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

the class CostUpdate method update.

//	createNew
/**************************************************************************
	 * 	Update Cost Records
	 * 	@return no updated
	 */
private int update() {
    int counter = 0;
    String sql = "SELECT * FROM M_Cost c WHERE M_CostElement_ID=?";
    if (p_M_Product_Category_ID != 0)
        sql += " AND EXISTS (SELECT * FROM M_Product p " + "WHERE c.M_Product_ID=p.M_Product_ID AND p.M_Product_Category_ID=?)";
    PreparedStatement pstmt = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        pstmt.setInt(1, m_ce.getM_CostElement_ID());
        if (p_M_Product_Category_ID != 0)
            pstmt.setInt(2, p_M_Product_Category_ID);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            MCost cost = new MCost(getCtx(), rs, get_TrxName());
            for (int i = 0; i < m_ass.length; i++) {
                //	Update Costs only for default Cost Type
                if (m_ass[i].getC_AcctSchema_ID() == cost.getC_AcctSchema_ID() && m_ass[i].getM_CostType_ID() == cost.getM_CostType_ID()) {
                    if (update(cost))
                        counter++;
                }
            }
        }
        rs.close();
        pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        log.log(Level.SEVERE, sql, e);
    }
    try {
        if (pstmt != null)
            pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        pstmt = null;
    }
    log.info("#" + counter);
    addLog(0, null, new BigDecimal(counter), "@Updated@");
    return counter;
}
Also used : MCost(org.compiere.model.MCost) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal)

Example 3 with MCost

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

the class CostUpdate method getCosts.

//	update
/**
	 * 	Get Costs
	 *	@param cost cost
	 *	@param to where to get costs from 
	 *	@return costs (could be 0) or null if not found
	 *	@throws Exception
	 */
private BigDecimal getCosts(MCost cost, String to) throws Exception {
    BigDecimal retValue = null;
    MCostElement costElement = MCostElement.getByMaterialCostElementType(cost);
    if (costElement == null)
        throw new AdempiereSystemError("@M_CostElement_ID@ @NotFound@: ");
    //	Average Invoice
    if (to.equals(TO_AverageInvoice)) {
        if (costElement == null)
            throw new AdempiereSystemError("CostElement not found: " + TO_AverageInvoice);
        MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
        if (costDimension != null)
            retValue = costDimension.getCurrentCostPrice();
    } else //	Average Invoice History
    if (to.equals(TO_AverageInvoiceHistory)) {
        if (costElement == null)
            throw new AdempiereSystemError("CostElement not found: " + TO_AverageInvoice);
        MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
        if (costDimension != null)
            retValue = costDimension.getHistoryAverage();
    } else //	Average PO
    if (to.equals(TO_AveragePO)) {
        if (costElement == null)
            throw new AdempiereSystemError("CostElement not found: " + TO_AveragePO);
        MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
        if (costDimension != null)
            retValue = costDimension.getCurrentCostPrice();
    } else //	Average PO History
    if (to.equals(TO_AveragePOHistory)) {
        if (costElement == null)
            throw new AdempiereSystemError("CostElement not found: " + TO_AveragePO);
        MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
        if (costDimension != null)
            retValue = costDimension.getHistoryAverage();
    } else //	FiFo
    if (to.equals(TO_FiFo)) {
        if (costElement == null)
            throw new AdempiereSystemError("CostElement not found: " + TO_FiFo);
        MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
        if (costDimension != null)
            retValue = costDimension.getCurrentCostPrice();
    } else //	Future Std Costs
    if (to.equals(TO_FutureStandardCost))
        retValue = cost.getFutureCostPrice();
    else //	Last Inv Price
    if (to.equals(TO_LastInvoicePrice)) {
        if (costElement != null) {
            MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
            if (costDimension != null)
                retValue = costDimension.getCurrentCostPrice();
        }
        if (retValue == null) {
            MProduct product = MProduct.get(getCtx(), cost.getM_Product_ID());
            MAcctSchema as = MAcctSchema.get(getCtx(), cost.getC_AcctSchema_ID());
            retValue = MCost.getLastInvoicePrice(product, cost.getM_AttributeSetInstance_ID(), cost.getAD_Org_ID(), as.getC_Currency_ID());
        }
    } else //	Last PO Price
    if (to.equals(TO_LastPOPrice)) {
        if (costElement != null) {
            MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
            if (costDimension != null)
                retValue = costDimension.getCurrentCostPrice();
        }
        if (retValue == null) {
            MProduct product = MProduct.get(getCtx(), cost.getM_Product_ID());
            MAcctSchema as = MAcctSchema.get(getCtx(), cost.getC_AcctSchema_ID());
            retValue = MCost.getLastPOPrice(product, cost.getM_AttributeSetInstance_ID(), cost.getAD_Org_ID(), as.getC_Currency_ID());
        }
    } else //	FiFo
    if (to.equals(TO_LiFo)) {
        if (costElement == null)
            throw new AdempiereSystemError("CostElement not found: " + TO_LiFo);
        MCost costDimension = getDimension(cost, costElement.getM_CostElement_ID());
        if (costDimension != null)
            retValue = costDimension.getCurrentCostPrice();
    } else //	Old Std Costs
    if (to.equals(TO_OldStandardCost))
        retValue = getOldCurrentCostPrice(cost);
    else //	Price List
    if (to.equals(TO_PriceListLimit))
        retValue = getPrice(cost);
    else //	Standard Costs
    if (to.equals(TO_StandardCost))
        retValue = cost.getCurrentCostPrice();
    return retValue;
}
Also used : MCostElement(org.compiere.model.MCostElement) MProduct(org.compiere.model.MProduct) MAcctSchema(org.compiere.model.MAcctSchema) AdempiereSystemError(org.compiere.util.AdempiereSystemError) MCost(org.compiere.model.MCost) BigDecimal(java.math.BigDecimal)

Example 4 with MCost

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

the class CostResult method assertCostReceiptReversal.

/**
	 * assert Cost Receipt
	 * @param product
	 * @param receiptLine
	 * @param as
	 * @param trxName
	 */
private void assertCostReceiptReversal(CostResult costResult, int M_InOutLine_ID, MAcctSchema as, String trxName) {
    MCost cost = assertCost(costResult);
    String whereClause = "M_Product_ID=? AND M_CostElement_ID=? AND M_CostType_ID=? AND M_InOutLine_ID=?";
    ArrayList<Object> parameters = new ArrayList();
    parameters.add(costResult.M_Product_ID);
    parameters.add(cost.getM_CostElement_ID());
    parameters.add(cost.getM_CostType_ID());
    parameters.add(M_InOutLine_ID);
    assertCostDetail(costResult, whereClause, parameters);
}
Also used : MCost(org.compiere.model.MCost) ArrayList(java.util.ArrayList)

Example 5 with MCost

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

the class CostResult method assertCostShipmentAdjust.

/**
	 * assert Cost Receipt
	 * @param product
	 * @param receiptLine
	 * @param as
	 * @param trxName
	 */
private void assertCostShipmentAdjust(CostResult costResult, int C_OrderLine_ID, MAcctSchema as, String trxName) {
    MCost cost = assertCost(costResult);
    String whereClause = "M_Product_ID=? AND M_CostElement_ID=? AND M_CostType_ID=?  AND M_InOutLine_ID IN (SELECT M_InOutLine_ID FROM M_InOutLine iol WHERE iol.C_OrderLine_ID=?)";
    ArrayList<Object> parameters = new ArrayList();
    parameters.add(costResult.M_Product_ID);
    parameters.add(cost.getM_CostElement_ID());
    parameters.add(cost.getM_CostType_ID());
    parameters.add(C_OrderLine_ID);
    assertCostDetail(costResult, whereClause, parameters);
}
Also used : MCost(org.compiere.model.MCost) ArrayList(java.util.ArrayList)

Aggregations

MCost (org.compiere.model.MCost)29 BigDecimal (java.math.BigDecimal)19 ArrayList (java.util.ArrayList)14 MProduct (org.compiere.model.MProduct)11 MAcctSchema (org.compiere.model.MAcctSchema)9 MCostElement (org.compiere.model.MCostElement)9 Query (org.compiere.model.Query)8 Arrays (java.util.Arrays)6 List (java.util.List)6 MCostType (org.compiere.model.MCostType)6 CostDimension (org.adempiere.engine.CostDimension)5 Trx (org.compiere.util.Trx)5 Env (org.compiere.util.Env)4 Msg (org.compiere.util.Msg)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 AdempiereException (org.adempiere.exceptions.AdempiereException)3 MTransaction (org.compiere.model.MTransaction)3 TrxRunnable (org.compiere.util.TrxRunnable)3 MWorkflow (org.compiere.wf.MWorkflow)3 MPPProductBOM (org.eevolution.model.MPPProductBOM)3