/
PlaceOrderUsrCtrl.cs
527 lines (438 loc) · 19.7 KB
/
PlaceOrderUsrCtrl.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Trial.DAO;
using Trial.DAOImpl;
using Trial.entity;
using System.Collections;
using DataBaseConfig;
using System.Data.SqlClient;
using Trial;
namespace Trial
{
public partial class PlaceOrderUsrCtrl : UserControl
{
string username;
string mobileNo;
string EmailId;
private static List<double> price = new List<double>();
private int qty = 1;
private int flag = 0, flag1 = 0, billNo = 1,flagForSearch=0,flagForReset=0;
private List<ArrayList> listOfItems = new List<ArrayList>();
private string str1;
public PlaceOrderUsrCtrl()
{
InitializeComponent();
}
private void searchBtn_Click(object sender, EventArgs e)
{
if(flagForSearch>0 && flagForReset==0)
{
Console.WriteLine("search called :{0}", flagForSearch);
clear();
}
ItemDAO itemsDAORef = new ItemDAOImpl();
Items itemsRef = new Items();
Category c = new Category();
c.categoryName = categorynameCmb.Text;
itemsRef.category = c;
itemsRef.itemName = null;
List<string> listNm = new List<string>();
listNm.Add("Item Name");
listNm.Add("Category Name");
listNm.Add("Item Desciption");
listNm.Add("Item Type");
listNm.Add("Quantity");
listNm.Add("Price");
List<ArrayList> itemList = new List<ArrayList>();
dataGridViewItemsList.ColumnHeadersVisible = true;
itemList = itemsDAORef.getItemsListSample(itemsRef);
dataGridViewItemsList.DataSource = CreateDataTable(listNm, itemList);
for (int i = 0; i < itemList.Count; i++)
{
double d=Convert.ToDouble(itemList[i][5]);
price.Add(d);
}
for (int i = 0; i < price.Count; i++)
{
Console.WriteLine("p[{0}]={1}", i, price[i]);
}
// dataGridViewItemsList.SelectedCells[0].ReadOnly = true;
//dataGridViewItemsList.Columns[0].ReadOnly = true;
//dataGridViewItemsList.Columns[1].ReadOnly = true;
//dataGridViewItemsList.Columns[2].ReadOnly = false;
//dataGridViewItemsList.Columns[3].ReadOnly = false;
//dataGridViewItemsList.Columns[4].ReadOnly = false;
//dataGridViewItemsList.Columns[5].ReadOnly = false;
// flag is used to ensure that the following buttons are added only once even if the search button pressed for number of times.
if(flag==0 )
{
DataGridViewButtonColumn col1 = new DataGridViewButtonColumn();
col1.UseColumnTextForButtonValue = true;
col1.Text = "+";
col1.Name = "+";
dataGridViewItemsList.Columns.Add(col1);
DataGridViewButtonColumn col2 = new DataGridViewButtonColumn();
col2.UseColumnTextForButtonValue = true;
col2.Text = "-";
col2.Name = "-";
dataGridViewItemsList.Columns.Add(col2);
DataGridViewButtonColumn col = new DataGridViewButtonColumn();
col.UseColumnTextForButtonValue = true;
col.Text = "Add to Cart";
col.Name = "Add to Cart";
dataGridViewItemsList.Columns.Add(col);
dataGridViewItemsList.Columns[6].Width = 20;
dataGridViewItemsList.Columns[7].Width = 20;
dataGridViewItemsList.Columns[8].Width = 120;
flag++;
}
//else
{
//// to make the column to fixed to right corner position .changing the display index value will shuffle position of the column in between the other columns
//dataGridViewItemsList.Columns["Item Name"].DisplayIndex = 0;
//dataGridViewItemsList.Columns["Category Name"].DisplayIndex = 1;
//dataGridViewItemsList.Columns["Item Desciption"].DisplayIndex = 2;
//dataGridViewItemsList.Columns["Item Type"].DisplayIndex = 3;
//dataGridViewItemsList.Columns["Quantity"].DisplayIndex = 4;
//dataGridViewItemsList.Columns["Price"].DisplayIndex = 5;
//dataGridViewItemsList.Columns["+"].DisplayIndex = 6;
//dataGridViewItemsList.Columns["-"].DisplayIndex = 7;
//dataGridViewItemsList.Columns["Add to Cart"].DisplayIndex = 8;
}
flagForSearch++;
}
private System.Data.DataTable CreateDataTable(List<string> columnDefinitions, List<ArrayList> rows)
{
DataTable table = new DataTable();
foreach (string colDef in columnDefinitions)
{
DataColumn column;
column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = colDef;
table.Columns.Add(column);
}
// Create DataRow and Add it to table
foreach (ArrayList rowData in rows)
{
DataRow row = table.NewRow();
// rowData is in same order as columnDefinitions
for (int i = 0; i < rowData.Count; i++)
{
row[i] = rowData[i];
}
table.Rows.Add(row);
}
return table;
}
private void categorynameCmb_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void PlaceOrderUsrCtrl_Load(object sender, EventArgs e)
{
loginPage loginPageRef = new loginPage();
username = loginPageRef.username;
mobileNo = loginPageRef.mobileNo;
EmailId = loginPageRef.EmailId;
CategoryDAO categoryDAORef = new CategoryDAOImpl();
List<Category> categoryRef = categoryDAORef.getcategoryList(null);
foreach (Category c in categoryRef)
categorynameCmb.Items.Add(c.categoryName);
//OrderCredentials orderCredentialsRef = new OrderCredentials(billNo++, dateTime);
//OrderCredentialsDAO orderCrDAORef = new OrderCredentialsDAOImpl();
//orderCrDAORef.insertOrderCredentials(orderCredentialsRef);
}
private void dataGridViewItemsList_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex==8)
{
MessageBox.Show("added to Cartt successfully");
btn_Click(sender, e);
}
if (e.ColumnIndex == 6)
{
// MessageBox.Show("+");
increaseByOne(sender, e);
}
if (e.ColumnIndex == 7)
{
// MessageBox.Show("-");
decreaseByOne(sender, e);
}
}
void Sendmail(String s)
{
try
{
// Create the Outlook application.
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
// Create a new mail item.
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
// Set HTMLBody.
//add the body of the email
//oMsg.HTMLBody = null;
oMsg.HTMLBody += "Item Name  ";
oMsg.HTMLBody += "quantity  ";
oMsg.HTMLBody += "Price <br />";
oMsg.HTMLBody += s;
//Subject line
oMsg.Subject = "Order Details";
// Add a recipient.
Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients;
// Change the recipient in the next line if necessary.
Microsoft.Office.Interop.Outlook.Recipient oRecip = oRecips.Add("Krathi.Kadamba@itcinfotech.com");
oRecip.Resolve();
// Send.
oMsg.Send();
// Clean up.
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}//end of try block
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}//end of catch
}
private void dataGridViewItemsList_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is Button)
{
// MessageBox.Show("inside button clicked event");
Button btn = e.Control as Button;
btn.Click -= new EventHandler(btn_Click);
btn.Click += new EventHandler(btn_Click);
}
}
private void orderLbl_Click(object sender, EventArgs e)
{
string content = "";
//if condition is to ensure that the Item list is not empty
//string str1 = "";
//str1 += "Item Name< br />";
//str1 += "Quantity < br />";
//str1 += "Price < br />";
if (listOfItems.Count == 0)
{
// Console.WriteLine("^^^^^^^^^^^^^^^^^^listofItems size={0}",listOfItems.Count);
MessageBox.Show("Cart is empty !!");
}
else
{
billNo = generateOrderId();
DateTime dateTime = DateTime.UtcNow.Date;
OrderCredentials orderCredentialsRef = new OrderCredentials(billNo, dateTime);
OrderCredentialsDAO orderCrDAORef = new OrderCredentialsDAOImpl();
orderCrDAORef.insertOrderCredentials(orderCredentialsRef);
BillDAO billDAORef = new BillDAOImpl();
Bill billRef = new Bill();
for (int i = 0; i < listOfItems.Count; i++)
{
string str = "";
billRef.billNoRef = orderCredentialsRef;
billRef.orderId = generateID();
Console.WriteLine("quantity:{0}\n", Convert.ToInt32(listOfItems[i][1]));
billRef.quantity = Convert.ToInt32(listOfItems[i][1]);
Console.WriteLine("item name:{0}\n", Convert.ToString(listOfItems[i][0]));
billRef.selectedItemName = Convert.ToString(listOfItems[i][0]);
Console.WriteLine("price:{0}\n", Convert.ToDouble(listOfItems[i][2]));
billRef.price = Convert.ToDouble(listOfItems[i][2]);
billDAORef.insertBill(billRef);
str += billRef.selectedItemName+ "  " + "  " + "  ";
str += billRef.quantity+ " " + " " + " " + " ";
str += billRef.price+ "  " + " " + " " + " ";
content += str+ "<br />";
}
MessageBox.Show("Order Placed !!");
canecelOrderBtn.Visible = true;
listOfItems.Clear();
}
// str1 += content;
Console.WriteLine(str1);
// MessageBox.Show(str1);
MessageBox.Show(content);
Sendmail(content);
}
private int generateID()
{
DataBaseconnection DbCon = new DataBaseconnection();
string qry = "select count(orderId) from dbo.order_orderBill;";
SqlConnection cnn = DbCon.ObtainConnection();
SqlCommand cmd = new SqlCommand(qry, cnn);
int i = Convert.ToInt32(cmd.ExecuteScalar());
cnn.Close();
return i++;
}
private int generateOrderId()
{
DataBaseconnection DbCon = new DataBaseconnection();
string qry = "select count(billNo) from dbo.order_orderCredentials;";
SqlConnection cnn = DbCon.ObtainConnection();
SqlCommand cmd = new SqlCommand(qry, cnn);
int i = Convert.ToInt32(cmd.ExecuteScalar());
cnn.Close();
return i++;
}
void btn_Click(object sender, EventArgs e)
{
int col = this.dataGridViewItemsList.CurrentCell.ColumnIndex;
int row = this.dataGridViewItemsList.CurrentCell.RowIndex;
var rowRef = dataGridViewItemsList.Rows[row];
var column = dataGridViewItemsList.Columns[col];
// MessageBox.Show("Button in Cell[" + col.ToString() + "," + row.ToString() + "] has been clicked");
ArrayList cartAdd = new ArrayList();
Items itemRef = new Items();
itemRef.itemType = Convert.ToString(rowRef.Cells["Item Name"].Value); ;
//if(flag_orderCredentials==0)
//{
// DateTime dateTime = DateTime.UtcNow.Date;
// OrderCredentials orderCredentialsRef = new OrderCredentials(billNo, dateTime);
// OrderCredentialsDAO orderCrDAORef = new OrderCredentialsDAOImpl();
// orderCrDAORef.insertOrderCredentials(orderCredentialsRef);
// flag_orderCredentials++;
//}
// MessageBox.Show(itemRef.itemType);
ArrayList billList = new ArrayList();
billList.Add(Convert.ToString(rowRef.Cells["Item Name"].Value));
billList.Add(Convert.ToInt32(rowRef.Cells["Quantity"].Value));
billList.Add(Convert.ToInt32(rowRef.Cells["Price"].Value));
listOfItems.Add(billList);
// billList.Add();
}
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("Satep1\n");
BillDAO billDAOImpl = new BillDAOImpl();
OrderCredentials o = new OrderCredentials();
o.billNo = billNo;
Bill billref = new Bill();
billref.billNoRef = o;
Console.WriteLine("Step22");
List<ArrayList> listBill = billDAOImpl.getBillList(billref);
for(int i=0;i<listBill.Count;i++)
{
Console.WriteLine("Step 3 inside for Loop\t");
billref.orderId =Convert.ToInt32( listBill[i][0]);
billDAOImpl.deleteBill(billref);
}
Console.WriteLine("step 4");
OrderCredentialsDAO ordercRedDAORef = new OrderCredentialsDAOImpl();
Console.WriteLine("Step5");
ordercRedDAORef.deleteorderCredentials(o);
MessageBox.Show("Order cancelled !!");
canecelOrderBtn.Visible = false;
try
{
// Create the Outlook application.
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
// Create a new mail item.
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
// Set HTMLBody.
//add the body of the email
//oMsg.HTMLBody = null;
oMsg.HTMLBody = "Order got cancelled!!!!";
//Subject line
oMsg.Subject = "Order Details";
// Add a recipient.
Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients;
// Change the recipient in the next line if necessary.
Microsoft.Office.Interop.Outlook.Recipient oRecip = oRecips.Add("Krathi.Kadamba@itcinfotech.com");
oRecip.Resolve();
// Send.
oMsg.Send();
// Clean up.
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}//end of try block
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}//end of catch
}
private void placeOrderLbl_Click(object sender, EventArgs e)
{
}
void increaseByOne(object sender, EventArgs e)
{
int col = this.dataGridViewItemsList.CurrentCell.ColumnIndex;
int row = this.dataGridViewItemsList.CurrentCell.RowIndex;
var rowRef = dataGridViewItemsList.Rows[row];
var column = dataGridViewItemsList.Columns[col];
// MessageBox.Show("Button in Cell[" + col.ToString() + "," + row.ToString() + "] has been clicked");
ArrayList cartAdd = new ArrayList();
Items itemRef = new Items();
// int currentVal= Convert.ToInt32(rowRef.Cells["Quantity"].Value);
qty++;
rowRef.Cells["Quantity"].Value = qty;
if(flag1==0)
{
price[row] = Convert.ToDouble(rowRef.Cells["Price"].Value);
flag1++;
}
rowRef.Cells["Price"].Value = qty * price[row];
Console.WriteLine("qty={0}\t price= {1}", qty, price);
//itemRef.stock -= qty;
qty = 1;
//MessageBox.Show(itemRef.itemType);
}
private void refreshBtn_Click(object sender, EventArgs e)
{
canecelOrderBtn.Visible = false;
flagForReset++;
// clear();
if(flagForReset<=1)
{
dataGridViewItemsList.DataSource = null;
dataGridViewItemsList.Columns.Remove("+");
dataGridViewItemsList.Columns.Remove("-");
dataGridViewItemsList.Columns.Remove("Add to Cart");
categorynameCmb.Text = "";
flag = 0;
}
}
void decreaseByOne(object sender, EventArgs e)
{
int col = this.dataGridViewItemsList.CurrentCell.ColumnIndex;
int row = this.dataGridViewItemsList.CurrentCell.RowIndex;
var rowRef = dataGridViewItemsList.Rows[row];
var column = dataGridViewItemsList.Columns[col];
// MessageBox.Show("Button in Cell[" + col.ToString() + "," + row.ToString() + "] has been clicked");
ArrayList cartAdd = new ArrayList();
if (qty > 1)
{
qty--;
rowRef.Cells["Quantity"].Value = qty;
if (flag1 == 0)
{
price[row] = Convert.ToDouble(rowRef.Cells["Price"].Value);
flag1++;
}
rowRef.Cells["Price"].Value = qty*price[row];
Console.WriteLine("qty={0}\t price= {1}", qty, price);
}
else
MessageBox.Show("quantity should be > 0");
//Items itemRef = new Items();
//itemRef.itemType = Convert.ToString(rowRef.Cells["Item Name"].Value); ;
//MessageBox.Show(itemRef.itemType);
}
public void clear()
{
dataGridViewItemsList.DataSource = null;
dataGridViewItemsList.Columns.Remove("+");
dataGridViewItemsList.Columns.Remove("-");
dataGridViewItemsList.Columns.Remove("Add to Cart");
flag = 0;//to re create button
}
}
}