# HG changeset patch
# User Jorge Garcia <jorge.garcia@openbravo.com>
# Date 1501656431 -7200
#      Wed Aug 02 08:47:11 2017 +0200
# Node ID f361d13c2a4c4a7338176e01943c7a52d57b4f46
# Parent  17ae36f8f9e74fe836747f994cdac290a8941f77
Related to issue 36515: Improve BusinessPartner query performance removing location info from it

diff --git a/web/org.openbravo.retail.returns/js/modalReturnLines.js b/web/org.openbravo.retail.returns/js/modalReturnLines.js
--- a/web/org.openbravo.retail.returns/js/modalReturnLines.js
+++ b/web/org.openbravo.retail.returns/js/modalReturnLines.js
@@ -427,6 +427,7 @@
       var nameLocation = "";
       var oldbp = me.args.args.context.model.get('order').get('bp');
       var bpLoc = me.args.args.order.bpLocId;
+      var bpBillLoc;
       var noFoundProduct = true,
           NoFoundCustomer = true,
           isLoadedPartiallyFromBackend = false;
@@ -451,7 +452,7 @@
               OB.error(arguments);
             });
           }
-          var locationForBpartner = function (bpLoc) {
+          var locationForBpartner = function (bpLoc, billLoc) {
               var finishOrderCreation = function () {
                   me.args.args.context.model.get('order').calculateReceipt(function () {
                     me.args.args.context.model.get('order').save(function () {
@@ -530,10 +531,27 @@
                   overdueServicesToApprove = '',
                   overdueServicesToApproveArr = [];
 
+              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');
-              bp.set('locationModel', bpLoc);
-              bp.set('locId', me.args.args.order.bpLocId);
-              bp.set('locName', me.nameLocation);
               //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);
@@ -591,10 +609,19 @@
               });
               };
           if (isLoadedPartiallyFromBackend) {
-            locationForBpartner(bpLoc);
+            locationForBpartner(bpLoc, bpBillLoc);
           } else {
             OB.Dal.get(OB.Model.BPLocation, me.args.args.order.bpLocId, function (bpLoc) {
-              locationForBpartner(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
             });
@@ -604,12 +631,22 @@
       OB.Dal.get(OB.Model.BusinessPartner, this.args.args.order.bp, function (bp) {
         findBusinessPartner(bp);
       }, null, function () {
-        new OB.DS.Request('org.openbravo.retail.posterminal.master.LoadedCustomer').exec({
+        var criteria = {
           bpartnerId: me.args.args.order.bp,
           bpLocationId: me.args.args.order.bpLocId
-        }, function (data) {
+        };
+        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) {
