-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
325 lines (268 loc) · 11 KB
/
Form1.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
fillData();
//Addbttn.Enabled = false;
RMVALLBttn.Enabled = false;
dscnttxt.Text = "0";
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.DataSource == null)
{
Addbttn.Enabled = false;
}
if(dataGridView1.Columns[e.ColumnIndex].Name == "Remove"){
dataGridView1.Rows.RemoveAt(this.dataGridView1.Rows[e.RowIndex].Index);
}
}
private void fillData()
{
string selectQuery = "SELECT * FROM sampleinventory.product ";
MySqlDataAdapter adapter = new MySqlDataAdapter(selectQuery, connection);
adapter.Fill(dt);
dataGridView2.DataSource = dt;
}
private void Addbttn_Click(object sender, EventArgs e)
{
if (quantitytxt.Text=="" || pnametxt.Text==""||pcodetxt.Text==""||pricetxt.Text==""||dscnttxt.Text=="")
{
MessageBox.Show("Quantity cannot be zero");
pcodetxt.Text = null;
pnametxt.Text = null;
quantitytxt.Text = null;
MessageBox.Show("p");
return;
}
string selectedpname;
string selectedprice;
string total;
string valuewithoutdiscount;
string selectedpcode;
selectedpname = Convert.ToString(dataGridView2.CurrentRow.Cells[1].Value);
selectedpcode = Convert.ToString(dataGridView2.CurrentRow.Cells[0].Value);
selectedprice = pricetxt.Text;
valuewithoutdiscount = Convert.ToString(Convert.ToDouble(selectedprice)*Convert.ToDouble(quantitytxt.Text));
total = Convert.ToString(Convert.ToDouble(valuewithoutdiscount) - ((Convert.ToDouble(dscnttxt.Text) / 100) * Convert.ToDouble(valuewithoutdiscount)));
dataGridView1.Rows.Add(" ", selectedpcode, selectedpname, quantitytxt.Text, selectedprice, total );
RMVALLBttn.Enabled = true;
addPrice();
pnametxt.Text = null;
pcodetxt.Text = null;
quantitytxt.Text = null;
dscnttxt.Text = "0";
pricetxt.Text = null;
pcodetxt.Enabled = true;
pnametxt.Enabled = true;
//Addbttn.Enabled = false;
//RMVALLBttn.Enabled = false;
}
private void Totaltxt_TextChanged(object sender, EventArgs e)
{
}
void addPrice()
{
double sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToDouble(dataGridView1.Rows[i].Cells[5].Value);
}
Totaltxt.Text = sum.ToString();
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
}
private void quantitytxt_TextChanged(object sender, EventArgs e)
{
}
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{/*
int index = e.RowIndex;
DataGridViewRow selectedRow = dataGridView2.Rows[index];
pnametxt.Text = selectedRow.Cells[1].Value.ToString();
string selectedpcode;
selectedpcode = Convert.ToString(dataGridView2.CurrentRow.Cells[0].Value);
pcodetxt.Text = selectedpcode;
pricetxt.Text = Convert.ToString(dataGridView2.CurrentRow.Cells[5].Value);*/
}
private void pnametxt_TextChanged(object sender, EventArgs e)
{
DataView dv = new DataView(dt);
dv.RowFilter = string.Format("name LIKE '{0}%'", pnametxt.Text);
dataGridView2.DataSource = dv;
}
private void pcodetxt_TextChanged(object sender, EventArgs e)
{
DataView dv = new DataView(dt);
dv.RowFilter = string.Format("CONVERT(`product_code`, 'System.String') LIKE '{0}%'", pcodetxt.Text);
dataGridView2.DataSource = dv;
}
private void pnametxt_KeyPress(object sender, KeyPressEventArgs e)
{
pcodetxt.Enabled = false;
}
private void pcodetxt_KeyPress(object sender, KeyPressEventArgs e)
{
pnametxt.Enabled = false;
}
private void RMVALLBttn_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
Totaltxt.Text = null;
}
private void dscnttxt_TextChanged(object sender, EventArgs e)
{
}
private void MASBttn_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
string currentstock = "";
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT stock FROM sampleinventory.product WHERE product_code = '" + Convert.ToString(dataGridView1.Rows[i].Cells[1].Value) + "'";
try
{
connection.Open();
MySqlDataReader myReader = cmd.ExecuteReader();
while (myReader.Read())
{
currentstock = Convert.ToString(Convert.ToInt32(myReader.GetString("stock")));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
}
string newstock = Convert.ToString(Convert.ToInt32(currentstock) - Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value));
string updatequery = "UPDATE sampleinventory.product SET stock = '" + newstock + "' WHERE name = '" + Convert.ToString(dataGridView1.Rows[i].Cells[2].Value) + "' ";
MySqlCommand cmdsampleinventory = new MySqlCommand(updatequery, connection);
try
{
connection.Open();
int myread2er = cmdsampleinventory.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
string details = "";
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
string product_code = "";
string product = "";
string quantity = "";
string price = "";
string total = "";
product_code = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
product = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value);
quantity = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value);
price = Convert.ToString(dataGridView1.Rows[i].Cells[4].Value);
total = Convert.ToString(dataGridView1.Rows[i].Cells[5].Value);
details += product_code + " " + product + " " + quantity + " " + price + " " + total + "/" ;
}
try
{
DateTime current = DateTime.Now;
string date = "";
string time = "";
date = current.ToShortDateString();
time = current.ToLongTimeString();
connection.Open();
string insertquery = "INSERT INTO sampleinventory.invoice VALUES (null,@details,@date,@time)";
MySqlCommand command = new MySqlCommand(insertquery, connection);
command.Parameters.AddWithValue("@details", details);
command.Parameters.AddWithValue("@date", date);
command.Parameters.AddWithValue("@time", time);
command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
}
Form2 frm = new Form2();
for (int i = 0; i < dataGridView1.RowCount-1; i++ )
{
frm.dataGridView3.Rows.Add();
for (int j = 2 ; j < 6 ; j++)
{
frm.dataGridView3.Rows[i].Cells[j-2].Value = this.dataGridView1.Rows[i].Cells[j].Value;
//MessageBox.Show(Convert.ToString(this.dataGridView1.Rows[i].Cells[j].Value));
}
}
MySqlCommand cmd2 = connection.CreateCommand();
cmd2.CommandText = "SELECT * FROM sampleinventory.invoice WHERE invoice_number = (SELECT MAX(invoice_number) FROM sampleinventory.invoice)";
string invoice_num = "";
try
{
connection.Open();
MySqlDataReader myReader2 = cmd2.ExecuteReader();
while (myReader2.Read())
{
invoice_num = Convert.ToString(Convert.ToInt32(myReader2.GetString("Invoice_number")));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
}
frm.invcnumtxtx.Text = invoice_num;
frm.ttlbilltxt.Text = Totaltxt.Text;
frm.Show();
Addbttn.Enabled = false;
}
private void Exitbttn_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void dataGridView2_SelectionChanged(object sender, EventArgs e)
{
string name = "";
string code = "";
string price = "";
foreach (DataGridViewRow row in dataGridView2.SelectedRows){
name = Convert.ToString(row.Cells[1].Value);
code = Convert.ToString(row.Cells[0].Value);
price = Convert.ToString(row.Cells[5].Value);
pnametxt.Text = name;
pcodetxt.Text = code;
pricetxt.Text = price;
}
}
private void pricetxt_TextChanged(object sender, EventArgs e)
{
}
}
}