📄 packingsession.java
字号:
/* * $Id$ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */package org.ofbiz.shipment.packing;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Map;import javolution.util.FastMap;import javolution.util.FastList;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.GeneralException;import org.ofbiz.base.util.UtilFormatOut;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.util.EntityUtil;import org.ofbiz.service.GenericDispatcher;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ServiceUtil;import org.ofbiz.product.product.ProductWorker;/** * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 6764 $ * @since Sep 1, 2005 */public class PackingSession implements java.io.Serializable { public static final String module = PackingSession.class.getName(); protected GenericValue userLogin = null; protected String primaryOrderId = null; protected String primaryShipGrp = null; protected String dispatcherName = null; protected String delegatorName = null; protected String facilityId = null; protected String shipmentId = null; protected String instructions = null; protected List packEvents = null; protected List packLines = null; protected int packageSeq = -1; protected int status = 1; private transient GenericDelegator _delegator = null; private transient LocalDispatcher _dispatcher = null; public PackingSession(LocalDispatcher dispatcher, GenericValue userLogin, String facilityId, String orderId, String shipGrp) { this._dispatcher = dispatcher; this.dispatcherName = dispatcher.getName(); this._delegator = _dispatcher.getDelegator(); this.delegatorName = _delegator.getDelegatorName(); this.primaryOrderId = orderId; this.primaryShipGrp = shipGrp; this.userLogin = userLogin; this.facilityId = facilityId; this.packLines = new ArrayList(); this.packEvents = new ArrayList(); this.packageSeq = 1; } public PackingSession(LocalDispatcher dispatcher, GenericValue userLogin, String facilityId) { this(dispatcher, userLogin, facilityId, null, null); } public PackingSession(LocalDispatcher dispatcher, GenericValue userLogin) { this(dispatcher, userLogin, null, null, null); } public void addOrIncreaseLine(String orderId, String orderItemSeqId, String shipGroupSeqId, String productId, double quantity, int packageSeqId) throws GeneralException { // reset the session if we just completed if (status == 0) { throw new GeneralException("Packing session has been completed; be sure to CLEAR before packing a new order! [000]"); } // find the actual product ID productId = ProductWorker.findProductId(this.getDelegator(), productId); // set the default null values - primary is the assumed first item if (orderId == null) { orderId = primaryOrderId; } if (shipGroupSeqId == null) { shipGroupSeqId = primaryShipGrp; } if (orderItemSeqId == null && productId != null) { orderItemSeqId = this.findOrderItemSeqId(productId, orderId, shipGroupSeqId, quantity); } // get the reservations for the item Map invLookup = FastMap.newInstance(); invLookup.put("orderId", orderId); invLookup.put("orderItemSeqId", orderItemSeqId); invLookup.put("shipGroupSeqId", shipGroupSeqId); List reservations = this.getDelegator().findByAnd("OrderItemShipGrpInvRes", invLookup); // no reservations we cannot add this item if (UtilValidate.isEmpty(reservations)) { throw new GeneralException("No inventory reservations available; cannot pack this item! [101]"); } // find the inventoryItemId to use if (reservations.size() == 1) { GenericValue res = EntityUtil.getFirst(reservations); int checkCode = this.checkLineForAdd(res, orderId, orderItemSeqId, shipGroupSeqId, quantity, packageSeqId); this.createPackLineItem(checkCode, res, orderId, orderItemSeqId, shipGroupSeqId, productId, quantity, packageSeqId); } else { // more than one reservation found Map toCreateMap = FastMap.newInstance(); Iterator i = reservations.iterator(); double qtyRemain = quantity; while (i.hasNext() && qtyRemain > 0) { GenericValue res = (GenericValue) i.next(); double resQty = res.getDouble("quantity").doubleValue(); double thisQty = resQty > qtyRemain ? qtyRemain : resQty; int thisCheck = this.checkLineForAdd(res, orderId, orderItemSeqId, shipGroupSeqId, thisQty, packageSeqId); switch (thisCheck) { case 2: Debug.log("Packing check returned '2' - new pack line will be created!", module); toCreateMap.put(res, new Double(resQty)); qtyRemain -= resQty; break; case 1: Debug.log("Packing check returned '1' - existing pack line has been updated!", module); qtyRemain -= resQty; break; case 0: Debug.log("Packing check returned '0' - doing nothing.", module); break; } } if (qtyRemain == 0) { Iterator x = toCreateMap.keySet().iterator(); while (x.hasNext()) { GenericValue res = (GenericValue) x.next(); Double qty = (Double) toCreateMap.get(res); this.createPackLineItem(2, res, orderId, orderItemSeqId, shipGroupSeqId, productId, qty.doubleValue(), packageSeqId); } } else { throw new GeneralException("Not enough inventory reservation available; cannot pack the item! [103]"); } } // run the add events this.runEvents(PackingEvent.EVENT_CODE_ADD); } public void addOrIncreaseLine(String orderId, String orderItemSeqId, String shipGroupSeqId, double quantity, int packageSeqId) throws GeneralException { this.addOrIncreaseLine(orderId, orderItemSeqId, shipGroupSeqId, null, quantity, packageSeqId); } public void addOrIncreaseLine(String productId, double quantity, int packageSeqId) throws GeneralException { this.addOrIncreaseLine(null, null, null, productId, quantity, packageSeqId); } public PackingSessionLine findLine(String orderId, String orderItemSeqId, String shipGroupSeqId, String inventoryItemId, int packageSeq) { List lines = this.getLines(); Iterator i = lines.iterator(); while (i.hasNext()) { PackingSessionLine line = (PackingSessionLine) i.next(); if (orderId.equals(line.getOrderId()) && orderItemSeqId.equals(line.getOrderItemSeqId()) && shipGroupSeqId.equals(line.getShipGroupSeqId()) && inventoryItemId.equals(line.getInventoryItemId()) && packageSeq == line.getPackageSeq()) { return line; } } return null; } protected void createPackLineItem(int checkCode, GenericValue res, String orderId, String orderItemSeqId, String shipGroupSeqId, String productId, double quantity, int packageSeqId) throws GeneralException { // process the result; add new item if necessary switch(checkCode) { case 0: // not enough reserved throw new GeneralException("Not enough inventory reservation available; cannot pack the item! [201]"); case 1: // we're all good to go; quantity already updated break; case 2: // need to create a new item String invItemId = res.getString("inventoryItemId"); packLines.add(new PackingSessionLine(orderId, orderItemSeqId, shipGroupSeqId, productId, invItemId, quantity, packageSeqId)); break; } // update the package sequence if (packageSeqId > packageSeq) { this.packageSeq = packageSeqId; } } protected String findOrderItemSeqId(String productId, String orderId, String shipGroupSeqId, double quantity) throws GeneralException { Map lookupMap = FastMap.newInstance(); lookupMap.put("orderId", orderId); lookupMap.put("productId", productId); lookupMap.put("shipGroupSeqId", shipGroupSeqId); List sort = UtilMisc.toList("-quantity"); List orderItems = this.getDelegator().findByAnd("OrderItemAndShipGroupAssoc", lookupMap, sort); String orderItemSeqId = null; if (orderItems != null) { Iterator i = orderItems.iterator(); while (i.hasNext()) { GenericValue item = (GenericValue) i.next(); Double qty = item.getDouble("quantity"); if (quantity <= qty.doubleValue()) { orderItemSeqId = item.getString("orderItemSeqId"); break; } } } if (orderItemSeqId != null) { return orderItemSeqId; } else { throw new GeneralException("No valid order item found for product [" + productId + "] with quantity: " + quantity); } } protected int checkLineForAdd(GenericValue res, String orderId, String orderItemSeqId, String shipGroupSeqId, double quantity, int packageSeqId) { // check to see if the reservation can hold the requested quantity amount String invItemId = res.getString("inventoryItemId"); double resQty = res.getDouble("quantity").doubleValue();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -