OutputStream out = (OutputStream) context.get("outputStream");
if (Debug.infoOn()) Debug.logInfo("Call to oagisSendProcessShipment for orderId [" + orderId + "], sendToUrl=[" + sendToUrl + "], saveToDirectory=[" + saveToDirectory + "], saveToFilename=[" + saveToFilename + "]", module);
Map result = ServiceUtil.returnSuccess();
MapStack bodyParameters = MapStack.create();
bodyParameters.put("orderId", orderId);
// the userLogin passed in will usually be the customer, so don't use it; use the system user instead
GenericValue userLogin = null;
try {
userLogin = delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", "system"));
} catch (GenericEntityException e) {
Debug.logError(e, "Error getting userLogin", module);
}
GenericValue orderHeader = null;
GenericValue orderItemShipGroup = null;
String logicalId = UtilProperties.getPropertyValue("oagis.properties", "CNTROLAREA.SENDER.LOGICALID");
String referenceId = null;
String task = "SHIPREQUEST"; // Actual value of task is "SHIPREQUEST" which is more than 10 char, need this in the db so it will match Confirm BODs, etc
String component = "INVENTORY";
Map omiPkMap = null;
String shipmentId = null;
try {
// see if there are any OagisMessageInfo for this order that are in the OAGMP_OGEN_SUCCESS or OAGMP_SENT statuses, if so don't send again; these need to be manually reviewed before resending to avoid accidental duplicate messages
List previousOagisMessageInfoList = delegator.findByAnd("OagisMessageInfo", UtilMisc.toMap("orderId", orderId, "task", task, "component", component));
if (EntityUtil.filterByAnd(previousOagisMessageInfoList, UtilMisc.toMap("processingStatusId", "OAGMP_OGEN_SUCCESS")).size() > 0) {
// this isn't really an error, just a failed constraint so return success
String successMsg = "Found existing message info(s) in OAGMP_OGEN_SUCCESS, so not sending Process Shipment message for order [" + orderId + "] existing message(s) are: " + EntityUtil.filterByAnd(previousOagisMessageInfoList, UtilMisc.toMap("processingStatusId", "OAGMP_OGEN_SUCCESS"));
return ServiceUtil.returnSuccess(successMsg);
}
if (EntityUtil.filterByAnd(previousOagisMessageInfoList, UtilMisc.toMap("processingStatusId", "OAGMP_SENT")).size() > 0) {
// this isn't really an error, just a failed constraint so return success
String successMsg = "Found existing message info(s) in OAGMP_SENT status, so not sending Process Shipment message for order [" + orderId + "] existing message(s) are: " + EntityUtil.filterByAnd(previousOagisMessageInfoList, UtilMisc.toMap("processingStatusId", "OAGMP_SENT"));
return ServiceUtil.returnSuccess(successMsg);
}
orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId));
if (orderHeader == null) {
return ServiceUtil.returnError("Could not find OrderHeader with ID [" + orderId + "]");
}
List validStores = StringUtil.split(UtilProperties.getPropertyValue("oagis.properties", "Oagis.Order.ValidProductStores"), ",");
if (UtilValidate.isNotEmpty(validStores)) {
if (!validStores.contains(orderHeader.getString("productStoreId"))) {
return ServiceUtil.returnSuccess("Order [" + orderId + "] placed is not for valid Store(s)");
}
}
String orderStatusId = orderHeader.getString("statusId");
if (!"ORDER_APPROVED".equals(orderStatusId)) {
return ServiceUtil.returnSuccess("OrderHeader not in the approved status (ORDER_APPROVED) with ID [" + orderId + "], is in the [" + orderStatusId + "] status");
}
if (!"SALES_ORDER".equals(orderHeader.getString("orderTypeId"))) {
return ServiceUtil.returnError("OrderHeader not a sales order (SALES_ORDER) with ID [" + orderId + "]");
}
// first check some things...
OrderReadHelper orderReadHelper = new OrderReadHelper(orderHeader);
// before doing or saving anything see if any OrderItems are Products with isPhysical=Y
if (!orderReadHelper.hasPhysicalProductItems()) {
// no need to process shipment, return success
return ServiceUtil.returnSuccess();
}
if (!orderReadHelper.hasShippingAddress()) {
return ServiceUtil.returnError("Cannot send Process Shipment for order [" + orderId + "], it has no shipping address.");
}
// check payment authorization
Map authServiceContext = FastMap.newInstance();
authServiceContext.put("orderId", orderId);
authServiceContext.put("userLogin", userLogin);
authServiceContext.put("reAuth", new Boolean("true"));
Map authResult = dispatcher.runSync("authOrderPayments", authServiceContext);
if (!authResult.get("processResult").equals("APPROVED")) {
return ServiceUtil.returnError("No authorized payment available, not sending Process Shipment");
}
referenceId = delegator.getNextSeqId("OagisMessageInfo");
omiPkMap = UtilMisc.toMap("logicalId", logicalId, "component", component, "task", task, "referenceId", referenceId);
String authId = UtilProperties.getPropertyValue("oagis.properties", "CNTROLAREA.SENDER.AUTHID");
Timestamp timestamp = UtilDateTime.nowTimestamp();
String sentDate = OagisServices.isoDateFormat.format(timestamp);
bodyParameters.putAll(omiPkMap);
bodyParameters.put("authId", authId);
bodyParameters.put("sentDate", sentDate);
// prepare map to Create Oagis Message Info
try {
Map comiCtx = FastMap.newInstance();
comiCtx.putAll(omiPkMap);
comiCtx.put("processingStatusId", "OAGMP_TRIGGERED");
comiCtx.put("outgoingMessage", "Y");
comiCtx.put("confirmation", "1");
comiCtx.put("bsrVerb", "PROCESS");
comiCtx.put("bsrNoun", "SHIPMENT");
comiCtx.put("bsrRevision", "001");
comiCtx.put("orderId", orderId);
comiCtx.put("sentDate", timestamp);
comiCtx.put("authId", authId);
comiCtx.put("userLogin", userLogin);
dispatcher.runSync("createOagisMessageInfo", comiCtx, 60, true);
} catch (GenericServiceException e) {
String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "OagisErrorInCreatingDataForOagisMessageInfoEntity", (Locale) context.get("locale"));
Debug.logError(e, errMsg, module);
}
if (Debug.infoOn()) Debug.logInfo("Saved OagisMessageInfo for oagisSendProcessShipment message for orderId [" + orderId + "]", module);
// check to see if there is already a Shipment for this order
EntityCondition findShipmentCondition = EntityCondition.makeCondition(UtilMisc.toList(
EntityCondition.makeCondition("primaryOrderId", EntityOperator.EQUALS, orderId),
EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "SHIPMENT_CANCELLED")
), EntityOperator.AND);
List shipmentList = delegator.findList("Shipment", findShipmentCondition, null, null, null, false);
GenericValue shipment = EntityUtil.getFirst(shipmentList);
if (shipment != null) {
// if picked, packed, shipped, delivered then complain, no reason to process the shipment!
String statusId = shipment.getString("statusId");
if ("SHIPMENT_PICKED".equals(statusId) || "SHIPMENT_PACKED".equals(statusId) || "SHIPMENT_SHIPPED".equals(statusId) || "SHIPMENT_DELIVERED".equals(statusId)) {
return ServiceUtil.returnError("Not sending Process Shipment message because found Shipment that is already being processed, is in status [" + statusId + "]");
}
shipmentId = shipment.getString("shipmentId");
} else {
Map cospResult= dispatcher.runSync("createOrderShipmentPlan", UtilMisc.<String, Object>toMap("orderId", orderId, "userLogin", userLogin));
shipmentId = (String) cospResult.get("shipmentId");
shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId));
}
bodyParameters.put("shipment", shipment);
List shipmentItems = delegator.findByAnd("ShipmentItem", UtilMisc.toMap("shipmentId", shipmentId));
bodyParameters.put("shipmentItems", shipmentItems);
GenericValue address = EntityUtil.getFirst(orderReadHelper.getShippingLocations());
bodyParameters.put("address", address);
String emailString = orderReadHelper.getOrderEmailString();
bodyParameters.put("emailString", emailString);
String contactMechId = shipment.getString("destinationTelecomNumberId");
GenericValue telecomNumber = delegator.findByPrimaryKey("TelecomNumber", UtilMisc.toMap("contactMechId", contactMechId));
if (telecomNumber == null) {
return ServiceUtil.returnError("In Send ProcessShipment Telecom number not found for orderId [" + orderId + "]");
}
bodyParameters.put("telecomNumber", telecomNumber);
orderItemShipGroup = EntityUtil.getFirst(delegator.findByAnd("OrderItemShipGroup", UtilMisc.toMap("orderId", orderId)));
bodyParameters.put("orderItemShipGroup", orderItemShipGroup);
Set correspondingPoIdSet = FastSet.newInstance();
List orderItems = orderReadHelper.getOrderItems();
Iterator oiIter = orderItems.iterator();
while (oiIter.hasNext()) {
GenericValue orderItem = (GenericValue) oiIter.next();
String correspondingPoId = orderItem.getString("correspondingPoId");
if (correspondingPoId != null) {
correspondingPoIdSet.add(correspondingPoId);
}
}
bodyParameters.put("correspondingPoIdSet", correspondingPoIdSet);
if (orderHeader.get("externalId") != null) {
Set externalIdSet = FastSet.newInstance();
externalIdSet.add(orderHeader.getString("externalId"));
bodyParameters.put("externalIdSet", externalIdSet);
}
// Check if order was a return replacement order (associated with return)
GenericValue returnItemResponse = EntityUtil.getFirst(delegator.findByAnd("ReturnItemResponse", UtilMisc.toMap("replacementOrderId", orderId)));
if (returnItemResponse != null) {
boolean includeReturnLabel = false;
// Get the associated return Id (replaceReturnId)
String returnItemResponseId = returnItemResponse.getString("returnItemResponseId");
List returnItemList = delegator.findByAnd("ReturnItem", UtilMisc.toMap("returnItemResponseId", returnItemResponseId));
GenericValue firstReturnItem = EntityUtil.getFirst(returnItemList);
if (firstReturnItem != null) {
bodyParameters.put("replacementReturnId", firstReturnItem.getString("returnId"));
} else {
Debug.logWarning("Could not find a ReturnItem for returnItemResponseId [" + returnItemResponseId + "]; this really shouldn't happen but isn't a real error either. It means a ReturnItemResponse was created but not attached to any item!", module);
}
// return label should only be sent when we want a return label to be included; this would be for a cross-ship replacement type ReturnItem
// go through the returnItemList and if any are cross-ship replacement, then include a label (not for wait replacement in other words)
Iterator returnItemIter = returnItemList.iterator();
while (returnItemIter.hasNext()) {
GenericValue returnItem = (GenericValue) returnItemIter.next();
if ("RTN_CSREPLACE".equals(returnItem.getString("returnTypeId"))) {
includeReturnLabel = true;
}
}
if (includeReturnLabel) {
bodyParameters.put("shipnotes", "RETURNLABEL");
}
}
// tracking shipper account, other Party info
String partyId = shipment.getString("partyIdTo");
bodyParameters.put("partyNameView", delegator.findByPrimaryKey("PartyNameView", UtilMisc.toMap("partyId", partyId)));
List partyCarrierAccounts = delegator.findByAnd("PartyCarrierAccount", UtilMisc.toMap("partyId", partyId));
partyCarrierAccounts = EntityUtil.filterByDate(partyCarrierAccounts);
if (partyCarrierAccounts != null) {
Iterator pcaIter = partyCarrierAccounts.iterator();
while (pcaIter.hasNext()) {
GenericValue partyCarrierAccount = (GenericValue) pcaIter.next();
String carrierPartyId = partyCarrierAccount.getString("carrierPartyId");
if (carrierPartyId.equals(orderItemShipGroup.getString("carrierPartyId"))) {
String accountNumber = partyCarrierAccount.getString("accountNumber");
bodyParameters.put("shipperId", accountNumber);
}
}
}
bodyParameters.put("shipmentId", shipmentId);
bodyParameters.put("orderId", orderId);
bodyParameters.put("userLogin", userLogin);
String bodyScreenUri = UtilProperties.getPropertyValue("oagis.properties", "Oagis.Template.ProcessShipment");
String outText = null;
Writer writer = new StringWriter();
ScreenRenderer screens = new ScreenRenderer(writer, bodyParameters, htmlScreenRenderer);