public async Task <bool> CreateCustomReport(ReportServiceModel reportServiceModel) { ReportType reportType = await _context.ReportTypes.SingleOrDefaultAsync(x => x.Name == "Custom"); reportServiceModel.Name = $"Custom_{reportServiceModel.StartYear}_{reportServiceModel.StartMonth}_{reportServiceModel.EndYear}_{reportServiceModel.EndMonth}"; if (await _context.Reports.AnyAsync(x => x.Name == reportServiceModel.Name && x.IsDeleted == false)) { return(false); } var report = reportServiceModel.To <Report>(); if (report.StartYear > report.EndYear) { return(false); } else if (report.StartMonth > report.EndMonth) { return(false); } report.ReportType = reportType; report.VehiclesInReport = GetVehiclesInReport(reportServiceModel.StartMonth, reportServiceModel.StartYear, reportServiceModel.EndMonth, reportServiceModel.EndYear); report.MonthlySalariesInReport = GetSalariesInReport(reportServiceModel.StartMonth, reportServiceModel.StartYear, reportServiceModel.EndMonth, reportServiceModel.EndYear); report.MechanicsBaseCosts = report.MonthlySalariesInReport.Sum(x => x.BaseSalary); report.VehicleBaseCost = report.VehiclesInReport.Sum(x => x.Depreciation); report.ExternalRepairCosts = report.VehiclesInReport.Sum(x => x.ExternalRepairs.Sum(y => y.LaborCost + y.PartsCost)); report.InternalRepairCosts = report.VehiclesInReport.Sum(x => x.InternalRepairs.Sum(y => (decimal)y.HoursWorked * y.MdmsUser.AdditionalOnHourPayment) + x.InternalRepairs.Sum(y => y.InternalRepairParts.Sum(z => z.Quantity * z.Part.Price))); await _context.AddAsync(report); var result = await _context.SaveChangesAsync(); return(result > 0); }