-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerformanceReportForm.cs
More file actions
307 lines (264 loc) · 12.1 KB
/
Copy pathPerformanceReportForm.cs
File metadata and controls
307 lines (264 loc) · 12.1 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace DB_Project
{
public partial class PerformanceReportForm : Form
{
// Database connection string
private string connectionString = @"Data Source=ABDULSABOOR190\SQLEXPRESS;Initial Catalog=sabbbb;Integrated Security=True;Encrypt=False";
// Tracking the currently selected date range
private DateTime startDate;
private DateTime endDate;
// Data variables
private int totalBookings = 0;
private decimal totalRevenue = 0;
private DataTable monthlyTrendsData;
public PerformanceReportForm()
{
InitializeComponent();
InitializeMonthlyTrendsTable();
}
private void InitializeMonthlyTrendsTable()
{
// Create data table for monthly trends
monthlyTrendsData = new DataTable();
monthlyTrendsData.Columns.Add("Month", typeof(string));
monthlyTrendsData.Columns.Add("Bookings", typeof(int));
monthlyTrendsData.Columns.Add("Revenue", typeof(string));
}
private void PerformanceReportsForm_Load(object sender, EventArgs e)
{
// Initialize date pickers with reasonable defaults (last 30 days)
dtpStartDate.Value = DateTime.Now.AddDays(-30);
dtpEndDate.Value = DateTime.Now;
// Set initial date range values
startDate = dtpStartDate.Value;
endDate = dtpEndDate.Value;
// Load initial data from database
LoadPerformanceData();
}
private void LoadPerformanceData()
{
try
{
// Load summary data from database
LoadSummaryDataFromDB();
// Load monthly trends data from database
LoadMonthlyTrendsFromDB();
}
catch (Exception ex)
{
MessageBox.Show("Error loading performance data: " + ex.Message, "Database Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadSummaryDataFromDB()
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
// Query to get total bookings and revenue within date range
string query = @"SELECT
COUNT(B.booking_id) AS TotalBookings,
COALESCE(SUM(P.amount), 0) AS TotalRevenue
FROM Booking B
LEFT JOIN Payment P ON B.booking_id = P.booking_id
WHERE B.booking_date BETWEEN @StartDate AND @EndDate
AND P.status = 'completed'";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@StartDate", startDate);
command.Parameters.AddWithValue("@EndDate", endDate);
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
// Get total bookings
totalBookings = !reader.IsDBNull(reader.GetOrdinal("TotalBookings")) ?
reader.GetInt32(reader.GetOrdinal("TotalBookings")) : 0;
// Get total revenue
totalRevenue = !reader.IsDBNull(reader.GetOrdinal("TotalRevenue")) ?
reader.GetDecimal(reader.GetOrdinal("TotalRevenue")) : 0;
// Update UI with real data
lblBookingsValue.Text = totalBookings.ToString();
lblRevenueValue.Text = "$" + totalRevenue.ToString("N0");
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Database error while loading summary data: " + ex.Message,
"Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
// Fallback to display zeros
lblBookingsValue.Text = "0";
lblRevenueValue.Text = "$0";
}
}
}
private void LoadMonthlyTrendsFromDB()
{
// Clear existing data
monthlyTrendsData.Rows.Clear();
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
// Query to get monthly booking and revenue data
string query = @"SELECT
FORMAT(B.booking_date, 'MMMM yyyy') AS Month,
COUNT(DISTINCT B.booking_id) AS TotalBookings,
COALESCE(SUM(P.amount), 0) AS TotalRevenue
FROM Booking B
LEFT JOIN Payment P ON B.booking_id = P.booking_id
WHERE B.booking_date BETWEEN DATEADD(MONTH, -5, @EndDate) AND @EndDate
AND (P.status = 'completed' OR P.status IS NULL)
GROUP BY FORMAT(B.booking_date, 'MMMM yyyy'), MONTH(B.booking_date), YEAR(B.booking_date)
ORDER BY YEAR(B.booking_date), MONTH(B.booking_date)";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@EndDate", endDate);
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
string month = reader["Month"].ToString();
int bookings = Convert.ToInt32(reader["TotalBookings"]);
decimal revenue = Convert.ToDecimal(reader["TotalRevenue"]);
// Add data to the DataTable
monthlyTrendsData.Rows.Add(month, bookings, "$" + revenue.ToString("N0"));
}
}
}
// If no data was returned, add current month with zeros
if (monthlyTrendsData.Rows.Count == 0)
{
monthlyTrendsData.Rows.Add(DateTime.Now.ToString("MMMM yyyy"), 0, "$0");
}
// Display the data in the DataGridView
LoadMonthlyTrendsData();
}
catch (Exception ex)
{
MessageBox.Show("Database error while loading monthly trends: " + ex.Message,
"Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
// Add a fallback row
monthlyTrendsData.Rows.Add(DateTime.Now.ToString("MMMM yyyy"), 0, "$0");
LoadMonthlyTrendsData();
}
}
}
private void LoadMonthlyTrendsData()
{
// Clear existing data
dgvMonthlyTrends.Rows.Clear();
// Add data rows from the data table
foreach (DataRow row in monthlyTrendsData.Rows)
{
dgvMonthlyTrends.Rows.Add(
row["Month"],
row["Bookings"],
row["Revenue"]
);
}
}
private void btnApplyFilters_Click(object sender, EventArgs e)
{
// Validate date range
if (dtpStartDate.Value > dtpEndDate.Value)
{
MessageBox.Show("Start date cannot be after end date.", "Invalid Date Range",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// Update date range values
startDate = dtpStartDate.Value;
endDate = dtpEndDate.Value;
// Reload data from database with new date range
LoadPerformanceData();
MessageBox.Show($"Report filtered for date range: {startDate.ToShortDateString()} - {endDate.ToShortDateString()}",
"Filters Applied", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void btnExportReport_Click(object sender, EventArgs e)
{
// Show a save file dialog
using (SaveFileDialog saveDialog = new SaveFileDialog())
{
saveDialog.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
saveDialog.DefaultExt = "csv";
saveDialog.FileName = "PerformanceReport_" + DateTime.Now.ToString("yyyyMMdd");
if (saveDialog.ShowDialog() == DialogResult.OK)
{
try
{
// Export data to CSV file
ExportToCSV(saveDialog.FileName);
MessageBox.Show("Report exported successfully!", "Export Complete",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error exporting report: " + ex.Message, "Export Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void ExportToCSV(string fileName)
{
// Create a StreamWriter to write to the file
using (StreamWriter writer = new StreamWriter(fileName))
{
// Write header for summary section
writer.WriteLine("TRAVEL EXPLORER PERFORMANCE REPORT");
writer.WriteLine($"Date Range: {startDate.ToShortDateString()} - {endDate.ToShortDateString()}");
writer.WriteLine();
// Write summary data
writer.WriteLine("SUMMARY");
writer.WriteLine($"Total Bookings,{totalBookings}");
writer.WriteLine($"Total Revenue,${totalRevenue:N2}");
writer.WriteLine();
// Write header for monthly trends
writer.WriteLine("MONTHLY TRENDS");
writer.WriteLine("Month,Bookings,Revenue");
// Write monthly trends data
foreach (DataRow row in monthlyTrendsData.Rows)
{
writer.WriteLine($"{row["Month"]},{row["Bookings"]},{row["Revenue"]}");
}
}
}
// Navigation button event handlers
private void btnRegistration_Click_1(object sender, EventArgs e)
{
//ServiceIntegrationForm serviceIntegrationForm = new ServiceIntegrationForm();
//serviceIntegrationForm.Show();
}
private void lblTravelExplorer_Click(object sender, EventArgs e)
{
// Empty event handler
}
private void button1_Click(object sender, EventArgs e)
{
ServiceListingForm serviceListingForm = new ServiceListingForm();
serviceListingForm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
BookingManagementForm bookingManagementForm = new BookingManagementForm();
bookingManagementForm.Show();
}
private void button3_Click(object sender, EventArgs e)
{
// Since we're already on the Performance Report Form, let's just refresh the data
LoadPerformanceData();
}
}
}