public async Task <IActionResult> Deliver(DeliveryViewModel model) { if (ModelState.IsValid) { var user = await _userHelper.GetUserByEmailAsync(User.Identity.Name); if (user.Address == null || user.FullName == string.Empty) { ModelState.AddModelError(string.Empty, "User must have a full name and an address to deliver"); return(View(model)); } await _orderRepository.DeliverOrderAsync(model); var order = await _orderRepository.GetByIdAsync(model.Id); order.User = user; IEnumerable <OrderDetail> items = _context.OrderDetails.Where(i => i.OrderId == order.Id); order.Items = items; _mailHelper.SendInvoiceMail(User.Identity.Name, model, await _pdfHelper.GenerateBillPDFAsync(order)); return(RedirectToAction("Index")); } return(View()); }
public async Task <IActionResult> CreateBill(BillViewModel model) { if (ModelState.IsValid) { model.FinalValue = _stepRepository.CalculateFinalPrice(model.Consumption); var result = await _billRepository.InsertBillAsync(model); if (result == 0) { return(NotFound()); } else if (result == 1) { ModelState.AddModelError(string.Empty, "The meter is innactive, can't insert new bills"); return(View(model)); } else if (result == 2) { ModelState.AddModelError(string.Empty, "Can't insert bill with month & year lower than water meter's creation"); return(View(model)); } else if (result == 3) { ModelState.AddModelError(string.Empty, "Can't insert bill with month & year equal or higher than the current date"); return(View(model)); } var bill = new Bill { Id = model.Id, Consumption = model.Consumption, FinalValue = model.FinalValue, MonthYear = model.MonthYear, WaterMeterId = model.WaterMeterId }; var waterMeter = await _context.WaterMeters.Include(w => w.User).Where(w => w.Id == model.WaterMeterId).FirstOrDefaultAsync(); _mailHelper.SendInvoiceMail(waterMeter.User.NormalizedUserName, model, await _pdfHelper.GenerateBillPDFAsync(bill)); return(RedirectToAction($"Details/{model.WaterMeterId}")); } var waterMeter1 = await _waterMeterRepository.GetByIdAsync(model.WaterMeterId); model.DatePickerStartDate = new DateTime(waterMeter1.CreationDate.Year, waterMeter1.CreationDate.Month, 1); model.DatePickerEndDate = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month - 1, 1); return(View(model)); }