public int GenerateSoThuTienByHocSinhRows(DateTime ngayTinh, List <HocSinhRow> hocSinhRows) { ViewBangThuTienTableAdapter viewBangThuTienTableAdapter = (ViewBangThuTienTableAdapter)StaticDataFacade.Get(StaticDataKeys.AdapterViewBangThuTien); ViewBangThuTienDataTable viewBangThuTienTable = viewBangThuTienTableAdapter.GetViewBangThuTienByNgayTinhAndLop(ngayTinh, null); List <int> hocSinhIds = new List <int>(); List <HocSinhRow> needToGenerateHocSinhRows = new List <Dao.QLMamNonDs.HocSinhRow>(); foreach (HocSinhRow hocSinh in hocSinhRows) { DataRow[] rows = viewBangThuTienTable.Select(String.Format("HocSinhId={0}", hocSinh.HocSinhId)); if (ArrayUtil.IsEmpty(rows)) { hocSinhIds.Add(hocSinh.HocSinhId); needToGenerateHocSinhRows.Add(hocSinh); } } Dictionary <int, HocSinhLopRow> hocSinhIdsToHocSinhLops = StaticDataUtil.GetHocSinhLopsByHocSinhIds(hocSinhIds, ngayTinh); Dictionary <int, Dictionary <int, ViewBangThuTienRow> > lopToHocSinhToViewBangThuTienRowsMap = buildLopToHocSinhToViewBangThuTienRowsMap(viewBangThuTienTable, ngayTinh); Dictionary <int, Dictionary <int, ViewBangThuTienRow> > prevMonthLopToHocSinhToViewBangThuTienRowsMap = getAndSortPrevMonthViewBangThuTienRows(ngayTinh, viewBangThuTienTableAdapter, hocSinhIds, hocSinhIdsToHocSinhLops, lopToHocSinhToViewBangThuTienRowsMap); Dictionary <int, int> generatedLopToSTTs = buildGeneratedLopIdToSTTMap(lopToHocSinhToViewBangThuTienRowsMap); // Generate SoThuTien for HocSinhs that exist in previous month foreach (HocSinhRow hocSinh in needToGenerateHocSinhRows) { if (hocSinhIdsToHocSinhLops.ContainsKey(hocSinh.HocSinhId)) { int lopId = hocSinhIdsToHocSinhLops[hocSinh.HocSinhId].LopId; ViewBangThuTienRow preMonthViewBangThuTien = null; if (prevMonthLopToHocSinhToViewBangThuTienRowsMap.ContainsKey(lopId)) { Dictionary <int, ViewBangThuTienRow> prevMonthhocSinhToViewBangThuTienRowsMap = prevMonthLopToHocSinhToViewBangThuTienRowsMap[lopId]; if (prevMonthhocSinhToViewBangThuTienRowsMap.ContainsKey(hocSinh.HocSinhId)) { preMonthViewBangThuTien = prevMonthhocSinhToViewBangThuTienRowsMap[hocSinh.HocSinhId]; } } if (preMonthViewBangThuTien != null) { GenerateSoThuTienByHocSinhAndLopAndNgayTinh(hocSinh.HocSinhId, lopId, ngayTinh, preMonthViewBangThuTien.STT, preMonthViewBangThuTien); if (!generatedLopToSTTs.ContainsKey(lopId)) { generatedLopToSTTs.Add(lopId, 0); } int currentSTT = generatedLopToSTTs[lopId]; if (currentSTT < preMonthViewBangThuTien.STT) { generatedLopToSTTs[lopId] = preMonthViewBangThuTien.STT; } } } } // Generate SoThuTien for HocSinhs that NOT exist in previous month foreach (HocSinhRow hocSinh in needToGenerateHocSinhRows) { if (hocSinhIdsToHocSinhLops.ContainsKey(hocSinh.HocSinhId)) { int lopId = hocSinhIdsToHocSinhLops[hocSinh.HocSinhId].LopId; ViewBangThuTienRow preMonthViewBangThuTien = null; if (prevMonthLopToHocSinhToViewBangThuTienRowsMap.ContainsKey(lopId)) { Dictionary <int, ViewBangThuTienRow> prevMonthhocSinhToViewBangThuTienRowsMap = prevMonthLopToHocSinhToViewBangThuTienRowsMap[lopId]; if (prevMonthhocSinhToViewBangThuTienRowsMap.ContainsKey(hocSinh.HocSinhId)) { preMonthViewBangThuTien = prevMonthhocSinhToViewBangThuTienRowsMap[hocSinh.HocSinhId]; } } if (preMonthViewBangThuTien == null) { if (!generatedLopToSTTs.ContainsKey(lopId)) { generatedLopToSTTs.Add(lopId, 0); } int stt = generatedLopToSTTs[lopId] + 1; GenerateSoThuTienByHocSinhAndLopAndNgayTinh(hocSinh.HocSinhId, lopId, ngayTinh, stt, preMonthViewBangThuTien); generatedLopToSTTs[lopId] = stt; } } } return(hocSinhIds.Count); }
protected override void onSaving() { ViewBangThuTienDataTable table = this.DataTable as ViewBangThuTienDataTable; List <int> bangThuTienIds = new List <int>(); foreach (ViewBangThuTienRow viewBangThuTienRow in table) { viewBangThuTienRow.SoTienSXThangTruoc = BangThuTienUtil.SXToSoTienSX(viewBangThuTienRow.SXThangTruoc, Settings.Default.TienAnChinh); viewBangThuTienRow.SoTienAnSangThangTruoc = BangThuTienUtil.SXAnSangToSoTienAnSang(viewBangThuTienRow.AnSangThangTruoc, Settings.Default.TienAnSang); viewBangThuTienRow.SoTienAnToiThangTruoc = BangThuTienUtil.SXAnToiToSoTienAnToi(viewBangThuTienRow.AnToiThangTruoc, Settings.Default.TienAnToi); if (this.isNeedToUpdateBangThuTienKhoanThu(viewBangThuTienRow)) { bangThuTienIds.Add(viewBangThuTienRow.BangThuTienId); } } if (!ListUtil.IsEmpty(bangThuTienIds)) { QLMamNon.Dao.QLMamNonDs.BangThuTienKhoanThuDataTable bangThuTienKhoanThuDataTable = bangThuTienKhoanThuTableAdapter.GetBangThuTienKhoanThuByBangThuTienIds(StringUtil.Join(bangThuTienIds, ",")); foreach (QLMamNon.Dao.QLMamNonDs.BangThuTienKhoanThuRow bangThuTienKhoanThuRow in bangThuTienKhoanThuDataTable) { ViewBangThuTienRow[] viewBangThuTienRows = (ViewBangThuTienRow[])table.Select(String.Format("BangThuTienId={0}", bangThuTienKhoanThuRow.BangThuTienId)); ViewBangThuTienRow viewBangThuTienRow = viewBangThuTienRows[0]; switch (bangThuTienKhoanThuRow.KhoanThuId) { case BangThuTienConstant.KhoanThuIdTienAnSua: bangThuTienKhoanThuRow.SoTien = viewBangThuTienRow.TienAnSua; break; case BangThuTienConstant.KhoanThuIdPhuPhi: bangThuTienKhoanThuRow.SoTien = viewBangThuTienRow.PhuPhi; break; case BangThuTienConstant.KhoanThuIdBanTru: bangThuTienKhoanThuRow.SoTien = viewBangThuTienRow.BanTru; break; case BangThuTienConstant.KhoanThuIdHocPhi: bangThuTienKhoanThuRow.SoTien = viewBangThuTienRow.HocPhi; break; case BangThuTienConstant.KhoanThuIdAnSang: bangThuTienKhoanThuRow.SoTien = viewBangThuTienRow.SoTienAnSangThangNay; break; case BangThuTienConstant.KhoanThuIdAnToi: bangThuTienKhoanThuRow.SoTien = viewBangThuTienRow.SoTienAnToiThangNay; break; default: break; } } bangThuTienKhoanThuTableAdapter.Update(bangThuTienKhoanThuDataTable); } // Start Updating SoTienTruyThu for each BangThuTienRow QLMNDaoJobInvoker qlmnDaoJobInvoker = new QLMNDaoJobInvoker(); qlmnDaoJobInvoker.UpdateSoTienTruyThuOfBangThuTienCommand = new UpdateSoTienTruyThuOfBangThuTienCommand(); qlmnDaoJobInvoker.UpdateSoTienTruyThuOfBangThuTienCommand.CommandParameter.Add(UpdateSoTienTruyThuOfBangThuTienCommand.ParameterViewBangThuTienTableAdapter, this.viewBangThuTienTableAdapter); foreach (ViewBangThuTienRow viewBangThuTienRow in table) { if (viewBangThuTienRow[ViewBangThuTienFieldName.ThanhTien, DataRowVersion.Original] != DBNull.Value && viewBangThuTienRow[ViewBangThuTienFieldName.ThanhTien, DataRowVersion.Current] != DBNull.Value) { long originalVersionToCompare = (long)viewBangThuTienRow[ViewBangThuTienFieldName.ThanhTien, DataRowVersion.Original]; long currentVersionToCompare = (long)viewBangThuTienRow[ViewBangThuTienFieldName.ThanhTien, DataRowVersion.Current]; if (originalVersionToCompare != currentVersionToCompare) { qlmnDaoJobInvoker.UpdateSoTienTruyThuOfBangThuTienCommand.CommandParameter.Remove(UpdateSoTienTruyThuOfBangThuTienCommand.ParameterCurrentViewBangThuTien); qlmnDaoJobInvoker.UpdateSoTienTruyThuOfBangThuTienCommand.CommandParameter.Remove(UpdateSoTienTruyThuOfBangThuTienCommand.ParameterSoTienAdded); qlmnDaoJobInvoker.UpdateSoTienTruyThuOfBangThuTienCommand.CommandParameter.Add(UpdateSoTienTruyThuOfBangThuTienCommand.ParameterCurrentViewBangThuTien, viewBangThuTienRow); qlmnDaoJobInvoker.UpdateSoTienTruyThuOfBangThuTienCommand.CommandParameter.Add(UpdateSoTienTruyThuOfBangThuTienCommand.ParameterSoTienAdded, currentVersionToCompare - originalVersionToCompare); qlmnDaoJobInvoker.UpdateSoTienTruyThuOfBangThuTien(); } } } // Finished Updating SoTienTruyThu for each BangThuTien base.onSaving(); }