private List <SelectListItem> GetAvailableMaterials(OrderProduct orderProduct, int?materialId = null) { List <SelectListItem> availableMaterials = new List <SelectListItem>(); if (context.Materials.Any()) { foreach (var material in context.Materials.Include(m => m.OrderProductMaterials)) { OrderProductMaterial orderProductMaterial = material.OrderProductMaterials .FirstOrDefault(opm => opm.OrderId == orderProduct.OrderId && opm.ProductId == orderProduct.ProductId && opm.MaterialId == material.Id); if (orderProductMaterial == null) { availableMaterials.Add(new SelectListItem { Value = material.Id.ToString(), Text = material.Name, Selected = material.Id == materialId }); } } } return(availableMaterials); }
public async Task <ActionResult> Details(int?orderId, int?productId, int?materialId) { if (orderId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (productId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (materialId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } OrderProductMaterial orderProductMaterial = await context.OrderProductMaterials .Include(opm => opm.OrderProduct.Order) .Include(opm => opm.OrderProduct.Product) .Include(opm => opm.Material) .Include(opm => opm.Supplier) .FirstOrDefaultAsync(opa => opa.OrderId == orderId && opa.ProductId == productId && opa.MaterialId == materialId); if (orderProductMaterial == null) { return(HttpNotFound()); } OrderProductMaterialViewModel model = new OrderProductMaterialViewModel(orderProductMaterial); return(View(model)); }
public OrderProductMaterialViewModel(OrderProductMaterial orderProductMaterial) { OrderProduct = new OrderProductViewModel(orderProductMaterial.OrderProduct); Material = new MaterialViewModel(orderProductMaterial.Material); Supplier = new SupplierViewModel(orderProductMaterial.Supplier); Quantity = orderProductMaterial.Quantity; DeliveryDate = orderProductMaterial.DeliveryDate; Price = orderProductMaterial.Price; Rate = orderProductMaterial.Rate; }
public async Task <ActionResult> Edit(EditOrderProductMaterialViewModel model) { if (!ModelState.IsValid) { model.AvailableSuppliers = GetAvailableSuppliers(model.SupplierId); return(View(model)); } OrderProductMaterial orderProductMaterial = await context.OrderProductMaterials .FindAsync(model.OrderProduct.Order.Id, model.OrderProduct.Product.Id, model.Material.Id); if (orderProductMaterial == null) { return(HttpNotFound()); } orderProductMaterial.SupplierId = model.SupplierId; orderProductMaterial.Quantity = model.Quantity; orderProductMaterial.DeliveryDate = model.DeliveryDate; orderProductMaterial.Price = model.Price; orderProductMaterial.Rate = model.Rate; context.Entry(orderProductMaterial).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 = orderProductMaterial.OrderId, productId = orderProductMaterial.ProductId, materialId = orderProductMaterial.MaterialId })); }
public async Task <ActionResult> Edit(int?orderId, int?productId, int?materialId) { if (orderId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (productId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (materialId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } OrderProductMaterial orderProductMaterial = await context.OrderProductMaterials .Include(opm => opm.OrderProduct.Order) .Include(opm => opm.OrderProduct.Product) .Include(opm => opm.Material) .Include(opm => opm.Supplier) .FirstOrDefaultAsync(opa => opa.OrderId == orderId && opa.ProductId == productId && opa.MaterialId == materialId); if (orderProductMaterial == null) { return(HttpNotFound()); } EditOrderProductMaterialViewModel model = new EditOrderProductMaterialViewModel(); model.OrderProduct = new OrderProductViewModel(orderProductMaterial.OrderProduct); model.Material = new MaterialViewModel(orderProductMaterial.Material); model.SupplierId = orderProductMaterial.SupplierId; model.Quantity = orderProductMaterial.Quantity; model.DeliveryDate = orderProductMaterial.DeliveryDate; model.Price = orderProductMaterial.Price; model.Rate = orderProductMaterial.Rate; model.AvailableSuppliers = GetAvailableSuppliers(); return(View(model)); }
public async Task <ActionResult> DeleteMaterialFromOrderProduct(DeleteMaterialFromOrderProductViewModel model) { OrderProductMaterial orderProductMaterial = await context.OrderProductMaterials .FindAsync(model.OrderProduct.Order.Id, model.OrderProduct.Product.Id, model.Material.Id); if (orderProductMaterial == null) { return(HttpNotFound()); } context.OrderProductMaterials.Remove(orderProductMaterial); 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 = orderProductMaterial.OrderId, productId = orderProductMaterial.ProductId })); }