protected void CurrentOrders_ItemCommand(object sender, ListViewCommandEventArgs e) { if (e.CommandName == "Ship") { // Gather information from the form to send to the BLL for shipping. // ShipOrder(int orderId, ShippingDirections shipping, List<ShippedItem> items) int orderId = 0; Label ordIdLabel = e.Item.FindControl("OrderIdLabel") as Label; //safe cast the control object to a label object if (ordIdLabel != null) { orderId = int.Parse(ordIdLabel.Text); } ShippingDirections shipInfo = new ShippingDirections(); //blank object DropDownList shipViaDropDown = e.Item.FindControl("ShipperDropDown") as DropDownList; if (shipViaDropDown != null) { shipInfo.ShipperId = int.Parse(shipViaDropDown.SelectedValue); } TextBox textTrackingCode = e.Item.FindControl("TrackingCode") as TextBox; if (textTrackingCode != null) { shipInfo.TrackingCode = textTrackingCode.Text; } decimal price; TextBox textFreightCharge = e.Item.FindControl("FreightCharge") as TextBox; if (textFreightCharge != null && decimal.TryParse(textFreightCharge.Text, out price)) { shipInfo.FreightCharge = price; } List <ShippedItem> goods = new List <ShippedItem>(); GridView gv = e.Item.FindControl("ProductGridView") as GridView; if (gv != null) { foreach (GridViewRow row in gv.Rows) { //get product id and ship qty short quantity; HiddenField prodId = row.FindControl("ProductID") as HiddenField; TextBox qty = row.FindControl("ShipQuantity") as TextBox; if (prodId != null && qty != null && short.TryParse(qty.Text, out quantity)) { ShippedItem item = new ShippedItem { Product = prodId.Value, Quantity = quantity }; goods.Add(item); } } } MessageUserControl.TryRun(() => { var controller = new OrderProcessingController(); controller.ShipOrder(orderId, shipInfo, goods); }, "OrderShipment recorded", "The products identified as shipped are recorded in the database"); } }
public void ShipOrder(int orderId, ShippingDirections shipping, List <ShippedItem> items) { using (var context = new WestWindContext()) { // TODO: Validation steps // a) OrderId must be valid var existingOrder = context.Orders.Find(orderId); if (existingOrder == null) { throw new Exception("Order does not exist"); } if (existingOrder.Shipped) { throw new Exception("This order has already been completed"); } if (!existingOrder.OrderDate.HasValue) { throw new Exception("This order is not ready to be shipped (no order date has been specified)"); } // b) ShippingDirections is required (cannot be null) if (shipping == null) { throw new Exception("No shipping details provided"); } // c) Shipper must exist var shipper = context.Shippers.Find(shipping.ShipperId); if (shipper == null) { throw new Exception("Invalid shipper Id"); } // d) Freight charge must be either null (no charge) or > $0.00 // TODO: Q) Should I just convert a $0 charge to a null?? if (shipping.FreightCharge.HasValue && shipping.FreightCharge <= 0) { throw new Exception("Freight charge must be either a positive value or no charge"); } // e) List<ShippedItem> cannot be empty/null if (items == null || !items.Any()) { throw new Exception("No products identified for shipping"); } // f) The products must be on the order foreach (var item in items) { if (item == null) { throw new Exception("Blank item listed in the products to be shipped"); } if (!existingOrder.OrderDetails.Any(x => x.ProductID.ToString() == item.Product)) { throw new Exception($"The product {item.Product} does not exist on the order"); } // f-2) AND items that this supplier provides // TODO: g) Quantities must be greater than zero and less than or equal to the quantity outstanding } //Process order shipment // 1) Create new Shipment var ship = new Shipment { OrderID = orderId, ShipVia = shipping.ShipperId, TrackingCode = shipping.TrackingCode, FreightCharge = shipping.FreightCharge.HasValue ? shipping.FreightCharge.Value : 0, ShippedDate = DateTime.Now }; // 2) Create manifest items for shipment foreach (var item in items) { //Notice that I'm adding the manifest item to the Shipment object //rather than directly to the db context. //That's because by adding to the Shipment object, //the correct values for foreign key fields will be assigned to the new data. ship.ManifestItems.Add(new ManifestItem { ProductID = int.Parse(item.Product), ShipQuantity = item.Quantity }); } // TODO: 3) Check if order is complete; if so, update Order.Shipped // 4) Add the shipment to the context context.Shipments.Add(ship); // 5) Save the changes (as a single transaction) context.SaveChanges(); } }
protected void CurrentOrders_ItemCommand(object sender, ListViewCommandEventArgs e) { if (e.CommandName == "Ship") { // Extract data from the form and call the BLL ship the order //Gather information from the form for the products to be shipped and the shipping information. This //Info is sent to the void OrderProcessingController.SHipOrder(int orderId, ShippingDirections shipping, list<ProductShipment> products) int orderId = 0; ShippingDirections shippingInfo = new ShippingDirections(); Label idLabel = e.Item.FindControl("OrderIdLabel") as Label; //safe cast to a label if (idLabel != null) // I sucessfully got the control { orderId = int.Parse(idLabel.Text); } DropDownList shipVia = e.Item.FindControl("ShipperDropDown") as DropDownList; if (shipVia != null) { shippingInfo.ShipperId = int.Parse(shipVia.SelectedValue); } TextBox tracking = e.Item.FindControl("TrackingCode") as TextBox; if (tracking != null) { shippingInfo.TrackingCode = tracking.Text; } TextBox freight = e.Item.FindControl("FreightCharge") as TextBox; decimal charge; if (freight != null && decimal.TryParse(freight.Text, out charge)) { shippingInfo.FreightCharge = charge; } //Extract the items being shipped, as per the GridView List <ProductShipment> itemShipped = new List <ProductShipment>(); GridView gv = e.Item.FindControl("ProductsGridView") as GridView; if (gv != null) { foreach (GridViewRow row in gv.Rows) { HiddenField prodHidden = row.FindControl("ProdId") as HiddenField; TextBox shipqty = row.FindControl("ShipQuantity") as TextBox; if (prodHidden != null && shipqty != null) { int qty; if (int.TryParse(shipqty.Text, out qty)) { var item = new ProductShipment { ProductId = int.Parse(prodHidden.Value), ShipQuantity = qty }; itemShipped.Add(item); } } } } MessageUserControl.TryRun(() => { //send the data into the bll var controller = new OrderProcessingController(); controller.ShipOrder(orderId, shippingInfo, itemShipped); }, "Success", "The order shipment information has been recorded"); } }
public void ShipOrder(int orderId, ShippingDirections shipping, List <ProductShipment> products) {
public void ShipOrder(int orderId, ShippingDirections shipping, List <ProductShipment> products) { using (var context = new WestWindContext()) { //TODO: Validation var existingOrder = context.Orders.Find(orderId); // a) OrderId must be valid if (existingOrder == null) { throw new Exception("Order does not exist"); } if (existingOrder.Shipped) { throw new Exception("This order has already been completed"); } if (!existingOrder.OrderDate.HasValue) { throw new Exception("This order is not ready to be shipped (no order date has beens specified)"); } // b) products cannot be an empty list if (products == null || !products.Any()) { throw new Exception("No products identified for shipping"); } // c) products identified must be on the order foreach (var item in products) { if (item == null) { throw new Exception("Blank item listed in products to be shipped"); } if (!existingOrder.OrderDetails.Any(x => x.ProductID == item.ProductId)) { throw new Exception($"The product {item.ProductId} does not exist on the order"); } // TODO: d) quantity must be greated than zero and less than or equal to the quantity outstanding } // e) shipper must exist if (shipping == null) { throw new Exception("No shipping details provided"); } var shipper = context.Shippers.Find(shipping.ShipperId); if (shipper == null) { throw new Exception("Invalid shipper ID"); } // f) freight charge must be either null or no charge or > 0 //todo Q) Should i just convert $0 charge to a null if (shipping.FreightCharge.HasValue && shipping.FreightCharge <= 0) { throw new Exception("Freight charge must be either a positive value or no charge"); } // Processing // 1) Create new Shipment var ship = new Shipment { OrderID = orderId, ShipVia = shipping.ShipperId, TrackingCode = shipping.TrackingCode, FreightCharge = shipping.FreightCharge.HasValue ? shipping.FreightCharge.Value : 0, ShippedDate = DateTime.Now }; // 2) Add all manifest items foreach (var item in products) { ship.ManifestItems.Add(new ManifestItem { ProductID = item.ProductId, ShipQuantity = (short)item.ShipQuantity }); } // TODO: 3) Check if order is complete; if so, update Order.Shipped // 4) Add the shipment to the database context context.Shipments.Add(ship); //5) Save the changes context.SaveChanges(); /*Processing (tables/data that must be updated/inserted/deleted/whatever) * Create new Shipment * * Check if order is complete; if so, update Order.Shipped */ } }
protected void CurrentOrders_ItemCommand(object sender, ListViewCommandEventArgs e) { if (e.CommandName == "Ship") { // gather information from the form to send to the BLL for shipping // - ShipOrder(int orderId, ShippingDirections shipping, List<ShippedItem> items) int orderId = 0; Label ordIdLabel = e.Item.FindControl("OrderIdLabel") as Label; // safe cast the Control object to a Label object if (ordIdLabel != null) { orderId = int.Parse(ordIdLabel.Text); } ShippingDirections shipInfo = new ShippingDirections(); // blank obj DropDownList shipViaDropDown = e.Item.FindControl("ShipperDropDown") as DropDownList; if (shipViaDropDown != null) // If i got the control { shipInfo.ShipperId = int.Parse(shipViaDropDown.SelectedValue); } TextBox trackingcodeTxtBox = e.Item.FindControl("TrackingCode") as TextBox; TextBox freightchargeTxtBox = e.Item.FindControl("FreightCharge") as TextBox; if (trackingcodeTxtBox != null) { shipInfo.TrackingCode = (trackingcodeTxtBox.Text); } decimal price; if (freightchargeTxtBox != null && decimal.TryParse(freightchargeTxtBox.Text, out price)) { shipInfo.FreightCharge = decimal.Parse(freightchargeTxtBox.Text); } //shipInfo.FreightCharge = price; List <ShippedItem> goods = new List <ShippedItem>(); GridView gv = e.Item.FindControl("ProductsGridView") as GridView; if (gv != null) { foreach (GridViewRow row in gv.Rows) { short quantity; // get product id and ship qty HiddenField prodId = row.FindControl("ProductId") as HiddenField; TextBox qty = row.FindControl("ShipQuantity") as TextBox; if (prodId != null && qty != null && short.TryParse(qty.Text, out quantity)) { ShippedItem item = new ShippedItem { Product = prodId.Value, Quantity = quantity, }; goods.Add(item); } } } var controller = new OrderProcessingController(); controller.ShipOrder(orderId, shipInfo, goods); } }
public void ShipOrder(int orderID, ShippingDirections shipping, List <ProductShipment> products) { using (var context = new WestWindContext()) { //Validation var existingOrder = context.Orders.Find(orderID); //Order ID must be valid if (existingOrder == null) { throw new Exception("Order does not exist"); } if (existingOrder.Shipped) { throw new Exception("This order has already been completed"); } if (!existingOrder.OrderDate.HasValue) { throw new Exception("this order is not ready to be shipped (no order data has been specified)"); } //Products cannot be an empty list if (products == null || !products.Any()) { throw new Exception("No products identified for shipping"); } //Products identified must be on the order foreach (var item in products) { if (item == null) { throw new Exception("Blank item listed in products to be shipped"); } if (!existingOrder.OrderDetails.Any(x => x.ProductID == item.ProductID)) { throw new Exception($"the product {item.ProductID} does not exist on the order"); } } //Shipper must exist if (shipping == null) { throw new Exception("No shipping details provided"); } var shipper = context.Shippers.Find(shipping.ShipperID); if (shipper == null) { throw new Exception("Invalid shipper ID"); } //Freight Charge must either be null (no charge) or > 0.00 if (shipping.FreightCharge.HasValue && shipping.FreightCharge <= 0) { throw new Exception("Freight charge must be either a positive value or no charge"); } //Processing the shipment //1. Create new Shipment var ship = new Shipment { OrderID = orderID, ShipVia = shipping.ShipperID, TrackingCode = shipping.TrackingCode, FreightCharge = shipping.FreightCharge.HasValue ? shipping.FreightCharge.Value : 0, ShippedDate = DateTime.Now }; //2. Add all manifest items foreach (var item in products) { ship.ManifestItems.Add(new ManifestItem { ProductID = item.ProductID, ShipQuantity = (short)item.ShipQuantity //TODO: Change the data type on ProductShipment.ShipQuantity to a short }); } //3. Check if order is complete; if so, update Order.Shipped //4. Add the shipment to the database context context.Shipments.Add(ship); //5. Save the changes as a single transaction context.SaveChanges(); } }
public void ShipOrder(int orderId, ShippingDirections shipping, List <ShippedItem> items) { using (var context = new WestWindContext()) { //VALIDATION //OrderId must be valid var existingOrder = context.Orders.Find(orderId); if (existingOrder == null) { throw new Exception("Order does not exist"); } if (existingOrder.Shipped) { throw new Exception("This order has already been complete."); } if (!existingOrder.OrderDate.HasValue) { throw new Exception("This order is not ready to be shipped (no order date has been specified)"); } // ShippingDirections is required (cannot be null) if (shipping == null) { throw new Exception("No shipping details have been provided."); } // Shipper must exist var shipper = context.Shippers.Find(shipping.ShipperId); if (shipper == null) { throw new Exception("Invalid shipper ID"); } //Freight charge must be either null (no charge) or > $0.00 if (shipping.FreightCharge.HasValue && shipping.FreightCharge <= 0) { throw new Exception("Freight charge must be either a positive value or no charge"); } //List<ShippedItem> cannot be empty/null if (items == null || !items.Any()) { throw new Exception("No products identified for shipping"); } //The products must be on the order foreach (var item in items) { if (items == null) { throw new Exception("Black items listed in the products to be shipped"); } if (!existingOrder.OrderDetails.Any(x => x.ProductID.ToString() == item.Product)) { throw new Exception($"The product{item.Product} does not exist"); } } /* * Validation: * * * * AND items that this supplier provides * Quantities must be greater than zero and less than or equal to the quantity outstanding * Shipper must exist * * Processing (tables/data that must be updated/inserted/deleted/whatever) * Create new Shipment * Add all manifest items * Check if order is complete; if so, update Order.Shipped */ } }