# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1564641611 -19800
#      Thu Aug 01 12:10:11 2019 +0530
# Node ID b43089cbc69cf83c3dd878eac3dafbc0f6d2ae04
# Parent  80f5fc759c74ae8d8975fea84905ccfa1daa1403
Related to issue 41513 : 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 80f5fc759c74 -r b43089cbc69c web/org.openbravo.retail.returns/js/modalReturnLines.js
--- a/web/org.openbravo.retail.returns/js/modalReturnLines.js	Mon Jul 01 10:35:04 2019 +0000
+++ b/web/org.openbravo.retail.returns/js/modalReturnLines.js	Thu Aug 01 12:10:11 2019 +0530
@@ -700,68 +700,22 @@
         OB.UTIL.showLoading(false);
         return;
       }
-      var bpBillLoc,
-        me = this,
-        receipt = me.args.args.context.model.get('order'),
-        bpLoc = me.args.args.order.bpLocId,
-        noFoundCustomer = true,
-        isLoadedPartiallyFromBackend = false;
+      var me = this,
+        receipt = me.args.args.context.model.get('order');
       if (OB.UTIL.isCrossStoreReceipt(me.args.args.order)) {
         receipt.set('organization', me.args.args.order.organization);
         receipt.set('documentType', me.args.args.order.documentTypeReturnId);
         receipt.set('priceList', me.args.args.order.priceList);
         receipt.set('priceIncludesTax', me.args.args.order.priceIncludesTax);
       }
-      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.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');
 
           receipt.setBPandBPLoc(bp, false, true, function() {
@@ -801,92 +755,6 @@
               }
             );
           });
-        };
-        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
-                    }
-                  ]
-                );
-              }
-            }
-          );
         }
       );
     },
