public async Task <ActionResult> Pay(InvoicePaymentViewModel viewModel) { if (ModelState.IsValid) { try { await _service.Pay(viewModel); return(RedirectToAction("View", "CreditCards", new { Id = viewModel.Invoice.CreditCardId })); } catch (ModelValidationException e) { ModelState.AddModelError("InvoiceError", e.Message); return(View(await GetInvoicePaymentViewModel(await _service.GetClosedInvoiceById(viewModel.Invoice.Id)))); } catch (DbUpdateException e) { TempData["ErrorMessage"] = e.Message; return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError)); } catch (NotFoundException e) { TempData["ErrorMessage"] = e.Message; return(new HttpStatusCodeResult(HttpStatusCode.NotFound)); } } else { return(View(await GetInvoicePaymentViewModel(await _service.GetClosedInvoiceById(viewModel.Invoice.Id)))); } }
/// <summary> /// Pay an invoice /// </summary> /// <param name="invoice"></param> public async Task Pay(InvoicePaymentViewModel viewModel) { _movementService = new MovementService(); var invoice = await _repository.GetInvoiceById(viewModel.Invoice.Id); invoice.InvoiceStatus = InvoiceStatus.Paid; invoice.PaymentDate = viewModel.PaymentDate; viewModel.Invoice = invoice; if (viewModel.PaidValue > invoice.TotalValue) { throw new InvalidOperationException($"The max value to pay is {viewModel.Invoice.TotalValue.ToString("F2")}"); } await _movementService.Add(await CreateInvoicePaymentMovementObject(viewModel)); await _movementService.Update(invoice.Movements.Select((m) => { m.MovementStatus = MovementStatus.Launched; return(m); }), false); await _repository.Update(invoice); }
public ActionResult MakeInvoicePayment() { List <InvoicePaymentViewModel> lstObj = new List <InvoicePaymentViewModel>(); List <Int32> InvIds = new ReportDA().GetInvoiceIds(); TicketingReportViewModel model = new TicketingReportViewModel(); model.TicketReport = new ReportDA().GetTicketingReportByDateRange("08/01/2014", "07/31/2015"); List <TransactionEMP> transactions = new ReportDA().GetAllTransactionSummary(); List <InvoiceName> iNames = new ReportDA().GetAllInvoiceNamesSummary(); for (int i = 0; i < 10; i++) { InvoicePaymentViewModel Obj = new InvoicePaymentViewModel(); Obj.Id = i + 1; Obj.InvoiceId = InvIds[i]; Obj.Amount = iNames.Where(a => a.InvoiceId == InvIds[i]).Select(x => x.Amount).Sum(); Obj.Received = transactions.Where(a => a.InvoiceId == InvIds[i]).Select(x => x.Amount).Sum(); lstObj.Add(Obj); } return(View()); }
/// <summary> /// Create a Movement object base on a invoice payment /// </summary> /// <param name="invoicePaymentViewModel"></param> /// <returns></returns> private async Task <Movement> CreateInvoicePaymentMovementObject(InvoicePaymentViewModel invoicePaymentViewModel) { var paymentCategory = await _categoryService.GetByName("Payments", "D"); var creditCardSubcategory = paymentCategory.Subcategories.Where(s => s.Name.Equals("Credit card")).First(); var creditCardName = invoicePaymentViewModel.Invoice.CreditCard.Name.Substring(0, (invoicePaymentViewModel.Invoice.CreditCard.Name.Length > 10) ? 10 : invoicePaymentViewModel.Invoice.CreditCard.Name.Length); return(new Movement { Type = "D", Description = $"Credit card invoice payment: {creditCardName} - {invoicePaymentViewModel.Invoice.Reference}", Value = invoicePaymentViewModel.PaidValue, AccountingDate = invoicePaymentViewModel.PaymentDate, AccountId = invoicePaymentViewModel.AccountId, CategoryId = paymentCategory.Id, SubcategoryId = creditCardSubcategory.Id, MovementStatus = MovementStatus.Launched, CanEdit = false, AutomaticallyLaunch = invoicePaymentViewModel.AutomaticallyLaunch }); }