(function () {
  'use strict';
  angular.module('fehlerberichtApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch'
  ]).directive('popover', function () {
    return {
      restrict: 'EA',
      link: function (scope, element, attrs) {
        $(element).popover({
          html: true,
          content: function () {
            return scope.getCommentsInfo($(element).attr('data-popover'));
          }
        });
        $(element).hover(function () {
          $(element).popover('show');
        }, function () {
          $(element).popover('hide');
        });
      }
    };
  }).config([
    '$routeProvider',
    function ($routeProvider) {
      $routeProvider.when('/overview/:customer/:date/:status', {
        templateUrl: 'views/overview.html',
        controller: 'OverviewCtrl'
      }).when('/calendar', {
        templateUrl: 'views/calendar.html',
        controller: 'CalendarCtrl'
      }).when('/customers', {
        templateUrl: 'views/customers.html',
        controller: 'CustomersCtrl'
      }).when('/stats/:customer', {
        templateUrl: 'views/stats.html',
        controller: 'StatsCtrl'
      }).when('/config/:customer/:date', {
        templateUrl: 'views/config.html',
        controller: 'ConfigCtrl'
      }).when('/config', {
        templateUrl: 'views/config.html',
        controller: 'ConfigCtrl'
      }).otherwise({ redirectTo: '/overview/all/today/errors' });
    }
  ]);
  moment.locale('de', {
    calendar: {
      lastDay: '[Gestern]',
      lastWeek: '[letzten] dddd',
      nextDay: '[Morgen]',
      nextWeek: 'dddd',
      sameDay: '[Heute]',
      sameElse: 'L'
    }
  });
}());
'use strict';
angular.module('fehlerberichtApp').controller('CalendarCtrl', [
  '$scope',
  '$http',
  function ($scope, $http) {
    var webservice = 'http://rbs06/fehlerbericht/db.php?';
    $().ready(function () {
      $('#calendar').fullCalendar({
        'weekNumbers': true,
        'header': {
          left: 'title',
          center: '',
          right: 'month prev,next'
        },
        'events': { 'url': webservice + 'a=liste&events=1' }
      });
    });
  }
]);
'use strict';
angular.module('fehlerberichtApp').controller('ConfigCtrl', [
  '$scope',
  '$http',
  '$routeParams',
  function ($scope, $http, $routeParams) {
    var webservice = 'http://rbs06/fehlerbericht/db.php?';
    $scope.current = {
      'kunde': $routeParams.customer,
      'datum': $routeParams.date
    };
    $scope.data = {};
    $scope.domain = {};
    $scope.filter = {};
    $scope.options = {
      'kunde': [],
      'datum': {}
    };
    $scope.dateFormat = function (date) {
      var d = moment(date, 'YYYY-MM-DD');
      if (d.isValid()) {
        return d.format('dd, DD.MM.YYYY');
      }
      return date;
    };
    $scope.refresh = function () {
      $scope.data = {};
      $http.get(webservice + 'a=config&kunde=' + $scope.current.kunde + '&datum=' + $scope.current.datum).success(function (data) {
        $scope.options.datum = data.options;
        $scope.options.datum[undefined] = [];
        $scope.options.kunde = _.keys(data.options);
        if (data.current) {
          $scope.current.datum = data.current.datum;
          $scope.options.tabelle = _.keys(data.current.info);
          $scope.current.tabelle = _.first($scope.options.tabelle);
          $scope.data = data.current.info;
          _.each($scope.data, function (table, t) {
            $scope.domain[t] = {};
            $scope.filter[t] = {};
            _.each(table[0], function (col, k) {
              $scope.domain[t][k] = _.uniq(_.pluck(table, k));
              $scope.filter[t][k] = '';
            });
          });
        }
        $scope.options.datum[undefined] = [];
      });
    };
    $scope.refresh();
  }
]);
'use strict';
angular.module('fehlerberichtApp').controller('CustomersCtrl', [
  '$scope',
  '$http',
  function ($scope, $http) {
    var webservice = 'http://rbs06/fehlerbericht/db.php?';
    $scope.info = [];
    $scope.error = -1;
    $scope.domain = {
      'aktiv': {
        '1': 'Aktiv',
        '0': 'Inaktiv'
      },
      'system': [
        '?',
        'Autosys',
        'Autosys-Light',
        'EDS',
        'Filaks',
        'GlobalCube',
        'Light',
        'Navision',
        'Optima'
      ],
      'woche': [
        'Mo-Fr',
        'Mo-Sa',
        'Mo-So',
        'Di-So'
      ],
      'bundesland': {
        'AT': '\xd6sterreich',
        'BW': 'Baden-W\xfcrttemberg',
        'BY': 'Bayern',
        'BE': 'Berlin',
        'BB': 'Brandenburg',
        'HB': 'Bremen',
        'HH': 'Hamburg',
        'HE': 'Hessen',
        'MV': 'Mecklenburg-Vorpommern',
        'NI': 'Niedersachsen',
        'NW': 'Nordrhein-Westfalen',
        'RP': 'Rheinland-Pfalz',
        'SL': 'Saarland',
        'SN': 'Sachsen',
        'ST': 'Sachsen-Anhalt',
        'SH': 'Schleswig-Holstein',
        'TH': 'Th\xfcringen'
      }
    };
    $scope.kunden = [];
    $scope.current = {
      'zeile': {},
      'fehler': {}
    };
    $scope.Filter = {
      'aktiv': '1',
      'kunde': '',
      'system': ''
    };
    $scope.removeItem = function (index) {
      // Beispiel
      $scope.Bestellungen = $scope.Bestellungen.splice(index, 1);
    };
    $scope.refresh = function () {
      $http.get(webservice + 'a=kunden').success(function (data) {
        $scope.kunden = data;
      });
    };
    $scope.refresh();
    $scope.firstChar = function (t) {
      if (t === '') {
        return '?';
      }
      return t.substring(0, 1);
    };
    $scope.save = function () {
      $http.put(webservice + 'a=kunden', $scope.kunden).success(function (data) {
        $scope.kunden = data;
      });
    };
  }
]);
'use strict';
/**
 * @ngdoc function
 * @name fehlerberichtApp.controller:AboutCtrl
 * @description
 * # AboutCtrl
 * Controller of the fehlerberichtApp
 */
