407 lines
19 KiB
C#
407 lines
19 KiB
C#
|
|
using _0_Framework.Application;
|
|
using _0_Framework.InfraStructure;
|
|
using Company.Domain.CustomizeCheckoutTempAgg;
|
|
using Company.Domain.empolyerAgg;
|
|
using Company.Domain.PersonnelCodeAgg;
|
|
using Company.Domain.RollCallAgg;
|
|
using Company.Domain.WorkshopAgg;
|
|
using CompanyManagment.App.Contracts.CustomizeCheckout;
|
|
using CompanyManagment.App.Contracts.Employer;
|
|
using CompanyManagment.App.Contracts.Fine;
|
|
using CompanyManagment.App.Contracts.Loan;
|
|
using CompanyManagment.App.Contracts.PersonnleCode;
|
|
using CompanyManagment.App.Contracts.Reward;
|
|
using CompanyManagment.App.Contracts.RollCall;
|
|
using CompanyManagment.App.Contracts.SalaryAid;
|
|
using CompanyManagment.App.Contracts.Workshop;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
|
|
namespace CompanyManagment.EFCore.Repository
|
|
{
|
|
public class CustomizeCheckoutTempRepository : RepositoryBase<long, CustomizeCheckoutTemp>, ICustomizeCheckoutTempRepository
|
|
{
|
|
private readonly IRollCallRepository _rollCallRepository;
|
|
private readonly CompanyContext _companyContext;
|
|
public CustomizeCheckoutTempRepository(CompanyContext context, IRollCallRepository rollCallRepository) : base(context)
|
|
{
|
|
_companyContext = context;
|
|
_rollCallRepository = rollCallRepository;
|
|
}
|
|
#region Pooya
|
|
public List<CustomizeCheckoutViewModel> GetByWorkshopIdInDates(long workshopId, DateTime startOfMonth, DateTime endOfMonth)
|
|
{
|
|
return _companyContext.CustomizeCheckoutTemps.Where(x => x.WorkshopId == workshopId && x.ContractEnd.Date >= startOfMonth.Date &&
|
|
x.ContractStart.Date <= endOfMonth.Date).Select(x => new CustomizeCheckoutViewModel
|
|
{
|
|
EmployeeId = x.EmployeeId,
|
|
BaseYearsPay = x.BaseYearsPay.ToMoney(),
|
|
BonusesPay = x.BonusesPay.ToMoney(),
|
|
ContractEndFa = x.ContractEnd.ToFarsi(),
|
|
ContractNo = x.ContractNo,
|
|
ContractStartFa = x.ContractStart.ToFarsi(),
|
|
EarlyExitDeduction = x.EarlyExitDeduction.ToMoney(),
|
|
FamilyAllowance = x.FamilyAllowance.ToMoney(),
|
|
AbsenceDeduction = x.FineAbsenceDeduction.ToMoney(),
|
|
FineDeduction = x.FineDeduction.ToMoney(),
|
|
FridayPay = x.FridayPay.ToMoney(),
|
|
InstallmentDeduction = x.InstallmentDeduction.ToMoney(),
|
|
InsuranceDeduction = x.InsuranceDeduction.ToMoney(),
|
|
LateToWorkDeduction = x.LateToWorkDeduction.ToMoney(),
|
|
LeavePay = x.LeavePay.ToMoney(),
|
|
MarriedAllowance = x.MarriedAllowance.ToMoney(),
|
|
MonthlySalary = x.MonthlySalary.ToMoney(),
|
|
NightworkPay = x.NightWorkPay.ToMoney(),
|
|
OvertimePay = x.OverTimePay.ToMoney(),
|
|
RewardPay = x.RewardPay.ToMoney(),
|
|
SalaryAidDeduction = x.SalaryAidDeduction.ToMoney(),
|
|
ShiftPay = x.ShiftPay.ToMoney(),
|
|
TaxDeducation = x.TaxDeduction.ToMoney(),
|
|
SumOfWorkingDays = x.SumOfWorkingDays.ToString(),
|
|
}).ToList();
|
|
}
|
|
|
|
#endregion
|
|
|
|
public IEnumerable<CustomizeCheckoutViewModel> Search(SearchCustomizeCheckout searchModel)
|
|
{
|
|
|
|
|
|
|
|
var query = _companyContext.CustomizeCheckoutTemps.Include(x => x.Employee).ThenInclude(x => x.PersonnelCodeList)
|
|
.AsSplitQuery().Where(x => x.WorkshopId == searchModel.WorkshopId);
|
|
#region parameters initialize
|
|
|
|
//start of search is the first day of the current month by default and end of search is today
|
|
var startSearchDate = DateTime.Now.FindFirstDayOfMonth().ToGeorgianDateTime().Date;
|
|
var endSearchDate = DateTime.Today;
|
|
|
|
var pc = new PersianCalendar();
|
|
var currentYear = pc.GetYear(DateTime.Now);
|
|
var currentMonth = pc.GetMonth(DateTime.Now);
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.SearchStartFa) && !string.IsNullOrWhiteSpace(searchModel.SearchEndFa) &&
|
|
searchModel.Year == 0 && searchModel.Month == 0)
|
|
{
|
|
var queryStartDate = searchModel.SearchStartFa.ToGeorgianDateTime().Date;
|
|
var queryEndDate = searchModel.SearchEndFa.ToGeorgianDateTime().Date;
|
|
|
|
if (queryEndDate > queryStartDate && queryEndDate <= DateTime.Today)
|
|
{
|
|
startSearchDate = queryStartDate;
|
|
endSearchDate = queryEndDate;
|
|
}
|
|
query = query.Where(x => x.ContractEnd.Date >= startSearchDate && x.ContractStart.Date <= endSearchDate);
|
|
}
|
|
|
|
|
|
if (searchModel.Year > 0 && searchModel.Month > 0 && searchModel.Month < 12)
|
|
{
|
|
var queryStartDate = $"{searchModel.Year:0000}/{searchModel.Month:00}/01".ToGeorgianDateTime();
|
|
queryStartDate.FindFirstDayOfNextMonth(out var queryEndDate);
|
|
queryEndDate = queryEndDate.AddDays(-1);
|
|
|
|
|
|
|
|
|
|
if (queryEndDate < DateTime.Today)
|
|
{
|
|
startSearchDate = queryStartDate;
|
|
endSearchDate = queryEndDate;
|
|
}
|
|
|
|
else if (searchModel.Year == currentYear && searchModel.Month == currentMonth)
|
|
{
|
|
queryEndDate = DateTime.Today.AddDays(-1);
|
|
|
|
startSearchDate = queryStartDate;
|
|
endSearchDate = queryEndDate;
|
|
}
|
|
query = query.Where(x => x.ContractEnd.Date <= endSearchDate && x.ContractEnd.Date >= startSearchDate);
|
|
|
|
}
|
|
|
|
|
|
////Month Index operations
|
|
//startSearchDate.AddMonthsFa(-1 * (searchModel.MonthIndex), out startSearchDate);
|
|
//startSearchDate.FindFirstDayOfNextMonth(out endSearchDate);
|
|
//endSearchDate = endSearchDate.AddDays(-1);
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
if (searchModel.EmployeeId > 0)
|
|
query = query.Where(x => x.EmployeeId == searchModel.EmployeeId);
|
|
|
|
|
|
|
|
switch (searchModel.OrderBy)
|
|
{
|
|
case CustomizeCheckoutOrderByEnum.ContractStartDesc:
|
|
query = query.OrderByDescending(x => x.ContractStart.Date);
|
|
break;
|
|
case CustomizeCheckoutOrderByEnum.ContractStart:
|
|
query = query.OrderBy(x => x.ContractStart.Date);
|
|
break;
|
|
case CustomizeCheckoutOrderByEnum.ContractNoDesc:
|
|
query = query.OrderByDescending(x => x.ContractNo);
|
|
break;
|
|
case CustomizeCheckoutOrderByEnum.ContractNo:
|
|
query = query.OrderBy(x => x.ContractNo);
|
|
break;
|
|
default:
|
|
query = query.OrderByDescending(x => x.ContractStart.Date);
|
|
break;
|
|
|
|
}
|
|
|
|
if (searchModel.Month == 0 || searchModel.Year == 0)
|
|
query = query.Skip(searchModel.PageIndex).Take(30);
|
|
|
|
|
|
return query.Select(x => new CustomizeCheckoutViewModel()
|
|
{
|
|
Id = x.id,
|
|
ContractEndFa = x.ContractEnd.ToFarsi(),
|
|
ContractStartFa = x.ContractStart.ToFarsi(),
|
|
ContractNo = x.ContractNo,
|
|
EmployeeFName = x.EmployeeFName,
|
|
EmployeeLName = x.EmployeeLName,
|
|
|
|
PersonnelCode = x.Employee.PersonnelCodeList.FirstOrDefault(y => y.WorkshopId == searchModel.WorkshopId).PersonnelCode,
|
|
Month = pc.GetMonth(x.ContractStart).ToFarsiMonthByIntNumber(),
|
|
Year = pc.GetYear(x.ContractStart).ToString(),
|
|
BaseYearsPay = x.BaseYearsPay.ToMoney(),
|
|
BonusesPay = x.BonusesPay.ToMoney(),
|
|
EarlyExitDeduction = x.EarlyExitDeduction.ToMoney(),
|
|
FamilyAllowance = x.FamilyAllowance.ToMoney(),
|
|
AbsenceDeduction = x.FineAbsenceDeduction.ToMoney(),
|
|
FineDeduction = x.FineDeduction.ToMoney(),
|
|
FridayPay = x.FridayPay.ToMoney(),
|
|
InstallmentDeduction = x.InstallmentDeduction.ToMoney(),
|
|
InsuranceDeduction = x.InsuranceDeduction.ToMoney(),
|
|
LateToWorkDeduction = x.LateToWorkDeduction.ToMoney(),
|
|
LeavePay = x.LeavePay.ToMoney(),
|
|
MarriedAllowance = x.MarriedAllowance.ToMoney(),
|
|
MonthlySalary = x.MonthlySalary.ToMoney(),
|
|
NightworkPay = x.NightWorkPay.ToMoney(),
|
|
OvertimePay = x.OverTimePay.ToMoney(),
|
|
RewardPay = x.RewardPay.ToMoney(),
|
|
SalaryAidDeduction = x.SalaryAidDeduction.ToMoney(),
|
|
ShiftPay = x.ShiftPay.ToMoney(),
|
|
SumOfWorkingDays = x.SumOfWorkingDays.ToString(),
|
|
TaxDeducation = x.TaxDeduction.ToMoney(),
|
|
}).ToList();
|
|
|
|
|
|
}
|
|
|
|
public List<CustomizeCheckoutViewModel> PrintAll(long workshopId, IEnumerable<long> customizeCheckoutIds)
|
|
{
|
|
|
|
//var pc = new PersianCalendar();
|
|
//var year = pc.GetYear(monthStart);
|
|
//var month = pc.GetMonth(monthStart);
|
|
|
|
IQueryable<CustomizeCheckoutTemp> customizeCheckoutsQuery = _companyContext.CustomizeCheckoutTemps.Where(x => customizeCheckoutIds.Contains(x.id));
|
|
|
|
IQueryable<Workshop> workshopsQuery = _companyContext.Workshops.Where(x => customizeCheckoutsQuery.Any(y => y.WorkshopId == x.id));
|
|
|
|
IQueryable<long> workshopEmployersIdsQuery = _companyContext.WorkshopEmployers.Where(x => x.WorkshopId == workshopId).Select(x => x.EmployerId);
|
|
IQueryable<Employer> employersQuery = _companyContext.Employers.Where(x => workshopEmployersIdsQuery.Contains(x.id));
|
|
|
|
IQueryable<PersonnelCodeDomain> personnelCodesQuery = _companyContext.PersonnelCodeSet
|
|
.Where(x => customizeCheckoutsQuery.Any(y => y.WorkshopId == x.WorkshopId && y.EmployeeId == x.EmployeeId));
|
|
|
|
//IQueryable<LeftWork> leftWorksQuery = _companyContext.LeftWorkList
|
|
// .Where(x => customizeCheckoutsQuery.Any(y => y.WorkshopId == x.WorkshopId && y.EmployeeId == x.EmployeeId) &&
|
|
// x.LeftWorkDate.AddDays(-1) >= monthStart && x.StartWorkDate <= monthEnd);
|
|
|
|
//IQueryable<Employee> employeesQuery = _companyContext.Employees.Where(x => customizeCheckoutsQuery.Any(y => y.EmployeeId == x.id));
|
|
|
|
|
|
|
|
List<CustomizeCheckoutViewModel> customizeCheckoutsList = customizeCheckoutsQuery.Select(x => new CustomizeCheckoutViewModel
|
|
{
|
|
Id = x.id,
|
|
WorkshopId = x.WorkshopId,
|
|
ContractId = x.ContractId == null ? 0 : x.ContractId.Value,
|
|
EmployeeId = x.EmployeeId,
|
|
Month = x.Month,
|
|
Year = x.Year,
|
|
ContractNo = x.ContractNo,
|
|
MonthlySalary = x.MonthlySalary.ToMoney(),
|
|
BaseYearsPay = x.BaseYearsPay.ToMoney(),
|
|
OvertimePay = x.OverTimePay.ToMoney(),
|
|
NightworkPay = x.NightWorkPay.ToMoney(),
|
|
FridayPay = x.FridayPay.ToMoney(),
|
|
ShiftPay = x.ShiftPay.ToMoney(),
|
|
FamilyAllowance = x.FamilyAllowance.ToMoney(),
|
|
BonusesPay = x.BonusesPay.ToMoney(),
|
|
LeavePay = x.LeavePay.ToMoney(),
|
|
InsuranceDeduction = x.InsuranceDeduction.ToMoney(),
|
|
TaxDeducation = x.TaxDeduction.ToMoney(),
|
|
InstallmentDeduction = x.InstallmentDeduction.ToMoney(),
|
|
SalaryAidDeduction = x.SalaryAidDeduction.ToMoney(),
|
|
AbsenceDeduction = x.FineAbsenceDeduction.ToMoney(),
|
|
TotalClaims = x.TotalClaims,
|
|
TotalDeductions = x.TotalDeductions,
|
|
TotalPayment = x.TotalPayment.ToMoney(),
|
|
RewardPay = x.RewardPay.ToMoney(),
|
|
ContractStartGr = x.ContractStart,
|
|
ContractEndGr = x.ContractEnd,
|
|
MarriedAllowance = x.MarriedAllowance.ToMoney(),
|
|
ContractEndFa = x.ContractEnd.ToFarsi(),
|
|
ContractStartFa = x.ContractStart.ToFarsi(),
|
|
CreationDate = x.CreationDate,
|
|
SumOfWorkingDays = x.SumOfWorkingDays,
|
|
WorkshopName = x.WorkshopFullName,
|
|
DateOfBirth = x.DateOfBirth.ToFarsi(),
|
|
NationalCode = x.NationalCode,
|
|
EmployeeFName = x.EmployeeFName,
|
|
EmployeeLName = x.EmployeeLName,
|
|
EarlyExitDeduction = x.EarlyExitDeduction.ToMoney(),
|
|
LateToWorkDeduction = x.LateToWorkDeduction.ToMoney(),
|
|
FineDeduction = x.FineDeduction.ToMoney(),
|
|
FineViewModelList = x.CheckoutFines.Select(y => new FineViewModel()
|
|
{
|
|
Amount = y.Amount,
|
|
FineDate = y.FineDateFa,
|
|
Title = y.Title
|
|
}).ToList(),
|
|
InstallmentViewModels = x.CustomizeCheckoutLoanInstallments.Select(i => new LoanInstallmentViewModel()
|
|
{
|
|
//موقتا مبلغ کل وام بجای قسط ارسال شده است
|
|
Amount = i.LoanAmount,
|
|
AmountDouble = i.AmountForMonth.MoneyToDouble(),
|
|
Year = i.Year,
|
|
Month = i.Month,
|
|
IsActive = i.IsActive,
|
|
RemainingAmount = i.LoanRemaining,
|
|
LoanAmount = i.LoanAmount
|
|
|
|
|
|
}).ToList(),
|
|
RewardViewModels = x.CustomizeCheckoutRewards.Select(r => new RewardViewModel()
|
|
{
|
|
IsActive = r.IsActive,
|
|
Title = r.Title,
|
|
Amount = r.Amount,
|
|
AmountDouble = r.Amount.MoneyToDouble(),
|
|
Description = r.Description,
|
|
GrantDateFa = r.GrantDateFa,
|
|
GrantDateGr = r.GrantDate
|
|
}).ToList(),
|
|
SalaryAidViewModels = x.CustomizeCheckoutSalaryAids.Select(s => new SalaryAidViewModel()
|
|
{
|
|
Amount = s.Amount,
|
|
AmountDouble = s.Amount.MoneyToDouble(),
|
|
SalaryAidDateTimeFa = s.SalaryAidDateTimeFa,
|
|
SalaryAidDateTimeGe = s.SalaryAidDateTime
|
|
}).ToList(),
|
|
|
|
}).ToList();
|
|
List<WorkshopViewModel> workshopsList = workshopsQuery.Select(x => new WorkshopViewModel
|
|
{
|
|
WorkshopFullName = x.WorkshopFullName,
|
|
Id = x.id
|
|
}).ToList();
|
|
List<EmployerViewModel> workshopEmployersList = employersQuery.Select(x => new EmployerViewModel
|
|
{
|
|
Id = x.id,
|
|
FullName = x.FullName
|
|
}).ToList();
|
|
//List<LeftWorkViewModel> leftWorksList = leftWorksQuery.Select(x => new LeftWorkViewModel
|
|
//{
|
|
// EmployeeId = x.EmployeeId,
|
|
// WorkshopId = x.WorkshopId,
|
|
// Id = x.id,
|
|
// StartWorkDateGr = x.StartWorkDate,
|
|
// LeftWorkDateGr = x.LeftWorkDate
|
|
//}).ToList();
|
|
//List<EmployeeViewModel> employees = employeesQuery.Select(x => new EmployeeViewModel()
|
|
//{
|
|
// Id = x.id,
|
|
// FName = x.FName,
|
|
// LName = x.LName,
|
|
// FatherName = x.FatherName,
|
|
// NationalCode = x.NationalCode,
|
|
// DateOfBirth = x.DateOfBirth.ToFarsi()
|
|
//}).ToList();
|
|
List<PersonnelCodeViewModel> personnelCodeList = personnelCodesQuery.Select(x => new PersonnelCodeViewModel
|
|
{
|
|
EmployeeId = x.EmployeeId,
|
|
WorkshopId = x.WorkshopId,
|
|
PersonnelCode = Convert.ToInt64(x.PersonnelCode)
|
|
}).ToList();
|
|
|
|
var date = customizeCheckoutsList.FirstOrDefault();
|
|
|
|
if (date == null)
|
|
return new();
|
|
|
|
var startDate = date.ContractStartGr.AddMonthsFa(0, out _).ToGeorgianDateTime().Date;
|
|
var endDate = startDate.AddMonthsFa(1, out _).ToGeorgianDateTime().Date.AddTicks(-1);
|
|
|
|
|
|
List<PersonnelCheckoutDailyRollCallViewModel> personnelRollCalls = _rollCallRepository
|
|
.GetEmployeeRollCallsInDates(customizeCheckoutsList.Select(x => x.EmployeeId).ToList(), workshopId, startDate, endDate);
|
|
|
|
int counter = 1;
|
|
foreach (var checkout in customizeCheckoutsList)
|
|
{
|
|
checkout.PrintCounter = counter++;
|
|
//var leftwork = leftWorksList.FirstOrDefault(x => checkout.WorkshopId == x.WorkshopId && x.EmployeeId == checkout.EmployeeId);
|
|
//checkout.LeftWorkDateGr = leftwork.LeftWorkDateGr;
|
|
|
|
//var employee = employees.FirstOrDefault(x => x.Id == checkout.EmployeeId);
|
|
var rollCalls = personnelRollCalls.FirstOrDefault(x => x.EmployeeId == checkout.EmployeeId);
|
|
|
|
checkout.EmployerList = workshopEmployersList;
|
|
checkout.EmployerName = workshopEmployersList.FirstOrDefault()?.FullName ?? "-";
|
|
|
|
checkout.MonthlyRollCall =rollCalls;
|
|
checkout.MonthlyRollCall.DailyRollCalls = rollCalls.DailyRollCalls.Select(x =>
|
|
{
|
|
var isInRange = x.DateTimeGr >= checkout.ContractStartGr && x.DateTimeGr <= checkout.ContractEndGr;
|
|
return new CheckoutDailyRollCallViewModel()
|
|
{
|
|
StartDate1 = isInRange ? x.StartDate1 : "",
|
|
EndDate1 = isInRange ? x.EndDate1 : "",
|
|
EndDate2 = isInRange ? x.EndDate2 : "",
|
|
StartDate2 = isInRange ? x.StartDate2 : "",
|
|
TotalWorkingHours = isInRange?x.TotalWorkingHours : "",
|
|
DayOfWeek = x.DayOfWeek,
|
|
RollCallDateFa = x.RollCallDateFa,
|
|
DateTimeGr = x.DateTimeGr,
|
|
IsSliced = isInRange && x.IsSliced
|
|
};
|
|
}).ToList();
|
|
checkout.PersonnelCode = personnelCodeList.FirstOrDefault(x => x.EmployeeId == checkout.EmployeeId)?.PersonnelCode ?? 0;
|
|
|
|
}
|
|
return customizeCheckoutsList.OrderBy(x => x.EmployeeFullName).ToList();
|
|
|
|
}
|
|
|
|
public void RemoveEmployeeTemporaryCheckoutInDates(long workshopId, long employeeId, DateTime startOfMonth, DateTime endOfMonth)
|
|
{
|
|
var checkout = _companyContext.CustomizeCheckoutTemps.FirstOrDefault(x => x.WorkshopId == workshopId && x.EmployeeId == employeeId &&
|
|
x.ContractStart.Date <= endOfMonth.Date && x.ContractEnd.Date >= startOfMonth.Date);
|
|
|
|
if (checkout != null)
|
|
_companyContext.CustomizeCheckoutTemps.Remove(checkout);
|
|
}
|
|
|
|
public IEnumerable<CustomizeCheckoutTemp> GetRange(long workshopId,List<long> ids)
|
|
{
|
|
return _companyContext.CustomizeCheckoutTemps.Where(x =>x.WorkshopId==workshopId && ids.Contains(x.id));
|
|
}
|
|
}
|
|
}
|