Ejemplo n.º 1
0
        private void CancelOrderButton_Click(object sender, EventArgs e)
        {
            NewOrderButton.Enabled     = true;
            PrintOrderButton.Enabled   = false;
            PrintPreviewButton.Enabled = false;
            CancelOrderButton.Enabled  = false;
            ItemGroupBox.Enabled       = false;

            ClientNameTextBox.Clear();
            ItemNameComboBox.SelectedIndex = -1;
            QuantityTextBox.Clear();
            UnitPriceTextBox.Clear();
            CurrentStockTextBox.Clear();
            TotalAmountTextBox.Text = "0";
            SalesTaxTextBox.Text    = "0";
            TotalToPayTextBox.Text  = "0";

            CartDataGridView.DataSource = null;
            shoppingCart.Clear();
        }
Ejemplo n.º 2
0
        private void AddToCartButton_Click(object sender, EventArgs e)
        {
            if (IsValidated())
            {
                if (Convert.ToInt16(QuantityTextBox.Text.Trim()) > Convert.ToInt16(CurrentStockTextBox.Text))
                {
                    MessageBox.Show("Quantity can't be greater than current stock!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                bool hasSameProductSelectedAgain = false;


                //If Product Is Reselected and Add it's value so update it's previous Value
                foreach (DataGridViewRow row in CartDataGridView.Rows)
                {
                    if (Convert.ToInt16(row.Cells["ItemId"].Value) == Convert.ToInt16(ItemNameComboBox.SelectedValue))
                    {
                        row.Cells["Quantity"].Value   = Convert.ToInt16(row.Cells["Quantity"].Value) + Convert.ToInt16(QuantityTextBox.Text);
                        row.Cells["TotalPrice"].Value = Convert.ToInt16(row.Cells["Quantity"].Value) * Convert.ToDecimal(row.Cells["UnitPrice"].Value);
                        hasSameProductSelectedAgain   = true;
                    }
                }

                if (hasSameProductSelectedAgain == false)
                {
                    CartItem item = new CartItem()
                    {
                        ItemId     = Convert.ToInt16(ItemNameComboBox.SelectedValue),
                        ItemName   = ItemNameComboBox.Text,
                        Quantity   = Convert.ToInt16(QuantityTextBox.Text.Trim()),
                        UnitPrice  = Convert.ToDecimal(UnitPriceTextBox.Text.Trim()),
                        TotalPrice = Convert.ToInt16(QuantityTextBox.Text.Trim()) * Convert.ToDecimal(UnitPriceTextBox.Text.Trim())
                    };

                    // Array, Collection, List, DataTable and DataSet
                    shoppingCart.Add(item);

                    CartDataGridView.DataSource = null;
                    CartDataGridView.DataSource = shoppingCart;
                }

                decimal totalAmount = shoppingCart.Sum(x => x.TotalPrice);
                TotalAmountTextBox.Text = totalAmount.ToString();

                decimal totalSalesTax = (16 * totalAmount) / 100;
                SalesTaxTextBox.Text = totalSalesTax.ToString();

                decimal totalPay = totalAmount + totalSalesTax;
                TotalToPayTextBox.Text = totalPay.ToString();

                //update stock in MySQL
                int productId = Convert.ToInt16(ItemNameComboBox.SelectedValue);
                int quantity  = Convert.ToInt16(QuantityTextBox.Text);
                UpdateStockLevel(productId, quantity);


                ItemNameComboBox.SelectedIndex = -1;
                QuantityTextBox.Clear();
                UnitPriceTextBox.Clear();
                CurrentStockTextBox.Clear();
            }
        }