private void btnLuu_Click(object sender, EventArgs e) { try { var db = new MyDBContextDataContext(); var kiemtra = (from s in db.tbKhoBTP_TPs where s.MaPhieu == MaPhieutxt.Text select s).ToList(); //var scd = db.tbKhoBTP_TPs.Where(s => s.SCD == _SCD).ToList(); //if (kiemtra.Count > 0 && _MaPhieu == "mới" && _flagluu == 1) //{ // MessageBox.Show("Mã phiếu đã có, hệ thống tự động thay đổi Mã phiếu mới"); // var maphieu = "PXK" + DateTime.Now.ToString("ddMMyyHHmmss").Substring(2, 4) + "/"; // dt = kbtpCtr.GetData_MaPhieu(maphieu); // MaPhieutxt.Text = maphieu + kbtpCtr.SinhMaTuDong_MaPhieu(dt); // _MaPhieu = MaPhieutxt.Text; //} var gioihan = DateTime.Today - NgayNhaptxt.DateTime; if (!string.IsNullOrEmpty(KhoCongTytxt.Text)) { if (!string.IsNullOrEmpty(txtBoPhan.Text)) { if ((int)gioihan.TotalDays <= 100 && gioihan.TotalDays >= 0) { if (kiemtra.Count == 0 && _MaPhieu == "mới" && _flagluu == 1 || kiemtra.Count > 0 && _MaPhieu != "mới" && _flagluu == 2) { DataRow dr; if (_flagluu == 2) { kbtpCtr.DelData("MaPhieu", _MaPhieu); } for (var i = 0; i < gridView1.RowCount - 1; i++) { var IDKho = "/" + DateTime.Now.ToString("ddMMyyHHmmss").Substring(2, 4) + "N"; dt = kbtpCtr.GetData_IDKhoBTP(IDKho); dr = gridView1.GetDataRow(i); var lst = (from s in db.LoadData_DonSanXuat_LanhLieu_DanhSachDonHang_NhapBTP() where s.SCD == dr["SCD"].ToString() select s).Single(); var kbtp = new tbKhoBTP_TP(); kbtp.IDKhoBTP_TP = kbtpCtr.SinhMaTuDong(dt) + IDKho; kbtp.MaPhieu = MaPhieutxt.Text; kbtp.SCD = dr["SCD"].ToString(); kbtp.Lo = txtlo.Text; kbtp.NhapXuat = "Nhập"; kbtp.Kho = KhoCongTytxt.Text; kbtp.Ngay = NgayNhaptxt.DateTime; kbtp.LoaiSanPham = lst.LoaiSanPham; kbtp.MaDonHang = lst.MaDonHang; kbtp.TenKhachHang = lst.TenKhachHang; kbtp.TenSanPham = dr["TenSanPham"].ToString(); if (Convert.ToInt32(dr["SoLuongNhapKhachHang"].ToString()) > 0) { kbtp.SoLuongNhapKhachHang = (int)dr["SoLuongNhapKhachHang"]; } if (!string.IsNullOrEmpty(dr["SoLuongNhapCongTy"].ToString())) { kbtp.SoLuongNhapCongTy = Convert.ToInt32(dr["SoLuongNhapCongTy"].ToString()); } kbtp.DonViTinh = dr["DonViTinh"].ToString(); kbtp.KichThuoc = lst.KichThuoc; kbtp.KhoGiayIn = lst.KhoGiayIn; kbtp.BoPhan = txtBoPhan.Text; kbtp.GhiChu = dr["GhiChu"].ToString(); kbtp.NhanVien = nvObj.Tennhanvien; var ll = (from s in db.tbLanhLieus where s.IDLanhLieu == dr["SCD"].ToString() select s) .ToList(); foreach (var item in ll) { item.XacNhanLanhLieu = 2; } db.tbKhoBTP_TPs.InsertOnSubmit(kbtp); db.SubmitChanges(); } switch (_flagluu) { case 1: MessageBox.Show("Thêm Thành Công"); break; case 2: MessageBox.Show("Cập nhật thành công"); break; } } else { MessageBox.Show("Mã phiếu đã tồn tại , vui lòng tạo mã phiếu mới"); } } else { MessageBox.Show("Dữ liệu đã vượt quá thời gian cho phép Lưu kho"); } } else { MessageBox.Show("Vui lòng nhập bộ phận"); } } else { MessageBox.Show("Vui lòng nhập kho công ty"); } } catch (Exception ex) { MessageBox.Show("Lỗi: " + ex.Message, "Lỗi"); //null } }