Search in sources :

Example 16 with MStorage

use of org.compiere.model.MStorage 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 17 with MStorage

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

the class StorageEngine method checkMaterialPolicy.

private static void checkMaterialPolicy(IDocumentLine line, String MovementType, Timestamp MovementDate, int M_Warehouse_ID) {
    deleteMA(line);
    //	Incoming Trx
    //	V+ Vendor Receipt
    boolean incomingTrx = MovementType.charAt(1) == '+';
    MProduct product = MProduct.get(line.getCtx(), line.getM_Product_ID());
    //	Need to have Location
    if (line.getM_Locator_ID() == 0) {
    //MWarehouse w = MWarehouse.get(getCtx(), getM_Warehouse_ID());
    //line.setM_Warehouse_ID(M_Warehouse_ID);
    //line.setM_Locator_ID(getM_Locator_ID(line.getCtx(),line.getM_Warehouse_ID(), line.getM_Product_ID(),line.getM_AttributeSetInstance_ID(), incomingTrx ? Env.ZERO : line.getMovementQty(), line.get_TrxName()));
    }
    //  Create an  Attribute Set Instance to any receipt FIFO/LIFO
    if (line.getM_AttributeSetInstance_ID() == 0) {
        //Validate Transaction
        if (incomingTrx) {
            MAttributeSetInstance asi = null;
            //auto balance negative on hand
            MStorage[] storages = MStorage.getWarehouse(line.getCtx(), M_Warehouse_ID, line.getM_Product_ID(), 0, null, MClient.MMPOLICY_FiFo.equals(product.getMMPolicy()), false, line.getM_Locator_ID(), line.get_TrxName());
            for (MStorage storage : storages) {
                if (storage.getQtyOnHand().signum() < 0) {
                    asi = new MAttributeSetInstance(line.getCtx(), storage.getM_AttributeSetInstance_ID(), line.get_TrxName());
                    break;
                }
            }
            //always create asi so fifo/lifo work.
            if (asi == null) {
                asi = MAttributeSetInstance.create(line.getCtx(), product, line.get_TrxName());
            }
            line.setM_AttributeSetInstance_ID(asi.getM_AttributeSetInstance_ID());
            log.config("New ASI=" + line);
            createMA(line, line.getM_AttributeSetInstance_ID(), line.getMovementQty());
        } else // Create consume the Attribute Set Instance using policy FIFO/LIFO			
        {
            String MMPolicy = product.getMMPolicy();
            Timestamp minGuaranteeDate = MovementDate;
            MStorage[] storages = MStorage.getWarehouse(line.getCtx(), M_Warehouse_ID, line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), minGuaranteeDate, MClient.MMPOLICY_FiFo.equals(MMPolicy), true, line.getM_Locator_ID(), line.get_TrxName());
            BigDecimal qtyToDeliver = line.getMovementQty();
            for (MStorage storage : storages) {
                if (storage.getQtyOnHand().compareTo(qtyToDeliver) >= 0) {
                    createMA(line, storage.getM_AttributeSetInstance_ID(), qtyToDeliver);
                    qtyToDeliver = Env.ZERO;
                } else {
                    createMA(line, storage.getM_AttributeSetInstance_ID(), storage.getQtyOnHand());
                    qtyToDeliver = qtyToDeliver.subtract(storage.getQtyOnHand());
                    log.fine("QtyToDeliver=" + qtyToDeliver);
                }
                if (qtyToDeliver.signum() == 0)
                    break;
            }
            if (qtyToDeliver.signum() != 0) {
                //deliver using new asi
                MAttributeSetInstance asi = MAttributeSetInstance.create(line.getCtx(), product, line.get_TrxName());
                createMA(line, asi.getM_AttributeSetInstance_ID(), qtyToDeliver);
            }
        }
    //	outgoing Trx
    } else {
        if (incomingTrx) {
            ;
        } else {
            createMA(line, line.getM_AttributeSetInstance_ID(), line.getMovementQty());
        }
    }
    save(line);
}
Also used : MProduct(org.compiere.model.MProduct) MAttributeSetInstance(org.compiere.model.MAttributeSetInstance) MStorage(org.compiere.model.MStorage) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal)

Example 18 with MStorage

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

the class SB_InOutGenerateFromOrderLine method createLine.

