From 86d231bba6686d2d1b984837c1086ac66db77180 Mon Sep 17 00:00:00 2001
From: Prakash M <prakash@qualiantech.com>
Date: Wed, 28 Oct 2020 23:20:58 +0530
Subject: [PATCH] Fixed BUG-44515 : Product Category is inserted to assortment
 even if issummarylevel is true * This causes inconsistent data in webpos *
 Validated issummarylevel='N' before inserting into assortment * Added event
 handler to add or remove product category from assortment based on
 issummarylevel property

---
 ...ProductCategoryByAssortmentBackground.java |  35 +++---
 .../event/ProductCategoryEventHandler.java    | 103 ++++++++++++++++++
 .../event/ProductListEventHandler.java        |   4 +-
 3 files changed, 123 insertions(+), 19 deletions(-)
 create mode 100644 src/org/openbravo/retail/posterminal/event/ProductCategoryEventHandler.java

diff --git a/src/org/openbravo/retail/posterminal/UpdateProductCategoryByAssortmentBackground.java b/src/org/openbravo/retail/posterminal/UpdateProductCategoryByAssortmentBackground.java
index 50b3e1fa9..dfaf248a3 100644
--- a/src/org/openbravo/retail/posterminal/UpdateProductCategoryByAssortmentBackground.java
+++ b/src/org/openbravo/retail/posterminal/UpdateProductCategoryByAssortmentBackground.java
@@ -84,24 +84,25 @@ public class UpdateProductCategoryByAssortmentBackground extends DalBaseProcess
             final String productCategoryId = (String) scroll.get()[0];
             final ProductCategory productCategory = OBDal.getInstance()
                 .get(ProductCategory.class, productCategoryId);
