예제 #1
0
파일: ROS_Main.cs 프로젝트: ainzbedo1/SAD-2
        private void createButton_Click(object sender, EventArgs e)
        {
            if (orderGridView.Rows.Count <= 0)
            {
                MessageBox.Show("There are no orders to be made.");
            }
            else
            {
                try
                {
                    DBConnect        db   = new DBConnect();
                    MySqlConnection  con  = db.connect();
                    MySqlCommand     comm = new MySqlCommand("SELECT id, reportDate FROM dailysalesreport WHERE reportDate = CURDATE()", con);
                    MySqlDataAdapter adp  = new MySqlDataAdapter(comm);
                    DataTable        dt   = new DataTable();
                    adp.Fill(dt);

                    //CREATES SALES REPORT FOR THE DAY IF DOES NOT EXISTS YET
                    if (dt.Rows.Count == 0)
                    {
                        con.Open();
                        MessageBox.Show("Sales Report for the day has been created!");
                        MySqlCommand comm2 = new MySqlCommand("INSERT INTO dailysalesreport (reportDate) VALUES (NOW())", con);
                        comm2.ExecuteNonQuery();
                        adp.Fill(dt);
                        con.Close();
                        DSR_ID = Int32.Parse(dt.Rows[0]["id"].ToString());
                    }
                    else
                    {
                        //GRABS THE ID OF CURRENT DSR
                        DSR_ID = Int32.Parse(dt.Rows[0]["id"].ToString());
                    }

                    //INSERTS ORDER RECEIPTS INTO CURRENT DSR
                    con.Open();
                    MySqlCommand comm3 = new MySqlCommand("INSERT INTO order_receipt (dailysalesreport_id, orderTime, totalPrice) " +
                                                          "VALUES (" + DSR_ID + ", CURRENT_TIME()," + totalPrice + ")", con);
                    comm3.ExecuteNonQuery();

                    //SELECTS BOTH THE ID'S OF ORDER_RECEIPTS AND MENUITEM
                    comm = new MySqlCommand("SELECT id FROM order_receipt ORDER BY id DESC", con);
                    adp  = new MySqlDataAdapter(comm);
                    DataTable dt2 = new DataTable();
                    adp.Fill(dt2);

                    //GRABS THE ID'S OF CURRENT ORDER RECEIPT AND MENUITEMS
                    int orderID = Int32.Parse(dt2.Rows[0]["id"].ToString());

                    MySqlCommand command1;
                    MySqlCommand comm4;
                    int          menuitemID;

                    //ADDS QUANTITY TO MENUITEM
                    foreach (DataGridViewRow row in orderGridView.Rows)
                    {
                        command1 = new MySqlCommand("SELECT id FROM menuitem WHERE name = '" + row.Cells[0].Value.ToString() + "'", con);
                        adp      = new MySqlDataAdapter(command1);
                        DataTable dt3 = new DataTable();
                        adp.Fill(dt3);
                        menuitemID = Int32.Parse(dt3.Rows[0]["id"].ToString());
                        comm4      = new MySqlCommand("INSERT INTO order_menuitem (order_id, menuitem_id, quantity) " +
                                                      "VALUES (" + orderID + "," + menuitemID + "," + Int32.Parse(row.Cells[2].Value.ToString()) + ")", con);
                        comm4.ExecuteNonQuery();
                    }

                    //UPDATES DSR REVENUE
                    String updateRevenue = ("UPDATE dailysalesreport SET revenue = revenue + " + (totalPrice - totalCostPrice) + " WHERE reportDate = CURDATE()");

                    MySqlCommand updateRev = new MySqlCommand(updateRevenue, con);
                    updateRev.ExecuteNonQuery();

                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                MessageBox.Show("Transaction has been recorded!");
                orderGridView.Rows.Clear();
                totalPrice       = 0.00f;
                totalCostPrice   = 0.00f;
                amountLabel.Text = "P" + totalPrice.ToString("0.00");
            }
        }