Search in sources :

Example 6 with MLocator

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

the class CalloutDistributionOrder method setLocatorTo.

/**
	 * Set Default Locator To
	 * @param ctx
	 * @param WindowNo
	 * @param mTab
	 * @param mField
	 * @param value
	 * @return
	 */
public String setLocatorTo(Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value) {
    I_DD_OrderLine line = GridTabWrapper.create(mTab, I_DD_OrderLine.class);
    if (value != null) {
        MProduct product = MProduct.get(ctx, (Integer) value);
        if (line.getC_UOM_ID() <= 0) {
            line.setC_UOM_ID(product.getC_UOM_ID());
        }
    }
    MWarehouse[] ws = MWarehouse.getForOrg(ctx, line.getAD_Org_ID());
    if (ws == null && ws.length < 0) {
        return "";
    }
    MLocator locator_to = MLocator.getDefault(ws[0]);
    if (locator_to != null) {
        line.setM_LocatorTo_ID(locator_to.getM_Locator_ID());
    }
    return "";
}
Also used : MProduct(org.compiere.model.MProduct) MLocator(org.compiere.model.MLocator) MWarehouse(org.compiere.model.MWarehouse)

Example 7 with MLocator

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

the class DistributionRun method distributionOrders.

//	insertDetails
/**************************************************************************
	 * 	Create Orders
	 * 	@return true if created
	 */
