849 lines
34 KiB
C#
849 lines
34 KiB
C#
using _0_Framework.Application;
|
|
using _0_Framework.InfraStructure;
|
|
using Company.Domain.LeaveAgg;
|
|
using CompanyManagment.App.Contracts.Checkout;
|
|
using CompanyManagment.App.Contracts.Leave;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace CompanyManagment.EFCore.Repository;
|
|
|
|
public class LeaveRepository : RepositoryBase<long, Leave>, ILeaveRepository
|
|
{
|
|
private readonly CompanyContext _context;
|
|
|
|
public LeaveRepository(CompanyContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
|
|
}
|
|
|
|
public EditLeave GetDetails(long id)
|
|
{
|
|
return _context.LeaveList.Select(x => new EditLeave()
|
|
{
|
|
Id = x.id,
|
|
StartLeave = x.StartLeave.ToFarsi(),
|
|
EndLeave = x.EndLeave.ToFarsi(),
|
|
StartLeaveGr = x.StartLeave,
|
|
EndLeaveGr = x.EndLeave,
|
|
WorkshopId = x.WorkshopId,
|
|
EmployeeId = x.EmployeeId,
|
|
LeaveHourses = x.LeaveHourses,
|
|
PaidLeaveType = x.PaidLeaveType,
|
|
LeaveType = x.LeaveType,
|
|
EmployeeFullName = x.EmployeeFullName,
|
|
WorkshopName = x.WorkshopName,
|
|
IsAccepted = x.IsAccepted,
|
|
Decription = x.Decription,
|
|
Year = x.Year,
|
|
Month = x.Month,
|
|
HasRollCall = x.HasShiftDuration,
|
|
}).FirstOrDefault(x => x.Id == id);
|
|
}
|
|
|
|
public List<LeaveViewModel> search(LeaveSearchModel searchModel)
|
|
{
|
|
var query = _context.LeaveList.Select(x => new LeaveViewModel()
|
|
{
|
|
Id = x.id,
|
|
StartLeave = x.StartLeave.ToFarsi(),
|
|
EndLeave = x.EndLeave.ToFarsi(),
|
|
StartLeaveGr = x.StartLeave,
|
|
EndLeaveGr = x.EndLeave,
|
|
WorkshopId = x.WorkshopId,
|
|
EmployeeId = x.EmployeeId,
|
|
LeaveHourses = x.LeaveHourses,
|
|
PaidLeaveType = x.PaidLeaveType,
|
|
LeaveType = x.LeaveType,
|
|
EmployeeFullName = x.EmployeeFullName,
|
|
WorkshopName = x.WorkshopName,
|
|
IsAccepted = x.IsAccepted,
|
|
Decription = x.Decription,
|
|
Year = x.Year,
|
|
Month = x.Month,
|
|
HasShiftDuration = x.HasShiftDuration,
|
|
ShiftDuration = x.ShiftDuration,
|
|
IsInvalid = x.IsInvalid
|
|
});
|
|
if (searchModel.IsInvalid)
|
|
{
|
|
query = query.IgnoreQueryFilters().Where(x => x.IsInvalid);
|
|
}
|
|
if (searchModel.WorkshopId != 0 && searchModel.EmployeeId != 0)
|
|
query = query.Where(x => x.WorkshopId == searchModel.WorkshopId && x.EmployeeId == searchModel.EmployeeId);
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.LeaveType))
|
|
query = query.Where(x => x.LeaveType == searchModel.LeaveType);
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.PaidLeaveType))
|
|
query = query.Where(x => x.PaidLeaveType == searchModel.PaidLeaveType);
|
|
if (!string.IsNullOrWhiteSpace(searchModel.StartLeave) && !string.IsNullOrWhiteSpace(searchModel.EndLeave))
|
|
{
|
|
DateTime startSearch = searchModel.StartLeave.ToGeorgianDateTime();
|
|
DateTime endSearch = searchModel.EndLeave.ToGeorgianDateTime();
|
|
query = query.Where(x => (startSearch <= x.StartLeaveGr.Date && endSearch >= x.StartLeaveGr.Date) ||
|
|
(startSearch <= x.EndLeaveGr.Date && endSearch >= x.EndLeaveGr.Date) ||
|
|
(startSearch >= x.StartLeaveGr.Date && startSearch <= x.EndLeaveGr.Date) ||
|
|
(endSearch >= x.StartLeaveGr.Date && endSearch <= x.EndLeaveGr.Date));
|
|
}
|
|
else if (searchModel.StartLeaveGr != null && searchModel.EndLeaveGr != null)
|
|
{
|
|
query = query.Where(x =>
|
|
(searchModel.StartLeaveGr <= x.StartLeaveGr.Date && searchModel.EndLeaveGr >= x.StartLeaveGr.Date) ||
|
|
(searchModel.StartLeaveGr <= x.EndLeaveGr.Date && searchModel.EndLeaveGr >= x.EndLeaveGr.Date) ||
|
|
(searchModel.StartLeaveGr >= x.StartLeaveGr.Date && searchModel.StartLeaveGr <= x.EndLeaveGr.Date) ||
|
|
(searchModel.EndLeaveGr >= x.StartLeaveGr.Date && searchModel.EndLeaveGr <= x.EndLeaveGr.Date));
|
|
}
|
|
|
|
if (searchModel.IsAccepted)
|
|
query = query.Where(x => x.IsAccepted == true);
|
|
return query.OrderByDescending(x => x.Id).ToList();
|
|
}
|
|
|
|
public List<LeaveMainViewModel> searchClient(LeaveSearchModel searchModel)
|
|
{
|
|
var leaveMainList = _context.LeaveList.Where(x =>
|
|
x.WorkshopId == searchModel.WorkshopId && x.EmployeeId == searchModel.EmployeeId);
|
|
if (searchModel.IsInvalid)
|
|
{
|
|
leaveMainList = leaveMainList.IgnoreQueryFilters().Where(x => x.IsInvalid);
|
|
}
|
|
|
|
if (searchModel.LeaveType == "paidLeave")
|
|
leaveMainList = leaveMainList.Where(x => x.LeaveType == "استحقاقی");
|
|
|
|
if (searchModel.LeaveType == "sickLeave")
|
|
leaveMainList = leaveMainList.Where(x => x.LeaveType == "استعلاجی");
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.StartLeave) && !string.IsNullOrWhiteSpace(searchModel.EndLeave))
|
|
{
|
|
var start = searchModel.StartLeave.ToGeorgianDateTime();
|
|
var end = searchModel.EndLeave.ToGeorgianDateTime();
|
|
leaveMainList = leaveMainList.Where(x => x.StartLeave >= start && x.EndLeave <= end);
|
|
}
|
|
else if (searchModel.Year != 0 || searchModel.Month != 0)
|
|
{
|
|
if (searchModel.Year != 0)
|
|
leaveMainList = leaveMainList.Where(x => x.Year == searchModel.Year);
|
|
|
|
if (searchModel.Month != 0)
|
|
leaveMainList = leaveMainList.Where(x => x.Month == searchModel.Month);
|
|
}
|
|
|
|
var leaveList = leaveMainList.GroupBy(x => new { x.Year, x.Month })
|
|
.OrderByDescending(group => group.Key.Year)
|
|
.ThenByDescending(group => group.Key.Month)
|
|
.Select(group => new LeaveMainViewModel
|
|
{
|
|
Year = group.Key.Year,
|
|
Month = group.Key.Month,
|
|
MonthStr = group.Key.Month.ToFarsiMonthByIntNumber(),
|
|
LeaveList = group.Select(item => new LeaveViewModel
|
|
{
|
|
Id = item.id,
|
|
StartLeave = item.StartLeave.ToFarsi(),
|
|
EndLeave = item.EndLeave.ToFarsi(),
|
|
StartLeaveGr = item.StartLeave,
|
|
EndLeaveGr = item.EndLeave,
|
|
WorkshopId = item.WorkshopId,
|
|
EmployeeId = item.EmployeeId,
|
|
LeaveHourses = Tools.CalculateLeaveHoursAndDays(item.PaidLeaveType, item.LeaveHourses),
|
|
PaidLeaveType = item.PaidLeaveType,
|
|
LeaveType = item.LeaveType,
|
|
IsAccepted = item.IsAccepted,
|
|
Decription = item.Decription,
|
|
Year = item.Year,
|
|
Month = item.Month
|
|
}).OrderByDescending(x => x.StartLeaveGr).ToList()
|
|
}).ToList();
|
|
|
|
return leaveList;
|
|
}
|
|
|
|
public LeavePrintViewModel PrintOne(long id)
|
|
{
|
|
var leave = _context.LeaveList.IgnoreQueryFilters().Select(x => new LeavePrintViewModel()
|
|
{
|
|
Id = x.id,
|
|
ContractNo = "",
|
|
EmployeeId = x.EmployeeId,
|
|
EmployeeFullName = x.EmployeeFullName,
|
|
NationalCode = "",
|
|
WorkshopId = x.WorkshopId,
|
|
WorkshopName = x.WorkshopName,
|
|
StartLeave = x.StartLeave.ToFarsi(),
|
|
EndLeave = x.EndLeave.ToFarsi(),
|
|
StartLeaveGr = x.StartLeave,
|
|
EndLeaveGr = x.EndLeave,
|
|
LeaveType = x.LeaveType,
|
|
PaidLeaveType = x.PaidLeaveType,
|
|
LeaveHourses = Tools.CalculateLeaveHoursAndDays(x.PaidLeaveType, x.LeaveHourses),
|
|
IsAccepted = x.IsAccepted,
|
|
Decription = x.Decription,
|
|
EmployerList = new List<EmprViewModel>(),
|
|
IsInvalid = x.IsInvalid
|
|
}).SingleOrDefault(x => x.Id == id);
|
|
|
|
//leave.ContractNo = _context.Contracts.FirstOrDefault(x => x.ContarctStart <= leave.StartLeaveGr && x.ContractEnd > leave.StartLeaveGr
|
|
// && x.ContarctStart < leave.EndLeaveGr && x.ContractEnd > leave.EndLeaveGr
|
|
// && x.WorkshopIds == leave.WorkshopId && x.EmployeeId == leave.EmployeeId).ContractNo;
|
|
leave.ContractNo = _context.Contracts.FirstOrDefault(x =>
|
|
x.ContarctStart <= leave.StartLeaveGr && x.ContractEnd >= leave.StartLeaveGr &&
|
|
x.EmployeeId == leave.EmployeeId && x.WorkshopIds == leave.WorkshopId)?.ContractNo;
|
|
|
|
var Employee = _context.Employees.SingleOrDefault(x => x.id == leave.EmployeeId);
|
|
leave.NationalCode = Employee.NationalCode;
|
|
|
|
var emp = _context.WorkshopEmployers.Where(x => x.WorkshopId == leave.WorkshopId).Select(x => x.EmployerId)
|
|
.ToList();
|
|
var employerlist = _context.Employers.Select(x => new EmprViewModel()
|
|
{
|
|
Id = x.id,
|
|
EmployerFullName = x.FName + " " + x.LName,
|
|
IsLegal = x.IsLegal,
|
|
}).Where(x => emp.Contains(x.Id)).ToList();
|
|
if (employerlist.Count > 0)
|
|
{
|
|
leave.EmployerList = employerlist;
|
|
}
|
|
else
|
|
{
|
|
leave.EmployerList = new List<EmprViewModel>();
|
|
}
|
|
|
|
return leave;
|
|
}
|
|
|
|
public List<LeavePrintViewModel> PrintAll(List<long> id)
|
|
{
|
|
var query = new List<LeavePrintViewModel>();
|
|
|
|
foreach (var item in id)
|
|
{
|
|
var leave = _context.LeaveList.IgnoreQueryFilters().Select(x => new LeavePrintViewModel
|
|
{
|
|
Id = x.id,
|
|
ContractNo = "",
|
|
EmployeeId = x.EmployeeId,
|
|
EmployeeFullName = x.EmployeeFullName,
|
|
NationalCode = "",
|
|
WorkshopId = x.WorkshopId,
|
|
WorkshopName = x.WorkshopName,
|
|
StartLeave = x.StartLeave.ToFarsi(),
|
|
EndLeave = x.EndLeave.ToFarsi(),
|
|
StartLeaveGr = x.StartLeave,
|
|
EndLeaveGr = x.EndLeave,
|
|
LeaveType = x.LeaveType,
|
|
PaidLeaveType = x.PaidLeaveType,
|
|
LeaveHourses = Tools.CalculateLeaveHoursAndDays(x.PaidLeaveType, x.LeaveHourses),
|
|
IsAccepted = x.IsAccepted,
|
|
Decription = x.Decription,
|
|
Year = x.Year,
|
|
Month = x.Month,
|
|
MonthGr = Tools.ToFarsiMonthByIntNumber(x.Month),
|
|
EmployerList = new List<EmprViewModel>(),
|
|
IsInvalid = x.IsInvalid
|
|
}).SingleOrDefault(x => x.Id == item);
|
|
|
|
//leave.ContractNo = _context.Contracts.FirstOrDefault(x => x.ContarctStart <= leave.StartLeaveGr && x.ContractEnd > leave.StartLeaveGr
|
|
// && x.ContarctStart < leave.EndLeaveGr && x.ContractEnd > leave.EndLeaveGr
|
|
// && x.WorkshopIds == leave.WorkshopId && x.EmployeeId == leave.EmployeeId).ContractNo;
|
|
|
|
leave.ContractNo = _context.Contracts.FirstOrDefault(x => x.ContarctStart <= leave.StartLeaveGr
|
|
&& x.ContractEnd >= leave.StartLeaveGr &&
|
|
x.EmployeeId == leave.EmployeeId
|
|
&& x.WorkshopIds == leave.WorkshopId)?.ContractNo;
|
|
|
|
var Employee = _context.Employees.SingleOrDefault(x => x.id == leave.EmployeeId);
|
|
leave.NationalCode = Employee.NationalCode;
|
|
|
|
var emp = _context.WorkshopEmployers.Where(x => x.WorkshopId == leave.WorkshopId).Select(x => x.EmployerId)
|
|
.ToList();
|
|
var employerlist = _context.Employers.Select(x => new EmprViewModel()
|
|
{
|
|
Id = x.id,
|
|
EmployerFullName = x.FName + " " + x.LName,
|
|
IsLegal = x.IsLegal,
|
|
}).Where(x => emp.Contains(x.Id)).ToList();
|
|
if (employerlist.Count > 0)
|
|
{
|
|
leave.EmployerList = employerlist;
|
|
}
|
|
else
|
|
{
|
|
leave.EmployerList = new List<EmprViewModel>();
|
|
}
|
|
|
|
query.Add(leave);
|
|
}
|
|
|
|
query = query.OrderBy(x => x.Year).ThenBy(x => x.Month).ToList();
|
|
int printNumer = 0;
|
|
foreach (var rec in query)
|
|
{
|
|
printNumer += 1;
|
|
rec.PrintCounter = printNumer;
|
|
}
|
|
|
|
return query;
|
|
}
|
|
|
|
public async Task<List<LeavePrintResponseViewModel>> PrintAllAsync(List<long> ids, long workshopId)
|
|
{
|
|
var leaves =await _context
|
|
.LeaveList.Where(x => ids.Contains(x.id) && x.WorkshopId == workshopId).ToListAsync();
|
|
|
|
var minLeave = leaves.Min(x => x.StartLeave);
|
|
var maxLeave = leaves.Max(x => x.EndLeave);
|
|
|
|
var employeeIds = leaves.Select(x => x.EmployeeId).Distinct().ToList();
|
|
|
|
var contracts = await _context.Contracts
|
|
.Where(x => x.WorkshopIds == workshopId && employeeIds.Contains(x.EmployeeId)
|
|
&& x.ContarctStart<= maxLeave && x.ContractEnd>= minLeave)
|
|
.ToListAsync();
|
|
|
|
var employees = await _context
|
|
.Employees.Where(x=> employeeIds.Contains(x.id)).ToListAsync();
|
|
|
|
var employerNames = _context.WorkshopEmployers.Include(x=>x.Employer)
|
|
.Where(x=>x.WorkshopId == workshopId).Select(x=>x.Employer.FName+" "+x.Employer.LName).ToList();
|
|
|
|
var workshopName = _context.Workshops.FirstOrDefault(x => x.id == workshopId)?.WorkshopName;
|
|
|
|
var res = leaves.Select(leave =>
|
|
{
|
|
var employee = employees.FirstOrDefault(x => x.id == leave.EmployeeId);
|
|
return new LeavePrintResponseViewModel
|
|
{
|
|
FullName = employee?.FName + " " +
|
|
employee?.LName,
|
|
NationalCode = employee?.NationalCode,
|
|
WorkshopName = workshopName,
|
|
EmployerNames = employerNames,
|
|
PaidLeaveType = leave.PaidLeaveType,
|
|
ContractNo = contracts.FirstOrDefault(x =>
|
|
x.EmployeeId == leave.EmployeeId &&
|
|
x.ContarctStart <= leave.StartLeave &&
|
|
x.ContractEnd >= leave.EndLeave)?.ContractNo,
|
|
DailyLeave = leave.PaidLeaveType == "روزانه" ? new LeavePrintDailyResponseViewModel
|
|
{
|
|
StartLeave = leave.StartLeave.ToFarsi(),
|
|
EndLeave = leave.EndLeave.ToFarsi(),
|
|
TotalDay = leave.LeaveHourses
|
|
} : null,
|
|
|
|
HourlyLeave = leave.PaidLeaveType == "ساعتی" ?
|
|
new LeavePrintHourlyResponseViewModel
|
|
{
|
|
LeaveDate = leave.StartLeave.ToFarsi(),
|
|
StartHour = leave.StartLeave.ToString("HH:mm"),
|
|
EndHour = leave.EndLeave.ToString("HH:mm"),
|
|
TotalHour = TimeSpan.Parse(leave.LeaveHourses)
|
|
.TotalHours.ToString("F0")
|
|
} : null
|
|
};
|
|
}).ToList();
|
|
return res;
|
|
}
|
|
|
|
#region Vafa
|
|
|
|
public List<LeaveViewModel> LastLeaveMain(LeaveSearchModel searchModel)
|
|
{
|
|
var leaveMainList = _context.LeaveList.Where(x => x.WorkshopId == searchModel.WorkshopId);
|
|
|
|
if (searchModel.EmployeeId != 0)
|
|
leaveMainList = leaveMainList.Where(x => x.EmployeeId == searchModel.EmployeeId);
|
|
|
|
if (searchModel.LeaveType == "paidLeave")
|
|
leaveMainList = leaveMainList.Where(x => x.LeaveType == "استحقاقی");
|
|
|
|
if (searchModel.LeaveType == "sickLeave")
|
|
leaveMainList = leaveMainList.Where(x => x.LeaveType == "استعلاجی");
|
|
|
|
if (searchModel.IsInvalid)
|
|
{
|
|
leaveMainList = leaveMainList.IgnoreQueryFilters().Where(x => x.IsInvalid);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.StartLeave) && !string.IsNullOrWhiteSpace(searchModel.EndLeave))
|
|
{
|
|
var start = searchModel.StartLeave.ToGeorgianDateTime();
|
|
var end = searchModel.EndLeave.ToGeorgianDateTime();
|
|
leaveMainList = leaveMainList.Where(x => x.StartLeave >= start && x.EndLeave <= end);
|
|
}
|
|
else if (searchModel.Year != 0 || searchModel.Month != 0)
|
|
{
|
|
if (searchModel.Year != 0)
|
|
leaveMainList = leaveMainList.Where(x => x.Year == searchModel.Year);
|
|
|
|
if (searchModel.Month != 0)
|
|
leaveMainList = leaveMainList.Where(x => x.Month == searchModel.Month);
|
|
}
|
|
|
|
|
|
var leaveList = leaveMainList.Where(x => x.WorkshopId == searchModel.WorkshopId).Select(item =>
|
|
new LeaveViewModel()
|
|
{
|
|
Id = item.id,
|
|
StartLeave = item.StartLeave.ToFarsi(),
|
|
EndLeave = item.EndLeave.ToFarsi(),
|
|
StartLeaveGr = item.StartLeave,
|
|
EndLeaveGr = item.EndLeave,
|
|
WorkshopId = item.WorkshopId,
|
|
EmployeeId = item.EmployeeId,
|
|
EmployeeFullName = item.EmployeeFullName,
|
|
LeaveHourses = Tools.CalculateLeaveHoursAndDays(item.PaidLeaveType, item.LeaveHourses),
|
|
PaidLeaveType = item.PaidLeaveType,
|
|
LeaveType = item.LeaveType,
|
|
IsAccepted = item.IsAccepted,
|
|
Decription = item.Decription,
|
|
Year = item.Year,
|
|
Month = item.Month,
|
|
MonthStr = item.Month.ToFarsiMonthByIntNumber(),
|
|
CreationDate = item.CreationDate,
|
|
IsInvalid = item.IsInvalid
|
|
}).OrderByDescending(x => x.CreationDate).Skip(searchModel.PageIndex).Take(30).ToList();
|
|
|
|
return leaveList;
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
public bool CheckIfValidToEdit(long id)
|
|
{
|
|
var leave = _context.LeaveList.FirstOrDefault(x => x.id == id);
|
|
var checkoutExist = _context.CheckoutSet
|
|
.Where(x => x.WorkshopId == leave.WorkshopId && x.EmployeeId == leave.EmployeeId)
|
|
.Where(x => leave.StartLeave <= x.ContractEnd).ToList();
|
|
if (checkoutExist.Count > 0)
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public async Task<OperationResult> RemoveLeave(long id)
|
|
{
|
|
var op = new OperationResult();
|
|
var item = _context.LeaveList.FirstOrDefault(x => x.id == id);
|
|
if (item != null)
|
|
{
|
|
var checkoutExist =await _context.CheckoutSet
|
|
.Where(x => x.WorkshopId == item.WorkshopId && x.EmployeeId == item.EmployeeId)
|
|
.Where(x => item.StartLeave <= x.ContractEnd).ToListAsync();
|
|
if (checkoutExist.Count > 0)
|
|
{
|
|
return op.Failed("در بازه زمانی این مرخصی و یا بعد از آن فیش حقوقی وجود دارد");
|
|
}
|
|
else
|
|
{
|
|
_context.LeaveList.Remove(item);
|
|
|
|
_context.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
}
|
|
|
|
return op.Failed("موردی یافت نشد");
|
|
}
|
|
|
|
#region Pooya
|
|
|
|
public bool HasDailyLeave(long employeeId, long workshopId, DateTime date)
|
|
{
|
|
return _context.LeaveList
|
|
.Where(x => (x.LeaveType == "استحقاقی" && x.PaidLeaveType == "روزانه") ||
|
|
x.LeaveType == "استعلاجی")
|
|
.FirstOrDefault(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId &&
|
|
x.StartLeave.Date <= date.Date && x.EndLeave.Date >= date.Date) != null;
|
|
}
|
|
|
|
public List<LeaveViewModel> GetByWorkshopIdEmployeeIdInDates(long workshopId, long employeeId, DateTime start,
|
|
DateTime end)
|
|
{
|
|
return _context.LeaveList.Where(x => x.EmployeeId == employeeId && x.EndLeave >= start && x.StartLeave <= end)
|
|
.Select(x => new LeaveViewModel
|
|
{
|
|
EmployeeId = employeeId,
|
|
WorkshopId = workshopId,
|
|
PaidLeaveType = x.PaidLeaveType,
|
|
LeaveType = x.LeaveType,
|
|
StartLeaveGr = x.StartLeave,
|
|
EndLeaveGr = x.EndLeave,
|
|
LeaveHourses = x.LeaveHourses
|
|
}).ToList();
|
|
}
|
|
|
|
#endregion
|
|
|
|
public bool CheckContractExist(DateTime myDate, long employeeId, long workshopId)
|
|
{
|
|
var result = _context.Contracts.Any(x => x.ContarctStart <= myDate
|
|
&& x.ContractEnd >= myDate && x.EmployeeId == employeeId &&
|
|
x.WorkshopIds == workshopId && x.IsActiveString == "true");
|
|
return result;
|
|
}
|
|
|
|
public LeavErrorViewModel CheckErrors(DateTime startLeav, DateTime endLeav, long employeeId, long workshopId,
|
|
bool invalidCheckout)
|
|
{
|
|
var res = new LeavErrorViewModel();
|
|
|
|
#region Check iF Has Checkout
|
|
|
|
//var checkoutExist = _context.CheckoutSet.Where(x => x.WorkshopId == workshopId && x.EmployeeId == employeeId)
|
|
// .Where(x => (startLeav >= x.ContractStart && startLeav <= x.ContractEnd && endLeav > x.ContractEnd) ||
|
|
// (startLeav >= x.ContractStart && endLeav <= x.ContractEnd) ||
|
|
// (startLeav < x.ContractStart && endLeav <= x.ContractEnd && endLeav >= x.ContractStart) ||
|
|
// (startLeav < x.ContractStart && endLeav > x.ContractEnd)).ToList();
|
|
var checkoutExist = _context.CheckoutSet.Where(x =>
|
|
x.WorkshopId == workshopId && x.EmployeeId == employeeId && startLeav <= x.ContractEnd).ToList();
|
|
if (checkoutExist.Count > 0)
|
|
{
|
|
res.HasChekout = true;
|
|
res.CheckoutErrMessage = "در بازه تاریخ وارد شده و یا بعد از آن فیش حقوقی وجود دارد";
|
|
return res;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Check If Has not Contract
|
|
|
|
var result = _context.Contracts.Where(x => x.WorkshopIds == workshopId && x.EmployeeId == employeeId)
|
|
.Where(x => (startLeav >= x.ContarctStart && startLeav <= x.ContractEnd && endLeav > x.ContractEnd) ||
|
|
(startLeav >= x.ContarctStart && endLeav <= x.ContractEnd) ||
|
|
(startLeav < x.ContarctStart && endLeav <= x.ContractEnd && endLeav >= x.ContarctStart) ||
|
|
(startLeav < x.ContarctStart && endLeav > x.ContractEnd)).ToList();
|
|
if (result.Count < 1)
|
|
{
|
|
res.HasNotContract = true;
|
|
res.ContractErrMessage = "در بازه تاریخ مرخصی وارد شده قراردادی وجود ندارد";
|
|
return res;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Check LeftWork
|
|
|
|
var leftCheck = _context.LeftWorkList
|
|
.Where(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId)
|
|
.Where(x => x.StartWorkDate <= startLeav && x.LeftWorkDate > startLeav && x.StartWorkDate <= endLeav &&
|
|
x.LeftWorkDate > endLeav).ToList();
|
|
|
|
|
|
if (leftCheck.Count < 1)
|
|
{
|
|
res.HasLeftWork = true;
|
|
res.LeftWorlErrMessage = " پرسنل در بازه تاریخ وارد شده شروع بکار ندارد";
|
|
return res;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region HolidayError
|
|
|
|
var hasCustomizeCheckout =
|
|
_context.RollCallServices.FirstOrDefault(x => x.WorkshopId == workshopId && x.IsActiveString == "true")
|
|
?.HasCustomizeCheckoutService == "true";
|
|
|
|
if (_context.HolidayItems.Any(x => startLeav.Date == x.Holidaydate.Date) ||
|
|
startLeav.DayOfWeek == DayOfWeek.Friday)
|
|
{
|
|
if (!hasCustomizeCheckout)
|
|
{
|
|
res.HasHolidayError = true;
|
|
res.HolidayErrorMessage = "شما نمیتوانید در روز های تعطیل مرخصی ثبت کنید";
|
|
}
|
|
else if (!invalidCheckout)
|
|
{
|
|
res.HasHolidayError = true;
|
|
res.HolidayErrorMessage = "شما نمیتوانید در روز های مرخصی دارای اعتبار ثبت کنید";
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
return res;
|
|
}
|
|
|
|
public LeaveViewModel LeavOnChekout(DateTime starContract, DateTime endContract, long employeeId, long workshopId)
|
|
{
|
|
return _context.LeaveList.Where(x => x.EmployeeId == employeeId && x.WorkshopId == workshopId).Select(x =>
|
|
new LeaveViewModel()
|
|
{
|
|
Id = x.id,
|
|
StartLeaveGr = x.StartLeave,
|
|
EndLeaveGr = x.EndLeave,
|
|
WorkshopId = x.WorkshopId,
|
|
EmployeeId = x.EmployeeId,
|
|
LeaveHourses = x.LeaveHourses,
|
|
PaidLeaveType = x.PaidLeaveType,
|
|
LeaveType = x.LeaveType,
|
|
IsAccepted = x.IsAccepted,
|
|
Decription = x.Decription
|
|
}).FirstOrDefault(x =>
|
|
(starContract <= x.StartLeaveGr && endContract >= x.StartLeaveGr) ||
|
|
(starContract <= x.EndLeaveGr && endContract >= x.EndLeaveGr) ||
|
|
(starContract >= x.StartLeaveGr && starContract <= x.EndLeaveGr) ||
|
|
(endContract >= x.StartLeaveGr && endContract <= x.EndLeaveGr));
|
|
}
|
|
|
|
|
|
#region ForApi
|
|
|
|
public async Task<PagedResult<leaveListDto>> GetList(LeaveListSearchModel searchModel)
|
|
{
|
|
var query = _context.LeaveList.Where(x => x.WorkshopId == searchModel.WorkshopId);
|
|
|
|
if (searchModel.EmployeeId != 0)
|
|
query = query.Where(x => x.EmployeeId == searchModel.EmployeeId);
|
|
|
|
if (searchModel.LeaveType == LeaveType.PaidLeave)
|
|
query = query.Where(x => x.LeaveType == "استحقاقی");
|
|
|
|
if (searchModel.LeaveType == LeaveType.SickLeave)
|
|
query = query.Where(x => x.LeaveType == "استعلاجی");
|
|
|
|
if (searchModel.IsInvalid)
|
|
{
|
|
query = query.IgnoreQueryFilters().Where(x => x.IsInvalid);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.StartLeave) && !string.IsNullOrWhiteSpace(searchModel.EndLeave))
|
|
{
|
|
var start = new DateTime();
|
|
var end = new DateTime();
|
|
try
|
|
{
|
|
start = searchModel.StartLeave.ToGeorgianDateTime();
|
|
end = searchModel.EndLeave.ToGeorgianDateTime();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return new PagedResult<leaveListDto>();
|
|
}
|
|
|
|
query = query.Where(x => x.StartLeave >= start && x.EndLeave <= end);
|
|
}
|
|
else if (!string.IsNullOrWhiteSpace(searchModel.YearStr) || !string.IsNullOrWhiteSpace(searchModel.MonthStr))
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(searchModel.YearStr))
|
|
{
|
|
try
|
|
{
|
|
int year = Convert.ToInt32(searchModel.YearStr);
|
|
query = query.Where(x => x.Year == year);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
return new PagedResult<leaveListDto>();
|
|
}
|
|
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.MonthStr))
|
|
{
|
|
try
|
|
{
|
|
int month = Convert.ToInt32(searchModel.MonthStr);
|
|
query = query.Where(x => x.Month == month);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
return new PagedResult<leaveListDto>();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
var count = await query.CountAsync();
|
|
query = query.OrderByDescending(x => x.CreationDate);
|
|
var queryPaginationFilter = await query.ApplyPagination(searchModel.PageIndex, searchModel.PageSize).ToListAsync();
|
|
|
|
|
|
|
|
var leaveResult = queryPaginationFilter.Select(item => new leaveListDto()
|
|
{
|
|
Id = item.id,
|
|
EmployeeFullName = item.EmployeeFullName,
|
|
YearStr = $"{item.Year}",
|
|
MonthStr = item.Month.ToFarsiMonthByIntNumber(),
|
|
IsInvalid = item.IsInvalid,
|
|
LeaveType = item.LeaveType,
|
|
StartLeave = item.StartLeave.ToFarsi(),
|
|
EndLeave = item.EndLeave.ToFarsi(),
|
|
HourlyInterval = item.PaidLeaveType == "ساعتی" ? $"{item.StartLeave.TimeOfDay:hh\\:mm} الی {item.EndLeave.TimeOfDay:hh\\:mm}" : "-",
|
|
LeaveDuration = Tools.CalculateLeaveHoursAndDays(item.PaidLeaveType, item.LeaveHourses),
|
|
IsAccepted = item.IsAccepted,
|
|
WorkshopId = item.WorkshopId,
|
|
EmployeeId = item.EmployeeId,
|
|
|
|
}).ToList();
|
|
return new PagedResult<leaveListDto>()
|
|
{
|
|
TotalCount = count,
|
|
List = leaveResult
|
|
};
|
|
}
|
|
|
|
|
|
public async Task<List<GroupLeaveListDto>> GetGroupList(LeaveListSearchModel searchModel)
|
|
{
|
|
var query = _context.LeaveList.Where(x => x.WorkshopId == searchModel.WorkshopId && x.EmployeeId == searchModel.EmployeeId);
|
|
|
|
|
|
|
|
if (searchModel.LeaveType == LeaveType.PaidLeave)
|
|
query = query.Where(x => x.LeaveType == "استحقاقی");
|
|
|
|
if (searchModel.LeaveType == LeaveType.SickLeave)
|
|
query = query.Where(x => x.LeaveType == "استعلاجی");
|
|
|
|
if (searchModel.IsInvalid)
|
|
{
|
|
query = query.IgnoreQueryFilters().Where(x => x.IsInvalid);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.StartLeave) && !string.IsNullOrWhiteSpace(searchModel.EndLeave))
|
|
{
|
|
var start = new DateTime();
|
|
var end = new DateTime();
|
|
try
|
|
{
|
|
start = searchModel.StartLeave.ToGeorgianDateTime();
|
|
end = searchModel.EndLeave.ToGeorgianDateTime();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return new List<GroupLeaveListDto>();
|
|
}
|
|
|
|
query = query.Where(x => x.StartLeave >= start && x.EndLeave <= end);
|
|
}
|
|
else if (!string.IsNullOrWhiteSpace(searchModel.YearStr) || !string.IsNullOrWhiteSpace(searchModel.MonthStr))
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(searchModel.YearStr))
|
|
{
|
|
try
|
|
{
|
|
int year = Convert.ToInt32(searchModel.YearStr);
|
|
query = query.Where(x => x.Year == year);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
return new List<GroupLeaveListDto>();
|
|
}
|
|
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.MonthStr))
|
|
{
|
|
try
|
|
{
|
|
int month = Convert.ToInt32(searchModel.MonthStr);
|
|
query = query.Where(x => x.Month == month);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
return new List<GroupLeaveListDto>();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
var leaveList = await query.GroupBy(x => new { x.Year, x.Month })
|
|
.OrderByDescending(group => group.Key.Year)
|
|
.ThenByDescending(group => group.Key.Month)
|
|
.Select(group => new GroupLeaveListDto
|
|
{
|
|
YearStr = $"{group.Key.Year}",
|
|
MonthStr = group.Key.Month.ToFarsiMonthByIntNumber(),
|
|
LeaveListItemsDto = group.OrderByDescending(x=>x.StartLeave).Select(item => new LeaveListItemsDto
|
|
{
|
|
Id = item.id,
|
|
IsInvalid = item.IsInvalid,
|
|
LeaveType = item.LeaveType,
|
|
StartLeave = item.StartLeave.ToFarsi(),
|
|
EndLeave = item.EndLeave.ToFarsi(),
|
|
HourlyInterval = item.PaidLeaveType == "ساعتی" ? $"{item.StartLeave.TimeOfDay:hh\\:mm} الی {item.EndLeave.TimeOfDay:hh\\:mm}" : "-",
|
|
LeaveDuration = Tools.CalculateLeaveHoursAndDays(item.PaidLeaveType, item.LeaveHourses),
|
|
IsAccepted = item.IsAccepted,
|
|
WorkshopId = item.WorkshopId,
|
|
EmployeeId = item.EmployeeId,
|
|
}).ToList()
|
|
}).ToListAsync();
|
|
|
|
return leaveList;
|
|
}
|
|
|
|
|
|
public async Task<LeaveListPrintDto> ListPrint(List<long> ids)
|
|
{
|
|
var result = new LeaveListPrintDto();
|
|
var timeSpanHourlyLeave = new TimeSpan();
|
|
var dailyLeaveTime = new TimeSpan();
|
|
if (ids.Any())
|
|
{
|
|
var query = _context.LeaveList.Where(x => ids.Contains(x.id)).OrderByDescending(x=>x.StartLeave).AsQueryable();
|
|
|
|
#region sumOfLeaves
|
|
|
|
var hourly = await query.Where(x => x.PaidLeaveType == "ساعتی").Select(x => x.LeaveHourses).ToListAsync();
|
|
var daily = await query.Where(x => x.PaidLeaveType == "روزانه").Select(x => x.LeaveHourses).ToListAsync();
|
|
|
|
|
|
if (hourly.Any())
|
|
timeSpanHourlyLeave = new TimeSpan(hourly.Sum(x => TimeOnly.Parse(x).Ticks));
|
|
if (daily.Any())
|
|
dailyLeaveTime = daily.Sum(x => Convert.ToInt32(x)) * TimeSpan.FromDays(1);
|
|
|
|
var sumOfLeaves = timeSpanHourlyLeave.Add(dailyLeaveTime);
|
|
|
|
result.SumOfEmployeeleaves = sumOfLeaves.ToFarsiDaysAndHoursAndMinutes();
|
|
|
|
#endregion
|
|
|
|
|
|
result.LeavePrintListItemsDto = await query.Select(item => new LeavePrintListItemsDto
|
|
{
|
|
YearStr = $"{item.Year}",
|
|
MonthStr = item.Month.ToFarsiMonthByIntNumber(),
|
|
StartLeave = item.StartLeave.ToFarsi(),
|
|
EndLeave = item.EndLeave.ToFarsi(),
|
|
LeaveType = item.LeaveType,
|
|
HourlyInterval = item.PaidLeaveType == "ساعتی" ? $"{item.StartLeave.TimeOfDay:hh\\:mm} الی {item.EndLeave.TimeOfDay:hh\\:mm}" : "-",
|
|
LeaveDuration = Tools.CalculateLeaveHoursAndDays(item.PaidLeaveType, item.LeaveHourses),
|
|
IsAccepted = item.IsAccepted,
|
|
}).ToListAsync();
|
|
|
|
|
|
result.EmployeeFullName = query.FirstOrDefault()!.EmployeeFullName;
|
|
}
|
|
|
|
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
} |