<commit_before>import moment from 'moment';
import * as _ from 'lodash';
import AddPersonController from '../masterdata/persons/modal/AddPersonController';

export default class FlightsController {
    constructor($scope, $q, $log, $modal, $timeout, MessageManager,
                TimeService, Flights, FlightsDateRange,
                FlightTypes, Locations, Persons, PersonPersister, PassengerPersister,
                Aircrafts, StartTypes, GLOBALS, FlightCostBalanceTypes,
                SoloFlightCheckboxEnablementCalculator, Clubs) {
        $scope.busy = true;
        $scope.debug = GLOBALS.DEBUG;
        $scope.showChart = false;
        var format = 'HH:mm';
        var masterDataLoaded = false;

        $scope.starttypes = [];
        $scope.locations = [];
        $scope.gliderFlightTypes = [];
        $scope.towingFlightTypes = [];
        $scope.gliderAircrafts = [];
        $scope.towerAircrafts = [];
        $scope.gliderPilots = [];
        $scope.towingPilots = [];
        $scope.observers = [];
        $scope.winchOperators = [];
        $scope.passengers = [];
        $scope.instructors = [];
        $scope.flightCostBalanceTypes = [];

        function renderWithId(idName, labelName) {
            return (data, escape) => {
                return '<div class="option">' + escape(data[idName]) + ' - ' + escape(data[labelName]) + '</div>';
            }
        }

        function renderPerson(person, escape) {
            return '<div class="option">' + escape(person.Firstname) + ' ' + escape(person.Lastname) + (person.City ? ' (' + escape(person.City) + ')' : '') + '</div>';
        }

        function renderAircraft(aircraft, escape) {
            return '<div class="option">' + escape(aircraft.Immatriculation) +
                (aircraft.CompetitionSign ? ' [' + escape(aircraft.CompetitionSign) + ']' : '') +
                (aircraft.AircraftModel ? ' (' + escape(aircraft.AircraftModel) + ')' : '') + '</div>';
        }

        $scope.renderStarttype = {
            option: renderWithId('StartTypeId', 'StartTypeName'),
            item: renderWithId('StartTypeId', 'StartTypeName')
        };

        $scope.renderFlighttype = {
            option: renderWithId('FlightCode', 'FlightTypeName'),
            item: renderWithId('FlightCode', 'FlightTypeName')
        };

        $scope.renderPerson = {
            option: renderPerson,
            item: renderPerson
        };

        $scope.renderAircraft = {
            option: renderAircraft,
            item: renderAircraft
        };

        var today = moment();
        var dateStringFormat = 'DD.MM.YYYY';

        function formatDate(d) {
            return d.format(dateStringFormat);
        }

        var todayFormatted = formatDate(today);
        $scope.fromDate = todayFormatted;
        $scope.toDate = todayFormatted;

        $scope.loadAllTime = function () {
            $scope.fromDate = '';
            $scope.toDate = todayFormatted;
            reloadFlights();
        };
        $scope.loadMonths = function (num) {
            $scope.fromDate = formatDate(moment().subtract(num, 'months'));
            $scope.toDate = todayFormatted;
            reloadFlights();
        };
        $scope.loadMonth = function (num) {
            $scope.fromDate = formatDate(moment().startOf('month').subtract(num, 'months'));
            $scope.toDate = formatDate(moment().startOf('month').subtract(num - 1, 'months'));
            reloadFlights();
        };
        $scope.loadToday = function () {
            $scope.fromDate = todayFormatted;
            $scope.toDate = todayFormatted;
            reloadFlights();
        };
        $scope.loadYesterday = function () {
            $scope.fromDate = formatDate(moment().subtract(1, 'days'));
            $scope.toDate = $scope.fromDate;
            reloadFlights();
        };

        $scope.reloadList = function () {
            reloadFlights();
        };

        function toQueryDate(d) {
            var queryDateFormat = 'YYYY-MM-DD';
            var m = moment(d, dateStringFormat);
            if (m.isValid()) {
                return m.format(queryDateFormat);
            } else {
                return '0001-01-01';
            }
        }

        function reloadFlights() {
            $scope.busy = true;
            var fromDate = toQueryDate($scope.fromDate);
            var toDate = toQueryDate($scope.toDate);
            FlightsDateRange.getFlights({from: fromDate, to: toDate}).$promise
                .then((result) => {
                    $scope.flights = result;
                    for (var i = 0; i < result.length; i++) {
                        $scope.flights[i]._formattedDate = result[i].StartDateTime && moment(result[i].StartDateTime).format('DD.MM.YYYY HH:mm dddd');
                    }
                })
                .catch(_.partial(MessageManager.raiseError, 'load', 'flights'))
                .finally(function () {
                    $scope.busy = false;
                });
        }

        reloadFlights();

        function hasDetails() {
            return $scope.flightDetails && $scope.flightDetails.GliderFlightDetailsData !== undefined;
        }

        $scope.gliderAircraftSelectionChanged = () => {
            $timeout(() => {
                if (hasDetails()) {
                    recalcCheckboxState();
                    $scope.gliderCompetitionSign = '?';
                    if ($scope.flightDetails && $scope.gliderAircrafts) {
                        for (var i = 0; i < $scope.gliderAircrafts.length; i++) {
                            var glider = $scope.gliderAircrafts[i];
                            if (glider.AircraftId === $scope.flightDetails.GliderFlightDetailsData.AircraftId) {
                                $scope.flightDetails.GliderFlightDetailsData.IsSoloFlight = glider.NrOfSeats === 1;
                                $scope.gliderCompetitionSign = glider.CompetitionSign;
                            }
                        }
                    }
                }
            }, 0);
        };

        $scope.towingAircraftSelectionChanged = function () {
            if ($scope.flightDetails.TowFlightDetailsData && !$scope.flightDetails.TowFlightDetailsData.PilotPersonId) {
                $scope.flightDetails.TowFlightDetailsData.PilotPersonId = "";
            }
            $timeout(() => {
                if (hasDetails()) {
                    $scope.towplaneRegistration = '?';
                    if ($scope.flightDetails && $scope.towerAircrafts) {
                        for (var i = 0; i < $scope.towerAircrafts.length; i++) {
                            var tow = $scope.towerAircrafts[i];
                            if (tow.AircraftId === ($scope.flightDetails.TowFlightDetailsData && $scope.flightDetails.TowFlightDetailsData.AircraftId)) {
                                $scope.flightDetails.TowFlightDetailsData.AircraftId = tow.AircraftId;
                                $scope.towplaneRegistration = tow.Immatriculation.substring(tow.Immatriculation.indexOf('-') + 2);
                            }
                        }
                    }
                }
            }, 0);
        };

        function initForNewFlight(flightDetails) {
            let deferred = $q.defer();
            flightDetails.GliderFlightDetailsData.FlightCostBalanceType = 1;
            flightDetails.CanUpdateRecord = true;
            Clubs.getMyClub().$promise.then((result) => {
                $scope.myClub = result;
                flightDetails.GliderFlightDetailsData.StartLocationId = $scope.myClub.HomebaseId || flightDetails.GliderFlightDetailsData.StartLocationId;
                flightDetails.GliderFlightDetailsData.LdgLocationId = $scope.myClub.HomebaseId || flightDetails.GliderFlightDetailsData.LdgLocationId;
                flightDetails.GliderFlightDetailsData.FlightTypeId = $scope.myClub.DefaultGliderFlightTypeId || flightDetails.GliderFlightDetailsData.FlightTypeId;
                flightDetails.TowFlightDetailsData.StartLocationId = $scope.myClub.HomebaseId || flightDetails.TowFlightDetailsData.StartLocationId;
                flightDetails.TowFlightDetailsData.LdgLocationId = $scope.myClub.HomebaseId || flightDetails.TowFlightDetailsData.LdgLocationId;
                flightDetails.TowFlightDetailsData.FlightTypeId = $scope.myClub.DefaultTowFlightTypeId || flightDetails.TowFlightDetailsData.FlightTypeId;
                flightDetails.StartType = flightDetails.StartType || $scope.myClub.DefaultStartType || "1";
                deferred.resolve(flightDetails);
            }).catch((reason) => {
                deferred.reject(reason);
            });
            return deferred.promise;
        }

        function loadFlight(flight, toBeCopied) {
            if (flight.FlightId) {<commit_msg>changed default for number of landings to 1<commit_after>import moment from 'moment';
import * as _ from 'lodash';
import AddPersonController from '../masterdata/persons/modal/AddPersonController';

export default class FlightsController {
    constructor($scope, $q, $log, $modal, $timeout, MessageManager,
                TimeService, Flights, FlightsDateRange,
                FlightTypes, Locations, Persons, PersonPersister, PassengerPersister,
                Aircrafts, StartTypes, GLOBALS, FlightCostBalanceTypes,
                SoloFlightCheckboxEnablementCalculator, Clubs) {
        $scope.busy = true;
        $scope.debug = GLOBALS.DEBUG;
        $scope.showChart = false;
        var format = 'HH:mm';
        var masterDataLoaded = false;

        $scope.starttypes = [];
        $scope.locations = [];
        $scope.gliderFlightTypes = [];
        $scope.towingFlightTypes = [];
        $scope.gliderAircrafts = [];
        $scope.towerAircrafts = [];
        $scope.gliderPilots = [];
        $scope.towingPilots = [];
        $scope.observers = [];
        $scope.winchOperators = [];
        $scope.passengers = [];
        $scope.instructors = [];
        $scope.flightCostBalanceTypes = [];

        function renderWithId(idName, labelName) {
            return (data, escape) => {
                return '<div class="option">' + escape(data[idName]) + ' - ' + escape(data[labelName]) + '</div>';
            }
        }

        function renderPerson(person, escape) {
            return '<div class="option">' + escape(person.Firstname) + ' ' + escape(person.Lastname) + (person.City ? ' (' + escape(person.City) + ')' : '') + '</div>';
        }

        function renderAircraft(aircraft, escape) {
            return '<div class="option">' + escape(aircraft.Immatriculation) +
                (aircraft.CompetitionSign ? ' [' + escape(aircraft.CompetitionSign) + ']' : '') +
                (aircraft.AircraftModel ? ' (' + escape(aircraft.AircraftModel) + ')' : '') + '</div>';
        }

        $scope.renderStarttype = {
            option: renderWithId('StartTypeId', 'StartTypeName'),
            item: renderWithId('StartTypeId', 'StartTypeName')
        };

        $scope.renderFlighttype = {
            option: renderWithId('FlightCode', 'FlightTypeName'),
            item: renderWithId('FlightCode', 'FlightTypeName')
        };

        $scope.renderPerson = {
            option: renderPerson,
            item: renderPerson
        };

        $scope.renderAircraft = {
            option: renderAircraft,
            item: renderAircraft
        };

        var today = moment();
        var dateStringFormat = 'DD.MM.YYYY';

        function formatDate(d) {
            return d.format(dateStringFormat);
        }

        var todayFormatted = formatDate(today);
        $scope.fromDate = todayFormatted;
        $scope.toDate = todayFormatted;

        $scope.loadAllTime = function () {
            $scope.fromDate = '';
            $scope.toDate = todayFormatted;
            reloadFlights();
        };
        $scope.loadMonths = function (num) {
            $scope.fromDate = formatDate(moment().subtract(num, 'months'));
            $scope.toDate = todayFormatted;
            reloadFlights();
        };
        $scope.loadMonth = function (num) {
            $scope.fromDate = formatDate(moment().startOf('month').subtract(num, 'months'));
            $scope.toDate = formatDate(moment().startOf('month').subtract(num - 1, 'months'));
            reloadFlights();
        };
        $scope.loadToday = function () {
            $scope.fromDate = todayFormatted;
            $scope.toDate = todayFormatted;
            reloadFlights();
        };
        $scope.loadYesterday = function () {
            $scope.fromDate = formatDate(moment().subtract(1, 'days'));
            $scope.toDate = $scope.fromDate;
            reloadFlights();
        };

        $scope.reloadList = function () {
            reloadFlights();
        };

        function toQueryDate(d) {
            var queryDateFormat = 'YYYY-MM-DD';
            var m = moment(d, dateStringFormat);
            if (m.isValid()) {
                return m.format(queryDateFormat);
            } else {
                return '0001-01-01';
            }
        }

        function reloadFlights() {
            $scope.busy = true;
            var fromDate = toQueryDate($scope.fromDate);
            var toDate = toQueryDate($scope.toDate);
            FlightsDateRange.getFlights({from: fromDate, to: toDate}).$promise
                .then((result) => {
                    $scope.flights = result;
                    for (var i = 0; i < result.length; i++) {
                        $scope.flights[i]._formattedDate = result[i].StartDateTime && moment(result[i].StartDateTime).format('DD.MM.YYYY HH:mm dddd');
                    }
                })
                .catch(_.partial(MessageManager.raiseError, 'load', 'flights'))
                .finally(function () {
                    $scope.busy = false;
                });
        }

        reloadFlights();

        function hasDetails() {
            return $scope.flightDetails && $scope.flightDetails.GliderFlightDetailsData !== undefined;
        }

        $scope.gliderAircraftSelectionChanged = () => {
            $timeout(() => {
                if (hasDetails()) {
                    recalcCheckboxState();
                    $scope.gliderCompetitionSign = '?';
                    if ($scope.flightDetails && $scope.gliderAircrafts) {
                        for (var i = 0; i < $scope.gliderAircrafts.length; i++) {
                            var glider = $scope.gliderAircrafts[i];
                            if (glider.AircraftId === $scope.flightDetails.GliderFlightDetailsData.AircraftId) {
                                $scope.flightDetails.GliderFlightDetailsData.IsSoloFlight = glider.NrOfSeats === 1;
                                $scope.gliderCompetitionSign = glider.CompetitionSign;
                            }
                        }
                    }
                }
            }, 0);
        };

        $scope.towingAircraftSelectionChanged = function () {
            if ($scope.flightDetails.TowFlightDetailsData && !$scope.flightDetails.TowFlightDetailsData.PilotPersonId) {
                $scope.flightDetails.TowFlightDetailsData.PilotPersonId = "";
            }
            $timeout(() => {
                if (hasDetails()) {
                    $scope.towplaneRegistration = '?';
                    if ($scope.flightDetails && $scope.towerAircrafts) {
                        for (var i = 0; i < $scope.towerAircrafts.length; i++) {
                            var tow = $scope.towerAircrafts[i];
                            if (tow.AircraftId === ($scope.flightDetails.TowFlightDetailsData && $scope.flightDetails.TowFlightDetailsData.AircraftId)) {
                                $scope.flightDetails.TowFlightDetailsData.AircraftId = tow.AircraftId;
                                $scope.towplaneRegistration = tow.Immatriculation.substring(tow.Immatriculation.indexOf('-') + 2);
                            }
                        }
                    }
                }
            }, 0);
        };

        function initForNewFlight(flightDetails) {
            let deferred = $q.defer();
            flightDetails.GliderFlightDetailsData.FlightCostBalanceType = 1;
            flightDetails.CanUpdateRecord = true;
            Clubs.getMyClub().$promise.then((result) => {
                $scope.myClub = result;
                flightDetails.GliderFlightDetailsData.StartLocationId = $scope.myClub.HomebaseId || flightDetails.GliderFlightDetailsData.StartLocationId;
                flightDetails.GliderFlightDetailsData.LdgLocationId = $scope.myClub.HomebaseId || flightDetails.GliderFlightDetailsData.LdgLocationId;
                flightDetails.GliderFlightDetailsData.FlightTypeId = $scope.myClub.DefaultGliderFlightTypeId || flightDetails.GliderFlightDetailsData.FlightTypeId;
                flightDetails.GliderFlightDetailsData.NrOfLdgs = 1;
                flightDetails.TowFlightDetailsData.StartLocationId = $scope.myClub.HomebaseId || flightDetails.TowFlightDetailsData.StartLocationId;
                flightDetails.TowFlightDetailsData.LdgLocationId = $scope.myClub.HomebaseId || flightDetails.TowFlightDetailsData.LdgLocationId;
                flightDetails.TowFlightDetailsData.FlightTypeId = $scope.myClub.DefaultTowFlightTypeId || flightDetails.TowFlightDetailsData.FlightTypeId;
                flightDetails.TowFlightDetailsData.NrOfLdgs = 1;
                flightDetails.StartType = flightDetails.StartType || $scope.myClub.DefaultStartType || "1";
                deferred.resolve(flightDetails);
            }).catch((reason) => {
                deferred.reject(reason);
            });
            return deferred.promise;
        }

        function loadFlight(flight, toBeCopied) {
            if (flight.FlightId) {
{'fraction_non_alphanumeric': 0.11987664704233249, 'fraction_numerical': 0.0022988505747126436, 'mean_word_length': 1.8730670103092784, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4532288', 'n_tokens_mistral': 5042, 'n_tokens_neox': 4758, 'n_words': 699}