public async Task <IActionResult> NewPosition(int id, string from, [Bind("ProductId,OrderId,Count,Discount")] OrderPositions orderPosition) { if (HttpContext.Session.GetString("EditOrders") == "false") { return(RedirectToAction("Index", "Home")); } string errorMessage = ""; var orderPositionInDB = await _context.OrderPositions .Include(op => op.Product) .ThenInclude(p => p.TaxRate) .FirstOrDefaultAsync(op => op.OrderId == orderPosition.OrderId && op.ProductId == orderPosition.ProductId); if (orderPositionInDB != null) { errorMessage = "Zamówienie już zawiera pozycję \"" + orderPositionInDB.Product.ProductName + "\"!"; } if (orderPosition.Discount < 0) { errorMessage = "Rabat nie może być ujemny!"; } if (orderPosition.Discount > 10) { errorMessage = "Rabat nie może być większy niż 10%!"; } if (orderPosition.Count < 0) { errorMessage = "Ilość produktu nie może być ujemna!"; } if (orderPosition.Count == 0) { errorMessage = "Ilość produktu musi być różna od zera!"; } if (ModelState.IsValid && errorMessage == "") { _context.Add(orderPosition); await _context.SaveChangesAsync(); if (from == "New") { var newOrder = await _context.Orders.FirstOrDefaultAsync(o => o.OrderId == orderPosition.OrderId); return(RedirectToAction("Create", new { id = newOrder.CustomerId, orderId = newOrder.OrderId })); } return(RedirectToAction("Edit", new { id = orderPosition.OrderId })); } var order = await _context.Orders .Include(o => o.Customer) .Include(o => o.Address) .FirstOrDefaultAsync(o => o.OrderId == orderPosition.OrderId); var product = await _context.Products.Include(p => p.TaxRate).FirstOrDefaultAsync(p => p.ProductId == orderPosition.ProductId); if (product == null) { return(NotFound()); } ViewBag.Product = product; ViewBag.Order = order; ViewBag.From = from; ViewBag.ErrorMessage = errorMessage; return(View(orderPosition)); }
public async Task <IActionResult> EditPosition(int id, int?orderId, string?from, [Bind("ProductId,OrderId,Count,Discount")] OrderPositions orderPosition) { if (HttpContext.Session.GetString("EditOrders") == "false") { return(RedirectToAction("Index", "Home")); } if (id != orderPosition.ProductId || orderId != orderPosition.OrderId) { return(NotFound()); } var order = await _context.Orders .Include(o => o.Customer) .Include(o => o.Address) .FirstOrDefaultAsync(o => o.OrderId == orderId); if (order == null) { return(NotFound()); } if (order.RealisationDate != null) { return(RedirectToAction("Details", new { id = orderId })); } string errorMessage = ""; if (orderPosition.Discount < 0) { errorMessage = "Rabat nie może być ujemny!"; } if (orderPosition.Discount > 10) { errorMessage = "Rabat nie może być większy niż 10%!"; } if (orderPosition.Count < 0) { errorMessage = "Ilość produktu nie może być ujemna!"; } if (orderPosition.Count == 0) { errorMessage = "Ilość produktu musi być różna od zera!"; } if (ModelState.IsValid && errorMessage == "") { try { _context.Update(orderPosition); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!OrdersExists(orderPosition.OrderId)) { return(NotFound()); } else { throw; } } if (from != null && from == "newOrder") { return(RedirectToAction("Create", new { id = order.CustomerId, orderId = order.OrderId })); } else { return(RedirectToAction("Edit", new { id = orderId })); } } ViewBag.Customer = order.Customer; ViewBag.Address = order.Address; ViewBag.OrderId = order.OrderId; ViewBag.OrderNumber = order.OrderNumber; ViewBag.OrderDate = order.OrderDate; ViewBag.ErrorMessage = errorMessage; ViewBag.From = from; var position = await _context.OrderPositions .Include(op => op.Product) .ThenInclude(p => p.TaxRate) .FirstOrDefaultAsync(o => o.OrderId == orderId && o.ProductId == id); position.Count = orderPosition.Count; position.Discount = orderPosition.Discount; return(View(position)); }
public async Task <IActionResult> Create(int?id, [Bind("OrderId,OrderNumber,OrderDate,RealisationDate,InvoiceId,UserId,CustomerId,AddressId")] Orders orders) { if (HttpContext.Session.GetString("EditOrders") == "false") { return(RedirectToAction("Index", "Home")); } var orderPositions = await _context.OrderPositions .Include(op => op.Product) .ThenInclude(p => p.TaxRate) .Where(op => op.OrderId == orders.OrderId) .ToListAsync(); if (orderPositions.Count == 0) { ViewBag.ErrorMessage = "Nie możesz zapisać zamówienia bez pozycji!"; } if (orderPositions.Count > 0) { Orders orderToAdd = new Orders(); var lastOrderInDB = await _context.Orders .Where(o => !o.OrderNumber.Contains("R")) .OrderByDescending(o => o.OrderId) .FirstOrDefaultAsync(); string month = DateTime.Now.ToString("MM", DateTimeFormatInfo.InvariantInfo); string year = DateTime.Now.ToString("yy", DateTimeFormatInfo.InvariantInfo); string newOrderNumber; if (lastOrderInDB == null) { newOrderNumber = "ZAM/0001/" + month + "/" + year; } else { string[] lastOrderNumberSplit = lastOrderInDB.OrderNumber.Split('/'); int numberInt; DateTime lastOrderDate = lastOrderInDB.OrderDate; DateTime todayDate = DateTime.Now; if (lastOrderDate.Month == todayDate.Month && lastOrderDate.Year == todayDate.Year) { numberInt = int.Parse(lastOrderNumberSplit[1]); } else { numberInt = 1; } string numberString = "" + (numberInt + 1); while (numberString.Length < 4) { numberString = "0" + numberString; } newOrderNumber = "ZAM/" + numberString + "/" + month + "/" + year; } orderToAdd.OrderNumber = newOrderNumber; orderToAdd.OrderDate = DateTime.Now; orderToAdd.UserId = orders.UserId; orderToAdd.CustomerId = orders.CustomerId; orderToAdd.AddressId = orders.AddressId; _context.Add(orderToAdd); await _context.SaveChangesAsync(); List <OrderPositions> newOrderPositions = new List <OrderPositions>(); orderPositions.ForEach(op => { var newPosition = new OrderPositions(); newPosition.OrderId = orderToAdd.OrderId; newPosition.ProductId = op.ProductId; newPosition.Count = op.Count; newPosition.Discount = op.Discount; newOrderPositions.Add(newPosition); }); _context.RemoveRange(orderPositions); await _context.SaveChangesAsync(); _context.AddRange(newOrderPositions); await _context.SaveChangesAsync(); _context.Remove(orders); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } Orders order; order = await _context.Orders .Include(o => o.OrderPositions) .ThenInclude(op => op.Product) .ThenInclude(p => p.TaxRate) .FirstOrDefaultAsync(o => o.OrderId == orders.OrderId); var customer = await _context.Customers.Include(c => c.Addresses).FirstOrDefaultAsync(c => c.CustomerId == order.CustomerId); if (customer == null) { return(NotFound()); } ViewBag.Customer = customer; ViewData["AddressId"] = new SelectList(customer.Addresses, "AddressId", "FullAddress"); return(View(order)); }
public decimal CalculateTotalPrice() { return(OrderPositions.Sum(item => item.Price * item.Count)); }