コード例 #1
0
        public IActionResult EditOrderProduct(string id)
        {
            OrderPosition orderPosition = orderPositionRepository.OrderPositions.Include(x => x.Product).FirstOrDefault(o => o.OrderPositionId == id);
            var           model         = new EditOrderProductViewModel()
            {
                OrderPositionId = id,
                OrderId         = orderPosition.OrderId,
                ProductName     = orderPosition.Product.Name,
                Quantity        = orderPosition.Quantity
            };

            return(View(model));
        }
コード例 #2
0
        public async Task <ActionResult> Edit(EditOrderProductViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            OrderProduct orderProduct = await context.OrderProducts
                                        .FindAsync(model.Order.Id, model.Product.Id);

            if (orderProduct == null)
            {
                return(HttpNotFound());
            }

            orderProduct.Price = model.Price;

            context.Entry(orderProduct).State = EntityState.Modified;

            try
            {
                await context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;

                while (ex != null)
                {
                    errorMessage = ex.Message;
                    ex           = ex.InnerException;
                }

                ModelState.AddModelError("", errorMessage);

                return(View(model));
            }

            return(RedirectToAction(nameof(Details),
                                    new
            {
                orderId = orderProduct.OrderId,
                productId = orderProduct.ProductId
            }));
        }
コード例 #3
0
 public IActionResult EditOrderProduct(EditOrderProductViewModel viewModel)
 {
     if (ModelState.IsValid)
     {
         if (viewModel.Quantity > 0)
         {
             var orderPosition = orderPositionRepository.OrderPositions.Include(x => x.Product).FirstOrDefault(x => x.OrderPositionId == viewModel.OrderPositionId);
             if (viewModel.Quantity <= 0)
             {
                 orderPositionRepository.DeleteOrderPosition(orderPosition);
                 TempData["SuccessMessage"] = "Udało się usunąć pozycję z zamówienia";
                 return(RedirectToAction("Orders"));
             }
             var orderPositions = orderPositionRepository.OrderPositions.Include(x => x.Product).Where(o => o.OrderId == viewModel.OrderId).ToList();
             var product        = productRepository.Products.FirstOrDefault(o => o.Name == viewModel.ProductName);
             product.Quantity += orderPosition.Quantity;
             if (viewModel.Quantity > product.Quantity)
             {
                 TempData["ErrorMessage"] = "Nie ma aż tyle tego produktu w magazynie!";
                 return(View(viewModel));
             }
             orderPosition.Quantity = viewModel.Quantity;
             product.Quantity      -= viewModel.Quantity;
             productRepository.SaveProduct(product);
             orderPositionRepository.SaveOrderPosition(orderPosition);
             var order = orderRepository.Orders.FirstOrDefault(x => x.OrderId == viewModel.OrderId);
             order.OrderValue = 0;
             foreach (var element in order.OrderPosition)
             {
                 order.OrderValue += element.Quantity * element.PurchasePrice;
             }
             orderRepository.SaveOrder(order);
             TempData["SuccessMessage"] = "Udało się zapisać zmiany";
             return(RedirectToAction("Orders"));
         }
         else
         {
             TempData["ErrorMessage"] = "Ilość produktu w zamówieniu nie może być mniejsza od 0.";
             return(View(viewModel));
         }
     }
     return(View(viewModel));
 }
コード例 #4
0
        public async Task <ActionResult> Edit(int?orderId, int?productId)
        {
            if (orderId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (productId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            OrderProduct orderProduct = await context.OrderProducts
                                        .Include(op => op.Order)
                                        .Include(op => op.Product)
                                        .Include(op => op.OrderProductAddresses)
                                        .Include(op => op.OrderProductMaterials)
                                        .Include(op => op.OrderProductDeliveries)
                                        .Include(op => op.OrderProductAddresses.Select(opa => opa.Address))
                                        .Include(op => op.OrderProductMaterials.Select(opm => opm.Material))
                                        .Include(op => op.OrderProductMaterials.Select(opm => opm.Supplier))
                                        .FirstOrDefaultAsync(op => op.OrderId == orderId &&
                                                             op.ProductId == productId);

            if (orderProduct == null)
            {
                return(HttpNotFound());
            }

            EditOrderProductViewModel model = new EditOrderProductViewModel();

            model.Order   = new OrderViewModel(orderProduct.Order);
            model.Product = new ProductViewModel(orderProduct.Product);
            model.Price   = orderProduct.Price;

            if (orderProduct.OrderProductAddresses.Any())
            {
                foreach (var orderAddressProduct in orderProduct.OrderProductAddresses)
                {
                    model.OrderProductAddresses.Add(new OrderProductAddressViewModel(orderAddressProduct));
                }
            }

            if (orderProduct.OrderProductMaterials.Any())
            {
                foreach (var orderAddressMaterial in orderProduct.OrderProductMaterials)
                {
                    model.OrderProductMaterials.Add(new OrderProductMaterialViewModel(orderAddressMaterial));
                }
            }

            if (orderProduct.OrderProductDeliveries.Any())
            {
                foreach (var orderProductDelivery in orderProduct.OrderProductDeliveries)
                {
                    model.OrderProductDeliveries.Add(new OrderProductDeliveryViewModel(orderProductDelivery));
                }
            }

            return(View(model));
        }