private boolean distributionOrders() {
    //The Quantity Available is distribute with respect to Distribution Order Demand
    if (p_BasedInDamnd) {
        int M_Warehouse_ID = 0;
        if (p_M_Warehouse_ID <= 0) {
            MOrgInfo oi_source = MOrgInfo.get(getCtx(), m_run.getAD_Org_ID(), get_TrxName());
            MWarehouse m_source = MWarehouse.get(getCtx(), oi_source.getM_Warehouse_ID());
            if (m_source == null)
                throw new AdempiereException("Do not exist Defautl Warehouse Source");
            M_Warehouse_ID = m_source.getM_Warehouse_ID();
        } else
            M_Warehouse_ID = p_M_Warehouse_ID;
        //			For all lines
        for (int i = 0; i < m_details.length; i++) {
            MDistributionRunDetail detail = m_details[i];
            StringBuffer sql = new StringBuffer("SELECT * FROM DD_OrderLine ol INNER JOIN DD_Order o ON (o.DD_Order_ID=ol.DD_Order_ID)  INNER JOIN M_Locator l ON (l.M_Locator_ID=ol.M_Locator_ID) ");
            //sql.append(" WHERE o.DocStatus IN ('DR','IN') AND o.C_BPartner_ID = ? AND M_Product_ID=? AND  l.M_Warehouse_ID=?  AND ol.DatePromised BETWEEN ? AND ? ");
            sql.append(" WHERE o.DocStatus IN ('DR','IN') AND o.C_BPartner_ID = ? AND M_Product_ID=? AND  l.M_Warehouse_ID=?  AND ol.DatePromised <=?");
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            try {
                pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
                pstmt.setInt(1, detail.getC_BPartner_ID());
                pstmt.setInt(2, detail.getM_Product_ID());
                pstmt.setInt(3, M_Warehouse_ID);
                pstmt.setTimestamp(4, p_DatePromised);
                //pstmt.setTimestamp(5, p_DatePromised_To);
                rs = pstmt.executeQuery();
                while (rs.next()) {
                    //	Create Order Line
                    MDDOrderLine line = new MDDOrderLine(getCtx(), rs, get_TrxName());
                    line.setM_Product_ID(detail.getM_Product_ID());
                    line.setConfirmedQty(line.getTargetQty().add(detail.getActualAllocation()));
                    if (p_M_Warehouse_ID > 0)
                        line.setDescription(Msg.translate(getCtx(), "PlannedQty"));
                    else
                        line.setDescription(m_run.getName());
                    line.saveEx();
                    break;
                //addLog(0,null, detail.getActualAllocation(), order.getDocumentNo() 
                //	+ ": " + bp.getName() + " - " + product.getName());
                }
            } catch (Exception e) {
                log.log(Level.SEVERE, "doIt - " + sql, e);
                return false;
            } finally {
                DB.close(rs, pstmt);
                rs = null;
                pstmt = null;
            }
        }
        return true;
    }
    //		Get Counter Org/BP
    int runAD_Org_ID = m_run.getAD_Org_ID();
    if (runAD_Org_ID == 0)
        runAD_Org_ID = Env.getAD_Org_ID(getCtx());
    MOrg runOrg = MOrg.get(getCtx(), runAD_Org_ID);
    int runC_BPartner_ID = runOrg.getLinkedC_BPartner_ID(get_TrxName());
    boolean counter = //	no single Order 
    !m_run.isCreateSingleOrder() && //	Org linked to BP
    runC_BPartner_ID > 0 && //	PO
    !m_docType.isSOTrx();
    MBPartner runBPartner = counter ? new MBPartner(getCtx(), runC_BPartner_ID, get_TrxName()) : null;
    if (!counter || runBPartner == null || runBPartner.get_ID() != runC_BPartner_ID)
        counter = false;
    if (counter)
        log.info("RunBP=" + runBPartner + " - " + m_docType);
    log.info("Single=" + m_run.isCreateSingleOrder() + " - " + m_docType + ",SO=" + m_docType.isSOTrx());
    log.fine("Counter=" + counter + ",C_BPartner_ID=" + runC_BPartner_ID + "," + runBPartner);
    //
    MBPartner bp = null;
    MDDOrder singleOrder = null;
    MProduct product = null;
    MWarehouse m_source = null;
    MLocator m_locator = null;
    MWarehouse m_target = null;
    MLocator m_locator_to = null;
    MWarehouse[] ws = null;
    MOrgInfo oi_source = MOrgInfo.get(getCtx(), m_run.getAD_Org_ID(), get_TrxName());
    m_source = MWarehouse.get(getCtx(), oi_source.getM_Warehouse_ID());
    if (m_source == null)
        throw new AdempiereException("Do not exist Defautl Warehouse Source");
    m_locator = MLocator.getDefault(m_source);
    //get the warehouse in transit
    ws = MWarehouse.getInTransitForOrg(getCtx(), m_source.getAD_Org_ID());
    if (ws == null)
        throw new AdempiereException("Warehouse Intransit do not found");
    //	Consolidated Single Order 
    if (m_run.isCreateSingleOrder()) {
        bp = new MBPartner(getCtx(), m_run.getC_BPartner_ID(), get_TrxName());
        if (bp.get_ID() == 0)
            throw new IllegalArgumentException("Business Partner not found - C_BPartner_ID=" + m_run.getC_BPartner_ID());
        //
        if (!p_IsTest) {
            singleOrder = new MDDOrder(getCtx(), 0, get_TrxName());
            singleOrder.setC_DocType_ID(m_docType.getC_DocType_ID());
            singleOrder.setIsSOTrx(m_docType.isSOTrx());
            singleOrder.setBPartner(bp);
            if (m_run.getC_BPartner_Location_ID() != 0)
                singleOrder.setC_BPartner_Location_ID(m_run.getC_BPartner_Location_ID());
            singleOrder.setDateOrdered(m_DateOrdered);
            singleOrder.setDatePromised(p_DatePromised);
            singleOrder.setM_Warehouse_ID(ws[0].getM_Warehouse_ID());
            if (!singleOrder.save()) {
                log.log(Level.SEVERE, "Order not saved");
                return false;
            }
            m_counter++;
        }
    }
    int lastC_BPartner_ID = 0;
    int lastC_BPartner_Location_ID = 0;
    MDDOrder order = null;
    //	For all lines
    for (int i = 0; i < m_details.length; i++) {
        MDistributionRunDetail detail = m_details[i];
        //	Create Order Header
        if (m_run.isCreateSingleOrder())
            order = singleOrder;
        else //	New Business Partner
        if (lastC_BPartner_ID != detail.getC_BPartner_ID() || lastC_BPartner_Location_ID != detail.getC_BPartner_Location_ID()) {
            //	finish order
            order = null;
        }
        lastC_BPartner_ID = detail.getC_BPartner_ID();
        lastC_BPartner_Location_ID = detail.getC_BPartner_Location_ID();
        bp = new MBPartner(getCtx(), detail.getC_BPartner_ID(), get_TrxName());
        MOrgInfo oi_target = MOrgInfo.get(getCtx(), bp.getAD_OrgBP_ID_Int(), get_TrxName());
        m_target = MWarehouse.get(getCtx(), oi_target.getM_Warehouse_ID());
        if (m_target == null)
            throw new AdempiereException("Do not exist Default Warehouse Target");
        m_locator_to = MLocator.getDefault(m_target);
        if (m_locator == null || m_locator_to == null) {
            throw new AdempiereException("Do not exist default Locator for Warehouses");
        }
        if (p_ConsolidateDocument) {
            String whereClause = "DocStatus IN ('DR','IN') AND AD_Org_ID=" + bp.getAD_OrgBP_ID_Int() + " AND " + MDDOrder.COLUMNNAME_C_BPartner_ID + "=? AND " + MDDOrder.COLUMNNAME_M_Warehouse_ID + "=?  AND " + MDDOrder.COLUMNNAME_DatePromised + "<=? ";
            order = new Query(getCtx(), MDDOrder.Table_Name, whereClause, get_TrxName()).setParameters(new Object[] { lastC_BPartner_ID, ws[0].getM_Warehouse_ID(), p_DatePromised }).setOrderBy(MDDOrder.COLUMNNAME_DatePromised + " DESC").first();
        }
        //	New Order
        if (order == null) {
            if (!p_IsTest) {
                order = new MDDOrder(getCtx(), 0, get_TrxName());
                order.setAD_Org_ID(bp.getAD_OrgBP_ID_Int());
                order.setC_DocType_ID(m_docType.getC_DocType_ID());
                order.setIsSOTrx(m_docType.isSOTrx());
                //	Counter Doc
                if (counter && bp.getAD_OrgBP_ID_Int() > 0) {
                    log.fine("Counter - From_BPOrg=" + bp.getAD_OrgBP_ID_Int() + "-" + bp + ", To_BP=" + runBPartner);
                    order.setAD_Org_ID(bp.getAD_OrgBP_ID_Int());
                    if (ws[0].getM_Warehouse_ID() > 0)
                        order.setM_Warehouse_ID(ws[0].getM_Warehouse_ID());
                    order.setBPartner(runBPartner);
                } else //	normal
                {
                    log.fine("From_Org=" + runAD_Org_ID + ", To_BP=" + bp);
                    order.setAD_Org_ID(bp.getAD_OrgBP_ID_Int());
                    order.setBPartner(bp);
                    if (detail.getC_BPartner_Location_ID() != 0)
                        order.setC_BPartner_Location_ID(detail.getC_BPartner_Location_ID());
                }
                order.setM_Warehouse_ID(ws[0].getM_Warehouse_ID());
                order.setDateOrdered(m_DateOrdered);
                order.setDatePromised(p_DatePromised);
                order.setIsInDispute(false);
                order.setIsInTransit(false);
                if (!order.save()) {
                    log.log(Level.SEVERE, "Order not saved");
                    return false;
                }
            }
        }
        //	Line
        if (product == null || product.getM_Product_ID() != detail.getM_Product_ID())
            product = MProduct.get(getCtx(), detail.getM_Product_ID());
        if (p_IsTest) {
            addLog(0, null, detail.getActualAllocation(), bp.getName() + " - " + product.getName());
            continue;
        }
        if (p_ConsolidateDocument) {
            String sql = "SELECT DD_OrderLine_ID FROM DD_OrderLine ol INNER JOIN DD_Order o ON (o.DD_Order_ID=ol.DD_Order_ID) WHERE o.DocStatus IN ('DR','IN') AND o.C_BPartner_ID = ? AND M_Product_ID=? AND  ol.M_Locator_ID=?  AND ol.DatePromised <= ?";
            int DD_OrderLine_ID = DB.getSQLValueEx(get_TrxName(), sql, new Object[] { detail.getC_BPartner_ID(), product.getM_Product_ID(), m_locator.getM_Locator_ID(), p_DatePromised });
            if (DD_OrderLine_ID <= 0) {
                MDDOrderLine line = new MDDOrderLine(order);
                line.setAD_Org_ID(bp.getAD_OrgBP_ID_Int());
                line.setM_Locator_ID(m_locator.getM_Locator_ID());
                line.setM_LocatorTo_ID(m_locator_to.getM_Locator_ID());
                line.setIsInvoiced(false);
                line.setProduct(product);
                BigDecimal QtyAllocation = detail.getActualAllocation();
                if (QtyAllocation == null)
                    QtyAllocation = Env.ZERO;
                line.setQty(QtyAllocation);
                line.setQtyEntered(QtyAllocation);
                //line.setTargetQty(detail.getActualAllocation());
                line.setTargetQty(Env.ZERO);
                String Description = "";
                if (m_run.getName() != null)
                    Description = Description.concat(m_run.getName());
                line.setDescription(Description + " " + Msg.translate(getCtx(), "Qty") + " = " + QtyAllocation + " ");
                //line.setConfirmedQty(QtyAllocation);
                line.saveEx();
            } else {
                MDDOrderLine line = new MDDOrderLine(getCtx(), DD_OrderLine_ID, get_TrxName());
                BigDecimal QtyAllocation = detail.getActualAllocation();
                if (QtyAllocation == null)
                    QtyAllocation = Env.ZERO;
                String Description = line.getDescription();
                if (Description == null)
                    Description = "";
                if (m_run.getName() != null)
                    Description = Description.concat(m_run.getName());
                line.setDescription(Description + " " + Msg.translate(getCtx(), "Qty") + " = " + QtyAllocation + " ");
                line.setQty(line.getQtyEntered().add(QtyAllocation));
                //line.setConfirmedQty(line.getConfirmedQty().add( QtyAllocation));
                line.saveEx();
            }
        } else {
            //	Create Order Line
            MDDOrderLine line = new MDDOrderLine(order);
            if (counter && bp.getAD_OrgBP_ID_Int() > 0)
                //	don't overwrite counter doc
                ;
            /*else	//	normal - optionally overwrite
				{
					line.setC_BPartner_ID(detail.getC_BPartner_ID());
					if (detail.getC_BPartner_Location_ID() != 0)
						line.setC_BPartner_Location_ID(detail.getC_BPartner_Location_ID());
				}*/
            //
            line.setAD_Org_ID(bp.getAD_OrgBP_ID_Int());
            line.setM_Locator_ID(m_locator.getM_Locator_ID());
            line.setM_LocatorTo_ID(m_locator_to.getM_Locator_ID());
            line.setIsInvoiced(false);
            line.setProduct(product);
            line.setQty(detail.getActualAllocation());
            line.setQtyEntered(detail.getActualAllocation());
            //line.setTargetQty(detail.getActualAllocation());
            line.setTargetQty(Env.ZERO);
            //line.setConfirmedQty(detail.getActualAllocation());
            String Description = "";
            if (m_run.getName() != null)
                Description = Description.concat(m_run.getName());
            line.setDescription(Description + " " + Msg.translate(getCtx(), "Qty") + " = " + detail.getActualAllocation() + " ");
            line.saveEx();
        }
        addLog(0, null, detail.getActualAllocation(), order.getDocumentNo() + ": " + bp.getName() + " - " + product.getName());
    }
    //	finish order
    order = null;
    return true;
}
Also used : MProduct(org.compiere.model.MProduct) Query(org.compiere.model.Query) PreparedStatement(java.sql.PreparedStatement) MBPartner(org.compiere.model.MBPartner) MWarehouse(org.compiere.model.MWarehouse) AdempiereException(org.adempiere.exceptions.AdempiereException) BigDecimal(java.math.BigDecimal) MDistributionRunDetail(org.compiere.model.MDistributionRunDetail) MDDOrderLine(org.eevolution.model.MDDOrderLine) MOrg(org.compiere.model.MOrg) AdempiereException(org.adempiere.exceptions.AdempiereException) MLocator(org.compiere.model.MLocator) MOrgInfo(org.compiere.model.MOrgInfo) ResultSet(java.sql.ResultSet) MDDOrder(org.eevolution.model.MDDOrder)

