private void updateCartItem(object sender, int delta) { // find the value of the qty to add textbox // First get the grandparent from control hierarchy: ListViewDataItem --> TableCell --> Sender button // then get the values from its controls Control item = ControlHelper.GetParent(sender, 2); string productName = ControlHelper.GetControlValue(item, "ProductDescriptionLabel"); int productID = ControlHelper.GetControlValue(item, "ProductIDLabel", -1); int qtyOnHand = ControlHelper.GetControlValue(item, "QtyOnHandLabel", -1); int qtyInCart = ControlHelper.GetControlValue(item, "QtyInCart", -1); int newQty = qtyInCart + delta; // generate our messages string unit = (newQty > 1) ? "units" : "unit"; string successMessage = string.Format("Cart updated to <strong>{0}</strong> {1} of <strong>{2}</strong>.", newQty, unit, productName); string failureMessage = "Cart cannot be updated for " + productName + " id=" + productID + " qty=" + qtyInCart + " new=" + newQty + " stock=" + qtyOnHand; MessageUserControl.TryRun(() => { // check if user is logged in if (!IsSalesUser || CurrentEmployee == null) { throw new BusinessRuleException(failureMessage, "User must be employee, logged in and have Sales role to add products to cart."); } // check product id else if (productID <= 0) { throw new BusinessRuleException(failureMessage, "Product ID must be positive integer."); } // check out of stock else if (qtyOnHand <= 0 && newQty > 0) { throw new BusinessRuleException(failureMessage, "Product is out of stock, must be deleted from cart."); } // check qty to add else if (newQty < 1 || newQty > qtyOnHand) { throw new BusinessRuleException(failureMessage, string.Format("Quantity must be between 1 and quantity on stock {0}.", qtyOnHand)); } // yay, we can try and add the product to the cart else { // All validation passed, we can safely call our BLL. // Now, our BLL cannot access the AppSecurity assembly, so we needed to get the Employee ID and // pass that to our BLL instead of the username: SalesCartController cartmgr = new SalesCartController(); cartmgr.UpdateCart(CurrentEmployeeID, productID, newQty); // refresh cart summary // CartSummaryView.DataBind(); RefreshCartSummary(); } }, "Success", successMessage); }
// add selected item to cart protected void AddToCartButton_Click(object sender, EventArgs e) { // find the value of the qty to add textbox // First get the grandparent from control hierarchy: ListViewDataItem --> TableCell --> Sender button // then get the values from its controls Control item = ControlHelper.GetParent(sender, 2); string productName = ControlHelper.GetControlValue(item, "ProductDescriptionLabel"); int productID = ControlHelper.GetControlValue(item, "ProductIDLabel", -1); int qtyOnHand = ControlHelper.GetControlValue(item, "QtyOnHandLabel", -1); int qtyToAdd = ControlHelper.GetControlValue(item, "AddToCartQty", -1); // generate our messages string unit = (qtyToAdd > 1) ? "units" : "unit"; string successMessage = string.Format("<strong>{0}</strong> {1} of <strong>{2}</strong> added to cart.", qtyToAdd, unit, productName); string failureMessage = "Product not added to cart."; MessageUserControl.TryRun(() => { // check if user is logged in if (!IsSalesUser || CurrentEmployee == null) { throw new BusinessRuleException(failureMessage, "User must be employee, logged in and have Sales role to add products to cart."); } // check product id else if (productID <= 0) { throw new BusinessRuleException(failureMessage, "Product ID must be positive integer."); } // check out of stock else if (qtyOnHand <= 0) { throw new BusinessRuleException(failureMessage, "Product is out of stock, cannot be added to cart."); } // check qty to add else if (qtyToAdd <= 0 || qtyToAdd > qtyOnHand) { throw new BusinessRuleException(failureMessage, "Quantity to add must be between 1 and quantity on stock."); } // yay, we can try and add the product to the cart else { // All validation passed, we can safely call our BLL. // Now, our BLL cannot access the AppSecurity assembly, so we needed to get the Employee ID and // pass that to our BLL instead of the username: SalesCartController cartmgr = new SalesCartController(); cartmgr.AddToCart(CurrentEmployeeID, productID, qtyToAdd); // refresh cart summary // CartSummaryView.DataBind(); RefreshCartSummary(); } }, "Success", successMessage); }
protected void btnPlaceOrder_Click(object sender, EventArgs e) { string couponCode = CartSummary_Coupon?.Text; string paymentMethod = PaymentTypeDDL?.SelectedValue; MessageUserControl.TryRun(() => { SalesCartController cartmgr = new SalesCartController(); SalesReceipt receipt = cartmgr.PlaceOrder(CurrentEmployeeID, couponCode, paymentMethod); // clear the coupon code and payment method for the next customer CartSummary_Coupon.Text = ""; PaymentTypeDDL.SelectedIndex = 0; // switch the display to sales receipt mode and display receipt ShowReceipt(receipt); }, "Success", "Order registered. Use the top menu bar to start a new sale."); }
protected void btnDeleteCartItem_Click(object sender, EventArgs e) { // delete selected product from cart Control item = ControlHelper.GetParent(sender, 2); string productName = ControlHelper.GetControlValue(item, "ProductDescriptionLabel"); int productID = ControlHelper.GetControlValue(item, "ProductIDLabel", -1); // generate our messages string successMessage = string.Format("<strong>{0}</strong> removed from cart.", productName); string failureMessage = string.Format("Cannot remove propduct <strong>{0}</strong> (ID={1}) from cart.", productName, productID); MessageUserControl.TryRun(() => { // check if user is logged in if (!IsSalesUser || CurrentEmployee == null) { throw new BusinessRuleException(failureMessage, "User must be employee, logged in and have Sales role to add products to cart."); } // check product id else if (productID <= 0) { throw new BusinessRuleException(failureMessage, "Product ID must be positive integer."); } // yay, we can try and remove the product from the cart else { // All validation passed, we can safely call our BLL. // Now, our BLL cannot access the AppSecurity assembly, so we needed to get the Employee ID and // pass that to our BLL instead of the username: SalesCartController cartmgr = new SalesCartController(); cartmgr.DeleteCartItem(CurrentEmployeeID, productID); // refresh cart summary // CartSummaryView.DataBind(); RefreshCartSummary(); } }, "Success", successMessage); }
protected void CancelButton_Click(object sender, EventArgs e) { MessageUserControl.TryRun(() => { if (!IsSalesUser || CurrentEmployee == null) { throw new BusinessRuleException("Cannot delete cart", "User must be employee, logged in and have Sales role to add products to cart."); } else { // delete cart and cart items SalesCartController cartmgr = new SalesCartController(); cartmgr.CancelCart(CurrentEmployeeID); // clear previous coupon code CartSummary_Coupon.Text = ""; // refresh cart RefreshCartSummary(); } SalesTabView.SetActiveView(TabShopping); }, "Cart deleted", "All items deleted from the current shopping cart."); }
protected void RefreshCartSummary() { // check if the user is logged in, has sales role and is an employee and set visibility accordingly if (CurrentEmployee == null) { // not logged in or not employee or has no sales role CartSummaryPanelNotLoggedIn.Visible = true; CartSummaryPanel.Visible = false; // disable checkout button in navigation setActiveTab(false); } else { // logged in and employee with sales role CartSummaryPanelNotLoggedIn.Visible = false; CartSummaryPanel.Visible = true; // set salesperson name CartSummary_EmployeeName.Text = CurrentEmployeeName; CartSummary_EmployeeID.Text = CurrentEmployeeID.ToString(); // get cart summary for current employee given the coupon code var cartmgr = new SalesCartController(); CartSummaryInfo summary = cartmgr.ShoppingCart_GetSummary(CurrentEmployeeID, CartSummary_Coupon.Text); // if the cart is empty, display a message, otherwise display the cart summary if (summary == null) { // cart is empty CartSummaryPanelEmpty.Visible = true; CartSummaryPanelNotEmpty.Visible = false; // disable checkout button in navigation setActiveTab(false); } else { // cart is not empty, populate fields CartSummaryPanelEmpty.Visible = false; CartSummaryPanelNotEmpty.Visible = true; // enable checkout button in navigation setActiveTab(true); // populate cart summary fields CartSummary_ProductCount.Text = string.Format("{0}", summary.ProductCount); CartSummary_ItemCount.Text = string.Format("{0}", summary.ItemCount); CartSummary_SubtotalAmount.Text = string.Format("{0:C2}", summary.Subtotal); CartSummary_DiscountAmount.Text = string.Format("{0:C2}", summary.Discount); CartSummary_TaxAmount.Text = string.Format("{0:C2}", summary.GST); CartSummary_TotalAmount.Text = string.Format("{0:C2}", summary.Total); // check coupon and display discount percent if coupon was valid or error otherwise if (string.IsNullOrWhiteSpace(CartSummary_Coupon.Text)) { // coupon field empty, hide all messages CartSummary_DicountIcon_OK.Visible = false; CartSummary_DiscontIcon_Error.Visible = false; CartSummary_DiscountError.Text = ""; CartSummary_DiscountPercent.Text = ""; } else if (summary.DiscountCoupon == null) { // coupon not found CartSummary_DicountIcon_OK.Visible = false; CartSummary_DiscontIcon_Error.Visible = true; CartSummary_DiscountError.Text = "Invalid"; CartSummary_DiscountPercent.Text = ""; } else if (!summary.DiscountCoupon.IsValid) { // coupon not valid because of date range CartSummary_DicountIcon_OK.Visible = false; CartSummary_DiscontIcon_Error.Visible = true; CartSummary_DiscountError.Text = "Expired"; CartSummary_DiscountPercent.Text = ""; } else { // coupon valid, dispay percentage CartSummary_DicountIcon_OK.Visible = true; CartSummary_DiscontIcon_Error.Visible = false; CartSummary_DiscountError.Text = ""; CartSummary_DiscountPercent.Text = string.Format("{0} %", summary.DiscountCoupon.DiscountPercent); } } } // refresh cart items CartItemsView.DataBind(); }