示例#1
0
        private void btnCheckout_Click(object sender, EventArgs e)
        {
            if (listOfPurchase.Count == 0)
            {
                MessageBox.Show("Cart is empty!");
            }
            else
            {
                string productname   = dataCart.CurrentRow.Cells[0].Value.ToString();
                int    id            = Int32.Parse(getLastRequestID().Substring(2)) + 1;
                String rqid          = "RQ" + id.ToString().PadLeft(5, '0');
                string paymentmethod = "";
                string creditcard    = "";

                if (rbCash.Checked == true)
                {
                    paymentmethod = "Cash";
                    creditcard    = "";
                }
                else
                {
                    paymentmethod = "Credit Card";
                    creditcard    = txtCardNumber.Text;
                }
                if (txtCustUsername.Text == "")
                {
                    MessageBox.Show("Please enter customer username");
                }
                else
                {
                    var usrnm = getCustID(txtCustUsername.Text);

                    var qry = from c in ent.Customers
                              where c.Customer_ID.Equals(usrnm)
                              select c;
                    if (qry.ToList().Count == 0)
                    {
                        MessageBox.Show("Username not found!");
                    }

                    else
                    {
                        var datareq = new CustomerRequestHeader
                        {
                            Request_ID   = rqid,
                            Customer_ID  = usrnm, // cst00000 = Guest
                            Request_Date = DateTime.Now,
                            Payment_Type = paymentmethod,
                            cardnumber   = creditcard,
                            Staff_ID     = getStaffID(userid)
                        };
                        ent.CustomerRequestHeaders.Add(datareq);
                        ent.SaveChanges();

                        int    idtr = Int32.Parse(getLastTr().Substring(2)) + 1;
                        String trid = "TR" + idtr.ToString().PadLeft(6, '0');

                        for (int i = 0; i < listOfPurchase.Count; i++)
                        {
                            var data = new CustomerRequestDetail
                            {
                                Request_ID = rqid,
                                Req_Status = "Done",
                                Product_ID = getProductID(listOfPurchase[i]),
                                Quantity   = 0,
                                Price      = getPrice(listOfPurchase[i])
                            };
                            ent.CustomerRequestDetails.Add(data);
                            ent.SaveChanges();

                            var dt = new Transaction
                            {
                                Request_ID       = rqid,
                                Transaction_ID   = trid,
                                Product_ID       = getProductID(listOfPurchase[i]),
                                Quantity         = Convert.ToInt32(dataCart.CurrentRow.Cells[1].Value.ToString()),
                                Transaction_Date = DateTime.Now
                            };
                            ent.Transactions.Add(dt);
                            ent.SaveChanges();
                        }

                        refreshRequestData();

                        var query2 = (from c in ent.Products
                                      where c.Product_Title.Equals(productname)
                                      select c).First();

                        query2.Stock_Qty = getQuantityOfProductByName(dataCart.CurrentRow.Cells[0].Value.ToString()) - Convert.ToInt32(dataCart.CurrentRow.Cells[1].Value.ToString());
                        ent.SaveChanges();
                        refreshBookData();
                        listOfPurchase.Clear();
                        txtCustUsername.Text = "";
                        MessageBox.Show("Purchase done!");
                        dataCart.DataSource = null;
                        refreshRequestData();
                        refreshBookData();
                        rbCash.Checked       = false;
                        rbCreditCard.Checked = false;
                        btnCheckout.Enabled  = false;
                        refreshTrHeaderData();
                    }
                }
            }
        }
        private void btnCheckout_Click(object sender, EventArgs e)
        {
            int sum = 0;

            for (int i = 0; i < dataCart.Rows.Count; ++i)
            {
                sum += Convert.ToInt32(dataCart.Rows[i].Cells[3].Value);
            }
            if (purchasecart.Count == 0)
            {
                MessageBox.Show("Your cart is empty!");
            }
            else
            {
                DialogResult dialogResult = MessageBox.Show("You will be charged the amount of " + sum, "Test", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    if (rbStoreCredit.Checked == true)
                    {
                        if (Convert.ToInt32(txtBalance.Text) > sum)
                        {
                            var data = new CustomerRequestHeader
                            {
                                Request_ID   = txtRequestID.Text,
                                Customer_ID  = userid,
                                Request_Date = DateTime.Now,
                                Payment_Type = "Store Credit",
                                cardnumber   = null
                            };
                            ent.CustomerRequestHeaders.Add(data);
                            ent.SaveChanges();

                            for (int i = 0; i < purchasecart.Count; i++)
                            {
                                Console.WriteLine(purchasecart[i]);
                                Console.WriteLine(getProductID(purchasecart[i]));

                                var detaildata = new CustomerRequestDetail
                                {
                                    Request_ID = txtRequestID.Text,
                                    Product_ID = getProductID(purchasecart[i]),
                                    Req_Status = "REQUESTED",
                                    Quantity   = Convert.ToInt32(dataCart.Rows[i].Cells[1].Value.ToString()),
                                    Price      = getPrice(purchasecart[i])
                                };
                                ent.CustomerRequestDetails.Add(detaildata);
                                ent.SaveChanges();
                            }
                            var query = (from c in ent.Customers
                                         where c.Customer_ID.Equals(userid)
                                         select c).First();

                            query.Balance = Convert.ToInt32(getCustomerBalance(userid)) - sum;
                            ent.SaveChanges();
                            MessageBox.Show("Your order will be processed soon. Thanks for your order!");
                            this.Hide();
                            Customer_MainMenu menu = new Customer_MainMenu(userid);
                            menu.Show();
                        }
                        else
                        {
                            MessageBox.Show("Insufficient balance!");
                        }
                    }
                    else
                    {
                        var data = new CustomerRequestHeader
                        {
                            Request_ID   = txtRequestID.Text,
                            Customer_ID  = userid,
                            Request_Date = DateTime.Now,
                            Payment_Type = "Credit Card",
                            cardnumber   = txtCardNumber.Text
                        };
                        ent.CustomerRequestHeaders.Add(data);
                        ent.SaveChanges();

                        for (int i = 0; i < purchasecart.Count; i++)
                        {
                            Console.WriteLine(purchasecart[i]);
                            Console.WriteLine(getProductID(purchasecart[i]));

                            var detaildata = new CustomerRequestDetail
                            {
                                Request_ID = txtRequestID.Text,
                                Product_ID = getProductID(purchasecart[i]),
                                Req_Status = "REQUESTED",
                                Quantity   = Convert.ToInt32(dataCart.Rows[i].Cells[1].Value.ToString()),
                                Price      = getPrice(purchasecart[i])
                            };
                            ent.CustomerRequestDetails.Add(detaildata);
                            ent.SaveChanges();
                        }
                        MessageBox.Show("Your order will be processed soon. Thanks for your order!");
                        this.Hide();
                        Customer_MainMenu menu = new Customer_MainMenu(userid);
                        menu.Show();
                    }
                }
                else if (dialogResult == DialogResult.No)
                {
                }
            }
        }