1 17 18 package org.ofbiz.manufacturing.bom; 19 20 import java.sql.Timestamp ; 21 import java.util.ArrayList ; 22 import java.util.Date ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Locale ; 27 import java.util.Map ; 28 29 import org.ofbiz.base.util.Debug; 30 import org.ofbiz.base.util.UtilMisc; 31 import org.ofbiz.base.util.UtilProperties; 32 import org.ofbiz.base.util.UtilValidate; 33 import org.ofbiz.entity.GenericDelegator; 34 import org.ofbiz.entity.GenericEntityException; 35 import org.ofbiz.entity.GenericValue; 36 import org.ofbiz.entity.util.EntityUtil; 37 import org.ofbiz.order.order.OrderReadHelper; 38 import org.ofbiz.security.Security; 39 import org.ofbiz.service.DispatchContext; 40 import org.ofbiz.service.GenericServiceException; 41 import org.ofbiz.service.LocalDispatcher; 42 import org.ofbiz.service.ServiceUtil; 43 44 49 public class BOMServices { 50 51 public static final String module = BOMServices.class.getName(); 52 public static final String resource = "ManufacturingUiLabels"; 53 54 62 public static Map getMaxDepth(DispatchContext dctx, Map context) { 63 64 Map result = new HashMap (); 65 Security security = dctx.getSecurity(); 66 GenericDelegator delegator = dctx.getDelegator(); 67 LocalDispatcher dispatcher = dctx.getDispatcher(); 68 String productId = (String ) context.get("productId"); 69 String fromDateStr = (String ) context.get("fromDate"); 70 String bomType = (String ) context.get("bomType"); 71 72 Date fromDate = null; 73 if (UtilValidate.isNotEmpty(fromDateStr)) { 74 try { 75 fromDate = Timestamp.valueOf(fromDateStr); 76 } catch (Exception e) { 77 } 78 } 79 if (fromDate == null) { 80 fromDate = new Date (); 81 } 82 List bomTypes = new ArrayList (); 83 if (bomType == null) { 84 try { 85 List bomTypesValues = delegator.findByAnd("ProductAssocType", UtilMisc.toMap("parentTypeId", "PRODUCT_COMPONENT")); 86 Iterator bomTypesValuesIt = bomTypesValues.iterator(); 87 while (bomTypesValuesIt.hasNext()) { 88 bomTypes.add(((GenericValue)bomTypesValuesIt.next()).getString("productAssocTypeId")); 89 } 90 } catch(GenericEntityException gee) { 91 return ServiceUtil.returnError("Error running max depth algorithm: " + gee.getMessage()); 92 } 93 } else { 94 bomTypes.add(bomType); 95 } 96 97 int depth = 0; 98 int maxDepth = 0; 99 Iterator bomTypesIt = bomTypes.iterator(); 100 try { 101 while (bomTypesIt.hasNext()) { 102 String oneBomType = (String )bomTypesIt.next(); 103 depth = BOMHelper.getMaxDepth(productId, oneBomType, fromDate, delegator); 104 if (depth > maxDepth) { 105 maxDepth = depth; 106 } 107 } 108 } catch(GenericEntityException gee) { 109 return ServiceUtil.returnError("Error running max depth algorithm: " + gee.getMessage()); 110 } 111 result.put("depth", new Integer (maxDepth)); 112 113 return result; 114 } 115 116 124 public static Map updateLowLevelCode(DispatchContext dctx, Map context) { 125 Map result = new HashMap (); 126 Security security = dctx.getSecurity(); 127 GenericDelegator delegator = dctx.getDelegator(); 128 LocalDispatcher dispatcher = dctx.getDispatcher(); 129 String productId = (String ) context.get("productIdTo"); 130 Boolean alsoComponents = (Boolean ) context.get("alsoComponents"); 131 if (alsoComponents == null) { 132 alsoComponents = new Boolean (true); 133 } 134 Boolean alsoVariants = (Boolean ) context.get("alsoVariants"); 135 if (alsoVariants == null) { 136 alsoVariants = new Boolean (true); 137 } 138 139 Integer llc = null; 140 try { 141 GenericValue product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); 142 Map depthResult = dispatcher.runSync("getMaxDepth", UtilMisc.toMap("productId", productId, "bomType", "MANUF_COMPONENT")); 143 llc = (Integer )depthResult.get("depth"); 144 List virtualProducts = delegator.findByAnd("ProductAssoc", UtilMisc.toMap("productIdTo", productId, "productAssocTypeId", "PRODUCT_VARIANT")); 147 if (virtualProducts != null) { 148 int virtualMaxDepth = 0; 149 Iterator virtualProductsIt = virtualProducts.iterator(); 150 while (virtualProductsIt.hasNext()) { 151 int virtualDepth = 0; 152 GenericValue oneVirtualProductAssoc = (GenericValue)virtualProductsIt.next(); 153 GenericValue virtualProduct = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", oneVirtualProductAssoc.getString("productId"))); 154 if (virtualProduct.get("billOfMaterialLevel") != null) { 155 virtualDepth = virtualProduct.getLong("billOfMaterialLevel").intValue(); 156 } else { 157 virtualDepth = 0; 158 } 159 if (virtualDepth > virtualMaxDepth) { 160 virtualMaxDepth = virtualDepth; 161 } 162 } 163 if (virtualMaxDepth > llc.intValue()) { 164 llc = new Integer (virtualMaxDepth); 165 } 166 } 167 product.set("billOfMaterialLevel", llc); 168 product.store(); 169 if (alsoComponents.booleanValue()) { 170 Map treeResult = dispatcher.runSync("getBOMTree", UtilMisc.toMap("productId", productId, "bomType", "MANUF_COMPONENT")); 171 BOMTree tree = (BOMTree)treeResult.get("tree"); 172 ArrayList products = new ArrayList (); 173 tree.print(products, llc.intValue()); 174 for (int i = 0; i < products.size(); i++) { 175 BOMNode oneNode = (BOMNode)products.get(i); 176 GenericValue oneProduct = oneNode.getProduct(); 177 int lev = 0; 178 if (oneProduct.get("billOfMaterialLevel") != null) { 179 lev = oneProduct.getLong("billOfMaterialLevel").intValue(); 180 } 181 if (lev < oneNode.getDepth()) { 182 oneProduct.set("billOfMaterialLevel", new Integer (oneNode.getDepth())); 183 oneProduct.store(); 184 } 185 } 186 } 187 if (alsoVariants.booleanValue()) { 188 List variantProducts = delegator.findByAnd("ProductAssoc", UtilMisc.toMap("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT")); 189 if (variantProducts != null) { 190 Iterator variantProductsIt = variantProducts.iterator(); 191 while (variantProductsIt.hasNext()) { 192 GenericValue oneVariantProductAssoc = (GenericValue)variantProductsIt.next(); 193 GenericValue variantProduct = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", oneVariantProductAssoc.getString("productId"))); 194 variantProduct.set("billOfMaterialLevel", llc); 195 variantProduct.store(); 196 } 197 } 198 } 199 } catch (Exception e) { 200 return ServiceUtil.returnError("Error running updateLowLevelCode: " + e.getMessage()); 201 } 202 result.put("lowLevelCode", llc); 203 return result; 204 } 205 206 212 public static Map initLowLevelCode(DispatchContext dctx, Map context) { 213 Map result = new HashMap (); 214 Security security = dctx.getSecurity(); 215 GenericDelegator delegator = dctx.getDelegator(); 216 LocalDispatcher dispatcher = dctx.getDispatcher(); 217 218 try { 219 List products = delegator.findAll("Product", UtilMisc.toList("isVirtual DESC")); 220 Iterator productsIt = products.iterator(); 221 Long zero = new Long (0); 222 List allProducts = new ArrayList (); 223 while (productsIt.hasNext()) { 224 GenericValue product = (GenericValue)productsIt.next(); 225 product.set("billOfMaterialLevel", zero); 226 allProducts.add(product); 227 } 228 delegator.storeAll(allProducts); 229 Debug.logInfo("Low Level Code set to 0 for all the products", module); 230 231 productsIt = products.iterator(); 232 while (productsIt.hasNext()) { 233 GenericValue product = (GenericValue)productsIt.next(); 234 try { 235 Map depthResult = dispatcher.runSync("updateLowLevelCode", UtilMisc.toMap("productIdTo", product.getString("productId"), "alsoComponents", Boolean.valueOf(false), "alsoVariants", Boolean.valueOf(false))); 236 Debug.logInfo("Product [" + product.getString("productId") + "] Low Level Code [" + depthResult.get("lowLevelCode") + "]", module); 237 } catch(Exception exc) { 238 Debug.logWarning(exc.getMessage(), module); 239 } 240 } 241 } catch (Exception e) { 243 return ServiceUtil.returnError("Error running initLowLevelCode: " + e.getMessage()); 244 } 245 return result; 246 } 247 248 256 public static Map searchDuplicatedAncestor(DispatchContext dctx, Map context) { 257 Map result = new HashMap (); 258 Security security = dctx.getSecurity(); 259 GenericDelegator delegator = dctx.getDelegator(); 260 LocalDispatcher dispatcher = dctx.getDispatcher(); 261 GenericValue userLogin = (GenericValue)context.get("userLogin"); 262 263 String productId = (String ) context.get("productId"); 264 String productIdKey = (String ) context.get("productIdTo"); 265 Timestamp fromDate = (Timestamp ) context.get("fromDate"); 266 String bomType = (String ) context.get("productAssocTypeId"); 267 if (fromDate == null) { 268 fromDate = Timestamp.valueOf((new Date ()).toString()); 269 } 270 GenericValue duplicatedProductAssoc = null; 271 try { 272 duplicatedProductAssoc = BOMHelper.searchDuplicatedAncestor(productId, productIdKey, bomType, fromDate, delegator, dispatcher, userLogin); 273 } catch(GenericEntityException gee) { 274 return ServiceUtil.returnError("Error running duplicated ancestor search: " + gee.getMessage()); 275 } 276 result.put("duplicatedProductAssoc", duplicatedProductAssoc); 277 return result; 278 } 279 280 290 public static Map getBOMTree(DispatchContext dctx, Map context) { 291 292 Map result = new HashMap (); 293 Security security = dctx.getSecurity(); 294 GenericDelegator delegator = dctx.getDelegator(); 295 LocalDispatcher dispatcher = dctx.getDispatcher(); 296 GenericValue userLogin = (GenericValue)context.get("userLogin"); 297 String productId = (String ) context.get("productId"); 298 String fromDateStr = (String ) context.get("fromDate"); 299 String bomType = (String ) context.get("bomType"); 300 Integer type = (Integer ) context.get("type"); 301 Double quantity = (Double ) context.get("quantity"); 302 Double amount = (Double ) context.get("amount"); 303 if (type == null) { 304 type = new Integer (0); 305 } 306 307 Date fromDate = null; 308 if (UtilValidate.isNotEmpty(fromDateStr)) { 309 try { 310 fromDate = Timestamp.valueOf(fromDateStr); 311 } catch (Exception e) { 312 } 313 } 314 if (fromDate == null) { 315 fromDate = new Date (); 316 } 317 318 BOMTree tree = null; 319 try { 320 tree = new BOMTree(productId, bomType, fromDate, type.intValue(), delegator, dispatcher, userLogin); 321 } catch(GenericEntityException gee) { 322 return ServiceUtil.returnError("Error creating bill of materials tree: " + gee.getMessage()); 323 } 324 if (tree != null && quantity != null) { 325 tree.setRootQuantity(quantity.doubleValue()); 326 } 327 if (tree != null && amount != null) { 328 tree.setRootAmount(amount.doubleValue()); 329 } 330 result.put("tree", tree); 331 332 return result; 333 } 334 335 342 public static Map getManufacturingComponents(DispatchContext dctx, Map context) { 343 344 Map result = new HashMap (); 345 Security security = dctx.getSecurity(); 346 GenericDelegator delegator = dctx.getDelegator(); 347 LocalDispatcher dispatcher = dctx.getDispatcher(); 348 Locale locale = (Locale ) context.get("locale"); 349 GenericValue userLogin = (GenericValue)context.get("userLogin"); 350 351 String productId = (String ) context.get("productId"); 352 Double quantity = (Double ) context.get("quantity"); 353 Double amount = (Double ) context.get("amount"); 354 String fromDateStr = (String ) context.get("fromDate"); 355 Boolean excludeWIPs = (Boolean ) context.get("excludeWIPs"); 356 357 if (quantity == null) { 358 quantity = new Double (1); 359 } 360 if (amount == null) { 361 amount = new Double (0); 362 } 363 364 Date fromDate = null; 365 if (UtilValidate.isNotEmpty(fromDateStr)) { 366 try { 367 fromDate = Timestamp.valueOf(fromDateStr); 368 } catch (Exception e) { 369 } 370 } 371 if (fromDate == null) { 372 fromDate = new Date (); 373 } 374 if (excludeWIPs == null) { 375 excludeWIPs = new Boolean (true); 376 } 377 378 BOMTree tree = null; 382 ArrayList components = new ArrayList (); 383 try { 384 tree = new BOMTree(productId, "MANUF_COMPONENT", fromDate, BOMTree.EXPLOSION_SINGLE_LEVEL, delegator, dispatcher, userLogin); 385 tree.setRootQuantity(quantity.doubleValue()); 386 tree.setRootAmount(amount.doubleValue()); 387 tree.print(components, excludeWIPs.booleanValue()); 388 if (components.size() > 0) components.remove(0); 389 } catch(GenericEntityException gee) { 390 return ServiceUtil.returnError("Error creating bill of materials tree: " + gee.getMessage()); 391 } 392 String workEffortId = null; 396 try { 397 Map routingInMap = UtilMisc.toMap("productId", productId, "ignoreDefaultRouting", "Y", "userLogin", userLogin); 398 Map routingOutMap = dispatcher.runSync("getProductRouting", routingInMap); 399 GenericValue routing = (GenericValue)routingOutMap.get("routing"); 400 if (routing == null) { 401 routingInMap = UtilMisc.toMap("productId", tree.getRoot().getProduct().getString("productId"), "userLogin", userLogin); 403 routingOutMap = dispatcher.runSync("getProductRouting", routingInMap); 404 routing = (GenericValue)routingOutMap.get("routing"); 405 } 406 if (routing != null) { 407 workEffortId = routing.getString("workEffortId"); 408 } 409 } catch(GenericServiceException gse) { 410 Debug.logWarning(gse.getMessage(), module); 411 } 412 if (workEffortId != null) { 413 result.put("workEffortId", workEffortId); 414 } 415 result.put("components", components); 416 417 List componentsMap = new ArrayList (); 419 Iterator componentsIt = components.iterator(); 420 while (componentsIt.hasNext()) { 421 Map componentMap = new HashMap (); 422 BOMNode node = (BOMNode)componentsIt.next(); 423 componentMap.put("product", node.getProduct()); 424 componentMap.put("quantity", new Double (node.getQuantity())); 425 componentsMap.add(componentMap); 426 } 427 result.put("componentsMap", componentsMap); 428 return result; 429 } 430 431 public static Map getNotAssembledComponents(DispatchContext dctx, Map context) { 432 Map result = new HashMap (); 433 Security security = dctx.getSecurity(); 434 GenericDelegator delegator = dctx.getDelegator(); 435 LocalDispatcher dispatcher = dctx.getDispatcher(); 436 String productId = (String ) context.get("productId"); 437 Double quantity = (Double ) context.get("quantity"); 438 Double amount = (Double ) context.get("amount"); 439 String fromDateStr = (String ) context.get("fromDate"); 440 GenericValue userLogin = (GenericValue)context.get("userLogin"); 441 442 if (quantity == null) { 443 quantity = new Double (1); 444 } 445 if (amount == null) { 446 amount = new Double (0); 447 } 448 449 Date fromDate = null; 450 if (UtilValidate.isNotEmpty(fromDateStr)) { 451 try { 452 fromDate = Timestamp.valueOf(fromDateStr); 453 } catch (Exception e) { 454 } 455 } 456 if (fromDate == null) { 457 fromDate = new Date (); 458 } 459 460 BOMTree tree = null; 461 ArrayList components = new ArrayList (); 462 ArrayList notAssembledComponents = new ArrayList (); 463 try { 464 tree = new BOMTree(productId, "MANUF_COMPONENT", fromDate, BOMTree.EXPLOSION_MANUFACTURING, delegator, dispatcher, userLogin); 465 tree.setRootQuantity(quantity.doubleValue()); 466 tree.setRootAmount(amount.doubleValue()); 467 tree.print(components); 468 } catch(GenericEntityException gee) { 469 return ServiceUtil.returnError("Error creating bill of materials tree: " + gee.getMessage()); 470 } 471 Iterator componentsIt = components.iterator(); 472 while (componentsIt.hasNext()) { 473 BOMNode oneComponent = (BOMNode)componentsIt.next(); 474 if (!oneComponent.isManufactured()) { 475 notAssembledComponents.add(oneComponent); 476 } 477 } 478 result.put("notAssembledComponents" , notAssembledComponents); 479 return result; 480 } 481 482 public static Map createShipmentPackages(DispatchContext dctx, Map context) { 486 Map result = new HashMap (); 487 Security security = dctx.getSecurity(); 488 GenericDelegator delegator = dctx.getDelegator(); 489 LocalDispatcher dispatcher = dctx.getDispatcher(); 490 Locale locale = (Locale ) context.get("locale"); 491 GenericValue userLogin = (GenericValue)context.get("userLogin"); 492 String shipmentId = (String ) context.get("shipmentId"); 493 494 try { 495 List packages = delegator.findByAnd("ShipmentPackage", UtilMisc.toMap("shipmentId", shipmentId)); 496 if (!UtilValidate.isEmpty(packages)) { 497 return ServiceUtil.returnError("Packages already found."); 498 } 499 } catch(GenericEntityException gee) { 500 return ServiceUtil.returnError("Error loading the ShipmentPackages"); 501 } 502 List shipmentItems = null; 504 try { 505 shipmentItems = delegator.findByAnd("ShipmentItem", UtilMisc.toMap("shipmentId", shipmentId)); 506 } catch(GenericEntityException gee) { 507 return ServiceUtil.returnError("Error loading the ShipmentItems"); 508 } 509 Iterator shipmentItemsIt = shipmentItems.iterator(); 510 HashMap orderReadHelpers = new HashMap (); 511 HashMap partyOrderShipments = new HashMap (); 512 while (shipmentItemsIt.hasNext()) { 513 GenericValue shipmentItem = (GenericValue)shipmentItemsIt.next(); 514 List orderShipments = null; 516 try { 517 orderShipments = delegator.findByAnd("OrderShipment", UtilMisc.toMap("shipmentId", shipmentId, "shipmentItemSeqId", shipmentItem.getString("shipmentItemSeqId"))); 518 } catch (GenericEntityException e) { 519 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingPackageConfiguratorError", locale)); 520 } 521 GenericValue orderShipment = org.ofbiz.entity.util.EntityUtil.getFirst(orderShipments); 522 if (orderShipment != null && !orderReadHelpers.containsKey(orderShipment.getString("orderId"))) { 523 orderReadHelpers.put(orderShipment.getString("orderId"), new OrderReadHelper(delegator, orderShipment.getString("orderId"))); 524 } 525 OrderReadHelper orderReadHelper = (OrderReadHelper)orderReadHelpers.get(orderShipment.getString("orderId")); 526 if (orderReadHelper != null) { 527 Map orderShipmentReadMap = UtilMisc.toMap("orderShipment", orderShipment, "orderReadHelper", orderReadHelper); 528 String partyId = (orderReadHelper.getPlacingParty() != null? orderReadHelper.getPlacingParty().getString("partyId"): null); if (partyId != null) { 530 if (!partyOrderShipments.containsKey(partyId)) { 531 ArrayList orderShipmentReadMapList = new ArrayList (); 532 partyOrderShipments.put(partyId, orderShipmentReadMapList); 533 } 534 ArrayList orderShipmentReadMapList = (ArrayList )partyOrderShipments.get(partyId); 535 orderShipmentReadMapList.add(orderShipmentReadMap); 536 } 537 } 538 } 539 Iterator partyOrderShipmentsIt = partyOrderShipments.entrySet().iterator(); 542 while (partyOrderShipmentsIt.hasNext()) { 543 Map.Entry partyOrderShipment = (Map.Entry )partyOrderShipmentsIt.next(); 544 String partyId = (String )partyOrderShipment.getKey(); 545 List orderShipmentReadMapList = (List )partyOrderShipment.getValue(); 546 for (int i = 0; i < orderShipmentReadMapList.size(); i++) { 547 Map orderShipmentReadMap = (Map )orderShipmentReadMapList.get(i); 548 GenericValue orderShipment = (GenericValue)orderShipmentReadMap.get("orderShipment"); 549 OrderReadHelper orderReadHelper = (OrderReadHelper)orderShipmentReadMap.get("orderReadHelper"); 550 GenericValue orderItem = orderReadHelper.getOrderItem(orderShipment.getString("orderItemSeqId")); 551 Map serviceContext = new HashMap (); 553 serviceContext.put("productId", orderItem.getString("productId")); 554 serviceContext.put("quantity", orderShipment.getDouble("quantity")); 555 Map resultService = null; 556 try { 557 resultService = dispatcher.runSync("getProductsInPackages", serviceContext); 558 } catch (GenericServiceException e) { 559 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingPackageConfiguratorError", locale)); 560 } 561 List productsInPackages = (List )resultService.get("productsInPackages"); 562 if (productsInPackages.size() == 1) { 563 BOMNode root = (BOMNode)productsInPackages.get(0); 564 String rootProductId = (root.getSubstitutedNode() != null? root.getSubstitutedNode().getProduct().getString("productId"): root.getProduct().getString("productId")); 565 if (orderItem.getString("productId").equals(rootProductId)) { 566 productsInPackages = null; 567 } 568 } 569 if (productsInPackages != null && productsInPackages.size() == 0) { 570 productsInPackages = null; 571 } 572 if (productsInPackages != null && productsInPackages.size() > 0) { 573 orderShipmentReadMap.put("productsInPackages", productsInPackages); 574 } 575 } 576 } 577 HashMap boxTypes = new HashMap (); 580 partyOrderShipmentsIt = partyOrderShipments.entrySet().iterator(); 581 while (partyOrderShipmentsIt.hasNext()) { 582 HashMap boxTypeContent = new HashMap (); 583 Map.Entry partyOrderShipment = (Map.Entry )partyOrderShipmentsIt.next(); 584 String partyId = (String )partyOrderShipment.getKey(); 585 List orderShipmentReadMapList = (List )partyOrderShipment.getValue(); 586 for (int i = 0; i < orderShipmentReadMapList.size(); i++) { 587 Map orderShipmentReadMap = (Map )orderShipmentReadMapList.get(i); 588 GenericValue orderShipment = (GenericValue)orderShipmentReadMap.get("orderShipment"); 589 OrderReadHelper orderReadHelper = (OrderReadHelper)orderShipmentReadMap.get("orderReadHelper"); 590 List productsInPackages = (List )orderShipmentReadMap.get("productsInPackages"); 591 if (productsInPackages != null) { 592 for (int j = 0; j < productsInPackages.size(); j++) { 595 BOMNode component = (BOMNode)productsInPackages.get(j); 596 HashMap boxTypeContentMap = new HashMap (); 597 boxTypeContentMap.put("content", orderShipmentReadMap); 598 boxTypeContentMap.put("componentIndex", new Integer (j)); 599 GenericValue product = component.getProduct(); 600 String boxTypeId = product.getString("shipmentBoxTypeId"); 601 if (boxTypeId != null) { 602 if (!boxTypes.containsKey(boxTypeId)) { 603 GenericValue boxType = null; 604 try { 605 boxType = delegator.findByPrimaryKey("ShipmentBoxType", UtilMisc.toMap("shipmentBoxTypeId", boxTypeId)); 606 } catch (GenericEntityException e) { 607 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingPackageConfiguratorError", locale)); 608 } 609 boxTypes.put(boxTypeId, boxType); 610 boxTypeContent.put(boxTypeId, new ArrayList ()); 611 } 612 GenericValue boxType = (GenericValue)boxTypes.get(boxTypeId); 613 List boxTypeContentList = (List )boxTypeContent.get(boxTypeId); 614 boxTypeContentList.add(boxTypeContentMap); 615 } 616 } 617 } else { 618 HashMap boxTypeContentMap = new HashMap (); 621 boxTypeContentMap.put("content", orderShipmentReadMap); 622 GenericValue orderItem = orderReadHelper.getOrderItem(orderShipment.getString("orderItemSeqId")); 623 GenericValue product = null; 624 try { 625 product = orderItem.getRelatedOne("Product"); 626 } catch (GenericEntityException e) { 627 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingPackageConfiguratorError", locale)); 628 } 629 String boxTypeId = product.getString("shipmentBoxTypeId"); 630 if (boxTypeId != null) { 631 if (!boxTypes.containsKey(boxTypeId)) { 632 GenericValue boxType = null; 633 try { 634 boxType = delegator.findByPrimaryKey("ShipmentBoxType", UtilMisc.toMap("shipmentBoxTypeId", boxTypeId)); 635 } catch (GenericEntityException e) { 636 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingPackageConfiguratorError", locale)); 637 } 638 639 boxTypes.put(boxTypeId, boxType); 640 boxTypeContent.put(boxTypeId, new ArrayList ()); 641 } 642 GenericValue boxType = (GenericValue)boxTypes.get(boxTypeId); 643 List boxTypeContentList = (List )boxTypeContent.get(boxTypeId); 644 boxTypeContentList.add(boxTypeContentMap); 645 } 646 } 647 } 648 Iterator boxTypeContentIt = boxTypeContent.entrySet().iterator(); 650 while (boxTypeContentIt.hasNext()) { 651 Map.Entry boxTypeContentEntry = (Map.Entry )boxTypeContentIt.next(); 652 String boxTypeId = (String )boxTypeContentEntry.getKey(); 653 List contentList = (List )boxTypeContentEntry.getValue(); 654 GenericValue boxType = (GenericValue)boxTypes.get(boxTypeId); 655 Double boxWidth = boxType.getDouble("boxLength"); 656 double totalWidth = 0; 657 double boxWidthDbl = 0; 658 if (boxWidth != null) { 659 boxWidthDbl = boxWidth.doubleValue(); 660 } 661 String shipmentPackageSeqId = null; 662 for (int i = 0; i < contentList.size(); i++) { 663 Map contentMap = (Map )contentList.get(i); 664 Map content = (Map )contentMap.get("content"); 665 OrderReadHelper orderReadHelper = (OrderReadHelper)content.get("orderReadHelper"); 666 List productsInPackages = (List )content.get("productsInPackages"); 667 GenericValue orderShipment = (GenericValue)content.get("orderShipment"); 668 669 GenericValue product = null; 670 double quantity = 0; 671 boolean subProduct = contentMap.containsKey("componentIndex"); 672 if (subProduct) { 673 Integer index = (Integer )contentMap.get("componentIndex"); 675 BOMNode component = (BOMNode)productsInPackages.get(index.intValue()); 676 product = component.getProduct(); 677 quantity = component.getQuantity(); 678 } else { 679 GenericValue orderItem = orderReadHelper.getOrderItem(orderShipment.getString("orderItemSeqId")); 681 try { 682 product = orderItem.getRelatedOne("Product"); 683 } catch (GenericEntityException e) { 684 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingPackageConfiguratorError", locale)); 685 } 686 quantity = orderShipment.getDouble("quantity").doubleValue(); 687 } 688 689 Double productDepth = product.getDouble("shippingDepth"); 690 if (productDepth == null) { 691 productDepth = product.getDouble("productDepth"); 692 } 693 double productDepthDbl = 1; 694 if (productDepth != null) { 695 productDepthDbl = productDepth.doubleValue(); 696 } 697 698 int firstMaxNumOfProducts = (int)((boxWidthDbl - totalWidth) / productDepthDbl); 699 if (firstMaxNumOfProducts == 0) firstMaxNumOfProducts = 1; 700 int maxNumOfProducts = (int)(boxWidthDbl / productDepthDbl); 702 if (maxNumOfProducts == 0) maxNumOfProducts = 1; 703 704 double remQuantity = quantity; 705 boolean isFirst = true; 706 while (remQuantity > 0) { 707 int maxQuantity = 0; 708 if (isFirst) { 709 maxQuantity = firstMaxNumOfProducts; 710 isFirst = false; 711 } else { 712 maxQuantity = maxNumOfProducts; 713 } 714 double qty = (remQuantity < maxQuantity? remQuantity: maxQuantity); 715 if (shipmentPackageSeqId == null) { 717 try { 718 Map resultService = dispatcher.runSync("createShipmentPackage", UtilMisc.toMap("shipmentId", orderShipment.getString("shipmentId"), "shipmentBoxTypeId", boxTypeId, "userLogin", userLogin)); 719 shipmentPackageSeqId = (String )resultService.get("shipmentPackageSeqId"); 720 } catch (GenericServiceException e) { 721 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingPackageConfiguratorError", locale)); 722 } 723 totalWidth = 0; 724 } 725 try { 726 Map inputMap = null; 727 if (subProduct) { 728 inputMap = UtilMisc.toMap("shipmentId", orderShipment.getString("shipmentId"), 729 "shipmentPackageSeqId", shipmentPackageSeqId, 730 "shipmentItemSeqId", orderShipment.getString("shipmentItemSeqId"), 731 "subProductId", product.getString("productId"), 732 "userLogin", userLogin, 733 "subProductQuantity", new Double (qty)); 734 } else { 735 inputMap = UtilMisc.toMap("shipmentId", orderShipment.getString("shipmentId"), 736 "shipmentPackageSeqId", shipmentPackageSeqId, 737 "shipmentItemSeqId", orderShipment.getString("shipmentItemSeqId"), 738 "userLogin", userLogin, 739 "quantity", new Double (qty)); 740 } 741 Map resultService = dispatcher.runSync("createShipmentPackageContent", inputMap); 742 } catch (GenericServiceException e) { 743 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingPackageConfiguratorError", locale)); 744 } 745 totalWidth += qty * productDepthDbl; 746 if (qty == maxQuantity) shipmentPackageSeqId = null; 747 remQuantity = remQuantity - qty; 748 } 749 } 750 } 751 } 752 return result; 753 } 754 755 762 public static Map getProductsInPackages(DispatchContext dctx, Map context) { 763 764 Map result = new HashMap (); 765 Security security = dctx.getSecurity(); 766 GenericDelegator delegator = dctx.getDelegator(); 767 LocalDispatcher dispatcher = dctx.getDispatcher(); 768 Locale locale = (Locale ) context.get("locale"); 769 GenericValue userLogin = (GenericValue)context.get("userLogin"); 770 771 String productId = (String ) context.get("productId"); 772 Double quantity = (Double ) context.get("quantity"); 773 String fromDateStr = (String ) context.get("fromDate"); 774 775 if (quantity == null) { 776 quantity = new Double (1); 777 } 778 Date fromDate = null; 779 if (UtilValidate.isNotEmpty(fromDateStr)) { 780 try { 781 fromDate = Timestamp.valueOf(fromDateStr); 782 } catch (Exception e) { 783 } 784 } 785 if (fromDate == null) { 786 fromDate = new Date (); 787 } 788 789 BOMTree tree = null; 793 ArrayList components = new ArrayList (); 794 try { 795 tree = new BOMTree(productId, "MANUF_COMPONENT", fromDate, BOMTree.EXPLOSION_MANUFACTURING, delegator, dispatcher, userLogin); 796 tree.setRootQuantity(quantity.doubleValue()); 797 tree.getProductsInPackages(components); 798 } catch(GenericEntityException gee) { 799 return ServiceUtil.returnError("Error creating bill of materials tree: " + gee.getMessage()); 800 } 801 802 result.put("productsInPackages", components); 803 804 return result; 805 } 806 807 } 808 | Popular Tags |