private List <SelectListItem> GetAvailableMaterials(OrderProductDelivery orderProductDelivery, int?materialId = null)
        {
            List <SelectListItem> availableMaterials = new List <SelectListItem>();

            if (context.Materials.Any())
            {
                foreach (var material in context.Materials.Include(m => m.OrderProductDeliveryMaterials))
                {
                    OrderProductDeliveryMaterial orderProductDeliveryMaterial = material.OrderProductDeliveryMaterials
                                                                                .FirstOrDefault(opdm => opdm.OrderId == orderProductDelivery.OrderId &&
                                                                                                opdm.ProductId == orderProductDelivery.ProductId &&
                                                                                                opdm.DeliveryId == orderProductDelivery.DeliveryId &&
                                                                                                opdm.MaterialId == material.Id);

                    if (orderProductDeliveryMaterial == null)
                    {
                        availableMaterials.Add(new SelectListItem
                        {
                            Value    = material.Id.ToString(),
                            Text     = material.Name,
                            Selected = material.Id == materialId
                        });
                    }
                }
            }

            return(availableMaterials);
        }
Ejemplo n.º 2
0
        public OrderProductDeliveryViewModel(OrderProductDelivery orderProductDelivery)
        {
            OrderProduct        = new OrderProductViewModel(orderProductDelivery.OrderProduct);
            DeliveryId          = orderProductDelivery.DeliveryId;
            DeliveryDate        = orderProductDelivery.DeliveryDate;
            DeliveryImagePath   = orderProductDelivery.DeliveryImagePath;
            AcceptanceId        = orderProductDelivery.AcceptanceId;
            AcceptanceDate      = orderProductDelivery.AcceptanceDate;
            AcceptanceImagePath = orderProductDelivery.AcceptanceImagePath;

            OrderProductDeliveryMaterials = new List <OrderProductDeliveryMaterialViewModel>();
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> AddDeliveryToOrderProduct(AddDeliveryToOrderProductViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            OrderProduct orderProduct = await context.OrderProducts
                                        .Include(op => op.OrderProductDeliveries)
                                        .FirstOrDefaultAsync(op => op.OrderId == model.OrderProduct.Order.Id &&
                                                             op.ProductId == model.OrderProduct.Product.Id);

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

            OrderProductDelivery orderProductDelivery = new OrderProductDelivery();

            orderProductDelivery.OrderProduct   = orderProduct;
            orderProductDelivery.DeliveryId     = model.DeliveryId;;
            orderProductDelivery.DeliveryDate   = model.DeliveryDate;
            orderProductDelivery.AcceptanceId   = model.AcceptanceId;
            orderProductDelivery.AcceptanceDate = model.AcceptanceDate;

            context.OrderProductDeliveries.Add(orderProductDelivery);

            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
            }));
        }
        public async Task <ActionResult> Edit(EditOrderProductDeliveryViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            OrderProductDelivery orderProductDelivery = await context.OrderProductDeliveries
                                                        .FindAsync(model.OrderProduct.Order.Id,
                                                                   model.OrderProduct.Product.Id,
                                                                   model.DeliveryId);

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

            orderProductDelivery.DeliveryDate        = model.DeliveryDate;
            orderProductDelivery.DeliveryImagePath   = model.DeliveryImagePath;
            orderProductDelivery.AcceptanceId        = model.AcceptanceId;
            orderProductDelivery.AcceptanceDate      = model.AcceptanceDate;
            orderProductDelivery.AcceptanceImagePath = model.AcceptanceImagePath;

            context.Entry(orderProductDelivery).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 = orderProductDelivery.OrderId,
                productId = orderProductDelivery.ProductId,
                deliveryId = orderProductDelivery.DeliveryId
            }));
        }
        public async Task <ActionResult> Edit(int?orderId, int?productId, int?deliveryId)
        {
            if (orderId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

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

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

            OrderProductDelivery orderProductDelivery = await context.OrderProductDeliveries
                                                        .Include(opd => opd.OrderProduct.Order)
                                                        .Include(opd => opd.OrderProduct.Product)
                                                        .Include(opd => opd.OrderProductDeliveryMaterials.Select(opdm => opdm.Material))
                                                        .FirstOrDefaultAsync(opd => opd.OrderId == orderId &&
                                                                             opd.ProductId == productId &&
                                                                             opd.DeliveryId == deliveryId);

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

            EditOrderProductDeliveryViewModel model = new EditOrderProductDeliveryViewModel();

            model.OrderProduct        = new OrderProductViewModel(orderProductDelivery.OrderProduct);
            model.DeliveryId          = orderProductDelivery.DeliveryId;
            model.DeliveryDate        = orderProductDelivery.DeliveryDate;
            model.DeliveryImagePath   = orderProductDelivery.DeliveryImagePath;
            model.AcceptanceId        = orderProductDelivery.AcceptanceId;
            model.AcceptanceDate      = orderProductDelivery.AcceptanceDate;
            model.AcceptanceImagePath = orderProductDelivery.AcceptanceImagePath;

            if (orderProductDelivery.OrderProductDeliveryMaterials.Any())
            {
                foreach (var orderProductlDeliveryMaterial in orderProductDelivery.OrderProductDeliveryMaterials)
                {
                    model.OrderProductDeliveryMaterials.Add(new OrderProductDeliveryMaterialViewModel(orderProductlDeliveryMaterial));
                }
            }

            return(View(model));
        }
        public async Task <ActionResult> DeleteMaterialFromOrderProductDelivery(int?orderId, int?productId, int?deliveryId, int?materialId)
        {
            if (orderId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

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

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

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

            OrderProductDelivery orderProductDelivery = await context.OrderProductDeliveries
                                                        .Include(opd => opd.OrderProduct.Order)
                                                        .Include(opd => opd.OrderProduct.Product)
                                                        .Include(opd => opd.OrderProductDeliveryMaterials)
                                                        .FirstOrDefaultAsync(opd => opd.OrderId == orderId &&
                                                                             opd.ProductId == productId &&
                                                                             opd.DeliveryId == deliveryId);

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

            Material material = await context.Materials.FindAsync(materialId);

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

            DeleteMaterialFromOrderProductDeliveryViewModel model = new DeleteMaterialFromOrderProductDeliveryViewModel(orderProductDelivery, material);

            return(View(model));
        }
Ejemplo n.º 7
0
        public async Task <ActionResult> DeleteDeliveryFromOrderProduct(DeleteDeliveryFromOrderProductViewModel model)
        {
            OrderProductDelivery orderProductDelivery = await context.OrderProductDeliveries
                                                        .FindAsync(model.OrderProduct.Order.Id,
                                                                   model.OrderProduct.Product.Id,
                                                                   model.DeliveryId);

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

            context.OrderProductDeliveries.Remove(orderProductDelivery);

            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 = orderProductDelivery.OrderId,
                productId = orderProductDelivery.ProductId
            }));
        }
        public async Task <ActionResult> AddMaterialToOrderProductDelivery(int?orderId, int?productId, int?deliveryId)
        {
            if (orderId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

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

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

            OrderProductDelivery orderProductDelivery = await context.OrderProductDeliveries
                                                        .Include(opd => opd.OrderProduct.Order)
                                                        .Include(opd => opd.OrderProduct.Product)
                                                        .FirstOrDefaultAsync(opa => opa.OrderId == orderId &&
                                                                             opa.ProductId == productId &&
                                                                             opa.DeliveryId == deliveryId);

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

            AddMaterialToOrderProductDeliveryViewModel model = new AddMaterialToOrderProductDeliveryViewModel();

            model.OrderProductDelivery = new OrderProductDeliveryViewModel(orderProductDelivery);
            model.AvailableMaterials   = GetAvailableMaterials(orderProductDelivery);

            return(View(model));
        }
 public DeleteMaterialFromOrderProductDeliveryViewModel(OrderProductDelivery orderProductDelivery, Material material)
 {
     OrderProductDelivery = new OrderProductDeliveryViewModel(orderProductDelivery);
     Material             = new MaterialViewModel(material);
 }
        public async Task <ActionResult> AddMaterialToOrderProductDelivery(AddMaterialToOrderProductDeliveryViewModel model)
        {
            OrderProductDelivery orderProductDelivery = await context.OrderProductDeliveries
                                                        .FindAsync(model.OrderProductDelivery.OrderProduct.Order.Id,
                                                                   model.OrderProductDelivery.OrderProduct.Product.Id,
                                                                   model.OrderProductDelivery.DeliveryId);

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

            if (!ModelState.IsValid)
            {
                model.AvailableMaterials = GetAvailableMaterials(orderProductDelivery, model.MaterialId);

                return(View(model));
            }

            Material material = await context.Materials.FindAsync(model.MaterialId);

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

            context.OrderProductDeliveryMaterials.Add(new OrderProductDeliveryMaterial
            {
                OrderProductDelivery = orderProductDelivery,
                Material             = material,
                DeliveryQuantity     = model.DeliveryQuantity,
                AcceptanceQuantity   = model.AcceptanceQuantity
            });

            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 = orderProductDelivery.OrderId,
                productId = orderProductDelivery.ProductId,
                deliveryId = orderProductDelivery.DeliveryId
            }));
        }