<commit_before>/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2013 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 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 GNU Affero General Public License for more details.
 * You should have received a copy of the GNU Affero General Public License along with this program.  If not, see http://www.gnu.org/licenses.  For additional information contact <EMAIL>. 
 */

function InitiateRnrController($scope, $location, Requisitions,
                               PeriodsForFacilityAndProgram, UserFacilityList,
                               CreateRequisitionProgramList,
                               UserSupervisedFacilitiesForProgram,
                               FacilityProgramRights, navigateBackService,
                               messageService) {
  $scope.fullScreen = false;
  $scope.$watch('fullScreen', function () {
    angular.element(window).scrollTop(0);
    if (!$.browser.msie) {
      $scope.fullScreen ? angular.element('.toggleFullScreen').slideUp('slow', function () {
      }) : angular.element('.toggleFullScreen').slideDown('slow', function () {
      });
    }
    else {
      $scope.fullScreen ? angular.element('.toggleFullScreen').hide() : angular.element('.toggleFullScreen').show();
    }
    $scope.fullScreen ? angular.element('.print-button').css('opacity', '1.0') : angular.element('.print-button').css('opacity', '0');
  });

  var isNavigatedBack;

  $scope.selectedRnrType = {"name": "Regular", "emergency": false};

  $scope.rnrTypes = {"types": [
    {"name": messageService.get("requisition.type.regular"), "emergency": false},
    {"name": messageService.get("requisition.type.emergency"), "emergency": true}
  ]};

  var resetRnrData = function () {
    $scope.periodGridData = [];
    $scope.selectedProgram = null;
    $scope.selectedFacilityId = null;
    $scope.myFacility = null;
    $scope.programs = null;
    $scope.facilities = null;
    $scope.error = null;
  };

  $scope.$on('$viewContentLoaded', function () {
    $scope.selectedType = navigateBackService.selectedType || "0";
    $scope.selectedProgram = navigateBackService.selectedProgram;
    $scope.selectedFacilityId = navigateBackService.selectedFacilityId;
    isNavigatedBack = navigateBackService.isNavigatedBack;
    $scope.$watch('programs', function () {
      isNavigatedBack = navigateBackService.isNavigatedBack;
      if (!isNavigatedBack) $scope.selectedProgram = undefined;
      if ($scope.programs && !isUndefined($scope.selectedProgram)) {
        $scope.selectedProgram = _.where($scope.programs, {id: $scope.selectedProgram.id})[0];
        $scope.loadPeriods();
      }
    });
    $scope.loadFacilityData($scope.selectedType);
    if (isNavigatedBack) {
      $scope.loadFacilitiesForProgram();
    }
    $scope.$watch('facilities', function () {
      if ($scope.facilities && isNavigatedBack) {
        $scope.selectedFacilityId = navigateBackService.selectedFacilityId;
        isNavigatedBack = false;
      }
    });
  });

  $scope.loadFacilityData = function (selectedType) {
    isNavigatedBack = isNavigatedBack ? selectedType !== "0" : resetRnrData();

    if (selectedType === "0") { //My facility
      UserFacilityList.get({}, function (data) {
        $scope.facilities = data.facilityList;
        $scope.myFacility = data.facilityList[0];
        if ($scope.myFacility) {
          $scope.facilityDisplayName = $scope.myFacility.code + '-' + $scope.myFacility.name;
          $scope.selectedFacilityId = $scope.myFacility.id;

          CreateRequisitionProgramList.get({facilityId: $scope.selectedFacilityId}, function (data) {
            $scope.programs = data.programList;
          }, {});
        } else {
          $scope.facilityDisplayName = messageService.get("label.none.assigned");
          $scope.programs = null;
          $scope.selectedProgram = null;
        }
      }, {});
    } else if (selectedType === "1") { // Supervised facility
      resetRnrData();
      CreateRequisitionProgramList.get({}, function (data) {
        $scope.programs = data.programList;
      }, {});
    }
  };

  $scope.loadFacilitiesForProgram = function () {
    if ($scope.selectedProgram.id) {
      UserSupervisedFacilitiesForProgram.get({programId: $scope.selectedProgram.id}, function (data) {
        $scope.facilities = data.facilities;
        $scope.selectedFacilityId = null;
        $scope.error = null;
      }, {});
    } else {
      $scope.facilities = null;
      $scope.selectedFacilityId = null;
    }
  };

  var getPeriodSpecificButton = function (activeForRnr) {
    return '<input type="button" ng-click="initRnr(row.entity)" openlmis-message="button.proceed" class="btn btn-primary btn-small grid-btn" ng-show="' + activeForRnr + '"/>';
  };

  var optionMessage = function (entity, defaultMessage) {
    return entity === null || entity.length === 0 ? messageService.get("label.none.assigned") : defaultMessage;
  };

  var resetValuesForFirstPeriod = function (periodGridData) {
    var firstPeriodWithRnrStatus = periodGridData[0];
    firstPeriodWithRnrStatus.activeForRnr = true;
    if (!firstPeriodWithRnrStatus.rnrId) {
      firstPeriodWithRnrStatus.rnrStatus = messageService.get("msg.rnr.not.started");
    }
  };

  var createPeriodWithRnrStatus = function (periods, rnrs) {

    if (periods === null || periods.length === 0) {
      $scope.error = messageService.get("msg.no.period.available");
      if ($scope.isEmergency) {
        addPreviousRequisitionToPeriodList(rnrs);
      }
      return;
    }

    $scope.periodGridData = [];

    var periodWithRnrStatus;<commit_msg>|#000||Fixing error of handling unavailable options (undefined) message in init-requisition page|<commit_after>/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2013 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 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 GNU Affero General Public License for more details.
 * You should have received a copy of the GNU Affero General Public License along with this program.  If not, see http://www.gnu.org/licenses.  For additional information contact <EMAIL>. 
 */

function InitiateRnrController($scope, $location, Requisitions,
                               PeriodsForFacilityAndProgram, UserFacilityList,
                               CreateRequisitionProgramList,
                               UserSupervisedFacilitiesForProgram,
                               FacilityProgramRights, navigateBackService,
                               messageService) {
  $scope.fullScreen = false;
  $scope.$watch('fullScreen', function () {
    angular.element(window).scrollTop(0);
    if (!$.browser.msie) {
      $scope.fullScreen ? angular.element('.toggleFullScreen').slideUp('slow', function () {
      }) : angular.element('.toggleFullScreen').slideDown('slow', function () {
      });
    }
    else {
      $scope.fullScreen ? angular.element('.toggleFullScreen').hide() : angular.element('.toggleFullScreen').show();
    }
    $scope.fullScreen ? angular.element('.print-button').css('opacity', '1.0') : angular.element('.print-button').css('opacity', '0');
  });

  var isNavigatedBack;

  $scope.selectedRnrType = {"name": "Regular", "emergency": false};

  $scope.rnrTypes = {"types": [
    {"name": messageService.get("requisition.type.regular"), "emergency": false},
    {"name": messageService.get("requisition.type.emergency"), "emergency": true}
  ]};

  var resetRnrData = function () {
    $scope.periodGridData = [];
    $scope.selectedProgram = null;
    $scope.selectedFacilityId = null;
    $scope.myFacility = null;
    $scope.programs = null;
    $scope.facilities = null;
    $scope.error = null;
  };

  $scope.$on('$viewContentLoaded', function () {
    $scope.selectedType = navigateBackService.selectedType || "0";
    $scope.selectedProgram = navigateBackService.selectedProgram;
    $scope.selectedFacilityId = navigateBackService.selectedFacilityId;
    isNavigatedBack = navigateBackService.isNavigatedBack;
    $scope.$watch('programs', function () {
      isNavigatedBack = navigateBackService.isNavigatedBack;
      if (!isNavigatedBack) $scope.selectedProgram = undefined;
      if ($scope.programs && !isUndefined($scope.selectedProgram)) {
        $scope.selectedProgram = _.where($scope.programs, {id: $scope.selectedProgram.id})[0];
        $scope.loadPeriods();
      }
    });
    $scope.loadFacilityData($scope.selectedType);
    if (isNavigatedBack) {
      $scope.loadFacilitiesForProgram();
    }
    $scope.$watch('facilities', function () {
      if ($scope.facilities && isNavigatedBack) {
        $scope.selectedFacilityId = navigateBackService.selectedFacilityId;
        isNavigatedBack = false;
      }
    });
  });

  $scope.loadFacilityData = function (selectedType) {
    isNavigatedBack = isNavigatedBack ? selectedType !== "0" : resetRnrData();

    if (selectedType === "0") { //My facility
      UserFacilityList.get({}, function (data) {
        $scope.facilities = data.facilityList;
        $scope.myFacility = data.facilityList[0];
        if ($scope.myFacility) {
          $scope.facilityDisplayName = $scope.myFacility.code + '-' + $scope.myFacility.name;
          $scope.selectedFacilityId = $scope.myFacility.id;

          CreateRequisitionProgramList.get({facilityId: $scope.selectedFacilityId}, function (data) {
            $scope.programs = data.programList;
          }, {});
        } else {
          $scope.facilityDisplayName = messageService.get("label.none.assigned");
          $scope.programs = null;
          $scope.selectedProgram = null;
        }
      }, {});
    } else if (selectedType === "1") { // Supervised facility
      resetRnrData();
      CreateRequisitionProgramList.get({}, function (data) {
        $scope.programs = data.programList;
      }, {});
    }
  };

  $scope.loadFacilitiesForProgram = function () {
    if ($scope.selectedProgram.id) {
      UserSupervisedFacilitiesForProgram.get({programId: $scope.selectedProgram.id}, function (data) {
        $scope.facilities = data.facilities;
        $scope.selectedFacilityId = null;
        $scope.error = null;
      }, {});
    } else {
      $scope.facilities = null;
      $scope.selectedFacilityId = null;
    }
  };

  var getPeriodSpecificButton = function (activeForRnr) {
    return '<input type="button" ng-click="initRnr(row.entity)" openlmis-message="button.proceed" class="btn btn-primary btn-small grid-btn" ng-show="' + activeForRnr + '"/>';
  };

  var optionMessage = function (entity, defaultMessage) {
    return entity === undefined || _.isEmpty(entity) ? messageService.get("label.none.assigned") : defaultMessage;
  };

  var resetValuesForFirstPeriod = function (periodGridData) {
    var firstPeriodWithRnrStatus = periodGridData[0];
    firstPeriodWithRnrStatus.activeForRnr = true;
    if (!firstPeriodWithRnrStatus.rnrId) {
      firstPeriodWithRnrStatus.rnrStatus = messageService.get("msg.rnr.not.started");
    }
  };

  var createPeriodWithRnrStatus = function (periods, rnrs) {

    if (periods === null || periods.length === 0) {
      $scope.error = messageService.get("msg.no.period.available");
      if ($scope.isEmergency) {
        addPreviousRequisitionToPeriodList(rnrs);
      }
      return;
    }

    $scope.periodGridData = [];

    var periodWithRnrStatus;
{'fraction_non_alphanumeric': 0.12829352376262004, 'fraction_numerical': 0.0031191003857834686, 'mean_word_length': 3.2601398601398603, 'pattern_counts': {'":': 14, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3528515', 'n_tokens_mistral': 3527, 'n_tokens_neox': 3413, 'n_words': 728}