-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfirmOrder.cs
More file actions
315 lines (248 loc) · 12.9 KB
/
ConfirmOrder.cs
File metadata and controls
315 lines (248 loc) · 12.9 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
308
309
310
311
312
313
314
315
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using static Project.allCategories;
namespace Project
{
public partial class ConfirmOrderForm : Form
{
private bool feedbackControlsVisible = false;
private int newOrderID;
private SqlConnection connection;
private string connectionString = "Data Source=SAAD-DESKTOP\\SQLEXPRESS;Integrated Security=True";
public ConfirmOrderForm()
{
InitializeComponent();
connection = new SqlConnection("Data Source= SAAD-DESKTOP\\SQLEXPRESS;Integrated Security=True");
}
private List<Product> cartProducts;
private decimal totalCost;
public ConfirmOrderForm(List<Product> cartProducts, decimal totalCost)
{
InitializeComponent();
// Initialize the cartProducts and totalCost fields
this.cartProducts = cartProducts;
this.totalCost = totalCost;
SetFeedbackControlsVisibility(false);
// Hide the rating and review controls initially
ratingBox.Visible = false;
label3.Visible = false;
ratingBox.Visible = false;
Review.Visible = false;
button1.Visible = true;
totalCostLabel.Visible = true;
button3.Visible = false;
// Display the final receipt
DisplayReceipt();
}
private void DisplayReceipt()
{
// Clear existing items in the receiptListBox
receiptListBox.Items.Clear();
// Create a dictionary to store product quantities
Dictionary<int, int> productQuantities = new Dictionary<int, int>();
// Populate the dictionary with product quantities
foreach (Product product in cartProducts)
{
if (productQuantities.ContainsKey(product.ProductID))
{
productQuantities[product.ProductID]++;
}
else
{
productQuantities.Add(product.ProductID, 1);
}
}
// Add a fixed heading to the receiptListBox
receiptListBox.Items.Add($"{"Product Name",-25}{"Quantity",-10}{"Price",-15}");
// Add the products to the receiptListBox with quantities and prices
foreach (var kvp in productQuantities)
{
Product product = cartProducts.Find(p => p.ProductID == kvp.Key);
int quantity = kvp.Value;
decimal subtotal = product.Price * quantity;
// Format the output with fixed-width columns for a professional look
string productName = $"{product.Name,-25}";
string productLine = $"{productName}{quantity,-10}{subtotal:C}";
receiptListBox.Items.Add(productLine);
}
// Display the total cost in a label
totalCostLabel.Text = $"Total Cost: {totalCost:C}";
customerInfo.Text = $"Customer ID: 1\nCustomer Name: John Doe";
}
private void SetFeedbackControlsVisibility(bool isVisible)
{
// Set the visibility of feedback-related controls
button2.Visible = isVisible;
ratingBox.Visible = isVisible;
reviewBox.Visible = isVisible;
totalCostLabel.Visible = false;
button1.Visible = false;
label3.Visible = true;
ratingBox.Visible = true;
Review.Visible = true;
button3.Visible = true;
}
private void ConfirmOrderForm_Load(object sender, EventArgs e)
{
// You can add any necessary initialization code here
}
private void receiptListBox_SelectedIndexChanged(object sender, EventArgs e)
{
// You can add any logic for when the receiptListBox selection changes
}
private void textBoxEmail_TextChanged(object sender, EventArgs e)
{
// You can add any logic for when the text in textBoxEmail changes
}
private void label4_Click(object sender, EventArgs e)
{
// You can add any logic for when label4 is clicked
}
private void reviewBox_TextChanged(object sender, EventArgs e)
{
// You can add any logic for when the text in reviewBox changes
}
private void button2_Click(object sender, EventArgs e)
{
// You can add any logic for when button2 is clicked
}
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "Data Source= SAAD-DESKTOP\\SQLEXPRESS;Integrated Security=True";
int newOrderID;
// For example:
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string getMaxOrderIDQuery = "SELECT MAX(OrderID) FROM Orders;";
int maxOrderID;
using (SqlCommand command = new SqlCommand(getMaxOrderIDQuery, connection))
{
object result = command.ExecuteScalar();
maxOrderID = result == DBNull.Value ? 0 : Convert.ToInt32(result);
}
// Increment to assign a new OrderID
newOrderID = maxOrderID + 1;
// Get the current maximum OrderItemID from the OrderItems table
string getMaxOrderItemIDQuery = "SELECT MAX(OrderItemID) FROM OrderItems;";
int maxOrderItemID;
using (SqlCommand command = new SqlCommand(getMaxOrderItemIDQuery, connection))
{
object result = command.ExecuteScalar();
maxOrderItemID = result == DBNull.Value ? 0 : Convert.ToInt32(result);
}
// Increment to assign a new OrderItemID
int newOrderItemID = maxOrderItemID + 1;
// Insert into Orders table
string insertOrderQuery = "INSERT INTO Orders (OrderID, CustomerID, OrderDate, TotalAmount) VALUES (@OrderID, @CustomerID, GETDATE(), @TotalAmount);";
using (SqlCommand command = new SqlCommand(insertOrderQuery, connection))
{
command.Parameters.AddWithValue("@OrderID", newOrderID);
command.Parameters.AddWithValue("@CustomerID", 1); // Replace with the actual customer ID
command.Parameters.AddWithValue("@TotalAmount", totalCost);
command.ExecuteNonQuery();
}
// Insert into OrderItems table
foreach (Product product in cartProducts)
{
string insertOrderItemQuery = "INSERT INTO OrderItems (OrderItemID, OrderID, ProductID, Quantity, Subtotal) VALUES (@OrderItemID, @OrderID, @ProductID, 1, @Subtotal);";
using (SqlCommand command = new SqlCommand(insertOrderItemQuery, connection))
{
command.Parameters.AddWithValue("@OrderItemID", newOrderItemID);
command.Parameters.AddWithValue("@OrderID", newOrderID);
command.Parameters.AddWithValue("@ProductID", product.ProductID);
command.Parameters.AddWithValue("@Subtotal", product.Price);
command.ExecuteNonQuery();
}
// Increment OrderItemID for the next iteration
newOrderItemID++;
}
}
string receiptContent = GenerateReceipt(newOrderID);
MessageBox.Show(receiptContent, "Receipt");
// Optionally, you can display a success message or close the form
MessageBox.Show("Order placed successfully!");
//this.Close();
feedbackControlsVisible = true;
SetFeedbackControlsVisibility(feedbackControlsVisible);
}
private string GenerateReceipt(int orderNumber)
{
StringBuilder receipt = new StringBuilder();
receipt.AppendLine("╔══════════════════════════════════════════════╗");
receipt.AppendLine($"║ Cafe Bytes Receipt ║");
receipt.AppendLine("╚══════════════════════════════════════════════╝");
receipt.AppendLine($"\nOrder #{orderNumber,5}");
// Rest of the receipt content
receipt.AppendLine("------------------------------");
foreach (Product product in cartProducts)
{
// Use a fixed-width font for better alignment
string productName = $"{product.Name,-25}";
string price = $"{product.Price:C}";
// Align the text properly
receipt.AppendLine($"{productName} {price,10}");
}
receipt.AppendLine("------------------------------");
receipt.AppendLine($"Total Cost: {cartProducts.Sum(product => product.Price):C}");
receipt.AppendLine("------------------------------");
// Add a professional thanksgiving statement and mention the order time
receipt.AppendLine($"Thank you for choosing Cafe Bytes!");
receipt.AppendLine($"Your order (#{orderNumber}) was placed on {DateTime.Now.ToString("MMMM dd, yyyy HH:mm:ss")}");
receipt.AppendLine("We appreciate your business. Have a great day!");
return receipt.ToString();
}
private void totalCostLabel_Click(object sender, EventArgs e)
{
// You can add any logic for when totalCostLabel is clicked
}
private void customerInfo_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// Get the current maximum FeedbackID from the Feedback table
string getMaxFeedbackIDQuery = "SELECT MAX(FeedbackID) FROM Feedback;";
using (SqlCommand maxFeedbackIDCommand = new SqlCommand(getMaxFeedbackIDQuery, connection))
{
int maxFeedbackID;
object result = maxFeedbackIDCommand.ExecuteScalar();
maxFeedbackID = result == DBNull.Value ? 0 : Convert.ToInt32(result);
// Increment to assign a new FeedbackID
int newFeedbackID = maxFeedbackID + 1;
// Display relevant information in a message box
string message = $"Feedback ID: {newFeedbackID}\nOrder ID: {newOrderID}\nCustomer ID: 1\nRating: {ratingBox.Text}\nComment: {reviewBox.Text}\nFeedback Date: {DateTime.Now}";
MessageBox.Show(message, "Feedback Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
// Insert into the database
string insertFeedbackQuery = "INSERT INTO dbo.Feedback (FeedbackID, OrderID, CustomerID, Rating, Comment, FeedbackDate) VALUES (@FeedbackID, @OrderID, @CustomerID, @Rating, @Comment, @FeedbackDate);";
using (SqlCommand command = new SqlCommand(insertFeedbackQuery, connection))
{
command.Parameters.AddWithValue("@FeedbackID", newFeedbackID);
command.Parameters.AddWithValue("@OrderID", newOrderID);
command.Parameters.AddWithValue("@CustomerID", 1); // Replace with the actual customer ID
command.Parameters.AddWithValue("@Rating", Convert.ToInt32(ratingBox.Text)); // Use appropriate conversion
command.Parameters.AddWithValue("@Comment", reviewBox.Text);
command.Parameters.AddWithValue("@FeedbackDate", DateTime.Now);
command.ExecuteNonQuery();
}
}
}
// Optionally, you can display a success message or perform other actions after feedback submission
MessageBox.Show("Feedback submitted successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}