📄 inventoryservices.java
字号:
List inventoryItems = null;
try {
List exprs = UtilMisc.toList(new EntityExpr("availableToPromise", EntityOperator.LESS_THAN, new Double(0)));
inventoryItems = delegator.findByAnd("InventoryItem", exprs);
} catch (GenericEntityException e) {
Debug.logError(e, "Trouble getting inventory items", module);
return ServiceUtil.returnError("Problem getting InventoryItem records");
}
if (inventoryItems == null) {
Debug.logInfo("No items out of stock; no backorders to worry about", module);
return ServiceUtil.returnSuccess();
}
Debug.log("OOS Inventory Items: " + inventoryItems.size(), module);
Iterator itemsIter = inventoryItems.iterator();
while (itemsIter.hasNext()) {
GenericValue inventoryItem = (GenericValue) itemsIter.next();
// get the incomming shipment information for the item
List shipmentAndItems = null;
try {
List exprs = new ArrayList();
exprs.add(new EntityExpr("productId", EntityOperator.EQUALS, inventoryItem.get("productId")));
exprs.add(new EntityExpr("destinationFacilityId", EntityOperator.EQUALS, inventoryItem.get("facilityId")));
exprs.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "SHIPMENT_DELIVERED"));
exprs.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "SHIPMENT_CANCELLED"));
shipmentAndItems = delegator.findByAnd("ShipmentAndItem", exprs, UtilMisc.toList("estimatedArrivalDate"));
} catch (GenericEntityException e) {
Debug.logError(e, "Problem getting ShipmentAndItem records", module);
return ServiceUtil.returnError("Problem getting ShipmentAndItem records");
}
// get the reservations in order of newest first
List reservations = null;
try {
reservations = inventoryItem.getRelated("OrderItemInventoryRes", null, UtilMisc.toList("-reservedDatetime"));
} catch (GenericEntityException e) {
Debug.logError(e, "Problem getting related reservations", module);
return ServiceUtil.returnError("Problem getting related reservations");
}
if (reservations == null) {
Debug.logWarning("No outstanding reservations for this inventory item, why is it negative then?", module);
continue;
}
Debug.log("Reservations for item: " + reservations.size(), module);
// available at the time of order
double availableBeforeReserved = inventoryItem.getDouble("availableToPromise").doubleValue();
// go through all the reservations in order
Iterator ri = reservations.iterator();
while (ri.hasNext()) {
GenericValue reservation = (GenericValue) ri.next();
String orderId = reservation.getString("orderId");
String orderItemSeqId = reservation.getString("orderItemSeqId");
Timestamp promisedDate = reservation.getTimestamp("promisedDatetime");
Timestamp currentPromiseDate = reservation.getTimestamp("currentPromisedDate");
Timestamp actualPromiseDate = currentPromiseDate;
if (actualPromiseDate == null) {
actualPromiseDate = promisedDate;
}
Debug.log("Promised Date: " + actualPromiseDate, module);
// find the next possible ship date
Timestamp nextShipDate = null;
double availableAtTime = 0.00;
Iterator si = shipmentAndItems.iterator();
while (si.hasNext()) {
GenericValue shipmentItem = (GenericValue) si.next();
availableAtTime += shipmentItem.getDouble("quantity").doubleValue();
if (availableAtTime >= availableBeforeReserved) {
nextShipDate = shipmentItem.getTimestamp("estimatedArrivalDate");
break;
}
}
Debug.log("Next Ship Date: " + nextShipDate, module);
// create a modified promise date (promise date - 1 day)
Calendar pCal = Calendar.getInstance();
pCal.setTimeInMillis(actualPromiseDate.getTime());
pCal.add(Calendar.DAY_OF_YEAR, -1);
Timestamp modifiedPromisedDate = new Timestamp(pCal.getTimeInMillis());
Timestamp now = UtilDateTime.nowTimestamp();
Debug.log("Promised Date + 1: " + modifiedPromisedDate, module);
Debug.log("Now: " + now, module);
// check the promised date vs the next ship date
if (nextShipDate == null || nextShipDate.after(actualPromiseDate)) {
if (nextShipDate == null && modifiedPromisedDate.after(now)) {
// do nothing; we are okay to assume it will be shipped on time
Debug.log("No ship date known yet, but promised date hasn't approached, assuming it will be here on time", module);
} else {
// we cannot ship by the promised date; need to notify the customer
Debug.log("We won't ship on time, getting notification info", module);
Map notifyItems = (Map) ordersToUpdate.get(orderId);
if (notifyItems == null) {
notifyItems = new HashMap();
}
notifyItems.put(orderItemSeqId, nextShipDate);
ordersToUpdate.put(orderId, notifyItems);
// need to know if nextShipDate is more then 30 days after promised
Calendar sCal = Calendar.getInstance();
sCal.setTimeInMillis(actualPromiseDate.getTime());
sCal.add(Calendar.DAY_OF_YEAR, 30);
Timestamp farPastPromised = new Timestamp(sCal.getTimeInMillis());
// check to see if this is >30 days or second run, if so flag to cancel
boolean needToCancel = false;
if (nextShipDate == null || nextShipDate.after(farPastPromised)) {
// we cannot ship until >30 days after promised; using cancel rule
Debug.log("Ship date is >30 past the promised date", module);
needToCancel = true;
}
if (currentPromiseDate != null && actualPromiseDate.equals(currentPromiseDate)) {
// this is the second notification; using cancel rule
needToCancel = true;
}
// add the info to the cancel map if we need to schedule a cancel
if (needToCancel) {
// queue the item to be cancelled
Debug.log("Flagging the item to auto-cancel", module);
Map cancelItems = (Map) ordersToCancel.get(orderId);
if (cancelItems == null) {
cancelItems = new HashMap();
}
cancelItems.put(orderItemSeqId, farPastPromised);
ordersToCancel.put(orderId, cancelItems);
}
// store the updated promiseDate as the nextShipDate
try {
reservation.set("currentPromisedDate", nextShipDate);
reservation.store();
} catch (GenericEntityException e) {
Debug.logError(e, "Problem storing reservation : " + reservation, module);
}
}
}
// subtract our qty from reserved to get the next value
availableBeforeReserved -= reservation.getDouble("quantity").doubleValue();
}
}
// all items to cancel will also be in the notify list so start with that
List ordersToNotify = new ArrayList();
Set orderSet = ordersToUpdate.keySet();
Iterator orderIter = orderSet.iterator();
while (orderIter.hasNext()) {
String orderId = (String) orderIter.next();
Map backOrderedItems = (Map) ordersToUpdate.get(orderId);
Map cancelItems = (Map) ordersToCancel.get(orderId);
GenericValue orderShipPref = null;
List orderItems = null;
try {
orderShipPref = delegator.findByPrimaryKey("OrderShipmentPreference", UtilMisc.toMap("orderId", orderId, "orderItemSeqId", "_NA_"));
orderItems = delegator.findByAnd("OrderItem", UtilMisc.toMap("orderId", orderId));
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot get order shipment preference or items", module);
}
// check the split pref
boolean maySplit = false;
if (orderShipPref != null && orderShipPref.get("maySplit") != null) {
maySplit = orderShipPref.getBoolean("maySplit").booleanValue();
}
// figure out if we must cancel all items
boolean cancelAll = false;
Timestamp cancelAllTime = null;
if (!maySplit && cancelItems != null) {
cancelAll = true;
Set cancelSet = cancelItems.keySet();
cancelAllTime = (Timestamp) cancelItems.get(cancelSet.iterator().next());
}
// if there are none to cancel just create an empty map
if (cancelItems == null) {
cancelItems = new HashMap();
}
if (orderItems != null) {
List toBeStored = new ArrayList();
Iterator orderItemsIter = orderItems.iterator();
while (orderItemsIter.hasNext()) {
GenericValue orderItem = (GenericValue) orderItemsIter.next();
String orderItemSeqId = orderItem.getString("orderItemSeqId");
Timestamp shipDate = (Timestamp) backOrderedItems.get(orderItemSeqId);
Timestamp cancelDate = (Timestamp) cancelItems.get(orderItemSeqId);
Timestamp currentCancelDate = (Timestamp) orderItem.getTimestamp("autoCancelDate");
if (backOrderedItems.containsKey(orderItemSeqId)) {
orderItem.set("estimatedShipDate", shipDate);
if (currentCancelDate == null) {
if (cancelAll || cancelDate != null) {
if (orderItem.get("dontCancelSetUserLogin") == null && orderItem.get("dontCancelSetDate") == null) {
if (cancelAllTime != null) {
orderItem.set("autoCancelDate", cancelAllTime);
} else {
orderItem.set("autoCancelDate", cancelDate);
}
}
}
// only notify orders which have not already sent the final notice
ordersToNotify.add(orderId);
}
toBeStored.add(orderItem);
}
}
if (toBeStored.size() > 0) {
try {
delegator.storeAll(toBeStored);
} catch (GenericEntityException e) {
Debug.logError(e, "Problem storing order items", module);
}
}
}
}
// send off a notification for each order
Iterator orderNotifyIter = ordersToNotify.iterator();
while (orderNotifyIter.hasNext()) {
String orderId = (String) orderNotifyIter.next();
try {
dispatcher.runAsync("sendOrderBackorderNotification", UtilMisc.toMap("orderId", orderId, "userLogin", userLogin));
} catch (GenericServiceException e) {
Debug.logError(e, "Problems sending off the notification", module);
continue;
}
}
return ServiceUtil.returnSuccess();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -