public static void GenerateCostsRaportAboutCarsModel(DateTime?raportBegin, DateTime?raportEnd) { string path = GetPath(); if (path == "") { return; } FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None); Document doc = new Document(); PdfWriter writer = PdfWriter.GetInstance(doc, fs); doc.Open(); var db = new AEiI_2020_BD2_Drynda_FlotaEntities(); var carModels = from Models in db.CarModels select Models; //Raport na temat np. Toledo, czyli pojedynczych modeli foreach (var carModel in carModels) { var cars = from Cars in db.Cars where Cars.CarModel.model == carModel.model select Cars; double modelFuelCost = 0.0; double modelServiceCost = 0.0; double modelFuelCostPerKm = 0.0; double modelServiceCostPerKm = 0.0; foreach (var car in cars) { CarCost carCost = CostInfoAboutCar(car, doc, raportBegin, raportEnd, false); modelFuelCost += carCost.fuelCost; modelServiceCost += carCost.serviceCost; modelFuelCostPerKm += carCost.fuelCostPerKm; modelServiceCostPerKm += carCost.serviceCostPerKm; } //ZASTOSOWANIE doc.Add(new iTextSharp.text.Paragraph(carModel.model + "\n\n", Font32)); //ZASTOSOWANIE //O KOSZTACH var modelFuelCostPerCar = modelFuelCost / cars.Count(); modelFuelCostPerCar = Math.Round(modelFuelCostPerCar, 2, MidpointRounding.AwayFromZero); var modelServiceCostPerCar = modelServiceCost / cars.Count(); modelServiceCostPerCar = Math.Round(modelServiceCostPerCar, 2, MidpointRounding.AwayFromZero); doc.Add(new iTextSharp.text.Paragraph("Samochodów: " + cars.Count() + "\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Koszta paliwa: " + modelFuelCost + "\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Koszta paliwa / samochoód: " + modelFuelCostPerCar + "\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Koszta paliwa / 1km: " + modelFuelCostPerKm + "\n\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Koszta serwisu: " + modelServiceCost + "\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Koszta serwisu / samochód: " + modelServiceCostPerCar + "\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Koszta serwisu / 1km: " + modelServiceCostPerKm + "\n\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Sumaryczne koszta: " + (modelFuelCost + modelServiceCost) + "\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Sumaryczne koszta/ samochod: " + (modelServiceCostPerCar + modelFuelCostPerCar) + "\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Sumaryczne koszta / 1km: " + (modelFuelCostPerKm + modelServiceCostPerKm) + "\n", Font14)); //O KOSZTACH doc.NewPage(); } doc.Close(); MessageBox.Show("Raport został wygenerowany.", "Komunikat"); }
private static CarCost CostInfoAboutCar(Car car, Document doc, DateTime?raportBegin, DateTime?raportEnd, bool write) { var distance = 0; var fuelCost = 0.0; var serviceCost = 0.0; var db = new AEiI_2020_BD2_Drynda_FlotaEntities(); //KOSZTA PALIWA var lends = from Lends in db.Lends where car.id == Lends.carId select Lends; if (raportBegin != null) { lends = lends.Where(x => x.lendDate >= raportBegin); } if (raportEnd != null) { lends = lends.Where(x => x.returnDate <= raportEnd); } foreach (var lend in lends) { if (lend.endOdometer != null) { distance += lend.endOdometer.Value - lend.startOdometer; } fuelCost += (distance * 4.75) + (0.05 * lend.Car.engineCapacity); } fuelCost = Math.Round(fuelCost, 2, MidpointRounding.AwayFromZero); var fuelCostPerKm = 0.0; if (distance != 0) { fuelCostPerKm = fuelCost / distance; } fuelCostPerKm = Math.Round(fuelCostPerKm, 2, MidpointRounding.AwayFromZero); //KOSZTA PALIWA //KOSZTA SERWISU var services = from Services in db.Activities where car.id == Services.carId select Services; if (raportBegin != null) { services = services.Where(x => x.reportDate >= raportBegin); } if (raportEnd != null) { services = services.Where(x => x.reportDate >= raportBegin); } foreach (var service in services) { serviceCost += service.price == null ? 0 : (double)service.price; } serviceCost = Math.Round(serviceCost, 2, MidpointRounding.AwayFromZero); var serviceCostPerKm = 0.0; if (distance != 0) { serviceCostPerKm = serviceCost / distance; } serviceCostPerKm = Math.Round(serviceCostPerKm, 2, MidpointRounding.AwayFromZero); //KOSZTA SERWISU if (write) { //SAMOCHÓD doc.Add(new iTextSharp.text.Paragraph(car.id + " " + car.Registration + "\n\n", Font32)); //SAMOCHÓD //O KOSZTACH doc.Add(new iTextSharp.text.Paragraph("Przejechany dystans: " + distance + "\n\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Koszta paliwa: " + fuelCost + "\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Koszta paliwa / 1km: " + fuelCostPerKm + "\n\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Koszta serwisu: " + serviceCost + "\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Koszta serwisu / 1km: " + serviceCostPerKm + "\n\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Sumaryczne koszta: " + (fuelCost + serviceCost) + "\n", Font14)); doc.Add(new iTextSharp.text.Paragraph("Sumaryczne koszta / 1km: " + (fuelCostPerKm + serviceCostPerKm) + "\n", Font14)); //O KOSZTACH doc.NewPage(); } CarCost carCost = new CarCost { fuelCost = fuelCost, serviceCost = serviceCost, fuelCostPerKm = fuelCostPerKm, serviceCostPerKm = serviceCostPerKm }; return(carCost); }