public void XoaThuePhong(DTOThuePhong dtoTP) { using (PhongTroDBDataContext pt = new PhongTroDBDataContext()) { pt.ThuePhong_Xoa(dtoTP.maphong, dtoTP.makhach, dtoTP.mahopdong); pt.SubmitChanges(); } }
public List <DTOThuePhong> GetAll() { using (PhongTroDBDataContext pt = new PhongTroDBDataContext()) { List <DTOThuePhong> liDtoTP = new List <DTOThuePhong>(); foreach (ThuePhong tp in pt.ThuePhongs) { DTOThuePhong dtoTP = new DTOThuePhong(); dtoTP.maphong = tp.MaPhong; dtoTP.makhach = tp.MaKhach; dtoTP.mahopdong = tp.MaHopDong; dtoTP.chuphong = tp.ChuPhong; liDtoTP.Add(dtoTP); } return(liDtoTP); } }
private void btnLuuHD_Click(object sender, EventArgs e) { DTOHopDong hd = new DTOHopDong(); hd.ngaythue = dtpNgayThue.Value; if (chbNgayTra.Checked) { hd.ngaytra = null; } else { hd.ngaytra = dtpNgayTra.Value; } if (chbNgayLamHD.Checked) { hd.ngaylamhopdong = null; } else { hd.ngaylamhopdong = dtpNgayLap.Value; } hd.tiencoc = Convert.ToInt64(nmrTienCoc.Value); hd.giathue = Convert.ToInt64(nmrGiaThue.Value); hd.chisodien = Convert.ToInt64(nmrSoDien.Value); hd.chisonuoc = Convert.ToInt64(nmrSoNuoc.Value); if (txtGhiChu.Text.Trim() == string.Empty) { hd.ghichu = null; } else { hd.ghichu = txtGhiChu.Text.Trim(); } try { //Thêm Hợp đồng. if (state == 0) { if (balHD.KiemTraHopLeHopDong(hd, balHD.HopDongHienTaiVaTuongLai(Convert.ToInt32(cboPhong.SelectedValue))) == false) { MessageBox.Show("Với Ngày Thuê như trên thì Phòng không trống.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop); } else { balHD.ThemHopDong(hd); #region Chỉ số tháng của phòng ngay ngày thuê hợp đồng. DTOChiSoThang cst = new DTOChiSoThang() { maphong = Convert.ToInt32(cboPhong.SelectedValue), ngaycapnhat = hd.ngaythue, chisodien = Convert.ToInt64(nmrSoDien.Value), chisonuoc = Convert.ToInt64(nmrSoNuoc.Value) }; //Có rồi thì cập nhật. if (balCST.CheckCST(cst.maphong, cst.ngaycapnhat)) { balCST.SuaChiSoThang(cst); } //Chưa có thì thêm mới. else { balCST.ThemChiSoThang(cst); } #endregion //Lấy mã hợp đồng. hd.mahopdong = balHD.MaHopDongSearchByAll(hd); #region Thêm Chịu cước phí. foreach (DataGridViewRow dgr in dgvCuocPhi.Rows) { DTOChiuCuocPhi ccp = new DTOChiuCuocPhi() { mahopdong = hd.mahopdong, macuocphi = Convert.ToInt32(dgr.Cells["macuocphi"].Value), soluong = Convert.ToInt32(dgr.Cells["soluong"].Value) }; balCCP.ThemChiuCuocPhi(ccp); } #endregion #region Thêm Thuê Phòng. foreach (DataGridViewRow dgr in dgvKhach.Rows) { DTOThuePhong dtoTP = new DTOThuePhong() { maphong = Convert.ToInt32(cboPhong.SelectedValue.ToString()), makhach = Convert.ToInt64(dgr.Cells["makhach"].Value), mahopdong = hd.mahopdong }; if (Convert.ToInt64(cboKhachThue.SelectedValue.ToString()) == dtoTP.makhach) { dtoTP.chuphong = true; } else { dtoTP.chuphong = false; } balTP.ThemThuePhong(dtoTP); } #endregion MessageBox.Show("Tạo Hợp đồng mới thành công.", "Thông báo"); } } //Sửa Hợp đồng. else if (state == -1) { hd.mahopdong = mahopdong; balHD.SuaHopDong(hd); #region Chỉ số tháng của phòng ngay ngày thuê hợp đồng. DTOChiSoThang cst = new DTOChiSoThang() { maphong = Convert.ToInt32(cboPhong.SelectedValue), ngaycapnhat = hd.ngaythue, chisodien = Convert.ToInt64(nmrSoDien.Value), chisonuoc = Convert.ToInt64(nmrSoNuoc.Value) }; //Có rồi thì cập nhật. if (balCST.CheckCST(cst.maphong, cst.ngaycapnhat)) { balCST.SuaChiSoThang(cst); } //Chưa có thì thêm mới. else { balCST.ThemChiSoThang(cst); } #endregion #region Sửa Chịu cước phí. List <DTOChiuCuocPhi> liCCP_Moi = new List <DTOChiuCuocPhi>(); foreach (DataGridViewRow dgr in dgvCuocPhi.Rows) { DTOChiuCuocPhi ccp = new DTOChiuCuocPhi() { mahopdong = hd.mahopdong, macuocphi = Convert.ToInt32(dgr.Cells["macuocphi"].Value), soluong = Convert.ToInt32(dgr.Cells["soluong"].Value) }; liCCP_Moi.Add(ccp); } //Lấy danh sách chịu cước phí cũ. List <DTOChiuCuocPhi> liCCP_Cu = balCCP.ChiuCuocPhiTuMaHopDong(hd.mahopdong); //So sánh list mới và cũ. [Chưa có thì thêm mới-Có thì cập nhật] foreach (DTOChiuCuocPhi ccp_moi in liCCP_Moi) { bool tontai = false; foreach (DTOChiuCuocPhi ccp_cu in liCCP_Cu) { if (ccp_moi.macuocphi == ccp_cu.macuocphi) { tontai = true; break; } } if (tontai == true) { balCCP.SuaChiuCuocPhi(ccp_moi); } else { balCCP.ThemChiuCuocPhi(ccp_moi); } } //So sánh list cũ và mới. Nếu mới không có thì xóa của cũ. foreach (DTOChiuCuocPhi ccp_cu in liCCP_Cu) { bool tontai = false; foreach (DTOChiuCuocPhi ccp_moi in liCCP_Moi) { if (ccp_cu.macuocphi == ccp_moi.macuocphi) { tontai = true; break; } } if (tontai == false) { balCCP.XoaChiuCuocPhi(ccp_cu); } } #endregion #region Sửa Thuê Phòng. List <DTOThuePhong> liTP_Moi = new List <DTOThuePhong>(); foreach (DataGridViewRow dgr in dgvKhach.Rows) { DTOThuePhong dtoTP = new DTOThuePhong() { maphong = Convert.ToInt32(cboPhong.SelectedValue.ToString()), makhach = Convert.ToInt64(dgr.Cells["makhach"].Value), mahopdong = hd.mahopdong }; if (Convert.ToInt64(cboKhachThue.SelectedValue.ToString()) == dtoTP.makhach) { dtoTP.chuphong = true; } else { dtoTP.chuphong = false; } liTP_Moi.Add(dtoTP); } //Thực hiện giống Chịu Cước Phí List <DTOThuePhong> liTP_Cu = balTP.ThuePhongTuMaHopDong(hd.mahopdong); foreach (DTOThuePhong tp_moi in liTP_Moi) { bool tontai = false; foreach (DTOThuePhong tp_cu in liTP_Cu) { if (tp_moi.mahopdong == tp_cu.mahopdong && tp_moi.maphong == tp_cu.maphong && tp_moi.makhach == tp_cu.makhach) { tontai = true; break; } } if (tontai == true) { balTP.SuaThuePhong(tp_moi); } else { balTP.ThemThuePhong(tp_moi); } } foreach (DTOThuePhong tp_cu in liTP_Cu) { bool tontai = false; foreach (DTOThuePhong tp_moi in liTP_Moi) { if (tp_moi.mahopdong == tp_cu.mahopdong && tp_moi.maphong == tp_cu.maphong && tp_moi.makhach == tp_cu.makhach) { tontai = true; break; } } if (tontai == false) { balTP.XoaThuePhong(tp_cu); } } #endregion MessageBox.Show("Cập nhật thông tin Hợp đồng thành công!", "Thông báo"); } } catch (Exception ex) { MessageBox.Show("Thao tác thất bại.\n" + ex.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop); } }