Example 8 with MLocator

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

the class GenerateInOutBound method createOutBoundOrder.

private MWMInOutBound createOutBoundOrder() {
    MLocator locator = MLocator.get(getCtx(), getLocatorId());
    MWMInOutBound outBoundOrder = new MWMInOutBound(getCtx(), 0, get_TrxName());
    outBoundOrder.setShipDate(getShipDate());
    outBoundOrder.setPickDate(getPickDate());
    if (getOrderReference() != null)
        outBoundOrder.setPOReference(getOrderReference());
    if (getDeliveryRule() != null)
        outBoundOrder.setDeliveryRule(getDeliveryRule());
    if (getDeliveryVia() != null)
        outBoundOrder.setDeliveryViaRule(getDeliveryVia());
    if (getDocumentTypeId() > 0)
        outBoundOrder.setC_DocType_ID(getDocumentTypeId());
    else {
        int docTypeId = MDocType.getDocType(MDocType.DOCBASETYPE_WarehouseManagementOrder);
        if (docTypeId <= 0)
            throw new DocTypeNotFoundException(MDocType.DOCBASETYPE_WarehouseManagementOrder, "");
        else
            outBoundOrder.setC_DocType_ID(docTypeId);
    }
    if (getDocumentAction() != null)
        outBoundOrder.setDocAction(getDocumentAction());
    else
        outBoundOrder.setDocAction(MWMInOutBound.ACTION_Prepare);
    outBoundOrder.setDocStatus(MWMInOutBound.DOCSTATUS_Drafted);
    outBoundOrder.setM_Warehouse_ID(locator.getM_Warehouse_ID());
    outBoundOrder.setIsSOTrx(true);
    outBoundOrder.saveEx();
    return outBoundOrder;
}
Also used : MWMInOutBound(org.eevolution.model.MWMInOutBound) MLocator(org.compiere.model.MLocator) DocTypeNotFoundException(org.adempiere.exceptions.DocTypeNotFoundException)