//	generate
/**************************************************************************
	 * 	Create Line
	 *	@param order order
	 *	@param orderLine line
	 *	@param qty qty
	 *	@param storages storage info
	 *	@param force force delivery
	 */
private void createLine(MOrder order, MOrderLine orderLine, BigDecimal qty, MStorage[] storages, boolean force) {
    //	Complete last Shipment - can have multiple shipments
    if (m_lastC_BPartner_Location_ID != orderLine.getC_BPartner_Location_ID())
        completeShipment();
    m_lastC_BPartner_Location_ID = orderLine.getC_BPartner_Location_ID();
    //	Create New Shipment
    if (m_shipment == null) {
        m_shipment = new MInOut(order, 0, m_movementDate);
        if (p_C_DocType_ID != 0)
            m_shipment.setC_DocType_ID(p_C_DocType_ID);
        //	sets Org too
        m_shipment.setM_Warehouse_ID(orderLine.getM_Warehouse_ID());
        if (order.getC_BPartner_ID() != orderLine.getC_BPartner_ID())
            m_shipment.setC_BPartner_ID(orderLine.getC_BPartner_ID());
        if (order.getC_BPartner_Location_ID() != orderLine.getC_BPartner_Location_ID())
            m_shipment.setC_BPartner_Location_ID(orderLine.getC_BPartner_Location_ID());
        if (!m_shipment.save())
            throw new IllegalStateException("Could not create Shipment");
    }
    //	Non Inventory Lines
    if (storages == null) {
        MInOutLine line = new MInOutLine(m_shipment);
        line.setOrderLine(orderLine, 0, Env.ZERO);
        //	Correct UOM
        line.setQty(qty);
        if (orderLine.getQtyEntered().compareTo(orderLine.getQtyOrdered()) != 0)
            line.setQtyEntered(qty.multiply(orderLine.getQtyEntered()).divide(orderLine.getQtyOrdered(), 12, BigDecimal.ROUND_HALF_UP));
        line.setLine(m_line + orderLine.getLine());
        if (!line.save())
            throw new IllegalStateException("Could not create Shipment Line");
        log.fine(line.toString());
        return;
    }
    //	Inventory Lines
    ArrayList<MInOutLine> list = new ArrayList<MInOutLine>();
    MProduct product = (MProduct) orderLine.getM_Product();
    BigDecimal toDeliver = qty;
    for (int i = 0; i < storages.length; i++) {
        MStorage storage = storages[i];
        BigDecimal deliver = toDeliver;
        //skip negative storage record
        if (storage.getQtyOnHand().signum() < 0)
            continue;
        //	Not enough On Hand
        if (deliver.compareTo(storage.getQtyOnHand()) > 0 && //	positive storage
        storage.getQtyOnHand().signum() >= 0) {
            if (//	Adjust to OnHand Qty  
            !force || //	if force not on last location
            (force && i + 1 != storages.length))
                deliver = storage.getQtyOnHand();
        }
        if (//	zero deliver
        deliver.signum() == 0)
            continue;
        int M_Locator_ID = storage.getM_Locator_ID();
        //
        MInOutLine line = null;
        if (//      find line with Locator
        orderLine.getM_AttributeSetInstance_ID() == 0) {
            for (int ll = 0; ll < list.size(); ll++) {
                MInOutLine test = (MInOutLine) list.get(ll);
                if (test.getM_Locator_ID() == M_Locator_ID && test.getM_AttributeSetInstance_ID() == 0) {
                    line = test;
                    break;
                }
            }
        }
        if (//	new line
        line == null) {
            line = new MInOutLine(m_shipment);
            line.setOrderLine(orderLine, M_Locator_ID, order.isSOTrx() ? deliver : Env.ZERO);
            line.setQty(deliver);
            if (product != null && product.isASIMandatory(order.isSOTrx(), line.getAD_Org_ID()))
                line.setM_AttributeSetInstance_ID(storage.getM_AttributeSetInstance_ID());
            list.add(line);
        } else
            //	existing line
            line.setQty(line.getMovementQty().add(deliver));
        if (orderLine.getQtyEntered().compareTo(orderLine.getQtyOrdered()) != 0)
            line.setQtyEntered(line.getMovementQty().multiply(orderLine.getQtyEntered()).divide(orderLine.getQtyOrdered(), 12, BigDecimal.ROUND_HALF_UP));
        line.setLine(m_line + orderLine.getLine());
        if (!line.save())
            throw new IllegalStateException("Could not create Shipment Line");
        log.fine("ToDeliver=" + qty + "/" + deliver + " - " + line);
        toDeliver = toDeliver.subtract(deliver);
        //      Temp adjustment, actual update happen in MInOut.completeIt
        storage.setQtyOnHand(storage.getQtyOnHand().subtract(deliver));
        //
        if (toDeliver.signum() == 0)
            break;
    }
    if (toDeliver.signum() != 0) {
        if (!force) {
            throw new IllegalStateException("Not All Delivered - Remainder=" + toDeliver);
        } else {
            MInOutLine line = new MInOutLine(m_shipment);
            line.setOrderLine(orderLine, 0, order.isSOTrx() ? toDeliver : Env.ZERO);
            line.setQty(toDeliver);
            if (!line.save())
                throw new IllegalStateException("Could not create Shipment Line");
        }
    }
}
Also used : MInOut(org.compiere.model.MInOut) MProduct(org.compiere.model.MProduct) MInOutLine(org.compiere.model.MInOutLine) ArrayList(java.util.ArrayList) MStorage(org.compiere.model.MStorage) BigDecimal(java.math.BigDecimal)

