-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransactionForm.cs
400 lines (344 loc) · 15.2 KB
/
TransactionForm.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
using MetroFramework;
using MetroFramework.Forms;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LoginForm
{
public partial class TransactionForm : MetroForm
{
private MySqlConnection connection;
string ConnectionString = ConfigurationManager.AppSettings["connection"];
bool IsInserting = false;
int rowIndex = 0;
public TransactionForm()
{
InitializeComponent();
}
int flag_update = 0;
private void btn_add_Click(object sender, EventArgs e)
{
this.ActiveControl = date_trans;
IsInserting = true;
btn_cancel.PerformClick();
transac_grid.ClearSelection();
txt_tid.Enabled = true;
date_trans.Enabled = true;
item_txt.Enabled = true;
mtxt_serial.Enabled = true;
txt_quant.Enabled = true;
txt_price.Enabled = true;
txt_supp.Enabled = true;
txt_warrant.Enabled = true;
pic_receipts.Enabled = true;
btn_cancel.Enabled = true;
btn_save.Enabled = true;
btn_add.Enabled = false;
}
private void Insert()
{
connection = new MySqlConnection(ConnectionString);
try
{
connection.Open();
string res = pic_receipts.ImageLocation.ToString().Replace(@"\","|");
// string[] splitinfoname = cmb_itemname.Text.Split(new Char[] { '-' });
MySqlCommand cmd = new MySqlCommand("insert into transaction(trans_date, iid, item_serial, quantity, price, supplier, warranty, receipts) value('" + date_trans.Value.ToString("yyyy-MM-dd") + "', '" + mtxt_iid.Tag + "' , '"+ mtxt_serial.Text +"', '" + txt_quant.Text + "','" + txt_price.Text + "', '" + txt_supp.Text + "', '" + txt_warrant.Text + "', '" + res + "')", connection);
cmd.ExecuteNonQuery();
MetroMessageBox.Show(this, "Added Succesfully!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
connection.Close();
LoadAllTransaction();
}
catch (Exception ex)
{
MetroMessageBox.Show(this, ex.Message);
}
}
private void LoadAllTransaction()
{
connection = new MySqlConnection(ConnectionString);
transac_grid.Rows.Clear();
try
{
connection.Open();
MySqlCommand cmd = new MySqlCommand("SELECT transaction.tid, transaction.trans_date, inventory_info.item_name, inventory_info.item_serial, transaction.quantity, transaction.price, transaction.supplier, transaction.warranty, transaction.receipts, inventory_info.iid FROM transaction Inner Join inventory_info ON transaction.iid = inventory_info.iid ORDER BY transaction.tid Desc ", connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
transac_grid.Rows.Add(dataReader["tid"] + "", Convert.ToDateTime(dataReader["trans_date"]).ToString("yyyy-MM-dd"), dataReader["item_name"] + "",dataReader["item_serial"] + "", dataReader["quantity"] + "", dataReader["price"] + "", dataReader["supplier"] + "", dataReader["warranty"] + "", dataReader["receipts"] + "", dataReader["iid"]+"");
}
connection.Close();
}
catch (Exception ex)
{
MetroMessageBox.Show(this, ex.Message);
}
}
private void LoadAllItems()
{
//connection = new MySqlConnection(ConnectionString);
//try
//{
// connection.Open();
// MySqlCommand cmd = new MySqlCommand("Select * from inventory_info order by iid desc", connection);
// MySqlDataReader dataReader = cmd.ExecuteReader();
// while (dataReader.Read())
// {
// cmb_itemname.Items.Add(dataReader["item_name"]);
// }
// connection.Close();
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message);
//}
}
private void Updates()
{
connection = new MySqlConnection(ConnectionString);
try
{
connection.Open();
string res = pic_receipts.ImageLocation.ToString().Replace(@"\","|");
// string[] splitinfoname = cmb_itemname.Text.Split(new Char[] { '-' });
string qString = "update transaction set trans_date = '" + date_trans.Value.ToString("yyyy-MM-dd") + "', iid = '" + mtxt_iid.Tag + "', item_serial = '"+ mtxt_serial.Text +"', quantity = '" + txt_quant.Text + "', price = '" + txt_price.Text + "', supplier = '" + txt_supp.Text + "', warranty ='" + txt_warrant.Text + "', receipts ='" + res + "' where tid = '" + txt_tid.Text + "' ";
// MessageBox.Show("" + item_txt.Tag);
MySqlCommand cmd = new MySqlCommand(qString, connection);
cmd.ExecuteNonQuery();
MetroMessageBox.Show(this, "Updated Succesfully!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
transac_grid.Update();
transac_grid.Refresh();
connection.Close();
LoadAllTransaction();
this.transac_grid.CurrentCell = this.transac_grid[1, Convert.ToInt32(rowIndex)];
}
catch (Exception ex)
{
MetroMessageBox.Show(this, ex.Message);
}
}
private void Delete(string ID)
{
connection = new MySqlConnection(ConnectionString);
try
{
connection.Open();
MySqlCommand cmd = new MySqlCommand("delete from transaction where tid = '" + ID + "'", connection);
cmd.ExecuteNonQuery();
MetroMessageBox.Show(this, "Deleted Succesfully!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
connection.Close();
LoadAllTransaction();
btn_cancel.PerformClick();
}
catch (Exception ex)
{
MetroMessageBox.Show(this, ex.Message);
}
}
private void btn_save_Click_1(object sender, EventArgs e)
{
connection = new MySqlConnection(ConnectionString);
if (item_txt.Text == "" || txt_quant.Text == "" || txt_price.Text == "" || mtxt_serial.Text == "")
{
string myStringVariable1 = string.Empty;
MetroMessageBox.Show(this, "Field is Empty");
return;
}
if (IsInserting == true)
{
connection.Open();
string str = "Select count(*) from transaction where item_serial = '" + mtxt_serial.Text + "'";
MySqlCommand cmd = new MySqlCommand(str, connection);
int itemExist = Convert.ToInt32(cmd.ExecuteScalar());
if (itemExist > 0)
{
MetroMessageBox.Show(this, "Item already exist");
btn_cancel.PerformClick();
return;
}
connection.Close();
Insert();
}
else
{
Updates();
}
flag_update = 0;
}
private void btn_cancel_Click(object sender, EventArgs e)
{
txt_tid.Text = "";
date_trans.Text = "";
item_txt.Text = "Select Item";
mtxt_serial.Text = "Serial";
txt_quant.Text = "";
txt_price.Text = "";
txt_supp.Text = "";
txt_warrant.Text = "";
pic_receipts.ImageLocation = "";
btn_add.Enabled = true;
transac_grid.ClearSelection();
}
private void updateToolStripMenuItem_Click(object sender, EventArgs e)
{
btn_add.Enabled = false;
this.ActiveControl = date_trans;
IsInserting = false;
flag_update = 1;
txt_tid.Enabled = true;
date_trans.Enabled = true;
item_txt.Enabled = true;
mtxt_serial.Enabled = true;
txt_quant.Enabled = true;
txt_price.Enabled = true;
txt_supp.Enabled = true;
txt_warrant.Enabled = true;
pic_receipts.Enabled = true;
btn_cancel.Enabled = true;
btn_save.Enabled = true;
rowIndex = transac_grid.CurrentCell.RowIndex;
foreach (DataGridViewRow row in this.transac_grid.Rows)
{
if (row.Selected == true)
{
txt_tid.Text = row.Cells[0].Value.ToString();
date_trans.Text = row.Cells[1].Value.ToString();
item_txt.Text = row.Cells[2].Value.ToString();
mtxt_serial.Text = row.Cells[3].Value.ToString();
txt_quant.Text = row.Cells[4].Value.ToString();
txt_price.Text = row.Cells[5].Value.ToString();
txt_supp.Text = row.Cells[6].Value.ToString();
txt_warrant.Text = row.Cells[7].Value.ToString();
pic_receipts.ImageLocation = row.Cells[8].Value.ToString().Replace("|", @"\"); ;
mtxt_iid.Tag = row.Cells[9].Value.ToString();
break;
}
}
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
btn_add.Enabled = true;
foreach (DataGridViewRow row in this.transac_grid.Rows)
{
if (row.Selected == true)
{
Delete(row.Cells[0].Value.ToString());
break;
}
}
}
private void transac_grid_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
transac_grid.Rows[e.RowIndex].Selected = true;
}
}
private void TransactionForm_Load(object sender, EventArgs e)
{
LoadAllTransaction();
// LoadAllItems();
this.ActiveControl = date_trans;
transac_grid.ReadOnly = true;
transac_grid.ClearSelection();
txt_tid.Enabled = false;
date_trans.Enabled = false;
item_txt.Enabled = false;
mtxt_serial.Enabled = false;
txt_quant.Enabled = false;
txt_price.Enabled = false;
txt_supp.Enabled = false;
txt_warrant.Enabled = false;
pic_receipts.Enabled = false;
btn_cancel.Enabled = false;
btn_save.Enabled = false;
}
private void pic_receipts_Click(object sender, EventArgs e)
{
OpenFileDialog openfile1 = new OpenFileDialog();
if (openfile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string filepath = openfile1.FileName;
if (filepath != string.Empty)
{
pic_receipts.ImageLocation = filepath;
}
}
}
private void transac_grid_CellClick_2(object sender, DataGridViewCellEventArgs e)
{
if (flag_update == 1)
{
DialogResult mb = MetroMessageBox.Show(this, "Do you want to save first?", "Save", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (mb == DialogResult.Yes)
{
flag_update = 0;
Updates();
}
else if (mb == DialogResult.No)
{
flag_update = 0;
txt_tid.Enabled = false;
date_trans.Enabled = false;
txt_quant.Enabled = false;
item_txt.Enabled = false;
mtxt_serial.Enabled = false;
txt_price.Enabled = false;
txt_supp.Enabled = false;
txt_warrant.Enabled = false;
pic_receipts.Enabled = false;
btn_cancel.PerformClick();
transac_grid.CurrentCell.Selected = true;
}
}
}
private void txt_price_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}
}
private void item_txt_Click(object sender, EventArgs e)
{
connection = new MySqlConnection(ConnectionString);
ItemName iname = new ItemName();
if (iname.ShowDialog(this) == DialogResult.OK)
{
item_txt.Text = iname.item_name_grid.Rows[iname.row].Cells[1].Value.ToString();
// item_txt.Tag = iname.item_name_grid.Rows[iname.row].Cells[0].Value.ToString();
mtxt_iid.Tag = iname.item_name_grid.Rows[iname.row].Cells[0].Value.ToString();
}
iname.Dispose();
try
{
connection.Open();
MySqlCommand cmd = new MySqlCommand("select item_name, item_serial, quantity from inventory_info where item_name = '" + item_txt.Text + "'", connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
txt_quant.Text = dataReader["quantity"] + "";
mtxt_serial.Text = dataReader["item_serial"] + "";
}
connection.Close();
}
catch (Exception ex)
{
MetroMessageBox.Show(this, ex.Message);
}
}
}
}