# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1564654716 -19800
#      Thu Aug 01 15:48:36 2019 +0530
# Node ID 6abb57ca27906364a460a3bbf75a4458955cc1a3
# Parent  c449bf10654bac2d80068b2cb55b27b257d13dba
Related to issue 41513 BUT 19Q1 : Load Location from Backoffice if not present in local db

* When loading receipt in verfied return, Location should be loaded from Backoffice
if not present in local db

diff -r c449bf10654b -r 6abb57ca2790 web/org.openbravo.retail.returns/js/modalReturnLines.js
--- a/web/org.openbravo.retail.returns/js/modalReturnLines.js	Thu Aug 01 15:44:54 2019 +0530
+++ b/web/org.openbravo.retail.returns/js/modalReturnLines.js	Thu Aug 01 15:48:36 2019 +0530
@@ -541,130 +541,37 @@
         OB.UTIL.showLoading(false);
         return;
       }
-      var i, bpBillLoc, nameLocation = '',
-          me = this,
-          receipt = me.args.args.context.model.get('order'),
-          oldbp = receipt.get('bp'),
-          bpLoc = me.args.args.order.bpLocId,
-          noFoundCustomer = true,
-          isLoadedPartiallyFromBackend = false;
+      var me = this,
+          receipt = me.args.args.context.model.get('order');
 
