public async Task <IActionResult> Edit(Guid id, InvoiceCreateEditViewModel vm) { if (id != vm.Id) { return(BadRequest(new MessageDTO("Id and vm.id do not match"))); } if (!await _bll.Invoices.ExistsAsync(vm.Id, User.UserGuidId())) { return(NotFound(new MessageDTO($"Current user does not have invoice with this id {id}"))); } vm.AppUserId = User.UserGuidId(); if (ModelState.IsValid) { await _bll.Invoices.UpdateAsync(_mapper.Map(vm)); await _bll.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } vm.CompanySelectList = new SelectList(await _bll.Companies.GetAllAsync(User.UserGuidId()), nameof(CompanyBLL.Id), nameof(CompanyBLL.CompanyName), vm.CompanyId); return(View(vm)); }
public async Task <IActionResult> Create(InvoiceCreateEditViewModel vm) { // Pärin kõik bookingud var bookings = (await _bll.Bookings.GetAllAsync(User.UserGuidId())); vm.AppUserId = User.UserGuidId(); vm.InvoiceNumber = _bll.Invoices.GetInvoiceNumber(); vm.InvoiceDate = DateTime.Now; vm.VatPercent = 20; var invoiceBLL = _mapper.Map(vm); _bll.Invoices.Add(invoiceBLL); await _bll.SaveChangesAsync(); vm.Id = invoiceBLL.Id; // Add invoice id to bookings // ID saan alles pärast SaveChanges foreach (var booking in bookings) { var bookingBLL = _bll.Bookings.FirstOrDefaultAsync(booking.Id, User.UserGuidId()).Result; bookingBLL.InvoiceId = invoiceBLL.Id; await _bll.Bookings.UpdateAsync(bookingBLL); } await _bll.SaveChangesAsync(); // Make payment var paymentBLL = new PaymentBLL() { AppUserId = User.UserGuidId(), Amount = vm.InvoiceTotal, PaymentDate = DateTime.Now, PaymentTypeId = vm.PaymentTypeId, InvoiceId = invoiceBLL.Id }; _bll.Payments.Add(paymentBLL); await _bll.SaveChangesAsync(); return(RedirectToAction("Index", "Items")); }
// GET: Invoices/Create public async Task <IActionResult> Create() { var bookings = (await _bll.Bookings.GetAllAsync(User.UserGuidId())).ToList(); if (bookings.Count == 0) { return(RedirectToAction("Index", "Bookings")); } var vm = new InvoiceCreateEditViewModel { InvoiceWithoutVat = _bll.Invoices.CalculateInvoiceTotalWithoutVAT(bookings), Vat = _bll.Invoices.CalculateInvoiceVAT(bookings), InvoiceTotal = _bll.Invoices.CalculateInvoiceTotalWithVAT(bookings), Bookings = (bookings).Select(e => new VMMapper <BookingBLL, BookingsCreateEditViewModel>().Map(e)), CompanySelectList = new SelectList(await _bll.Companies.GetAllAsync(User.UserGuidId()), nameof(CompanyBLL.Id), nameof(CompanyBLL.CompanyName)), PaymentTypeSelectList = new SelectList(await _bll.PaymentTypes.GetAllAsync(), nameof(PaymentTypeBLL.Id), nameof(PaymentTypeBLL.Description)) }; return(View(vm)); }