Search in sources :

Example 51 with MProduct

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

the class CostBillOfMaterial method explodeProduct.

// doIt
/**
     * * Generate an Explosion for this product
     *
     * @param accountSchema
     * @param productId
     * @param isComponent
     */
private void explodeProduct(MAcctSchema accountSchema, int productId, boolean isComponent) {
    MProduct product = MProduct.get(getCtx(), productId);
    List<MPPProductBOM> boms = getBOMs(product, isComponent);
    if (!isComponent && boms.size() == 0) {
        throw new AdempiereException("@Error@ Product is not a BOM");
    }
    boms.stream().filter(bom -> bom != null).forEach(bom -> {
        if (!isComponent)
            createLines(accountSchema, bom, null);
        levelNo++;
        Arrays.stream(bom.getLines()).filter(bomLine -> bomLine != null && bomLine.isActive()).forEach(bomLine -> {
            createLines(accountSchema, bom, bomLine);
            explodeProduct(accountSchema, bomLine.getM_Product_ID(), true);
        });
        levelNo--;
    });
}
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) AdempiereException(org.adempiere.exceptions.AdempiereException) MPPProductBOM(org.eevolution.model.MPPProductBOM)

Example 52 with MProduct

use of org.compiere.model.MProduct 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 53 with MProduct

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

the class MovementGenerate method generate.

//	doIt
/**
	 * 	Generate Shipments
	 * 	@param pstmt Order Query
	 *	@return info
	 */