angular.module('fehlerberichtApp').controller('OverviewCtrl', [
  '$scope',
  '$http',
  '$routeParams',
  '$interval',
  function ($scope, $http, $routeParams, $interval) {
    var webservice = 'http://rbs06/fehlerbericht/db.php?';
    $scope.error = {
      'level': -1,
      'info': [],
      'message': ''
    };
    $scope.domain = {
      'bearbeitet': {
        '0': 'angesehen',
        '1': 'behoben',
        '2': 'zugeteilt',
        '3': 'R\xfccksprache mit Kunde',
        '4': 'Serverproblem',
        '5': 'Prozess l\xe4uft',
        '6': 'ignoriert / egal',
        '7': 'weitergeleitet'
      },
      'comment': {
        '0': 'glyphicon-eye-open',
        '1': 'glyphicon-ok',
        '2': 'glyphicon-user',
        '3': 'glyphicon-earphone',
        '4': 'glyphicon-flash',
        '5': 'glyphicon-refresh',
        '6': 'glyphicon-remove',
        '7': 'glyphicon-share-alt'
      },
      'statusfilter': {
        '2': 'nicht behoben',
        '5': 'l\xe4uft',
        '6': 'ignoriert'
      },
      'fehler': {
        '0': 'okay',
        '1': 'fehlerhaft'
      },
      'system': [
        '?',
        'Autosys',
        'Autosys-Light',
        'EDS',
        'Filaks',
        'GlobalCube',
        'Light',
        'Navision',
        'Optima'
      ],
      'datum': {},
      'kunde': {},
      'status': {
        'errors': '1',
        'all': '',
        'missing': '',
        'flawless': '0'
      },
      'fehlend': {
        '1': 'fehlt',
        '0': 'vorhanden'
      },
      'date': {
        'all': '',
        'today': moment().format('YYYY-MM-DD'),
        'yesterday': moment().subtract(1, 'days').format('YYYY-MM-DD')
      },
      'benutzer': []
    };
    $scope.benutzer = window.localStorage.getItem('Benutzer');
    $scope.benutzer2 = 'bitte ausw\xe4hlen';
    $scope.overview = [];
    $scope.comments = {};
    $scope.datum = [];
    $scope.current = {
      'zeile': {},
      'fehler': {},
      'details': null,
      'status': 2,
      'kommentar': {},
      'fehlerdetails': null
    };
    $scope.Filter = {
      'datum': moment($routeParams.date, 'YYYY-MM-DD').isValid() ? $routeParams.date : $scope.domain.date[$routeParams.date],
      'kunde': $routeParams.customer === 'all' ? '' : $routeParams.customer,
      'fehler': $scope.domain.status[$routeParams.status],
      'fehlend': $routeParams.status === 'missing' ? '1' : '',
      'system': '',
      'benutzer': ''
    };
    $scope.Predicate = function () {
      if ($routeParams.customer !== 'all') {
        return 'datum';
      }
      if ($routeParams.date === 'today' && $routeParams.status === 'errors') {
        return 'ende_soll';
      }
      return [
        'kunde',
        'datum'
      ];
    }();
    $scope.Reverse = $routeParams.customer !== 'all';
    $scope.SearchText = '';
    $scope.removeItem = function (index) {
      // Beispiel
      $scope.Bestellungen = $scope.Bestellungen.splice(index, 1);
    };
    $http.get(webservice + 'a=benutzer').success(function (data) {
      $scope.domain.benutzer = _.unique(_.pluck(data, 'benutzer'));
    });
    $http.get(webservice + 'a=kommentar&kunde=' + $scope.Filter.kunde + '&datum=' + $scope.Filter.datum).success(function (data) {
      $scope.comments = _.groupBy(data, 'kunde');
    });
    $scope.refresh = function () {
      $http.get(webservice + 'a=liste&kunde=' + $scope.Filter.kunde + '&datum=' + $scope.Filter.datum).success(function (data) {
        $scope.overview = data;  //$scope.domain.benutzer = _.unique(_.pluck(data, 'benutzer'));
      });
    };
    $scope.importData = function () {
      $scope.error.level = 0;
      $scope.error.message = 'Aktualisierung l\xe4uft!';
      $http.get('http://rbs06/GlobalCube/Fehlerbericht/app/import.php').success(function (data) {
        if (Array.isArray(data)) {
          $scope.error.info = data;
          if (data.length === 0) {
            $scope.error.level = 0;
            $scope.error.message = 'Keine neuen Datens\xe4tze!';
          } else {
            $scope.error.message = 'Neue Datens\xe4tze:';
            $scope.error.level = 0;
            $scope.refresh();
          }
        } else {
          $scope.error.info = [];
          $scope.error.level = 1;
          $scope.error.message = data;
        }
      });
    };
    $scope.refresh();
    $interval($scope.refresh, 5 * 60 * 1000);
    $scope.minuten = function (t) {
      var faktor, m, mins;
      if (!t) {
        return '';
      }
      faktor = 1;
      if (t < '00:00:00') {
        t = t.substring(1);
        faktor = -1;
      }
      m = moment(t, 'HH:mm:ss');
      mins = faktor * (m.minutes() + m.hours() * 60);
      return mins + ' Min.';
    };
    $scope.timeDiffStart = function (z) {
      if (z.start === null) {
        return '';
      }
      var duration = moment.duration(z.start, 'HH:mm:ss').subtract(moment(z.start_soll, 'HH:mm:ss')).asMinutes();
      if (duration !== 0) {
        var plus = duration > 0 ? '+' : '';
        return plus + duration + ' Minuten';
      }
      return '';
    };
    $scope.timeDiff = function (z) {
      if (z.start === null) {
        return '';
      }
      var duration = moment.duration(z.ende, 'HH:mm:ss').subtract(moment(z.ende_soll, 'HH:mm:ss')).asMinutes() - moment.duration(z.start, 'HH:mm:ss').subtract(moment(z.start_soll, 'HH:mm:ss')).asMinutes();
      if (duration !== 0) {
        var plus = duration > 0 ? '+' : '';
        return '(' + plus + duration + ' Min.)';
      }
      return '';
    };
    $scope.dateFormat = function (date) {
      var d = moment(date, 'YYYY-MM-DD');
      if (d.isValid()) {
        if (date === $scope.domain.date.today) {
          return 'Heute';
        }
        if (date === $scope.domain.date.yesterday) {
          return 'Gestern';
        }
        return d.format('dd, DD.MM.YYYY');
      }
      return date;
    };
    $scope.dateTimeFormat = function (date) {
      var d = moment(date, 'YYYY-MM-DD HH:mm:ss');
      if (d.isValid()) {
        return d.format('DD.MM.YYYY, HH:mm');
      }
      return date;
    };
    $scope.timeFormat = function (t) {
      var d = moment(t, 'YYYY-MM-DD HH:mm:ss');
      if (d.isValid()) {
        return d.format('HH:mm:ss');
      }
      return t;
    };
    $scope.mailFormat = function (t) {
      if (t === undefined) {
        return '';
      }
      return _.map(t.split(';'), function (e) {
        return '<a href="mailto:' + e + '">' + e + '</a>';
      }).join('<br/>');
    };
    $scope.left = function (text, length) {
      if (text.length === 0) {
        return '?';
      }
      return text.substring(0, length);
    };
    $scope.errorCount = function (e) {
      if (e === null) {
        return '0';
      }
      if (e.Errors.length === 0 || e.Errors.length === 1 && e.Errors[0] === '') {
        return $scope.daysCount(e);
      }
      var type = 'Info';
      if (e.ErrorLevel < 3) {
        type = 'Fehler';
      }
      return e.Errors.length + ' ' + type;
    };
    $scope.daysCount = function (e) {
      if (e.LastChangedDays === 0) {
        return '0';
      }
      return e.LastChangedDays + ' Tage alt';
    };
    $scope.errorLevel = function (e) {
      return e.ErrorLevel <= 2 || e.Name.indexOf('bat') > -1 || e.LastChangedDays >= 3;
    };
    $scope.setCurrent = function (row) {
      $scope.current.zeile = row;
      $scope.current.fehler = {};
      $scope.current.details = null;
      $scope.current.fehlerdetails = null;
      $scope.current.limit = 7;
      $scope.current.status = 2;
      $scope.current.kommentar = {};
      $scope.current.template = {
        'id': '',
        'datum': row.datum,
        'kunde': row.kunde,
        'start': row.start,
        'benutzer': $scope.benutzer,
        'fehler': row.anzahl === null ? 0 : parseInt(row.anzahl, 10),
        'status': '0',
        'kommentar': '',
        'benutzer2': ''
      };
      $http.get(webservice + 'a=liste&kunde=' + $scope.current.zeile.kunde + '&datum=').success(function (data) {
        $scope.current.overview = data;
      });
      $http.get(webservice + 'a=fehlerbericht&kunde=' + $scope.current.zeile.kunde + '&datum=' + $scope.current.zeile.datum + '&start=' + $scope.current.zeile.start).success(function (data) {
        $scope.current.fehler = data;
        if (data.benutzer === '' || data.benutzer !== $scope.benutzer) {
          $scope.current.kommentar = $scope.current.template;
        } else {
          $scope.current.kommentar = $scope.comments[data.kunde][0];
          $scope.current.kommentar.fehler = parseInt($scope.current.kommentar.fehler, 10);
        }
        data.fehlerbericht = _.sortBy(data.fehlerbericht, function (e) {
          return moment(e.Modified);
        });
        data.weitere = _.chain(data.fehlerbericht).reject($scope.errorLevel).groupBy(function (e) {
          return e.Type;
        }).value();
        var duration = _.map(_.filter(data.fehlerbericht, function (e) {
            return e.Summary !== null && e.Summary.Duration !== null && e.Summary.Duration >= '00:01:00';
          }), function (e) {
            return {
              'label': e.Type + ': ' + e.Name + ' (' + e.Summary.Duration + ')',
              'data': moment.duration(e.Summary.Duration, 'HH:mm:ss').asMinutes()
            };
          });
        $.plot('#duration', duration, {
          series: {
            pie: {
              show: true,
              radius: 0.8
            }
          },
          legend: { show: true }
        });
      });
    };
    $scope.getComments = function (zeile) {
      if ($scope.comments[zeile.kunde]) {
        var comments = _.filter($scope.comments[zeile.kunde], function (row) {
            return row.datum <= zeile.datum;
          });
        if (zeile.fehlend === '1') {
          return comments;
        }
        if (zeile.bearbeitet === '0') {
          var lastErrorComments = _.filter(comments, function (row) {
              return row.status > 0;
            });
          if (lastErrorComments.length > 0) {
            return lastErrorComments;
          }
        }
        var newComments = _.filter(comments, function (row) {
            return row.datum === zeile.datum;
          });
        if (newComments.length > 0) {
          return newComments;
        }
      }
      return false;
    };
    $scope.getCommentsInfo = function (kunde) {
      var comments = _.filter($scope.comments[kunde], function (row) {
          return row.status > 0;
        });
      var info = '<table class="table table-bordered table-condensed">';
      if (comments) {
        for (var i = 0; i < comments.length; i++) {
          info += '<tr><td>' + $scope.dateFormat(comments[i].datum) + '</td><td>' + comments[i].benutzer + '</td><td>' + $scope.domain.bearbeitet[comments[i].status] + '</td><td>' + comments[i].kommentar + '</td></tr>';
        }
      }
      return info + '</table>';
    };
    $scope.getCommentIcon = function (c) {
      var c0 = c;
      if (c.length > 0) {
        c0 = c[0];
      }
      if (c0.status) {
        return $scope.domain.comment[c0.status];
      }
      return 'glyphicon-question-sign';
    };
    $scope.shortFilename = function (filename) {
      if (filename !== undefined && filename.length > 0 && filename.indexOf('\\') >= 0) {
        return filename.substring(filename.lastIndexOf('\\') + 1);
      }
      return filename;
    };
    $scope.showDetails = function (entry) {
      $scope.current.details = entry;
      var duration = _.map(_.filter(entry.Sources, function (e) {
          return e.Duration > '00:01:00';
        }), function (e) {
          return {
            'label': $scope.shortFilename(e.Filename) + ' (' + e.Duration + ')',
            'data': Math.round(moment.duration(e.Duration, 'HH:mm:ss').asMinutes())
          };
        });
      $.plot('#duration_details', duration, {
        series: {
          pie: {
            show: true,
            radius: 0.8
          }
        },
        legend: { show: true }
      });
    };
    $scope.saveUser = function (name) {
      window.localStorage.setItem('Benutzer', name);
      $scope.benutzer = name;
    };
    $scope.saveComments = function () {
      $http.put(webservice + 'a=kommentar&id=' + $scope.current.kommentar.id, $scope.current.kommentar).success(function (data) {
        $scope.comments = _.groupBy(data, 'kunde');
        if ($scope.current.kommentar.id === '') {
          $scope.refresh();
        }
      });
    };
    $scope.saveStartTime = function (zeile) {
      $http.put(webservice + 'a=zeit&kunde=' + zeile.kunde + '&start_soll=' + zeile.start + '&ende_soll=' + zeile.ende_soll).success(function (data) {
        zeile.start_soll = zeile.start;
      });
    };
    $scope.saveEndTime = function (zeile) {
      $http.put(webservice + 'a=zeit&kunde=' + zeile.kunde + '&start_soll=' + zeile.start_soll + '&ende_soll=' + zeile.ende).success(function (data) {
        zeile.ende_soll = zeile.ende;
      });
    };
    $scope.statusFilter = function (row) {
      return row.status >= $scope.current.status || row.datum === $scope.current.zeile.datum;
    };
    $scope.editComment = function (c) {
      c.fehler = parseInt(c.fehler, 10);
      $scope.current.kommentar = c;
    };
  }
]);
(function () {
  'use strict';
  angular.module('fehlerberichtApp').controller('StatsCtrl', [
    '$scope',
    '$http',
    '$routeParams',
    function ($scope, $http, $routeParams) {
      var webservice = 'http://rbs06/fehlerbericht/db.php?';
      $scope.data = {};
      $scope.summary = {};
      $scope.options = {};
      $scope.customer = $routeParams.customer;
      $scope.refresh = function () {
        $scope.data = {};
        $scope.summary = {};
        $http.get(webservice + 'a=config&datum=current&kunde=' + $scope.customer).success(function (data) {
          if (data.current) {
            $scope.options.customer = _.keys(data.options);
            $scope.data = data.current.info;
            var reports = _.filter($scope.data.reports, function (row) {
                return row.Datei.match(/ver/i) === null && row.GIF === 'N';
              });
            $scope.summary = {
              '1. Portale': _.uniq(_.pluck(reports, 'Datei')),
              '1.1 Analyse-User': _.uniq(_.pluck(_.filter(reports, function (row) {
                return row.PDF === 'N';
              }), 'Benutzer')),
              '1.2 PDF-User': _.uniq(_.pluck(_.filter(reports, function (row) {
                return row.PDF === 'J';
              }), 'Benutzer')),
              '1.3 Ebenen': _.uniq(_.pluck(reports, 'Struktur')),
              '2. Versandintervalle': _.uniq(_.pluck($scope.data.versand, 'Datei')),
              '2.1 Empf\xe4nger': _.uniq(_.sortBy(_.pluck($scope.data.versand, 'Empfaenger'), function (row) {
                return row;
              })),
              '2.2 Reports': _.uniq(_.sortBy(_.pluck($scope.data.versand, 'Report'), function (row) {
                return row;
              })),
              '3. Cubes': _.uniq(_.sortBy(_.filter(_.pluck($scope.data.ppr, 'Cube'), function (row) {
                return row !== '';
              }), function (row) {
                return row;
              })),
              '4. Fehlende Reports': _.uniq(_.pluck(_.filter($scope.data.ppr, function (row) {
                return row.PPR === 'fehlt' && row.Typ === 'PPR';
              }), 'Report'))
            };
          }
        });
      };
      $scope.refresh();
      $scope.test = function (v) {
        return v.match(/(test|global-cube)/i);
      };
    }
  ]);
}());