using System.Collections.Generic; using System.Globalization; using System.Linq; using _0_Framework.Application; using _0_Framework.InfraStructure; using Company.Domain.SalaryAidAgg; using CompanyManagment.App.Contracts.Employee; using CompanyManagment.App.Contracts.Fine; using CompanyManagment.App.Contracts.Reward; using CompanyManagment.App.Contracts.SalaryAid; using Microsoft.EntityFrameworkCore; namespace CompanyManagment.EFCore.Repository; public class SalaryAidRepository : RepositoryBase, ISalaryAidRepository { private readonly CompanyContext _companyContext; public SalaryAidRepository(CompanyContext context) : base(context) { _companyContext = context; } public List GetSearchList(SalaryAidSearchViewModel searchViewModel) { var query = _companyContext.SalaryAids.Where(x => x.WorkshopId == searchViewModel.WorkshopId); if (searchViewModel.EmployeeId != 0) query = query.Where(x => x.EmployeeId == searchViewModel.EmployeeId); if (!string.IsNullOrWhiteSpace(searchViewModel.StartDate) && !string.IsNullOrWhiteSpace(searchViewModel.EndDate)) { var startDate = searchViewModel.StartDate.ToGeorgianDateTime(); var endDate = searchViewModel.EndDate.ToGeorgianDateTime(); query = query.Where(x => x.SalaryAidDateTime >= startDate && x.SalaryAidDateTime <= endDate); } var result = query.OrderByDescending(x => x.CreationDate).Skip(searchViewModel.PageIndex).Take(30).AsEnumerable(); var employees = _companyContext.Employees.Where(x => result.Any(a => a.EmployeeId == x.id)).ToList(); var personnelCodes = _companyContext.PersonnelCodeSet .Where(x => result.Any(a => a.EmployeeId == x.EmployeeId && x.WorkshopId == a.WorkshopId)).ToList(); return result.Select(x => new SalaryAidViewModel() { Amount = x.Amount.ToMoney(), CreationDate = x.CreationDate.ToFarsi(), EmployeeFullName = employees.FirstOrDefault(e => e.id == x.EmployeeId)?.FullName, Id = x.id, PersonnelCode = personnelCodes .FirstOrDefault(a => a.WorkshopId == x.WorkshopId && a.EmployeeId == x.EmployeeId)?.PersonnelCode .ToString(), EmployeeId = x.EmployeeId, SalaryAidDateTimeFa = x.SalaryAidDateTime.ToFarsi(), SalaryAidDateTimeGe = x.SalaryAidDateTime, WorkshopId = x.WorkshopId }).ToList(); } public EditSalaryAidViewModel GetDetails(long id) { var entity = _companyContext.SalaryAids.FirstOrDefault(x => x.id == id); if (entity == null) return new(); var res = new EditSalaryAidViewModel() { Id = entity.id, WorkshopId = entity.WorkshopId, EmployeeId = entity.EmployeeId, Amount = entity.Amount.ToMoney(), SalaryDateTime = entity.SalaryAidDateTime.ToFarsi(), CalculationMonth = entity.CalculationMonth, CalculationYear = entity.CalculationYear, }; res.EmployeeFullName = _companyContext.Employees.Find(entity.EmployeeId)?.FullName; return res; } public List GetBy(IEnumerable ids) { return _companyContext.SalaryAids.Where(x => ids.Contains(x.id)).ToList(); } #region Pooya /// /// گروهبندی بر اساس ماه هنگام جستجو با انتخاب کارمند /// public SalaryAidsGroupedViewModel GetSearchListAsGrouped(SalaryAidSearchViewModel searchModel) { var result = new SalaryAidsGroupedViewModel(); var query = _companyContext.SalaryAids.Where(x => x.WorkshopId == searchModel.WorkshopId); var personnelCodes = _companyContext.PersonnelCodeSet .Where(x => x.WorkshopId == searchModel.WorkshopId).ToList(); var employees = _companyContext.LeftWorkList.Where(x => x.WorkshopId == searchModel.WorkshopId) .Include(x => x.Employee).Select(x => x.Employee).ToList(); var pc = new PersianCalendar(); if (searchModel.ShowAsGrouped) { if (searchModel.EmployeeId > 0) { if (!string.IsNullOrWhiteSpace(searchModel.StartDate) && !string.IsNullOrWhiteSpace(searchModel.EndDate)) { var startDate = searchModel.StartDate.ToGeorgianDateTime(); var endDate = searchModel.EndDate.ToGeorgianDateTime(); query = query.Where(x => x.SalaryAidDateTime >= startDate && x.SalaryAidDateTime <= endDate); } query = query.Where(x => x.EmployeeId == searchModel.EmployeeId); var list = query.ToList().Select(x => { var monthInt = pc.GetMonth(x.CalculationDate); var yearInt = pc.GetYear(x.CalculationDate); return new SalaryAidViewModel { Amount = x.Amount.ToMoney(), AmountDouble = x.Amount, EmployeeFullName = employees.FirstOrDefault(e => e.id == x.EmployeeId).FullName, EmployeeId = x.EmployeeId, CreationDate = x.CreationDate.ToFarsi(), Id = x.id, MonthFa = monthInt.ToFarsiMonthByIntNumber(), YearFa = yearInt.ToString(), Month = monthInt, Year = yearInt, PersonnelCode = personnelCodes.FirstOrDefault(p => p.EmployeeId == x.EmployeeId).PersonnelCode .ToString(), SalaryAidDateTimeFa = x.SalaryAidDateTime.ToFarsi(), SalaryAidDateTimeGe = x.SalaryAidDateTime, WorkshopId = x.WorkshopId, CalculationDateTimeGe = x.CalculationDate }; }).ToList(); result.GroupedByDate = list.GroupBy(x => new { x.YearFa, x.MonthFa }).Select(x => new SalaryAidGroupedByDateViewModel() { YearFa = x.Key.YearFa, MonthFa = x.Key.MonthFa, Items = x.Select(s => new SalaryAidGroupedByDateViewModelItems() { Amount = s.Amount, EmployeeName = s.EmployeeFullName, Id = s.Id, PersonnelCode = s.PersonnelCode, SalaryAidDateTimeFa = s.SalaryAidDateTimeFa, }).ToList(), Year = x.First().Year, Month = x.First().Month, TotalAmount = x.Sum(s => s.AmountDouble).ToMoney() }).OrderByDescending(x => x.Year).ThenByDescending(x=>x.Month).ToList(); return result; } else if (!string.IsNullOrWhiteSpace(searchModel.StartDate) && !string.IsNullOrWhiteSpace(searchModel.EndDate)) { var startDate = searchModel.StartDate.ToGeorgianDateTime(); var endDate = searchModel.EndDate.ToGeorgianDateTime(); query = query.Where(x => x.SalaryAidDateTime >= startDate && x.SalaryAidDateTime <= endDate); result.GroupedByEmployee = query.GroupBy(x => x.EmployeeId).ToList() .Select(x => new SalaryAidGroupedByEmployeeViewModel() { EmployeeId = x.Key, TotalAmount = x.Sum(s => s.Amount).ToMoney(), EmployeeName = employees.FirstOrDefault(e => e.id == x.Key).FullName, Items = x.Select(s => new SalaryAidGroupedByEmployeeViewModelItems() { Amount = s.Amount.ToMoney(), Id = s.id, SalaryAidDateTimeFa = s.SalaryAidDateTime.ToFarsi(), PersonnelCode = personnelCodes.FirstOrDefault(p => p.EmployeeId == s.EmployeeId).PersonnelCode.ToString(), }).ToList() }).ToList(); return result; } } if (searchModel.EmployeeId > 0) query = query.Where(x => x.EmployeeId == searchModel.EmployeeId); if (!string.IsNullOrWhiteSpace(searchModel.StartDate) && !string.IsNullOrWhiteSpace(searchModel.EndDate)) { var startDate = searchModel.StartDate.ToGeorgianDateTime(); var endDate = searchModel.EndDate.ToGeorgianDateTime(); query = query.Where(x => x.SalaryAidDateTime >= startDate && x.SalaryAidDateTime <= endDate); } result.List = new PagedResult() { TotalCount = query.Count(), List = query.OrderByDescending(x => x.SalaryAidDateTime).ApplyPagination(searchModel.PageIndex,searchModel.PageSize).ToList() .Select(x => new SalaryAidViewModel() { Amount = x.Amount.ToMoney(), CreationDate = x.CreationDate.ToFarsi(), Id = x.id, EmployeeId = x.EmployeeId, SalaryAidDateTimeFa = x.SalaryAidDateTime.ToFarsi(), SalaryAidDateTimeGe = x.SalaryAidDateTime, WorkshopId = x.WorkshopId, YearFa = x.SalaryAidDateTime.ToFarsi().Substring(0, 4), MonthFa = x.SalaryAidDateTime.ToFarsi().Substring(5, 2), PersonnelCode = personnelCodes.FirstOrDefault(p => p.EmployeeId == x.EmployeeId).PersonnelCode .ToString(), EmployeeFullName = employees.FirstOrDefault(e => e.id == x.EmployeeId).FullName, AmountDouble = x.Amount, }).ToList() }; return result; } #endregion }