Files
Backend-Api/CompanyManagment.EFCore/Repository/CustomizeCheckoutRepository.cs

156 lines
6.1 KiB
C#

using _0_Framework.Application;
using _0_Framework.InfraStructure;
using Company.Domain.CustomizeCheckoutAgg;
using CompanyManagment.App.Contracts.CustomizeCheckout;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace CompanyManagment.EFCore.Repository
{
public class CustomizeCheckoutRepository : RepositoryBase<long, CustomizeCheckout>, ICustomizeCheckoutRepository
{
private readonly CompanyContext _companyContext;
public CustomizeCheckoutRepository(CompanyContext context) : base(context)
{
_companyContext = context;
}
public IEnumerable<CustomizeCheckoutMandatoryViewModel> Search(SearchCustomizeCheckout searchModel)
{
var query = _companyContext.CustomizeCheckouts.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;
startSearchDate = queryStartDate;
endSearchDate = queryEndDate;
}
}
//Month Index operations
startSearchDate.AddMonthsFa(-1 * (searchModel.MonthIndex), out startSearchDate);
startSearchDate.FindFirstDayOfNextMonth(out endSearchDate);
endSearchDate = endSearchDate.AddDays(-1);
#endregion
query = query.Where(x => x.ContractEnd.Date <= endSearchDate && x.ContractEnd.Date >= startSearchDate);
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;
}
return query.Select(x => new CustomizeCheckoutMandatoryViewModel()
{
ContractEndFa = x.ContractEnd.ToFarsi(),
ContractStartFa = x.ContractStart.ToFarsi(),
ContractNo = x.ContractNo,
EmployeeName = x.Employee.FName + " " + x.Employee.LName,
PersonnelCode = x.Employee.PersonnelCodeList.FirstOrDefault(y => y.WorkshopId == searchModel.WorkshopId).PersonnelCode,
Month = pc.GetMonth(startSearchDate),
Year = pc.GetYear(endSearchDate),
BaseYearsPay = x.BaseYearsPay,
BonusesPay = x.BonusesPay,
EarlyExitDeduction = x.EarlyExitDeduction,
FamilyAllowance = x.FamilyAllowance,
FineAbsenceDeduction = x.FineAbsenceDeduction,
FineDeduction = x.FineDeduction,
FridayPay = x.FridayPay,
InstallmentDeduction = x.InstallmentDeduction,
InsuranceDeduction = x.InsuranceDeduction,
LateToWorkDeduction = x.LateToWorkDeduction,
LeavePay = x.LeavePay,
MarriedAllowance = x.MarriedAllowance,
MonthlySalary = x.MonthlySalary,
NightWorkPay = x.NightWorkPay,
OverTimePay = x.OverTimePay,
RewardPay = x.RewardPay,
SalaryAidDeduction = x.SalaryAidDeduction,
ShiftPay = x.ShiftPay,
SumOfWorkingDays = x.SumOfWorkingDays,
TaxDeduction = x.TaxDeduction,
TotalClaims = x.TotalClaims,
TotalDeductions = x.TotalDeductions,
TotalPayment = x.TotalPayment
}).ToList();
}
}
}