public SalesReceipt PlaceOrder(int employeeId, string couponCode, string paymentMethod) { using (var context = new eToolsContext()) { Employee employee = context.Employees.Find(employeeId); CartSummaryInfo cartSummary = ShoppingCart_GetSummary(employeeId, couponCode); List <CartItemInfo> cartItems = ShoppingCart_GetItems(employeeId); Dictionary <string, string> validPaymentMethods = new Dictionary <string, string> { { "M", "Money" }, { "C", "Credit" }, { "D", "Debit" } }; List <string> errors = new List <string>(); if (employee == null) { errors.Add("Employee not found."); } else if (cartSummary == null) { errors.Add("Employee has no shopping cart."); } else if (cartItems == null || cartItems.Count == 0) { errors.Add("Shopping cart is empty."); } else if (!string.IsNullOrWhiteSpace(couponCode) && cartSummary.DiscountCoupon == null) { errors.Add("Coupon code invalid."); } else if (cartSummary.DiscountCoupon != null && !cartSummary.DiscountCoupon.IsValid) { errors.Add("Coupon expired."); } if (string.IsNullOrWhiteSpace(paymentMethod)) { errors.Add("Payment method must not be empty."); } else if (!validPaymentMethods.ContainsKey(paymentMethod)) { errors.Add("Payment method invalid."); } // validate each item in the cart foreach (var item in cartItems) { StockItem product = context.StockItems.Find(item.ProductID); List <string> itemErrors = ValidateCartItem(employee, product, item.QtyInCart); errors.AddRange(itemErrors); } if (errors.Count > 0) { throw new BusinessRuleException("Order cannot be placed.", errors); } else { // all validation passed, build sales receipt var receipt = new SalesReceipt() { SaleDate = DateTime.Now, PaymentType = paymentMethod, PaymentTypeDesc = validPaymentMethods[paymentMethod], EmployeeId = employeeId, EmployeeName = employee.LastName + ", " + employee.FirstName, SubTotal = cartSummary.Subtotal, CouponId = cartSummary.DiscountCoupon?.CouponID, CouponCode = couponCode, DiscountPercent = (cartSummary.DiscountCoupon == null) ? 0 : cartSummary.DiscountCoupon.DiscountPercent, DiscountAmount = cartSummary.Discount, TaxAmount = cartSummary.GST, TotalAmount = cartSummary.Total, Items = cartItems.Select(item => new SalesReceiptItem { ProductId = item.ProductID, ProductName = item.ProductDescription, SellingPrice = item.SellingPrice, Qty = item.QtyInCart, SubTotal = item.TotalPrice }).ToList() }; // store new sale Sale newSale = context.Sales.Add(new Sale() { SaleDate = receipt.SaleDate, PaymentType = receipt.PaymentType, EmployeeID = receipt.EmployeeId, TaxAmount = receipt.TaxAmount, SubTotal = receipt.SubTotal, CouponID = receipt.CouponId }); if (newSale == null) { throw new Exception("Sale cannot be added to the database."); } // process each item: insert into the database and decrease qty on hand foreach (var item in receipt.Items) { SaleDetail newSaleDetail = context.SaleDetails.Add(new SaleDetail() { SaleID = newSale.SaleID, StockItemID = item.ProductId, SellingPrice = item.SellingPrice, Quantity = item.Qty, Backordered = false, ShippedDate = null }); if (newSaleDetail == null) { throw new Exception("Sale detail cannot be added to the database."); } // decrease stock StockItem currentProduct = context.StockItems.Find(item.ProductId); currentProduct.QuantityOnHand -= item.Qty; context.Entry(currentProduct).Property(x => x.QuantityOnHand).IsModified = true; if (currentProduct.QuantityOnHand < 0) { throw new Exception("Product out of stock."); } } // all good, delete cart context.ShoppingCartItems.RemoveRange( context.ShoppingCartItems.Where(x => x.ShoppingCart.EmployeeID == employeeId)); // delete shopping cart (I used RemoveRange just to make it simpler) context.ShoppingCarts.RemoveRange( context.ShoppingCarts.Where(x => x.EmployeeID == employeeId)); // commit all changes context.SaveChanges(); // retrieve receipt ID receipt.SaleID = newSale.SaleID; return(receipt); } } }
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(); }