public async Task <IActionResult> Add(AddPaymentInputModel model) { if (!this.ModelState.IsValid) { model.ListOfPaymentTypes = this.paymentTypesService .GetAllPaymentTypes <PaymentTypeDropDownViewModel>(); model.ListOfNotPaidReservations = this.reservationsService .GetAllReservations <DetailsReservationViewModel>() .Where(x => x.TotalAmount > x.AdvancedPayment) .ToList(); return(this.View(model)); } Payment payment = AutoMapperConfig.MapperInstance.Map <Payment>(model); List <Reservation> reservations = new List <Reservation>(); foreach (var res in model.ReservationIds) { var reservation = await this.reservationsService.GetReservationByIdAsync(res); reservations.Add(reservation); } decimal totaAmoumtForAllReservations = reservations.Sum(x => x.TotalAmount); foreach (var res in reservations) { res.AdvancedPayment += model.Amount * res.TotalAmount / totaAmoumtForAllReservations; await this.reservationsService.SaveChangesForReservationAsync(res); var reservationPayment = new ReservationPayment { PaymentId = payment.Id, ReservationId = res.Id, }; payment.ReservationPayments.Add(reservationPayment); } await this.paymentsService.AddPaymentAsync(payment); return(this.Redirect($"/Administration/Payments/All")); }
public IActionResult Add() { var paymentTypes = this.paymentTypesService .GetAllPaymentTypes <PaymentTypeDropDownViewModel>(); var reservations = this.reservationsService .GetAllReservations <DetailsReservationViewModel>() .Where(x => x.TotalAmount > x.AdvancedPayment) .ToList(); var model = new AddPaymentInputModel() { ListOfPaymentTypes = paymentTypes, ListOfNotPaidReservations = reservations, }; return(this.View(model)); }