-      var findBusinessPartner = function (bp) {
-          if (OB.MobileApp.model.hasPermission('OBPOS_remote.discount.bp', true)) {
-            var bpFilter = {
-              columns: ['businessPartner'],
-              operator: 'equals',
-              value: OB.MobileApp.model.get('businessPartner').id
-            };
-            var remoteCriteria = [bpFilter];
-            var criteriaFilter = {};
-            criteriaFilter.remoteFilters = remoteCriteria;
-            OB.Dal.find(OB.Model.DiscountFilterBusinessPartner, criteriaFilter, function (discountsBP) {
-              _.each(discountsBP.models, function (dsc) {
-                OB.Dal.saveOrUpdate(dsc, function () {}, function () {
-                  OB.error(arguments);
-                });
-              });
-            }, function () {
-              OB.error(arguments);
-            });
-          }
-          var locationForBpartner = function (bpLoc, billLoc) {
-              bp.set('shipLocName', bpLoc.get('name'));
-              bp.set('shipLocId', bpLoc.get('id'));
-              bp.set('shipPostalCode', bpLoc.get('postalCode'));
-              bp.set('shipCityName', bpLoc.get('cityName'));
-              bp.set('shipCountryName', bpLoc.get('countryName'));
-              bp.set('shipCountryId', bpLoc.get('countryId'));
-              bp.set('shipRegionId', bpLoc.get('regionId'));
-              bp.set('locName', bpLoc.get('name'));
-              bp.set('locId', bpLoc.get('id'));
-              bp.set('locationModel', bpLoc);
-              if (billLoc) {
-                bp.set('locName', billLoc.get('name'));
-                bp.set('locId', billLoc.get('id'));
-                bp.set('postalCode', billLoc.get('postalCode'));
-                bp.set('cityName', billLoc.get('cityName'));
-                bp.set('countryName', billLoc.get('countryName'));
-                bp.set('locationBillModel', billLoc);
-              }
-              me.nameLocation = bpLoc.get('name');
-
-              receipt.setBPandBPLoc(bp, false, true, function () {
-
-                //If we do not let user to select qty to return of shipment lines, we do it automatically
-                if (!OB.MobileApp.model.hasPermission("OBPOS_SplitLinesInShipments", true)) {
-                  me.args.args.order.receiptLines = me.autoSplitShipmentLines(me.args.args.order.receiptLines);
-                }
-                _.each(me.args.args.order.receiptLines, function (line) {
-                  line.blockAddProduct = true;
-                  line.isVerifiedReturn = true;
-                  line.allowLayawayWithReturn = false;
-                });
+      me.args.args.context.model.get('orderList').loadCustomer({
+        bpId: me.args.args.order.bp,
+        bpLocId: me.args.args.order.bpLocId,
+        bpBillLocId: me.args.args.order.bpBillLocId || me.args.args.order.bpLocId
+      }, function (bp, bpLoc, billLoc) {
+        me.nameLocation = bpLoc.get('name');
 
-                OB.UTIL.ReturnUtils.addReturnLines(receipt, me.args.args.order, me.args.args.order.receiptLines, function () {
-                  receipt.calculateReceipt(function () {
-                    receipt.save(function () {
-                      OB.MobileApp.model.orderList.saveCurrent();
-                      OB.UTIL.showLoading(false);
-                    });
-                  });
-                  me.args.args.cancelOperation = true;
-                  OB.UTIL.HookManager.callbackExecutor(me.args.args, me.args.callbacks);
-                });
+        receipt.setBPandBPLoc(bp, false, true, function () {
+          //If we do not let user to select qty to return of shipment lines, we do it automatically
+          if (!OB.MobileApp.model.hasPermission("OBPOS_SplitLinesInShipments", true)) {
+            me.args.args.order.receiptLines = me.autoSplitShipmentLines(me.args.args.order.receiptLines);
+          }
+          _.each(me.args.args.order.receiptLines, function (line) {
+            line.blockAddProduct = true;
+            line.isVerifiedReturn = true;
+            line.allowLayawayWithReturn = false;
+          });
+
+          OB.UTIL.ReturnUtils.addReturnLines(receipt, me.args.args.order, me.args.args.order.receiptLines, function () {
+            receipt.calculateReceipt(function () {
+              receipt.save(function () {
+                OB.MobileApp.model.orderList.saveCurrent();
+                OB.UTIL.showLoading(false);
               });
-              };
-          if (isLoadedPartiallyFromBackend) {
-            locationForBpartner(bpLoc, bpBillLoc);
-          } else {
-            OB.Dal.get(OB.Model.BPLocation, me.args.args.order.bpLocId, function (bpLoc) {
-              if (me.args.args.order.bpBillLocId) {
-                OB.Dal.get(OB.Model.BPLocation, me.args.args.order.bpBillLocId, function (billLoc) {
-                  locationForBpartner(bpLoc, billLoc);
-                }, function () {
-                  // TODO: Report errors properly
-                });
-              } else {
-                locationForBpartner(bpLoc);
-              }
-
-            }, function () {
-              // TODO: Report errors properly
-            }, function () {
-              OB.UTIL.showConfirmation.display(OB.I18N.getLabel('OBMOBC_Error'), OB.I18N.getLabel('OBPOS_NoReceiptLoadedLocation'));
-              return;
             });
-          }
-          };
-      //Check businesspartner
-      OB.Dal.get(OB.Model.BusinessPartner, this.args.args.order.bp, function (bp) {
-        findBusinessPartner(bp);
-      }, null, function () {
-        var criteria = {
-          bpartnerId: me.args.args.order.bp,
-          bpLocationId: me.args.args.order.bpLocId
-        };
-        if (me.args.args.order.bpLocId !== me.args.args.order.bpBillLocId) {
-          criteria.bpBillLocationId = me.args.args.order.bpBillLocId;
-        }
-        new OB.DS.Request('org.openbravo.retail.posterminal.master.LoadedCustomer').exec(criteria, function (data) {
-          isLoadedPartiallyFromBackend = true;
-          bpLoc = OB.Dal.transform(OB.Model.BPLocation, data[1]);
-          //If we do not have a bill address we have an address for both
-          if (data.length === 3) {
-            bpBillLoc = OB.Dal.transform(OB.Model.BPLocation, data[2]);
-          } else {
-            bpBillLoc = OB.Dal.transform(OB.Model.BPLocation, data[1]);
-          }
-          findBusinessPartner(OB.Dal.transform(OB.Model.BusinessPartner, data[0]));
-        }, function () {
-          if (noFoundCustomer) {
-            noFoundCustomer = false;
-            OB.UTIL.showConfirmation.display(OB.I18N.getLabel('OBPOS_InformationTitle'), OB.I18N.getLabel('OBPOS_NoReceiptLoadedText'), [{
-              label: OB.I18N.getLabel('OBPOS_LblOk'),
-              isConfirmButton: true
-            }]);
-          }
+            me.args.args.cancelOperation = true;
+            OB.UTIL.HookManager.callbackExecutor(me.args.args, me.args.callbacks);
+          });
         });
       });
     },
