Search in sources :

Example 31 with MStorage

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

the class MStorageTest method createLocator.

private MLocator createLocator(MWarehouse wh, String locatorValue, double qtyOnHand) {
    MLocator loc = new MLocator(wh, wh.getValue() + "-" + locatorValue);
    loc.setXYZ("X" + locatorValue, "Y" + locatorValue, "Z" + locatorValue);
    loc.saveEx();
    //
    BigDecimal targetQty = BigDecimal.valueOf(qtyOnHand).setScale(12, BigDecimal.ROUND_HALF_UP);
    MStorage s1 = MStorage.getCreate(getCtx(), loc.get_ID(), product_id, 0, getTrxName());
    s1.setQtyOnHand(targetQty);
    s1.saveEx();
    //
    BigDecimal qty = MStorage.getQtyAvailable(wh.get_ID(), loc.get_ID(), product_id, 0, getTrxName()).setScale(12, BigDecimal.ROUND_HALF_UP);
    assertEquals("Error on locator " + locatorValue, targetQty, qty);
    //
    return loc;
}
Also used : MLocator(org.compiere.model.MLocator) MStorage(org.compiere.model.MStorage) BigDecimal(java.math.BigDecimal)

Example 32 with MStorage

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

the class ReplenishReportProduction method createMovements.

//	createRequisition
/**
	 * 	Create Inventory Movements
	 */
private void createMovements() {
    int noMoves = 0;
    String info = "";
    //
    MClient client = null;
    MMovement move = null;
    int M_Warehouse_ID = 0;
    int M_WarehouseSource_ID = 0;
    MWarehouse whSource = null;
    MWarehouse wh = null;
    X_T_Replenish[] replenishs = getReplenish("M_WarehouseSource_ID IS NOT NULL AND C_BPartner_ID > 0");
    for (int i = 0; i < replenishs.length; i++) {
        X_T_Replenish replenish = replenishs[i];
        if (whSource == null || whSource.getM_WarehouseSource_ID() != replenish.getM_WarehouseSource_ID())
            whSource = MWarehouse.get(getCtx(), replenish.getM_WarehouseSource_ID());
        if (wh == null || wh.getM_Warehouse_ID() != replenish.getM_Warehouse_ID())
            wh = MWarehouse.get(getCtx(), replenish.getM_Warehouse_ID());
        if (client == null || client.getAD_Client_ID() != whSource.getAD_Client_ID())
            client = MClient.get(getCtx(), whSource.getAD_Client_ID());
        //
        if (move == null || M_WarehouseSource_ID != replenish.getM_WarehouseSource_ID() || M_Warehouse_ID != replenish.getM_Warehouse_ID()) {
            M_WarehouseSource_ID = replenish.getM_WarehouseSource_ID();
            M_Warehouse_ID = replenish.getM_Warehouse_ID();
            move = new MMovement(getCtx(), 0, get_TrxName());
            move.setC_DocType_ID(p_C_DocType_ID);
            move.setDescription(Msg.getMsg(getCtx(), "Replenishment") + ": " + whSource.getName() + "->" + wh.getName());
            //	Set Org
            move.setAD_Org_ID(whSource.getAD_Org_ID());
            if (!move.save())
                return;
            log.fine(move.toString());
            noMoves++;
            info += " - " + move.getDocumentNo();
        }
        //	To
        int M_LocatorTo_ID = wh.getDefaultLocator().getM_Locator_ID();
        //	From: Look-up Storage
        MProduct product = MProduct.get(getCtx(), replenish.getM_Product_ID());
        String MMPolicy = product.getMMPolicy();
        MStorage[] storages = MStorage.getWarehouse(getCtx(), whSource.getM_Warehouse_ID(), replenish.getM_Product_ID(), 0, 0, true, null, MClient.MMPOLICY_FiFo.equals(MMPolicy), get_TrxName());
        //
        BigDecimal target = replenish.getQtyToOrder();
        for (int j = 0; j < storages.length; j++) {
            MStorage storage = storages[j];
            if (storage.getQtyOnHand().signum() <= 0)
                continue;
            BigDecimal moveQty = target;
            if (storage.getQtyOnHand().compareTo(moveQty) < 0)
                moveQty = storage.getQtyOnHand();
            //
            MMovementLine line = new MMovementLine(move);
            line.setM_Product_ID(replenish.getM_Product_ID());
            line.setMovementQty(moveQty);
            if (replenish.getQtyToOrder().compareTo(moveQty) != 0)
                line.setDescription("Total: " + replenish.getQtyToOrder());
            //	from
            line.setM_Locator_ID(storage.getM_Locator_ID());
            line.setM_AttributeSetInstance_ID(storage.getM_AttributeSetInstance_ID());
            //	to
            line.setM_LocatorTo_ID(M_LocatorTo_ID);
            line.setM_AttributeSetInstanceTo_ID(storage.getM_AttributeSetInstance_ID());
            line.save();
            //
            target = target.subtract(moveQty);
            if (target.signum() == 0)
                break;
        }
    }
    if (replenishs.length == 0) {
        m_info = "No Source Warehouse";
        log.warning(m_info);
    } else {
        m_info = "#" + noMoves + info;
        log.info(m_info);
    }
}
Also used : MProduct(org.compiere.model.MProduct) X_T_Replenish(org.compiere.model.X_T_Replenish) MMovementLine(org.compiere.model.MMovementLine) MMovement(org.compiere.model.MMovement) MStorage(org.compiere.model.MStorage) MWarehouse(org.compiere.model.MWarehouse) BigDecimal(java.math.BigDecimal) MClient(org.compiere.model.MClient)

