Beispiel #1
0
        /*! \fn void setPaymentForm(PaymentForm paymentForm)
         *  \brief A set Payment Form.
         *  \details It is used to make operation on PaymentForm.
         *  \param paymentForm it is an PaymentForm object.
         *  \return void
         */
        public void setPaymentForm(PaymentForm paymentForm)
        {
            customernamelbl.Text       = paymentForm.nametxtbox.Text;
            customeraddresslbl.Text    = paymentForm.customeraddresstxtbox.Text;
            customertotalpricelbl.Text = paymentForm.customerform.total_price_textbox.Text;
            //to assign clicked installment radiobutton
            RadioButton checkedButton = paymentForm.installment_groupbox.Controls.OfType <RadioButton>()
                                        .FirstOrDefault(r => r.Checked);

            if (checkedButton != null)
            {
                customerinstallmentlbl.Text = checkedButton.Text;
            }

            for (int i = 0; i < paymentForm.customerform.shopping_cart_list.Count; i++)
            {
                ItemToPurchase item = (ItemToPurchase)paymentForm.customerform.shopping_cart_list[i];
                if (item.product.getId() == LoginForm.current_customer_id)
                {
                    ListViewItem lv_item = new ListViewItem(item.product.getName());
                    lv_item.SubItems.Add(item.quantity.ToString());
                    lv_item.SubItems.Add(item.product.getPrice().ToString());
                    lv_item.SubItems.Add(item.total_price.ToString());

                    productlistview.Items.Add(lv_item);
                }
            }
            paymentForm.Close();
        }
        /*! \fn void addProduct(ItemToPurchase item)
         *  \brief A void add product function.
         *  \details It is used to add product into shopping cart.
         *  \param item it is ItemToPurchase object.
         *  \return void
         */
        public void addProduct(ItemToPurchase item)
        {
            bool     check    = true;
            Database database = Database.get_instance();

            //to check whether the product exists in the database or not
            for (int i = 0; i < itemsToPurchase_list.Count; i++)
            {
                ItemToPurchase item_obj = (ItemToPurchase)itemsToPurchase_list[i];

                if (item_obj.product.getId() == customerID && item_obj.product.getName().Equals(item.product.getName()))
                {
                    item_obj.quantity++;
                    itemsToPurchase_list.RemoveAt(i);
                    itemsToPurchase_list.Insert(i, item_obj);
                    using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + Application.StartupPath + "\\BookStore.db;Version=3"))
                    {
                        connection.Open();
                        using (SQLiteCommand command = new SQLiteCommand(connection))
                        {
                            command.CommandText =
                                "update ShoppingCartTable set Amount = :amount,TotalPrice = :totalprice where Id = :id AND Product= :product";
                            command.Parameters.Add("amount", DbType.Int32).Value      = item_obj.quantity;
                            command.Parameters.Add("totalprice", DbType.Double).Value = item_obj.quantity * item_obj.product.getPrice();
                            command.Parameters.Add("id", DbType.Int32).Value          = customerID;
                            command.Parameters.Add("product", DbType.String).Value    = item.product.getName();

                            command.ExecuteNonQuery();
                        }

                        connection.Close();
                    }
                    check = false;
                    break;
                }
            }

            if (check)
            {
                //if the product does not exist in the database,it is added to the database.
                itemsToPurchase_list.Add(item);
                using (SQLiteConnection sql_con = new SQLiteConnection("Data Source=" + Application.StartupPath + "\\BookStore.db;Version=3"))
                {
                    SQLiteCommand sql_command = new SQLiteCommand();
                    sql_command.CommandText = "Insert Into ShoppingCartTable(Id,Product,Amount,UnitPrice,TotalPrice,Image) Values(@Id,@Product,@Amount,@UnitPrice,@TotalPrice,@Image)";
                    sql_command.Connection  = sql_con;

                    sql_command.Parameters.AddWithValue("@Id", customerID);
                    sql_command.Parameters.AddWithValue("@Product", item.product.getName());
                    sql_command.Parameters.AddWithValue("@Amount", item.quantity);
                    sql_command.Parameters.AddWithValue("@UnitPrice", item.product.getPrice());
                    sql_command.Parameters.AddWithValue("@TotalPrice", item.product.getPrice() * item.quantity);
                    sql_command.Parameters.AddWithValue("@Image", item.product.ToString().Split('.')[1] + "," + item.product.getId().ToString());
                    sql_con.Open();
                    sql_command.ExecuteNonQuery();
                    sql_con.Close();
                }
            }
        }
        /*! void buy_button_Click(object sender, EventArgs e)
         *  \brief A click listener function.
         *  \details It is used to make a purchase.
         *  \param sender it is an object
         *  \param e it is an EventArgs
         *  \return void
         */
        private void buy_button_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(customeraddresstxtbox.Text) && cash_radiobutton.Checked)
            {
                MessageBox.Show("Please fill full in the fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (creditcard_radiobutton.Checked &&
                     (String.IsNullOrEmpty(nametxtbox.Text) || !expirydate_maskedTextBox.MaskFull ||
                      !cardnumber_maskedTextBox.MaskFull || !cvv_maskedTextBox.MaskFull))
            {
                MessageBox.Show("Please fill full in the fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                DialogResult confirm_question = MessageBox.Show("Would you like to buy it?",
                                                                "The Confirmation",
                                                                MessageBoxButtons.YesNoCancel,
                                                                MessageBoxIcon.Question);
                if (confirm_question == DialogResult.Yes)
                {
                    InvoiceForm invoice_form = new InvoiceForm();

                    invoice_form.setPaymentForm(this);
                    invoice_form.Show();


                    ShoppingCart shopping_cart = new ShoppingCart(LoginForm.current_customer_id, ref customerform.shopping_cart_list, 1);


                    for (int i = 0; i < customerform.shopping_cart_list.Count; i++)
                    {
                        ItemToPurchase item = (ItemToPurchase)customerform.shopping_cart_list[i];
                        if (item.product.getId() == LoginForm.current_customer_id)
                        {
                            shopping_cart.removeProduct(item);

                            // to send message to customer about change of stock by using Observer Pattern
                            item.product.Attach(customer);
                            item.product.ChangeStock();
                            i--;
                        }
                    }

                    customerform.total_quantity       = 0;
                    customerform.numberofproduct.Text = "0";
                    customerform.shopping_cart_list.Clear();

                    customerform.shoppingcart_datagridview.Rows.Clear();
                    customerform.shoppingcart_datagridview.Refresh();
                    customerform.total_price_textbox.Text = "0";
                }
            }
        }
        /*! \fn int getTotalProduct()
         *  \brief A int return total product.
         *  \details It is used to return total product in shopping cart.
         *  \return int
         */
        public int getTotalProduct()
        {
            int totalproduct = 0;

            for (int i = 0; i < itemsToPurchase_list.Count; i++)
            {
                ItemToPurchase item = (ItemToPurchase)itemsToPurchase_list[i];
                if (customerID == item.product.getId())
                {
                    totalproduct += item.quantity;
                }
            }

            return(totalproduct);
        }
        /*! \fn void removeProduct(ItemToPurchase item)
         *  \brief A void remove product function.
         *  \details It is used to remove product from shopping cart.
         *  \param item it is ItemToPurchase object.
         *  \return void
         */
        public void removeProduct(ItemToPurchase item)
        {
            Database database = Database.get_instance();

            using (SQLiteConnection sql_con = new SQLiteConnection("Data Source=" + Application.StartupPath + "\\BookStore.db;Version=3"))
            {
                SQLiteCommand sql_command = new SQLiteCommand();
                sql_command.CommandText = "Delete From ShoppingCartTable where Id = '" + customerID + "' AND Product = '" + item.product.getName() + "'";
                sql_command.Connection  = sql_con;
                sql_con.Open();
                sql_command.ExecuteNonQuery();
                sql_con.Close();
            }

            if (itemsToPurchase_list.Contains(item))
            {
                itemsToPurchase_list.Remove(item);
            }
        }
        /*! \fn void clean_unknownuser_shoppingcart()
         *  \brief A void function.
         *  \details It is used to clean shopping cart of unknown customer.
         *  \return void
         */
        private void clean_unknownuser_shoppingcart()
        {
            ths.read_shopping_cart();
            ShoppingCart shopping_cart = new ShoppingCart(-1, ref ths.shopping_cart_list, 1);

            for (int i = 0; i < ths.shopping_cart_list.Count; i++)
            {
                ItemToPurchase item = (ItemToPurchase)ths.shopping_cart_list[i];

                if (item.product.getId() == -1)
                {
                    shopping_cart.removeProduct(item);
                    i--;
                }
            }
            if (ths.shoppingcart_datagridview != null)
            {
                ths.shoppingcart_datagridview.ClearSelection();
                ths.shoppingcart_datagridview.Update();
                ths.shoppingcart_datagridview.Refresh();
            }

            this.Close();
        }