1 24 package org.ofbiz.shipment.shipment; 25 26 import java.util.*; 27 28 import javolution.util.FastMap; 29 30 import org.ofbiz.base.util.Debug; 31 import org.ofbiz.base.util.StringUtil; 32 import org.ofbiz.base.util.UtilMisc; 33 import org.ofbiz.base.util.UtilValidate; 34 import org.ofbiz.common.geo.GeoWorker; 35 import org.ofbiz.entity.GenericDelegator; 36 import org.ofbiz.entity.GenericEntityException; 37 import org.ofbiz.entity.GenericValue; 38 import org.ofbiz.entity.util.EntityListIterator; 39 import org.ofbiz.entity.util.EntityUtil; 40 import org.ofbiz.service.DispatchContext; 41 import org.ofbiz.service.GenericServiceException; 42 import org.ofbiz.service.LocalDispatcher; 43 import org.ofbiz.service.ModelService; 44 import org.ofbiz.service.ServiceUtil; 45 46 53 public class ShipmentServices { 54 55 public static final String module = ShipmentServices.class.getName(); 56 57 public static Map createShipmentEstimate(DispatchContext dctx, Map context) { 58 Map result = new HashMap(); 59 GenericDelegator delegator = dctx.getDelegator(); 60 List storeAll = new ArrayList(); 61 62 String shipMethodAndParty = (String ) context.get("shipMethod"); 63 List shipMethodSplit = StringUtil.split(shipMethodAndParty, "|"); 64 65 GenericValue estimate = delegator.makeValue("ShipmentCostEstimate", null); 67 68 estimate.set("shipmentCostEstimateId", delegator.getNextSeqId("ShipmentCostEstimate")); 69 estimate.set("shipmentMethodTypeId", shipMethodSplit.get(1)); 70 estimate.set("carrierPartyId", shipMethodSplit.get(0)); 71 estimate.set("carrierRoleTypeId", "CARRIER"); 72 estimate.set("productStoreId", context.get("productStoreId")); 73 estimate.set("geoIdTo", context.get("toGeo")); 74 estimate.set("geoIdFrom", context.get("fromGeo")); 75 estimate.set("partyId", context.get("partyId")); 76 estimate.set("roleTypeId", context.get("roleTypeId")); 77 estimate.set("orderPricePercent", context.get("flatPercent")); 78 estimate.set("orderFlatPrice", context.get("flatPrice")); 79 estimate.set("orderItemFlatPrice", context.get("flatItemPrice")); 80 estimate.set("productFeatureGroupId", context.get("productFeatureGroupId")); 81 estimate.set("oversizeUnit", context.get("oversizeUnit")); 82 estimate.set("oversizePrice", context.get("oversizePrice")); 83 estimate.set("featurePercent", context.get("featurePercent")); 84 estimate.set("featurePrice", context.get("featurePrice")); 85 storeAll.add(estimate); 86 87 if (!applyQuantityBreak(context, result, storeAll, delegator, estimate, "w", "weight", "Weight")) { 88 return result; 89 } 90 91 if (!applyQuantityBreak(context, result, storeAll, delegator, estimate, "q", "quantity", "Quantity")) { 92 return result; 93 } 94 95 if (!applyQuantityBreak(context, result, storeAll, delegator, estimate, "p", "price", "Price")) { 96 return result; 97 } 98 99 try { 100 delegator.storeAll(storeAll); 101 } catch (GenericEntityException e) { 102 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 103 result.put(ModelService.ERROR_MESSAGE, "Problem reading product features: " + e.toString()); 104 return result; 105 } 106 107 result.put("shipmentCostEstimateId", estimate.get("shipmentCostEstimateId")); 108 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 109 return result; 110 } 111 112 public static Map removeShipmentEstimate(DispatchContext dctx, Map context) { 113 GenericDelegator delegator = dctx.getDelegator(); 114 String shipmentCostEstimateId = (String ) context.get("shipmentCostEstimateId"); 115 116 GenericValue estimate = null; 117 118 try { 119 estimate = delegator.findByPrimaryKey("ShipmentCostEstimate", UtilMisc.toMap("shipmentCostEstimateId", shipmentCostEstimateId)); 120 estimate.remove(); 121 if (estimate.get("weightBreakId") != null) 122 delegator.removeRelated("WeightQuantityBreak", estimate); 123 if (estimate.get("quantityBreakId") != null) 124 delegator.removeRelated("QuantityQuantityBreak", estimate); 125 if (estimate.get("priceBreakId") != null) 126 delegator.removeRelated("PriceQuantityBreak", estimate); 127 } catch (GenericEntityException e) { 128 Debug.logError(e, module); 129 return ServiceUtil.returnError("Problem removing entity or related entities (" + e.toString() + ")"); 130 } 131 return ServiceUtil.returnSuccess(); 132 } 133 134 private static boolean applyQuantityBreak(Map context, Map result, List storeAll, GenericDelegator delegator, 135 GenericValue estimate, String prefix, String breakType, String breakTypeString) { 136 Double min = (Double ) context.get(prefix + "min"); 137 Double max = (Double ) context.get(prefix + "max"); 138 if (min != null || max != null) { 139 if (min != null && max != null) { 140 if (min.doubleValue() <= max.doubleValue() || max.doubleValue() == 0) { 141 try { 142 String newSeqId = delegator.getNextSeqId("QuantityBreak"); 143 GenericValue weightBreak = delegator.makeValue("QuantityBreak", null); 144 weightBreak.set("quantityBreakId", newSeqId); 145 weightBreak.set("quantityBreakTypeId", "SHIP_" + breakType.toUpperCase()); 146 weightBreak.set("fromQuantity", min); 147 weightBreak.set("thruQuantity", max); 148 estimate.set(breakType + "BreakId", newSeqId); 149 estimate.set(breakType + "UnitPrice", (Double ) context.get(prefix + "price")); 150 if (context.containsKey(prefix + "uom")) { 151 estimate.set(breakType + "UomId", (String ) context.get(prefix + "uom")); 152 } 153 storeAll.add(0, weightBreak); 154 } 155 catch ( Exception e ) { 156 Debug.logError(e, module); 157 } 158 } 159 else { 160 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 161 result.put(ModelService.ERROR_MESSAGE, "Max " + breakTypeString + 162 " must not be less than Min " + breakTypeString + "."); 163 return false; 164 } 165 } 166 else { 167 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 168 result.put(ModelService.ERROR_MESSAGE, breakTypeString+" Span Requires BOTH Fields."); 169 return false; 170 } 171 } 172 return true; 173 } 174 175 public static Map calcShipmentCostEstimate(DispatchContext dctx, Map context) { 177 GenericDelegator delegator = dctx.getDelegator(); 178 179 String productStoreId = (String ) context.get("productStoreId"); 181 String carrierRoleTypeId = (String ) context.get("carrierRoleTypeId"); 182 String carrierPartyId = (String ) context.get("carrierPartyId"); 183 String shipmentMethodTypeId = (String ) context.get("shipmentMethodTypeId"); 184 String shippingContactMechId = (String ) context.get("shippingContactMechId"); 185 186 List shippableItemInfo = (List) context.get("shippableItemInfo"); 187 190 Double shippableTotal = (Double ) context.get("shippableTotal"); 191 Double shippableQuantity = (Double ) context.get("shippableQuantity"); 192 Double shippableWeight = (Double ) context.get("shippableWeight"); 193 Double initialEstimateAmt = (Double ) context.get("initialEstimateAmt"); 194 195 if (shippableTotal == null) { 196 shippableTotal = new Double (0.00); 197 } 198 if (shippableQuantity == null) { 199 shippableQuantity = new Double (0.00); 200 } 201 if (shippableWeight == null) { 202 shippableWeight = new Double (0.00); 203 } 204 205 Map estFields = UtilMisc.toMap("productStoreId", productStoreId, "shipmentMethodTypeId", shipmentMethodTypeId, 207 "carrierPartyId", carrierPartyId, "carrierRoleTypeId", carrierRoleTypeId); 208 209 Collection estimates = null; 210 try { 211 estimates = delegator.findByAnd("ShipmentCostEstimate", estFields); 212 } catch (GenericEntityException e) { 213 Debug.logError(e, module); 214 return ServiceUtil.returnError("Unable to locate estimates from database"); 215 } 216 if (estimates == null || estimates.size() < 1) { 217 if (initialEstimateAmt == null || initialEstimateAmt.doubleValue() == 0.00) { 218 Debug.logWarning("Using the passed context : " + context, module); 219 Debug.logWarning("No shipping estimates found; the shipping amount returned is 0!", module); 220 } 221 222 Map respNow = ServiceUtil.returnSuccess(); 223 respNow.put("shippingEstimateAmount", new Double (0.00)); 224 return respNow; 225 } 226 227 GenericValue shipAddress = null; 229 230 try { 231 shipAddress = delegator.findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId", shippingContactMechId)); 232 } catch (GenericEntityException e) { 233 Debug.logError(e, module); 234 return ServiceUtil.returnError("Cannot get shipping address entity"); 235 } 236 237 ArrayList estimateList = new ArrayList(); 239 Iterator i = estimates.iterator(); 240 241 while (i.hasNext()) { 242 GenericValue thisEstimate = (GenericValue) i.next(); 243 String toGeo = thisEstimate.getString("geoIdTo"); 244 List toGeoList = GeoWorker.expandGeoGroup(toGeo, delegator); 245 246 if (toGeoList == null || toGeoList.size() == 0 || 248 GeoWorker.containsGeo(toGeoList, shipAddress.getString("countryGeoId"), delegator) || 249 GeoWorker.containsGeo(toGeoList, shipAddress.getString("stateProvinceGeoId"), delegator) || 250 GeoWorker.containsGeo(toGeoList, shipAddress.getString("postalCodeGeoId"), delegator)) { 251 252 257 258 GenericValue wv = null; 259 GenericValue qv = null; 260 GenericValue pv = null; 261 262 try { 263 wv = thisEstimate.getRelatedOne("WeightQuantityBreak"); 264 } catch (GenericEntityException e) { 265 } 266 try { 267 qv = thisEstimate.getRelatedOne("QuantityQuantityBreak"); 268 } catch (GenericEntityException e) { 269 } 270 try { 271 pv = thisEstimate.getRelatedOne("PriceQuantityBreak"); 272 } catch (GenericEntityException e) { 273 } 274 if (wv == null && qv == null && pv == null) { 275 estimateList.add(thisEstimate); 276 } else { 277 boolean useWeight = false; 279 boolean weightValid = false; 280 boolean useQty = false; 281 boolean qtyValid = false; 282 boolean usePrice = false; 283 boolean priceValid = false; 284 285 if (wv != null) { 286 useWeight = true; 287 double min = 0.0001; 288 double max = 0.0001; 289 290 try { 291 min = wv.getDouble("fromQuantity").doubleValue(); 292 max = wv.getDouble("thruQuantity").doubleValue(); 293 } catch (Exception e) { 294 } 295 if (shippableWeight.doubleValue() >= min && (max == 0 || shippableWeight.doubleValue() <= max)) 296 weightValid = true; 297 } 298 if (qv != null) { 299 useQty = true; 300 double min = 0.0001; 301 double max = 0.0001; 302 303 try { 304 min = qv.getDouble("fromQuantity").doubleValue(); 305 max = qv.getDouble("thruQuantity").doubleValue(); 306 } catch (Exception e) { 307 } 308 if (shippableQuantity.doubleValue() >= min && (max == 0 || shippableQuantity.doubleValue() <= max)) 309 qtyValid = true; 310 } 311 if (pv != null) { 312 usePrice = true; 313 double min = 0.0001; 314 double max = 0.0001; 315 316 try { 317 min = pv.getDouble("fromQuantity").doubleValue(); 318 max = pv.getDouble("thruQuantity").doubleValue(); 319 } catch (Exception e) { 320 } 321 if (shippableTotal.doubleValue() >= min && (max == 0 || shippableTotal.doubleValue() <= max)) 322 priceValid = true; 323 } 324 if ((useWeight && weightValid) || (useQty && qtyValid) || (usePrice && priceValid)) 326 estimateList.add(thisEstimate); 327 } 328 } 329 } 330 331 if (estimateList.size() < 1) { 332 return ServiceUtil.returnError("No shipping estimate found"); 333 } 334 335 List shippableItemSizes = new LinkedList(); 337 Map shippableFeatureMap = new HashMap(); 338 if (shippableItemInfo != null) { 339 Iterator sii = shippableItemInfo.iterator(); 340 while (sii.hasNext()) { 341 Map itemMap = (Map) sii.next(); 342 343 Double itemSize = (Double ) itemMap.get("size"); 345 if (itemSize != null) { 346 shippableItemSizes.add(itemSize); 347 } 348 349 Double quantity = (Double ) itemMap.get("quantity"); 351 Set featureSet = (Set) itemMap.get("featureSet"); 352 if (featureSet != null && featureSet.size() > 0) { 353 Iterator fi = featureSet.iterator(); 354 while (fi.hasNext()) { 355 String featureId = (String ) fi.next(); 356 Double featureQuantity = (Double ) shippableFeatureMap.get(featureId); 357 if (featureQuantity == null) { 358 featureQuantity = new Double (0.00); 359 } 360 featureQuantity = new Double (featureQuantity.doubleValue() + quantity.doubleValue()); 361 shippableFeatureMap.put(featureId, featureQuantity); 362 } 363 } 364 365 } 366 } 367 368 double PRIORITY_PARTY = 9; 370 double PRIORITY_ROLE = 8; 371 double PRIORITY_GEO = 4; 372 double PRIORITY_WEIGHT = 1; 373 double PRIORITY_QTY = 1; 374 double PRIORITY_PRICE = 1; 375 376 int estimateIndex = 0; 377 378 if (estimateList.size() > 1) { 379 TreeMap estimatePriority = new TreeMap(); 380 382 for (int x = 0; x < estimateList.size(); x++) { 383 GenericValue currentEstimate = (GenericValue) estimateList.get(x); 384 385 int prioritySum = 0; 386 if (UtilValidate.isNotEmpty(currentEstimate.getString("partyId"))) 387 prioritySum += PRIORITY_PARTY; 388 if (UtilValidate.isNotEmpty(currentEstimate.getString("roleTypeId"))) 389 prioritySum += PRIORITY_ROLE; 390 if (UtilValidate.isNotEmpty(currentEstimate.getString("geoIdTo"))) 391 prioritySum += PRIORITY_GEO; 392 if (UtilValidate.isNotEmpty(currentEstimate.getString("weightBreakId"))) 393 prioritySum += PRIORITY_WEIGHT; 394 if (UtilValidate.isNotEmpty(currentEstimate.getString("quantityBreakId"))) 395 prioritySum += PRIORITY_QTY; 396 if (UtilValidate.isNotEmpty(currentEstimate.getString("priceBreakId"))) 397 prioritySum += PRIORITY_PRICE; 398 399 estimatePriority.put(new Integer (prioritySum), currentEstimate); 401 } 402 403 Object [] estimateArray = estimatePriority.values().toArray(); 405 estimateIndex = estimateList.indexOf(estimateArray[estimateArray.length - 1]); 406 } 407 408 GenericValue estimate = (GenericValue) estimateList.get(estimateIndex); 410 411 413 double orderFlat = 0.00; 415 if (estimate.getDouble("orderFlatPrice") != null) 416 orderFlat = estimate.getDouble("orderFlatPrice").doubleValue(); 417 418 double orderItemFlat = 0.00; 419 if (estimate.getDouble("orderItemFlatPrice") != null) 420 orderItemFlat = estimate.getDouble("orderItemFlatPrice").doubleValue(); 421 422 double orderPercent = 0.00; 423 if (estimate.getDouble("orderPricePercent") != null) 424 orderPercent = estimate.getDouble("orderPricePercent").doubleValue(); 425 426 double itemFlatAmount = shippableQuantity.doubleValue() * orderItemFlat; 427 double orderPercentage = shippableTotal.doubleValue() * (orderPercent / 100); 428 429 double flatTotal = orderFlat + itemFlatAmount + orderPercentage; 431 432 double weightUnit = 0.00; 434 if (estimate.getDouble("weightUnitPrice") != null) 435 weightUnit = estimate.getDouble("weightUnitPrice").doubleValue(); 436 437 double qtyUnit = 0.00; 438 if (estimate.getDouble("quantityUnitPrice") != null) 439 qtyUnit = estimate.getDouble("quantityUnitPrice").doubleValue(); 440 441 double priceUnit = 0.00; 442 if (estimate.getDouble("priceUnitPrice") != null) 443 priceUnit = estimate.getDouble("priceUnitPrice").doubleValue(); 444 445 double weightAmount = shippableWeight.doubleValue() * weightUnit; 446 double quantityAmount = shippableQuantity.doubleValue() * qtyUnit; 447 double priceAmount = shippableTotal.doubleValue() * priceUnit; 448 449 double spanTotal = weightAmount + quantityAmount + priceAmount; 451 452 double featureSurcharge = 0.00; 454 String featureGroupId = estimate.getString("productFeatureGroupId"); 455 Double featurePercent = estimate.getDouble("featurePercent"); 456 Double featurePrice = estimate.getDouble("featurePrice"); 457 if (featurePercent == null) { 458 featurePercent = new Double (0); 459 } 460 if (featurePrice == null) { 461 featurePrice = new Double (0.00); 462 } 463 464 if (featureGroupId != null && featureGroupId.length() > 0 && shippableFeatureMap != null) { 465 Iterator fii = shippableFeatureMap.keySet().iterator(); 466 while (fii.hasNext()) { 467 String featureId = (String ) fii.next(); 468 Double quantity = (Double ) shippableFeatureMap.get(featureId); 469 GenericValue appl = null; 470 Map fields = UtilMisc.toMap("productFeatureGroupId", featureGroupId, "productFeatureId", featureId); 471 try { 472 List appls = delegator.findByAndCache("ProductFeatureGroupAppl", fields); 473 appls = EntityUtil.filterByDate(appls); 474 appl = EntityUtil.getFirst(appls); 475 } catch (GenericEntityException e) { 476 Debug.logError(e, "Unable to lookup feature/group" + fields, module); 477 } 478 if (appl != null) { 479 featureSurcharge += (shippableTotal.doubleValue() * (featurePercent.doubleValue() / 100) * quantity.doubleValue()); 480 featureSurcharge += featurePrice.doubleValue() * quantity.doubleValue(); 481 } 482 } 483 } 484 485 double sizeSurcharge = 0.00; 487 Double sizeUnit = estimate.getDouble("oversizeUnit"); 488 Double sizePrice = estimate.getDouble("oversizePrice"); 489 if (sizeUnit != null && sizeUnit.doubleValue() > 0) { 490 if (shippableItemSizes != null) { 491 Iterator isi = shippableItemSizes.iterator(); 492 while (isi.hasNext()) { 493 Double size = (Double ) isi.next(); 494 if (size != null && size.doubleValue() >= sizeUnit.doubleValue()) { 495 sizeSurcharge += sizePrice.doubleValue(); 496 } 497 } 498 } 499 } 500 501 double surchargeTotal = featureSurcharge + sizeSurcharge; 503 504 double shippingTotal = spanTotal + flatTotal + surchargeTotal; 506 507 Map responseResult = ServiceUtil.returnSuccess(); 509 responseResult.put("shippingEstimateAmount", new Double (shippingTotal)); 510 return responseResult; 511 } 512 513 public static Map fillShipmentStagingTables(DispatchContext dctx, Map context) { 514 GenericDelegator delegator = dctx.getDelegator(); 515 String shipmentId = (String ) context.get("shipmentId"); 516 517 GenericValue shipment = null; 518 if (shipmentId != null) { 519 try { 520 shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId)); 521 } catch (GenericEntityException e) { 522 Debug.logError(e, module); 523 return ServiceUtil.returnError(e.getMessage()); 524 } 525 } 526 if (shipment == null) { 527 return ServiceUtil.returnError("No shipment found!"); 528 } 529 530 String shipmentStatusId = shipment.getString("statusId"); 531 if ("SHIPMENT_PACKED".equals(shipmentStatusId)) { 532 GenericValue address = null; 533 try { 534 address = shipment.getRelatedOne("DestinationPostalAddress"); 535 } catch (GenericEntityException e) { 536 Debug.logError(e, module); 537 return ServiceUtil.returnError(e.getMessage()); 538 } 539 if (address == null) { 540 return ServiceUtil.returnError("No address found for shipment!"); 541 } 542 543 List packages = null; 544 try { 545 packages = shipment.getRelated("ShipmentPackage") ; 546 } catch (GenericEntityException e) { 547 Debug.logError(e, module); 548 return ServiceUtil.returnError(e.getMessage()); 549 } 550 551 if (packages == null || packages.size() == 0) { 552 return ServiceUtil.returnError("No packages are available for shipping!"); 553 } 554 555 List routeSegs = null; 556 try { 557 routeSegs = shipment.getRelated("ShipmentRouteSegment"); 558 } catch (GenericEntityException e) { 559 Debug.logError(e, module); 560 return ServiceUtil.returnError(e.getMessage()); 561 } 562 GenericValue routeSeg = EntityUtil.getFirst(routeSegs); 563 564 List toStore = new ArrayList(); 566 567 String shipGroupSeqId = shipment.getString("primaryShipGroupSeqId"); 568 String orderId = shipment.getString("primaryOrderId"); 569 String orderInfoKey = orderId + "/" + shipGroupSeqId; 570 571 GenericValue stageShip = delegator.makeValue("OdbcShipmentOut", null); 573 stageShip.set("shipmentId", shipment.get("shipmentId")); 574 stageShip.set("partyId", shipment.get("partyIdTo")); 575 stageShip.set("carrierPartyId", routeSeg.get("carrierPartyId")); 576 stageShip.set("shipmentMethodTypeId", routeSeg.get("shipmentMethodTypeId")); 577 stageShip.set("toName", address.get("toName")); 578 stageShip.set("attnName", address.get("attnName")); 579 stageShip.set("address1", address.get("address1")); 580 stageShip.set("address2", address.get("address2")); 581 stageShip.set("directions", address.get("directions")); 582 stageShip.set("city", address.get("city")); 583 stageShip.set("postalCode", address.get("postalCode")); 584 stageShip.set("postalCodeExt", address.get("postalCodeExt")); 585 stageShip.set("countryGeoId", address.get("countryGeoId")); 586 stageShip.set("stateProvinceGeoId", address.get("stateProvinceGeoId")); 587 stageShip.set("numberOfPackages", new Long (packages.size())); 588 stageShip.set("handlingInstructions", shipment.get("handlingInstructions")); 589 toStore.add(stageShip); 590 591 592 Iterator p = packages.iterator(); 593 while (p.hasNext()) { 594 GenericValue shipmentPkg = (GenericValue) p.next(); 595 GenericValue stagePkg = delegator.makeValue("OdbcPackageOut", null); 596 stagePkg.set("shipmentId", shipmentPkg.get("shipmentId")); 597 stagePkg.set("shipmentPackageSeqId", shipmentPkg.get("shipmentPackageSeqId")); 598 stagePkg.set("orderId", shipment.get("primaryOrderId")); 599 stagePkg.set("shipGroupSeqId", shipment.get("primaryShipGroupSeqId")); 600 stagePkg.set("shipmentBoxTypeId", shipmentPkg.get("shipmentBoxTypeId")); 601 stagePkg.set("weight", shipmentPkg.get("weight")); 602 toStore.add(stagePkg); 603 } 604 605 try { 606 delegator.storeAll(toStore); 607 } catch (GenericEntityException e) { 608 Debug.logError(e, module); 609 return ServiceUtil.returnError(e.getMessage()); 610 } 611 } else { 612 Debug.logWarning("Shipment #" + shipmentId + " is not available for shipment; not setting in staging tables.", module); 613 } 614 615 return ServiceUtil.returnSuccess(); 616 } 617 618 public static Map updateShipmentsFromStaging(DispatchContext dctx, Map context) { 619 LocalDispatcher dispatcher = dctx.getDispatcher(); 620 GenericDelegator delegator = dctx.getDelegator(); 621 GenericValue userLogin = (GenericValue) context.get("userLogin"); 622 623 List orderBy = UtilMisc.toList("shipmentId", "shipmentPackageSeqId", "voidIndicator"); 624 Map shipmentMap = FastMap.newInstance(); 625 626 EntityListIterator eli = null; 627 try { 628 eli = delegator.findListIteratorByCondition("OdbcPackageIn", null, null, orderBy); 629 GenericValue pkgInfo; 630 while ((pkgInfo = (GenericValue) eli.next()) != null) { 631 String packageSeqId = pkgInfo.getString("shipmentPackageSeqId"); 632 String shipmentId = pkgInfo.getString("shipmentId"); 633 634 GenericValue shipmentPackage = delegator.findByPrimaryKey("ShipmentPackage", 636 UtilMisc.toMap("shipmentId", shipmentId, "shipmentPackageSeqId", packageSeqId)); 637 638 if (shipmentPackage != null) { 639 if ("00001".equals(packageSeqId)) { 640 GenericValue rtSeg = null; 642 try { 643 rtSeg = delegator.findByPrimaryKey("ShipmentRouteSegment", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", "00001")); 644 } catch (GenericEntityException e) { 645 Debug.logError(e, module); 646 return ServiceUtil.returnError(e.getMessage()); 647 } 648 649 if (rtSeg == null) { 650 rtSeg = delegator.makeValue("ShipmentRouteSegment", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", "00001")); 651 try { 652 delegator.create(rtSeg); 653 } catch (GenericEntityException e) { 654 Debug.logError(e, module); 655 return ServiceUtil.returnError(e.getMessage()); 656 } 657 } 658 659 rtSeg.set("actualStartDate", pkgInfo.get("shippedDate")); 660 rtSeg.set("billingWeight", pkgInfo.get("billingWeight")); 661 rtSeg.set("actualCost", pkgInfo.get("shippingTotal")); 662 rtSeg.set("trackingIdNumber", pkgInfo.get("trackingNumber")); 663 try { 664 delegator.store(rtSeg); 665 } catch (GenericEntityException e) { 666 Debug.logError(e, module); 667 return ServiceUtil.returnError(e.getMessage()); 668 } 669 } 670 671 Map pkgCtx = FastMap.newInstance(); 672 pkgCtx.put("shipmentId", shipmentId); 673 pkgCtx.put("shipmentPackageSeqId", packageSeqId); 674 675 GenericValue pkg = null; 677 try { 678 pkg = delegator.findByPrimaryKey("ShipmentPackage", pkgCtx); 679 } catch (GenericEntityException e) { 680 Debug.logError(e, module); 681 return ServiceUtil.returnError(e.getMessage()); 682 } 683 684 if (pkg == null) { 685 return ServiceUtil.returnError("Package not found! - " + pkgCtx); 686 } 687 688 pkg.set("weight", pkgInfo.get("packageWeight")); 689 try { 690 delegator.store(pkg); 691 } catch (GenericEntityException e) { 692 Debug.logError(e, module); 693 return ServiceUtil.returnError(e.getMessage()); 694 } 695 696 pkgCtx.put("shipmentRouteSegmentId", "00001"); 698 GenericValue pkgRtSeg = null; 699 try { 700 pkgRtSeg = delegator.findByPrimaryKey("ShipmentPackageRouteSeg", pkgCtx); 701 } catch (GenericEntityException e) { 702 Debug.logError(e, module); 703 return ServiceUtil.returnError(e.getMessage()); 704 } 705 706 if (pkgRtSeg == null) { 707 pkgRtSeg = delegator.makeValue("ShipmentPackageRouteSeg", pkgCtx); 708 try { 709 delegator.create(pkgRtSeg); 710 } catch (GenericEntityException e) { 711 Debug.logError(e, module); 712 return ServiceUtil.returnError(e.getMessage()); 713 } 714 } 715 716 pkgRtSeg.set("trackingCode", pkgInfo.get("trackingNumber")); 717 pkgRtSeg.set("boxNumber", pkgInfo.get("shipmentPackageSeqId")); 718 pkgRtSeg.set("packageServiceCost", pkgInfo.get("packageTotal")); 719 try { 720 delegator.store(pkgRtSeg); 721 } catch (GenericEntityException e) { 722 Debug.logError(e, module); 723 return ServiceUtil.returnError(e.getMessage()); 724 } 725 shipmentMap.put(shipmentId, pkgInfo.get("voidIndicator")); 726 } 727 } 728 } catch (GenericEntityException e) { 729 Debug.logError(e, module); 730 return ServiceUtil.returnError(e.getMessage()); 731 } finally { 732 if (eli != null) { 733 try { 734 eli.close(); 735 } catch (GenericEntityException e) { 736 Debug.logError(e, module); 737 } 738 } 739 } 740 741 Iterator i = shipmentMap.keySet().iterator(); 743 while (i.hasNext()) { 744 String shipmentId = (String ) i.next(); 745 String voidInd = (String ) shipmentMap.get(shipmentId); 746 Map shipCtx = FastMap.newInstance(); 747 shipCtx.put("shipmentId", shipmentId); 748 if ("Y".equals(voidInd)) { 749 shipCtx.put("statusId", "SHIPMENT_CANCELLED"); 750 } else { 751 shipCtx.put("statusId", "SHIPMENT_SHIPPED"); 752 } 753 shipCtx.put("userLogin", userLogin); 754 Map shipResp = null; 755 try { 756 shipResp = dispatcher.runSync("updateShipment", shipCtx); 757 } catch (GenericServiceException e) { 758 Debug.logError(e, module); 759 return ServiceUtil.returnError(e.getMessage()); 760 } 761 if (ServiceUtil.isError(shipResp)) { 762 return ServiceUtil.returnError(ServiceUtil.getErrorMessage(shipResp)); 763 } 764 765 Map clearResp = null; 767 try { 768 clearResp = dispatcher.runSync("clearShipmentStaging", UtilMisc.toMap("shipmentId", shipmentId, "userLogin", userLogin)); 769 } catch (GenericServiceException e) { 770 Debug.logError(e, module); 771 return ServiceUtil.returnError(e.getMessage()); 772 } 773 if (ServiceUtil.isError(clearResp)) { 774 return ServiceUtil.returnError(ServiceUtil.getErrorMessage(clearResp)); 775 } 776 } 777 778 return ServiceUtil.returnSuccess(); 779 } 780 781 public static Map clearShipmentStagingInfo(DispatchContext dctx, Map context) { 782 GenericDelegator delegator = dctx.getDelegator(); 783 String shipmentId = (String ) context.get("shipmentId"); 784 try { 785 delegator.removeByAnd("OdbcPackageIn", UtilMisc.toMap("shipmentId", shipmentId)); 786 delegator.removeByAnd("OdbcPackageOut", UtilMisc.toMap("shipmentId", shipmentId)); 787 delegator.removeByAnd("OdbcShipmentOut", UtilMisc.toMap("shipmentId", shipmentId)); 788 } catch (GenericEntityException e) { 789 Debug.logError(e, module); 790 return ServiceUtil.returnError(e.getMessage()); 791 } 792 return ServiceUtil.returnSuccess(); 793 } 794 795 802 public static Map updatePurchaseShipmentFromReceipt(DispatchContext dctx, Map context) { 803 GenericDelegator delegator = dctx.getDelegator(); 804 LocalDispatcher dispatcher = dctx.getDispatcher(); 805 String shipmentId = (String ) context.get("shipmentId"); 806 GenericValue userLogin = (GenericValue) context.get("userLogin"); 807 try { 808 List shipmentAndItems = delegator.findByAnd("ShipmentAndItem", UtilMisc.toMap("shipmentId", shipmentId, "statusId", "PURCH_SHIP_SHIPPED")); 809 if (shipmentAndItems.size() == 0) return ServiceUtil.returnSuccess(); 810 List shipmentReceipts = delegator.findByAnd("ShipmentReceipt", UtilMisc.toMap("shipmentId", shipmentId)); 811 if (shipmentReceipts.size() == 0) return ServiceUtil.returnSuccess(); 812 813 Map shippedCountMap = new HashMap(); 815 Iterator iter = shipmentAndItems.iterator(); 816 while (iter.hasNext()) { 817 GenericValue item = (GenericValue) iter.next(); 818 double shippedQuantity = item.getDouble("quantity").doubleValue(); 819 Double quantity = (Double ) shippedCountMap.get(item.getString("productId")); 820 quantity = new Double (quantity == null ? shippedQuantity : shippedQuantity + quantity.doubleValue()); 821 shippedCountMap.put(item.getString("productId"), quantity); 822 } 823 824 Map receivedCountMap = new HashMap(); 826 iter = shipmentReceipts.iterator(); 827 while (iter.hasNext()) { 828 GenericValue item = (GenericValue) iter.next(); 829 double receivedQuantity = item.getDouble("quantityAccepted").doubleValue(); 830 Double quantity = (Double ) receivedCountMap.get(item.getString("productId")); 831 quantity = new Double (quantity == null ? receivedQuantity : receivedQuantity + quantity.doubleValue()); 832 receivedCountMap.put(item.getString("productId"), quantity); 833 } 834 835 if (!shippedCountMap.equals(receivedCountMap)) { 837 return ServiceUtil.returnSuccess(); 838 } 839 840 dispatcher.runSync("updateShipment", UtilMisc.toMap("shipmentId", shipmentId, "statusId", "PURCH_SHIP_RECEIVED", "userLogin", userLogin)); 842 } catch (GenericEntityException e) { 843 Debug.logError(e, module); 844 return ServiceUtil.returnError(e.getMessage()); 845 } catch (GenericServiceException se) { 846 Debug.logError(se, module); 847 return ServiceUtil.returnError(se.getMessage()); 848 } 849 return ServiceUtil.returnSuccess("Intentional error at end to keep from committing."); 850 } 851 852 public static Map duplicateShipmentRouteSegment(DispatchContext dctx, Map context) { 853 GenericDelegator delegator = dctx.getDelegator(); 854 LocalDispatcher dispatcher = dctx.getDispatcher(); 855 GenericValue userLogin = (GenericValue) context.get("userLogin"); 856 857 String shipmentId = (String ) context.get("shipmentId"); 858 String shipmentRouteSegmentId = (String ) context.get("shipmentRouteSegmentId"); 859 860 Map results = ServiceUtil.returnSuccess(); 861 862 try { 863 GenericValue shipmentRouteSeg = delegator.findByPrimaryKey("ShipmentRouteSegment", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId)); 864 if (shipmentRouteSeg == null) { 865 return ServiceUtil.returnError("Shipment Route Segment not found for shipment [" + shipmentId + "] route segment [" + shipmentRouteSegmentId + "]"); 866 } 867 868 Map params = UtilMisc.toMap("shipmentId", shipmentId, "carrierPartyId", shipmentRouteSeg.getString("carrierPartyId"), "shipmentMethodTypeId", shipmentRouteSeg.getString("shipmentMethodTypeId"), 869 "originFacilityId", shipmentRouteSeg.getString("originFacilityId"), "originContactMechId", shipmentRouteSeg.getString("originContactMechId"), 870 "originTelecomNumberId", shipmentRouteSeg.getString("originTelecomNumberId")); 871 params.put("destFacilityId", shipmentRouteSeg.getString("destFacilityId")); 872 params.put("destContactMechId", shipmentRouteSeg.getString("destContactMechId")); 873 params.put("destTelecomNumberId", shipmentRouteSeg.getString("destTelecomNumberId")); 874 params.put("billingWeight", shipmentRouteSeg.get("billingWeight")); 875 params.put("billingWeightUomId", shipmentRouteSeg.get("billingWeightUomId")); 876 params.put("userLogin", userLogin); 877 878 Map tmpResult = dispatcher.runSync("createShipmentRouteSegment", params); 879 if (ServiceUtil.isError(tmpResult)) { 880 return tmpResult; 881 } else { 882 results.put("newShipmentRouteSegmentId", tmpResult.get("shipmentRouteSegmentId")); 883 return results; 884 } 885 } catch (GenericEntityException ex) { 886 return ServiceUtil.returnError(ex.getMessage()); 887 } catch (GenericServiceException ex) { 888 return ServiceUtil.returnError(ex.getMessage()); 889 } 890 } 891 894 public static Map quickScheduleShipmentRouteSegment(DispatchContext dctx, Map context) { 895 GenericDelegator delegator = dctx.getDelegator(); 896 LocalDispatcher dispatcher = dctx.getDispatcher(); 897 GenericValue userLogin = (GenericValue) context.get("userLogin"); 898 899 String shipmentId = (String ) context.get("shipmentId"); 900 String shipmentRouteSegmentId = (String ) context.get("shipmentRouteSegmentId"); 901 String carrierPartyId = null; 902 903 try { 905 GenericValue shipmentRouteSegment = shipmentRouteSegment = delegator.findByPrimaryKeyCache("ShipmentRouteSegment", 906 UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId)); 907 carrierPartyId = shipmentRouteSegment.getString("carrierPartyId"); 908 } catch (GenericEntityException e) { 909 Debug.logError(e, module); 910 return ServiceUtil.returnError(e.getMessage()); 911 } 912 913 try { 917 Map input = UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId, "userLogin", userLogin); 918 if (carrierPartyId.equals("DHL")) { 920 dispatcher.runAsync("dhlShipmentConfirm", input); 921 } else { 922 Debug.logError(carrierPartyId + " is not supported at this time. Sorry.", module); 923 } 924 } catch (GenericServiceException se) { 925 Debug.logError(se, se.getMessage(), module); 926 } 927 928 return ServiceUtil.returnSuccess(); 930 } 931 } 932 | Popular Tags |