public static final int rounding = UtilNumber.getBigDecimalRoundingMode("order.rounding");
public static final MathContext generalRounding = new MathContext(10);
public static Map<String, Object> upsShipmentConfirm(DispatchContext dctx, Map<String, ? extends Object> context) {
Map<String, Object> result = FastMap.newInstance();
GenericDelegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
String shipmentId = (String) context.get("shipmentId");
String shipmentRouteSegmentId = (String) context.get("shipmentRouteSegmentId");
boolean shipmentUpsSaveCertificationInfo = "true".equals(UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.info"));
String shipmentUpsSaveCertificationPath = UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.path");
File shipmentUpsSaveCertificationFile = null;
if (shipmentUpsSaveCertificationInfo) {
shipmentUpsSaveCertificationFile = new File(shipmentUpsSaveCertificationPath);
if (!shipmentUpsSaveCertificationFile.exists()) {
shipmentUpsSaveCertificationFile.mkdirs();
}
}
String shipmentConfirmResponseString = null;
try {
GenericValue shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId));
if (shipment == null) {
return ServiceUtil.returnError("Shipment not found with ID " + shipmentId);
}
GenericValue shipmentRouteSegment = delegator.findByPrimaryKey("ShipmentRouteSegment", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId));
if (shipmentRouteSegment == null) {
return ServiceUtil.returnError("ShipmentRouteSegment not found with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId);
}
if (!"UPS".equals(shipmentRouteSegment.getString("carrierPartyId"))) {
return ServiceUtil.returnError("ERROR: The Carrier for ShipmentRouteSegment " + shipmentRouteSegmentId + " of Shipment " + shipmentId + ", is not UPS.");
}
// add ShipmentRouteSegment carrierServiceStatusId, check before all UPS services
if (UtilValidate.isNotEmpty(shipmentRouteSegment.getString("carrierServiceStatusId")) && !"SHRSCS_NOT_STARTED".equals(shipmentRouteSegment.getString("carrierServiceStatusId"))) {
return ServiceUtil.returnError("ERROR: The Carrier Service Status for ShipmentRouteSegment " + shipmentRouteSegmentId + " of Shipment " + shipmentId + ", is [" + shipmentRouteSegment.getString("carrierServiceStatusId") + "], but must be not-set or [SHRSCS_NOT_STARTED] to perform the UPS Shipment Confirm operation.");
}
// Get Origin Info
GenericValue originPostalAddress = shipmentRouteSegment.getRelatedOne("OriginPostalAddress");
if (originPostalAddress == null) {
return ServiceUtil.returnError("OriginPostalAddress not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId);
}
GenericValue originTelecomNumber = shipmentRouteSegment.getRelatedOne("OriginTelecomNumber");
if (originTelecomNumber == null) {
return ServiceUtil.returnError("OriginTelecomNumber not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId);
}
String originPhoneNumber = originTelecomNumber.getString("areaCode") + originTelecomNumber.getString("contactNumber");
// don't put on country code if not specified or is the US country code (UPS wants it this way)
if (UtilValidate.isNotEmpty(originTelecomNumber.getString("countryCode")) && !"001".equals(originTelecomNumber.getString("countryCode"))) {
originPhoneNumber = originTelecomNumber.getString("countryCode") + originPhoneNumber;
}
originPhoneNumber = StringUtil.replaceString(originPhoneNumber, "-", "");
originPhoneNumber = StringUtil.replaceString(originPhoneNumber, " ", "");
// lookup the two letter country code (in the geoCode field)
GenericValue originCountryGeo = originPostalAddress.getRelatedOne("CountryGeo");
if (originCountryGeo == null) {
return ServiceUtil.returnError("OriginCountryGeo not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId);
}
// Get Dest Info
GenericValue destPostalAddress = shipmentRouteSegment.getRelatedOne("DestPostalAddress");
if (destPostalAddress == null) {
return ServiceUtil.returnError("DestPostalAddress not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId);
}
GenericValue destTelecomNumber = shipmentRouteSegment.getRelatedOne("DestTelecomNumber");
if (destTelecomNumber == null) {
String missingErrMsg = "DestTelecomNumber not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId;
Debug.logError(missingErrMsg, module);
// for now we won't require the dest phone number, but is it required?
//return ServiceUtil.returnError(missingErrMsg);
}
String destPhoneNumber = null;
if (destTelecomNumber != null) {
destPhoneNumber = destTelecomNumber.getString("areaCode") + destTelecomNumber.getString("contactNumber");
// don't put on country code if not specified or is the US country code (UPS wants it this way)
if (UtilValidate.isNotEmpty(destTelecomNumber.getString("countryCode")) && !"001".equals(destTelecomNumber.getString("countryCode"))) {
destPhoneNumber = destTelecomNumber.getString("countryCode") + destPhoneNumber;
}
destPhoneNumber = StringUtil.replaceString(destPhoneNumber, "-", "");
destPhoneNumber = StringUtil.replaceString(destPhoneNumber, " ", "");
}
// lookup the two letter country code (in the geoCode field)
GenericValue destCountryGeo = destPostalAddress.getRelatedOne("CountryGeo");
if (destCountryGeo == null) {
return ServiceUtil.returnError("DestCountryGeo not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId);
}
Map<String, Object> findCarrierShipmentMethodMap = UtilMisc.toMap("partyId", shipmentRouteSegment.get("carrierPartyId"), "roleTypeId", "CARRIER", "shipmentMethodTypeId", shipmentRouteSegment.get("shipmentMethodTypeId"));
GenericValue carrierShipmentMethod = delegator.findByPrimaryKey("CarrierShipmentMethod", findCarrierShipmentMethodMap);
if (carrierShipmentMethod == null) {
return ServiceUtil.returnError("CarrierShipmentMethod not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId + "; partyId is " + shipmentRouteSegment.get("carrierPartyId") + " and shipmentMethodTypeId is " + shipmentRouteSegment.get("shipmentMethodTypeId"));
}
List<GenericValue> shipmentPackageRouteSegs = shipmentRouteSegment.getRelated("ShipmentPackageRouteSeg", null, UtilMisc.toList("+shipmentPackageSeqId"));
if (shipmentPackageRouteSegs == null || shipmentPackageRouteSegs.size() == 0) {
return ServiceUtil.returnError("No ShipmentPackageRouteSegs (ie No Packages) found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId);
}
List<GenericValue> itemIssuances = shipment.getRelated("ItemIssuance");
Set<String> orderIdSet = new TreeSet<String>();
for (GenericValue itemIssuance: itemIssuances) {
orderIdSet.add(itemIssuance.getString("orderId"));
}
String ordersDescription = "";
if (orderIdSet.size() > 1) {
StringBuilder odBuf = new StringBuilder("Orders ");
for (String orderId: orderIdSet) {
if (odBuf.length() > 0) {
odBuf.append(", ");
}
odBuf.append(orderId);
}
ordersDescription = odBuf.toString();
} else if (orderIdSet.size() > 0) {
ordersDescription = "Order " + orderIdSet.iterator().next();
}
// COD Support
boolean allowCOD = "true".equalsIgnoreCase(UtilProperties.getPropertyValue("shipment", "shipment.ups.cod.allowCOD"));
// COD only applies if all orders involved with the shipment were paid only with EXT_COD - anything else becomes too complicated
if (allowCOD) {
// Get the paymentMethodTypeIds of all the orderPaymentPreferences involved with the shipment
List<GenericValue> opps = delegator.findList("OrderPaymentPreference", EntityCondition.makeCondition("orderId", EntityOperator.IN, orderIdSet), null, null, null, false);
List<String> paymentMethodTypeIds = EntityUtil.getFieldListFromEntityList(opps, "paymentMethodTypeId", true);
if (paymentMethodTypeIds.size() > 1 || ! paymentMethodTypeIds.contains("EXT_COD")) {
allowCOD = false;
}