From 5d16020c6ab6fdfbabe48a1a56e2f41d40a03596 Mon Sep 17 00:00:00 2001 From: mahan Date: Sun, 12 Oct 2025 12:23:56 +0330 Subject: [PATCH 1/2] feat: optimize join queries in InsuranceListRepository for improved data retrieval --- .../Repository/InsuranceListRepository.cs | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/CompanyManagment.EFCore/Repository/InsuranceListRepository.cs b/CompanyManagment.EFCore/Repository/InsuranceListRepository.cs index dcf1447b..e8f7130e 100644 --- a/CompanyManagment.EFCore/Repository/InsuranceListRepository.cs +++ b/CompanyManagment.EFCore/Repository/InsuranceListRepository.cs @@ -1594,19 +1594,25 @@ public class InsuranceListRepository : RepositoryBase, IIns .Where(x => x.LeftWorkInsurances.Any(l=>l.StartWorkDate <= firstDayOfMonth && (l.LeftWorkDate == null || l.LeftWorkDate >= firstDayOfMonth))); - var res = await notCreatedWorkshop - .GroupJoin(_context.WorkshopEmployers, + var res = await notCreatedWorkshop + .Join( + _context.WorkshopEmployers + .OrderBy(we => we.WorkshopId) + .ThenBy(we => we.EmployerId) + .GroupBy(we => we.WorkshopId) + .Select(g => g.First()), workshop => workshop.id, - workshopEmployer => workshopEmployer.WorkshopId, - (workshop, workshopEmployer) => new { workshop, workshopEmployer }) - .SelectMany(x => x.workshopEmployer.DefaultIfEmpty(), (x, workshopEmployer) => new { x.workshop, workshopEmployer }) - .GroupJoin(_context.Employers, - allResult => allResult.workshopEmployer.EmployerId, - employer => employer.id, - (allResult, employer) => new { allResult.workshop, allResult.workshopEmployer, employer }) - .SelectMany(x => x.employer.DefaultIfEmpty(), (x, employer) => new { x.workshop, x.workshopEmployer, employer }) - .Select(x => new InsuranceListViewModel - { + employer => employer.WorkshopId, + (workshop, workshopEmployer) => new + { + workshop, + workshopEmployer, + employer = _context.Employers + .FirstOrDefault(e => e.id == workshopEmployer.EmployerId) + } + ) + .Select(x=> new InsuranceListViewModel + { WorkShopId = x.workshop.id, WorkShopCode = x.workshop.InsuranceCode, WorkShopName = x.workshop.InsuranceWorkshopInfo != null ? x.workshop.InsuranceWorkshopInfo.WorkshopName : x.workshop.WorkshopFullName, From 6045153865cdb91bf214c5cebafab36c00c4b494 Mon Sep 17 00:00:00 2001 From: mahan Date: Mon, 13 Oct 2025 07:48:45 +0330 Subject: [PATCH 2/2] fix: duplicate insurance record in not created insurances --- .../Repository/InsuranceListRepository.cs | 828 ++++++++++-------- 1 file changed, 480 insertions(+), 348 deletions(-) diff --git a/CompanyManagment.EFCore/Repository/InsuranceListRepository.cs b/CompanyManagment.EFCore/Repository/InsuranceListRepository.cs index e8f7130e..62e5038a 100644 --- a/CompanyManagment.EFCore/Repository/InsuranceListRepository.cs +++ b/CompanyManagment.EFCore/Repository/InsuranceListRepository.cs @@ -40,7 +40,13 @@ public class InsuranceListRepository : RepositoryBase, IIns private readonly IWebHostEnvironment _webHostEnvironment; private readonly AccountContext _accountContext; - public InsuranceListRepository(CompanyContext context, IEmployeeInsurancListDataRepository employeeInsurancListDataRepository, IInsuranceListWorkshopRepository insuranceListWorkshopRepository, IInsuranceWorkshopInfoRepository insuranceWorkshopInfoRepository, IAuthHelper authHelper, IPersonalContractingPartyApp contractingPartyApp, IInsuranceYearlySalaryRepository insuranceYearlySalaryRepository, IWebHostEnvironment webHostEnvironment, AccountContext accountContext) : base(context) + public InsuranceListRepository(CompanyContext context, + IEmployeeInsurancListDataRepository employeeInsurancListDataRepository, + IInsuranceListWorkshopRepository insuranceListWorkshopRepository, + IInsuranceWorkshopInfoRepository insuranceWorkshopInfoRepository, IAuthHelper authHelper, + IPersonalContractingPartyApp contractingPartyApp, + IInsuranceYearlySalaryRepository insuranceYearlySalaryRepository, IWebHostEnvironment webHostEnvironment, + AccountContext accountContext) : base(context) { _context = context; _employeeInsurancListDataRepository = employeeInsurancListDataRepository; @@ -52,6 +58,7 @@ public class InsuranceListRepository : RepositoryBase, IIns _webHostEnvironment = webHostEnvironment; _accountContext = accountContext; } + public OperationResult CreateInsuranceListworkshop(long id, List workshopIds) { var operation = new OperationResult(); @@ -60,7 +67,6 @@ public class InsuranceListRepository : RepositoryBase, IIns var list = new List(); foreach (var item in workshopIds) { - list.Add(new InsuranceListWorkshop() { WorkshopId = item, @@ -78,6 +84,7 @@ public class InsuranceListRepository : RepositoryBase, IIns //} return operation; } + /// /// ذخیره اطلاعات کارگاه برای مشاهده در بیمه /// @@ -87,19 +94,21 @@ public class InsuranceListRepository : RepositoryBase, IIns { var operation = new OperationResult(); - var obj = _context.InsuranceWorkshopInformationSet.Where(x => x.WorkshopId == command.WorkshopId).FirstOrDefault(); + var obj = _context.InsuranceWorkshopInformationSet.Where(x => x.WorkshopId == command.WorkshopId) + .FirstOrDefault(); if (obj != null) { - obj.Edit(command.WorkshopName, command.InsuranceCode, command.AgreementNumber, command.EmployerName, command.Address, command.ListNumber); + obj.Edit(command.WorkshopName, command.InsuranceCode, command.AgreementNumber, command.EmployerName, + command.Address, command.ListNumber); _context.SaveChanges(); } else { var insuranceWorkshopInfoObj = new InsuranceWorkshopInfo(command.WorkshopId, command.WorkshopName, - command.InsuranceCode, command.AgreementNumber, command.EmployerName, command.Address, command.ListNumber); + command.InsuranceCode, command.AgreementNumber, command.EmployerName, command.Address, + command.ListNumber); _context.InsuranceWorkshopInformationSet.Add(insuranceWorkshopInfoObj); _context.SaveChanges(); - } return operation; @@ -113,8 +122,12 @@ public class InsuranceListRepository : RepositoryBase, IIns try { var insuranceListObj = Get(command.Id); - insuranceListObj.Edit(command.SumOfEmployees, command.SumOfWorkingDays, command.SumOfSalaries, command.SumOfBenefitsIncluded, command.Included, command.IncludedAndNotIncluded, command.InsuredShare, - command.EmployerShare, command.UnEmploymentInsurance, command.DifficultJobsInsuranc, command.StartDate, command.SumOfDailyWage, command.SumOfBaseYears, command.SumOfMarriedAllowance, command.ConfirmSentlist); + insuranceListObj.Edit(command.SumOfEmployees, command.SumOfWorkingDays, command.SumOfSalaries, + command.SumOfBenefitsIncluded, command.Included, command.IncludedAndNotIncluded, + command.InsuredShare, + command.EmployerShare, command.UnEmploymentInsurance, command.DifficultJobsInsuranc, + command.StartDate, command.SumOfDailyWage, command.SumOfBaseYears, command.SumOfMarriedAllowance, + command.ConfirmSentlist); var id = insuranceListObj.id; if (command.EmployeeInsurancListDataList != null && command.EmployeeInsurancListDataList.Count > 0) @@ -131,20 +144,27 @@ public class InsuranceListRepository : RepositoryBase, IIns item.BenefitsIncludedContinuous, item.BenefitsIncludedNonContinuous, item.InsuranceShare, item.StartWorkDate, - item.LeftWorkDate, item.JobId, item.IncludeStatus, item.BaseYears, item.MarriedAllowance, item.OverTimePay, item.FamilyAllowance); + item.LeftWorkDate, item.JobId, item.IncludeStatus, item.BaseYears, + item.MarriedAllowance, item.OverTimePay, item.FamilyAllowance); _employeeInsurancListDataRepository.Create(employeeInsurancListData); } else { - var employeeInsurancListDataObj = _employeeInsurancListDataRepository.Get(item.EmployeeInsurancListDataId); - employeeInsurancListDataObj.Edit(item.WorkingDays, item.DailyWage, item.MonthlySalary, item.MonthlyBenefits, item.MonthlyBenefitsIncluded, item.BenefitsIncludedContinuous, - item.BenefitsIncludedNonContinuous, item.InsuranceShare, item.StartWorkDate, item.LeftWorkDate, item.JobId, item.IncludeStatus, item.BaseYears, item.MarriedAllowance, item.OverTimePay, item.FamilyAllowance); + var employeeInsurancListDataObj = + _employeeInsurancListDataRepository.Get(item.EmployeeInsurancListDataId); + employeeInsurancListDataObj.Edit(item.WorkingDays, item.DailyWage, item.MonthlySalary, + item.MonthlyBenefits, item.MonthlyBenefitsIncluded, item.BenefitsIncludedContinuous, + item.BenefitsIncludedNonContinuous, item.InsuranceShare, item.StartWorkDate, + item.LeftWorkDate, item.JobId, item.IncludeStatus, item.BaseYears, + item.MarriedAllowance, item.OverTimePay, item.FamilyAllowance); } } + _employeeInsurancListDataRepository.SaveChanges(); } - var insuranceListworkshopList = _context.InsuranceListWorkshopSet.Where(x => x.InsurancListId == command.Id).ToList(); + var insuranceListworkshopList = _context.InsuranceListWorkshopSet + .Where(x => x.InsurancListId == command.Id).ToList(); _context.InsuranceListWorkshopSet.RemoveRange(insuranceListworkshopList); if (command.WorkshopIds != null && command.WorkshopIds.Count > 0) { @@ -153,12 +173,13 @@ public class InsuranceListRepository : RepositoryBase, IIns if (command.InsuranceWorkshopInfo != null) { - var insuranceWorkshopInfoObj = _insuranceWorkshopInfoRepository.Get(command.InsuranceWorkshopInfo.InsuranceWorkshopInfoId); + var insuranceWorkshopInfoObj = + _insuranceWorkshopInfoRepository.Get(command.InsuranceWorkshopInfo.InsuranceWorkshopInfoId); insuranceWorkshopInfoObj.Edit(command.InsuranceWorkshopInfo.WorkshopName, command.InsuranceWorkshopInfo.InsuranceCode, command.InsuranceWorkshopInfo.AgreementNumber, - command.InsuranceWorkshopInfo.EmployerName, command.InsuranceWorkshopInfo.Address, command.InsuranceWorkshopInfo.ListNumber); + command.InsuranceWorkshopInfo.EmployerName, command.InsuranceWorkshopInfo.Address, + command.InsuranceWorkshopInfo.ListNumber); //_insuranceWorkshopInfoRepository.SaveChanges(); - } SaveChanges(); @@ -173,12 +194,14 @@ public class InsuranceListRepository : RepositoryBase, IIns transaction.Rollback(); } } + return result; } public EditInsuranceList GetDetails(long id) { var editInsuranceList = new EditInsuranceList(); + #region InsuranceList var insuranceList = _context.InsuranceListSet.Where(x => x.id == id).FirstOrDefault(); @@ -202,6 +225,7 @@ public class InsuranceListRepository : RepositoryBase, IIns editInsuranceList.EndDate = insuranceList.EndDate; editInsuranceList.SumOfDailyWage = insuranceList.SumOfDailyWage; } + #endregion #region InsuranceWorkshopInformation @@ -223,10 +247,13 @@ public class InsuranceListRepository : RepositoryBase, IIns Address = x.Address, ListNumber = x.ListNumber, }).FirstOrDefault(); + #endregion #region EmployeeInsurancListData - var employeeInsurancList = _context.EmployeeInsurancListDataSet.Where(x => x.InsuranceListId == id).AsNoTracking().ToList(); + + var employeeInsurancList = _context.EmployeeInsurancListDataSet.Where(x => x.InsuranceListId == id) + .AsNoTracking().ToList(); var employeeInsurancListDataViewModelList = new List(); foreach (var item in employeeInsurancList) { @@ -259,8 +286,10 @@ public class InsuranceListRepository : RepositoryBase, IIns employeeInsurancListData.LeftWorkDate = item.LeftWorkDate; // آی دی شغل employeeInsurancListData.JobId = item.JobId; - employeeInsurancListData.JobName = job != null ? job.JobName : string.Empty; ; - employeeInsurancListData.JobCode = job != null ? job.JobCode : string.Empty; ; + employeeInsurancListData.JobName = job != null ? job.JobName : string.Empty; + ; + employeeInsurancListData.JobCode = job != null ? job.JobCode : string.Empty; + ; employeeInsurancListData.StrStartWorkDate = item.StartWorkDate.ToFarsi(); if (item.LeftWorkDate != null) @@ -271,22 +300,28 @@ public class InsuranceListRepository : RepositoryBase, IIns employeeInsurancListData.BaseYears = item.BaseYears; employeeInsurancListData.BenefitsIncludedNonContinuous = item.BenefitsIncludedNonContinuous; employeeInsurancListDataViewModelList.Add(employeeInsurancListData); - } + #endregion #region WorkshopIds - var workshopIds = _context.InsuranceListWorkshopSet.Where(x => x.InsurancListId == id).Select(x => x.WorkshopId).ToList(); + + var workshopIds = _context.InsuranceListWorkshopSet.Where(x => x.InsurancListId == id).Select(x => x.WorkshopId) + .ToList(); + #endregion #region InsuranceEmployeeInformation + //آی دی همه ی پرسنلهایی که با این آی دی بیمه برای آنها لیست بیمه رد شده را بدست آورده و از جدول اطلاعات پرسنل لیست این پرسنلها را بیرون می کشیم var employeeIds = employeeInsurancList.Select(x => x.EmployeeId).ToList(); - var insuranceEmployeeInformationList = _context.InsuranceEmployeeInformationSet.Where(x => employeeIds.Contains(x.EmployeeId)).ToList(); + var insuranceEmployeeInformationList = _context.InsuranceEmployeeInformationSet + .Where(x => employeeIds.Contains(x.EmployeeId)).ToList(); var employeeDetailsForInsuranceList = new List(); foreach (var item in insuranceEmployeeInformationList) { - var employeeInsurancListData = employeeInsurancListDataViewModelList.Where(x => x.EmployeeId == item.EmployeeId).FirstOrDefault(); + var employeeInsurancListData = employeeInsurancListDataViewModelList + .Where(x => x.EmployeeId == item.EmployeeId).FirstOrDefault(); var employeeDetails = new EmployeeDetailsForInsuranceListViewModel(); employeeDetails.EmployeeId = item.EmployeeId; employeeDetails.InsuranceEmployeeInformationId = item.id; @@ -296,7 +331,8 @@ public class InsuranceListRepository : RepositoryBase, IIns employeeDetails.DateOfBirthGr = item.DateOfBirth; employeeDetails.DateOfIssueGr = item.DateOfIssue; employeeDetails.DateOfBirth = item.DateOfBirth.ToFarsi(); - employeeDetails.DateOfIssue = ((item.DateOfIssue.ToFarsi()) == "1300/10/11") ? "" : item.DateOfIssue.ToFarsi(); + employeeDetails.DateOfIssue = + ((item.DateOfIssue.ToFarsi()) == "1300/10/11") ? "" : item.DateOfIssue.ToFarsi(); employeeDetails.PlaceOfIssue = item.PlaceOfIssue; employeeDetails.NationalCode = item.NationalCode; //employeeDetails.Nationality = item.Nationality; @@ -311,7 +347,8 @@ public class InsuranceListRepository : RepositoryBase, IIns employeeDetails.MonthlyBenefitsIncluded = employeeInsurancListData.MonthlyBenefitsIncluded; employeeDetails.BenefitsIncludedContinuous = employeeInsurancListData.BenefitsIncludedContinuous; employeeDetails.BenefitsIncludedNonContinuous = employeeInsurancListData.BenefitsIncludedNonContinuous; - employeeDetails.IncludedAndNotIncluded = employeeInsurancListData.BenefitsIncludedNonContinuous + employeeInsurancListData.BenefitsIncludedContinuous; + employeeDetails.IncludedAndNotIncluded = employeeInsurancListData.BenefitsIncludedNonContinuous + + employeeInsurancListData.BenefitsIncludedContinuous; employeeDetails.InsuranceShare = employeeInsurancListData.InsuranceShare; employeeDetails.StartWorkDate = employeeInsurancListData.StrStartWorkDate; employeeDetails.LeftWorkDate = employeeInsurancListData.StrLeftWorkDate; @@ -339,21 +376,20 @@ public class InsuranceListRepository : RepositoryBase, IIns public List OptimizedSearch(InsuranceListSearchModel searchModel) { - var acountId = _authHelper.CurrentAccountId(); var workshopIds = _context.WorkshopAccounts.Where(x => x.AccountId == acountId).Select(x => x.WorkshopId); - + var query = _context.InsuranceListSet .Where(x => workshopIds.Contains(x.WorkshopId)) .Join(_context.Workshops.Include(x => x.InsuranceWorkshopInfo), - insurance => insurance.WorkshopId, - workshop => workshop.id, - (insurance, workshop) => new { insurance, workshop }) + insurance => insurance.WorkshopId, + workshop => workshop.id, + (insurance, workshop) => new { insurance, workshop }) .GroupJoin(_context.WorkshopEmployers, - result => result.workshop.id, - employer => employer.WorkshopId, - (result, employer) => new { result.insurance, result.workshop, employer }) + result => result.workshop.id, + employer => employer.WorkshopId, + (result, employer) => new { result.insurance, result.workshop, employer }) .Select(result => new InsuranceListViewModel { Id = result.insurance.id, @@ -361,14 +397,20 @@ public class InsuranceListRepository : RepositoryBase, IIns Month = result.insurance.Month, MonthNumber = result.insurance.Month, WorkShopId = result.insurance.WorkshopId, - WorkShopCode = result.workshop.InsuranceWorkshopInfo != null ? result.workshop.InsuranceWorkshopInfo.InsuranceCode : result.workshop.InsuranceCode, - WorkShopName = result.workshop.InsuranceWorkshopInfo != null ? result.workshop.InsuranceWorkshopInfo.WorkshopName : result.workshop.WorkshopFullName, + WorkShopCode = result.workshop.InsuranceWorkshopInfo != null + ? result.workshop.InsuranceWorkshopInfo.InsuranceCode + : result.workshop.InsuranceCode, + WorkShopName = result.workshop.InsuranceWorkshopInfo != null + ? result.workshop.InsuranceWorkshopInfo.WorkshopName + : result.workshop.WorkshopFullName, TypeOfInsuranceSend = result.workshop.TypeOfInsuranceSend == "NormalList" ? "عادی" : - result.workshop.TypeOfInsuranceSend == "Govermentlist" ? "کمک دولت" : - result.workshop.TypeOfInsuranceSend == "Familylist" ? "خانوادگی" : "", + result.workshop.TypeOfInsuranceSend == "Govermentlist" ? "کمک دولت" : + result.workshop.TypeOfInsuranceSend == "Familylist" ? "خانوادگی" : "", FixedSalary = result.workshop.FixedSalary, StrFixedSalary = result.workshop.FixedSalary ? "دارد" : "ندارد", - EmployerName = result.workshop.InsuranceWorkshopInfo != null ? result.workshop.InsuranceWorkshopInfo.EmployerName : result.workshop.WorkshopFullName, + EmployerName = result.workshop.InsuranceWorkshopInfo != null + ? result.workshop.InsuranceWorkshopInfo.EmployerName + : result.workshop.WorkshopFullName, Branch = "", City = "", ConfirmSentlist = result.insurance.ConfirmSentlist, @@ -382,7 +424,7 @@ public class InsuranceListRepository : RepositoryBase, IIns InspectionDone = result.insurance.Inspection.IsDone, EmployerApprovalStatus = result.insurance.EmployerApproval.Status, ArchiveCode = result.workshop.ArchiveCode, - }); + }); query = searchModel.Status switch { @@ -390,7 +432,8 @@ public class InsuranceListRepository : RepositoryBase, IIns !x.DebtDone && !x.EmployerApproved && !x.InspectionDone && !x.ConfirmSentlist), InsuranceListSearchStatus.InProgress => query.Where(x => - (x.DebtDone || x.EmployerApproved || x.InspectionDone)&& !(x.DebtDone && x.EmployerApproved && x.InspectionDone) && !x.ConfirmSentlist), + (x.DebtDone || x.EmployerApproved || x.InspectionDone) && + !(x.DebtDone && x.EmployerApproved && x.InspectionDone) && !x.ConfirmSentlist), InsuranceListSearchStatus.ReadyToSendList => query.Where(x => x.DebtDone && x.EmployerApproved && x.InspectionDone && !x.ConfirmSentlist), @@ -400,39 +443,48 @@ public class InsuranceListRepository : RepositoryBase, IIns _ => query }; - if (!string.IsNullOrEmpty(searchModel.Year) && searchModel.Year != "0" && !string.IsNullOrEmpty(searchModel.Month) && searchModel.Month != "0") - query = query.Where(x => x.Year == searchModel.Year && x.Month == searchModel.Month).OrderByDescending(x => x.Id); + if (!string.IsNullOrEmpty(searchModel.Year) && searchModel.Year != "0" && + !string.IsNullOrEmpty(searchModel.Month) && searchModel.Month != "0") + query = query.Where(x => x.Year == searchModel.Year && x.Month == searchModel.Month) + .OrderByDescending(x => x.Id); else { if (!string.IsNullOrEmpty(searchModel.Month) && searchModel.Month != "0") - query = query.Where(x => x.Month == searchModel.Month).OrderByDescending(x => x.WorkShopName).ThenByDescending(x => x.EmployerName).ThenByDescending(x => x.Year); + query = query.Where(x => x.Month == searchModel.Month).OrderByDescending(x => x.WorkShopName) + .ThenByDescending(x => x.EmployerName).ThenByDescending(x => x.Year); if (!string.IsNullOrEmpty(searchModel.Year) && searchModel.Year != "0") - query = query.Where(x => x.Year == searchModel.Year).OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.WorkShopName).ThenByDescending(x => x.Month); - + query = query.Where(x => x.Year == searchModel.Year).OrderByDescending(x => x.EmployerName) + .ThenByDescending(x => x.WorkShopName).ThenByDescending(x => x.Month); } + if (!string.IsNullOrEmpty(searchModel.WorkShopCode)) - query = query.Where(x => x.WorkShopCode == searchModel.WorkShopCode).OrderByDescending(x => x.Year).OrderByDescending(x => x.Month).ThenByDescending(x => x.EmployerName); + query = query.Where(x => x.WorkShopCode == searchModel.WorkShopCode).OrderByDescending(x => x.Year) + .OrderByDescending(x => x.Month).ThenByDescending(x => x.EmployerName); if (!string.IsNullOrEmpty(searchModel.WorkShopName)) - query = query.Where(x => x.WorkShopName.Contains(searchModel.WorkShopName)).OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).OrderByDescending(x => x.Month); + query = query.Where(x => x.WorkShopName.Contains(searchModel.WorkShopName)) + .OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).OrderByDescending(x => x.Month); if (searchModel.WorkshopId > 0) { var workshopName = query.FirstOrDefault(u => u.WorkShopId == searchModel.WorkshopId)?.WorkShopName; - query = query.Where(x => x.WorkShopName.Contains(workshopName)).OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).OrderByDescending(x => x.Month); + query = query.Where(x => x.WorkShopName.Contains(workshopName)).OrderByDescending(x => x.EmployerName) + .ThenByDescending(x => x.Year).OrderByDescending(x => x.Month); } if (searchModel.EmployerId > 0) { var employerName = query.FirstOrDefault(u => u.EmployerId == searchModel.EmployerId)?.EmployerName; - query = query.Where(x => x.EmployerName.Contains(employerName)).OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).OrderByDescending(x => x.Month); + query = query.Where(x => x.EmployerName.Contains(employerName)).OrderByDescending(x => x.EmployerName) + .ThenByDescending(x => x.Year).OrderByDescending(x => x.Month); } if (!string.IsNullOrEmpty(searchModel.EmployerName)) - query = query.Where(x => x.EmployerName.Contains(searchModel.EmployerName)).OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).OrderByDescending(x => x.Month); + query = query.Where(x => x.EmployerName.Contains(searchModel.EmployerName)) + .OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).OrderByDescending(x => x.Month); if (searchModel.FixedSalary != null) @@ -452,20 +504,24 @@ public class InsuranceListRepository : RepositoryBase, IIns return query.Skip(searchModel.PageIndex).Take(30).ToList(); } + public List Search(InsuranceListSearchModel searchModel) { var cornometr = new Stopwatch(); cornometr.Start(); var acountID = _authHelper.CurrentAccountId(); - var workshopIds = _context.WorkshopAccounts.Where(x => x.AccountId == acountID).Select(x => x.WorkshopId).ToList(); + var workshopIds = _context.WorkshopAccounts.Where(x => x.AccountId == acountID).Select(x => x.WorkshopId) + .ToList(); List list = new List(); var query = _context.InsuranceListSet.Where(x => workshopIds.Contains(x.WorkshopId)).ToList(); - var insuranceWorkshopInformationList = _context.InsuranceWorkshopInformationSet.Where(u => workshopIds.Contains(u.WorkshopId)); + var insuranceWorkshopInformationList = + _context.InsuranceWorkshopInformationSet.Where(u => workshopIds.Contains(u.WorkshopId)); var workshopList = _context.Workshops.Where(u => workshopIds.Contains(u.id)); foreach (var item in query) { - var insuranceWorkshopInformation = insuranceWorkshopInformationList.FirstOrDefault(u => u.WorkshopId == item.WorkshopId); + var insuranceWorkshopInformation = + insuranceWorkshopInformationList.FirstOrDefault(u => u.WorkshopId == item.WorkshopId); var workshop = workshopList.FirstOrDefault(u => u.id == item.WorkshopId); var employerId = _context.WorkshopEmployers.Where(x => x.WorkshopId == item.WorkshopId) .Select(x => x.EmployerId).FirstOrDefault(); @@ -490,13 +546,19 @@ public class InsuranceListRepository : RepositoryBase, IIns Year = item.Year, Month = item.Month, MonthNumber = item.Month, - WorkShopCode = insuranceWorkshopInformation == null ? workshop.InsuranceCode : insuranceWorkshopInformation.InsuranceCode, - WorkShopName = insuranceWorkshopInformation == null ? workshop.WorkshopName : insuranceWorkshopInformation.WorkshopName, + WorkShopCode = insuranceWorkshopInformation == null + ? workshop.InsuranceCode + : insuranceWorkshopInformation.InsuranceCode, + WorkShopName = insuranceWorkshopInformation == null + ? workshop.WorkshopName + : insuranceWorkshopInformation.WorkshopName, WorkShopId = workshop.id, TypeOfInsuranceSend = typeOfInsuranceSend, FixedSalary = workshop.FixedSalary, StrFixedSalary = workshop.FixedSalary ? "دارد" : "ندارد", - EmployerName = insuranceWorkshopInformation == null ? workshop.WorkshopFullName : insuranceWorkshopInformation.EmployerName, + EmployerName = insuranceWorkshopInformation == null + ? workshop.WorkshopFullName + : insuranceWorkshopInformation.EmployerName, Branch = "", City = "", ConfirmSentlist = item.ConfirmSentlist, @@ -509,29 +571,37 @@ public class InsuranceListRepository : RepositoryBase, IIns list = list.OrderByDescending(x => x.Id).ToList(); - if (!string.IsNullOrEmpty(searchModel.Year) && searchModel.Year != "0" && !string.IsNullOrEmpty(searchModel.Month) && searchModel.Month != "0") - list = list.Where(x => x.Year == searchModel.Year && x.Month == searchModel.Month).OrderByDescending(x => x.Id).ToList(); + if (!string.IsNullOrEmpty(searchModel.Year) && searchModel.Year != "0" && + !string.IsNullOrEmpty(searchModel.Month) && searchModel.Month != "0") + list = list.Where(x => x.Year == searchModel.Year && x.Month == searchModel.Month) + .OrderByDescending(x => x.Id).ToList(); else { if (!string.IsNullOrEmpty(searchModel.Month) && searchModel.Month != "0") - list = list.Where(x => x.Month == searchModel.Month).OrderByDescending(x => x.WorkShopName).ThenByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).ToList(); + list = list.Where(x => x.Month == searchModel.Month).OrderByDescending(x => x.WorkShopName) + .ThenByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).ToList(); if (!string.IsNullOrEmpty(searchModel.Year) && searchModel.Year != "0") - list = list.Where(x => x.Year == searchModel.Year).OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.WorkShopName).ThenByDescending(x => x.Month).ToList(); - + list = list.Where(x => x.Year == searchModel.Year).OrderByDescending(x => x.EmployerName) + .ThenByDescending(x => x.WorkShopName).ThenByDescending(x => x.Month).ToList(); } + if (!string.IsNullOrEmpty(searchModel.WorkShopCode)) - list = list.Where(x => x.WorkShopCode == searchModel.WorkShopCode).OrderByDescending(x => x.Year).OrderByDescending(x => x.Month).ThenByDescending(x => x.EmployerName).ToList(); + list = list.Where(x => x.WorkShopCode == searchModel.WorkShopCode).OrderByDescending(x => x.Year) + .OrderByDescending(x => x.Month).ThenByDescending(x => x.EmployerName).ToList(); if (!string.IsNullOrEmpty(searchModel.WorkShopName)) - list = list.Where(x => x.WorkShopName.Contains(searchModel.WorkShopName)).OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).OrderByDescending(x => x.Month).ToList(); + list = list.Where(x => x.WorkShopName.Contains(searchModel.WorkShopName)) + .OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).OrderByDescending(x => x.Month) + .ToList(); if (searchModel.WorkshopId > 0) { var workshopName = workshopList.FirstOrDefault(u => u.id == searchModel.WorkshopId).WorkshopName; - list = list.Where(x => x.WorkShopName.Contains(workshopName)).OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).OrderByDescending(x => x.Month).ToList(); + list = list.Where(x => x.WorkShopName.Contains(workshopName)).OrderByDescending(x => x.EmployerName) + .ThenByDescending(x => x.Year).OrderByDescending(x => x.Month).ToList(); } if (searchModel.EmployerId > 0) @@ -540,11 +610,14 @@ public class InsuranceListRepository : RepositoryBase, IIns string employerName = employer.IsLegal == "حقیقی" ? (employer.FName + " " + employer.LName) : employer.LName; - list = list.Where(x => x.EmployerName.Contains(employerName)).OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).OrderByDescending(x => x.Month).ToList(); + list = list.Where(x => x.EmployerName.Contains(employerName)).OrderByDescending(x => x.EmployerName) + .ThenByDescending(x => x.Year).OrderByDescending(x => x.Month).ToList(); } if (!string.IsNullOrEmpty(searchModel.EmployerName)) - list = list.Where(x => x.EmployerName.Contains(searchModel.EmployerName)).OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).OrderByDescending(x => x.Month).ToList(); + list = list.Where(x => x.EmployerName.Contains(searchModel.EmployerName)) + .OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).OrderByDescending(x => x.Month) + .ToList(); if (searchModel.FixedSalary != null) @@ -568,7 +641,6 @@ public class InsuranceListRepository : RepositoryBase, IIns OperationResult result = new OperationResult(); using (var transaction = _context.Database.BeginTransaction()) { - try { //var employeeInsurancList= _context.EmployeeInsurancListDataSet.Where(x => x.InsuranceListId == id).ToList(); @@ -600,6 +672,7 @@ public class InsuranceListRepository : RepositoryBase, IIns return result; } + /// /// ایجاد لیست بیمه /// @@ -635,10 +708,11 @@ public class InsuranceListRepository : RepositoryBase, IIns item.BenefitsIncludedContinuous, item.BenefitsIncludedNonContinuous, item.InsuranceShare, item.StartWorkDate, - item.LeftWorkDate, item.JobId, item.IncludeStatus, item.BaseYears, item.MarriedAllowance, item.OverTimePay, item.FamilyAllowance); + item.LeftWorkDate, item.JobId, item.IncludeStatus, item.BaseYears, item.MarriedAllowance, + item.OverTimePay, item.FamilyAllowance); _employeeInsurancListDataRepository.Create(employeeInsurancListData); - } + _employeeInsurancListDataRepository.SaveChanges(); } @@ -674,12 +748,15 @@ public class InsuranceListRepository : RepositoryBase, IIns transaction.Rollback(); } } + return result; } + //ویرایش public EditInsuranceList GetDetailsForEdit(long id) { var editInsuranceList = new EditInsuranceList(); + #region InsuranceList var insuranceList = _context.InsuranceListSet.Where(x => x.id == id).FirstOrDefault(); @@ -704,6 +781,7 @@ public class InsuranceListRepository : RepositoryBase, IIns editInsuranceList.SumOfDailyWage = insuranceList.SumOfDailyWage; editInsuranceList.ConfirmSentlist = insuranceList.ConfirmSentlist; } + #endregion #region InsuranceWorkshopInformation @@ -725,9 +803,11 @@ public class InsuranceListRepository : RepositoryBase, IIns Address = x.Address, ListNumber = x.ListNumber, }).FirstOrDefault(); + #endregion #region EmployeeInsurancListData + //var employeeInsurancList = _context.EmployeeInsurancListDataSet.Where(x => x.InsuranceListId == id).AsNoTracking().ToList(); //var employeeInsurancListDataViewModelList = new List(); //foreach (var item in employeeInsurancList) @@ -768,13 +848,18 @@ public class InsuranceListRepository : RepositoryBase, IIns // employeeInsurancListData.StrLeftWorkDate = item.LeftWorkDate.ToFarsi(); // employeeInsurancListDataViewModelList.Add(employeeInsurancListData); //} + #endregion #region WorkshopIds - var workshopIds = _context.InsuranceListWorkshopSet.Where(x => x.InsurancListId == id).Select(x => x.WorkshopId).ToList(); + + var workshopIds = _context.InsuranceListWorkshopSet.Where(x => x.InsurancListId == id).Select(x => x.WorkshopId) + .ToList(); + #endregion #region InsuranceEmployeeInformation + //آی دی همه ی پرسنلهایی که با این آی دی بیمه برای آنها لیست بیمه رد شده را بدست آورده و از جدول اطلاعات پرسنل لیست این پرسنلها را بیرون می کشیم //var employeeIds = employeeInsurancList.Select(x => x.EmployeeId).ToList(); //var insuranceEmployeeInformationList = _context.InsuranceEmployeeInformationSet.Where(x => employeeIds.Contains(x.EmployeeId)).ToList(); @@ -826,15 +911,17 @@ public class InsuranceListRepository : RepositoryBase, IIns editInsuranceList.WorkshopIds = workshopIds; return editInsuranceList; - } - public MainEmployeeDetailsViewModel SearchEmployeeListForEditByInsuranceListId(EmployeeForEditInsuranceListSearchModel searchModel) + public MainEmployeeDetailsViewModel SearchEmployeeListForEditByInsuranceListId( + EmployeeForEditInsuranceListSearchModel searchModel) { var employeeDetailsViewModel = new MainEmployeeDetailsViewModel(); #region EmployeeInsurancListData - var employeeInsurancList = _context.EmployeeInsurancListDataSet.Where(x => x.InsuranceListId == searchModel.InsuranceId).AsNoTracking().ToList(); + + var employeeInsurancList = _context.EmployeeInsurancListDataSet + .Where(x => x.InsuranceListId == searchModel.InsuranceId).AsNoTracking().ToList(); var employeeInsurancListDataViewModelList = new List(); foreach (var item in employeeInsurancList) { @@ -884,11 +971,16 @@ public class InsuranceListRepository : RepositoryBase, IIns employeeInsurancListData.IncludeStatus = item.IncludeStatus; + #region after change LeftWorkDateInsurance + //ممکن است کسی شامل مزایا باشد و در آن ماه برایش یا مزد روزانه رد نشده باشد // باید با یک فیلد چک شود // - if (leftWorkInsurance != null && ((leftWorkInsurance.IncludeStatus != leftWorkInsurance.IncludeStatus) || ((leftWorkInsurance.LeftWorkDate != item.LeftWorkDate) || (leftWorkInsurance.StartWorkDate != item.StartWorkDate) || (leftWorkInsurance.JobId != item.JobId)))) + if (leftWorkInsurance != null && ((leftWorkInsurance.IncludeStatus != leftWorkInsurance.IncludeStatus) || + ((leftWorkInsurance.LeftWorkDate != item.LeftWorkDate) || + (leftWorkInsurance.StartWorkDate != item.StartWorkDate) || + (leftWorkInsurance.JobId != item.JobId)))) { employeeInsurancListData.HasConfilictLeftWork = true; if (leftWorkInsurance.LeftWorkDate != null) @@ -904,23 +996,27 @@ public class InsuranceListRepository : RepositoryBase, IIns employeeInsurancListData.JobNameNew = jobNew != null ? jobNew.JobName : string.Empty; employeeInsurancListData.JobCodeNew = jobNew != null ? jobNew.JobCode : string.Empty; } + #endregion employeeInsurancListDataViewModelList.Add(employeeInsurancListData); - } + #endregion #region InsuranceEmployeeInformation + //آی دی همه ی پرسنلهایی که با این آی دی بیمه برای آنها لیست بیمه رد شده را بدست آورده و از جدول اطلاعات پرسنل لیست این پرسنلها را بیرون می کشیم var employeeIds = employeeInsurancList.Select(x => x.EmployeeId).ToList(); - var insuranceEmployeeInformationList = _context.InsuranceEmployeeInformationSet.Where(x => employeeIds.Contains(x.EmployeeId)).ToList(); + var insuranceEmployeeInformationList = _context.InsuranceEmployeeInformationSet + .Where(x => employeeIds.Contains(x.EmployeeId)).ToList(); var employeeDetailsForInsuranceList = new List(); foreach (var item in insuranceEmployeeInformationList) { var employeeObject = _context.Employees.Where(x => x.id == item.EmployeeId)?.FirstOrDefault(); - var employeeInsurancListData = employeeInsurancListDataViewModelList.Where(x => x.EmployeeId == item.EmployeeId)?.FirstOrDefault(); + var employeeInsurancListData = employeeInsurancListDataViewModelList + .Where(x => x.EmployeeId == item.EmployeeId)?.FirstOrDefault(); var employeeDetails = new EmployeeDetailsForInsuranceListViewModel(); employeeDetails.EmployeeId = item.EmployeeId; employeeDetails.InsuranceEmployeeInformationId = item.id; @@ -930,7 +1026,8 @@ public class InsuranceListRepository : RepositoryBase, IIns employeeDetails.DateOfBirthGr = item.DateOfBirth; employeeDetails.DateOfIssueGr = item.DateOfIssue; employeeDetails.DateOfBirth = item.DateOfBirth.ToFarsi(); - employeeDetails.DateOfIssue = ((item.DateOfIssue.ToFarsi()) == "1300/10/11") ? "" : item.DateOfIssue.ToFarsi(); + employeeDetails.DateOfIssue = + ((item.DateOfIssue.ToFarsi()) == "1300/10/11") ? "" : item.DateOfIssue.ToFarsi(); employeeDetails.PlaceOfIssue = item.PlaceOfIssue; employeeDetails.IdNumber = item.IdNumber; employeeDetails.Gender = item.Gender; @@ -982,7 +1079,6 @@ public class InsuranceListRepository : RepositoryBase, IIns return employeeDetailsViewModel; - } public OperationResult ConfirmInsuranceList(long id) @@ -993,8 +1089,12 @@ public class InsuranceListRepository : RepositoryBase, IIns try { var insuranceListObj = Get(id); - insuranceListObj.Edit(insuranceListObj.SumOfEmployees, insuranceListObj.SumOfWorkingDays, insuranceListObj.SumOfSalaries, insuranceListObj.SumOfBenefitsIncluded, insuranceListObj.Included, insuranceListObj.IncludedAndNotIncluded, insuranceListObj.InsuredShare, - insuranceListObj.EmployerShare, insuranceListObj.UnEmploymentInsurance, insuranceListObj.DifficultJobsInsuranc, insuranceListObj.StartDate, insuranceListObj.SumOfDailyWage, insuranceListObj.SumOfBaseYears, insuranceListObj.SumOfMarriedAllowance, true); + insuranceListObj.Edit(insuranceListObj.SumOfEmployees, insuranceListObj.SumOfWorkingDays, + insuranceListObj.SumOfSalaries, insuranceListObj.SumOfBenefitsIncluded, insuranceListObj.Included, + insuranceListObj.IncludedAndNotIncluded, insuranceListObj.InsuredShare, + insuranceListObj.EmployerShare, insuranceListObj.UnEmploymentInsurance, + insuranceListObj.DifficultJobsInsuranc, insuranceListObj.StartDate, insuranceListObj.SumOfDailyWage, + insuranceListObj.SumOfBaseYears, insuranceListObj.SumOfMarriedAllowance, true); SaveChanges(); @@ -1002,9 +1102,7 @@ public class InsuranceListRepository : RepositoryBase, IIns } catch (Exception) { - result.Message = "تایید ارسال لیست بیمه با موفقیت انجام شد"; - } @@ -1015,7 +1113,8 @@ public class InsuranceListRepository : RepositoryBase, IIns { var insuranceListViewModel = new InsuranceListViewModel(); - var insuranceList = _context.InsuranceListSet.Where(x => x.WorkshopId == workshopId && x.Year == year).OrderByDescending(x => x.id)?.FirstOrDefault(); + var insuranceList = _context.InsuranceListSet.Where(x => x.WorkshopId == workshopId && x.Year == year) + .OrderByDescending(x => x.id)?.FirstOrDefault(); if (insuranceList != null) { @@ -1026,10 +1125,12 @@ public class InsuranceListRepository : RepositoryBase, IIns insuranceListViewModel.WorkShopId = insuranceList.WorkshopId; insuranceListViewModel.ConfirmSentlist = insuranceList.ConfirmSentlist; } + return insuranceListViewModel; } #region client + public List SearchForClient(InsuranceListSearchModel searchModel) { #region CheckValidtWorkshop @@ -1141,16 +1242,14 @@ public class InsuranceListRepository : RepositoryBase, IIns } - - public List SearchForClientOld(InsuranceListSearchModel searchModel) { - List list = new List(); var query = _context.InsuranceListSet.Where(x => x.WorkshopId == searchModel.WorkshopId).ToList(); foreach (var item in query) { - var insuranceWorkshopInformation = _context.InsuranceWorkshopInformationSet.FirstOrDefault(u => u.WorkshopId == item.WorkshopId); + var insuranceWorkshopInformation = + _context.InsuranceWorkshopInformationSet.FirstOrDefault(u => u.WorkshopId == item.WorkshopId); //var workshop = _context.Workshops.FirstOrDefault(u => u.id == item.WorkshopId); string typeOfInsuranceSend = ""; if (searchModel.TypeOfInsuranceSend == "NormalList") @@ -1172,13 +1271,19 @@ public class InsuranceListRepository : RepositoryBase, IIns Year = item.Year, Month = item.Month, MonthNumber = item.Month, - WorkShopCode = insuranceWorkshopInformation == null ? searchModel.WorkShopCode : insuranceWorkshopInformation.InsuranceCode, - WorkShopName = insuranceWorkshopInformation == null ? searchModel.WorkShopName : insuranceWorkshopInformation.WorkshopName, + WorkShopCode = insuranceWorkshopInformation == null + ? searchModel.WorkShopCode + : insuranceWorkshopInformation.InsuranceCode, + WorkShopName = insuranceWorkshopInformation == null + ? searchModel.WorkShopName + : insuranceWorkshopInformation.WorkshopName, WorkShopId = searchModel.WorkshopId, TypeOfInsuranceSend = typeOfInsuranceSend, FixedSalary = (bool)searchModel.FixedSalary, StrFixedSalary = (bool)searchModel.FixedSalary ? "دارد" : "ندارد", - EmployerName = insuranceWorkshopInformation == null ? searchModel.WorkshopFullName : insuranceWorkshopInformation.EmployerName, + EmployerName = insuranceWorkshopInformation == null + ? searchModel.WorkshopFullName + : insuranceWorkshopInformation.EmployerName, Branch = "", City = "", ConfirmSentlist = item.ConfirmSentlist @@ -1189,16 +1294,19 @@ public class InsuranceListRepository : RepositoryBase, IIns list = list.OrderByDescending(x => x.Id).ToList(); - if (!string.IsNullOrEmpty(searchModel.Year) && searchModel.Year != "0" && !string.IsNullOrEmpty(searchModel.Month) && searchModel.Month != "0") - list = list.Where(x => x.Year == searchModel.Year && x.Month == searchModel.Month).OrderByDescending(x => x.Id).ToList(); + if (!string.IsNullOrEmpty(searchModel.Year) && searchModel.Year != "0" && + !string.IsNullOrEmpty(searchModel.Month) && searchModel.Month != "0") + list = list.Where(x => x.Year == searchModel.Year && x.Month == searchModel.Month) + .OrderByDescending(x => x.Id).ToList(); else { if (!string.IsNullOrEmpty(searchModel.Month) && searchModel.Month != "0") - list = list.Where(x => x.Month == searchModel.Month).OrderByDescending(x => x.WorkShopName).ThenByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).ToList(); + list = list.Where(x => x.Month == searchModel.Month).OrderByDescending(x => x.WorkShopName) + .ThenByDescending(x => x.EmployerName).ThenByDescending(x => x.Year).ToList(); if (!string.IsNullOrEmpty(searchModel.Year) && searchModel.Year != "0") - list = list.Where(x => x.Year == searchModel.Year).OrderByDescending(x => x.EmployerName).ThenByDescending(x => x.WorkShopName).ThenByDescending(x => x.Month).ToList(); - + list = list.Where(x => x.Year == searchModel.Year).OrderByDescending(x => x.EmployerName) + .ThenByDescending(x => x.WorkShopName).ThenByDescending(x => x.Month).ToList(); } //if (!string.IsNullOrEmpty(searchModel.WorkShopCode)) // list = list.Where(x => x.WorkShopCode == searchModel.WorkShopCode).OrderByDescending(x => x.Year).OrderByDescending(x => x.Month).ThenByDescending(x => x.EmployerName).ToList(); @@ -1221,13 +1329,12 @@ public class InsuranceListRepository : RepositoryBase, IIns return list; //.OrderByDescending(x => x.Id).ToList(); } - #endregion - public (int insuranceHistoryYearsCount, double baseYear) GetEmployeeInsuranceBaseYear(long employeeId, long workshopId, int countWorkingDay, DateTime listStartDate, DateTime listEndDate, DateTime startWorkDate, DateTime leftDate, bool hasLeft) + public (int insuranceHistoryYearsCount, double baseYear) GetEmployeeInsuranceBaseYear(long employeeId, + long workshopId, int countWorkingDay, DateTime listStartDate, DateTime listEndDate, DateTime startWorkDate, + DateTime leftDate, bool hasLeft) { - - var lefts = _context.LeftWorkInsuranceList .Where(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId).Select(x => new { @@ -1276,19 +1383,19 @@ public class InsuranceListRepository : RepositoryBase, IIns // baseYear = baseyarPerDay * countWorkingDay; - //} - return (yearsCount, baseYear); } - public List GetEmployeeInsuranceDataAmonthAgo(DateTime currentMonthStartDate, long workshopId) + public List GetEmployeeInsuranceDataAmonthAgo(DateTime currentMonthStartDate, + long workshopId) { var previusMonthListdate = currentMonthStartDate.AddDays(-1); var res = _context.InsuranceListSet - .Where(x => x.StartDate < previusMonthListdate && x.EndDate >= previusMonthListdate && x.WorkshopId == workshopId) + .Where(x => x.StartDate < previusMonthListdate && x.EndDate >= previusMonthListdate && + x.WorkshopId == workshopId) .Join(_context.EmployeeInsurancListDataSet, insuranceList => insuranceList.id, employeeInsurancListData => employeeInsurancListData.InsuranceListId, @@ -1298,127 +1405,142 @@ public class InsuranceListRepository : RepositoryBase, IIns EmployeeId = employeeInsurancListData.EmployeeId, WorkingDays = employeeInsurancListData.WorkingDays, BenefitsIncludedNonContinuous = employeeInsurancListData.BenefitsIncludedNonContinuous - - }); return res.ToList(); } - public List GetEmployeeInsuranceDataForEdit(long insuranceListId, DateTime startDate, DateTime endDate) + public List GetEmployeeInsuranceDataForEdit(long insuranceListId, + DateTime startDate, DateTime endDate) { - var employeeDataQuery = _context.EmployeeInsurancListDataSet - .Where(x => x.InsuranceListId == insuranceListId) - .Join(_context.InsuranceListSet, - employeeInsurancListData => employeeInsurancListData.InsuranceListId, - insuranceList => insuranceList.id, - (employeeInsurancListData, insuranceList) => new{employeeInsurancListData,insuranceList}) - .Join(_context.LeftWorkInsuranceList - .Where(x => - ((x.LeftWorkDate != null && x.LeftWorkDate != DateTime.MinValue) && - ((DateTime)x.LeftWorkDate >= startDate && - (DateTime)x.LeftWorkDate <= endDate)) || - ((x.LeftWorkDate != null && x.LeftWorkDate != DateTime.MinValue) && - (DateTime)x.LeftWorkDate >= endDate) || - (x.LeftWorkDate == null || x.LeftWorkDate == DateTime.MinValue)) - .Where(x => x.StartWorkDate <= endDate) - .Include(x => x.Employee), - employeeData =>new { employeeData.employeeInsurancListData.EmployeeId,employeeData.insuranceList.WorkshopId}, - leftwork => new {leftwork.EmployeeId,leftwork.WorkshopId}, - (employeeData, leftwork) - => new { employeeData = employeeData.employeeInsurancListData, leftwork }); - - var employeeJobs =employeeDataQuery.Join(_context.Jobs, - result => result.leftwork.JobId, - job => job.id, - (result, job) => new { result, job }); - - - var res =employeeJobs.GroupJoin(_context.InsuranceEmployeeInformationSet.AsSplitQuery(), - allResult => allResult.result.employeeData.EmployeeId, - employeeInfo => employeeInfo.EmployeeId, - (allResult, employeeInfo) => new - { - allResult.result, - allResult.job, - employeeInfo - }) - .SelectMany(x => x.employeeInfo.DefaultIfEmpty(), (x, employeeInfo) => new { x, employeeInfo }) - .Select(result => new EmployeeDetailsForInsuranceListViewModel - { - StartWorkDateNew = result.x.result.leftwork.StartWorkDate, - LeftWorkDateNew = result.x.result.leftwork.LeftWorkDate, - JobIdNew = result.x.result.leftwork.JobId, + .Where(x => x.InsuranceListId == insuranceListId) + .Join(_context.InsuranceListSet, + employeeInsurancListData => employeeInsurancListData.InsuranceListId, + insuranceList => insuranceList.id, + (employeeInsurancListData, insuranceList) => new { employeeInsurancListData, insuranceList }) + .Join(_context.LeftWorkInsuranceList + .Where(x => + ((x.LeftWorkDate != null && x.LeftWorkDate != DateTime.MinValue) && + ((DateTime)x.LeftWorkDate >= startDate && + (DateTime)x.LeftWorkDate <= endDate)) || + ((x.LeftWorkDate != null && x.LeftWorkDate != DateTime.MinValue) && + (DateTime)x.LeftWorkDate >= endDate) || + (x.LeftWorkDate == null || x.LeftWorkDate == DateTime.MinValue)) + .Where(x => x.StartWorkDate <= endDate) + .Include(x => x.Employee), + employeeData => new + { employeeData.employeeInsurancListData.EmployeeId, employeeData.insuranceList.WorkshopId }, + leftwork => new { leftwork.EmployeeId, leftwork.WorkshopId }, + (employeeData, leftwork) + => new { employeeData = employeeData.employeeInsurancListData, leftwork }); + + var employeeJobs = employeeDataQuery.Join(_context.Jobs, + result => result.leftwork.JobId, + job => job.id, + (result, job) => new { result, job }); - StartWorkDateGr = result.x.result.leftwork.StartWorkDate, - LeftWorkDateGr = result.x.result.leftwork.LeftWorkDate, - IncludeStatus = result.x.result.employeeData.IncludeStatus, - JobId = result.x.result.employeeData.JobId, - JobName = result.x.job != null ? result.x.job.JobName : string.Empty, - JobCode = result.x.job != null ? result.x.job.JobCode : string.Empty, - InsuranceEmployeeInformationId = result.employeeInfo != null ? result.employeeInfo.id : 0, - EmployeeId = result.x.result.employeeData.EmployeeId, - - //اطلاعات هویتی - FName = result.employeeInfo != null ? result.employeeInfo.FName : result.x.result.leftwork.Employee.FName, - LName = result.employeeInfo != null ? result.employeeInfo.LName : result.x.result.leftwork.Employee.LName, - FatherName = result.employeeInfo != null ? result.employeeInfo.FatherName : result.x.result.leftwork.Employee.FatherName, - DateOfBirthGr = result.employeeInfo != null ? result.employeeInfo.DateOfBirth : result.x.result.leftwork.Employee.DateOfBirth, - DateOfIssueGr = result.employeeInfo != null ? result.employeeInfo.DateOfIssue : result.x.result.leftwork.Employee.DateOfIssue, - PlaceOfIssue = result.employeeInfo != null ? result.employeeInfo.PlaceOfIssue : result.x.result.leftwork.Employee.PlaceOfIssue, - IdNumber = result.employeeInfo != null ? result.employeeInfo.IdNumber : result.x.result.leftwork.Employee.IdNumber, - Gender = result.employeeInfo != null ? result.employeeInfo.Gender : result.x.result.leftwork.Employee.Gender, - NationalCode = result.x.result.leftwork.Employee.NationalCode, - Nationality = result.x.result.leftwork.Employee.Nationality, - InsuranceCode = result.x.result.leftwork.Employee.InsuranceCode, - MaritalStatus = result.x.result.leftwork.Employee.MaritalStatus, - IsMaritalStatusSet = !string.IsNullOrWhiteSpace(result.x.result.leftwork.Employee.MaritalStatus), - - //اطاعات محاسباتی - EmployeeInsurancListDataId = result.x.result.employeeData.id, - DailyWage = result.x.result.employeeData.DailyWage, - // LeftWorkDateGr = x.LeftWorkDate, - // StartWorkDateGr = x.StartWorkDate, - MonthlyBenefitsIncluded = result.x.result.employeeData.MonthlyBenefitsIncluded, - // JobId = x.JobId, - WorkingDays = result.x.result.employeeData.WorkingDays, - //پایه سنوات - BaseYears = result.x.result.employeeData.BaseYears, - - //مجموع مزد روزانه و پایه سنوات - DailyWagePlusBaseYears = result.x.result.employeeData.DailyWagePlusBaseYears, - - //حق تاهل - MarriedAllowance = result.x.result.employeeData.MarriedAllowance, - - //دستمزد ماهانه - MonthlySalary = result.x.result.employeeData.MonthlySalary, + var res = employeeJobs.GroupJoin(_context.InsuranceEmployeeInformationSet.AsSplitQuery(), + allResult => allResult.result.employeeData.EmployeeId, + employeeInfo => employeeInfo.EmployeeId, + (allResult, employeeInfo) => new + { + allResult.result, + allResult.job, + employeeInfo + }) + .SelectMany(x => x.employeeInfo.DefaultIfEmpty(), (x, employeeInfo) => new { x, employeeInfo }) + .Select(result => new EmployeeDetailsForInsuranceListViewModel + { + StartWorkDateNew = result.x.result.leftwork.StartWorkDate, + LeftWorkDateNew = result.x.result.leftwork.LeftWorkDate, + JobIdNew = result.x.result.leftwork.JobId, - //مزایای ماهانه - MonthlyBenefits = result.x.result.employeeData.MonthlyBenefits, + StartWorkDateGr = result.x.result.leftwork.StartWorkDate, + LeftWorkDateGr = result.x.result.leftwork.LeftWorkDate, + IncludeStatus = result.x.result.employeeData.IncludeStatus, + JobId = result.x.result.employeeData.JobId, + JobName = result.x.job != null ? result.x.job.JobName : string.Empty, + JobCode = result.x.job != null ? result.x.job.JobCode : string.Empty, + InsuranceEmployeeInformationId = result.employeeInfo != null ? result.employeeInfo.id : 0, + EmployeeId = result.x.result.employeeData.EmployeeId, - //مزایای مشمول - BenefitsIncludedContinuous = result.x.result.employeeData.MonthlyBenefitsIncluded, + //اطلاعات هویتی + FName = result.employeeInfo != null + ? result.employeeInfo.FName + : result.x.result.leftwork.Employee.FName, + LName = result.employeeInfo != null + ? result.employeeInfo.LName + : result.x.result.leftwork.Employee.LName, + FatherName = result.employeeInfo != null + ? result.employeeInfo.FatherName + : result.x.result.leftwork.Employee.FatherName, + DateOfBirthGr = result.employeeInfo != null + ? result.employeeInfo.DateOfBirth + : result.x.result.leftwork.Employee.DateOfBirth, + DateOfIssueGr = result.employeeInfo != null + ? result.employeeInfo.DateOfIssue + : result.x.result.leftwork.Employee.DateOfIssue, + PlaceOfIssue = result.employeeInfo != null + ? result.employeeInfo.PlaceOfIssue + : result.x.result.leftwork.Employee.PlaceOfIssue, + IdNumber = result.employeeInfo != null + ? result.employeeInfo.IdNumber + : result.x.result.leftwork.Employee.IdNumber, + Gender = result.employeeInfo != null + ? result.employeeInfo.Gender + : result.x.result.leftwork.Employee.Gender, + NationalCode = result.x.result.leftwork.Employee.NationalCode, + Nationality = result.x.result.leftwork.Employee.Nationality, + InsuranceCode = result.x.result.leftwork.Employee.InsuranceCode, + MaritalStatus = result.x.result.leftwork.Employee.MaritalStatus, + IsMaritalStatusSet = !string.IsNullOrWhiteSpace(result.x.result.leftwork.Employee.MaritalStatus), - //مزایای غیر مشمول - BenefitsIncludedNonContinuous = result.x.result.employeeData.BenefitsIncludedNonContinuous, + //اطاعات محاسباتی + EmployeeInsurancListDataId = result.x.result.employeeData.id, + DailyWage = result.x.result.employeeData.DailyWage, + // LeftWorkDateGr = x.LeftWorkDate, + // StartWorkDateGr = x.StartWorkDate, + MonthlyBenefitsIncluded = result.x.result.employeeData.MonthlyBenefitsIncluded, + // JobId = x.JobId, + WorkingDays = result.x.result.employeeData.WorkingDays, + //پایه سنوات + BaseYears = result.x.result.employeeData.BaseYears, - // جمع کل دستمزد و مزایای ماهانه مشمول و غیر مشمول - IncludedAndNotIncluded = result.x.result.employeeData.BenefitsIncludedContinuous, + //مجموع مزد روزانه و پایه سنوات + DailyWagePlusBaseYears = result.x.result.employeeData.DailyWagePlusBaseYears, - //حق بیمه سهم بیمه شده - InsuranceShare = result.x.result.employeeData.InsuranceShare, + //حق تاهل + MarriedAllowance = result.x.result.employeeData.MarriedAllowance, - //اضافه کار فیش حقوقی - OverTimePay = result.x.result.employeeData.OverTimePay, + //دستمزد ماهانه + MonthlySalary = result.x.result.employeeData.MonthlySalary, - //حق اولا فیش حقوقی - FamilyAllowance = result.x.result.employeeData.FamilyAllowance, - }); + + //مزایای ماهانه + MonthlyBenefits = result.x.result.employeeData.MonthlyBenefits, + + //مزایای مشمول + BenefitsIncludedContinuous = result.x.result.employeeData.MonthlyBenefitsIncluded, + + //مزایای غیر مشمول + BenefitsIncludedNonContinuous = result.x.result.employeeData.BenefitsIncludedNonContinuous, + + // جمع کل دستمزد و مزایای ماهانه مشمول و غیر مشمول + IncludedAndNotIncluded = result.x.result.employeeData.BenefitsIncludedContinuous, + + //حق بیمه سهم بیمه شده + InsuranceShare = result.x.result.employeeData.InsuranceShare, + + //اضافه کار فیش حقوقی + OverTimePay = result.x.result.employeeData.OverTimePay, + + //حق اولا فیش حقوقی + FamilyAllowance = result.x.result.employeeData.FamilyAllowance, + }); //.Select(x => new EmployeeDetailsForInsuranceListViewModel @@ -1466,183 +1588,193 @@ public class InsuranceListRepository : RepositoryBase, IIns return res.ToList(); } - public async Task GetTabCounts(InsuranceListSearchModel searchModel) - { - var acountId = _authHelper.CurrentAccountId(); + public async Task GetTabCounts(InsuranceListSearchModel searchModel) + { + var acountId = _authHelper.CurrentAccountId(); - var workshopIds = _context.WorkshopAccounts - .Where(a => a.AccountId == acountId) - .Select(a => a.WorkshopId); - - var query = _context.InsuranceListSet - .Where(x => workshopIds.Contains(x.WorkshopId)) - .Join(_context.Workshops.Include(x => x.InsuranceWorkshopInfo), - insurance => insurance.WorkshopId, - workshop => workshop.id, - (insurance, workshop) => new { insurance, workshop }) - .GroupJoin(_context.WorkshopEmployers, - result => result.workshop.id, - employer => employer.WorkshopId, - (result, employer) => new { result.insurance, result.workshop, employer }) - .Select(result => new InsuranceListViewModel - { - Id = result.insurance.id, - Year = result.insurance.Year, - Month = result.insurance.Month, - WorkShopId = result.insurance.WorkshopId, - WorkShopCode = result.workshop.InsuranceWorkshopInfo != null ? result.workshop.InsuranceWorkshopInfo.InsuranceCode : result.workshop.InsuranceCode, - WorkShopName = result.workshop.InsuranceWorkshopInfo != null ? result.workshop.InsuranceWorkshopInfo.WorkshopName : result.workshop.WorkshopFullName, - TypeOfInsuranceSend = result.workshop.TypeOfInsuranceSend == "NormalList" ? "عادی" : - result.workshop.TypeOfInsuranceSend == "Govermentlist" ? "کمک دولت" : - result.workshop.TypeOfInsuranceSend == "Familylist" ? "خانوادگی" : "", - FixedSalary = result.workshop.FixedSalary, - EmployerName = result.workshop.InsuranceWorkshopInfo != null ? result.workshop.InsuranceWorkshopInfo.EmployerName : result.workshop.WorkshopFullName, - ConfirmSentlist = result.insurance.ConfirmSentlist, - EmployerId = result.employer.First().EmployerId, - DebtDone = result.insurance.Debt.IsDone, - EmployerApproved = result.insurance.EmployerApproval.IsDone, - InspectionDone = result.insurance.Inspection.IsDone - }); + var workshopIds = _context.WorkshopAccounts + .Where(a => a.AccountId == acountId) + .Select(a => a.WorkshopId); - if (!string.IsNullOrEmpty(searchModel.Year) && searchModel.Year != "0" && !string.IsNullOrEmpty(searchModel.Month) && searchModel.Month != "0") - query = query.Where(x => x.Year == searchModel.Year && x.Month == searchModel.Month); - else - { - if (!string.IsNullOrEmpty(searchModel.Month) && searchModel.Month != "0") - query = query.Where(x => x.Month == searchModel.Month); + var query = _context.InsuranceListSet + .Where(x => workshopIds.Contains(x.WorkshopId)) + .Join(_context.Workshops.Include(x => x.InsuranceWorkshopInfo), + insurance => insurance.WorkshopId, + workshop => workshop.id, + (insurance, workshop) => new { insurance, workshop }) + .GroupJoin(_context.WorkshopEmployers, + result => result.workshop.id, + employer => employer.WorkshopId, + (result, employer) => new { result.insurance, result.workshop, employer }) + .Select(result => new InsuranceListViewModel + { + Id = result.insurance.id, + Year = result.insurance.Year, + Month = result.insurance.Month, + WorkShopId = result.insurance.WorkshopId, + WorkShopCode = result.workshop.InsuranceWorkshopInfo != null + ? result.workshop.InsuranceWorkshopInfo.InsuranceCode + : result.workshop.InsuranceCode, + WorkShopName = result.workshop.InsuranceWorkshopInfo != null + ? result.workshop.InsuranceWorkshopInfo.WorkshopName + : result.workshop.WorkshopFullName, + TypeOfInsuranceSend = result.workshop.TypeOfInsuranceSend == "NormalList" ? "عادی" : + result.workshop.TypeOfInsuranceSend == "Govermentlist" ? "کمک دولت" : + result.workshop.TypeOfInsuranceSend == "Familylist" ? "خانوادگی" : "", + FixedSalary = result.workshop.FixedSalary, + EmployerName = result.workshop.InsuranceWorkshopInfo != null + ? result.workshop.InsuranceWorkshopInfo.EmployerName + : result.workshop.WorkshopFullName, + ConfirmSentlist = result.insurance.ConfirmSentlist, + EmployerId = result.employer.First().EmployerId, + DebtDone = result.insurance.Debt.IsDone, + EmployerApproved = result.insurance.EmployerApproval.IsDone, + InspectionDone = result.insurance.Inspection.IsDone + }); - if (!string.IsNullOrEmpty(searchModel.Year) && searchModel.Year != "0") - query = query.Where(x => x.Year == searchModel.Year); + if (!string.IsNullOrEmpty(searchModel.Year) && searchModel.Year != "0" && + !string.IsNullOrEmpty(searchModel.Month) && searchModel.Month != "0") + query = query.Where(x => x.Year == searchModel.Year && x.Month == searchModel.Month); + else + { + if (!string.IsNullOrEmpty(searchModel.Month) && searchModel.Month != "0") + query = query.Where(x => x.Month == searchModel.Month); - } - if (!string.IsNullOrEmpty(searchModel.WorkShopCode)) - query = query.Where(x => x.WorkShopCode == searchModel.WorkShopCode); + if (!string.IsNullOrEmpty(searchModel.Year) && searchModel.Year != "0") + query = query.Where(x => x.Year == searchModel.Year); + } - if (!string.IsNullOrEmpty(searchModel.WorkShopName)) - query = query.Where(x => x.WorkShopName.Contains(searchModel.WorkShopName)); + if (!string.IsNullOrEmpty(searchModel.WorkShopCode)) + query = query.Where(x => x.WorkShopCode == searchModel.WorkShopCode); + + if (!string.IsNullOrEmpty(searchModel.WorkShopName)) + query = query.Where(x => x.WorkShopName.Contains(searchModel.WorkShopName)); - if (searchModel.WorkshopId > 0) - { - var workshopName = query.FirstOrDefault(u => u.WorkShopId == searchModel.WorkshopId)?.WorkShopName; + if (searchModel.WorkshopId > 0) + { + var workshopName = query.FirstOrDefault(u => u.WorkShopId == searchModel.WorkshopId)?.WorkShopName; - query = query.Where(x => x.WorkShopName.Contains(workshopName)); - } + query = query.Where(x => x.WorkShopName.Contains(workshopName)); + } - if (searchModel.EmployerId > 0) - { - var employerName = query.FirstOrDefault(u => u.EmployerId == searchModel.EmployerId)?.EmployerName; - query = query.Where(x => x.EmployerName.Contains(employerName)); - } + if (searchModel.EmployerId > 0) + { + var employerName = query.FirstOrDefault(u => u.EmployerId == searchModel.EmployerId)?.EmployerName; + query = query.Where(x => x.EmployerName.Contains(employerName)); + } - if (!string.IsNullOrEmpty(searchModel.EmployerName)) - query = query.Where(x => x.EmployerName.Contains(searchModel.EmployerName)); + if (!string.IsNullOrEmpty(searchModel.EmployerName)) + query = query.Where(x => x.EmployerName.Contains(searchModel.EmployerName)); - if (searchModel.FixedSalary != null) - query = query.Where(x => x.FixedSalary == searchModel.FixedSalary); + if (searchModel.FixedSalary != null) + query = query.Where(x => x.FixedSalary == searchModel.FixedSalary); - if (!string.IsNullOrEmpty(searchModel.TypeOfInsuranceSend) && searchModel.TypeOfInsuranceSend != "0") - query = query.Where(x => x.TypeOfInsuranceSend == searchModel.TypeOfInsuranceSend); + if (!string.IsNullOrEmpty(searchModel.TypeOfInsuranceSend) && searchModel.TypeOfInsuranceSend != "0") + query = query.Where(x => x.TypeOfInsuranceSend == searchModel.TypeOfInsuranceSend); - if (!string.IsNullOrEmpty(searchModel.City) && searchModel.City != "0") - query = query.Where(x => x.City == searchModel.City); + if (!string.IsNullOrEmpty(searchModel.City) && searchModel.City != "0") + query = query.Where(x => x.City == searchModel.City); - if (!string.IsNullOrEmpty(searchModel.Branch)) - query = query.Where(x => x.Branch.Contains(searchModel.Branch)); + if (!string.IsNullOrEmpty(searchModel.Branch)) + query = query.Where(x => x.Branch.Contains(searchModel.Branch)); + var res = await query.GroupBy(x => 1) + .Select(g => new InsuranceListTabsCountViewModel + { + NotStarted = g.Count(x => + !x.DebtDone && !x.EmployerApproved && !x.InspectionDone && !x.ConfirmSentlist), + InProgress = g.Count(x => + (x.DebtDone || x.EmployerApproved || x.InspectionDone) && + !(x.DebtDone && x.EmployerApproved && x.InspectionDone) && !x.ConfirmSentlist), + ReadyToSendList = g.Count(x => + x.DebtDone && x.EmployerApproved && x.InspectionDone && !x.ConfirmSentlist), + Done = g.Count(x => x.ConfirmSentlist) + }) + .FirstOrDefaultAsync() ?? new InsuranceListTabsCountViewModel(); - var res = await query.GroupBy(x => 1) - .Select(g => new InsuranceListTabsCountViewModel - { - NotStarted = g.Count(x => - !x.DebtDone && !x.EmployerApproved && !x.InspectionDone && !x.ConfirmSentlist), - InProgress = g.Count(x => - (x.DebtDone || x.EmployerApproved || x.InspectionDone) && !(x.DebtDone && x.EmployerApproved && x.InspectionDone) && !x.ConfirmSentlist), - ReadyToSendList = g.Count(x => - x.DebtDone && x.EmployerApproved && x.InspectionDone && !x.ConfirmSentlist), - Done = g.Count(x => x.ConfirmSentlist) - }) - .FirstOrDefaultAsync() ?? new InsuranceListTabsCountViewModel(); - - return res; - - } + return res; + } public async Task> GetNotCreatedWorkshop(InsuranceListSearchModel searchModel) { - if (string.IsNullOrEmpty(searchModel.Month) || string.IsNullOrEmpty(searchModel.Year) ) + if (string.IsNullOrEmpty(searchModel.Month) || string.IsNullOrEmpty(searchModel.Year)) { return []; } + var acountId = _authHelper.CurrentAccountId(); var accountWorkshopIds = _context.WorkshopAccounts.Where(x => x.AccountId == acountId) .Select(x => x.WorkshopId); var firstDayOfMonth = $"{searchModel.Year}/{searchModel.Month}/01".ToGeorgianDateTime(); var insuranceWorkshops = _context.Workshops - .Where(x=>x.InsuranceCode != null && x.InsuranceCode.Length >=10 && accountWorkshopIds.Contains(x.id) && x.IsActiveString== "true"); - - + .Where(x => x.InsuranceCode != null && x.InsuranceCode.Length >= 10 && accountWorkshopIds.Contains(x.id) && + x.IsActiveString == "true"); + + var existInsurances = _context.InsuranceListSet - .Where(x=>x.Month == searchModel.Month&& x.Year == searchModel.Year).AsQueryable(); - - var notCreatedWorkshop = insuranceWorkshops.Where(w=>!existInsurances.Select(x=>x.WorkshopId).Contains(w.id)); + .Where(x => x.Month == searchModel.Month && x.Year == searchModel.Year).AsQueryable(); + + var notCreatedWorkshop = + insuranceWorkshops.Where(w => !existInsurances.Select(x => x.WorkshopId).Contains(w.id)); notCreatedWorkshop = notCreatedWorkshop .Include(x => x.LeftWorkInsurances) - .Where(x => x.LeftWorkInsurances.Any(l=>l.StartWorkDate <= firstDayOfMonth && - (l.LeftWorkDate == null || l.LeftWorkDate >= firstDayOfMonth))); - - var res = await notCreatedWorkshop - .Join( - _context.WorkshopEmployers - .OrderBy(we => we.WorkshopId) - .ThenBy(we => we.EmployerId) - .GroupBy(we => we.WorkshopId) - .Select(g => g.First()), - workshop => workshop.id, - employer => employer.WorkshopId, - (workshop, workshopEmployer) => new - { - workshop, - workshopEmployer, - employer = _context.Employers - .FirstOrDefault(e => e.id == workshopEmployer.EmployerId) - } - ) - .Select(x=> new InsuranceListViewModel - { - WorkShopId = x.workshop.id, - WorkShopCode = x.workshop.InsuranceCode, - WorkShopName = x.workshop.InsuranceWorkshopInfo != null ? x.workshop.InsuranceWorkshopInfo.WorkshopName : x.workshop.WorkshopFullName, - EmployerName = x.employer != null ? x.employer.FullName : (x.workshop.InsuranceWorkshopInfo != null ? x.workshop.InsuranceWorkshopInfo.EmployerName : x.workshop.WorkshopFullName), - Year = searchModel.Year, - Month = searchModel.Month, - TypeOfInsuranceSend = x.workshop.TypeOfInsuranceSend == "NormalList" ? "عادی" : - x.workshop.TypeOfInsuranceSend == "Govermentlist" ? "کمک دولت" : - x.workshop.TypeOfInsuranceSend == "Familylist" ? "خانوادگی" : "", - FixedSalary = (bool)x.workshop.FixedSalary, - StrFixedSalary = (bool)x.workshop.FixedSalary ? "دارد" : "ندارد", - EmployerId = x.workshopEmployer != null ? x.workshopEmployer.EmployerId : 0, - ArchiveCode = x.workshop.ArchiveCode, - City = x.workshop.City, - }).Skip(searchModel.PageIndex).Take(30).ToListAsync(); + .Where(x => x.LeftWorkInsurances.Any(l => l.StartWorkDate <= firstDayOfMonth && + (l.LeftWorkDate == null || l.LeftWorkDate >= firstDayOfMonth))); + + var workshopList = await notCreatedWorkshop.Skip(searchModel.PageIndex).Take(30).ToListAsync(); + var workshopIds = workshopList.Select(x=>x.id); + + var employers =await _context.WorkshopEmployers + .Where(x=>workshopIds.Contains(x.WorkshopId)) + .GroupBy(x=>x.WorkshopId) + .Select(x=>x.First()).ToListAsync(); + + var res = workshopList.Select(x => + { + var employer = employers.FirstOrDefault(e => e.WorkshopId ==x.id)?.Employer; + + return new InsuranceListViewModel + { + WorkShopId = x.id, + WorkShopCode = x.InsuranceCode, + WorkShopName = x.InsuranceWorkshopInfo != null + ? x.InsuranceWorkshopInfo.WorkshopName + : x.WorkshopFullName, + EmployerName = employer != null + ? employer.FullName + : (x.InsuranceWorkshopInfo != null + ? x.InsuranceWorkshopInfo.EmployerName + : x.WorkshopFullName), + Year = searchModel.Year, + Month = searchModel.Month, + TypeOfInsuranceSend = x.TypeOfInsuranceSend == "NormalList" ? "عادی" : + x.TypeOfInsuranceSend == "Govermentlist" ? "کمک دولت" : + x.TypeOfInsuranceSend == "Familylist" ? "خانوادگی" : "", + FixedSalary = (bool)x.FixedSalary, + StrFixedSalary = (bool)x.FixedSalary ? "دارد" : "ندارد", + EmployerId = employer?.id ?? 0, + ArchiveCode = x.ArchiveCode, + City = x.City, + }; + }).ToList(); return res; - } /// - /// - /// - /// - /// - /// - /// debt / inspection - /// - private string GetInsuranceListFilePath(long mediaId) - { - return _accountContext.Medias.FirstOrDefault(x => x.id == mediaId)?.Path ?? ""; - } + /// + /// + /// + /// + /// + /// debt / inspection + /// + private string GetInsuranceListFilePath(long mediaId) + { + return _accountContext.Medias.FirstOrDefault(x => x.id == mediaId)?.Path ?? ""; + } } \ No newline at end of file