private String generate(PreparedStatement pstmt) {
    MClient client = MClient.get(getCtx());
    try {
        ResultSet rs = pstmt.executeQuery();
        while (//	Order
        rs.next()) {
            MDDOrder order = new MDDOrder(getCtx(), rs, get_TrxName());
            //	New Header different Shipper, Shipment Location
            if (!p_ConsolidateDocument || (m_movement != null && (m_movement.getC_BPartner_Location_ID() != order.getC_BPartner_Location_ID() || m_movement.getM_Shipper_ID() != order.getM_Shipper_ID()))) {
                completeMovement();
            }
            log.fine("check: " + order + " - DeliveryRule=" + order.getDeliveryRule());
            //
            Timestamp minGuaranteeDate = m_movementDate;
            boolean completeOrder = MDDOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule());
            //	OrderLine WHERE
            String where = " " + p_M_Warehouse_ID + " IN (SELECT l.M_Warehouse_ID FROM M_Locator l WHERE l.M_Locator_ID=M_Locator_ID) ";
            if (p_DatePromised != null)
                where += " AND (TRUNC(DatePromised)<=" + DB.TO_DATE(p_DatePromised, true) + " OR DatePromised IS NULL)";
            //	Exclude Auto Delivery if not Force
            if (!MDDOrder.DELIVERYRULE_Force.equals(order.getDeliveryRule()))
                where += " AND (DD_OrderLine.M_Product_ID IS NULL" + " OR EXISTS (SELECT * FROM M_Product p " + "WHERE DD_OrderLine.M_Product_ID=p.M_Product_ID" + " AND IsExcludeAutoDelivery='N'))";
            //	Exclude Unconfirmed
            if (!p_IsUnconfirmedInOut)
                where += " AND NOT EXISTS (SELECT * FROM M_MovementLine iol" + " INNER JOIN M_Movement io ON (iol.M_Movement_ID=io.M_Movement_ID) " + "WHERE iol.DD_OrderLine_ID=DD_OrderLine.DD_OrderLine_ID AND io.DocStatus IN ('IP','WC'))";
            //	Deadlock Prevention - Order by M_Product_ID
            MDDOrderLine[] lines = order.getLines(where, "M_Product_ID");
            for (int i = 0; i < lines.length; i++) {
                MDDOrderLine line = lines[i];
                MLocator l = new MLocator(getCtx(), line.getM_Locator_ID(), get_TrxName());
                if (l.getM_Warehouse_ID() != p_M_Warehouse_ID)
                    continue;
                log.fine("check: " + line);
                BigDecimal onHand = Env.ZERO;
                //BigDecimal toDeliver = line.getQtyOrdered()
                //.subtract(line.getQtyDelivered());
                BigDecimal toDeliver = line.getConfirmedQty();
                MProduct product = line.getProduct();
                //	Nothing to Deliver
                if (product != null && toDeliver.signum() == 0)
                    continue;
                // or it's a charge - Bug#: 1603966 
                if (line.getC_Charge_ID() != 0 && toDeliver.signum() == 0)
                    continue;
                //	Check / adjust for confirmations
                BigDecimal unconfirmedShippedQty = Env.ZERO;
                if (p_IsUnconfirmedInOut && product != null && toDeliver.signum() != 0) {
                    String where2 = "EXISTS (SELECT * FROM M_Movement io WHERE io.M_Movement_ID=M_MovementLine.M_Movement_ID AND io.DocStatus IN ('IP','WC'))";
                    MMovementLine[] iols = MMovementLine.getOfOrderLine(getCtx(), line.getDD_OrderLine_ID(), where2, null);
                    for (int j = 0; j < iols.length; j++) unconfirmedShippedQty = unconfirmedShippedQty.add(iols[j].getMovementQty());
                    String logInfo = "Unconfirmed Qty=" + unconfirmedShippedQty + " - ToDeliver=" + toDeliver + "->";
                    toDeliver = toDeliver.subtract(unconfirmedShippedQty);
                    logInfo += toDeliver;
                    if (toDeliver.signum() < 0) {
                        toDeliver = Env.ZERO;
                        logInfo += " (set to 0)";
                    }
                    //	Adjust On Hand
                    onHand = onHand.subtract(unconfirmedShippedQty);
                    log.fine(logInfo);
                }
                //	Comments & lines w/o product & services
                if ((product == null || !product.isStocked()) && (//	comments
                line.getQtyOrdered().signum() == 0 || //	lines w/o product
                toDeliver.signum() != 0)) {
                    if (//	printed later
                    !MDDOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule()))
                        createLine(order, line, toDeliver, null, false);
                    continue;
                }
                //	Stored Product
                MProductCategory pc = MProductCategory.get(order.getCtx(), product.getM_Product_Category_ID());
                String MMPolicy = pc.getMMPolicy();
                if (MMPolicy == null || MMPolicy.length() == 0)
                    MMPolicy = client.getMMPolicy();
                //
                MStorage[] storages = getStorages(l.getM_Warehouse_ID(), line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), product.getM_AttributeSet_ID(), line.getM_AttributeSetInstance_ID() == 0, minGuaranteeDate, MClient.MMPOLICY_FiFo.equals(MMPolicy));
                for (int j = 0; j < storages.length; j++) {
                    MStorage storage = storages[j];
                    onHand = onHand.add(storage.getQtyOnHand());
                }
                boolean fullLine = onHand.compareTo(toDeliver) >= 0 || toDeliver.signum() < 0;
                //	Complete Order
                if (completeOrder && !fullLine) {
                    log.fine("Failed CompleteOrder - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + " - " + line);
                    completeOrder = false;
                    break;
                } else //	Complete Line
                if (fullLine && MDDOrder.DELIVERYRULE_CompleteLine.equals(order.getDeliveryRule())) {
                    log.fine("CompleteLine - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + ", ToDeliver=" + toDeliver + " - " + line);
                    //	
                    createLine(order, line, toDeliver, storages, false);
                } else //	Availability
                if (MDDOrder.DELIVERYRULE_Availability.equals(order.getDeliveryRule()) && (onHand.signum() > 0 || toDeliver.signum() < 0)) {
                    BigDecimal deliver = toDeliver;
                    if (deliver.compareTo(onHand) > 0)
                        deliver = onHand;
                    log.fine("Available - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + ", Delivering=" + deliver + " - " + line);
                    //	
                    createLine(order, line, deliver, storages, false);
                } else //	Force
                if (MDDOrder.DELIVERYRULE_Force.equals(order.getDeliveryRule())) {
                    BigDecimal deliver = toDeliver;
                    log.fine("Force - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + ", Delivering=" + deliver + " - " + line);
                    //	
                    createLine(order, line, deliver, storages, true);
                } else //	Manual
                if (MDDOrder.DELIVERYRULE_Manual.equals(order.getDeliveryRule()))
                    log.fine("Manual - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + ") - " + line);
                else
                    log.fine("Failed: " + order.getDeliveryRule() + " - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + " - " + line);
            }
            //	Complete Order successful
            if (completeOrder && MDDOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule())) {
                for (int i = 0; i < lines.length; i++) {
                    MDDOrderLine line = lines[i];
                    MLocator l = new MLocator(getCtx(), line.getM_Locator_ID(), get_TrxName());
                    if (l.getM_Warehouse_ID() != p_M_Warehouse_ID)
                        continue;
                    MProduct product = line.getProduct();
                    BigDecimal toDeliver = line.getQtyOrdered().subtract(line.getQtyDelivered());
                    //
                    MStorage[] storages = null;
                    if (product != null && product.isStocked()) {
                        MProductCategory pc = MProductCategory.get(order.getCtx(), product.getM_Product_Category_ID());
                        String MMPolicy = pc.getMMPolicy();
                        if (MMPolicy == null || MMPolicy.length() == 0)
                            MMPolicy = client.getMMPolicy();
                        //
                        storages = getStorages(l.getM_Warehouse_ID(), line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), product.getM_AttributeSet_ID(), line.getM_AttributeSetInstance_ID() == 0, minGuaranteeDate, MClient.MMPOLICY_FiFo.equals(MMPolicy));
                    }
                    //	
                    createLine(order, line, toDeliver, storages, false);
                }
            }
            m_line += 1000;
        }
        //	while order
        rs.close();
        pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        log.log(Level.SEVERE, m_sql, e);
    }
    try {
        if (pstmt != null)
            pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        pstmt = null;
    }
    completeMovement();
    return "@Created@ = " + m_created;
}
Also used : MProduct(org.compiere.model.MProduct) Timestamp(java.sql.Timestamp) MStorage(org.compiere.model.MStorage) BigDecimal(java.math.BigDecimal) AdempiereException(org.adempiere.exceptions.AdempiereException) MClient(org.compiere.model.MClient) MDDOrderLine(org.eevolution.model.MDDOrderLine) MProductCategory(org.compiere.model.MProductCategory) MLocator(org.compiere.model.MLocator) ResultSet(java.sql.ResultSet) MDDOrder(org.eevolution.model.MDDOrder) MMovementLine(org.compiere.model.MMovementLine)

