예제 #1
0
        /// <summary>
        /// export daily report to pdf
        /// </summary>
        /// <param name="vehicleId"></param>
        /// <param name="startPeriod"></param>
        /// <returns></returns>
        public async Task <FileResult> ExportReportPdf(string vehicleId, string startPeriod)
        {
            var      id = Guid.Parse(vehicleId);
            DateTime start;

            try
            {
                DateTime.TryParseExact(startPeriod, "yyyy-MM-dd", null, DateTimeStyles.AssumeLocal, out start);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }


            var endPeriod = start.AddHours(24).AddTicks(-1);
            var vehicle   = await _vehicleService.GetVehicleByIdAsync(id);

            var positions = await _positionService.GetVehiclePositionsByPeriod(id, start, endPeriod);

            MemoryStream stream = new MemoryStream();

            if (positions.Any())
            {
                var report = new CompleteDailyReport(positions.OrderBy(p => p.Timestamp).ToList(), vehicle);
                _pdfService.CreatePdfReport(positions.OrderBy(p => p.Timestamp).ToList(), vehicle, report, stream);
            }
            stream.Flush();      //Always catches me out
            stream.Position = 0; //Not sure if this is required
            return(File(stream, "application/pdf", $"reeport_{vehicle?.VehicleName}_{DateTime.Now.Date:yyyy-MM-dd}.pdf"));
        }