-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmPhieuNhacTra.cs
415 lines (400 loc) · 15.2 KB
/
frmPhieuNhacTra.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
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;
namespace BTL_QLThuvien_Csharp_Nhom10
{
public partial class frmPhieuNhacTra : Form
{
string A;
SqlConnection conn;
public frmPhieuNhacTra()
{
InitializeComponent();
conn = new SqlConnection("Data Source=THINKPADE14/MSSQLSERVER01;Initial Catalog=QLThuVien_BTL_NET1;Integrated Security=True;User Id=sa;Password=1");
}
private void txtghichu_TextChanged(object sender, EventArgs e)
{
}
private void dtpngaylap_ValueChanged(object sender, EventArgs e)
{
}
private void btnthoatpnt_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void frmPhieuNhacTra_Load(object sender, EventArgs e)
{
}
private string taomapnt()
{
string mapnt;
Random r = new Random();
mapnt = "PNT" + r.Next(0, 999).ToString();
return mapnt;
}
private void lienket_dulieu()
{
txtmapnt.DataBindings.Add("Text", dtgttpnt.DataSource, "MaPNT");
cbomathe.DataBindings.Add("Text", dtgttpnt.DataSource, "Mathe");
cbomasach.DataBindings.Add("Text", dtgttpnt.DataSource, "MaSach");
cbomanv.DataBindings.Add("Text", dtgttpnt.DataSource, "Manhanvien");
txtghichu.DataBindings.Add("Text", dtgttpnt.DataSource, "GhiChu");
txtdgp.DataBindings.Add("Text", dtgttpnt.DataSource, "DonGiaphat");
}
private void huy_lienketdulieu()
{
if (txtmapnt.DataBindings != null)
txtmapnt.DataBindings.Clear();
if (txtghichu.DataBindings != null)
txtghichu.DataBindings.Clear();
if (dtpngaylap.DataBindings != null)
dtpngaylap.DataBindings.Clear();
if (txtdgp.DataBindings != null)
txtdgp.DataBindings.Clear();
if (cbomasach.DataBindings != null)
cbomasach.DataBindings.Clear();
if (cbomanv.DataBindings != null)
cbomanv.DataBindings.Clear();
if (cbomathe.DataBindings != null)
cbomathe.DataBindings.Clear();
}
private void hienthitieudecotDataTable()
{
dtgttpnt.Columns[0].HeaderText = "Ma phieu nt";
dtgttpnt.Columns[1].HeaderText = "Ma the";
dtgttpnt.Columns[2].HeaderText = "Ma sach";
dtgttpnt.Columns[3].HeaderText = "Ma nhan vien";
dtgttpnt.Columns[4].HeaderText = "Ghi chu";
dtgttpnt.Columns[5].HeaderText = "Don gia phat";
}
private DataTable loadsach()
{
SqlCommand sql = new SqlCommand();
sql.CommandType = CommandType.StoredProcedure;
sql.CommandText = "sp_LOADSACH";
sql.Connection = conn;
DataTable sach = new DataTable();
conn.Open();
sach.Load(sql.ExecuteReader());
conn.Close();
return sach;
}
private void hienthidlsach()
{
cbomasach.DataSource = loadsach();
cbomasach.ValueMember = "MaSach";
cbomasach.DisplayMember = "TenSach";
}
private DataTable loadnv()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = @"SELECT * FROM NHANVIEN";
cmd.Connection = conn;
DataTable nhanvien = new DataTable();
conn.Open();
nhanvien.Load(cmd.ExecuteReader());
conn.Close();
return nhanvien;
}
private void hienthidlnhanvien()
{
cbomanv.DataSource = loadnv();
cbomanv.DisplayMember = "TenNhanVien";
cbomanv.ValueMember = "MaNhanVien";
}
private DataTable loadthethuvien()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = @"SELECT * FROM THETHUVIEN";
cmd.Connection = conn;
DataTable thethuvien
= new DataTable();
conn.Open();
thethuvien.Load(cmd.ExecuteReader());
conn.Close();
return thethuvien;
}
private void hienthidlthethuvien()
{
cbomanv.DataSource = loadthethuvien();
cbomanv.DisplayMember = "TenSinhVien";
cbomanv.ValueMember = "MaTheThuVien";
}
private DataTable loadphieunhactra()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = @"SELECT MaPNT,MaTheThuVien,CONVERT(CHAR(10),NgayLapPhieu,103) as NgayLapPhieu,DonGiaPhat,MaNhanVien,MaSach FROM PHIEUNHACTRA";
cmd.Connection = conn;
DataTable phieunhactra = new DataTable();
conn.Open();
phieunhactra.Load(cmd.ExecuteReader());
conn.Close();
return phieunhactra;
}
private void hienthidlphieunt()
{
dtgttpnt.DataSource = loadphieunhactra();
}
private void btnthempnt_Click(object sender, EventArgs e)
{
txtmapnt.Clear();
txtghichu.Clear();
txtdgp.Clear();
txtmapnt.Text = taomapnt();
}
private void luuphieunhactra()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = @"@MaPhieuNhacTra CHAR(10),
@MaTheThuVien CHAR(10)
@NgayLapPhieu DATETIME,
@DonGiaPhat INT,
@MaNhanVien CHAR(10),
@MaSach CHAR(10)
AS
BEGIN
IF EXISTS(SELECT * FROM PHIEUNHACTRA WHERE MaPhieuNhacTra=@MaPhieuNhacTra)
RETURN 1 ---TON TAI MA PNT
IF NOT EXISTS(SELECT * FROM NHANVIEN WHERE MaNhanVien=@MaNhanVien)
RETURN 2 ---KHONG TON TAI MA NV
IF NOT EXISTS(SELECT * FROM THETHUVIEN WHERE MaTheThuVien=@MaTheTV)
RETURN 3 ----KHONG TON TAI MA THETV
IF NOT EXISTS(SELECT * FROM SACH WHERE MaSach=@MaSach)
RETURN 4 ----KHONG TON TAI MA SACH
INSERT INTO PHIEUNHACTRA VALUES (@MaPhieuNhacTra,@MaTheThuVien,@NgayLapPhieu,@DonGiaPhat,@MaNhanVienV,@MaSach)
END";
cmd.Connection = conn;
string mapnt, masach, manv, mathe, ghichu;
double dongiaphat;
DateTime ngaylapphieu;
mapnt = txtmapnt.Text;
masach = cbomasach.SelectedValue.ToString();
manv = cbomanv.SelectedValue.ToString();
mathe = cbomathe.SelectedValue.ToString();
ghichu = txtghichu.Text;
ngaylapphieu = DateTime.Parse(dtpngaylap.Value.ToString());
if (txtdgp.Text == "")
{
dongiaphat = 0;
}
else
{
dongiaphat = double.Parse(txtdgp.Text);
}
cmd.Parameters.Add("@MaPhieuNhacTra", mapnt);
cmd.Parameters.Add("@MaTheThuVien", mathe);
cmd.Parameters.Add("@MaSach", masach);
cmd.Parameters.Add("@MaNhanVien", manv);
cmd.Parameters.Add("@NgayLapPhieu", ngaylapphieu);
cmd.Parameters.Add("@DonGiaphat", dongiaphat);
cmd.Parameters.Add("@GhiChu", ghichu);
try
{
cmd.Parameters.Add("@kq", SqlDbType.Int).Direction =
ParameterDirection.ReturnValue;
conn.Open();
cmd.ExecuteNonQuery();
int kq = (int)cmd.Parameters["@kq"].Value;
if (kq == 1)
{
lblthongbaopnt.ForeColor = Color.Red;
lblthongbaopnt.Text = "Da ton tai phieu";
return;
}else if (kq == 2)
{
lblthongbaopnt.ForeColor = Color.Red;
lblthongbaopnt.Text = "Khong ton tai nhan vien";
return;
}else if (kq == 3)
{
lblthongbaopnt.ForeColor = Color.Red;
lblthongbaopnt.Text = "Khong ton tai sinh vien";
return;
}else if (kq == 4)
{
lblthongbaopnt.ForeColor = Color.Red;
lblthongbaopnt.Text = "Khong ton tai sach";
return;
}else
{
lblthongbaopnt.ForeColor = Color.Green;
lblthongbaopnt.Text = "Luu thanh cong";
}
}catch(Exception exp)
{
MessageBox.Show("Loi khong luu duoc vi" + exp.Message);
}
finally
{
if (conn != null)
{
conn.Close();
}
}
}
private void btnluupnt_Click(object sender, EventArgs e)
{
huy_lienketdulieu();
luuphieunhactra();
loadphieunhactra();
lienket_dulieu();
}
private void suaphieunhactra()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText =
@"@MaPhieuNhacTra CHAR(10),
@MaTheThuVien CHAR(10),
@NgayLapPhieu DATETIME,
@DonGiaPhat INT,
@MaNhanVien CHAR(10),
@MaSach CHAR(10)
AS
BEGIN
IF NOT EXISTS(SELECT * FROM PHIEUNHACTRA WHERE MaPhieuNhacTra=@MaPhieuNhacTra)
RETURN 1 ---KHONG TON TAI MA PNT
IF NOT EXISTS(SELECT * FROM NHANVIEN WHERE MaNhanVien=@MaNhanVien)
RETURN 2 ---KHONG TON TAI MA NV
IF NOT EXISTS(SELECT * FROM THETHUVIEN WHERE MaTheThuVien=@MaTheTV)
RETURN 3 ---KHONG TON TAI MA THE TV
IF NOT EXISTS(SELECT * FROM SACH WHERE MaSach=@MaSach)
RETURN 4 ---KHONG TON TAI MA SACH
UPDATE PHIEUNHACTRA
SET MaTheThuVien=@MaTheTV,
NgayLapPhieu=@NgayLap,
DonGiaPhat=@DonGiaPhat,
MaNhanVien=@MaNV,
MaSach=@MaSach
WHERE MaPhieuNhacTra=@MaPhieuNhacTra
END";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
string mapnt, manv, mathe, masach, ghichu;
double dongiaphat;
DateTime ngaylap;
mapnt = txtmapnt.Text;
ghichu = txtghichu.Text;
manv = cbomanv.SelectedValue.ToString();
mathe = cbomathe.SelectedValue.ToString();
masach = cbomasach.SelectedValue.ToString();
ngaylap = DateTime.Parse(dtpngaylap.Value.ToString());
if (txtdgp.Text == "")
{
dongiaphat = 0;
}
else dongiaphat = double.Parse(txtdgp.Text);
cmd.Parameters.Add("@MaPhieuNhacTra", mapnt);
cmd.Parameters.Add("@NgayLap", ngaylap);
cmd.Parameters.Add("@DonGiaPhat", dongiaphat);
cmd.Parameters.Add("@MaNhanVien", manv);
cmd.Parameters.Add("@MaSach", masach);
cmd.Parameters.Add("@MaTheThuVien", mathe);
cmd.Parameters.Add("@GhiChu", ghichu);
try
{
cmd.Parameters.Add("@kq",
SqlDbType.Int).Direction =
ParameterDirection.ReturnValue;
conn.Open();
cmd.ExecuteNonQuery();
int kq = (int)cmd.Parameters["@kq"].Value;
if (kq == 1)
{
lblthongbaopnt.ForeColor = Color.Red;
lblthongbaopnt.Text = "Không tồn tại PNT";
return;
}
else if (kq == 2)
{
lblthongbaopnt.ForeColor = Color.Red;
lblthongbaopnt.Text = "Không tồn tại nhân viên";
return;
}
else if (kq == 3)
{
lblthongbaopnt.ForeColor = Color.Red;
lblthongbaopnt.Text = "Không tồn tại Sinh Viên";
return;
}
else if (kq == 4)
{
lblthongbaopnt.ForeColor = Color.Red;
lblthongbaopnt.Text = "Không tồn tại Sách";
return;
}
else
{
lblthongbaopnt.ForeColor = Color.Green;
lblthongbaopnt.Text = "Sửa Thành công";
}
}
catch (Exception ex)
{
MessageBox.Show("Loi kg them duoc vi" + ex.Message);
}
finally
{
if (conn != null)
conn.Close();
}
}
private void btnsuapnt_Click(object sender, EventArgs e)
{
huy_lienketdulieu();
suaphieunhactra();
loadphieunhactra();
lienket_dulieu();
}
private void xoaphieunhactra()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = @"@MaPhieuNhacTra CHAR(10)
BEGIN
DELETE FROM PHIEUNHACTRA
WHERE @MaPhieuNhacTra=MaPhieuNhacTra
END";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
string mapnt;
mapnt = txtmapnt.Text;
cmd.Parameters.Add("@MaPhieuNhacTra", mapnt);
DialogResult kq;
kq = MessageBox.Show("Bạn Thật Sự Muốn Xóa", "Chú Ý", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (kq == DialogResult.Yes)
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
lblthongbaopnt.ForeColor = Color.Green;
lblthongbaopnt.Text = "Xóa Thành Công";
}
private void btnxoapnt_Click(object sender, EventArgs e)
{
huy_lienketdulieu();
xoaphieunhactra();
loadphieunhactra();
lienket_dulieu();
}
private void txtdgp_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 45 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 45 && e.KeyChar != 46)
{
e.Handled = true;
lblthongbaopnt.ForeColor = Color.Red;
lblthongbaopnt.Text = "Không được nhập chữ";
}
}
}
}