private void ThemTieuThu_Load(object sender, EventArgs e)
 {
     try
     {
         dtpThoiGian.CustomFormat = "dd/MM/yyyy";
         if (EditTieuThu == null)
         {
             var lstTieuThu = new TieuThuLDM().GetElements(mCustomerID);
             if (lstTieuThu.Count > 0 && EditTieuThu == null)
             {
                 dtpThoiGian.Value = lstTieuThu.Last().Time.Value.AddMonths(1);
             }
         }
         else
         {
             txtChiSoDienMoi.Text = EditTieuThu.NewElectricIndex.ToString();
             txtChiSoNuocMoi.Text = EditTieuThu.NewWaterIndex.ToString();
             dtpThoiGian.Value    = EditTieuThu.Time.Value;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Exemple #2
0
 private void dgvKhachHang_CellClick(object sender, DataGridViewCellEventArgs e)
 {
     try
     {
         if ((mUser.isAdmin == 1 || mUR.isRemove.Value) && e.ColumnIndex == (tempXoa - 14)) // click nut xoa
         {
             var result = MessageBox.Show($" Dữ liệu về tài khoản ,tiêu thụ cũng sẽ bị xóa \n Bạn có muốn xóa khách hàng {dgvKhachHang["TenKhachHang", e.RowIndex].Value.ToString().ToUpper()}?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
             if (result == DialogResult.Yes)
             {
                 if (new KhachHangLDM().Delete(int.Parse(dgvKhachHang["MaKhachHang", e.RowIndex].Value.ToString())))
                 {
                     if (new UserLDM().Delete(int.Parse(dgvKhachHang["UserID", e.RowIndex].Value.ToString())))
                     {
                         var lstTieuThu = new TieuThuLDM().GetElements(int.Parse(dgvKhachHang["MaKhachHang", e.RowIndex].Value.ToString()));
                         var isvalid    = true;
                         foreach (var item in lstTieuThu)
                         {
                             if (new TieuThuLDM().Delete(item.ID) == false)
                             {
                                 isvalid = false;
                                 break;
                             }
                         }
                         if (isvalid == true)
                         {
                             ReLoadDataKhachHang();
                             MessageBox.Show("Xóa thành công!");
                         }
                         else
                         {
                             MessageBox.Show("Xóa thất bại!");
                         }
                     }
                 }
             }
         }
         if ((mUser.isAdmin == 1 || mUR.isEdit.Value) && e.ColumnIndex == (tempSua - 14))  // click nut sua
         {
             Customer objCustomer = new Customer();
             objCustomer.User             = new User();
             objCustomer.UserID           = int.Parse(dgvKhachHang["UserID", e.RowIndex].Value.ToString() == null ? "-1" : dgvKhachHang["UserID", e.RowIndex].Value.ToString());
             objCustomer.PriceID          = int.Parse(dgvKhachHang["PriceID", e.RowIndex].Value.ToString() == null ? "-1" : dgvKhachHang["PriceID", e.RowIndex].Value.ToString());
             objCustomer.ID               = int.Parse(dgvKhachHang["MaKhachHang", e.RowIndex].Value.ToString());
             objCustomer.PassportID       = dgvKhachHang["PassPortID", e.RowIndex].Value.ToString();
             objCustomer.User.DisplayName = dgvKhachHang["TenKhachHang", e.RowIndex].Value.ToString();
             objCustomer.User.Gender      = dgvKhachHang["Gender", e.RowIndex].Value.ToString() == "Nam" ? true : false;
             objCustomer.User.DateOfBirth = DateTime.ParseExact(dgvKhachHang["DateOfBirth", e.RowIndex].Value.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
             objCustomer.User.Phone       = dgvKhachHang["PhoneNumber", e.RowIndex].Value.ToString();
             objCustomer.User.Email       = dgvKhachHang["EmailKH", e.RowIndex].Value.ToString();
             objCustomer.User.Address     = dgvKhachHang["AddressKH", e.RowIndex].Value.ToString();
             SuaKhachHang mSuaKhachHang = new SuaKhachHang(mUser, objCustomer);
             this.Parent.Parent.Enabled = false;
             mSuaKhachHang.UCCuaSoChinh = this;
             mSuaKhachHang.Show();
         }
         if ((mUser.isAdmin == 1 || new User_RoleLDM().GetElements(mUser.ID).Where(x => x.Role.RoleName == "Quản lý tiêu thụ").First().isView.Value) && e.ColumnIndex == (tempQLTT - 14)) // click nut QL Tieu thu
         {
             UCquanLyTieuThu mUCQuanLyTieuThu = new UCquanLyTieuThu(mUser, int.Parse(dgvKhachHang["MaKhachHang", e.RowIndex].Value.ToString()), frmSource);
             mUCQuanLyTieuThu.mUCCuaSoChinh = this;
             this.Parent.Controls.Add(mUCQuanLyTieuThu);
             this.Parent.Controls.Remove(this);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
 private void btnLuuLai_Click(object sender, EventArgs e)
 {
     try
     {
         Consume newConsume = new Consume();
         var     lstTieuThu = new TieuThuLDM().GetElements(mCustomerID);
         erpThemTieuThu.Clear();
         if (EditTieuThu == null)
         {
             if (string.IsNullOrEmpty(txtChiSoDienMoi.Text.Trim()))
             {
                 erpThemTieuThu.SetError(txtChiSoDienMoi, "Vui lòng điền trường này!");
                 txtChiSoDienMoi.Focus();
             }
             else
             {
                 if (lstTieuThu.Count > 0 && int.Parse(txtChiSoDienMoi.Text.Trim()) <= lstTieuThu.Last().NewElectricIndex)
                 {
                     erpThemTieuThu.SetError(txtChiSoDienMoi, $"Chỉ số phải lớn hơn {lstTieuThu.Last().NewElectricIndex}!");
                     txtChiSoDienMoi.Focus();
                 }
                 else
                 {
                     newConsume.NewElectricIndex = int.Parse(txtChiSoDienMoi.Text.Trim());
                     if (string.IsNullOrEmpty(txtChiSoNuocMoi.Text.Trim()))
                     {
                         erpThemTieuThu.SetError(txtChiSoNuocMoi, "Vui lòng điền trường này!");
                         txtChiSoNuocMoi.Focus();
                     }
                     else
                     {
                         if (lstTieuThu.Count > 0 && int.Parse(txtChiSoNuocMoi.Text.Trim()) <= lstTieuThu.Last().NewWaterIndex)
                         {
                             erpThemTieuThu.SetError(txtChiSoNuocMoi, $"Chỉ số phải lớn hơn {lstTieuThu.Last().NewWaterIndex}!");
                             txtChiSoNuocMoi.Focus();
                         }
                         else
                         {
                             newConsume.NewWaterIndex = int.Parse(txtChiSoNuocMoi.Text.Trim());
                             if (dtpThoiGian.Value == null)
                             {
                                 erpThemTieuThu.SetError(dtpThoiGian, "Vui lòng điền trường này!");
                                 dtpThoiGian.Focus();
                             }
                             else
                             {
                                 if ((lstTieuThu.Count > 0) && lstTieuThu.Last().Time.Value.Date == DateTime.Now.Date)
                                 {
                                     MessageBox.Show("Không thể thêm tiêu thụ\nBạn đã nhập tiêu thụ có thời gian là hôm nay rồi!");
                                     this.Close();
                                 }
                                 else
                                 {
                                     if (lstTieuThu.Count > 0 && dtpThoiGian.Value.Date <= lstTieuThu.Last().Time.Value.Date)
                                     {
                                         erpThemTieuThu.SetError(dtpThoiGian, $"Thời gian phải sau ngày {lstTieuThu.Last().Time.Value.ToString("dd/MM/yyyy")}!");
                                         dtpThoiGian.Focus();
                                     }
                                     else
                                     {
                                         if (dtpThoiGian.Value.Date > DateTime.Now.Date)
                                         {
                                             erpThemTieuThu.SetError(dtpThoiGian, $"Thời gian phải trước ngày {DateTime.Now.ToString("dd/MM/yyyy")}!");
                                             dtpThoiGian.Focus();
                                         }
                                         else
                                         {
                                             newConsume.Time        = dtpThoiGian.Value.Date;
                                             newConsume.CreatedByID = mUser.ID;
                                             newConsume.CreatedDate = DateTime.Now.Date;
                                             if (lstTieuThu.Count > 0)
                                             {
                                                 newConsume.OldElectricIndex = lstTieuThu.Last().NewElectricIndex;
                                                 newConsume.OldWaterIndex    = lstTieuThu.Last().NewWaterIndex;
                                             }
                                             else
                                             {
                                                 newConsume.OldElectricIndex = 0;
                                                 newConsume.OldWaterIndex    = 0;
                                             }
                                             newConsume.CustomerID      = mCustomerID;
                                             newConsume.ElectricConsume = newConsume.NewElectricIndex - newConsume.OldElectricIndex;
                                             newConsume.WaterConsume    = newConsume.NewWaterIndex - newConsume.OldWaterIndex;
                                             if (new TieuThuLDM().Insert(newConsume) != null)
                                             {
                                                 MessageBox.Show("Nhập mới tiêu thụ thành công!");
                                                 mUCQuanLyTieuThu.ReLoadDataTieuThu();
                                                 mUCQuanLyTieuThu.Parent.Parent.Enabled = true;
                                             }
                                             else
                                             {
                                                 MessageBox.Show("Nhập mới tiêu thụ thất bại!");
                                             }
                                             this.Close();
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         else
         {
             var     current = lstTieuThu.Where(x => x.ID == EditTieuThu.ID).First();
             int     index   = lstTieuThu.FindIndex(x => x.ID == EditTieuThu.ID);
             Consume obj     = new Consume();
             if (lstTieuThu.Count > 2)
             {
                 if (current == lstTieuThu.First())
                 {
                     var behind = lstTieuThu[index + 1];
                     if (string.IsNullOrEmpty(txtChiSoDienMoi.Text.Trim()))
                     {
                         erpThemTieuThu.SetError(txtChiSoDienMoi, "Vui lòng điền trường này!");
                         txtChiSoDienMoi.Focus();
                     }
                     else
                     {
                         if (lstTieuThu.Count > 0 && int.Parse(txtChiSoDienMoi.Text.Trim()) <= current.OldElectricIndex)
                         {
                             erpThemTieuThu.SetError(txtChiSoDienMoi, $"Chỉ số phải lớn hơn {current.OldElectricIndex}!");
                             txtChiSoDienMoi.Focus();
                         }
                         else
                         {
                             if (lstTieuThu.Count > 0 && int.Parse(txtChiSoDienMoi.Text.Trim()) >= behind.NewElectricIndex)
                             {
                                 erpThemTieuThu.SetError(txtChiSoDienMoi, $"Chỉ số phải nhỏ hơn {behind.NewElectricIndex}!");
                                 txtChiSoDienMoi.Focus();
                             }
                             else
                             {
                                 obj.NewElectricIndex = int.Parse(txtChiSoDienMoi.Text.Trim());
                                 if (string.IsNullOrEmpty(txtChiSoNuocMoi.Text.Trim()))
                                 {
                                     erpThemTieuThu.SetError(txtChiSoNuocMoi, "Vui lòng điền trường này!");
                                     txtChiSoNuocMoi.Focus();
                                 }
                                 else
                                 {
                                     if (lstTieuThu.Count > 0 && int.Parse(txtChiSoNuocMoi.Text.Trim()) <= current.OldWaterIndex)
                                     {
                                         erpThemTieuThu.SetError(txtChiSoNuocMoi, $"Chỉ số phải lớn hơn {current.OldWaterIndex}!");
                                         txtChiSoNuocMoi.Focus();
                                     }
                                     else
                                     {
                                         if (lstTieuThu.Count > 0 && int.Parse(txtChiSoNuocMoi.Text.Trim()) >= behind.NewWaterIndex)
                                         {
                                             erpThemTieuThu.SetError(txtChiSoNuocMoi, $"Chỉ số phải nhỏ hơn {behind.NewWaterIndex}!");
                                             txtChiSoNuocMoi.Focus();
                                         }
                                         else
                                         {
                                             obj.NewWaterIndex = int.Parse(txtChiSoNuocMoi.Text.Trim());
                                             if (dtpThoiGian.Value.Date >= behind.Time.Value.Date)
                                             {
                                                 erpThemTieuThu.SetError(dtpThoiGian, $"Thời gian phải trước ngày {behind.Time.Value.Date.ToString("dd/MM/yyyy")}!");
                                                 dtpThoiGian.Focus();
                                             }
                                             else
                                             {
                                                 obj.ElectricConsume = obj.NewElectricIndex - current.OldElectricIndex;
                                                 obj.ID           = EditTieuThu.ID;
                                                 obj.ModifiedByID = mUser.ID;
                                                 obj.ModifiedDate = DateTime.Now.Date;
                                                 obj.Time         = dtpThoiGian.Value;
                                                 obj.WaterConsume = obj.NewWaterIndex - current.OldWaterIndex;
                                                 if (new TieuThuLDM().Update(obj))
                                                 {
                                                     MessageBox.Show("Cập nhật tiêu thụ thành công!");
                                                     mUCQuanLyTieuThu.ReLoadDataTieuThu();
                                                 }
                                                 else
                                                 {
                                                     MessageBox.Show("Cập nhật tiêu thụ thất bại!");
                                                 }
                                                 this.Close();
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
                 else
                 {
                     if (current == lstTieuThu.Last())
                     {
                         var previous = lstTieuThu[index - 1];
                         if (string.IsNullOrEmpty(txtChiSoDienMoi.Text.Trim()))
                         {
                             erpThemTieuThu.SetError(txtChiSoDienMoi, "Vui lòng điền trường này!");
                             txtChiSoDienMoi.Focus();
                         }
                         else
                         {
                             if (lstTieuThu.Count > 0 && int.Parse(txtChiSoDienMoi.Text.Trim()) <= previous.NewElectricIndex)
                             {
                                 erpThemTieuThu.SetError(txtChiSoDienMoi, $"Chỉ số phải lớn hơn {previous.NewElectricIndex}!");
                                 txtChiSoDienMoi.Focus();
                             }
                             else
                             {
                                 obj.NewElectricIndex = int.Parse(txtChiSoDienMoi.Text.Trim());
                                 if (string.IsNullOrEmpty(txtChiSoNuocMoi.Text.Trim()))
                                 {
                                     erpThemTieuThu.SetError(txtChiSoNuocMoi, "Vui lòng điền trường này!");
                                     txtChiSoNuocMoi.Focus();
                                 }
                                 else
                                 {
                                     if (lstTieuThu.Count > 0 && int.Parse(txtChiSoNuocMoi.Text.Trim()) <= previous.NewWaterIndex)
                                     {
                                         erpThemTieuThu.SetError(txtChiSoNuocMoi, $"Chỉ số phải lớn hơn {previous.NewWaterIndex}!");
                                         txtChiSoNuocMoi.Focus();
                                     }
                                     else
                                     {
                                         obj.NewWaterIndex = int.Parse(txtChiSoNuocMoi.Text.Trim());
                                         if (dtpThoiGian.Value.Date <= previous.Time.Value.Date)
                                         {
                                             erpThemTieuThu.SetError(dtpThoiGian, $"Thời gian phải sau ngày {previous.Time.Value.Date.ToString("dd/MM/yyyy")}!");
                                             dtpThoiGian.Focus();
                                         }
                                         else
                                         {
                                             if (dtpThoiGian.Value.Date > DateTime.Now.Date)
                                             {
                                                 erpThemTieuThu.SetError(dtpThoiGian, $"Thời gian không được vượt quá thời gian hiện tại!");
                                                 dtpThoiGian.Focus();
                                             }
                                             else
                                             {
                                                 obj.ElectricConsume = obj.NewElectricIndex - current.OldElectricIndex;
                                                 obj.ID           = EditTieuThu.ID;
                                                 obj.ModifiedByID = mUser.ID;
                                                 obj.ModifiedDate = DateTime.Now;
                                                 obj.Time         = dtpThoiGian.Value;
                                                 obj.WaterConsume = obj.NewWaterIndex - current.OldWaterIndex;
                                                 if (new TieuThuLDM().Update(obj))
                                                 {
                                                     MessageBox.Show("Cập nhật tiêu thụ thành công!");
                                                     mUCQuanLyTieuThu.ReLoadDataTieuThu();
                                                 }
                                                 else
                                                 {
                                                     MessageBox.Show("Cập nhật tiêu thụ thất bại!");
                                                 }
                                                 this.Close();
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     else
                     {
                         var behind   = lstTieuThu[index + 1];
                         var previous = lstTieuThu[index - 1];
                         if (string.IsNullOrEmpty(txtChiSoDienMoi.Text.Trim()))
                         {
                             erpThemTieuThu.SetError(txtChiSoDienMoi, "Vui lòng điền trường này!");
                             txtChiSoDienMoi.Focus();
                         }
                         else
                         {
                             if (lstTieuThu.Count > 0 && int.Parse(txtChiSoDienMoi.Text.Trim()) <= previous.NewElectricIndex)
                             {
                                 erpThemTieuThu.SetError(txtChiSoDienMoi, $"Chỉ số phải lớn hơn {previous.NewElectricIndex}!");
                                 txtChiSoDienMoi.Focus();
                             }
                             else
                             {
                                 if (lstTieuThu.Count > 0 && int.Parse(txtChiSoDienMoi.Text.Trim()) >= behind.NewElectricIndex)
                                 {
                                     erpThemTieuThu.SetError(txtChiSoDienMoi, $"Chỉ số phải nhỏ hơn {behind.NewElectricIndex}!");
                                     txtChiSoDienMoi.Focus();
                                 }
                                 else
                                 {
                                     obj.NewElectricIndex = int.Parse(txtChiSoDienMoi.Text.Trim());
                                     if (string.IsNullOrEmpty(txtChiSoNuocMoi.Text.Trim()))
                                     {
                                         erpThemTieuThu.SetError(txtChiSoNuocMoi, "Vui lòng điền trường này!");
                                         txtChiSoNuocMoi.Focus();
                                     }
                                     else
                                     {
                                         if (lstTieuThu.Count > 0 && int.Parse(txtChiSoNuocMoi.Text.Trim()) <= previous.NewWaterIndex)
                                         {
                                             erpThemTieuThu.SetError(txtChiSoNuocMoi, $"Chỉ số phải lớn hơn {previous.NewWaterIndex}!");
                                             txtChiSoNuocMoi.Focus();
                                         }
                                         else
                                         {
                                             if (lstTieuThu.Count > 0 && int.Parse(txtChiSoNuocMoi.Text.Trim()) >= behind.NewWaterIndex)
                                             {
                                                 erpThemTieuThu.SetError(txtChiSoNuocMoi, $"Chỉ số phải nhỏ hơn {behind.NewWaterIndex}!");
                                                 txtChiSoNuocMoi.Focus();
                                             }
                                             else
                                             {
                                                 obj.NewWaterIndex = int.Parse(txtChiSoNuocMoi.Text.Trim());
                                                 if (dtpThoiGian.Value.Date <= previous.Time.Value.Date)
                                                 {
                                                     erpThemTieuThu.SetError(dtpThoiGian, $"Thời gian phải sau ngày {previous.Time.Value.Date.ToString("dd/MM/yyyy")}!");
                                                     dtpThoiGian.Focus();
                                                 }
                                                 else
                                                 {
                                                     if (dtpThoiGian.Value.Date >= behind.Time.Value.Date)
                                                     {
                                                         erpThemTieuThu.SetError(dtpThoiGian, $"Thời gian phải trước ngày {behind.Time.Value.Date.ToString("dd/MM/yyyy")}!");
                                                         dtpThoiGian.Focus();
                                                     }
                                                     else
                                                     {
                                                         obj.ElectricConsume = obj.NewElectricIndex - current.OldElectricIndex;
                                                         obj.ID           = EditTieuThu.ID;
                                                         obj.ModifiedByID = mUser.ID;
                                                         obj.ModifiedDate = DateTime.Now.Date;
                                                         obj.Time         = dtpThoiGian.Value;
                                                         obj.WaterConsume = obj.NewWaterIndex - current.OldWaterIndex;
                                                         if (new TieuThuLDM().Update(obj))
                                                         {
                                                             MessageBox.Show("Cập nhật tiêu thụ thành công!");
                                                             mUCQuanLyTieuThu.ReLoadDataTieuThu();
                                                         }
                                                         else
                                                         {
                                                             MessageBox.Show("Cập nhật tiêu thụ thất bại!");
                                                         }
                                                         this.Close();
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             else
             {
                 if (lstTieuThu.Count > 1)
                 {
                     if (current == lstTieuThu.First())
                     {
                         var behind = lstTieuThu[index + 1];
                         if (string.IsNullOrEmpty(txtChiSoDienMoi.Text.Trim()))
                         {
                             erpThemTieuThu.SetError(txtChiSoDienMoi, "Vui lòng điền trường này!");
                             txtChiSoDienMoi.Focus();
                         }
                         else
                         {
                             if (lstTieuThu.Count > 0 && int.Parse(txtChiSoDienMoi.Text.Trim()) <= current.OldElectricIndex)
                             {
                                 erpThemTieuThu.SetError(txtChiSoDienMoi, $"Chỉ số phải lớn hơn {current.OldElectricIndex}!");
                                 txtChiSoDienMoi.Focus();
                             }
                             else
                             {
                                 if (lstTieuThu.Count > 0 && int.Parse(txtChiSoDienMoi.Text.Trim()) >= behind.NewElectricIndex)
                                 {
                                     erpThemTieuThu.SetError(txtChiSoDienMoi, $"Chỉ số phải nhỏ hơn {behind.NewElectricIndex}!");
                                     txtChiSoDienMoi.Focus();
                                 }
                                 else
                                 {
                                     obj.NewElectricIndex = int.Parse(txtChiSoDienMoi.Text.Trim());
                                     if (string.IsNullOrEmpty(txtChiSoNuocMoi.Text.Trim()))
                                     {
                                         erpThemTieuThu.SetError(txtChiSoNuocMoi, "Vui lòng điền trường này!");
                                         txtChiSoNuocMoi.Focus();
                                     }
                                     else
                                     {
                                         if (lstTieuThu.Count > 0 && int.Parse(txtChiSoNuocMoi.Text.Trim()) <= current.OldWaterIndex)
                                         {
                                             erpThemTieuThu.SetError(txtChiSoNuocMoi, $"Chỉ số phải lớn hơn {current.OldWaterIndex}!");
                                             txtChiSoNuocMoi.Focus();
                                         }
                                         else
                                         {
                                             if (lstTieuThu.Count > 0 && int.Parse(txtChiSoNuocMoi.Text.Trim()) >= behind.NewWaterIndex)
                                             {
                                                 erpThemTieuThu.SetError(txtChiSoNuocMoi, $"Chỉ số phải nhỏ hơn {behind.NewWaterIndex}!");
                                                 txtChiSoNuocMoi.Focus();
                                             }
                                             else
                                             {
                                                 obj.NewWaterIndex = int.Parse(txtChiSoNuocMoi.Text.Trim());
                                                 if (dtpThoiGian.Value.Date >= behind.Time.Value.Date)
                                                 {
                                                     erpThemTieuThu.SetError(dtpThoiGian, $"Thời gian phải trước ngày {behind.Time.Value.Date.ToString("dd/MM/yyyy")}!");
                                                     dtpThoiGian.Focus();
                                                 }
                                                 else
                                                 {
                                                     obj.ElectricConsume = obj.NewElectricIndex - current.OldElectricIndex;
                                                     obj.ID           = EditTieuThu.ID;
                                                     obj.ModifiedByID = mUser.ID;
                                                     obj.ModifiedDate = DateTime.Now.Date;
                                                     obj.Time         = dtpThoiGian.Value;
                                                     obj.WaterConsume = obj.NewWaterIndex - current.OldWaterIndex;
                                                     if (new TieuThuLDM().Update(obj))
                                                     {
                                                         MessageBox.Show("Cập nhật tiêu thụ thành công!");
                                                         mUCQuanLyTieuThu.ReLoadDataTieuThu();
                                                     }
                                                     else
                                                     {
                                                         MessageBox.Show("Cập nhật tiêu thụ thất bại!");
                                                     }
                                                     this.Close();
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     else
                     {
                         if (current == lstTieuThu.Last())
                         {
                             var previous = lstTieuThu[index - 1];
                             if (string.IsNullOrEmpty(txtChiSoDienMoi.Text.Trim()))
                             {
                                 erpThemTieuThu.SetError(txtChiSoDienMoi, "Vui lòng điền trường này!");
                                 txtChiSoDienMoi.Focus();
                             }
                             else
                             {
                                 if (lstTieuThu.Count > 0 && int.Parse(txtChiSoDienMoi.Text.Trim()) <= previous.NewElectricIndex)
                                 {
                                     erpThemTieuThu.SetError(txtChiSoDienMoi, $"Chỉ số phải lớn hơn {previous.NewElectricIndex}!");
                                     txtChiSoDienMoi.Focus();
                                 }
                                 else
                                 {
                                     obj.NewElectricIndex = int.Parse(txtChiSoDienMoi.Text.Trim());
                                     if (string.IsNullOrEmpty(txtChiSoNuocMoi.Text.Trim()))
                                     {
                                         erpThemTieuThu.SetError(txtChiSoNuocMoi, "Vui lòng điền trường này!");
                                         txtChiSoNuocMoi.Focus();
                                     }
                                     else
                                     {
                                         if (lstTieuThu.Count > 0 && int.Parse(txtChiSoNuocMoi.Text.Trim()) <= previous.NewWaterIndex)
                                         {
                                             erpThemTieuThu.SetError(txtChiSoNuocMoi, $"Chỉ số phải lớn hơn {previous.NewWaterIndex}!");
                                             txtChiSoNuocMoi.Focus();
                                         }
                                         else
                                         {
                                             obj.NewWaterIndex = int.Parse(txtChiSoNuocMoi.Text.Trim());
                                             if (dtpThoiGian.Value.Date <= previous.Time.Value.Date)
                                             {
                                                 erpThemTieuThu.SetError(dtpThoiGian, $"Thời gian phải sau ngày {previous.Time.Value.Date.ToString("dd/MM/yyyy")}!");
                                                 dtpThoiGian.Focus();
                                             }
                                             else
                                             {
                                                 if (dtpThoiGian.Value.Date > DateTime.Now.Date)
                                                 {
                                                     erpThemTieuThu.SetError(dtpThoiGian, $"Thời gian không được vượt quá thời gian hiện tại!");
                                                     dtpThoiGian.Focus();
                                                 }
                                                 else
                                                 {
                                                     obj.ElectricConsume = obj.NewElectricIndex - current.OldElectricIndex;
                                                     obj.ID           = EditTieuThu.ID;
                                                     obj.ModifiedByID = mUser.ID;
                                                     obj.ModifiedDate = DateTime.Now.Date;
                                                     obj.Time         = dtpThoiGian.Value;
                                                     obj.WaterConsume = obj.NewWaterIndex - current.OldWaterIndex;
                                                     if (new TieuThuLDM().Update(obj))
                                                     {
                                                         MessageBox.Show("Cập nhật tiêu thụ thành công!");
                                                         mUCQuanLyTieuThu.ReLoadDataTieuThu();
                                                     }
                                                     else
                                                     {
                                                         MessageBox.Show("Cập nhật tiêu thụ thất bại!");
                                                     }
                                                     this.Close();
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
                 else
                 {
                     if (lstTieuThu.Count > 0)
                     {
                         if (current == lstTieuThu.First())
                         {
                             if (string.IsNullOrEmpty(txtChiSoDienMoi.Text.Trim()))
                             {
                                 erpThemTieuThu.SetError(txtChiSoDienMoi, "Vui lòng điền trường này!");
                                 txtChiSoDienMoi.Focus();
                             }
                             else
                             {
                                 if (lstTieuThu.Count > 0 && int.Parse(txtChiSoDienMoi.Text.Trim()) <= current.OldElectricIndex)
                                 {
                                     erpThemTieuThu.SetError(txtChiSoDienMoi, $"Chỉ số phải lớn hơn {current.OldElectricIndex}!");
                                     txtChiSoDienMoi.Focus();
                                 }
                                 else
                                 {
                                     obj.NewElectricIndex = int.Parse(txtChiSoDienMoi.Text.Trim());
                                     if (string.IsNullOrEmpty(txtChiSoNuocMoi.Text.Trim()))
                                     {
                                         erpThemTieuThu.SetError(txtChiSoNuocMoi, "Vui lòng điền trường này!");
                                         txtChiSoNuocMoi.Focus();
                                     }
                                     else
                                     {
                                         if (lstTieuThu.Count > 0 && int.Parse(txtChiSoNuocMoi.Text.Trim()) <= current.OldWaterIndex)
                                         {
                                             erpThemTieuThu.SetError(txtChiSoNuocMoi, $"Chỉ số phải lớn hơn {current.OldWaterIndex}!");
                                             txtChiSoNuocMoi.Focus();
                                         }
                                         else
                                         {
                                             obj.NewWaterIndex = int.Parse(txtChiSoNuocMoi.Text.Trim());
                                             if (dtpThoiGian.Value.Date > DateTime.Now.Date)
                                             {
                                                 erpThemTieuThu.SetError(dtpThoiGian, $"Thời gian không được vượt quá thời gian hiện tại!");
                                                 dtpThoiGian.Focus();
                                             }
                                             else
                                             {
                                                 obj.ElectricConsume = obj.NewElectricIndex - current.OldElectricIndex;
                                                 obj.ID           = EditTieuThu.ID;
                                                 obj.ModifiedByID = mUser.ID;
                                                 obj.ModifiedDate = DateTime.Now.Date;
                                                 obj.Time         = dtpThoiGian.Value;
                                                 obj.WaterConsume = obj.NewWaterIndex - current.OldWaterIndex;
                                                 if (new TieuThuLDM().Update(obj))
                                                 {
                                                     MessageBox.Show("Cập nhật tiêu thụ thành công!");
                                                     mUCQuanLyTieuThu.ReLoadDataTieuThu();
                                                 }
                                                 else
                                                 {
                                                     MessageBox.Show("Cập nhật tiêu thụ thất bại!");
                                                 }
                                                 this.Close();
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }