public async Task <ActionResult> ExportPDF(ReportConfigureViewModel viewModel) { var receiptsModel = await dbReceipt.GetAll(); var receiptsViewModel = receiptsModel.Where(m => m.FueledCarId == viewModel.CarId) .Where(m => m.RefuelingDate >= viewModel.StartDate) .Where(m => m.RefuelingDate <= viewModel.EndDate) .Select(vm => vm.ToViewModel()); var averageFuelCons = decimal.Round(receiptsViewModel.Where(m => m.FuelType == TypeOfFuel.LPG).Average(r => r.FuelConsumption), 2, MidpointRounding.AwayFromZero).ToString().Replace(',', '.'); var maxFuelCons = decimal.Round(receiptsViewModel.Where(m => m.FuelType == TypeOfFuel.LPG).Max(r => r.FuelConsumption), 2, MidpointRounding.AwayFromZero).ToString().Replace(',', '.'); var minFuelCons = decimal.Round(receiptsViewModel.Where(m => m.FuelType == TypeOfFuel.LPG).Min(r => r.FuelConsumption), 2, MidpointRounding.AwayFromZero).ToString().Replace(',', '.'); var avgPrice = decimal.Round(receiptsViewModel.Where(m => m.FuelType == TypeOfFuel.LPG).Average(r => r.PriceFor100km), 2, MidpointRounding.AwayFromZero).ToString().Replace(',', '.'); var totalDistance = decimal.Round(receiptsViewModel.Where(m => m.FuelType == TypeOfFuel.LPG).Sum(r => r.DistanceFromLastRefueling), 2, MidpointRounding.AwayFromZero).ToString().Replace(',', '.'); var totalPrice = decimal.Round(receiptsViewModel.Where(m => m.FuelType == TypeOfFuel.LPG).Sum(r => r.FuelAmount * r.FuelPrice), 2, MidpointRounding.AwayFromZero).ToString().Replace(',', '.'); var lpgConsumptionArray = receiptsViewModel.Where(m => m.FuelType == TypeOfFuel.LPG).Select(r => decimal.Round(r.FuelConsumption, 2, MidpointRounding.AwayFromZero)).ToArray(); var lpgDateTimesArray = receiptsViewModel.Where(m => m.FuelType == TypeOfFuel.LPG).Select(r => r.RefuelingDate.ToString("dd.MM.yyyy")).ToArray(); var exportPdfViewModel = receiptsViewModel.Select(vm => new ExportPdfViewModel { ReportAuthor = HttpContext.User.Identity.IsAuthenticated ? HttpContext.User.Identity.Name : "Anonymous", CarData = viewModel.CarData.ToUpper(), RefuelingDate = vm.RefuelingDate.ToString("dd.MM.yyyy"), PetrolStationName = vm.PetrolStationName, FuelType = vm.FuelType.ToString(), FuelPrice = decimal.Round(vm.FuelPrice, 2, MidpointRounding.AwayFromZero).ToString().Replace(',', '.'), FuelAmount = decimal.Round(vm.FuelAmount, 2, MidpointRounding.AwayFromZero).ToString().Replace(',', '.'), DistanceFromLastRefueling = decimal.Round(vm.DistanceFromLastRefueling, 2, MidpointRounding.AwayFromZero).ToString().Replace(',', '.'), FuelConsumption = decimal.Round(vm.FuelConsumption, 2, MidpointRounding.AwayFromZero).ToString().Replace(',', '.'), PriceFor100km = decimal.Round(vm.PriceFor100km, 2, MidpointRounding.AwayFromZero).ToString().Replace(',', '.'), AverageFuelCons = averageFuelCons, MaxFuelCons = maxFuelCons, MinFuelCons = minFuelCons, AvgPrice = avgPrice, TotalDistance = totalDistance, TotalPrice = totalPrice, LpgConsumptionArray = lpgConsumptionArray, LpgDateTimesArray = lpgDateTimesArray }); string fileName = ($"Report {viewModel.CarData} {viewModel.StartDate.ToString("yyyyMMdd")} {viewModel.EndDate.ToString("yyyyMMdd")}.pdf").Replace(' ', '_'); string footer = "--footer-center \"Report for " + viewModel.CarData + " from " + viewModel.StartDate.ToString("dd.MM.yyyy") + " to " + viewModel.EndDate.ToString("dd.MM.yyyy") + " Page: [page]/[toPage]\"" + " --footer-line --footer-font-size \"9\" --footer-spacing 6 --footer-font-name \"calibri light\""; return(new PartialViewAsPdf("ExportPDF", exportPdfViewModel) { PageSize = Rotativa.Options.Size.A4, PageOrientation = Rotativa.Options.Orientation.Landscape, FileName = fileName, CustomSwitches = footer }); }
// GET: FuelReceipts public async Task <ActionResult> Index(int?id, [Form] TableQueryOptions queryOptions, string searchString) { ViewBag.QueryOptions = queryOptions; var start = QueryOptionsCalculator.CalculateStartPage(queryOptions); ViewBag.CarId = id; if (id != null) { var results = await dbReceipt.GetAll(); var resultsCar = await dbCar.Get(id.GetValueOrDefault()); ViewBag.CarName = $"{resultsCar.CarProducer} {resultsCar.CarModel}"; ViewBag.NumberOfEntries = results.Count(); var receiptViewModels = results.Where(m => m.FueledCarId == id).Select(vm => vm.ToViewModel()).AsQueryable <FuelReceiptViewModel>(); ViewBag.NumberOfEntries = receiptViewModels.Count(); if (!String.IsNullOrEmpty(searchString)) { searchString = searchString.ToUpper(); receiptViewModels = receiptViewModels.Where(r => r.RefuelingDate.ToString().Contains(searchString) || r.PetrolStationName.ToUpper().Contains(searchString) || r.FuelPrice.ToString().Contains(searchString) || r.FuelAmount.ToString().Contains(searchString) || r.FuelConsumption.ToString().Contains(searchString) || r.PriceFor100km.ToString().Contains(searchString)); } queryOptions.TotalPages = QueryOptionsCalculator.CalculateTotalPages(receiptViewModels.Count(), queryOptions.PageSize); return(View(receiptViewModels.OrderBy(queryOptions.Sort).Skip(start).Take(queryOptions.PageSize).ToList())); } return(RedirectToAction("Index", "Cars")); }
// GET: Charts public async Task <ActionResult> Chart(int?id, [Form] ChartQueryOptions chartQueryOptions) { ViewBag.chartQueryOptions = chartQueryOptions; if (id != null) { var results = await db.GetAll(); var carResults = await dbCar.Get(id.GetValueOrDefault()); if (results != null) { var lastFuelingReceipt = results.Where(r => r.FueledCarId == id).Where(r => r.FuelType == TypeOfFuel.LPG); if (lastFuelingReceipt.Count() == 0) { return(RedirectToAction("Index", "FuelReceipts", new { id })); } var lastFueling = lastFuelingReceipt.OrderByDescending(r => r.RefuelingDate).First().RefuelingDate; var startDate = chartQueryOptions.startingTimeRange > lastFueling ? lastFueling : chartQueryOptions.startingTimeRange; ViewBag.startingdate = startDate; var receiptViewModels = results.Where(vm => vm.FueledCarId == id).Where(vm => vm.RefuelingDate >= startDate).Select(vm => vm.ToViewModel()); var chartViewModel = new ChartViewModel() { carId = carResults.Id, carData = $"{carResults.CarProducer} {carResults.CarModel}", lpgConsumptionArray = receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.LPG).Select(r => decimal.Round(r.FuelConsumption, 2, MidpointRounding.AwayFromZero)).ToArray(), lpgPriceArray = receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.LPG).Select(r => decimal.Round(r.PriceFor100km, 2, MidpointRounding.AwayFromZero)).ToArray(), lpgDateTimesArray = receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.LPG).Select(r => r.RefuelingDate.ToString("dd/MM/yyyy")).ToArray(), petrolConsumptionArray = receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.Petrol).Select(r => decimal.Round(r.FuelConsumption, 2, MidpointRounding.AwayFromZero)).ToArray(), petrolPriceArray = receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.Petrol).Select(r => decimal.Round(r.PriceFor100km, 2, MidpointRounding.AwayFromZero)).ToArray(), petrolDateTimesArray = receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.Petrol).Select(r => r.RefuelingDate.ToString("dd/MM/yyyy")).ToArray(), dieselConsumptionArray = receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.Diesel).Select(r => decimal.Round(r.FuelConsumption, 2, MidpointRounding.AwayFromZero)).ToArray(), dieselPriceArray = receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.Diesel).Select(r => decimal.Round(r.PriceFor100km, 2, MidpointRounding.AwayFromZero)).ToArray(), dieselDateTimesArray = receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.Diesel).Select(r => r.RefuelingDate.ToString("dd/MM/yyyy")).ToArray(), averageFuelCons = decimal.Round(receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.LPG).Average(r => r.FuelConsumption), 2, MidpointRounding.AwayFromZero), maxFuelCons = decimal.Round(receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.LPG).Max(r => r.FuelConsumption), 2, MidpointRounding.AwayFromZero), minFuelCons = decimal.Round(receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.LPG).Min(r => r.FuelConsumption), 2, MidpointRounding.AwayFromZero), avgPrice = decimal.Round(receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.LPG).Average(r => r.PriceFor100km), 2, MidpointRounding.AwayFromZero), totalDistance = decimal.Round(receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.LPG).Sum(r => r.DistanceFromLastRefueling), 2, MidpointRounding.AwayFromZero), totalPrice = decimal.Round(receiptViewModels.Where(vm => vm.FuelType == TypeOfFuel.LPG).Sum(r => r.FuelAmount * r.FuelPrice), 2, MidpointRounding.AwayFromZero) }; return(View(chartViewModel)); } else { return(RedirectToAction("Index", "FuelReceipts", new { id })); } } return(RedirectToAction("Index", "Cars")); }