Beispiel #1
0
        private void btnCaptureSale_Click(object sender, EventArgs e)
        {
            try
            {
                //Place Order
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    SqlConnection sqlcon = new SqlConnection(Globals_Class.ConnectionString);
                    sqlcon.Open();
                    string     Command = "INSERT INTO Sales(ItemName, ItemQuantity, LinePrice) VALUES(@ItemName, @ItemQuantity, @LinePrice)";
                    SqlCommand sqlcom  = new SqlCommand(Command, sqlcon);
                    sqlcom.Parameters.AddWithValue("@ItemName", row.Cells["colItemName"].Value);
                    sqlcom.Parameters.AddWithValue("@ItemQuantity", row.Cells["colItemQuantity"].Value);
                    sqlcom.Parameters.AddWithValue("@LinePrice", row.Cells["colLinePrice"].Value);
                    sqlcom.ExecuteNonQuery();
                    sqlcon.Close();


                    //Update Stock Quantity
                    int QinStock;
                    int QPurchased;
                    int QLeft;
                    int ProductID;

                    SqlConnection sqlcon1 = new SqlConnection(Globals_Class.ConnectionString);
                    sqlcon1.Open();
                    string        SelectQinStock = "SELECT ProductQuantityInStock, ProductID FROM Products WHERE ProductName ='" + row.Cells["colItemName"].Value.ToString() + "'";
                    SqlCommand    sqlcom2        = new SqlCommand(SelectQinStock, sqlcon1);
                    SqlDataReader dr;
                    dr = sqlcom2.ExecuteReader();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            QinStock   = Convert.ToInt32((dr["ProductQuantityInStock"]));
                            QPurchased = Convert.ToInt32(row.Cells["colItemQuantity"].Value);
                            QLeft      = QinStock - QPurchased;
                            ProductID  = Convert.ToInt32((dr["ProductID"]));

                            SqlConnection sqlcon3 = new SqlConnection(Globals_Class.ConnectionString);
                            sqlcon3.Open();
                            string     cmd     = "UPDATE Products SET ProductQuantityInStock ='" + QLeft.ToString() + "' WHERE ProductID ='" + ProductID.ToString() + "'";
                            SqlCommand sqlcom3 = new SqlCommand(cmd, sqlcon3);
                            sqlcom3.ExecuteNonQuery();
                            sqlcon3.Close();
                        }
                    }

                    //Add Loyalty Points
                    //Get Loyalty Membership
                    try
                    {
                        SqlConnection sqlcon5 = new SqlConnection(Globals_Class.ConnectionString);
                        sqlcon5.Open();
                        string        GetMembership = "SELECT isMember From Customers Where CustomerFullName ='" + SelectedCustomer.ToString() + "'";
                        SqlCommand    sqlcom5       = new SqlCommand(GetMembership, sqlcon5);
                        SqlDataReader dr5;
                        dr5 = sqlcom5.ExecuteReader();
                        if (dr5.HasRows)
                        {
                            while (dr5.Read())
                            {
                                isMember = Convert.ToInt32((dr5["isMember"]));
                            }
                        }
                        dr5.Close();
                        sqlcon5.Close();
                    }
                    catch
                    {
                        isMember = 0;
                    }
                    //Get Current Points
                    try
                    {
                        SqlConnection sqlcon9 = new SqlConnection(Globals_Class.ConnectionString);
                        sqlcon9.Open();
                        string        GetPoints = "SELECT LoyaltyPointsAvailable FROM Customers WHERE CustomerFullName ='" + SelectedCustomer.ToString() + "'";
                        SqlCommand    sqlcom9   = new SqlCommand(GetPoints, sqlcon9);
                        SqlDataReader dr9;
                        dr9 = sqlcom9.ExecuteReader();
                        if (dr9.HasRows)
                        {
                            while (dr9.Read())
                            {
                                CurrentPoints = Convert.ToDecimal((dr9["LoyaltyPointsAvailable"]));
                            }
                        }
                        dr9.Close();
                        sqlcon9.Close();
                    }
                    catch
                    {
                    }


                    if (isMember == 1)
                    {
                        decimal PointsReceived;
                        decimal FinalPoints;
                        PointsReceived = Convert.ToDecimal(row.Cells["colLinePrice"].Value) * Convert.ToDecimal(Globals_Class.loyaltyPointsPercentage);
                        FinalPoints    = CurrentPoints + PointsReceived;

                        //Add Points
                        SqlConnection sqlcon66 = new SqlConnection(Globals_Class.ConnectionString);
                        sqlcon66.Open();
                        string     UpdatePoints = "UPDATE Customers SET LoyaltyPointsAvailable ='" + FinalPoints.ToString() + "' WHERE CustomerFullName ='" + SelectedCustomer.ToString() + "'";
                        SqlCommand sqlcom66     = new SqlCommand(UpdatePoints, sqlcon66);
                        sqlcom66.ExecuteNonQuery();
                        sqlcon66.Close();
                    }
                    else
                    {
                    }
                }

                //Calculate Sale Total
                decimal sum = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; ++i)
                {
                    sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
                }



                MetroFramework.MetroMessageBox.Show(this, "The New Sale was Captured Successfully! The Total Cost of this Sale is:" + "\n" + "R" + " " + sum.ToString() + " ", "Message", MessageBoxButtons.OK, MessageBoxIcon.None);
                this.dataGridView1.Rows.Clear();


                cbxCustomerName.Text    = "";
                cbxCustomerName.Enabled = true;

                //Add To SalesMAde by Employee
                //Get Employee Instance
                SqlConnection sqlcons = new SqlConnection(Globals_Class.ConnectionString);
                sqlcons.Open();
                string        cmds    = "SELECT EmployeeName From Employees WHERE UserName ='******'";
                SqlCommand    sqlcoms = new SqlCommand(cmds, sqlcons);
                SqlDataReader rs;
                rs = sqlcoms.ExecuteReader();
                if (rs.HasRows)
                {
                    while (rs.Read())
                    {
                        EmployeeName = (rs["EmployeeName"].ToString());
                    }
                }
                rs.Close();
                sqlcons.Close();

                //Get Current Amount of Sales
                SqlConnection sql = new SqlConnection(Globals_Class.ConnectionString);
                sql.Open();
                string        sqlcmd    = "SELECT TotalSalesMade FROM SalesMade WHERE EmployeeName ='" + EmployeeName.ToString() + "'";
                SqlCommand    sqlcomss  = new SqlCommand(sqlcmd, sql);
                SqlDataReader sqlreader = sqlcomss.ExecuteReader();
                if (sqlreader.HasRows)
                {
                    while (sqlreader.Read())
                    {
                        SalesMadeSoFar = Convert.ToInt32((sqlreader["TotalSalesMade"]));
                    }
                }
                sqlreader.Close();
                sql.Close();
                int TotalSalesMadeSoFar = SalesMadeSoFar + MadeSale;
                //Update TotalAmount of Sales Made
                SqlConnection sqlz = new SqlConnection(Globals_Class.ConnectionString);
                sqlz.Open();
                string     cmdz    = "UPDATE SalesMade SET TotalSalesMade = @TotalAmountofSalesMade WHERE EmployeeName ='" + EmployeeName.ToString() + "'";
                SqlCommand sqlcomz = new SqlCommand(cmdz, sqlz);
                sqlcomz.Parameters.Add(new SqlParameter("@TotalAmountofSalesMade", TotalSalesMadeSoFar));
                sqlcomz.ExecuteNonQuery();
                sqlz.Close();
            }
            catch
            {
                MetroFramework.MetroMessageBox.Show(this, "An Error Occurred Whilst Capturing your Sale Information!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.dataGridView1.Rows.Clear();
                cbxCustomerName.Enabled = true;
            }
        }
