-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewCart.aspx.cs
338 lines (309 loc) · 12.9 KB
/
ViewCart.aspx.cs
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
using CSharpAssignment.DAO;
using CSharpAssignment.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CSharpAssignment
{
public partial class ViewCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PanelErrMsgg.Visible = false;
// If the cart does not exist yet
if (Session["ShoppingCart"] == null)
{
this.CartPanel.Visible = false;
this.EmptyPanel.Visible = true;
PanelMsgg.Visible = false;
}
// If the cart exists
else
{
PanelMsgg.Visible = false;
this.CartPanel.Visible = true;
this.EmptyPanel.Visible = false;
DataTable dataTable = (DataTable)Session["ShoppingCart"];
GridItemCart.DataSource = dataTable;
GridItemCart.DataBind();
}
if (Session["AccountID"] != null)
{
DiscountPanel.Visible = true;
PleaseLoginPanel.Visible = false;
SqlDataSource2.SelectCommand += "and (ISNULL((SELECT AccountID FROM Booking where DiscountID = D.ID AND StatusID <> 3), -1)) <> " + Session["AccountID"];
DataView view = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
DataTable dt = view.ToTable();
DataRowCollection rows = dt.Rows;
foreach (DataRow row in rows)
{
string code = row["Code"].ToString();
string value = row["Value"].ToString();
string id = row["ID"].ToString();
DropdownListDiscount.Items.Add(new ListItem(code + " - " + value + " (%)", code));
}
}
else
{
DiscountPanel.Visible = false;
PleaseLoginPanel.Visible = true;
}
CalculatePrice();
}
}
private double CalculatePrice()
{
double price = 0;
for (int i = 0; i < GridItemCart.Rows.Count; i++)
{
GridViewRow selectedRow = GridItemCart.Rows[i];
TableCell id = selectedRow.Cells[6];
TableCell priceText = selectedRow.Cells[5];
if(priceText.Text.Length > 0)
{
price += Convert.ToDouble(id.Text) * Convert.ToDouble(priceText.Text);
}
}
lblTotalAmount.Text = price.ToString();
return price;
}
bool RemoveShoppingCart(string itemID)
{
if (Session["ShoppingCart"] == null)
{
Literal1.Text = "Sorry, Can not remove item in your shopping cart";
}
else
{
DataTable dataTable = (DataTable)Session["ShoppingCart"];
Common common = new Common();
int indexOfRow = common.IsExistItemInShoppingCart(itemID, dataTable);
if (indexOfRow != -1)
{
dataTable.Rows.RemoveAt(indexOfRow);
Session["ShoppingCart"] = dataTable;
GridItemCart.DataSource = dataTable;
GridItemCart.DataBind();
if(dataTable.Rows.Count == 0)
{
Session.Remove("ShoppingCart");
}
return true;
}
}
return false;
}
void UpdateShoppingCart(string itemID, string itemName, String itemPrice, string unitPrice, String quantity, int index)
{
if (Session["ShoppingCart"] == null)
{
Literal1.Text = "Sorry, Can not Update item in your shopping cart";
}
else
{
DataTable dataTable = (DataTable)Session["ShoppingCart"];
dataTable.Rows.RemoveAt(index);
//xoa cai bang kia và add 1 round moi theo cai gia tri nhap
DataRow dataRow = dataTable.NewRow();
dataRow["ID"] = itemID;
dataRow["Name"] = itemName;
dataRow["Quantity"] = quantity;
dataRow["Price"] = itemPrice;
dataRow["UnitPrice"] = unitPrice;
dataTable.Rows.Add(dataRow);
// add vô session
Session["ShoppingCart"] = dataTable;
GridItemCart.DataSource = dataTable;
GridItemCart.DataBind();
}
}
protected void GridItemCart_RowEditing(object sender, GridViewEditEventArgs e)
{
int index = Convert.ToInt32(e.NewEditIndex);
GridViewRow selectedRow = GridItemCart.Rows[index];
TableCell id = selectedRow.Cells[3];
TableCell name = selectedRow.Cells[4];
TableCell quanti = selectedRow.Cells[5];
TableCell price = selectedRow.Cells[6];
//int LABAVID = Convert.ToInt32(id.Text);
string ids = id.Text;
string itemName = name.Text;
string itemquantity = quanti.Text;
string itemprice = price.Text;
//Literal1.Text = "lấy dc ID: " + ids + itemName + itemquantity + itemprice;
bool success = RemoveShoppingCart(ids);
if (success)
{
lblTotalAmount.Text = CalculatePrice().ToString();
if (GridItemCart.Rows.Count == 0)
{
Session.Remove("ShoppingCart");
}
Literal1.Text = "Delete Susscess";
}
}
protected void GridItemCart_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = e.RowIndex;
GridViewRow selectedRow = GridItemCart.Rows[index];
TableCell id = selectedRow.Cells[3];
TableCell name = selectedRow.Cells[4];
TextBox quanti = (TextBox)selectedRow.Cells[2].FindControl("txtQuanity");
TableCell price = selectedRow.Cells[6];
TableCell unitPriceEle = selectedRow.Cells[7];
string ids = id.Text;
string itemName = name.Text;
string itemquantity = quanti.Text;
double itemprice = Convert.ToDouble(unitPriceEle.Text) * Convert.ToInt32(itemquantity);
double unitPrice = Convert.ToDouble(unitPriceEle.Text);
UpdateShoppingCart(ids, itemName, itemprice.ToString(), unitPrice.ToString(), itemquantity, index);
lblTotalAmount.Text = CalculatePrice().ToString();
}
protected void GridItemCart_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void BtnApply_Click(object sender, EventArgs e)
{
string code = DropdownListDiscount.Text;
DiscountDAO dao = new DiscountDAO();
decimal number = dao.CheckDiscountCode(code);
if (number != -1)
{
bool checkAlreadyUsed = dao.CheckAlreadyUsedDiscount(code, 1);
if (checkAlreadyUsed == false)
{
PriceDiscount.Text = (CalculatePrice() * ((double)number / 100)).ToString();
lblTotalAmount.Text = (CalculatePrice() * ((double)(100 - number)) / 100).ToString();
litDiscount.Text = "";
}
else
{
litDiscount.Text = "Your code is already used ";
}
}
else
{
litDiscount.Text = "Your code is not valid";
}
}
public bool CheckUserInfo()
{
string cusName = txtCusName.Text.Trim();
if (cusName.Length <= 3 || cusName.Length > 50)
{
litCusName.Text = "Customer name must be from 3 to 50 characters";
return false;
}
string phone = txtPhoneNumber.Text.Trim();
if (!Regex.IsMatch(phone, @"\d{10,11}"))
{
litPhone.Text = "Phone must be from 10 to 11 number";
return false;
}
string address = txtAddress.Text.Trim();
if (address.Length <= 3 || address.Length > 50)
{
litAddress.Text = "Address name must be from 3 to 50 characters";
return false;
}
string contactPerson = txtContactPerson.Text.Trim();
if (contactPerson.Length <= 3 || contactPerson.Length > 50)
{
litContactPerson.Text = "Customer name must be from 3 to 50 characters";
return false;
}
return true;
}
public void Clear()
{
txtCusName.Text = "";
txtPhoneNumber.Text = "";
txtAddress.Text = "";
txtContactPerson.Text = "";
txtDescription.Text = "";
}
protected void btnCheckOut_Click(object sender, EventArgs e)
{
}
protected void btnCheckOut_Click1(object sender, EventArgs e)
{
Literal1.Text = "";
LblErr.Text = "";
if (CheckUserInfo())
{
bool checkBooking = false;
string cusName = txtCusName.Text.Trim();
string phoneNumber = txtPhoneNumber.Text.Trim();
string address = txtAddress.Text.Trim();
string contactPerson = txtContactPerson.Text.Trim();
string description = txtDescription.Text.Trim();
string payment = chkPayment.Text;
CheckOutDAO dao = new CheckOutDAO();
ProductDAO products = new ProductDAO();
DateTime today = DateTime.Now;
for (int i = 0; i < GridItemCart.Rows.Count; i++)
{
TableCell productId = GridItemCart.Rows[i].Cells[3];
TableCell quanitity = GridItemCart.Rows[i].Cells[5];
int proId = Convert.ToInt32(productId.Text.ToString());
int quan = Convert.ToInt32(quanitity.Text.ToString());
int currentQuantity = products.GetQuantityOfProduct(proId);
if (quan <= currentQuantity)
{
checkBooking = true;
}
else
{
string productName = GridItemCart.Rows[i].Cells[4].Text;
LblErr.Text += Literal1.Text + "Product " + productName + " only have " + currentQuantity + " left. \n";
}
}
if (checkBooking)
{
AccountDTO dto = (AccountDTO)Session["USER"];
int accountID = -1;
if (dto != null)
{
accountID = dto.AccountID;
}
var discountID = -1;
if (Session["DiscountID"] != null)
{
discountID = (int)Session["DiscountID"];
}
bool insertBooking = dao.InsertIntoBooking(today, "ANCHOIBOCODE", address, phoneNumber, payment, contactPerson, description, accountID, discountID, cusName);
int bookingId = dao.GetBookingId(today);
for (int i = 0; i < GridItemCart.Rows.Count; i++)
{
TableCell productId = GridItemCart.Rows[i].Cells[3];
TableCell quanitity = GridItemCart.Rows[i].Cells[5];
int proId = Convert.ToInt32(productId.Text.ToString());
int quan = Convert.ToInt32(quanitity.Text.ToString());
int currentQuantity = products.GetQuantityOfProduct(proId);
bool insertDetails = dao.InsertBookingDetails(bookingId, proId, quan);
}
if(LblErr.Text.Length == 0)
{
PanelErrMsgg.Visible = false;
}
if (insertBooking)
{
PanelMsgg.Visible = true;
Literal1.Text = "Booking successfully !!!";
Session.Remove("ShoppingCart");
GridItemCart.Visible = false;
EmptyPanel.Visible = true;
Clear();
}
}
}
}
}
}