-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrmSellerDashboard.cs
307 lines (288 loc) · 13.2 KB
/
FrmSellerDashboard.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace PharmacyAutomation
{
public partial class FrmSellerDashboard : Form
{
public FrmSellerDashboard()
{
InitializeComponent();
}
public string username;
public string personID;
SqlConnection connection = new SqlConnection("Data Source=DESKTOP-2H5V0KB\\SQLEXPRESS;Initial Catalog=DbPharmacy;Integrated Security=True");
static string myApi = "2cd512a81a207c80a98260dea6a3d0e9";
private static string city = "istanbul";
static string conc =
"https://api.openweathermap.org/data/2.5/weather?q=" + city + "&mode=xml&lang=tr&units=metric&appid=" + myApi;
static XDocument weather = XDocument.Load(conc);
string temp = weather.Descendants("temperature").ElementAt(0).Attribute("value").Value;
string weatherstate = weather.Descendants("weather").ElementAt(0).Attribute("value").Value;
public void MedicineList()
{
SqlDataAdapter dataAdapter = new SqlDataAdapter("Select MedicineID, MedicineName, Quantity, Stock, ConsumptionDate, Country, PurchasePrice, SalePrice, CategoryName, TblMedicine.Situation from TblMedicine inner join TblMedicineCategory on TblMedicine.CategoryID = TblMedicineCategory.CategoryID where TblMedicine.Situation = 1", connection);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);
dataGridView1.Rows.Clear();
foreach (DataRow item in dataTable.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[1].Value = item["MedicineID"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["MedicineName"].ToString();
dataGridView1.Rows[n].Cells[3].Value = item["Quantity"].ToString();
dataGridView1.Rows[n].Cells[4].Value = item["Stock"].ToString();
dataGridView1.Rows[n].Cells[5].Value = item["ConsumptionDate"].ToString();
dataGridView1.Rows[n].Cells[6].Value = item["SalePrice"].ToString();
dataGridView1.Rows[n].Cells[7].Value = item["CategoryName"].ToString();
}
}
private void FrmSellerDashboard_Load(object sender, EventArgs e)
{
timer1.Start();
timer2.Enabled = true;
lblWeather.Text = temp + " " + weatherstate;
lblCity.Text = city.ToUpper();
lblUserName.Text = username;
MedicineList();
}
private void timer1_Tick(object sender, EventArgs e)
{
lblDate.Text = DateTime.Now.ToShortDateString();
lblTime.Text = DateTime.Now.ToLongTimeString();
}
private void timer2_Tick(object sender, EventArgs e)
{
if (lblWeather.Text.Length >= 4)
{
if (lblWeather.Left > 0)
{
lblWeather.Left -= 3;
}
else
{
lblWeather.Left = 151;
}
}
}
private void txtSearchMedicine_Enter(object sender, EventArgs e)
{
if (txtSearchMedicine.Text == "İlaç ismi giriniz...")
{
txtSearchMedicine.Text = "";
}
}
private void txtSearchMedicine_Leave(object sender, EventArgs e)
{
if (txtSearchMedicine.Text == "")
{
txtSearchMedicine.Text = "İlaç ismi giriniz...";
}
}
private void btnEditProfile_Click(object sender, EventArgs e)
{
FrmEditProfile frmEditProfile = new FrmEditProfile();
frmEditProfile.personID = personID;
frmEditProfile.Show();
Close();
}
private void SignOut_Click(object sender, EventArgs e)
{
Close();
}
private void txtSearchMedicine_TextChanged(object sender, EventArgs e)
{
if (txtSearchMedicine.Text != "" && txtSearchMedicine.Text != "İlaç ismi giriniz...")
{
SqlCommand command =
new SqlCommand(
"Select * From TblMedicine where MedicineName Like '" + txtSearchMedicine.Text + "%'",
connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
dataGridView1.Rows.Clear();
foreach (DataRow item in dataTable.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[1].Value = item["MedicineID"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["MedicineName"].ToString();
dataGridView1.Rows[n].Cells[3].Value = item["Quantity"].ToString();
dataGridView1.Rows[n].Cells[4].Value = item["Stock"].ToString();
dataGridView1.Rows[n].Cells[5].Value = item["ConsumptionDate"].ToString();
dataGridView1.Rows[n].Cells[6].Value = item["SalePrice"].ToString();
}
}
else
{
MedicineList();
}
}
private void btnSelectedCopy_Click(object sender, EventArgs e)
{
copyData();
}
double total = 0;
private Dictionary<string, string> medicineIDsAndCount = new Dictionary<string, string>();
private int stock = 0;
private int stock1 = 0;
public void copyData()
{
foreach (DataGridViewRow drv in dataGridView1.Rows)
{
if (Convert.ToBoolean(drv.Cells[0].Value))
{
if (txtMedicinePiece.Text == "")
{
txtMedicinePiece.Text = "1";
}
int n = dataGridView2.Rows.Add();
dataGridView2.Rows[n].Cells[0].Value = drv.Cells[1].Value.ToString();
dataGridView2.Rows[n].Cells[1].Value = drv.Cells[2].Value.ToString();
dataGridView2.Rows[n].Cells[2].Value = drv.Cells[3].Value.ToString();
dataGridView2.Rows[n].Cells[3].Value = drv.Cells[6].Value.ToString();
dataGridView2.Rows[n].Cells[4].Value = txtMedicinePiece.Text;
int secilen = dataGridView2.SelectedCells[0].RowIndex;
if (medicineIDsAndCount.ContainsKey(drv.Cells[1].Value.ToString()))
{
stock1 = int.Parse(medicineIDsAndCount[drv.Cells[1].Value.ToString()]);
stock1 += Int32.Parse(txtMedicinePiece.Text);
connection.Open();
SqlCommand command2 = new SqlCommand("select Stock from TblMedicine where MedicineID=@p1", connection);
command2.Parameters.AddWithValue("@p1", drv.Cells[1].Value.ToString());
SqlDataReader reader = command2.ExecuteReader();
while (reader.Read())
{
stock = int.Parse(reader[0].ToString());
}
connection.Close();
if (stock1 > stock)
{
MessageBox.Show("Yeteri kadar ilaç sayısı depoda yok, ilaçtan " + stock + " adet kaldı", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);
dataGridView2.Rows.RemoveAt(n);
}
else
{
total += Convert.ToDouble(drv.Cells[6].Value.ToString()) * Int32.Parse(txtMedicinePiece.Text);
lblTotal.Text = total.ToString();
medicineIDsAndCount[drv.Cells[1].Value.ToString()] = (Int32.Parse(medicineIDsAndCount[drv.Cells[1].Value.ToString()]) + Int32.Parse(txtMedicinePiece.Text)).ToString();
stock1 = 0;
}
}
else
{
connection.Open();
SqlCommand command2 = new SqlCommand("select Stock from TblMedicine where MedicineID=@p1", connection);
command2.Parameters.AddWithValue("@p1", drv.Cells[1].Value.ToString());
SqlDataReader reader = command2.ExecuteReader();
while (reader.Read())
{
stock = int.Parse(reader[0].ToString());
}
connection.Close();
if (int.Parse(txtMedicinePiece.Text) > stock)
{
dataGridView2.Rows.RemoveAt(n);
MessageBox.Show("Yeteri kadar ilaç sayısı depoda yok, ilaçtan " + stock + " adet kaldı", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
total += Convert.ToDouble(drv.Cells[6].Value.ToString()) * Int32.Parse(txtMedicinePiece.Text);
lblTotal.Text = total.ToString();
medicineIDsAndCount.Add(drv.Cells[1].Value.ToString(), txtMedicinePiece.Text);
}
}
drv.Cells[0].Value = false;
txtMedicinePiece.Text = "";
}
}
}
private void txtPaidMoney_TextChanged(object sender, EventArgs e)
{
if (txtPaidMoney.Text != "")
{
if (lblTotal.Text != "0")
{
lblReturnMoney.Text = (Convert.ToDouble(txtPaidMoney.Text) - Convert.ToDouble(lblTotal.Text)).ToString();
}
else
{
lblTotal.Text = "0";
lblReturnMoney.Text = "0";
}
}
else
{
lblReturnMoney.Text = "0";
}
}
public void Sales()
{
connection.Open();
SqlCommand command = new SqlCommand("insert into TblSales (Gain) values (@p1)", connection);
command.Parameters.AddWithValue("@p1", Convert.ToDouble(lblTotal.Text));
command.ExecuteNonQuery();
connection.Close();
}
public void SalesSellerMedicine()
{
foreach (var m in medicineIDsAndCount)
{
connection.Open();
SqlCommand command = new SqlCommand("insert into TblSalesSellerMedicine (SalesID,SellerID,MedicineID,MedicineCount,Situation) values((Select Top(1) SalesID from TblSales order by Date desc),@p1,@p2,@p3,1)", connection);
command.Parameters.AddWithValue("@p1", personID);
command.Parameters.AddWithValue("@p2", m.Key);
command.Parameters.AddWithValue("@p3", m.Value);
command.ExecuteNonQuery();
connection.Close();
connection.Open();
SqlCommand command1 = new SqlCommand("Update TblMedicine set Stock=Stock-@p2 where MedicineID=@p1", connection);
command1.Parameters.AddWithValue("@p1", m.Key);
command1.Parameters.AddWithValue("@p2", m.Value);
command1.ExecuteNonQuery();
connection.Close();
}
MessageBox.Show("Satış başarıyla gerçekleştirildi");
medicineIDsAndCount.Clear();
lblTotal.Text = "0";
}
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
DeleteMedicine();
}
public void DeleteMedicine()
{
total = Convert.ToDouble(lblTotal.Text);
int secilen = dataGridView2.SelectedCells[0].RowIndex;
total -= Convert.ToDouble(dataGridView2.Rows[secilen].Cells[3].Value.ToString()) * Int32.Parse(dataGridView2.Rows[secilen].Cells[4].Value.ToString());
lblTotal.Text = total.ToString();
medicineIDsAndCount.Remove(dataGridView2.Rows[secilen].Cells[0].Value.ToString());
dataGridView2.Rows.RemoveAt(secilen);
}
private void btnSell_Click(object sender, EventArgs e)
{
Sales();
SalesSellerMedicine();
lblTotal.Text = "0";
txtPaidMoney.Text = "0";
lblReturnMoney.Text = "0";
dataGridView2.Rows.Clear();
MedicineList();
}
private void lblTotal_TextChanged(object sender, EventArgs e)
{
if (txtPaidMoney.Text != "")
{
lblReturnMoney.Text = (Convert.ToDouble(txtPaidMoney.Text) - Convert.ToDouble(lblTotal.Text)).ToString();
}
}
}
}