Search in sources :

Example 16 with MAcctSchema

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

the class CostBillOfMaterial method createLines.

/**
     * createLines
     *
     * @param bom
     * @param bomLine
     */
private void createLines(MAcctSchema accountSchema, MPPProductBOM bom, MPPProductBOMLine bomLine) {
    MProduct product;
    BigDecimal qty;
    if (bomLine != null) {
        product = MProduct.get(getCtx(), bomLine.getM_Product_ID());
        qty = bomLine.getQty();
    } else if (bom != null) {
        product = MProduct.get(getCtx(), bom.getM_Product_ID());
        qty = Env.ONE;
    } else {
        throw new AdempiereException("@NotFound@ @PP_Product_BOM_ID@");
    }
    //for (MCostElement costElement : getCostElements())
    getCostElements().stream().filter(costElement -> costElement != null).forEach(costElement -> {
        X_T_BOMLine reportBOMLine = new X_T_BOMLine(getCtx(), 0, get_TrxName());
        reportBOMLine.setAD_Org_ID(getOrganizationId());
        reportBOMLine.setM_Warehouse_ID(getWarehouseId());
        reportBOMLine.setSel_Product_ID(getProductId());
        reportBOMLine.setImplosion(isImplosion);
        reportBOMLine.setC_AcctSchema_ID(getAccountingSchemaId());
        reportBOMLine.setM_CostType_ID(getCostTypeId());
        reportBOMLine.setCostingMethod(getCostingMethod());
        reportBOMLine.setAD_PInstance_ID(getAD_PInstance_ID());
        reportBOMLine.setM_CostElement_ID(costElement.get_ID());
        reportBOMLine.setM_Product_ID(product.get_ID());
        reportBOMLine.setM_Warehouse_ID(getWarehouseId());
        reportBOMLine.setQtyBOM(qty);
        reportBOMLine.setSeqNo(seqNo);
        reportBOMLine.setLevelNo(levelNo);
        reportBOMLine.setLevels(LEVELS.substring(0, levelNo) + levelNo);
        BigDecimal currentCostPrice = Env.ZERO;
        BigDecimal currentCostPriceLL = Env.ZERO;
        BigDecimal futureCostPrice = Env.ZERO;
        BigDecimal futureCostPriceLL = Env.ZERO;
        final CostEngine engine = CostEngineFactory.getCostEngine(getAD_Client_ID());
        List<MCost> costs = MCost.getByElement(product, accountSchema, getCostTypeId(), getOrganizationId(), getWarehouseId(), 0, costElement.getM_CostElement_ID());
        boolean isCostFrozen = false;
        for (MCost cost : costs) {
            currentCostPrice = currentCostPrice.add(cost.getCurrentCostPrice());
            currentCostPriceLL = currentCostPriceLL.add(cost.getCurrentCostPriceLL());
            futureCostPrice = futureCostPrice.add(cost.getFutureCostPrice());
            futureCostPriceLL = futureCostPriceLL.add(cost.getFutureCostPriceLL());
            isCostFrozen = cost.isCostFrozen();
        }
        reportBOMLine.setCurrentCostPrice(currentCostPrice);
        reportBOMLine.setCurrentCostPriceLL(currentCostPriceLL);
        reportBOMLine.setFutureCostPrice(currentCostPrice);
        reportBOMLine.setFutureCostPriceLL(currentCostPriceLL);
        reportBOMLine.setIsCostFrozen(isCostFrozen);
        if (bomLine != null) {
            reportBOMLine.setPP_Product_BOM_ID(bomLine.getPP_Product_BOM_ID());
            reportBOMLine.setPP_Product_BOMLine_ID(bomLine.getPP_Product_BOMLine_ID());
        } else if (bom != null) {
            reportBOMLine.setPP_Product_BOM_ID(bom.getPP_Product_BOM_ID());
        }
        reportBOMLine.saveEx();
        seqNo++;
    });
}
Also used : CostEngineFactory(org.adempiere.engine.CostEngineFactory) Arrays(java.util.Arrays) Env(org.compiere.util.Env) X_T_BOMLine(org.eevolution.model.X_T_BOMLine) MAcctSchema(org.compiere.model.MAcctSchema) ArrayList(java.util.ArrayList) MPPProductBOM(org.eevolution.model.MPPProductBOM) MCost(org.compiere.model.MCost) BigDecimal(java.math.BigDecimal) List(java.util.List) Query(org.compiere.model.Query) AdempiereException(org.adempiere.exceptions.AdempiereException) MCostElement(org.compiere.model.MCostElement) MProduct(org.compiere.model.MProduct) MPPProductBOMLine(org.eevolution.model.MPPProductBOMLine) CostEngine(org.adempiere.engine.CostEngine) MProduct(org.compiere.model.MProduct) CostEngine(org.adempiere.engine.CostEngine) AdempiereException(org.adempiere.exceptions.AdempiereException) MCost(org.compiere.model.MCost) BigDecimal(java.math.BigDecimal) X_T_BOMLine(org.eevolution.model.X_T_BOMLine)

