/// <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")); }
/// <summary> /// /// </summary> /// <param name="vehicleId"></param> /// <param name="startPeriod"></param> /// <returns></returns> public async Task <JsonResult> GetDailyVehicleReport(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 hubContext = GlobalHost.ConnectionManager.GetHubContext <SignalRHandler>(); var report = new CompleteDailyReport(); var connctionId = string.Empty; try { connctionId = SignalRHubManager.Connections[User.Identity.Name]; } catch (Exception) { // ignored } var endPeriod = start.AddHours(24).AddTicks(-1); var vehicle = await _vehicleService.GetVehicleByIdAsync(id); var positions = await _positionService.GetVehiclePositionsByPeriod(id, start, endPeriod); if (!string.IsNullOrEmpty(connctionId)) { hubContext.Clients.Client(connctionId).sendprogressVal(50); } if (!positions.Any()) { return(Json(new CompleteDailyReport { VehicleName = vehicle?.VehicleName, ReportDate = start.ToShortDateString(), Positions = new List <TargetViewModel>() }, JsonRequestBehavior.AllowGet)); } report.UpdateProgress += val => { if (string.IsNullOrEmpty(connctionId)) { return; } hubContext.Clients.Client(connctionId) .sendprogressVal(val); }; report.Build(positions.OrderBy(p => p.Timestamp).ToList(), vehicle); report.FuelConsumption = _vehicleService.GetFuelConsuptionByPeriod(start, endPeriod, id, report.Distance); return(Json(report, JsonRequestBehavior.AllowGet)); }