public DateTime getThangNamGanNhatThuocHoaDon(int tuyenKHID, int currentMonth, int currentYear) { DateTime ngayThangHoaDon = new DateTime(1, 1, 1); DateTime currentTime = new DateTime(currentYear, currentMonth, 1); DateTime nearestDate = new DateTime(1, 1, 1); var hoaDonThangGanNhat = (from i in db.Hoadonnuocs join kH in db.Khachhangs on i.KhachhangID equals kH.KhachhangID join cT in db.Chitiethoadonnuocs on i.HoadonnuocID equals cT.HoadonnuocID where kH.TuyenKHID == tuyenKHID select new HoaDonNuocHaDong.Models.SoLieuTieuThu.HoaDonNuoc { Thang = i.ThangHoaDon.Value, Nam = i.NamHoaDon.Value, HoaDonNuocID = i.HoadonnuocID } ).Distinct().ToList(); if (hoaDonThangGanNhat.Count > 0) { HoaDonNuocHaDong.Models.SoLieuTieuThu.HoaDonNuoc obj = hoaDonThangGanNhat.First(); nearestDate = new DateTime(obj.Nam, obj.Thang, 1); foreach (var item in hoaDonThangGanNhat) { ngayThangHoaDon = new DateTime(item.Nam, item.Thang, 1); if (DateTime.Compare(ngayThangHoaDon, nearestDate) > 0 && DateTime.Compare(ngayThangHoaDon, currentTime) < 0) { nearestDate = ngayThangHoaDon; } } return(new DateTime(nearestDate.Year, nearestDate.Month, 1)); } return(ngayThangHoaDon); }
public void generateChiSoFromNearestMonth(int currentMonth, int currentYear, int nhanVienId, int tuyenKHID) { DateTime thangNamGanNhat = getThangNamGanNhatThuocHoaDon(tuyenKHID, currentMonth, currentYear); int previousMonth = thangNamGanNhat.Month; int previousYear = thangNamGanNhat.Year; if (previousYear != 1) { var hoaDonNuocsThangTruoc = getHoaDonThang(previousMonth, previousYear, tuyenKHID); var hoaDonNuocsHienTai = getHoaDonThang(currentMonth, currentYear, tuyenKHID); int countHoaDonsThangTruoc = hoaDonNuocsThangTruoc.Count(); int countHoaDonsThangHienTai = hoaDonNuocsHienTai.Count(); if (countHoaDonsThangTruoc != 0) { foreach (var hoaDonNuocThangTruoc in hoaDonNuocsThangTruoc) { HoaDonNuocHaDong.Models.SoLieuTieuThu.HoaDonNuoc hoaDonNuoc = hoaDonNuocsHienTai.FirstOrDefault(p => p.Thang == currentMonth && p.Nam == currentYear && p.KhachHangID == hoaDonNuocThangTruoc.KhachHangID); if (hoaDonNuoc == null) { Hoadonnuoc hoaDonThangHienTai = new Hoadonnuoc(); hoaDonThangHienTai.ThangHoaDon = currentMonth; hoaDonThangHienTai.NamHoaDon = currentYear; if (hoaDonNuocThangTruoc.NgayKetThucSuDung == null || hoaDonNuocThangTruoc.NgayKetThucSuDung == DateTime.MinValue) { hoaDonThangHienTai.Ngaybatdausudung = hoaDonNuocThangTruoc.NgayBatDauSuDung; } else { hoaDonThangHienTai.Ngaybatdausudung = hoaDonNuocThangTruoc.NgayKetThucSuDung; } hoaDonThangHienTai.KhachhangID = hoaDonNuocThangTruoc.KhachHangID; hoaDonThangHienTai.NhanvienID = nhanVienId; hoaDonThangHienTai.Trangthaixoa = false; hoaDonThangHienTai.Trangthaithu = false; hoaDonThangHienTai.Trangthaiin = false; hoaDonThangHienTai.Trangthaichot = false; db.Hoadonnuocs.Add(hoaDonThangHienTai); db.SaveChanges(); //Thêm chi tiết hóa đơn nước tháng sau Chitiethoadonnuoc chiTiet = new Chitiethoadonnuoc(); chiTiet.HoadonnuocID = hoaDonThangHienTai.HoadonnuocID; chiTiet.Chisocu = hoaDonNuocThangTruoc.ChiSoMoi; db.Chitiethoadonnuocs.Add(chiTiet); } } db.SaveChanges(); } } }