Fixes Issue 38547. When cloning a sales order, exploded non
authorDavid Miguelez <david.miguelez@openbravo.com>
Tue, 29 May 2018 11:49:24 +0200
changeset 34140 85397f1d9ed3
parent 34139 5ae34663c033
child 34141 4e724a6b981c
Fixes Issue 38547. When cloning a sales order, exploded non
stocked BOM product is related with the original order line

* When an order line that has been created from an exploded
BOM is created, it is added to a list to fix the relationship
after cloning all order lines
* When all order lines are cloned, this relationship is fixed
by pointing to the cloned order line of the parent bom product
* Moved logic for fixing services relationship into it's own method
modules/org.openbravo.client.application/src/org/openbravo/client/application/businesslogic/CloneOrderActionHandler.java
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/businesslogic/CloneOrderActionHandler.java	Wed May 30 13:03:45 2018 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/businesslogic/CloneOrderActionHandler.java	Tue May 29 11:49:24 2018 +0200
@@ -102,6 +102,8 @@
 
       Map<String, OrderLine> mapOriginalOrderLineWithCloneOrderLine = new HashMap<>();
       List<OrderlineServiceRelation> orderLinesServiceRelation = new ArrayList<>();
+      List<OrderLine> orderLinesCreatedFromExplodedBOM = new ArrayList<>();
+
       // get the lines associated with the order and clone them to the new
       // order line.
       for (OrderLine ordLine : objOrder.getOrderLineList()) {
@@ -129,17 +131,20 @@
         List<OrderlineServiceRelation> lineServiceRelation = cloneProductServiceRelation(ordLine,
             objCloneOrdLine);
         orderLinesServiceRelation.addAll(lineServiceRelation);
+        if (ordLine.getBOMParent() != null) {
+          orderLinesCreatedFromExplodedBOM.add(ordLine);
+        }
       }
 
-      for (OrderlineServiceRelation lineServiceRelation : orderLinesServiceRelation) {
-        OrderLine clonedOrderLine = mapOriginalOrderLineWithCloneOrderLine.get(lineServiceRelation
-            .getOrderlineRelated().getId());
-        lineServiceRelation.setOrderlineRelated(clonedOrderLine);
-        OBDal.getInstance().save(lineServiceRelation);
-      }
+      fixRelatedServicesReferences(mapOriginalOrderLineWithCloneOrderLine,
+          orderLinesServiceRelation);
+
+      fixRelatedBOMProductsReferences(mapOriginalOrderLineWithCloneOrderLine,
+          orderLinesCreatedFromExplodedBOM);
 
       mapOriginalOrderLineWithCloneOrderLine.clear();
       orderLinesServiceRelation.clear();
+      orderLinesCreatedFromExplodedBOM.clear();
 
       OBDal.getInstance().save(objCloneOrder);
       OBDal.getInstance().flush();
@@ -170,6 +175,28 @@
     return cloneServiceRelation;
   }
 
+  private void fixRelatedServicesReferences(
+      Map<String, OrderLine> mapOriginalOrderLineWithCloneOrderLine,
+      List<OrderlineServiceRelation> orderLinesServiceRelation) {
+    for (OrderlineServiceRelation lineServiceRelation : orderLinesServiceRelation) {
+      OrderLine clonedOrderLine = mapOriginalOrderLineWithCloneOrderLine.get(lineServiceRelation
+          .getOrderlineRelated().getId());
+      lineServiceRelation.setOrderlineRelated(clonedOrderLine);
+      OBDal.getInstance().save(lineServiceRelation);
+    }
+  }
+
+  private void fixRelatedBOMProductsReferences(
+      Map<String, OrderLine> mapOriginalOrderLineWithCloneOrderLine,
+      List<OrderLine> orderLinesCreatedFromExplodedBOM) {
+    for (OrderLine orderLine : orderLinesCreatedFromExplodedBOM) {
+      OrderLine clonedOrderLine = mapOriginalOrderLineWithCloneOrderLine.get(orderLine.getId());
+      String bomParentId = orderLine.getBOMParent().getId();
+      OrderLine clonedBomParent = mapOriginalOrderLineWithCloneOrderLine.get(bomParentId);
+      clonedOrderLine.setBOMParent(clonedBomParent);
+    }
+  }
+
   private String getPriceListVersion(String priceList, String clientId) {
     try {
       String whereClause = " as plv left outer join plv.priceList pl where plv.active='Y' and plv.active='Y' and "