<commit_before>/*
 * This program was produced for the U.S. Agency for International Development. It was prepared by the USAID | DELIVER PROJECT, Task Order 4. It is part of a project which utilizes code originally licensed under the terms of the Mozilla Public License (MPL) v2 and therefore is licensed under MPL v2 or later.
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the Mozilla Public License as published by the Mozilla Foundation, either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Mozilla Public License for more details.
 *
 * You should have received a copy of the Mozilla Public License along with this program. If not, see http://www.mozilla.org/MPL/
 */

function DonorController($scope, sharedSpace, $routeParams, $location, SaveDonor, GetDonor, $dialog, messageService, RemoveDonor) {
    $scope.donor = {};
    $scope.message={};

    if ($routeParams.donorId) {
        GetDonor.get({id: $routeParams.donorId}, function (data) {
          $scope.donor = data.donor;
          $scope.showError = true;
        }, {});
    }

    $scope.cancelAddEdit = function(){
        $scope.$parent.message={};
        $scope.$parent.donorId=null;
        $location.path('#/list');
    };

    $scope.saveDonor = function () {
        var successHandler = function (response) {
            $scope.donor = response.donor;
            $scope.showError = false;
            $scope.error = "";
            $scope.$parent.message = response.success;
            $scope.$parent.donorId = $scope.donor.id;
            $location.path('');
        };

        var errorHandler = function (response) {
            $scope.showError = true;
            $scope.error = messageService.get(response.data.error);
        };

      if(!$scope.donorForm.$invalid){
        SaveDonor.save($scope.donor, successHandler, errorHandler);
      }

        return true;
    };

    $scope.validateDonorShortName = function () {
        $scope.donorShortNameInvalid = $scope.donor.shortName === null;
    };

    $scope.validateDonorLongName = function () {
        $scope.donorLongNameInvalid = $scope.donor.longName === null;
    };

    $scope.showRemoveDonorConfirmDialog = function () {
        if(sharedSpace.getCountOfDonations() > 0){
            $scope.showError = true;
            $scope.error = "Donations have been recorded by this donor. It can't be removed.";
            return false;
        }

        $scope.selectedDonor = $scope.donor;
        var options = {
            id: "removeDonorMemberConfirmDialog",
            header: "Confirmation",
            body: "Are you sure you want to remove the donor: " + $scope.selectedDonor.shortName
        };
        OpenLmisDialog.newDialog(options, $scope.removeDonorConfirm, $dialog, messageService);
    };

    $scope.removeDonorConfirm = function (result) {
        if (result) {
            $scope.removeDonor($scope.selectedDonor.id);
            $scope.$parent.message = "Donor: " + $scope.selectedDonor.shortName + " has been successfully removed. ";
            $location.path('#/list');
        }
        $scope.selectedDonor = undefined;
    };

    $scope.removeDonor = function(id){
        RemoveDonor.get({id: id});
    };

}

<commit_msg>ELMIS-1285: show proper error when donation cannot be deleted<commit_after>/*
 * This program was produced for the U.S. Agency for International Development. It was prepared by the USAID | DELIVER PROJECT, Task Order 4. It is part of a project which utilizes code originally licensed under the terms of the Mozilla Public License (MPL) v2 and therefore is licensed under MPL v2 or later.
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the Mozilla Public License as published by the Mozilla Foundation, either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Mozilla Public License for more details.
 *
 * You should have received a copy of the Mozilla Public License along with this program. If not, see http://www.mozilla.org/MPL/
 */

function DonorController($scope, sharedSpace, $routeParams, $location, SaveDonor, GetDonor, $dialog, messageService, RemoveDonor) {
    $scope.donor = {};
    $scope.message={};

    if ($routeParams.donorId) {
        GetDonor.get({id: $routeParams.donorId}, function (data) {
          $scope.donor = data.donor;
          $scope.showError = true;
        }, {});
    }

    $scope.cancelAddEdit = function(){
        $scope.$parent.message={};
        $scope.$parent.donorId=null;
        $location.path('#/list');
    };

    $scope.saveDonor = function () {
        var successHandler = function (response) {
            $scope.donor = response.donor;
            $scope.showError = false;
            $scope.error = "";
            $scope.$parent.message = response.success;
            $scope.$parent.donorId = $scope.donor.id;
            $location.path('');
        };

        var errorHandler = function (response) {
            $scope.showError = true;
            $scope.error = messageService.get(response.data.error);
        };

      if(!$scope.donorForm.$invalid){
        SaveDonor.save($scope.donor, successHandler, errorHandler);
      }

        return true;
    };

    $scope.validateDonorShortName = function () {
        $scope.donorShortNameInvalid = $scope.donor.shortName === null;
    };

    $scope.validateDonorLongName = function () {
        $scope.donorLongNameInvalid = $scope.donor.longName === null;
    };

    $scope.showRemoveDonorConfirmDialog = function () {
        if(sharedSpace.getCountOfDonations() > 0){
            $scope.showError = true;
            $scope.error = "Donations have been recorded by this donor. It can't be removed.";
            return false;
        }

        $scope.selectedDonor = $scope.donor;
        var options = {
            id: "removeDonorMemberConfirmDialog",
            header: "Confirmation",
            body: "Are you sure you want to remove the donor: " + $scope.selectedDonor.shortName
        };
        OpenLmisDialog.newDialog(options, $scope.removeDonorConfirm, $dialog, messageService);
    };

    $scope.removeDonorConfirm = function (result) {
        if (result) {

          RemoveDonor.get({id: $scope.selectedDonor.id}, function (){
            // on success
            $scope.$parent.message = "Donor: " + $scope.selectedDonor.shortName + " has been successfully removed. ";
            $location.path('#/list');
          }, function(){
            $scope.error = 'This Donor cannot be deleted because donations have been recorded under it. Please remove the donations before deleting this donor';
          });


        }
        $scope.selectedDonor = undefined;
    };



}


{'fraction_non_alphanumeric': 0.11024062674874091, 'fraction_numerical': 0.00195858981533296, 'mean_word_length': 2.58705469141997, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6992922', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1916, 'n_words': 588}