Example 54 with MProduct

use of org.compiere.model.MProduct 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 55 with MProduct

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

the class VOrderReceiptIssue method vetoableChange.

public void vetoableChange(PropertyChangeEvent e) throws PropertyVetoException {
    String name = e.getPropertyName();
    Object value = e.getNewValue();
    log.fine("VOrderReceip.vetoableChange - " + name + "=" + value);
    if (value == null)
        return;
    // PP_Order_ID
    if (name.equals("PP_Order_ID")) {
        orderField.setValue(value);
        MPPOrder pp_order = getPP_Order();
        if (pp_order != null) {
            setS_Resource_ID(pp_order.getS_Resource_ID());
            setM_Warehouse_ID(pp_order.getM_Warehouse_ID());
            setDeliveredQty(pp_order.getQtyDelivered());
            setOrderedQty(pp_order.getQtyOrdered());
            // m_PP_order.getQtyOrdered().subtract(m_PP_order.getQtyDelivered());
            setQtyBatchs(pp_order.getQtyBatchs());
            setQtyBatchSize(pp_order.getQtyBatchSize());
            setOpenQty(pp_order.getQtyOrdered().subtract(pp_order.getQtyDelivered()));
            setToDeliverQty(getOpenQty());
            setM_Product_ID(pp_order.getM_Product_ID());
            MProduct m_product = MProduct.get(Env.getCtx(), pp_order.getM_Product_ID());
            setC_UOM_ID(m_product.getC_UOM_ID());
            setOrder_UOM_ID(pp_order.getC_UOM_ID());
            // Default ASI defined from the Parent BOM Order
            setM_AttributeSetInstance_ID(pp_order.getMPPOrderBOM().getM_AttributeSetInstance_ID());
            // default to first entry -
            pickcombo.setSelectedIndex(0);
        // isBackflush
        }
    }
// PP_Order_ID
}
Also used : MProduct(org.compiere.model.MProduct) MPPOrder(org.eevolution.model.MPPOrder)

Aggregations

MProduct (org.compiere.model.MProduct)180 BigDecimal (java.math.BigDecimal)70 AdempiereException (org.adempiere.exceptions.AdempiereException)30 ArrayList (java.util.ArrayList)29 Query (org.compiere.model.Query)29 ResultSet (java.sql.ResultSet)19 MLocator (org.compiere.model.MLocator)18 PreparedStatement (java.sql.PreparedStatement)17 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)17 KeyNamePair (org.compiere.util.KeyNamePair)17 MCostElement (org.compiere.model.MCostElement)16 MStorage (org.compiere.model.MStorage)16 MAcctSchema (org.compiere.model.MAcctSchema)14 MCost (org.compiere.model.MCost)14 MPPProductBOM (org.eevolution.model.MPPProductBOM)14 MPPProductBOMLine (org.eevolution.model.MPPProductBOMLine)14 SQLException (java.sql.SQLException)13 Timestamp (java.sql.Timestamp)13 MAttributeSetInstance (org.compiere.model.MAttributeSetInstance)13 Vector (java.util.Vector)12