-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyticsFilter.cs
More file actions
244 lines (215 loc) · 9.81 KB
/
Copy pathAnalyticsFilter.cs
File metadata and controls
244 lines (215 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using H1;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace DB_Project
{
public partial class AnalyticsFilter : Form
{
// Update the connection string with your provided connection string
private string connectionString = @"Data Source=ABDULSABOOR190\SQLEXPRESS;Initial Catalog=sab;Integrated Security=True;Encrypt=False";
public AnalyticsFilter()
{
InitializeComponent();
LoadTourCategories();
}
private void LoadTourCategories()
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "SELECT name FROM TourCategory";
SqlCommand command = new SqlCommand(query, connection);
using (SqlDataReader reader = command.ExecuteReader())
{
comboBox1.Items.Clear();
while (reader.Read())
{
comboBox1.Items.Add(reader["name"].ToString());
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error loading tour categories: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
// You can add functionality here if needed
}
private void button1_Click(object sender, EventArgs e)
{
Form1 u = new Form1();
u.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Role_Permission role_Permission = new Role_Permission();
role_Permission.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(2);
form2.Show();
}
private void button6_Click(object sender, EventArgs e)
{
Review_Moderation_Form review_Moderation_Form = new Review_Moderation_Form();
review_Moderation_Form.Show();
}
private void button3_Click(object sender, EventArgs e)
{
StatusManagement statusManagement = new StatusManagement();
statusManagement.Show();
}
private void button5_Click(object sender, EventArgs e)
{
try
{
// Define the filter criteria
DateTime dateFrom = dateTimePicker1.Value;
DateTime dateTo = dateTimePicker2.Value;
string userType = comboBox2.SelectedItem?.ToString();
string tourCategory = comboBox1.SelectedItem?.ToString();
// Validate date range
if (dateTo < dateFrom)
{
MessageBox.Show("End date cannot be earlier than start date.", "Invalid Date Range", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// Format dates for SQL query
string formattedDateFrom = dateFrom.ToString("yyyy-MM-dd");
string formattedDateTo = dateTo.ToString("yyyy-MM-dd");
// Build the base query based on the database schema
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.Append(@"
SELECT
t.trip_id,
t.title AS TripTitle,
t.destination,
t.start_date,
t.end_date,
t.price,
tc.name AS CategoryName,
op.company_name AS OperatorName,
u.role AS UserRole,
COUNT(b.booking_id) AS BookingCount,
AVG(ISNULL(r.rating, 0)) AS AverageRating
FROM
Trip t
INNER JOIN TourOperatorProfile op ON t.operator_id = op.user_id
INNER JOIN AppUser u ON op.user_id = u.user_id
LEFT JOIN TripCategory tcat ON t.trip_id = tcat.trip_id
LEFT JOIN TourCategory tc ON tcat.category_id = tc.category_id
LEFT JOIN Booking b ON t.trip_id = b.trip_id
LEFT JOIN Review r ON b.booking_id = r.booking_id
WHERE
t.start_date >= @DateFrom AND t.end_date <= @DateTo
");
// Add filters based on selection
if (!string.IsNullOrEmpty(userType))
{
queryBuilder.Append(" AND u.role = @UserType");
}
if (!string.IsNullOrEmpty(tourCategory))
{
queryBuilder.Append(" AND tc.name = @TourCategory");
}
// Group by to avoid duplicate results
queryBuilder.Append(@"
GROUP BY
t.trip_id,
t.title,
t.destination,
t.start_date,
t.end_date,
t.price,
tc.name,
op.company_name,
u.role
ORDER BY t.start_date
");
// Execute the query
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(queryBuilder.ToString(), connection))
{
// Add parameters to prevent SQL injection
command.Parameters.AddWithValue("@DateFrom", formattedDateFrom);
command.Parameters.AddWithValue("@DateTo", formattedDateTo);
if (!string.IsNullOrEmpty(userType))
command.Parameters.AddWithValue("@UserType", userType);
if (!string.IsNullOrEmpty(tourCategory))
command.Parameters.AddWithValue("@TourCategory", tourCategory);
// Open connection
connection.Open();
// Create a DataTable to hold the results
DataTable dataTable = new DataTable();
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
adapter.Fill(dataTable);
}
// Show results in a DataGridView or format for a message box
if (dataTable.Rows.Count > 0)
{
// Create a formatted string for display in a message box
StringBuilder resultBuilder = new StringBuilder();
resultBuilder.AppendLine($"Filter Results ({dataTable.Rows.Count} trips found):\n");
foreach (DataRow row in dataTable.Rows)
{
resultBuilder.AppendLine($"Trip: {row["TripTitle"]}");
resultBuilder.AppendLine($"Destination: {row["destination"]}");
resultBuilder.AppendLine($"Date: {Convert.ToDateTime(row["start_date"]).ToShortDateString()} - {Convert.ToDateTime(row["end_date"]).ToShortDateString()}");
resultBuilder.AppendLine($"Category: {row["CategoryName"]}");
resultBuilder.AppendLine($"Operator: {row["OperatorName"]} ({row["UserRole"]})");
resultBuilder.AppendLine($"Price: ${row["price"]}");
resultBuilder.AppendLine($"Bookings: {row["BookingCount"]}");
resultBuilder.AppendLine($"Avg Rating: {Math.Round(Convert.ToDouble(row["AverageRating"]), 1)}/5.0");
resultBuilder.AppendLine("----------------------------------------");
}
// Display the results
MessageBox.Show(resultBuilder.ToString(), "Analytics Results", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("No trips match the selected filters.", "No Results", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error applying filters: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void AnalyticsFilter_Load(object sender, EventArgs e)
{
// Set default date range to current month
DateTime today = DateTime.Today;
dateTimePicker1.Value = new DateTime(today.Year, today.Month, 1);
dateTimePicker2.Value = today;
// Set default items for user type if needed
comboBox2.SelectedIndex = comboBox2.Items.Count > 0 ? 0 : -1;
}
private void button7_Click(object sender, EventArgs e)
{
// Reset button functionality
dateTimePicker1.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
dateTimePicker2.Value = DateTime.Today;
comboBox1.SelectedIndex = -1;
comboBox2.SelectedIndex = -1;
}
}
}