Search in sources :

Example 11 with MStorage

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

the class SB_InOutGenerateFromOrderLine method generate.

//	doIt
/**
	 * 	Generate Shipments
	 * 	@param pstmt Order Query
	 *	@return info
	 */
private String generate(MOrder order) {
    try {
        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 = "";
        //	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'))";
        //	Deadlock Prevention - Order by M_Product_ID
        MOrderLine[] lines = order.getLines(where, "C_BPartner_Location_ID, M_Product_ID");
        for (MOrderLine oLine : lines) {
            if (!getSelectionKeys().contains(oLine.getC_OrderLine_ID()))
                continue;
            log.fine("check: " + oLine);
            BigDecimal onHand = Env.ZERO;
            BigDecimal toDeliver = getQtyToDeliver(oLine);
            if (toDeliver.compareTo(Env.ZERO) == 0)
                continue;
            MProduct product = oLine.getProduct();
            //	Nothing to Deliver
            if (product != null && toDeliver.signum() == 0)
                continue;
            // or it's a charge - Bug#: 1603966 
            if (oLine.getC_Charge_ID() != 0 && toDeliver.signum() == 0)
                continue;
            //	Check / adjust for confirmations
            BigDecimal unconfirmedShippedQty = Env.ZERO;
            //	Comments & lines w/o product & services
            if ((product == null || !product.isStocked()) && (//	comments
            oLine.getQtyOrdered().signum() == 0 || //	lines w/o product
            toDeliver.signum() != 0)) {
                if (//	printed later
                !MOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule()))
                    createLine(order, oLine, toDeliver, null, false);
                continue;
            }
            //	Stored Product
            String MMPolicy = product.getMMPolicy();
            MStorage[] storages = getStorages(oLine.getM_Warehouse_ID(), oLine.getM_Product_ID(), oLine.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 + " - " + oLine);
                completeOrder = false;
                break;
            } else //	Complete Line
            if (fullLine && MOrder.DELIVERYRULE_CompleteLine.equals(order.getDeliveryRule())) {
                log.fine("CompleteLine - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + ", ToDeliver=" + toDeliver + " - " + oLine);
                //	
                createLine(order, oLine, 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 + " - " + oLine);
                //	
                createLine(order, oLine, 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 + " - " + oLine);
                //	
                createLine(order, oLine, deliver, storages, true);
            } else //	Manual
            if (MOrder.DELIVERYRULE_Manual.equals(order.getDeliveryRule()))
                log.fine("Manual - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + ") - " + oLine);
            else
                log.fine("Failed: " + order.getDeliveryRule() + " - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + " - " + oLine);
        }
        //	Complete Order successful
        if (completeOrder && MOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule())) {
            for (MOrderLine oLine : lines) {
                MProduct product = oLine.getProduct();
                BigDecimal toDeliver = oLine.getQtyOrdered().subtract(oLine.getQtyDelivered());
                //
                MStorage[] storages = null;
                if (product != null && product.isStocked()) {
                    String MMPolicy = product.getMMPolicy();
                    storages = getStorages(oLine.getM_Warehouse_ID(), oLine.getM_Product_ID(), oLine.getM_AttributeSetInstance_ID(), minGuaranteeDate, MClient.MMPOLICY_FiFo.equals(MMPolicy));
                }
                //	
                createLine(order, oLine, toDeliver, storages, false);
            }
        }
        m_line += 1000;
    } catch (Exception e) {
        log.log(Level.SEVERE, m_sql, e);
    }
    completeShipment();
    return "@Created@ = " + m_created;
}
Also used : MProduct(org.compiere.model.MProduct) MOrderLine(org.compiere.model.MOrderLine) Timestamp(java.sql.Timestamp) MStorage(org.compiere.model.MStorage) BigDecimal(java.math.BigDecimal)

Example 12 with MStorage

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

the class HTMLMessenger method getStorageInfo.

public String getStorageInfo(MProduct p, MAttributeSetInstance asi) {
    StorageReasoner mr = new StorageReasoner();
    int[] ids = mr.getPOIDs(MLocator.Table_Name, null, null);
    MWarehouse warehouse = null;
    MStorage storage = null;
    MLocator locator = null;
    StringBuffer sb = new StringBuffer(STORAGE_HEADER_INFO_PATTERN);
    Object[] obj = null;
    BigDecimal sumQtyOnHand = BigDecimal.ZERO;
    BigDecimal sumQtyReserved = BigDecimal.ZERO;
    BigDecimal sumQtyOrdered = BigDecimal.ZERO;
    int count = 0;
    for (int i = 0; i < ids.length; i++) {
        storage = MStorage.get(Env.getCtx(), ids[i], p.get_ID(), asi.get_ID(), null);
        if (storage == null) {
            continue;
        }
        count++;
        warehouse = new MWarehouse(Env.getCtx(), storage.getM_Warehouse_ID(), null);
        locator = new MLocator(Env.getCtx(), storage.getM_Locator_ID(), null);
        sumQtyOnHand = sumQtyOnHand.add(storage.getQtyOnHand());
        sumQtyReserved = sumQtyReserved.add(storage.getQtyReserved());
        sumQtyOrdered = sumQtyOrdered.add(storage.getQtyOrdered());
        // the quantities of specific locator
        obj = new Object[] { locator.getX() + " - " + locator.getY() + " - " + locator.getZ(), warehouse.getName(), storage.getQtyOnHand(), storage.getQtyReserved(), storage.getQtyOrdered(), storage.getQtyOnHand().subtract(storage.getQtyReserved()) };
        sb.append(MessageFormat.format(STORAGE_LINE_INFO_PATTERN, obj));
    }
    // the sum of the single quantities, if there is more than one line
    if (count > 1) {
        obj = new Object[] { sumQtyOnHand, sumQtyReserved, sumQtyOrdered, sumQtyOnHand.subtract(sumQtyReserved) };
        sb.append(MessageFormat.format(STORAGE_SUM_LINE_INFO_PATTERN, obj));
    }
    double available = sumQtyOnHand.subtract(sumQtyReserved).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    if (count == 0 || (available <= 0.00d)) {
        sb.append(MessageFormat.format(STORAGE_NOINVENTORY_INFO_PATTERN, obj));
    }
    sb.append(STORAGE_FOOTER_INFO_PATTERN);
    return sb.toString();
}
Also used : MLocator(org.compiere.model.MLocator) StorageReasoner(org.eevolution.model.reasoner.StorageReasoner) MStorage(org.compiere.model.MStorage) MWarehouse(org.compiere.model.MWarehouse) BigDecimal(java.math.BigDecimal)

Example 13 with MStorage

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

the class StorageReasoner method getSumQtyAvailable.

public BigDecimal getSumQtyAvailable(MProduct p, MAttributeSetInstance asi) {
    int[] ids = getPOIDs(MLocator.Table_Name, null, null);
    MStorage storage = null;
    Object[] obj = null;
    BigDecimal sumQtyAvailable = BigDecimal.ZERO;
    int count = 0;
    for (int i = 0; i < ids.length; i++) {
        storage = MStorage.get(Env.getCtx(), ids[i], p.get_ID(), asi.get_ID(), null);
        if (storage == null) {
            continue;
        }
        count++;
        sumQtyAvailable = sumQtyAvailable.add(storage.getQtyOnHand().subtract(storage.getQtyReserved()));
    }
    return sumQtyAvailable;
}
Also used : MStorage(org.compiere.model.MStorage) BigDecimal(java.math.BigDecimal)

Example 14 with MStorage

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

the class InOutGenerate 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);
        //	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>();
    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);
            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) MInOutLine(org.compiere.model.MInOutLine) ArrayList(java.util.ArrayList) MStorage(org.compiere.model.MStorage) BigDecimal(java.math.BigDecimal)

Example 15 with MStorage

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

the class ScanBar method createLine.