Example 33 with MStorage

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

the class MovementGenerate method createLine.

//	generate
/**************************************************************************
	 * 	Create Line
	 *	@param Distribution order order
	 *	@param orderLine line
	 *	@param qty qty
	 *	@param storages storage info
	 *	@param force force delivery
	 */
private void createLine(MDDOrder order, MDDOrderLine orderLine, BigDecimal qty, MStorage[] storages, boolean force) {
    //	Complete last Shipment - can have multiple shipments
    if (m_lastC_BPartner_Location_ID != order.getC_BPartner_Location_ID())
        completeMovement();
    m_lastC_BPartner_Location_ID = order.getC_BPartner_Location_ID();
    //	Create New Shipment
    if (m_movement == null) {
        MLocator locator = MLocator.get(getCtx(), orderLine.getM_Locator_ID());
        m_movement = createMovement(order, m_movementDate);
        m_movement.setAD_Org_ID(locator.getAD_Org_ID());
        //m_movement.setM_Warehouse_ID(orderLine.getM_Warehouse_ID());	//	sets Org too
        m_movement.setIsInTransit(true);
        m_movement.setDD_Order_ID(order.getDD_Order_ID());
        if (order.getC_BPartner_ID() != order.getC_BPartner_ID())
            m_movement.setC_BPartner_ID(order.getC_BPartner_ID());
        if (order.getC_BPartner_Location_ID() != order.getC_BPartner_Location_ID())
            m_movement.setC_BPartner_Location_ID(order.getC_BPartner_Location_ID());
        //Look the document type based on organization
        int docTypeDO_ID = getDocType(MDocType.DOCBASETYPE_MaterialMovement, m_movement.getAD_Org_ID());
        if (docTypeDO_ID > 0)
            m_movement.setC_DocType_ID(docTypeDO_ID);
        if (!m_movement.save())
            throw new IllegalStateException("Could not create Movement");
    }
    //	Non Inventory Lines
    if (storages == null) {
        MMovementLine line = new MMovementLine(m_movement);
        line.setOrderLine(orderLine, Env.ZERO, false);
        //	Correct UOM
        line.setMovementQty(qty);
        if (orderLine.getQtyEntered().compareTo(orderLine.getQtyOrdered()) != 0)
            line.setMovementQty(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;
    }
    //	Product
    MProduct product = orderLine.getProduct();
    boolean linePerASI = false;
    if (product.getM_AttributeSet_ID() != 0) {
        MAttributeSet mas = MAttributeSet.get(getCtx(), product.getM_AttributeSet_ID());
        linePerASI = mas.isInstanceAttribute();
    }
    //	Inventory Lines
    ArrayList<MMovementLine> list = new ArrayList<MMovementLine>();
    BigDecimal toDeliver = qty;
    for (int i = 0; i < storages.length; i++) {
        MStorage storage = storages[i];
        BigDecimal deliver = toDeliver;
        //	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();
        //
        MMovementLine line = null;
        if (//	find line with Locator
        !linePerASI) {
            for (int ll = 0; ll < list.size(); ll++) {
                MMovementLine test = (MMovementLine) list.get(ll);
                if (test.getM_Locator_ID() == M_Locator_ID) {
                    line = test;
                    break;
                }
            }
        }
        if (//	new line
        line == null) {
            line = new MMovementLine(m_movement);
            line.setOrderLine(orderLine, deliver, false);
            line.setMovementQty(deliver);
            list.add(line);
        } else
            //	existing line
            line.setMovementQty(line.getMovementQty().add(deliver));
        if (orderLine.getQtyEntered().compareTo(orderLine.getQtyOrdered()) != 0)
            line.setMovementQty(line.getMovementQty().multiply(orderLine.getQtyEntered()).divide(orderLine.getQtyOrdered(), 12, BigDecimal.ROUND_HALF_UP));
        line.setLine(m_line + orderLine.getLine());
        if (linePerASI)
            line.setM_AttributeSetInstance_ID(storage.getM_AttributeSetInstance_ID());
        if (!line.save())
            throw new IllegalStateException("Could not create Shipment Line");
        log.fine("ToDeliver=" + qty + "/" + deliver + " - " + line);
        toDeliver = toDeliver.subtract(deliver);
        //	Temp adjustment
        storage.setQtyOnHand(storage.getQtyOnHand().subtract(deliver));
        //
        if (toDeliver.signum() == 0)
            break;
    }
    if (toDeliver.signum() != 0)
        throw new IllegalStateException("Not All Delivered - Remainder=" + toDeliver);
}
Also used : MProduct(org.compiere.model.MProduct) MLocator(org.compiere.model.MLocator) ArrayList(java.util.ArrayList) MMovementLine(org.compiere.model.MMovementLine) MStorage(org.compiere.model.MStorage) BigDecimal(java.math.BigDecimal) MAttributeSet(org.compiere.model.MAttributeSet)

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