public ActionResult TakePayment(TakePaymentViewModel model) { var success = true; var failureMessage = "There has been an error whilst taking the payment."; try { if (model.Allocations.Sum(x => x.Value) > model.Amount) { failureMessage += " The amount allocated to individual invoices cannot exceed the total payment amount."; success = false; } else { var date = DateTime.ParseExact(model.PaymentDate, "dd/MM/yyyy", CultureInfo.InvariantCulture); var transaction = paymentService.TakeCustomerPayment(model.Payer.ID, date, model.PaymentMethod, model.Reference, model.Amount, model.Notes, true); foreach (var allocation in model.Allocations.Where(x => x.Value > 0)) { paymentService.AllocatePayment(transaction, allocation.Value, allocation.Key.ID, model.Notes); } } } catch (Exception ex) { success = false; loggingService.LogException(ex); } SetFeedbackMessage(success, "Payment has been taken.", failureMessage); return(RedirectToSamePage()); }
public ActionResult TakePaymentModal(int id) { var statements = statementService.GetCustomerStatementsByPayerID(id); var outstandingStatements = statements .Where(x => x.AmountOutstanding > 0) .OrderBy(x => x.StatementDate) .ToList(); var payer = userService.GetPayer(id); var model = new TakePaymentViewModel() { Payer = payer, TotalOutstanding = outstandingStatements.Sum(x => x.AmountOutstanding) }; outstandingStatements.ForEach(statement => { model.Allocations.Add(statement, 0); }); if (statements.Any()) { model.PaymentMethod = statements.First().Booking.PaymentMethod; } return(PartialView("_TakePaymentModal", model)); }