Beispiel #2
0
        private void metroButton1_Click(object sender, EventArgs e)
        {
            try
            {
                decimal Cost = Convert.ToDecimal(txtCost.Text);
                datetime        = monthCalendar1.SelectionStart.Date;
                SupplierOrderID = Convert.ToInt32(lbxSupplierOrders.Text);
                int    newReceivedStatus = 1;
                string SupplierName      = lblOrderedFrom.Text;

                SqlConnection sqlcon = new SqlConnection(Globals_Class.ConnectionString);
                sqlcon.Open();
                string     cmd    = "INSERT INTO ReceiveSupplierOrder(SupplierOrderID, SuppleirID, ProductName, ProductQuantity, ReceivedDate, OrderCost, ReceivedStatus, SupplierName) VALUES(@SupplierOrderID, @SupplierID, @ProductName, @ProductQuantity, @ReceivedDate, @OrderCost, @ReceivedStatus, @SupplierName)";
                SqlCommand sqlcom = new SqlCommand(cmd, sqlcon);
                sqlcom.Parameters.Add(new SqlParameter("@SupplierOrderID", SupplierOrderID));
                sqlcom.Parameters.Add(new SqlParameter("@SupplierID", SupplierID));
                sqlcom.Parameters.Add(new SqlParameter("@ProductName", ProductName));
                sqlcom.Parameters.Add(new SqlParameter("@ProductQuantity", ProductQuantity));
                sqlcom.Parameters.Add(new SqlParameter("@ReceivedDate", datetime));
                sqlcom.Parameters.Add(new SqlParameter("@OrderCost", Cost));
                sqlcom.Parameters.Add(new SqlParameter("@ReceivedStatus", newReceivedStatus));
                sqlcom.Parameters.Add(new SqlParameter("@SupplierName", SupplierName));
                sqlcom.ExecuteNonQuery();

                MetroFramework.MetroMessageBox.Show(this, "The Order has been Received Successfully!", "Message", MessageBoxButtons.OK, MessageBoxIcon.None);

                //Update Quantity in Stock
                //Update Stock Quantity
                int QinStock;
                int QReceived;
                int QLeft;
                int ProductID;

                SqlConnection sqlcon1 = new SqlConnection(Globals_Class.ConnectionString);
                sqlcon1.Open();
                string        SelectQinStock = "SELECT ProductQuantityInStock, ProductID FROM Products WHERE ProductName ='" + lblProductOrdered.Text + "'";
                SqlCommand    sqlcom2        = new SqlCommand(SelectQinStock, sqlcon1);
                SqlDataReader dr;
                dr = sqlcom2.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        QinStock  = Convert.ToInt32((dr["ProductQuantityInStock"]));
                        QReceived = Convert.ToInt32(lblQuantityOrdered.Text);
                        QLeft     = QinStock + QReceived;
                        ProductID = Convert.ToInt32((dr["ProductID"]));
                        //   try
                        //    {
                        SqlConnection sqlcon3 = new SqlConnection(Globals_Class.ConnectionString);
                        sqlcon3.Open();
                        string     cmd2    = "UPDATE Products SET ProductQuantityInStock ='" + QLeft.ToString() + "' WHERE ProductID ='" + ProductID.ToString() + "'";
                        SqlCommand sqlcom3 = new SqlCommand(cmd2, sqlcon3);
                        sqlcom3.ExecuteNonQuery();
                        sqlcon3.Close();
                        //  }
                        // catch
                        //  {
                        //      MetroFramework.MetroMessageBox.Show(this, "An Error Occurred Whilst Updating your Stock Information!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        // }
                    }
                }
                sqlcon1.Close();
            }
            catch
            {
                MetroFramework.MetroMessageBox.Show(this, "An Error Occured whilst Receiving the Order!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }