private InvoiceData GetCalucatedInvoiceData(DateTime startDate, DateTime endDate, int localAuthorityId = 0, int billingCycleId = 0) { if (startDate == null) { throw new ArgumentNullException(nameof(startDate)); } if (endDate == null) { throw new ArgumentNullException(nameof(endDate)); } var invoiceResidents = this.GetInvoiceResidentData(startDate, endDate); if (invoiceResidents == null || !invoiceResidents.Any()) { return(null); } if (localAuthorityId > 0) { invoiceResidents = invoiceResidents.Where(d => d.LocalAuthorityId == localAuthorityId); } var numOfDays = _feeCalculatorService.GetNumberOfDaysInMonth(startDate, endDate); // get usernames for mapping var users = _userService.GetUsers(); // assemble validated date with invoice data var validatedInvoiceData = _invoiceDataProvider.GetValidatedInvoices(startDate, endDate).Result; // get comments var comments = _invoiceDataProvider.GetInvoiceComments(startDate, endDate).Result; invoiceResidents.ForEach(d => { d.SchedulePayments.ForEach(sp => { var invoiceValidatedEntity = validatedInvoiceData.Where(ed => ed.LocalAuthorityId == sp.LocalAuthorityId && ed.PaymentTypeId == sp.PaymentTypeId && ed.ResidentId == sp.ResidentId && ed.ScheduleId == sp.Id).FirstOrDefault(); sp.InvoiceValidatedModel = new InvoiceValidatedModel(); if (invoiceValidatedEntity != null) { sp.InvoiceValidatedModel = new InvoiceValidatedModel() { Id = invoiceValidatedEntity.Id, BillingCycleId = invoiceValidatedEntity.BillingCycleId, PaymentTypeId = invoiceValidatedEntity.PaymentTypeId, AmountDue = invoiceValidatedEntity.AmountDue, Validated = invoiceValidatedEntity.Validated, ValidatedAmount = invoiceValidatedEntity.ValidatedAmount, UpdatedDate = invoiceValidatedEntity.UpdatedDate, UpdatedBy = users.Where(u => u.Id == invoiceValidatedEntity.UpdatedById).FirstOrDefault().ForeName }; } // get comments for each sch payments var filteredComments = comments .Where(c => c.LocalAuthorityId == sp.LocalAuthorityId && c.PaymentTypeId == sp.PaymentTypeId && c.ResidentId == sp.ResidentId); var cmts = filteredComments.Select(c => c.Comments).ToArray(); sp.Comments = cmts; }); }); return(new InvoiceData() { BillingCycleId = billingCycleId, BeginDate = startDate, EndDate = endDate, BillingDate = DateTime.Now, //Todo NumberOfDays = numOfDays, InvoiceResidents = invoiceResidents }); }