Example 9 with MLocator

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

the class GenerateShipmentOutBound method createShipment.

/**
	 * Create Shipment to Out Bound Order
	 * @param outBoundLine
	 */
public void createShipment(MWMInOutBoundLine outBoundLine) {
    // Generate Shipment based on Outbound Order
    if (outBoundLine.getC_OrderLine_ID() > 0) {
        MOrderLine orderLine = outBoundLine.getOrderLine();
        if (outBoundLine.getPickedQty().subtract(orderLine.getQtyDelivered()).signum() <= 0 && !isIncludeNotAvailable())
            return;
        MLocator standing = getStandingLocator(outBoundLine);
        BigDecimal qtyDelivered = getQtyDelivered(outBoundLine, orderLine.getQtyDelivered());
        MInOut shipment = getShipment(orderLine);
        MInOutLine shipmentLine = new MInOutLine(outBoundLine.getCtx(), 0, outBoundLine.get_TrxName());
        shipmentLine.setM_InOut_ID(shipment.getM_InOut_ID());
        shipmentLine.setM_Locator_ID(standing.getM_Locator_ID());
        shipmentLine.setM_Product_ID(outBoundLine.getM_Product_ID());
        shipmentLine.setQtyEntered(qtyDelivered);
        shipmentLine.setMovementQty(qtyDelivered);
        shipmentLine.setC_OrderLine_ID(orderLine.getC_OrderLine_ID());
        shipmentLine.saveEx();
    }
    // Generate Delivery Movement
    if (outBoundLine.getDD_OrderLine_ID() > 0) {
        MDDOrderLine distributionOrderLine = (MDDOrderLine) outBoundLine.getDD_OrderLine();
        if (distributionOrders.get(distributionOrderLine.getDD_Order_ID()) == null)
            distributionOrders.put(distributionOrderLine.getDD_Order_ID(), distributionOrderLine.getDD_Order());
        distributionOrderLine.setConfirmedQty(outBoundLine.getPickedQty());
        distributionOrderLine.saveEx();
    }
    // Generate Delivery Manufacturing Order
    if (outBoundLine.getPP_Order_BOMLine_ID() > 0) {
        MPPOrderBOMLine orderBOMLine = (MPPOrderBOMLine) outBoundLine.getPP_Order_BOMLine();
        if (outBoundLine.getPickedQty().subtract(orderBOMLine.getQtyDelivered()).signum() <= 0 && !isIncludeNotAvailable())
            return;
        MLocator standing = getStandingLocator(outBoundLine);
        MStorage[] storage = MStorage.getAll(getCtx(), orderBOMLine.getM_Product_ID(), standing.getM_Locator_ID(), get_TrxName());
        BigDecimal qtyDelivered = getQtyDelivered(outBoundLine, orderBOMLine.getQtyDelivered());
        List<MPPCostCollector> issues = MPPOrder.createIssue(orderBOMLine.getParent(), orderBOMLine, getMovementDate(), qtyDelivered, BigDecimal.ZERO, BigDecimal.ZERO, storage, true);
        issues.stream().forEach(costCollector -> {
            if (manufacturingIssues.get(costCollector.getPP_Cost_Collector_ID()) == null)
                manufacturingIssues.put(costCollector.getPP_Cost_Collector_ID(), costCollector);
        });
    }
}
Also used : MInOut(org.compiere.model.MInOut) MDDOrderLine(org.eevolution.model.MDDOrderLine) MInOutLine(org.compiere.model.MInOutLine) MLocator(org.compiere.model.MLocator) MPPOrderBOMLine(org.eevolution.model.MPPOrderBOMLine) MPPCostCollector(org.eevolution.model.MPPCostCollector) MOrderLine(org.compiere.model.MOrderLine) MStorage(org.compiere.model.MStorage) BigDecimal(java.math.BigDecimal)

