/// <summary> /// Creator: Jaeho Kim /// Created: 03/25/2020 /// Approver: Rasha Mohammed /// /// Adds a product to the shopping cart. /// </summary> /// <remarks> /// Updater: Robert Holmes /// Updated: 5/5/2020 /// Update: try/catch around dangerous code. /// </remarks> /// <param name="e"></param> /// <param name="sender"></param> private void btnAddProduct_Click(object sender, RoutedEventArgs e) { var salesTax = new SalesTax(); try { // Populates the salesTax data transfer object by zipcode. salesTax.ZipCode = txtZipCode.Text.ToString(); salesTax.TaxDate = _transactionManager .RetrieveLatestSalesTaxDateByZipCode(txtZipCode.Text.ToString()); salesTax.TaxRate = _transactionManager .RetrieveTaxRateBySalesTaxDateAndZipCode(txtZipCode.Text.ToString(), salesTax.TaxDate); // SalesTax details operation now complete. } catch (Exception) { salesTax.TaxRate = 0M; } taxRate = salesTax.TaxRate; bool isValid = false; // The logic verifies that the product searched actually exists and is valid. try { // Populates the productVM data transfer object using the product UPC number that was searched. ProductVM productVM = new ProductVM() { ProductID = txtSearchProduct.Text.ToString(), Name = txtItemName.Text.ToString(), Taxable = chkTaxable.IsChecked == true, Price = decimal.Parse(txtPrice.Text), Quantity = int.Parse(txtQuantity.Text), ItemDescription = txtItemDescription.Text.ToString(), ItemQuantity = quantityInStock, Active = true }; isValid = _transactionManager.isItemQuantityValid(_transactionManager.GetAllProducts(), productVM); if (isValid) { _transactionManager.AddProduct(productVM); // If the product is taxable, add it to the taxable product list. if (productVM.Taxable) { _transactionManager.AddProductTaxable(productVM); } clearFields(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } if (isValid == false) { // Let the user know that the item quantity did not get entered. MessageBox.Show("Invalid Item Quantity"); } // Displays all the products on the data grid. dgShoppingCart.ItemsSource = _transactionManager.GetAllProducts(); // CalculateSubTotal, takes the master list of products. // The reason for storing the variable is the value obtained from the CalculateSubTotal // is going to be passed to calculate the total. subTotal = _transactionManager.CalculateSubTotal(_transactionManager.GetAllProducts()); txtSubTotal.Text = subTotal.ToString(); // CalculateSubTotalTaxable, takes the taxable list of products. // The reason for storing the variable is the value obtained from the CalculateSubTotal // is going to be passed to calculate the tax. subTotalTaxable = _transactionManager.CalculateSubTotalTaxable(_transactionManager.GetTaxableProducts()); txtSubTotalTaxable.Text = subTotalTaxable.ToString(); // tax exempt simply means the tax rate is zero. Zero (tax rate) // multiply with sub total taxable is zero. Zero add sub total // is simply the total without tax. if (!String.IsNullOrWhiteSpace(txtTaxExemptNumber.Text)) { salesTax.TaxRate = 0; } // Calculates the total. total = _transactionManager.CalculateTotal(subTotal, subTotalTaxable, salesTax); txtTotal.Text = total.ToString(); }