public ActionResult ConfirmCart(CartDto cart) { // check for second submission if (Session[CartKey] == null) { TempData["ErrorMessage"] = "Already submitted this cart; invoice has already been generated"; return RedirectToAction("DisplayCart"); } // dropping cart reference here to prevent double submission Session[CartKey] = null; if (cart.Rows == null || cart.Rows.Count == 0) { TempData["ErrorMessage"] = "Submitted an empty cart"; return View("EditCart", GetOrInitCartFromSession()); } var invoiceFile = Service.ConfirmCart(cart); var encoding = Encoding.UTF8; var bytes = invoiceFile.FileRows.SelectMany(row => encoding.GetBytes(row + Environment.NewLine)).ToArray(); var contentDisposition = new System.Net.Mime.ContentDisposition { FileName = string.Format("Invoice{0}.csv", invoiceFile.InvoiceNumber), Inline = false }; Response.AppendHeader("Content-Disposition", contentDisposition.ToString()); return File(bytes, contentDisposition.ToString()); }
public InvoiceFileDto ConfirmCart(CartDto cart) { InvoiceFileDto fileDto; using (var context = new DalContext()) { var invoice = SaveInvoice(cart, context); // if code wont call this DbSet, all lazy requests for InvoiceRow.Equipment property will return null var equipments = context.Equipments.ToList(); var fees = context.Fees.ToDictionary(f => f.Type, f => f); fileDto = PrepareInvoiceFile(invoice, fees); } return fileDto; }
public InvoiceFileDto ConfirmCart(CartDto cart) { var message = new Message(); message.Type = MessageType.ConfirmCart; message.Data = cart; var response = SendAndReceiveMessage(message); var invoiceFile = response.Data as InvoiceFileDto; if (invoiceFile == null) throw new BackendUnexpectedResponseDataException(); return invoiceFile; }
private static Invoice SaveInvoice(CartDto cart, DalContext context) { var invoice = new Invoice(); invoice.Customer = cart.CustomerName; invoice.Date = DateTime.Now; invoice.Rows = cart.Rows.Select(cr => GetInvoiceRow(cr, invoice)).ToList(); var lastId = GetLastInvoiceId(context); invoice.Number = string.Format("#{0:D6}", lastId + 1); context.Invoices.Add(invoice); context.SaveChanges(); invoice = context.Invoices.Single(inv => inv.Id == invoice.Id); return invoice; }
private CartDto GetOrInitCartFromSession() { CartDto cart; if (Session[CartKey] == null) { cart = new CartDto(); cart.Rows = new List<CartRowDto>(); Session[CartKey] = cart; } else cart = (CartDto)Session[CartKey]; return cart; }