+            if (!productCategory.isSummaryLevel()) {
+              final OBRETCOProductcategory productCategoryElement = OBProvider.getInstance()
+                  .get(OBRETCOProductcategory.class);
+              productCategoryElement.setClient(assortment.getClient());
+              productCategoryElement.setOrganization(assortment.getOrganization());
+              productCategoryElement.setProductCategory(productCategory);
+              productCategoryElement.setObretcoProductlist(assortment);
+              OBDal.getInstance().save(productCategoryElement);
+              productCategoryElementList.add(productCategoryElement);
+              assortment.getOBRETCOProductcategoryList().add(productCategoryElement);
 
-            final OBRETCOProductcategory productCategoryElement = OBProvider.getInstance()
-                .get(OBRETCOProductcategory.class);
-            productCategoryElement.setClient(assortment.getClient());
-            productCategoryElement.setOrganization(assortment.getOrganization());
-            productCategoryElement.setProductCategory(productCategory);
-            productCategoryElement.setObretcoProductlist(assortment);
-            OBDal.getInstance().save(productCategoryElement);
-            productCategoryElementList.add(productCategoryElement);
-            assortment.getOBRETCOProductcategoryList().add(productCategoryElement);
-
-            String logMsg = "Product category: " + productCategory.getName()
-                + " created for assortment: " + assortment.getName();
-            bgLogger.log(logMsg + "\n\n");
-            log.debug(logMsg);
-            if ((i++) % 1000 == 0) {
-              session.flush();
-              session.clear();
+              String logMsg = "Product category: " + productCategory.getName()
+                  + " created for assortment: " + assortment.getName();
+              bgLogger.log(logMsg + "\n\n");
+              log.debug(logMsg);
+              if ((i++) % 1000 == 0) {
+                session.flush();
+                session.clear();
+              }
             }
           }
         } finally {
diff --git a/src/org/openbravo/retail/posterminal/event/ProductCategoryEventHandler.java b/src/org/openbravo/retail/posterminal/event/ProductCategoryEventHandler.java
new file mode 100644
index 000000000..3eb335aa8
--- /dev/null
+++ b/src/org/openbravo/retail/posterminal/event/ProductCategoryEventHandler.java
@@ -0,0 +1,103 @@
+/*
+ ************************************************************************************
+ * Copyright (C) 2020 Openbravo S.L.U.
+ * Licensed under the Openbravo Commercial License version 1.0
+ * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
+ * or in the legal folder of this module distribution.
+ ************************************************************************************
+ */
+
+package org.openbravo.retail.posterminal.event;
+
+import java.util.List;
+
+import javax.enterprise.event.Observes;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.openbravo.base.model.Entity;
+import org.openbravo.base.model.ModelProvider;
+import org.openbravo.base.model.Property;
+import org.openbravo.base.provider.OBProvider;
+import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
+import org.openbravo.client.kernel.event.EntityUpdateEvent;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.model.common.plm.ProductCategory;
+import org.openbravo.retail.config.OBRETCOProductList;
+import org.openbravo.retail.config.OBRETCOProductcategory;
+
+public class ProductCategoryEventHandler extends EntityPersistenceEventObserver {
+  private static Entity[] entities = {
+      ModelProvider.getInstance().getEntity(ProductCategory.ENTITY_NAME) };
+  protected Logger logger = LogManager.getLogger();
+
+  @Override
+  protected Entity[] getObservedEntities() {
+    return entities;
+  }
+
+  public void onUpdate(@Observes EntityUpdateEvent event) {
+    if (!isValidEvent(event)) {
+      return;
+    }
+
+    final Entity productCategoryEntity = ModelProvider.getInstance()
+        .getEntity(ProductCategory.ENTITY_NAME);
+    final Property isSummaryProperty = productCategoryEntity
+        .getProperty(ProductCategory.PROPERTY_SUMMARYLEVEL);
+    final boolean previousSummary = (boolean) event.getPreviousState(isSummaryProperty);
+    final boolean currentSummary = (boolean) event.getCurrentState(isSummaryProperty);
+    ProductCategory productCtgry = (ProductCategory) event.getTargetInstance();
+
+    if (previousSummary != currentSummary) {
+      if (currentSummary) {
+        //@formatter:off
+        String hql = "as assortctgry "
+            +" join assortctgry.productCategory as pc "
+            +" where pc.id= :prodCategoryId ";
+        //@formatter:on
+        final List<OBRETCOProductcategory> assortmentProdCategoryList = OBDal.getInstance()
+            .createQuery(OBRETCOProductcategory.class, hql)
+            .setNamedParameter("prodCategoryId", productCtgry.getId())
+            .list();
+
+        // Remove product category from assortment
+        for (OBRETCOProductcategory assortProdCtgry : assortmentProdCategoryList) {
+          OBDal.getInstance().remove(assortProdCtgry);
+        }
+      } else {
+        //@formatter:off
+        String hql = "select distinct(asortprod.obretcoProductlist.id) "
+            +" from OBRETCO_Prol_Product as asortprod "
+            +" join asortprod.product as p "
+            +" join p.productCategory as pc "
+            +" where pc.id= :prodCategoryId ";
+        //@formatter:on
+        final List<String> assortmentList = OBDal.getInstance()
+            .getSession()
+            .createQuery(hql, String.class)
+            .setParameter("prodCategoryId", productCtgry.getId())
+            .list();
+
+        // Add product category to assortment
+        for (String assortmentId : assortmentList) {
+          OBRETCOProductList assortment = OBDal.getInstance()
+              .get(OBRETCOProductList.class, assortmentId);
+          if (assortment != null) {
+            createAssortmentProductCategory(assortment, productCtgry);
+          }
+        }
+      }
+    }
+  }
+
+  private void createAssortmentProductCategory(final OBRETCOProductList assortment,
+      final ProductCategory productCategory) {
+    final OBRETCOProductcategory assortmentProductCategory = OBProvider.getInstance()
+        .get(OBRETCOProductcategory.class);
+    assortmentProductCategory.setOrganization(assortment.getOrganization());
+    assortmentProductCategory.setObretcoProductlist(assortment);
+    assortmentProductCategory.setProductCategory(productCategory);
+    OBDal.getInstance().save(assortmentProductCategory);
+  }
+}
diff --git a/src/org/openbravo/retail/posterminal/event/ProductListEventHandler.java b/src/org/openbravo/retail/posterminal/event/ProductListEventHandler.java
index 9bc013f7b..a5ff349be 100644
--- a/src/org/openbravo/retail/posterminal/event/ProductListEventHandler.java
+++ b/src/org/openbravo/retail/posterminal/event/ProductListEventHandler.java
@@ -1,6 +1,6 @@
 /*
  ************************************************************************************
- * Copyright (C) 2015-2019 Openbravo S.L.U.
+ * Copyright (C) 2015-2020 Openbravo S.L.U.
  * Licensed under the Openbravo Commercial License version 1.0
  * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
  * or in the legal folder of this module distribution.
@@ -88,7 +88,7 @@ public class ProductListEventHandler extends EntityPersistenceEventObserver {
       final ProductCategory productCategory) {
     final OBRETCOProductcategory assortmentProductCategory = getAssortmentProductCategory(
         assortment, productCategory);
-    if (assortmentProductCategory == null) {
+    if (assortmentProductCategory == null && !productCategory.isSummaryLevel()) {
       createAssortmentProductCategory(assortment, productCategory);
     }
   }
-- 
2.20.1