Example 19 with MStorage

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

the class InOutGenerate method generate.

//	doIt
/**
	 * 	Generate Shipments
	 * 	@param pstmt Order Query
	 *	@return info
	 */
private String generate(PreparedStatement pstmt) {
    try {
        ResultSet rs = pstmt.executeQuery();
        while (//	Order
        rs.next()) {
            MOrder order = new MOrder(getCtx(), rs, get_TrxName());
            //	New Header different Shipper, Shipment Location
            if (!p_ConsolidateDocument || (m_shipment != null && (m_shipment.getC_BPartner_Location_ID() != order.getC_BPartner_Location_ID() || m_shipment.getM_Shipper_ID() != order.getM_Shipper_ID())))
                completeShipment();
            log.fine("check: " + order + " - DeliveryRule=" + order.getDeliveryRule());
            //
            Timestamp minGuaranteeDate = m_movementDate;
            boolean completeOrder = MOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule());
            //	OrderLine WHERE
            String where = " AND M_Warehouse_ID=" + p_M_Warehouse_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 (!MOrder.DELIVERYRULE_Force.equals(order.getDeliveryRule()))
                where += " AND (C_OrderLine.M_Product_ID IS NULL" + " OR EXISTS (SELECT * FROM M_Product p " + "WHERE C_OrderLine.M_Product_ID=p.M_Product_ID" + " AND IsExcludeAutoDelivery='N'))";
            //	Exclude Unconfirmed
            if (!p_IsUnconfirmedInOut)
                where += " AND NOT EXISTS (SELECT * FROM M_InOutLine iol" + " INNER JOIN M_InOut io ON (iol.M_InOut_ID=io.M_InOut_ID) " + "WHERE iol.C_OrderLine_ID=C_OrderLine.C_OrderLine_ID AND io.DocStatus IN ('IP','WC'))";
            //	Deadlock Prevention - Order by M_Product_ID
            MOrderLine[] lines = order.getLines(where, "C_BPartner_Location_ID, M_Product_ID");
            for (int i = 0; i < lines.length; i++) {
                MOrderLine line = lines[i];
                if (line.getM_Warehouse_ID() != p_M_Warehouse_ID)
                    continue;
                log.fine("check: " + line);
                BigDecimal onHand = Env.ZERO;
                BigDecimal toDeliver = line.getQtyOrdered().subtract(line.getQtyDelivered());
                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_InOut io WHERE io.M_InOut_ID=M_InOutLine.M_InOut_ID AND io.DocStatus IN ('IP','WC'))";
                    MInOutLine[] iols = MInOutLine.getOfOrderLine(getCtx(), line.getC_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
                    !MOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule()))
                        createLine(order, line, toDeliver, null, false);
                    continue;
                }
                //	Stored Product
                String MMPolicy = product.getMMPolicy();
                MStorage[] storages = getStorages(line.getM_Warehouse_ID(), line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), 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 && MOrder.DELIVERYRULE_CompleteLine.equals(order.getDeliveryRule())) {
                    log.fine("CompleteLine - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + ", ToDeliver=" + toDeliver + " - " + line);
                    //	
                    createLine(order, line, toDeliver, storages, false);
                } else //	Availability
                if (MOrder.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 (MOrder.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 (MOrder.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 && MOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule())) {
                for (int i = 0; i < lines.length; i++) {
                    MOrderLine line = lines[i];
                    if (line.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()) {
                        String MMPolicy = product.getMMPolicy();
                        storages = getStorages(line.getM_Warehouse_ID(), line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), 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;
    }
    completeShipment();
    return "@Created@ = " + m_created;
}
Also used : MProduct(org.compiere.model.MProduct) MInOutLine(org.compiere.model.MInOutLine) Timestamp(java.sql.Timestamp) MStorage(org.compiere.model.MStorage) BigDecimal(java.math.BigDecimal) MOrder(org.compiere.model.MOrder) ResultSet(java.sql.ResultSet) MOrderLine(org.compiere.model.MOrderLine)

Example 20 with MStorage

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

the class InventoryCountUpdate method updateWithMA.

//	doIt
/**
	 * 	Update Inventory Lines With Material Allocation
	 *	@return no updated
	 */
private int updateWithMA() {
    int no = 0;
    //
    String sql = "SELECT * FROM M_InventoryLine WHERE M_Inventory_ID=? AND M_AttributeSetInstance_ID=0";
    PreparedStatement preparedStatement = null;
    try {
        preparedStatement = DB.prepareStatement(sql, get_TrxName());
        preparedStatement.setInt(1, getRecord_ID());
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            MInventoryLine inventoryLine = new MInventoryLine(getCtx(), resultSet, get_TrxName());
            BigDecimal onHand = Env.ZERO;
            MStorage[] storages = MStorage.getAll(getCtx(), inventoryLine.getM_Product_ID(), inventoryLine.getM_Locator_ID(), get_TrxName());
            MInventoryLineMA inventoryLineMA = null;
            for (int i = 0; i < storages.length; i++) {
                MStorage storage = storages[i];
                if (storage.getQtyOnHand().signum() == 0)
                    continue;
                onHand = onHand.add(storage.getQtyOnHand());
                //	No ASI
                if (storage.getM_AttributeSetInstance_ID() == 0 && storages.length == 1)
                    continue;
                //	Save ASI
                inventoryLineMA = new MInventoryLineMA(inventoryLine, storage.getM_AttributeSetInstance_ID(), storage.getQtyOnHand());
                if (!inventoryLineMA.save())
                    ;
            }
            inventoryLine.setQtyBook(onHand);
            if (isUpdateCountQty())
                inventoryLine.setQtyCount(onHand);
            if (inventoryLine.save())
                no++;
        }
        resultSet.close();
        preparedStatement.close();
        preparedStatement = null;
    } catch (Exception e) {
        log.log(Level.SEVERE, sql, e);
    }
    try {
        if (preparedStatement != null)
            preparedStatement.close();
        preparedStatement = null;
    } catch (Exception e) {
        preparedStatement = null;
    }
    //
    log.info("#" + no);
    return no;
}
Also used : MInventoryLineMA(org.compiere.model.MInventoryLineMA) MInventoryLine(org.compiere.model.MInventoryLine) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MStorage(org.compiere.model.MStorage) BigDecimal(java.math.BigDecimal)

Aggregations

MStorage (org.compiere.model.MStorage)33 BigDecimal (java.math.BigDecimal)27 MProduct (org.compiere.model.MProduct)16 ArrayList (java.util.ArrayList)11 Timestamp (java.sql.Timestamp)9 MLocator (org.compiere.model.MLocator)8 MMovementLine (org.compiere.model.MMovementLine)7 ResultSet (java.sql.ResultSet)6 AdempiereException (org.adempiere.exceptions.AdempiereException)5 MInOutLine (org.compiere.model.MInOutLine)5 PreparedStatement (java.sql.PreparedStatement)4 MClient (org.compiere.model.MClient)4 MMovement (org.compiere.model.MMovement)4 MOrderLine (org.compiere.model.MOrderLine)4 MWarehouse (org.compiere.model.MWarehouse)4 KeyNamePair (org.compiere.util.KeyNamePair)4 MDDOrderLine (org.eevolution.model.MDDOrderLine)4 MAttributeSet (org.compiere.model.MAttributeSet)3 MAttributeSetInstance (org.compiere.model.MAttributeSetInstance)3 MInOut (org.compiere.model.MInOut)3