details:   https://code.openbravo.com/erp/devel/pi/rev/77bc835034ea
changeset: 33624:77bc835034ea
user:      Mark <markmm82 <at> gmail.com>
date:      Wed Feb 28 14:26:52 2018 -0500
summary:   Fixes issue 37939:Cannot confirm Cancel and Replace if a Service 
Product related
to a product was included on the Sales Order

After the new lines were created in the Cancel and Replace process the relations
between services and products were not copied from the replaced order, due that
the Confirm and Cancel process was failing.

To fix that, if exists relationships between the lines of the creplaced order 
they
are copied taking as reference the new order lines.

details:   https://code.openbravo.com/erp/devel/pi/rev/22d1f25dbdeb
changeset: 33625:22d1f25dbdeb
user:      David Miguelez <david.miguelez <at> openbravo.com>
date:      Fri Mar 02 11:10:17 2018 +0100
summary:   Related to Issue 37939. Code Review changes:

* Added method to retrieve orderLines with related replaced orderline
  that has services associated to it
* Moved flush and added comment
* Changed method and variable names
* Reordered methods

diffstat:

 src/org/openbravo/erpCommon/businessUtility/CancelAndReplaceUtils.java |  98 
++++++++++
 1 files changed, 98 insertions(+), 0 deletions(-)

diffs (112 lines):

diff -r ee2edf01ede6 -r 22d1f25dbdeb 
src/org/openbravo/erpCommon/businessUtility/CancelAndReplaceUtils.java
--- a/src/org/openbravo/erpCommon/businessUtility/CancelAndReplaceUtils.java    
Fri Mar 02 20:13:06 2018 +0000
+++ b/src/org/openbravo/erpCommon/businessUtility/CancelAndReplaceUtils.java    
Fri Mar 02 11:10:17 2018 +0100
@@ -156,10 +156,108 @@
         orderLines.close();
       }
     }
+    // Flush before updating Relations between Products and services to ensure 
all the Order Lines
+    // have been calculated properly
+    OBDal.getInstance().flush();
+    updateRelationsBetweenOrderLinesProductsAndServices(newOrder);
     return newOrder;
   }
 
   /**
+   * Update the relationships between the services and products in the new 
order lines. After all
+   * the lines are created, it is needed to update relations taking into 
account if those relations
+   * were present in the lines they are replacing
+   * 
+   * @param order
+   *          The new created order where the lines will be updated
+   */
+  private static void 
updateRelationsBetweenOrderLinesProductsAndServices(Order order) {
+    ScrollableResults newOrderLines = null;
+    try {
+      int i = 0;
+      newOrderLines = 
getOrderLinesListWithReplacedLineWithRelatedService(order);
+      while (newOrderLines.next()) {
+        updateOrderLineRelatedServices((OrderLine) newOrderLines.get(0));
+
+        if ((++i % 100) == 0) {
+          OBDal.getInstance().flush();
+          OBDal.getInstance().getSession().clear();
+        }
+      }
+    } finally {
+      if (newOrderLines != null) {
+        newOrderLines.close();
+      }
+    }
+  }
+
+  private static ScrollableResults 
getOrderLinesListWithReplacedLineWithRelatedService(Order order) {
+    StringBuilder hql = new StringBuilder("");
+    hql.append(" select ol ");
+    hql.append(" from OrderLine ol");
+    hql.append(" join ol.replacedorderline rol "); // Explicit join to avoid 
null values
+    hql.append(" where rol.orderlineServiceRelationList is not empty");
+    hql.append(" and ol.salesOrder.id = :orderId");
+
+    Query query = OBDal.getInstance().getSession().createQuery(hql.toString());
+    query.setParameter("orderId", order.getId());
+    return query.scroll(ScrollMode.FORWARD_ONLY);
+  }
+
+  private static void updateOrderLineRelatedServices(OrderLine orderLine) {
+    Order order = orderLine.getSalesOrder();
+    OrderLine replacedOrderLine = orderLine.getReplacedorderline();
+    List<OrderlineServiceRelation> replacedRelatedServices = replacedOrderLine
+        .getOrderlineServiceRelationList();
+    for (OrderlineServiceRelation replacedRelatedService : 
replacedRelatedServices) {
+      OrderLine replacedRelatedOrderLine = 
replacedRelatedService.getOrderlineRelated();
+      OrderLine orderLineReplacingRelatedOrderLine = 
getOrderLineReplacingRelatedOrderLine(order,
+          replacedRelatedOrderLine);
+      addNewOrderLineServiceRelation(orderLine, 
orderLineReplacingRelatedOrderLine);
+    }
+  }
+
+  /**
+   * Method returns the order line of an order that is replacing an specific 
order line
+   * 
+   * @param order
+   *          The order where the order line will be searched
+   * @param replacedOrderLine
+   *          The replaced order line that is searching for
+   * @return The order line that is replacing the one passed as parameter
+   */
+  private static OrderLine getOrderLineReplacingRelatedOrderLine(Order order,
+      OrderLine replacedOrderLine) {
+    OBCriteria<OrderLine> orderLinesCriteria = 
OBDal.getInstance().createCriteria(OrderLine.class);
+    orderLinesCriteria.add(Restrictions.eq(OrderLine.PROPERTY_SALESORDER, 
order));
+    orderLinesCriteria
+        .add(Restrictions.eq(OrderLine.PROPERTY_REPLACEDORDERLINE, 
replacedOrderLine));
+    orderLinesCriteria.setMaxResults(1);
+
+    return (OrderLine) orderLinesCriteria.uniqueResult();
+  }
+
+  private static void addNewOrderLineServiceRelation(OrderLine orderLine, 
OrderLine orderLineRelated) {
+    OrderlineServiceRelation newOrderLineServiceRelation = 
getNewOrderLineServiceRelation(
+        orderLine, orderLineRelated);
+    
orderLine.getOrderlineServiceRelationList().add(newOrderLineServiceRelation);
+  }
+
+  private static OrderlineServiceRelation 
getNewOrderLineServiceRelation(OrderLine orderLine,
+      OrderLine orderLineRelated) {
+    OrderlineServiceRelation newOrderLineServiceRelation = 
OBProvider.getInstance().get(
+        OrderlineServiceRelation.class);
+    newOrderLineServiceRelation.setClient(orderLine.getClient());
+    newOrderLineServiceRelation.setOrganization(orderLine.getOrganization());
+    newOrderLineServiceRelation.setAmount(orderLine.getLineGrossAmount());
+    newOrderLineServiceRelation.setOrderlineRelated(orderLineRelated);
+    newOrderLineServiceRelation.setQuantity(orderLine.getOrderedQuantity());
+    newOrderLineServiceRelation.setSalesOrderLine(orderLine);
+    OBDal.getInstance().save(newOrderLineServiceRelation);
+    return newOrderLineServiceRelation;
+  }
+
+  /**
    * Method that given an Order Id it cancels it and creates another one equal 
but with negative
    * quantities.
    * 

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to