public void createLine() {
    int lineNo = DB.getSQLValueEx(null, "SELECT Line FROM " + tableLine.getTableName() + " WHERE " + table.getTableName() + "_ID=?", getRecord_ID());
    if (lineNo <= 0)
        lineNo = 10;
    for (Vector<Object> line : getData().values()) {
        String value = (String) line.get(0);
        String lotNo = (String) line.get(2);
        String serNo = (String) line.get(3);
        Boolean isASI = (lotNo != null && lotNo.length() > 0) || (serNo != null && serNo.length() > 0) ? true : false;
        BigDecimal qty = (BigDecimal) line.get(4);
        int id = (Integer) line.get(5);
        Integer referenceId = (Integer) line.get(6);
        PO poLine = null;
        MAttributeSetInstance asi = null;
        MProduct product = new Query(Env.getCtx(), I_M_Product.Table_Name, "Value = ? ", null).setClient_ID().setParameters(value).firstOnly();
        String desc = null;
        poLine = tableLine.getPO(id, null);
        if (product.getM_AttributeSet_ID() > 0 && isASI) {
            if (poLine != null && poLine.get_ValueAsInt(I_M_AttributeSetInstance.COLUMNNAME_M_AttributeSetInstance_ID) > 0)
                asi = new MAttributeSetInstance(Env.getCtx(), poLine.get_ValueAsInt(I_M_AttributeSetInstance.COLUMNNAME_M_AttributeSetInstance_ID), null);
            else
                asi = getAttributeSetInstance(product, lotNo, serNo, getM_Locater_ID(), null);
        }
        poLine.set_ValueOfColumn(table.getKeyColumns()[0], getRecord_ID());
        poLine.set_ValueOfColumn(I_M_Product.COLUMNNAME_M_Product_ID, product.get_ID());
        poLine.set_ValueOfColumn(I_M_Product.COLUMNNAME_C_UOM_ID, product.getC_UOM_ID());
        poLine.set_ValueOfColumn(I_M_InOutLine.COLUMNNAME_Line, lineNo);
        poLine.set_ValueOfColumn(I_M_InOutLine.COLUMNNAME_IsActive, true);
        int locatorColumnId = poLine.get_ColumnIndex(I_M_InOutLine.COLUMNNAME_M_Locator_ID);
        if (locatorColumnId > 0 && getM_Locater_ID() > 0)
            poLine.set_ValueOfColumn(I_M_InOutLine.COLUMNNAME_M_Locator_ID, getM_Locater_ID());
        if (asi == null && isASI) {
            if (asi == null && isASI) {
                asi = new MAttributeSetInstance(Env.getCtx(), 0, product.getM_AttributeSet_ID(), null);
                if (lotNo != null) {
                    asi.setLot(lotNo);
                    desc = lotNo;
                }
                if (serNo != null) {
                    asi.setSerNo(serNo);
                    if (desc != null)
                        desc = desc + " - " + serNo;
                    else
                        desc = serNo;
                }
                asi.setDescription(desc);
                asi.saveEx();
            }
        }
        if (poLine instanceof MInventoryLine) {
            MStorage storage = MStorage.get(Env.getCtx(), getM_Locater_ID(), product.getM_Product_ID(), asi == null ? 0 : asi.getM_AttributeSetInstance_ID(), null);
            poLine.set_CustomColumn(I_M_InventoryLine.COLUMNNAME_QtyCount, qty);
            poLine.set_CustomColumn(I_M_InventoryLine.COLUMNNAME_QtyBook, storage == null ? Env.ZERO : storage.getQtyOnHand());
        } else if (poLine instanceof MInOutLine) {
            MInOutLine ioLine = (MInOutLine) poLine;
            ioLine.setQty(qty);
            ioLine.setC_OrderLine_ID(referenceId);
        } else if (poLine instanceof MMovementLine) {
            MMovementLine movementLine = (MMovementLine) poLine;
            movementLine.setM_LocatorTo_ID(getM_LocaterTo_ID());
            movementLine.setMovementQty(qty);
        } else
            poLine.set_ValueOfColumn(I_M_InOutLine.COLUMNNAME_MovementQty, qty);
        poLine.set_ValueOfColumn(MAttributeSetInstance.COLUMNNAME_M_AttributeSetInstance_ID, asi == null ? 0 : asi.get_ID());
        if (poLine.is_Changed())
            poLine.saveEx();
        lineNo = lineNo + 10;
    }
}
Also used : MProduct(org.compiere.model.MProduct) Query(org.compiere.model.Query) MInventoryLine(org.compiere.model.MInventoryLine) MInOutLine(org.compiere.model.MInOutLine) MAttributeSetInstance(org.compiere.model.MAttributeSetInstance) MStorage(org.compiere.model.MStorage) BigDecimal(java.math.BigDecimal) MMovementLine(org.compiere.model.MMovementLine) PO(org.compiere.model.PO)

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