Example 10 with MLocator

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

the class MWMInOutBoundLine method getLocator.

/**
	 * get MLocator based in Distribution Order Line
	 * @return MLocator or null
	 */
public MLocator getLocator() {
    String whereClause = MWMInOutBoundLine.COLUMNNAME_WM_InOutBoundLine_ID + "=?";
    MDDOrderLine line = new Query(getCtx(), I_DD_OrderLine.Table_Name, whereClause, get_TrxName()).setClient_ID().setParameters(new Object[] { getWM_InOutBoundLine_ID() }).firstOnly();
    return (MLocator) line.getM_LocatorTo();
}
Also used : Query(org.compiere.model.Query) MLocator(org.compiere.model.MLocator)

Aggregations

MLocator (org.compiere.model.MLocator)49 MProduct (org.compiere.model.MProduct)18 BigDecimal (java.math.BigDecimal)16 MWarehouse (org.compiere.model.MWarehouse)12 MStorage (org.compiere.model.MStorage)9 ArrayList (java.util.ArrayList)8 Properties (java.util.Properties)8 Query (org.compiere.model.Query)7 AdempiereException (org.adempiere.exceptions.AdempiereException)6 MMovementLine (org.compiere.model.MMovementLine)6 KeyNamePair (org.compiere.util.KeyNamePair)6 ResultSet (java.sql.ResultSet)5 Timestamp (java.sql.Timestamp)5 MBPartner (org.compiere.model.MBPartner)5 MMovement (org.compiere.model.MMovement)5 MOrderLine (org.compiere.model.MOrderLine)5 MDDOrderLine (org.eevolution.model.MDDOrderLine)5 ListItem (org.adempiere.webui.component.ListItem)4 MDDOrder (org.eevolution.model.MDDOrder)4 PreparedStatement (java.sql.PreparedStatement)3