Search in sources :

Example 1 with ProductCost

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

the class Doc_Invoice method landedCost.

//	createFactCash
/**
	 * 	Create Landed Cost accounting & Cost lines
	 *	@param as accounting schema
	 *	@param fact fact
	 *	@param line document line
	 *	@param isDebit DR entry (normal api)
	 *	@return true if landed costs were created
	 */
private boolean landedCost(MAcctSchema as, Fact fact, DocLine line, boolean isDebit) {
    int invoiceLineId = line.get_ID();
    MLandedCostAllocation[] landedCostAllocations = MLandedCostAllocation.getOfInvoiceLine(getCtx(), invoiceLineId, getTrxName());
    if (landedCostAllocations.length == 0)
        return false;
    BigDecimal totalBase = Arrays.stream(landedCostAllocations).map(MLandedCostAllocation::getBase).reduce(BigDecimal.ZERO, BigDecimal::add);
    //	Create New
    MInvoiceLine invoiceLine = new MInvoiceLine(getCtx(), invoiceLineId, getTrxName());
    Arrays.stream(landedCostAllocations).filter(// only cost allocation with base > 0
    landedCostAllocation -> landedCostAllocation.getBase().signum() != 0).forEach(landedCostAllocation -> {
        BigDecimal percent = landedCostAllocation.getBase().divide(totalBase, BigDecimal.ROUND_HALF_UP);
        String desc = invoiceLine.getDescription();
        if (desc == null)
            desc = percent + "%";
        else
            desc += " - " + percent + "%";
        if (line.getDescription() != null)
            desc += " - " + line.getDescription();
        ProductCost productCost = new ProductCost(Env.getCtx(), landedCostAllocation.getM_Product_ID(), landedCostAllocation.getM_AttributeSetInstance_ID(), getTrxName());
        BigDecimal debitAmount = BigDecimal.ZERO;
        BigDecimal creditAmount = BigDecimal.ZERO;
        ;
        FactLine factLine = null;
        MCostType costType = MCostType.get(as, landedCostAllocation.getM_Product_ID(), landedCostAllocation.getAD_Org_ID());
        if (MCostType.COSTINGMETHOD_AverageInvoice.equals(costType.getCostingMethod())) {
            BigDecimal assetAmount = Optional.ofNullable(MCostDetail.getByDocLineLandedCost(landedCostAllocation, as.getC_AcctSchema_ID(), costType.get_ID())).orElse(BigDecimal.ZERO);
            BigDecimal costAdjustment = landedCostAllocation.getAmt().subtract(assetAmount);
            if (assetAmount.signum() != 0) {
                if (isDebit)
                    debitAmount = assetAmount;
                else
                    creditAmount = assetAmount;
                factLine = fact.createLine(line, productCost.getAccount(ProductCost.ACCTTYPE_P_Asset, as), as.getC_Currency_ID(), debitAmount, creditAmount);
                factLine.setDescription(desc + " " + landedCostAllocation.getM_CostElement().getName());
                factLine.setM_Product_ID(landedCostAllocation.getM_Product_ID());
            }
            if (costAdjustment.signum() != 0) {
                if (isDebit)
                    debitAmount = costAdjustment;
                else
                    creditAmount = costAdjustment;
                factLine = fact.createLine(line, productCost.getAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as), getC_Currency_ID(), debitAmount, creditAmount);
            }
        } else {
            factLine = fact.createLine(line, productCost.getAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as), getC_Currency_ID(), debitAmount, creditAmount);
        }
        factLine.setDescription(desc + " " + landedCostAllocation.getM_CostElement().getName());
        factLine.setM_Product_ID(landedCostAllocation.getM_Product_ID());
    });
    log.config("Created #" + landedCostAllocations.length);
    return true;
}
Also used : Arrays(java.util.Arrays) Env(org.compiere.util.Env) MClientInfo(org.compiere.model.MClientInfo) PreparedStatement(java.sql.PreparedStatement) MCostType(org.compiere.model.MCostType) MLandedCostAllocation(org.compiere.model.MLandedCostAllocation) MAcctSchema(org.compiere.model.MAcctSchema) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ProductCost(org.compiere.model.ProductCost) BigDecimal(java.math.BigDecimal) MInvoice(org.compiere.model.MInvoice) SQLException(java.sql.SQLException) MCurrency(org.compiere.model.MCurrency) DB(org.compiere.util.DB) MInvoiceLine(org.compiere.model.MInvoiceLine) ResultSet(java.sql.ResultSet) MTax(org.compiere.model.MTax) Optional(java.util.Optional) MAccount(org.compiere.model.MAccount) MCostDetail(org.compiere.model.MCostDetail) MLandedCostAllocation(org.compiere.model.MLandedCostAllocation) ProductCost(org.compiere.model.ProductCost) MInvoiceLine(org.compiere.model.MInvoiceLine) MCostType(org.compiere.model.MCostType) BigDecimal(java.math.BigDecimal)

Example 2 with ProductCost

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

the class Doc_MatchInv method loadDocumentDetails.

