From 166dadf7a2921cfa5be2b7fdb6c10d0b17c5b6f8 Mon Sep 17 00:00:00 2001 From: Jen Rasmussen Date: Fri, 22 Jan 2016 18:17:24 -0600 Subject: [PATCH] #11 - Same As Shipping Checkbox - when billing address is defaulted in changing the ship address clears billing --- .../sameAsShippingCheckbox.js | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/Same As Shipping Checkbox/sameAsShippingCheckbox.js b/Same As Shipping Checkbox/sameAsShippingCheckbox.js index b6074f4..96ebc98 100644 --- a/Same As Shipping Checkbox/sameAsShippingCheckbox.js +++ b/Same As Shipping Checkbox/sameAsShippingCheckbox.js @@ -33,37 +33,44 @@ function SameAsShippingCheckboxCtrl($scope, $rootScope) { $scope.copyShipAddress = false; $scope.setBilling = function() { - if ($scope.copyShipAddress == true) { - $scope.BillAddress = $scope.orderShipAddress; - $scope.BillAddressID = $scope.currentOrder.ShipAddressID; - $scope.currentOrder.BillAddressID = $scope.currentOrder.ShipAddressID; - $scope.BillAddress.IsBilling = true; - $scope.billaddresses.push($scope.BillAddress); - } - if ($scope.copyShipAddress == false) { - $scope.BillAddress = ''; - $scope.BillAddressID = ''; - $scope.currentOrder.BillAddressID = ''; + if ($scope.copyShipAddress === true) { + if ($scope.currentOrder && ($scope.currentOrder.BillAddressID != $scope.currentOrder.ShipAddressID)) { + $scope.currentOrder.BillAddressID = $scope.currentOrder.ShipAddressID; + $scope.BillAddress = $scope.orderShipAddress; + $scope.BillAddressID = $scope.currentOrder.BillAddressID; + $scope.BillAddress.IsBilling = true; + + var addressChecker = []; + angular.forEach($scope.billaddresses, function(address) { + addressChecker.push(address.ID); + }); + + if(addressChecker.indexOf($scope.orderShipAddress.ID) == -1){ + $scope.billaddresses.push($scope.orderShipAddress); + } + } } }; - $scope.$watch('currentOrder.ShipAddressID', function(newValue) { - if (newValue || newValue == null) { + $scope.$watch('currentOrder.ShipAddressID', function(newValue) { + if (newValue || newValue === null) { //broadcast that the ship address changed $rootScope.$broadcast('shipChange'); } }); - $scope.$watch('currentOrder.BillAddressID', function(newValue) { - if ($scope.currentOrder && newValue != $scope.currentOrder.ShipAddressID) { - $scope.copyShipAddress = false; + $scope.$on('shipChange', function() { + if ($scope.currentOrder && ($scope.currentOrder.BillAddressID != $scope.currentOrder.ShipAddressID) && ($scope.copyShipAddress === true)) { + $scope.copyShipAddress = false; + $scope.currentOrder.BillAddressID = ''; + $scope.BillAddress = null; + $scope.BillAddressID = ''; } }); - $scope.$on('shipChange', function() { - $scope.copyShipAddress = false; - $scope.BillAddress = null; - $scope.BillAddressID = ''; - if ($scope.currentOrder) $scope.currentOrder.BillAddressID = ''; + $scope.$watch('currentOrder.BillAddressID', function(newValue) { + if ($scope.currentOrder && (newValue != $scope.currentOrder.ShipAddressID) && ($scope.copyShipAddress === true)) { + $scope.copyShipAddress = false; + } }); } \ No newline at end of file