# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1488375406 -19800
#      Wed Mar 01 19:06:46 2017 +0530
# Node ID c6c274d968d2818fa6207c545fef4342f9756744
# Parent  c04938eafe0f8067af8fcad75f5ea21a7dfacac6
Related to issue 35392 : Service Product should be not deleted if quantity rule is configured

* To delete the service product,  If service quantity rule is defined as "As per product", all the services must be selected

diff -r c04938eafe0f -r c6c274d968d2 web/org.openbravo.retail.posterminal/js/components/order.js
--- a/web/org.openbravo.retail.posterminal/js/components/order.js	Thu Feb 23 17:47:42 2017 +0530
+++ b/web/org.openbravo.retail.posterminal/js/components/order.js	Wed Mar 01 19:06:46 2017 +0530
@@ -755,16 +755,14 @@
 
           // Split/Remove services lines
           var siblingServicesLines = getSiblingServicesLines(line.get('product').id, line.get('relatedLines')[0].orderlineId);
-          if (!me.order.get('deleting')) {
-            if (siblingServicesLines.length < qtyService) {
-              var i, p, newLine;
-              for (i = 0; i < qtyService - siblingServicesLines.length; i++) {
-                p = line.get('product').clone();
-                p.set('groupProduct', false);
-                newLine = me.order.createLine(p, qtyLineServ);
-                newLine.set('relatedLines', siblingServicesLines[0].get('relatedLines'));
-                newLine.set('groupService', false);
-              }
+          if (!me.order.get('deleting') && siblingServicesLines.length < qtyService) {
+            var i, p, newLine;
+            for (i = 0; i < qtyService - siblingServicesLines.length; i++) {
+              p = line.get('product').clone();
+              p.set('groupProduct', false);
+              newLine = me.order.createLine(p, qtyLineServ);
+              newLine.set('relatedLines', siblingServicesLines[0].get('relatedLines'));
+              newLine.set('groupService', false);
             }
           } else if (siblingServicesLines.length > qtyService) {
             linesToRemove = OB.UTIL.mergeArrays(linesToRemove, _.initial(siblingServicesLines, qtyService));
diff -r c04938eafe0f -r c6c274d968d2 web/org.openbravo.retail.posterminal/js/pointofsale/view/editline.js
--- a/web/org.openbravo.retail.posterminal/js/pointofsale/view/editline.js	Thu Feb 23 17:47:42 2017 +0530
+++ b/web/org.openbravo.retail.posterminal/js/pointofsale/view/editline.js	Wed Mar 01 19:06:46 2017 +0530
@@ -201,7 +201,8 @@
     classes: 'btnlink-orange',
     tap: function () {
       var me = this,
-          order = this.model.get('order');
+          order = this.model.get('order'),
+          unGroupedServiceLines;
 
       function callback() {
         OB.UTIL.HookManager.executeHooks('OBPOS_PostDeleteLine', {
@@ -226,6 +227,39 @@
           });
           return;
         }
+
+        unGroupedServiceLines = _.filter(me.owner.owner.selectedModels, function (line) {
+          return line.has('relatedLines') && line.get('relatedLines').length > 0 && !line.get('originalOrderLineId') && line.get('product').get('quantityRule') === 'PP' && !line.get('groupService');
+        });
+
+        function getServiceQty(service) {
+          return _.filter(unGroupedServiceLines, function (line) {
+            return line.get('product').get('id') === service.get('product').get('id') && line.get('relatedLines')[0].orderlineId === service.get('relatedLines')[0].orderlineId;
+          }).length;
+        }
+
+        function getProductQty(service) {
+          return _.find(order.get('lines').models, function (line) {
+            return _.indexOf(_.pluck(service.get('relatedLines'), 'orderlineId'), line.get('id')) !== -1;
+          }).get('qty');
+        }
+        if (unGroupedServiceLines && unGroupedServiceLines.length > 0) {
+          var i, j, serviceQty, productQty, uniqueServices;
+          uniqueServices = _.uniq(unGroupedServiceLines, false, function (line) {
+            return line.get('product').get('id') + line.get('relatedLines')[0].orderlineId;
+          });
+          for (i = 0; i < uniqueServices.length; i++) {
+            serviceQty = getServiceQty(uniqueServices[i]);
+            productQty = getProductQty(uniqueServices[i]);
+            if (productQty && productQty !== serviceQty) {
+              OB.UTIL.showConfirmation.display(OB.I18N.getLabel('OBPOS_LineCanNotBeDeleted'), OB.I18N.getLabel('OBPOS_AllServiceLineMustSelectToDelete'), [{
+                label: OB.I18N.getLabel('OBMOBC_LblOk')
+              }]);
+              return;
+            }
+          }
+        }
+
         OB.UTIL.Approval.requestApproval(
         me.model, 'OBPOS_approval.deleteLine', function (approved, supervisor, approvalType) {
           if (approved) {