/** Commitments			*/
//	private DocLine[]		m_commitments = null;
/**
	 *  Load Specific Document Details
	 *  @return error message or null
	 */
protected String loadDocumentDetails() {
    setC_Currency_ID(Doc.NO_CURRENCY);
    MMatchInv matchInv = (MMatchInv) getPO();
    setDateDoc(matchInv.getDateTrx());
    setQty(matchInv.getQty());
    //	Invoice Info
    int C_InvoiceLine_ID = matchInv.getC_InvoiceLine_ID();
    m_invoiceLine = new MInvoiceLine(getCtx(), C_InvoiceLine_ID, getTrxName());
    //		BP for NotInvoicedReceipts
    int C_BPartner_ID = m_invoiceLine.getParent().getC_BPartner_ID();
    setC_BPartner_ID(C_BPartner_ID);
    //
    int M_InOutLine_ID = matchInv.getM_InOutLine_ID();
    m_receiptLine = new MInOutLine(getCtx(), M_InOutLine_ID, getTrxName());
    //
    m_pc = new ProductCost(Env.getCtx(), getM_Product_ID(), matchInv.getM_AttributeSetInstance_ID(), getTrxName());
    m_pc.setQty(getQty());
    return null;
}
Also used : ProductCost(org.compiere.model.ProductCost) MInOutLine(org.compiere.model.MInOutLine) MInvoiceLine(org.compiere.model.MInvoiceLine) MMatchInv(org.compiere.model.MMatchInv)

Example 3 with ProductCost

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

the class FifoLifoCostingMethod method calculate.

public void calculate() {
    ProductCost pc = new ProductCost(model.getCtx(), model.getM_Product_ID(), model.getM_AttributeSetInstance_ID(), model.get_TrxName());
    pc.setQty(transaction.getMovementQty());
    //
    List<CostComponent> ccs = pc.getProductCostsLayers(dimension, 0, false);
    if (ccs == null || ccs.size() == 0) {
        MProduct product = MProduct.get(Env.getCtx(), model.getM_Product_ID());
        throw new AdempiereException("No Costs for " + product.getName());
    }
    m_calculatedCosts = ccs;
}
Also used : MProduct(org.compiere.model.MProduct) ProductCost(org.compiere.model.ProductCost) AdempiereException(org.adempiere.exceptions.AdempiereException)

Example 4 with ProductCost

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

the class Doc_MatchPO method loadDocumentDetails.

/**
	 *  Load Specific Document Details
	 *  @return error message or null
	 */
protected String loadDocumentDetails() {
    setC_Currency_ID(Doc.NO_CURRENCY);
    MMatchPO matchPO = (MMatchPO) getPO();
    setDateDoc(matchPO.getDateTrx());
    //
    m_M_AttributeSetInstance_ID = matchPO.getM_AttributeSetInstance_ID();
    setQty(matchPO.getQty());
    //
    m_C_OrderLine_ID = matchPO.getC_OrderLine_ID();
    m_oLine = new MOrderLine(getCtx(), m_C_OrderLine_ID, getTrxName());
    //
    m_M_InOutLine_ID = matchPO.getM_InOutLine_ID();
    m_ioLine = new MInOutLine(getCtx(), m_M_InOutLine_ID, getTrxName());
    m_C_InvoiceLine_ID = matchPO.getC_InvoiceLine_ID();
    //
    m_pc = new ProductCost(Env.getCtx(), getM_Product_ID(), m_M_AttributeSetInstance_ID, getTrxName());
    m_pc.setQty(getQty());
    return null;
}
Also used : ProductCost(org.compiere.model.ProductCost) MMatchPO(org.compiere.model.MMatchPO) MInOutLine(org.compiere.model.MInOutLine) MOrderLine(org.compiere.model.MOrderLine)

Example 5 with ProductCost

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

the class MAssetAcct method getP_Asset_Acct.

public MAccount getP_Asset_Acct(int M_Product_ID) {
    MAcctSchema as = getC_AcctSchema();
    ProductCost pc = new ProductCost(getCtx(), M_Product_ID, 0, null);
    return pc.getAccount(ProductCost.ACCTTYPE_P_Asset, as);
}
Also used : MAcctSchema(org.compiere.model.MAcctSchema) ProductCost(org.compiere.model.ProductCost)

Aggregations

ProductCost (org.compiere.model.ProductCost)5 MAcctSchema (org.compiere.model.MAcctSchema)2 MInOutLine (org.compiere.model.MInOutLine)2 MInvoiceLine (org.compiere.model.MInvoiceLine)2 BigDecimal (java.math.BigDecimal)1 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 AdempiereException (org.adempiere.exceptions.AdempiereException)1 MAccount (org.compiere.model.MAccount)1 MClientInfo (org.compiere.model.MClientInfo)1 MCostDetail (org.compiere.model.MCostDetail)1 MCostType (org.compiere.model.MCostType)1 MCurrency (org.compiere.model.MCurrency)1 MInvoice (org.compiere.model.MInvoice)1 MLandedCostAllocation (org.compiere.model.MLandedCostAllocation)1