Example 17 with MAcctSchema

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

the class CostBillOfMaterial method doIt.

/**
     * Execute process
     *
     * @return
     * @throws Exception
     */
protected String doIt() throws Exception {
    MAcctSchema accountSchema = MAcctSchema.get(getCtx(), getAccountingSchemaId());
    explodeProduct(accountSchema, getProductId(), false);
    return "@Ok@";
}
Also used : MAcctSchema(org.compiere.model.MAcctSchema)

Example 18 with MAcctSchema

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

the class RollupBillOfMaterial method doIt.

//	prepare
/**
	 * 	Generate Calculate Cost
	 *	@return info
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    //Get account schema
    MAcctSchema acctSchema = MAcctSchema.get(getCtx(), getAccountingSchemaId());
    //Get cost type
    MCostType costType = MCostType.get(getCtx(), getCostTypeId());
    final List<MCostElement> costElements = getCostElementId() > 0 ? Arrays.asList(MCostElement.get(getCtx(), getCostElementId())) : MCostElement.getCostElement(getCtx(), get_TrxName());
    //Get max low level
    int maxLowLevel = MPPMRP.getMaxLowLevel(getCtx(), get_TrxName());
    // Cost Roll-up for all levels
    for (int lowLevel = maxLowLevel; lowLevel >= 0; lowLevel--) {
        //Iterate product based in parameters
        Arrays.stream(getProductIds(lowLevel)).filter(productId -> productId > 0).forEach(productId -> {
            MProduct product = MProduct.get(getCtx(), productId);
            I_PP_Product_Planning productPlanning = MPPProductPlanning.find(getCtx(), getOrganizationId(), getWarehouseId(), getResourcePlantId(), productId, get_TrxName());
            int bomId = 0;
            if (productPlanning != null)
                bomId = productPlanning.getPP_Product_BOM_ID();
            else
                createNotice(product, Msg.parseTranslation(getCtx(), "@NotFound@ @PP_Product_Planning_ID@"));
            if (bomId <= 0)
                bomId = MPPProductBOM.getBOMSearchKey(product);
            MPPProductBOM bom = MPPProductBOM.get(getCtx(), bomId);
            if (bom == null)
                createNotice(product, Msg.parseTranslation(getCtx(), "@NotFound@ @PP_Product_BOM_ID@"));
            Trx.run(new TrxRunnable() {

                MAcctSchema acctSchema;

                MCostType costType;

                MProduct product;

                MPPProductBOM bom;

                public TrxRunnable setParameters(MAcctSchema acctSchema, MCostType costType, MProduct product, MPPProductBOM bom) {
                    this.acctSchema = acctSchema;
                    this.costType = costType;
                    this.product = product;
                    this.bom = bom;
                    return this;
                }

                public void run(String trxName) {
                    costElements.stream().filter(costElement -> costElement != null).forEach(costElement -> {
                        rollup(acctSchema, costType, costElement, product, bom, trxName);
                    });
                }
            }.setParameters(acctSchema, costType, product, bom));
        });
    // Products List
    }
    // for Low Lever
    return "@OK@";
}
Also used : MUOMConversion(org.compiere.model.MUOMConversion) MPPProductPlanning(org.eevolution.model.MPPProductPlanning) Arrays(java.util.Arrays) I_PP_Product_Planning(org.eevolution.model.I_PP_Product_Planning) MWorkflow(org.compiere.wf.MWorkflow) Env(org.compiere.util.Env) MCostType(org.compiere.model.MCostType) AtomicReference(java.util.concurrent.atomic.AtomicReference) MAcctSchema(org.compiere.model.MAcctSchema) ArrayList(java.util.ArrayList) MPPProductBOM(org.eevolution.model.MPPProductBOM) MCost(org.compiere.model.MCost) BigDecimal(java.math.BigDecimal) List(java.util.List) Query(org.compiere.model.Query) DB(org.compiere.util.DB) Msg(org.compiere.util.Msg) MCostElement(org.compiere.model.MCostElement) TrxRunnable(org.compiere.util.TrxRunnable) MPPMRP(org.eevolution.model.MPPMRP) Trx(org.compiere.util.Trx) MProduct(org.compiere.model.MProduct) MCostElement(org.compiere.model.MCostElement) MAcctSchema(org.compiere.model.MAcctSchema) MProduct(org.compiere.model.MProduct) I_PP_Product_Planning(org.eevolution.model.I_PP_Product_Planning) TrxRunnable(org.compiere.util.TrxRunnable) MCostType(org.compiere.model.MCostType) MPPProductBOM(org.eevolution.model.MPPProductBOM)

Example 19 with MAcctSchema

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

the class Doc method get.

//	get
/**
	 *  Create Posting document
	 *	@param ass accounting schema
	 *  @param AD_Table_ID Table ID of Documents
	 *  @param rs ResultSet
	 *  @param trxName transaction name
	 *  @return Document
	 * @throws AdempiereUserError 
	 */
