<commit_before>/*
 * Copyright © 2013 VillageReach.  All Rights Reserved.  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 *
 * If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

function SupplylineController($scope,$location,$dialog,messageService,ReportPrograms, AllFacilites, SupervisoryNodes, Supplylines, Supplyline, SupplylineDelete) {

    //initialize
    $scope.supplylinesBackupMap = [];
    $scope.newSupplyline = {};
    $scope.supplylines = {};
    $scope.editSupplyline = {};
    $scope.creationError = '';

    if ($scope.$parent.newSupplylineMode || $scope.$parent.editSupplylineMode) {
        $scope.AddEditMode = true;
        $scope.title = ($scope.$parent.newSupplylineMode) ? $scope.title = 'Add Supply Line' : $scope.title = 'Edit Supply Line';

    } else {
        $scope.AddEditMode = false;
        $scope.title = 'Supply Lines';
    }


    // drop down lists
    ReportPrograms.get(function (data){
        $scope.programs = data.programs;
        //alert(JSON.stringify($scope.programs, null, 4));
    });

    AllFacilites.get(function(data){
        $scope.facilities = data.allFacilities;
    });

    //$scope.facilities  ={};

    SupervisoryNodes.get(function(data){
        $scope.supervisoryNodes = data.supervisoryNodes;
    });

    // all supply lines
    Supplylines.get({}, function (data) {
        $scope.initialSupplylines = angular.copy(data.supplylines, $scope.initialSupplylines);
        $scope.supplylines = data.supplylines;
        //alert(JSON.stringify($scope.supplylines, null, 4));
        for(var supplylineIndex in data.supplylines){
            var supplyline = data.supplylines[supplylineIndex];
            $scope.supplylinesBackupMap[supplyline.id] =  $scope.getBackupSupplyline(supplyline);
        }

    }, function (data) {
        $location.path($scope.$parent.sourceUrl);
    });


//  given supply line
    $scope.getBackupSupplyline = function (supplyline) {
        return {
            programid: supplyline.programid,
            supplyingfacilityid: supplyline.supplyingfacilityid,
            supervisorynodeid: supplyline.supervisorynodeid,
            description: supplyline.description
        };
    };

    $scope.processSave = function (supplyline,form)
    {
      if ($scope.$parent.newSupplylineMode)
      {
         $scope.createSupplyline();
      } else {
          $scope.updateSupplyline(supplyline, form);
      }
    }

    //  switch to new mode
    $scope.startAddNewSupplyline = function() {
        $scope.setFlags('add','start');
        $scope.$parent.formActive = "supplyline-form-active";
    };

    // create supply line
    $scope.createSupplyline = function () {
        $scope.error = "";

        var form = document.getElementById('createSupplylineForm');

        if (form.$invalid) {
            $scope.showError = true;
            return;
        }
        $scope.showError = false;
        Supplylines.save({}, $scope.supplyline, function (data) {

            //alert(JSON.stringify(data.supplyline, null, 4));
            $scope.supplylines.unshift(data.supplyline);
            $scope.completeAddNewSupplyline(data.supplyline);
            $scope.message = data.success;
            setTimeout(function() {
                $scope.$apply(function() {
                    $scope.supplylineslist = data.supplyLineList;
                    $scope.message = "";
                });
            }, 4000);
            $scope.newSupplyline = {};
        }, function (data) {
            $scope.message = "";
            $scope.creationError = data.data.error;
        });
    };

   //  backup record
    $scope.completeAddNewSupplyline = function(supplyline) {
        $scope.supplylinesBackupMap[supplyline.id] = $scope.getBackupSupplyline(supplyline);
        $scope.showError = false;
        $scope.setFlags('add','end');
    };

// cancel record
    $scope.cancelAddNewSupplyline = function(supplyline) {
        $scope.showError = false;<commit_msg>ELMIS-390 Fixed save problems for supply lines<commit_after>/*
 * Copyright © 2013 VillageReach.  All Rights Reserved.  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 *
 * If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

function SupplylineController($scope,$location,$dialog,messageService,ReportPrograms, AllFacilites, SupervisoryNodes, Supplylines, Supplyline, SupplylineDelete) {

    //initialize
    $scope.supplylinesBackupMap = [];
    $scope.newSupplyline = {};
    $scope.supplylines = {};
    $scope.editSupplyline = {};
    $scope.creationError = '';

    if ($scope.$parent.newSupplylineMode || $scope.$parent.editSupplylineMode) {
        $scope.AddEditMode = true;
        $scope.title = ($scope.$parent.newSupplylineMode) ? $scope.title = 'Add Supply Line' : $scope.title = 'Edit Supply Line';

    } else {
        $scope.AddEditMode = false;
        $scope.title = 'Supply Lines';
    }


    // drop down lists
    ReportPrograms.get(function (data){
        $scope.programs = data.programs;
        //alert(JSON.stringify($scope.programs, null, 4));
    });

    AllFacilites.get(function(data){
        $scope.facilities = data.allFacilities;
    });

    //$scope.facilities  ={};

    SupervisoryNodes.get(function(data){
        $scope.supervisoryNodes = data.supervisoryNodes;
    });

    // all supply lines
    Supplylines.get({}, function (data) {
        $scope.initialSupplylines = angular.copy(data.supplylines, $scope.initialSupplylines);
        $scope.supplylines = data.supplylines;
        //alert(JSON.stringify($scope.supplylines, null, 4));
        for(var supplylineIndex in data.supplylines){
            var supplyline = data.supplylines[supplylineIndex];
            $scope.supplylinesBackupMap[supplyline.id] =  $scope.getBackupSupplyline(supplyline);
        }

    }, function (data) {
        $location.path($scope.$parent.sourceUrl);
    });


//  given supply line
    $scope.getBackupSupplyline = function (supplyline) {
        return {
            programid: supplyline.programid,
            supplyingfacilityid: supplyline.supplyingfacilityid,
            supervisorynodeid: supplyline.supervisorynodeid,
            description: supplyline.description
        };
    };

    $scope.processSave = function (supplyline,form)
    {
      if ($scope.$parent.newSupplylineMode)
      {
         $scope.createSupplyline();
      } else {
          $scope.updateSupplyline(supplyline, form);
      }
    }

    //  switch to new mode
    $scope.startAddNewSupplyline = function() {
        $scope.setFlags('add','start');
        $scope.$parent.formActive = "supplyline-form-active";
    };

    // create supply line
    $scope.createSupplyline = function () {
        $scope.error = "";

        var form = document.getElementById('createSupplylineForm');

        if (form.$invalid) {
            $scope.showError = true;
            return;
        }
        $scope.showError = false;
        Supplylines.save({}, $scope.newSupplyline, function (data) {

            //alert(JSON.stringify(data.supplyline, null, 4));
            $scope.supplylines.unshift(data.supplyline);
            $scope.completeAddNewSupplyline(data.supplyline);
            $scope.message = data.success;
            setTimeout(function() {
                $scope.$apply(function() {
                    $scope.supplylineslist = data.supplyLineList;
                    $scope.message = "";
                });
            }, 4000);
            $scope.newSupplyline = {};
        }, function (data) {
            $scope.message = "";
            $scope.creationError = data.data.error;
        });
    };

   //  backup record
    $scope.completeAddNewSupplyline = function(supplyline) {
        $scope.supplylinesBackupMap[supplyline.id] = $scope.getBackupSupplyline(supplyline);
        $scope.showError = false;
        $scope.setFlags('add','end');
    };

// cancel record
    $scope.cancelAddNewSupplyline = function(supplyline) {
        $scope.showError = false;
{'fraction_non_alphanumeric': 0.12894930198383542, 'fraction_numerical': 0.004041146216017634, 'mean_word_length': 2.4576629974597797, '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': '1469199', 'n_tokens_mistral': 2475, 'n_tokens_neox': 2378, 'n_words': 417}