private async Task <List <Repayment> > ProcessUploadWithoutSeasonId(RepaymentUpload repaymentUpload) { var customerSummaries = await _customerSummaryRepository.GetOutstandingByCustomerId(repaymentUpload.CustomerId); var repayments = new List <Repayment>(); if (customerSummaries.Count <= 0) { repayments.Add(await ProcessUploadToLastDebt(repaymentUpload)); return(repayments); } var linkedList = new LinkedList <CustomerSummary>(customerSummaries); var current = linkedList.First; var amount = repaymentUpload.Amount; int?parentId = null; while (current != null && amount > 0) { var cs = current.Value; var result = DoAdjustment(new AdjustmentInput(amount, cs.TotalCredit - cs.TotalRepaid, cs.CustomerId, repaymentUpload.Date, cs.SeasonId, parentId)); if (result.RemainingAmount <= 0 || current == linkedList.Last) { cs.TotalRepaid += amount; repayments.Add(result.Repayment); _customerSummaryRepository.Update(cs); break; } amount = result.RemainingAmount; if (!parentId.HasValue) { await _appDbContext.SaveChangesAsync(); parentId = result.Repayment.Id; } repayments.Add(result.Repayment); if (amount > 0) { cs.TotalRepaid = cs.TotalCredit; _customerSummaryRepository.Update(cs); result = DoAdjustment(new AdjustmentInput(-amount, null, cs.CustomerId, repaymentUpload.Date, cs.SeasonId, parentId)); repayments.Add(result.Repayment); } current = current.Next; } return(repayments); }
private void UpdateCustomerSummary(int customerId, int seasonId, decimal amount) { var customerSummary = _customerSummaryRepository.GetByCustomerAndSeason(customerId, seasonId); customerSummary.TotalRepaid += amount; _customerSummaryRepository.Update(customerSummary); }