public static Doc get(MAcctSchema[] ass, int AD_Table_ID, ResultSet rs, String trxName) throws AdempiereUserError {
    Doc doc = null;
    /* Classname of the Doc class follows this convention:
		 * if the prefix (letters before the first underscore _) is 1 character, then the class is Doc_TableWithoutPrefixWithoutUnderscores
		 * otherwise Doc_WholeTableWithoutUnderscores
		 * i.e. following this query
              SELECT t.ad_table_id, tablename, 
              	CASE 
              		WHEN instr(tablename, '_') = 2 
              		THEN 'Doc_' || substr(tablename, 3) 
              		WHEN instr(tablename, '_') > 2 
              		THEN 'Doc_' || 
              		ELSE '' 
              	REPLACE
              		(
              			tablename, '_', ''
              		)
              	END AS classname 
              FROM ad_table t, ad_column C 
              WHERE t.ad_table_id = C.ad_table_id AND
              	C.columnname = 'Posted' AND
              	isview = 'N' 
              ORDER BY 1
		 * This is:
		 * 224		GL_Journal			Doc_GLJournal
		 * 259		C_Order				Doc_Order
		 * 318		C_Invoice			Doc_Invoice
		 * 319		M_InOut				Doc_InOut
		 * 321		M_Inventory			Doc_Inventory
		 * 323		M_Movement			Doc_Movement
		 * 325		M_Production		Doc_Production
		 * 335		C_Payment			Doc_Payment
		 * 392		C_BankStatement		Doc_BankStatement
		 * 407		C_Cash				Doc_Cash
		 * 472		M_MatchInv			Doc_MatchInv
		 * 473		M_MatchPO			Doc_MatchPO
		 * 623		C_ProjectIssue		Doc_ProjectIssue
		 * 702		M_Requisition		Doc_Requisition
		 * 735		C_AllocationHdr		Doc_AllocationHdr
		 * 53027	PP_Order			Doc_PPOrder
		 * 53035	PP_Cost_Collector	Doc_PPCostCollector
		 * 53037	DD_Order			Doc_DDOrder
		 * 53092	HR_Process			Doc_HRProcess
		 */
    String tableName = MTable.getTableName(Env.getCtx(), AD_Table_ID);
    String packageName = "org.compiere.acct";
    String className = null;
    int firstUnderscore = tableName.indexOf("_");
    if (firstUnderscore == 1)
        className = packageName + ".Doc_" + tableName.substring(2).replaceAll("_", "");
    else
        className = packageName + ".Doc_" + tableName.replaceAll("_", "");
    try {
        Class<?> cClass = Class.forName(className);
        Constructor<?> cnstr = cClass.getConstructor(new Class[] { MAcctSchema[].class, ResultSet.class, String.class });
        doc = (Doc) cnstr.newInstance(ass, rs, trxName);
    } catch (Exception e) {
        s_log.log(Level.SEVERE, "Doc Class invalid: " + className + " (" + e.toString() + ")");
        throw new AdempiereUserError("Doc Class invalid: " + className + " (" + e.toString() + ")");
    }
    if (doc == null)
        s_log.log(Level.SEVERE, "Unknown AD_Table_ID=" + AD_Table_ID);
    return doc;
}
Also used : MAcctSchema(org.compiere.model.MAcctSchema) AdempiereUserError(org.compiere.util.AdempiereUserError) SQLException(java.sql.SQLException) DBException(org.adempiere.exceptions.DBException)

Example 20 with MAcctSchema

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

Aggregations

MAcctSchema (org.compiere.model.MAcctSchema)45 BigDecimal (java.math.BigDecimal)20 MCostElement (org.compiere.model.MCostElement)18 MProduct (org.compiere.model.MProduct)15 ArrayList (java.util.ArrayList)14 MCostType (org.compiere.model.MCostType)14 List (java.util.List)12 MCost (org.compiere.model.MCost)12 Arrays (java.util.Arrays)11 Query (org.compiere.model.Query)11 Trx (org.compiere.util.Trx)9 AdempiereException (org.adempiere.exceptions.AdempiereException)8 Env (org.compiere.util.Env)8 CostDimension (org.adempiere.engine.CostDimension)7 SQLException (java.sql.SQLException)6 MAcctSchemaElement (org.compiere.model.MAcctSchemaElement)6 Msg (org.compiere.util.Msg)6 Timestamp (java.sql.Timestamp)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 AdempiereSystemError (org.compiere.util.AdempiereSystemError)5