<commit_before>﻿(function (controllers, undefined) {

    /**
     * @ngdoc controller
     * @name Merchello.Dashboards.Customer.CustomerEditAddressController
     * @function
     * 
     * @description
     * The controller for adding or editing an address.
     */
    controllers.CustomerEditAddressController = function ($scope, dialogService) {

        /**
         * @ngdoc method
         * @name chooseAddress
         * @function
         * 
         * @description
         * Return the appropriate address depending on the filter and the type.
         */
        $scope.chooseAddress = function (type, filter) {
            var address = $scope.dialogData.addresses[(filter.id * 1) + 1];
            $scope.currentAddress = address;
        };

        /**
         * @ngdoc method
         * @name doesCountryHaveProvinces
         * @function
         * 
         * @description
         * Returns true if the country provided has provinces. Otherwise returns false.
         */
        $scope.doesCountryHaveProvinces = function (country) {
            var result = false;
            if (country.provinces) {
                if (country.provinces.length > 0) {
                    result = true;
                }
            }
            return result;
        };

        /**
         * @ngdoc method
         * @name init
         * @function
         * 
         * @description
         * Inits the scope.
         */
        $scope.init = function () {
            $scope.setVariables();
            $scope.setAsDefault();
        };

        /**
         * @ngdoc method
         * @name openDeleteAddressDialog
         * @function
         * 
         * @description
         * Open a dialog to confirm whether to delete a selected address.
         */
        $scope.openDeleteAddressDialog = function () {
            var dialogData = {};
            dialogData.name = $scope.currentAddress.label;
            dialogService.open({
                template: '/App_Plugins/Merchello/Common/Js/Dialogs/deleteconfirmation.html',
                show: true,
                callback: $scope.processDeleteAddressDialog,
                dialogData: dialogData
            });
        };

        // Remove any blank addresses and fix multiple defaults.
        $scope.prepareAddressesForSave = function() {
            var addresses = _.reject($scope.dialogData.addresses, function(address) {
                return address.address1 == '';
            });
            console.info(addresses);
        };

        /**
         * @ngdoc method
         * @name prepareCountryDropdown
         * @function
         * 
         * @description
         * This function sets the country dropdown to match the proper country.
         */
        $scope.prepareCountryDropdown = function() {
            for (var i = 0; i < $scope.countries.length; i++) {
                if ($scope.countries[i].countryCode == $scope.currentAddress.countryCode) {
                    $scope.dialogData.filters.country = $scope.countries[i];
                }
            }
            $scope.updateCountry($scope.dialogData.filters.country);
            $scope.prepareProvinceDropdown();
        };

        /**
         * @ngdoc method
         * @name prepareProvinceDropdown
         * @function
         * 
         * @description
         * This function sets the province dropdown to match the proper country.
         */
        $scope.prepareProvinceDropdown = function () {
            for (var i = 0; i < $scope.provinces.length; i++) {
                if ($scope.provinces[i].name == $scope.currentAddress.region) {
                    $scope.filters.province = $scope.provinces[i];
                }
            }
            $scope.updateProvince($scope.filters.province);
        };

        /**
         * @ngdoc method
         * @name processDeleteAddressDialog
         * @function
         * 
         * @description
         * Get response from a dialog on whether the selected address is to be deleted.
         */
        $scope.processDeleteAddressDialog = function (dialogData) {
            if ($scope.currentAddress.key === '') {
                $scope.currentAddress = new merchello.Models.CustomerAddress();
            } else {
                $scope.dialogData.shouldDelete = true;
                $scope.saveAddress();
            }
        };


        /**
         * @ngdoc method
         * @name saveAddress
         * @function
         * 
         * @description
         * Acquire the address and close the dialog, submitting collected data.
         */
        $scope.saveAddress = function() {
            if ($scope.editAddressForm.address1.$valid && $scope.editAddressForm.locality.$valid && $scope.editAddressForm.postalCode && $scope.dialogData.filters.country.id != -1) {
                console.info($scope.dialogData.addresses);
                $scope.prepareAddressesForSave();
                $scope.dialogData.addressToReturn = $scope.currentAddress;
                $scope.submit($scope.dialogData);
            } else {
                $scope.wasFormSubmitted = true;
            }
        };

        /**
         * @ngdoc method
         * @name setAsDefault
         * @function
         * 
         * @description
         * Set the default address as the first one to be edited.
         */
        $scope.setAsDefault = function() {<commit_msg>Added functionality to Customer Edit Address Dialog that turns false isDefault parameter on addresses that are out of date due to new one being set.<commit_after>﻿(function (controllers, undefined) {

    /**
     * @ngdoc controller
     * @name Merchello.Dashboards.Customer.CustomerEditAddressController
     * @function
     * 
     * @description
     * The controller for adding or editing an address.
     */
    controllers.CustomerEditAddressController = function ($scope, dialogService) {

        /**
         * @ngdoc method
         * @name chooseAddress
         * @function
         * 
         * @description
         * Return the appropriate address depending on the filter and the type.
         */
        $scope.chooseAddress = function (type, filter) {
            var address = $scope.dialogData.addresses[(filter.id * 1) + 1];
            $scope.currentAddress = address;
        };

        /**
         * @ngdoc method
         * @name doesCountryHaveProvinces
         * @function
         * 
         * @description
         * Returns true if the country provided has provinces. Otherwise returns false.
         */
        $scope.doesCountryHaveProvinces = function (country) {
            var result = false;
            if (country.provinces) {
                if (country.provinces.length > 0) {
                    result = true;
                }
            }
            return result;
        };

        /**
         * @ngdoc method
         * @name init
         * @function
         * 
         * @description
         * Inits the scope.
         */
        $scope.init = function () {
            $scope.setVariables();
            $scope.setAsDefault();
        };

        /**
         * @ngdoc method
         * @name openDeleteAddressDialog
         * @function
         * 
         * @description
         * Open a dialog to confirm whether to delete a selected address.
         */
        $scope.openDeleteAddressDialog = function () {
            var dialogData = {};
            dialogData.name = $scope.currentAddress.label;
            dialogService.open({
                template: '/App_Plugins/Merchello/Common/Js/Dialogs/deleteconfirmation.html',
                show: true,
                callback: $scope.processDeleteAddressDialog,
                dialogData: dialogData
            });
        };

        // Remove any blank addresses and fix multiple defaults.
        $scope.prepareAddressesForSave = function() {
            var addresses = _.reject($scope.dialogData.addresses, function (address) {
                // Reject an address if it is blank (and remove from the array).
                return address.address1 == '';
            });
            if ($scope.dialogData.addressToReturn.isDefault) {
                _.each(addresses, function(address) {
                    if (address.isDefault) {
                        if (address.key !== $scope.dialogData.addressToReturn.key) {
                            address.isDefault = false;
                        }
                    }
                });
            }
            $scope.dialogData.addresses = _.map(addresses, function(address) {
                return new merchello.Models.CustomerAddress(address);
            });
        };

        /**
         * @ngdoc method
         * @name prepareCountryDropdown
         * @function
         * 
         * @description
         * This function sets the country dropdown to match the proper country.
         */
        $scope.prepareCountryDropdown = function() {
            for (var i = 0; i < $scope.countries.length; i++) {
                if ($scope.countries[i].countryCode == $scope.currentAddress.countryCode) {
                    $scope.dialogData.filters.country = $scope.countries[i];
                }
            }
            $scope.updateCountry($scope.dialogData.filters.country);
            $scope.prepareProvinceDropdown();
        };

        /**
         * @ngdoc method
         * @name prepareProvinceDropdown
         * @function
         * 
         * @description
         * This function sets the province dropdown to match the proper country.
         */
        $scope.prepareProvinceDropdown = function () {
            for (var i = 0; i < $scope.provinces.length; i++) {
                if ($scope.provinces[i].name == $scope.currentAddress.region) {
                    $scope.filters.province = $scope.provinces[i];
                }
            }
            $scope.updateProvince($scope.filters.province);
        };

        /**
         * @ngdoc method
         * @name processDeleteAddressDialog
         * @function
         * 
         * @description
         * Get response from a dialog on whether the selected address is to be deleted.
         */
        $scope.processDeleteAddressDialog = function (dialogData) {
            if ($scope.currentAddress.key === '') {
                $scope.currentAddress = new merchello.Models.CustomerAddress();
            } else {
                $scope.dialogData.shouldDelete = true;
                $scope.saveAddress();
            }
        };

        /**
         * @ngdoc method
         * @name saveAddress
         * @function
         * 
         * @description
         * Acquire the address and close the dialog, submitting collected data.
         */
        $scope.saveAddress = function() {
            if ($scope.editAddressForm.address1.$valid && $scope.editAddressForm.locality.$valid && $scope.editAddressForm.postalCode && $scope.dialogData.filters.country.id != -1) {
                $scope.dialogData.addressToReturn = $scope.currentAddress;
                $scope.prepareAddressesForSave();
                $scope.submit($scope.dialogData);
            } else {
                $scope.wasFormSubmitted = true;
            }
        };

        /**
         * @ngdoc method
         * @name setAsDefault
         * @function
         * 
         * @description
         * Set the default address as the first one to be edited.
         */
        $scope.setAsDefault = function() {
{'fraction_non_alphanumeric': 0.10077586958416877, 'fraction_numerical': 0.0013948217243483568, 'mean_word_length': 1.4977139124755061, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6083012', 'n_tokens_mistral': 2882, 'n_tokens_neox': 2765, 'n_words': 634}