<commit_before>(function() {

    'use strict';

    angular.module('openlmis.requisitions').factory('PeriodFactory', periodFactory);

    periodFactory.$inject = ['$resource', 'RequisitionURL', 'RequisitionService',
    'messageService', '$q', 'DateUtils', 'Status'];

    function periodFactory($resource, RequisitionURL, RequisitionService, messageService, $q,
    DateUtils, Status) {

        var resource = $resource(RequisitionURL('/api/requisitions/periodsForInitiate'), {}, {
            get: {
                method: 'GET',
                isArray: true,
                transformResponse: transformResponse
            }
        });

        var service = {
            get: get
        };
        return service;

        function get(programId, facilityId, emergency) {
            var deferred = $q.defer();

            resource.get({programId: programId, facilityId: facilityId, emergency: emergency}, function(data) {
                getPeriodGridLineItems(data, programId, facilityId, emergency).then(function(items) {
                    deferred.resolve(items);
                }).catch(function() {
                    deferred.reject();
                });
            }, function() {
                deferred.reject();
            });
            return deferred.promise;
        }

        function getPeriodGridLineItems(periods, programId, facilityId, emergency) {
            var periodGridLineItems = [],
                deferred = $q.defer();

            if (emergency == true) {
                getPreviousPeriodLineItems(programId, facilityId, emergency).then(search);

            } else {
                search(periodGridLineItems);
            }

            return deferred.promise;

            function search(lineItems) {
                RequisitionService.search(programId, facilityId).then(function(data) {
                    periods.forEach(function (period, idx) {
                        var foundRequisition = null;
                        data.forEach(function (requisition) {
                            if (requisition.processingPeriod.id == period.id) {
                                foundRequisition = requisition;
                            }
                        });
                        if (emergency == false || (emergency == true &&
                        foundRequisition.status != Status.INITIATED &&
                        foundRequisition.status != Status.SUBMITTED)) {
                            lineItems.push(createPeriodGridItem(period, foundRequisition, idx));
                        }
                    });
                    deferred.resolve(lineItems);
                }, function() {
                    periods.forEach(function (period, idx) {
                        lineItems.push(createPeriodGridItem(period, null, idx));
                    });
                    deferred.resolve(lineItems);
                });
            }
        }

        function getPreviousPeriodLineItems(programId, facilityId, emergency) {<commit_msg>OLMIS-1077: Added check if found requisition is not null<commit_after>(function() {

    'use strict';

    angular.module('openlmis.requisitions').factory('PeriodFactory', periodFactory);

    periodFactory.$inject = ['$resource', 'RequisitionURL', 'RequisitionService',
    'messageService', '$q', 'DateUtils', 'Status'];

    function periodFactory($resource, RequisitionURL, RequisitionService, messageService, $q,
    DateUtils, Status) {

        var resource = $resource(RequisitionURL('/api/requisitions/periodsForInitiate'), {}, {
            get: {
                method: 'GET',
                isArray: true,
                transformResponse: transformResponse
            }
        });

        var service = {
            get: get
        };
        return service;

        function get(programId, facilityId, emergency) {
            var deferred = $q.defer();

            resource.get({programId: programId, facilityId: facilityId, emergency: emergency}, function(data) {
                getPeriodGridLineItems(data, programId, facilityId, emergency).then(function(items) {
                    deferred.resolve(items);
                }).catch(function() {
                    deferred.reject();
                });
            }, function() {
                deferred.reject();
            });
            return deferred.promise;
        }

        function getPeriodGridLineItems(periods, programId, facilityId, emergency) {
            var periodGridLineItems = [],
                deferred = $q.defer();

            if (emergency == true) {
                getPreviousPeriodLineItems(programId, facilityId, emergency).then(search);

            } else {
                search(periodGridLineItems);
            }

            return deferred.promise;

            function search(lineItems) {
                RequisitionService.search(programId, facilityId).then(function(data) {
                    periods.forEach(function (period, idx) {
                        var foundRequisition = null;
                        data.forEach(function (requisition) {
                            if (requisition.processingPeriod.id == period.id) {
                                foundRequisition = requisition;
                            }
                        });
                        if (emergency == false || (emergency == true &&
                        foundRequisition != null &&
                        foundRequisition.status != Status.INITIATED &&
                        foundRequisition.status != Status.SUBMITTED)) {
                            lineItems.push(createPeriodGridItem(period, foundRequisition, idx));
                        }
                    });
                    deferred.resolve(lineItems);
                }, function() {
                    periods.forEach(function (period, idx) {
                        lineItems.push(createPeriodGridItem(period, null, idx));
                    });
                    deferred.resolve(lineItems);
                });
            }
        }

        function getPreviousPeriodLineItems(programId, facilityId, emergency) {
{'fraction_non_alphanumeric': 0.1032741488841831, 'fraction_numerical': 0.0006515719172503665, 'mean_word_length': 1.5413907284768211, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '822615', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1359, 'n_words': 256}