use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.
the class InventoryServices method checkInventoryAvailability.
/**
* In spite of the generic name this does the very specific task of checking availability of all back-ordered items and sends notices, etc
*/
public static Map<String, Object> checkInventoryAvailability(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
Map<String, Map<String, Timestamp>> ordersToUpdate = new HashMap<>();
Map<String, Map<String, Timestamp>> ordersToCancel = new HashMap<>();
// find all inventory items w/ a negative ATP
List<GenericValue> inventoryItems = null;
try {
inventoryItems = EntityQuery.use(delegator).from("InventoryItem").where(EntityCondition.makeCondition("availableToPromiseTotal", EntityOperator.LESS_THAN, BigDecimal.ZERO)).queryList();
} catch (GenericEntityException e) {
Debug.logError(e, "Trouble getting inventory items", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductPriceCannotRetrieveInventoryItem", locale));
}
if (inventoryItems == null) {
Debug.logInfo("No items out of stock; no backorders to worry about", module);
return ServiceUtil.returnSuccess();
}
Debug.logInfo("OOS Inventory Items: " + inventoryItems.size(), module);
for (GenericValue inventoryItem : inventoryItems) {
// get the incomming shipment information for the item
List<GenericValue> shipmentAndItems = null;
try {
List<EntityExpr> exprs = new ArrayList<>();
exprs.add(EntityCondition.makeCondition("productId", EntityOperator.EQUALS, inventoryItem.get("productId")));
exprs.add(EntityCondition.makeCondition("destinationFacilityId", EntityOperator.EQUALS, inventoryItem.get("facilityId")));
exprs.add(EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "SHIPMENT_DELIVERED"));
exprs.add(EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "SHIPMENT_CANCELLED"));
shipmentAndItems = EntityQuery.use(delegator).from("ShipmentAndItem").where(EntityCondition.makeCondition(exprs, EntityOperator.AND)).orderBy("estimatedArrivalDate").queryList();
} catch (GenericEntityException e) {
Debug.logError(e, "Problem getting ShipmentAndItem records", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductPriceCannotRetrieveShipmentAndItem", locale));
}
// get the reservations in order of newest first
List<GenericValue> reservations = null;
try {
reservations = inventoryItem.getRelated("OrderItemShipGrpInvRes", null, UtilMisc.toList("-reservedDatetime"), false);
} catch (GenericEntityException e) {
Debug.logError(e, "Problem getting related reservations", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductPriceCannotRetrieveRelativeReservation", locale));
}
if (reservations == null) {
Debug.logWarning("No outstanding reservations for this inventory item, why is it negative then?", module);
continue;
}
Debug.logInfo("Reservations for item: " + reservations.size(), module);
// available at the time of order
BigDecimal availableBeforeReserved = inventoryItem.getBigDecimal("availableToPromiseTotal");
// go through all the reservations in order
for (GenericValue reservation : reservations) {
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) {
if (promisedDate != null) {
actualPromiseDate = promisedDate;
} else {
// fall back if there is no promised date stored
actualPromiseDate = reservation.getTimestamp("reservedDatetime");
}
}
Debug.logInfo("Promised Date: " + actualPromiseDate, module);
// find the next possible ship date
Timestamp nextShipDate = null;
BigDecimal availableAtTime = BigDecimal.ZERO;
for (GenericValue shipmentItem : shipmentAndItems) {
availableAtTime = availableAtTime.add(shipmentItem.getBigDecimal("quantity"));
if (availableAtTime.compareTo(availableBeforeReserved) >= 0) {
nextShipDate = shipmentItem.getTimestamp("estimatedArrivalDate");
break;
}
}
Debug.logInfo("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.logInfo("Promised Date + 1: " + modifiedPromisedDate, module);
Debug.logInfo("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.logInfo("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.logInfo("We won't ship on time, getting notification info", module);
Map<String, Timestamp> notifyItems = 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.logInfo("Ship date is >30 past the promised date", module);
needToCancel = true;
} else 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.logInfo("Flagging the item to auto-cancel", module);
Map<String, Timestamp> cancelItems = 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 = availableBeforeReserved.subtract(reservation.getBigDecimal("quantity"));
}
}
// all items to cancel will also be in the notify list so start with that
List<String> ordersToNotify = new LinkedList<>();
for (Map.Entry<String, Map<String, Timestamp>> entry : ordersToUpdate.entrySet()) {
String orderId = entry.getKey();
Map<String, Timestamp> backOrderedItems = entry.getValue();
Map<String, Timestamp> cancelItems = ordersToCancel.get(orderId);
boolean cancelAll = false;
Timestamp cancelAllTime = null;
List<GenericValue> orderItemShipGroups = null;
try {
orderItemShipGroups = EntityQuery.use(delegator).from("OrderItemShipGroup").where("orderId", orderId).queryList();
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot get OrderItemShipGroups from orderId" + orderId, module);
}
for (GenericValue orderItemShipGroup : orderItemShipGroups) {
List<GenericValue> orderItems = new LinkedList<>();
List<GenericValue> orderItemShipGroupAssoc = null;
try {
orderItemShipGroupAssoc = EntityQuery.use(delegator).from("OrderItemShipGroupAssoc").where("shipGroupSeqId", orderItemShipGroup.get("shipGroupSeqId"), "orderId", orderId).queryList();
for (GenericValue assoc : orderItemShipGroupAssoc) {
GenericValue orderItem = assoc.getRelatedOne("OrderItem", false);
if (orderItem != null) {
orderItems.add(orderItem);
}
}
} catch (GenericEntityException e) {
Debug.logError(e, "Problem fetching OrderItemShipGroupAssoc", module);
}
/* Check the split preference. */
boolean maySplit = false;
if (orderItemShipGroup.get("maySplit") != null) {
maySplit = orderItemShipGroup.getBoolean("maySplit").booleanValue();
}
/* Figure out if we must cancel all items. */
if (!maySplit && cancelItems != null) {
cancelAll = true;
Set<String> cancelSet = cancelItems.keySet();
cancelAllTime = cancelItems.get(cancelSet.iterator().next());
}
// if there are none to cancel just create an empty map
if (cancelItems == null) {
cancelItems = new HashMap<>();
}
List<GenericValue> toBeStored = new LinkedList<>();
for (GenericValue orderItem : orderItems) {
String orderItemSeqId = orderItem.getString("orderItemSeqId");
Timestamp shipDate = backOrderedItems.get(orderItemSeqId);
Timestamp cancelDate = cancelItems.get(orderItemSeqId);
Timestamp currentCancelDate = orderItem.getTimestamp("autoCancelDate");
Debug.logInfo("OI: " + orderId + " SEQID: " + orderItemSeqId + " cancelAll: " + cancelAll + " cancelDate: " + cancelDate, module);
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
for (String orderId : ordersToNotify) {
try {
dispatcher.runAsync("sendOrderBackorderNotification", UtilMisc.<String, Object>toMap("orderId", orderId, "userLogin", userLogin));
} catch (GenericServiceException e) {
Debug.logError(e, "Problems sending off the notification", module);
continue;
}
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.
the class ProductPromoContentWrapper method getProductPromoContentAsText.
public static void getProductPromoContentAsText(String productPromoId, GenericValue productPromo, String productPromoContentTypeId, Locale locale, String mimeTypeId, String partyId, String roleTypeId, Delegator delegator, LocalDispatcher dispatcher, Writer outWriter, boolean cache) throws GeneralException, IOException {
if (UtilValidate.isEmpty(productPromoId) && productPromo != null) {
productPromoId = productPromo.getString("productPromoId");
}
if (UtilValidate.isEmpty(delegator) && productPromo != null) {
delegator = productPromo.getDelegator();
}
if (UtilValidate.isEmpty(mimeTypeId)) {
mimeTypeId = EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator);
}
if (UtilValidate.isEmpty(delegator)) {
throw new GeneralRuntimeException("Unable to find a delegator to use!");
}
List<EntityExpr> exprs = new ArrayList<>();
exprs.add(EntityCondition.makeCondition("productPromoId", EntityOperator.EQUALS, productPromoId));
exprs.add(EntityCondition.makeCondition("productPromoContentTypeId", EntityOperator.EQUALS, productPromoContentTypeId));
List<GenericValue> productPromoContentList = EntityQuery.use(delegator).from("ProductPromoContent").where(EntityCondition.makeCondition(exprs, EntityOperator.AND)).orderBy("-fromDate").cache(cache).queryList();
GenericValue productPromoContent = null;
if (UtilValidate.isNotEmpty(productPromoContentList)) {
productPromoContent = EntityUtil.getFirst(EntityUtil.filterByDate(productPromoContentList));
}
if (productPromoContent != null) {
// when rendering the product promo content, always include the ProductPromo and ProductPromoContent records that this comes from
Map<String, Object> inContext = new HashMap<>();
inContext.put("productPromo", productPromo);
inContext.put("productPromoContent", productPromoContent);
ContentWorker.renderContentAsText(dispatcher, productPromoContent.getString("contentId"), outWriter, inContext, locale, mimeTypeId, partyId, roleTypeId, cache);
return;
}
String candidateFieldName = ModelUtil.dbNameToVarName(productPromoContentTypeId);
ModelEntity productModel = delegator.getModelEntity("ProductPromo");
if (productModel.isField(candidateFieldName)) {
if (UtilValidate.isEmpty(productPromo)) {
productPromo = EntityQuery.use(delegator).from("ProductPromo").where("productPromoId", productPromoId).cache().queryOne();
}
if (productPromo != null) {
String candidateValue = productPromo.getString(candidateFieldName);
if (UtilValidate.isNotEmpty(candidateValue)) {
outWriter.write(candidateValue);
return;
}
}
}
}
use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.
the class WorkEffortServices method getWorkEffortAssignedEventsForRoleOfAllParties.
public static Map<String, Object> getWorkEffortAssignedEventsForRoleOfAllParties(DispatchContext ctx, Map<String, ? extends Object> context) {
Delegator delegator = ctx.getDelegator();
String roleTypeId = (String) context.get("roleTypeId");
Locale locale = (Locale) context.get("locale");
List<GenericValue> validWorkEfforts = null;
try {
List<EntityExpr> conditionList = new LinkedList<>();
conditionList.add(EntityCondition.makeCondition("roleTypeId", EntityOperator.EQUALS, roleTypeId));
conditionList.add(EntityCondition.makeCondition("workEffortTypeId", EntityOperator.EQUALS, "EVENT"));
conditionList.add(EntityCondition.makeCondition("currentStatusId", EntityOperator.NOT_EQUAL, "CAL_DECLINED"));
conditionList.add(EntityCondition.makeCondition("currentStatusId", EntityOperator.NOT_EQUAL, "CAL_DELEGATED"));
conditionList.add(EntityCondition.makeCondition("currentStatusId", EntityOperator.NOT_EQUAL, "CAL_COMPLETED"));
conditionList.add(EntityCondition.makeCondition("currentStatusId", EntityOperator.NOT_EQUAL, "CAL_CANCELLED"));
EntityConditionList<EntityExpr> ecl = EntityCondition.makeCondition(conditionList, EntityOperator.AND);
validWorkEfforts = EntityQuery.use(delegator).from("WorkEffortAndPartyAssign").where(ecl).orderBy("estimatedStartDate", "priority").filterByDate().queryList();
} catch (GenericEntityException e) {
Debug.logWarning(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "WorkEffortNotFound", UtilMisc.toMap("errorString", e.toString()), locale));
}
Map<String, Object> result = new HashMap<>();
if (validWorkEfforts == null) {
validWorkEfforts = new LinkedList<>();
}
result.put("events", validWorkEfforts);
return result;
}
use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.
the class WorkEffortServices method getWorkEffortAssignedEventsForRole.
public static Map<String, Object> getWorkEffortAssignedEventsForRole(DispatchContext ctx, Map<String, ? extends Object> context) {
Delegator delegator = ctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String roleTypeId = (String) context.get("roleTypeId");
Locale locale = (Locale) context.get("locale");
List<GenericValue> validWorkEfforts = null;
if (userLogin != null && userLogin.get("partyId") != null) {
try {
EntityConditionList<EntityExpr> ecl = EntityCondition.makeCondition(EntityOperator.AND, EntityCondition.makeCondition("partyId", EntityOperator.EQUALS, userLogin.get("partyId")), EntityCondition.makeCondition("roleTypeId", EntityOperator.EQUALS, roleTypeId), EntityCondition.makeCondition("workEffortTypeId", EntityOperator.EQUALS, "EVENT"), EntityCondition.makeCondition("currentStatusId", EntityOperator.NOT_EQUAL, "CAL_DECLINED"), EntityCondition.makeCondition("currentStatusId", EntityOperator.NOT_EQUAL, "CAL_DELEGATED"), EntityCondition.makeCondition("currentStatusId", EntityOperator.NOT_EQUAL, "CAL_COMPLETED"), EntityCondition.makeCondition("currentStatusId", EntityOperator.NOT_EQUAL, "CAL_CANCELLED"));
validWorkEfforts = EntityQuery.use(delegator).from("WorkEffortAndPartyAssign").where(ecl).orderBy("estimatedStartDate", "priority").filterByDate().queryList();
} catch (GenericEntityException e) {
Debug.logWarning(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "WorkEffortNotFound", UtilMisc.toMap("errorString", e.toString()), locale));
}
}
Map<String, Object> result = new HashMap<>();
if (validWorkEfforts == null) {
validWorkEfforts = new LinkedList<>();
}
result.put("events", validWorkEfforts);
return result;
}
use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.
the class WorkEffortServices method getWorkEffortAssignedActivitiesByGroup.
public static Map<String, Object> getWorkEffortAssignedActivitiesByGroup(DispatchContext ctx, Map<String, ? extends Object> context) {
Delegator delegator = ctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
List<GenericValue> groupWorkEfforts = null;
if (userLogin != null && userLogin.get("partyId") != null) {
try {
List<EntityExpr> constraints = new LinkedList<>();
constraints.add(EntityCondition.makeCondition("partyId", EntityOperator.EQUALS, userLogin.get("partyId")));
constraints.add(EntityCondition.makeCondition("workEffortTypeId", EntityOperator.EQUALS, "ACTIVITY"));
constraints.add(EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "CAL_DECLINED"));
constraints.add(EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "CAL_DELEGATED"));
constraints.add(EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "CAL_COMPLETED"));
constraints.add(EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "CAL_CANCELLED"));
constraints.add(EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "PRTYASGN_UNASSIGNED"));
constraints.add(EntityCondition.makeCondition("currentStatusId", EntityOperator.NOT_EQUAL, "WF_COMPLETED"));
constraints.add(EntityCondition.makeCondition("currentStatusId", EntityOperator.NOT_EQUAL, "WF_TERMINATED"));
constraints.add(EntityCondition.makeCondition("currentStatusId", EntityOperator.NOT_EQUAL, "WF_ABORTED"));
groupWorkEfforts = EntityQuery.use(delegator).from("WorkEffortPartyAssignByGroup").where(constraints).orderBy("priority").filterByDate().queryList();
} catch (GenericEntityException e) {
Debug.logWarning(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "WorkEffortNotFound", UtilMisc.toMap("errorString", e.toString()), locale));
}
}
Map<String, Object> result = new HashMap<>();
if (groupWorkEfforts == null) {
groupWorkEfforts = new LinkedList<>();
}
result.put("groupActivities", groupWorkEfforts);
return result;
}
Aggregations