public async Task <IActionResult> CreateReport([Bind("Date, Odo, Gps, GasStation")]
                                                       kuras.ViewModels.ReportCreateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(NotFound());
            }

            System.Diagnostics.Debug.WriteLine("Date is {0}, the odo id is: {1} and GPS is: {2}", model.Date, model.Odo, model.Gps);

            if (model.Odo == 0 || model.Gps == 0)
            {
                return(NotFound());
            }

            var Reports = await ReturnFormattedReport(model);

            if (Reports.Any())
            {
                foreach (var el in Reports)
                {
                    _context.Add(el);
                }
                await _context.SaveChangesAsync();

                return(View(Reports));
            }

            else
            {
                return(NotFound());
            }
        }
        // private methods
        private async Task <List <Report> > ReturnFormattedReport(kuras.ViewModels.ReportCreateViewModel model)
        {
            var gpsfile = await _context.KilometersFile
                          .FirstOrDefaultAsync(m => m.Id == model.Gps);

            var odofile = await _context.KilometersFile
                          .FirstOrDefaultAsync(m => m.Id == model.Odo);

            var gasStationFile = await _context.GasStationFile
                                 .FirstOrDefaultAsync(m => m.Id == model.GasStation);

            var cards = await _context.Card.ToListAsync();

            var cars = await _context.Car.ToListAsync();

            //if no data in that id
            if (gpsfile == null || odofile == null || gasStationFile == null)
            {
                return(null);
            }

            List <Report> Reports = new List <Report>();

            if (gpsfile.FileName != null && odofile.FileName != null &&
                model.Date != null && gasStationFile.FileName != null)
            {
                var FileDataGps = ReturnFileDataGps(gpsfile.FileName);
                var FileDataOdo = new List <ViewModels.KilometersOdoFileViewModel>();
                var FileDataRF  = ReturnFileDataOdoRF(odofile.FileName, ref FileDataOdo);
                var FileDataGS  = ReturnFileDataGasStation(gasStationFile.FileName);
                if (FileDataGps.Any() && FileDataGS.Any() && FileDataOdo.Any())
                {
                    foreach (var odo in FileDataOdo)
                    {
                        if (odo.Period == model.Date)
                        {
                            // Find the Gps kilometers for the number plate
                            var gps = FileDataGps.FirstOrDefault(x => x.NumberPlate == odo.NumberPlate);
                            // finds the car object where the number plate is equal
                            var cardNumber = cards.FirstOrDefault(x => x.Number == odo.NumberPlate);
                            // calculates driven kilometers from the odometer
                            double km = odo.End - odo.Start;
                            // separates the date
                            var date1 = model.Date.Split('-');
                            if (date1.Length > 1)
                            {
                                if (Int32.TryParse(date1[0], out int year) &&
                                    Int32.TryParse(date1[1], out int month))
                                {
                                    var remfuel = FileDataRF.FirstOrDefault(x => x.NumberPlate == odo.NumberPlate &&
                                                                            x.Month == month && x.Year == year);
                                    string numberplate = odo.NumberPlate;

                                    int carId  = ReturnCarId(cars, numberplate);
                                    int cardId = -1;
                                    if (carId != -1)
                                    {
                                        cardId = ReturnCardId(cards, carId);
                                    }

                                    // finds the gas station data
                                    ViewModels.GasStationFileViewModel gsData = null;
                                    if (cardId != -1)
                                    {
                                        var card = cards.FirstOrDefault(x => x.Id == cardId);

                                        if (Int32.TryParse(card.Number, out int cardnumber))
                                        {
                                            gsData = FileDataGS.FirstOrDefault(x => Int32.Parse(x.CardNumber) == cardnumber);
                                        }
                                    }
                                    // creates a report about a single car/card
                                    if (gps == null)
                                    {
                                        gps = new ViewModels.KilometersGpsFileViewModel
                                        {
                                            DeviceKm = 0
                                        };
                                    }
                                    if (remfuel == null)
                                    {
                                        remfuel = new RemainingFuel
                                        {
                                            StartFuel = 0,
                                            EndFuel   = 0
                                        };
                                    }
                                    if (gsData == null)
                                    {
                                        gsData = new ViewModels.GasStationFileViewModel
                                        {
                                            L   = 0,
                                            Sum = 0
                                        };
                                    }
                                    Report rep = new Report
                                    {
                                        NumberPlate = odo.NumberPlate,
                                        Km          = km,
                                        Km_gps      = gps.DeviceKm,
                                        Date        = model.Date,
                                        Start       = remfuel.StartFuel,
                                        End         = remfuel.EndFuel,
                                        Card        = cardId,
                                        L           = Math.Round(gsData.L, 2),
                                        Suma        = Math.Round(gsData.Sum, 2)
                                    };
                                    Reports.Add(rep);
                                }
                            }
                        }
                    }
                }
            }
            return(Reports);
        }