<commit_before>var rnrModule = angular.module('rnr', ['openlmis']).config(['$routeProvider', function ($routeProvider) {
    $routeProvider.
        when('/init-rnr', {controller:InitiateRnrController, templateUrl:'partials/init.html', resolve:InitiateRnrController.resolve}).
        when('/create-rnr', {controller:CreateRnrController, templateUrl:'partials/create.html'}).
        otherwise({redirectTo:'/init-rnr'});
}]).directive('rnrValidator', function () {
        return {
            require:'?ngModel',
            link:function (scope, element, attrs, ctrl) {
                var validationFunction = rnrModule[attrs.rnrValidator];
                ctrl.$parsers.unshift(function (viewValue) {
                    if (validationFunction(viewValue, element.attr('name'))) {
                        ctrl.$setValidity(element.attr('name'), true);
                        ctrl.$setValidity('rnrError', true);
                        if (viewValue == "") viewValue = undefined;
                        return viewValue;
                    } else {
                        ctrl.$setValidity(element.attr('name'), false);
                        ctrl.$setValidity('rnrError', false);
                        return undefined;
                    }
                });
            }
        };
    });

rnrModule.positiveInteger = function (value, errorHolder) {
    var toggleErrorMessageDisplay = function (valid, errorHolder) {
        if (valid) {
            document.getElementById(errorHolder).style.display = 'none';
        } else {
            document.getElementById(errorHolder).style.display = 'block';
        }
    };

    var INTEGER_REGEXP = /^\d*$/;
    var valid = INTEGER_REGEXP.test(value);

    if (errorHolder != undefined) toggleErrorMessageDisplay(valid, errorHolder)

    return valid;
};

rnrModule.fill = function (lineItem, programRnrColumnList) {

    function isNumber(number) {
        return !isNaN(parseInt(number));
    }

    function fillConsumption() {
        c = lineItem.quantityDispensed = (isNumber(a) && isNumber(b) && isNumber(d) && isNumber(e)) ? a + b - d - e : null;
    }

    function fillStockInHand() {
        e = lineItem.stockInHand = (isNumber(a) && isNumber(b) && isNumber(c) && isNumber(d)) ? a + b - d - c : null;
    }

    var getSource = function (indicator) {
        var code = null;
        $(programRnrColumnList).each(function (i, column) {
            if (column.indicator == indicator) {
                code = column.source.name;
                return false;
            }
        });
        return code;
    };

    function fillNormalizedConsumption() {
        var m = 3; // will be picked up from the database in future
        var x = isNumber(lineItem.stockOutDays) ? parseInt(lineItem.stockOutDays) : null;

        var f = 0;
        if (getSource('F') != null) {
            if (isNumber(lineItem.newPatientCount)) {
                f = parseInt(lineItem.newPatientCount);
            } else {
                lineItem.normalizedConsumption = null;
                return;
            }
        }

        if (!isNumber(c) || !isNumber(x)) {
            lineItem.normalizedConsumption = null;
            return;
        }

        var dosesPerMonth = parseInt(lineItem.dosesPerMonth);
        var g = parseInt(lineItem.dosesPerDispensingUnit);
        var consumptionAdjustedWithStockOutDays = ((m * 30) - x) == 0 ? c : (c * ((m * 30) / ((m * 30) - x)));
        var adjustmentForNewPatients = (f * Math.ceil(dosesPerMonth / g) ) * m;
        lineItem.normalizedConsumption = Math.round(consumptionAdjustedWithStockOutDays + adjustmentForNewPatients);
    }
<commit_msg>#359 | +Shruthi | Defaulting patient count to 0 when not displayed for calculating normalized consumption. Refactoring.<commit_after>var rnrModule = angular.module('rnr', ['openlmis']).config(['$routeProvider', function ($routeProvider) {
    $routeProvider.
        when('/init-rnr', {controller:InitiateRnrController, templateUrl:'partials/init.html', resolve:InitiateRnrController.resolve}).
        when('/create-rnr', {controller:CreateRnrController, templateUrl:'partials/create.html'}).
        otherwise({redirectTo:'/init-rnr'});
}]).directive('rnrValidator', function () {
        return {
            require:'?ngModel',
            link:function (scope, element, attrs, ctrl) {
                var validationFunction = rnrModule[attrs.rnrValidator];
                ctrl.$parsers.unshift(function (viewValue) {
                    if (validationFunction(viewValue, element.attr('name'))) {
                        ctrl.$setValidity(element.attr('name'), true);
                        ctrl.$setValidity('rnrError', true);
                        if (viewValue == "") viewValue = undefined;
                        return viewValue;
                    } else {
                        ctrl.$setValidity(element.attr('name'), false);
                        ctrl.$setValidity('rnrError', false);
                        return undefined;
                    }
                });
            }
        };
    });

rnrModule.positiveInteger = function (value, errorHolder) {
    var toggleErrorMessageDisplay = function (valid, errorHolder) {
        if (valid) {
            document.getElementById(errorHolder).style.display = 'none';
        } else {
            document.getElementById(errorHolder).style.display = 'block';
        }
    };

    var INTEGER_REGEXP = /^\d*$/;
    var valid = INTEGER_REGEXP.test(value);

    if (errorHolder != undefined) toggleErrorMessageDisplay(valid, errorHolder)

    return valid;
};

rnrModule.fill = function (lineItem, programRnrColumnList) {

    function isNumber(number) {
        return !isNaN(parseInt(number));
    }

    function fillConsumption() {
        c = lineItem.quantityDispensed = (isNumber(a) && isNumber(b) && isNumber(d) && isNumber(e)) ? a + b - d - e : null;
    }

    function fillStockInHand() {
        e = lineItem.stockInHand = (isNumber(a) && isNumber(b) && isNumber(c) && isNumber(d)) ? a + b - d - c : null;
    }

    var getSource = function (indicator) {
        var code = null;
        $(programRnrColumnList).each(function (i, column) {
            if (column.indicator == indicator) {
                code = column.source.name;
                return false;
            }
        });
        return code;
    };

    function fillNormalizedConsumption() {
        var m = 3; // will be picked up from the database in future
        var x = isNumber(lineItem.stockOutDays) ? parseInt(lineItem.stockOutDays) : null;
        var f = isNumber(lineItem.newPatientCount) ? parseInt(lineItem.newPatientCount) : null;
        if (getSource('F') == null) {
            f = 0;
        }

        if (!isNumber(c) || !isNumber(x) || !isNumber(f)) {
            lineItem.normalizedConsumption = null;
            return;
        }

        var dosesPerMonth = parseInt(lineItem.dosesPerMonth);
        var g = parseInt(lineItem.dosesPerDispensingUnit);
        var consumptionAdjustedWithStockOutDays = ((m * 30) - x) == 0 ? c : (c * ((m * 30) / ((m * 30) - x)));
        var adjustmentForNewPatients = (f * Math.ceil(dosesPerMonth / g) ) * m;
        lineItem.normalizedConsumption = Math.round(consumptionAdjustedWithStockOutDays + adjustmentForNewPatients);
    }

{'fraction_non_alphanumeric': 0.12799452429842573, 'fraction_numerical': 0.0030116358658453116, 'mean_word_length': 2.18205574912892, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '983519', 'n_tokens_mistral': 2110, 'n_tokens_neox': 2003, 'n_words': 407}