1042 lines
53 KiB
C#
1042 lines
53 KiB
C#
using _0_Framework.Application;
|
|
using _0_Framework.Domain.CustomizeCheckoutShared.Enums;
|
|
using Company.Domain.CustomizeWorkshopEmployeeSettingsAgg;
|
|
using Company.Domain.EmployeeAgg;
|
|
using Company.Domain.HolidayItemAgg;
|
|
using Company.Domain.LeaveAgg;
|
|
using Company.Domain.RollCallAgg;
|
|
using Company.Domain.RollCallServiceAgg;
|
|
using Company.Domain.WorkshopAgg;
|
|
using CompanyManagment.App.Contracts.CustomizeWorkshopSettings;
|
|
using CompanyManagment.App.Contracts.HolidayItem;
|
|
using CompanyManagment.App.Contracts.Leave;
|
|
using CompanyManagment.App.Contracts.RollCallEmployeeStatus;
|
|
using CompanyManagment.App.Contracts.RollCallService;
|
|
using CompanyManagment.EFCore.Migrations;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CompanyManagment.Application;
|
|
|
|
public class LeaveApplication : ILeaveApplication
|
|
{
|
|
private readonly ILeaveRepository _leaveRepository;
|
|
private readonly IEmployeeRepository _employeeRepository;
|
|
private readonly IWorkshopRepository _workshopRepository;
|
|
private readonly IRollCallRepository _rollCallRepository;
|
|
private readonly ICustomizeWorkshopEmployeeSettingsRepository _employeeSettingsRepository;
|
|
private readonly IRollCallEmployeeStatusApplication _rollCallEmployeeStatusApplication;
|
|
private readonly IHolidayItemRepository _holidayItemRepository;
|
|
private readonly IRollCallServiceRepository _rollCallServiceRepository;
|
|
|
|
|
|
|
|
public LeaveApplication(ILeaveRepository leaveRepository, IEmployeeRepository employeeRepository, IWorkshopRepository workshopRepository, IRollCallRepository rollCallRepository, ICustomizeWorkshopEmployeeSettingsRepository employeeSettingsRepository, IRollCallEmployeeStatusApplication rollCallEmployeeStatusApplication, IHolidayItemRepository holidayItemRepository, IRollCallServiceRepository rollCallServiceRepository)
|
|
{
|
|
_leaveRepository = leaveRepository;
|
|
_employeeRepository = employeeRepository;
|
|
_workshopRepository = workshopRepository;
|
|
_rollCallRepository = rollCallRepository;
|
|
_employeeSettingsRepository = employeeSettingsRepository;
|
|
_rollCallEmployeeStatusApplication = rollCallEmployeeStatusApplication;
|
|
_holidayItemRepository = holidayItemRepository;
|
|
_rollCallServiceRepository = rollCallServiceRepository;
|
|
}
|
|
|
|
public OperationResult Create(CreateLeave command)
|
|
{
|
|
TimeSpan shiftDuration = TimeSpan.Zero;
|
|
bool hasShiftDuration = false;
|
|
var startH = new TimeSpan();
|
|
var endH = new TimeSpan();
|
|
var op = new OperationResult();
|
|
if (command.PaidLeaveType == "روزانه")
|
|
{
|
|
if (string.IsNullOrWhiteSpace(command.StartLeave))
|
|
{
|
|
return op.Failed("لطفا تاریخ شروع را وارد کنید");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(command.EndLeave))
|
|
{
|
|
return op.Failed("لطفا تاریخ پایان را وارد کنید");
|
|
}
|
|
}
|
|
else if (command.PaidLeaveType == "ساعتی")
|
|
{
|
|
if (string.IsNullOrWhiteSpace(command.StartLeave))
|
|
return op.Failed("لطفا تاریخ شروع را وارد کنید");
|
|
if (string.IsNullOrWhiteSpace(command.StartHoures) || string.IsNullOrWhiteSpace(command.EndHours))
|
|
return op.Failed("ساعت شروع و پایان نمیتواند خالی باشد");
|
|
|
|
string pattern = @"^([01]\d|2[0-3]):[0-5]\d$";
|
|
|
|
if (!Regex.IsMatch(command.StartHoures, pattern))
|
|
return op.Failed("لطفا ساعت شروع را به درستی وارد کنید");
|
|
|
|
if (!Regex.IsMatch(command.EndHours, pattern))
|
|
return op.Failed("لطفا ساعت پایان را به درستی وارد کنید");
|
|
|
|
startH = TimeSpan.Parse(command.StartHoures);
|
|
endH = TimeSpan.Parse(command.EndHours);
|
|
if (startH == endH)
|
|
return op.Failed("ساعت شروع و پایان نباید برابر باشد");
|
|
}
|
|
|
|
if (command.LeaveType == "استعلاجی" && string.IsNullOrWhiteSpace(command.StartLeave))
|
|
return op.Failed("لطفا تاریخ شروع را وارد کنید");
|
|
if (command.LeaveType == "استعلاجی" && string.IsNullOrWhiteSpace(command.EndLeave))
|
|
return op.Failed("لطفا تاریخ پایان را وارد کنید");
|
|
|
|
|
|
var start = command.StartLeave.ToGeorgianDateTime();
|
|
|
|
var end = command.PaidLeaveType == "ساعتی" ? start : command.EndLeave.ToGeorgianDateTime();
|
|
|
|
var checkErr = _leaveRepository.CheckErrors(start, end, command.EmployeeId, command.WorkshopId,command.IsInvallid);
|
|
|
|
// start = new DateTime(start.Year, start.Month, start.Day, startH.Hours, startH.Minutes, startH.Seconds);
|
|
//end = new DateTime(end.Year, end.Month, end.Day, endH.Hours, endH.Minutes, endH.Seconds);
|
|
|
|
|
|
if (checkErr.HasChekout)
|
|
return op.Failed(checkErr.CheckoutErrMessage);
|
|
if (checkErr.HasNotContract)
|
|
return op.Failed(checkErr.ContractErrMessage);
|
|
if (checkErr.HasLeftWork)
|
|
return op.Failed(checkErr.LeftWorlErrMessage);
|
|
//if (checkErr.HasHolidayError)
|
|
// return op.Failed(checkErr.HolidayErrorMessage);
|
|
|
|
if (start > end)
|
|
return op.Failed("تارخ شروع از پایان بزرگتر است");
|
|
|
|
|
|
var totalhourses = "-";
|
|
if (command.LeaveType == "استحقاقی" && command.PaidLeaveType == "ساعتی")
|
|
{
|
|
|
|
start = new DateTime(start.Year, start.Month, start.Day, startH.Hours, startH.Minutes, startH.Seconds);
|
|
end = new DateTime(start.Year, start.Month, start.Day, endH.Hours, endH.Minutes, endH.Seconds);
|
|
if (start > end)
|
|
end = end.AddDays(1);
|
|
|
|
var totalLeavHourses = (end - start);
|
|
var h = totalLeavHourses.Hours < 10 ? $"0{totalLeavHourses.Hours}" : $"{totalLeavHourses.Hours}";
|
|
var m = totalLeavHourses.Minutes < 10 ? $"0{totalLeavHourses.Minutes}" : $"{totalLeavHourses.Minutes}";
|
|
totalhourses = $"{h}:{m}";
|
|
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= start && x.EndLeave >= start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی"))
|
|
return op.Failed("برای ساعت شروع سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= end && x.EndLeave >= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی"))
|
|
return op.Failed("برای ساعت پایان سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave >= start && x.EndLeave <= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی"))
|
|
return op.Failed("در بازه زمانی وارد شده مرخصی ثبت شده است");
|
|
|
|
var end24 = endH.Hours == 0 && endH.Minutes == 0 ? end.AddDays(-1) : end;
|
|
if (_leaveRepository.Exists(x =>
|
|
(x.StartLeave.Date == start.Date || x.EndLeave.Date == end24.Date) && (x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه")))
|
|
return op.Failed("در بازه زمانی وارد شده مرخصی ثبت شده است");
|
|
}
|
|
else if (command.LeaveType == "استحقاقی" && command.PaidLeaveType == "روزانه")
|
|
{
|
|
var totalLeavHourses = (end - start).TotalDays + 1;
|
|
totalhourses = $"{(int)totalLeavHourses}";
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= start && x.EndLeave >= start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه"))
|
|
return op.Failed("برای تاریخ شروع سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= end && x.EndLeave >= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه"))
|
|
return op.Failed("برای تاریخ پایان سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave >= start && x.EndLeave <= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه"))
|
|
return op.Failed("در بازه زمانی وارد شده مرخصی ثبت شده است");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave.Date >= start.Date && x.StartLeave.Date <= end.Date && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی"))
|
|
return op.Failed("دربازه تاریخ وارد شده مرخصی ساعتی ثبت شده است");
|
|
|
|
var employeeSettings = _employeeSettingsRepository.GetByEmployeeIdAndWorkshopIdIncludeGroupSettings(command.WorkshopId, command.EmployeeId);
|
|
|
|
if (command.HasRollCall)
|
|
{
|
|
|
|
if ((end - start).TotalDays > 1 && employeeSettings.WorkshopShiftStatus != WorkshopShiftStatus.Regular)
|
|
{
|
|
return op.Failed("شما نمیتوانید بیشتر از یک روز مرخصی روزانه ثبت کنید");
|
|
}
|
|
|
|
if (employeeSettings.WorkshopShiftStatus == WorkshopShiftStatus.Rotating &&
|
|
command.SelectedShift == null)
|
|
{
|
|
return op.Failed("لطفا شیفت پرسنل را انتخاب کنید");
|
|
}
|
|
|
|
if (command.SelectedShift != null)
|
|
{
|
|
var validShiftStart = TimeOnly.TryParse(command.SelectedShift.StartTime, out var shiftStart);
|
|
var validShiftEnd = TimeOnly.TryParse(command.SelectedShift.EndTime, out var shiftEnd);
|
|
|
|
if (validShiftStart == false && validShiftEnd == false)
|
|
{
|
|
return op.Failed("شیفت های انتخاب شده معتبر نمیباشد");
|
|
}
|
|
|
|
var shiftStartDateTime = new DateTime(new DateOnly(), shiftStart);
|
|
var shiftEndDateTime = new DateTime(new DateOnly(), shiftEnd);
|
|
if (shiftEndDateTime <= shiftStartDateTime)
|
|
shiftEndDateTime = shiftEndDateTime.AddDays(1);
|
|
shiftDuration = shiftEndDateTime - shiftStartDateTime;
|
|
hasShiftDuration = true;
|
|
}
|
|
else if (employeeSettings is { WorkshopShiftStatus: WorkshopShiftStatus.Irregular })
|
|
{
|
|
if ((end - start).TotalDays > 1)
|
|
{
|
|
return op.Failed("شما نمیتوانید بیشتر از یک روز مرخصی روزانه ثبت کنید");
|
|
}
|
|
|
|
var isActive = _rollCallEmployeeStatusApplication.IsActiveInPeriod(command.EmployeeId, command.WorkshopId, start, start);
|
|
if (isActive)
|
|
{
|
|
shiftDuration = employeeSettings.IrregularShift.WorkshopIrregularShifts switch
|
|
{
|
|
WorkshopIrregularShifts.TwelveThirtySix => TimeSpan.FromHours(12),
|
|
WorkshopIrregularShifts.TwelveTwentyFour => TimeSpan.FromHours(12),
|
|
WorkshopIrregularShifts.TwentyFourFortyEight => TimeSpan.FromHours(24),
|
|
WorkshopIrregularShifts.TwentyFourTwentyFour => TimeSpan.FromHours(24),
|
|
_ => new TimeSpan()
|
|
};
|
|
hasShiftDuration = true;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
if (command.LeaveType == "استعلاجی")
|
|
{
|
|
var totalLeavHourses = (end - start).TotalDays + 1;
|
|
totalhourses = $"{(int)totalLeavHourses}";
|
|
command.PaidLeaveType = "روزانه";
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= start && x.EndLeave >= start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه"))
|
|
return op.Failed("برای تاریخ شروع سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= end && x.EndLeave >= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه"))
|
|
return op.Failed("برای تاریخ پایان سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave >= start && x.EndLeave <= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه"))
|
|
return op.Failed("در بازه زمانی وارد شده مرخصی ثبت شده است");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave.Date >= start.Date && x.StartLeave.Date <= end.Date && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی"))
|
|
return op.Failed("دربازه تاریخ وارد شده مرخصی ساعتی ثبت شده است");
|
|
}
|
|
|
|
var year = Convert.ToInt32(command.StartLeave.Substring(0, 4));
|
|
var month = Convert.ToInt32(command.StartLeave.Substring(5, 2));
|
|
|
|
var validation = ValidateNewLeaveWithExistingRollCalls(command.WorkshopId, command.EmployeeId, command.PaidLeaveType, start, end);
|
|
if (validation.IsSuccedded == false)
|
|
return validation;
|
|
|
|
var employeeFullName = _employeeRepository.GetDetails(command.EmployeeId).EmployeeFullName;
|
|
var workshopName = _workshopRepository.GetDetails(command.WorkshopId).WorkshopName;
|
|
var leave = new Company.Domain.LeaveAgg.Leave(start, end, totalhourses, command.WorkshopId, command.EmployeeId
|
|
, command.PaidLeaveType, command.LeaveType, employeeFullName, workshopName, command.IsAccepted, command.Decription,
|
|
year, month, shiftDuration, hasShiftDuration,command.IsInvallid);
|
|
_leaveRepository.Create(leave);
|
|
_leaveRepository.SaveChanges();
|
|
|
|
var id = leave.id;
|
|
return op.Succcedded(sendId: id);
|
|
}
|
|
|
|
public OperationResult Edit(EditLeave command)
|
|
{
|
|
TimeSpan shiftDuration = TimeSpan.Zero;
|
|
bool hasShiftDuration = false;
|
|
var startH = new TimeSpan();
|
|
var endH = new TimeSpan();
|
|
var op = new OperationResult();
|
|
var leave = _leaveRepository.Get(command.Id);
|
|
if (leave == null)
|
|
op.Failed("رکورد مورد نظر وجود ندارد");
|
|
|
|
if (command.PaidLeaveType == "روزانه")
|
|
{
|
|
if (string.IsNullOrWhiteSpace(command.EndLeave))
|
|
{
|
|
return op.Failed("لطفا تاریخ پایان را وارد کنید");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(command.StartLeave))
|
|
{
|
|
return op.Failed("لطفا تاریخ شروع را وارد کنید");
|
|
}
|
|
}
|
|
else if (command.PaidLeaveType == "ساعتی")
|
|
{
|
|
if (string.IsNullOrWhiteSpace(command.StartLeave))
|
|
return op.Failed("لطفا تاریخ شروع را وارد کنید");
|
|
if (string.IsNullOrWhiteSpace(command.StartHoures) || string.IsNullOrWhiteSpace(command.EndHours))
|
|
return op.Failed("ساعت شروع و پایان نمیتواند خالی باشد");
|
|
string pattern = @"^([01]\d|2[0-3]):[0-5]\d$";
|
|
|
|
if (!Regex.IsMatch(command.StartHoures, pattern))
|
|
return op.Failed("لطفا ساعت شروع را به درستی وارد کنید");
|
|
|
|
if (!Regex.IsMatch(command.EndHours, pattern))
|
|
return op.Failed("لطفا ساعت پایان را به درستی وارد کنید");
|
|
|
|
startH = TimeSpan.Parse(command.StartHoures);
|
|
endH = TimeSpan.Parse(command.EndHours);
|
|
if (startH == endH)
|
|
return op.Failed("ساعت شروع و پایان نباید برابر باشد");
|
|
}
|
|
|
|
if (command.LeaveType == "استعلاجی" && string.IsNullOrWhiteSpace(command.StartLeave))
|
|
return op.Failed("لطفا تاریخ شروع را وارد کنید");
|
|
if (command.LeaveType == "استعلاجی" && string.IsNullOrWhiteSpace(command.EndLeave))
|
|
return op.Failed("لطفا تاریخ پایان را وارد کنید");
|
|
|
|
if (command.LeaveType == "استعلاجی" && string.IsNullOrWhiteSpace(command.StartLeave))
|
|
return op.Failed("لطفا تاریخ شروع را وارد کنید");
|
|
if (command.LeaveType == "استعلاجی" && string.IsNullOrWhiteSpace(command.EndLeave))
|
|
return op.Failed("لطفا تاریخ پایان را وارد کنید");
|
|
var start = command.StartLeave.ToGeorgianDateTime();
|
|
var end = command.PaidLeaveType == "ساعتی" ? start : command.EndLeave.ToGeorgianDateTime();
|
|
var checkErr = _leaveRepository.CheckErrors(start, end, command.EmployeeId, command.WorkshopId,command.IsInvallid);
|
|
|
|
|
|
if (checkErr.HasChekout)
|
|
return op.Failed(checkErr.CheckoutErrMessage);
|
|
if (checkErr.HasLeftWork)
|
|
return op.Failed(checkErr.LeftWorlErrMessage);
|
|
if (checkErr.HasHolidayError)
|
|
return op.Failed(checkErr.HolidayErrorMessage);
|
|
if (start > end)
|
|
return op.Failed("تارخ شروع از پایان بزرگتر است");
|
|
|
|
|
|
//var totalHoursbetween = new TimeSpan();
|
|
//totalHoursbetween = (end - start);
|
|
//var totalhoursInt = totalHoursbetween.Hours;
|
|
//var totalhourses = totalhoursInt.ToString();
|
|
var totalhourses = "-";
|
|
if (command.LeaveType == "استحقاقی" && command.PaidLeaveType == "ساعتی")
|
|
{
|
|
start = new DateTime(start.Year, start.Month, start.Day, startH.Hours, startH.Minutes, startH.Seconds);
|
|
end = new DateTime(start.Year, start.Month, start.Day, endH.Hours, endH.Minutes, endH.Seconds);
|
|
if (start > end)
|
|
end = end.AddDays(1);
|
|
|
|
var totalLeavHourses = (end - start);
|
|
var h = totalLeavHourses.Hours < 10 ? $"0{totalLeavHourses.Hours}" : $"{totalLeavHourses.Hours}";
|
|
var m = totalLeavHourses.Minutes < 10 ? $"0{totalLeavHourses.Minutes}" : $"{totalLeavHourses.Minutes}";
|
|
totalhourses = $"{h}:{m}";
|
|
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= start && x.EndLeave >= start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی" && x.id != command.Id))
|
|
return op.Failed("برای ساعت شروع سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= end && x.EndLeave >= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی" && x.id != command.Id))
|
|
return op.Failed("برای ساعت پایان سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave >= start && x.EndLeave <= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی" && x.id != command.Id))
|
|
return op.Failed("در بازه زمانی وارد شده مرخصی ثبت شده است");
|
|
|
|
var end24 = endH.Hours == 0 && endH.Minutes == 0 ? end.AddDays(-1) : end;
|
|
if (_leaveRepository.Exists(x =>
|
|
(x.StartLeave.Date == start.Date || x.EndLeave.Date == end24.Date) && (x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه" && x.id != command.Id)))
|
|
return op.Failed("در بازه زمانی وارد شده مرخصی ثبت شده است");
|
|
}
|
|
else if (command.LeaveType == "استحقاقی" && command.PaidLeaveType == "روزانه")
|
|
{
|
|
var totalLeavHourses = (end - start).TotalDays + 1;
|
|
totalhourses = $"{(int)totalLeavHourses}";
|
|
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= start && x.EndLeave >= start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه" && x.id != command.Id))
|
|
return op.Failed("برای تاریخ شروع سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= end && x.EndLeave >= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه" && x.id != command.Id))
|
|
return op.Failed("برای تاریخ پایان سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave >= start && x.EndLeave <= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه" && x.id != command.Id))
|
|
return op.Failed("در بازه زمانی وارد شده مرخصی ثبت شده است");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave.Date >= start.Date && x.StartLeave.Date <= end.Date && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی" && x.id != command.Id))
|
|
return op.Failed("دربازه تاریخ وارد شده مرخصی ساعتی ثبت شده است");
|
|
if (command.HasRollCall)
|
|
{
|
|
var employeeSettings = _employeeSettingsRepository.GetByEmployeeIdAndWorkshopIdIncludeGroupSettings(command.WorkshopId, command.EmployeeId);
|
|
|
|
if ((end - start).TotalDays > 1 && employeeSettings.WorkshopShiftStatus != WorkshopShiftStatus.Regular)
|
|
{
|
|
return op.Failed("شما نمیتوانید بیشتر از یک روز مرخصی روزانه ثبت کنید");
|
|
}
|
|
|
|
if (employeeSettings.WorkshopShiftStatus == WorkshopShiftStatus.Rotating && command.SelectedShift == null)
|
|
{
|
|
return op.Failed("لطفا شیفت پرسنل را انتخاب کنید");
|
|
}
|
|
|
|
if (command.SelectedShift != null)
|
|
{
|
|
var validShiftStart = TimeOnly.TryParse(command.SelectedShift.StartTime, out var shiftStart);
|
|
var validShiftEnd = TimeOnly.TryParse(command.SelectedShift.EndTime, out var shiftEnd);
|
|
|
|
if (validShiftStart == false && validShiftEnd == false)
|
|
{
|
|
return op.Failed("شیفت های انتخاب شده معتبر نمیباشد");
|
|
}
|
|
|
|
var shiftStartDateTime = new DateTime(new DateOnly(), shiftStart);
|
|
var shiftEndDateTime = new DateTime(new DateOnly(), shiftEnd);
|
|
if (shiftEndDateTime < shiftStartDateTime)
|
|
shiftEndDateTime = shiftEndDateTime.AddDays(1);
|
|
shiftDuration = shiftEndDateTime - shiftStartDateTime;
|
|
hasShiftDuration = true;
|
|
}
|
|
else if (employeeSettings.WorkshopShiftStatus == WorkshopShiftStatus.Irregular)
|
|
{
|
|
shiftDuration = employeeSettings.IrregularShift.WorkshopIrregularShifts switch
|
|
{
|
|
WorkshopIrregularShifts.TwelveThirtySix => TimeSpan.FromHours(12),
|
|
WorkshopIrregularShifts.TwelveTwentyFour => TimeSpan.FromHours(12),
|
|
WorkshopIrregularShifts.TwentyFourFortyEight => TimeSpan.FromHours(24),
|
|
WorkshopIrregularShifts.TwentyFourTwentyFour => TimeSpan.FromHours(24),
|
|
_ => new TimeSpan()
|
|
};
|
|
hasShiftDuration = true;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if (command.LeaveType == "استعلاجی")
|
|
{
|
|
var totalLeavHourses = (end - start).TotalDays + 1;
|
|
totalhourses = $"{(int)totalLeavHourses}";
|
|
command.PaidLeaveType = "روزانه";
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= start && x.EndLeave >= start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه" && x.id != command.Id))
|
|
return op.Failed("برای تاریخ شروع سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= end && x.EndLeave >= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه" && x.id != command.Id))
|
|
return op.Failed("برای تاریخ پایان سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave >= start && x.EndLeave <= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه" && x.id != command.Id))
|
|
return op.Failed("در بازه زمانی وارد شده مرخصی ثبت شده است");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave.Date >= start.Date && x.StartLeave.Date <= end.Date && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی" && x.id != command.Id))
|
|
return op.Failed("دربازه تاریخ وارد شده مرخصی ساعتی ثبت شده است");
|
|
}
|
|
|
|
var year = Convert.ToInt32(command.StartLeave.Substring(0, 4));
|
|
var month = Convert.ToInt32(command.StartLeave.Substring(5, 2));
|
|
|
|
var validation = ValidateNewLeaveWithExistingRollCalls(command.WorkshopId, command.EmployeeId, command.PaidLeaveType, start, end);
|
|
if (validation.IsSuccedded == false)
|
|
return validation;
|
|
|
|
var employeeFullName = _employeeRepository.GetDetails(command.EmployeeId).EmployeeFullName;
|
|
var workshopName = _workshopRepository.GetDetails(command.WorkshopId).WorkshopName;
|
|
leave.Edit(start, end, totalhourses, command.WorkshopId, command.EmployeeId
|
|
, command.PaidLeaveType, command.LeaveType, employeeFullName, workshopName, command.IsAccepted, command.Decription, year, month, hasShiftDuration, shiftDuration);
|
|
_leaveRepository.SaveChanges();
|
|
op.SendId = leave.id;
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public EditLeave GetDetails(long id)
|
|
{
|
|
var res = _leaveRepository.GetDetails(id);
|
|
|
|
if (res.LeaveType == "استحقاقی" && res.PaidLeaveType == "ساعتی")
|
|
{ var hStart = res.StartLeaveGr.Hour < 10 ? $"0{res.StartLeaveGr.Hour}" : $"{res.StartLeaveGr.Hour}";
|
|
var mStart = res.StartLeaveGr.Minute < 10 ? $"0{res.StartLeaveGr.Minute}" : $"{res.StartLeaveGr.Minute}";
|
|
res.StartHoures = $"{hStart}:{mStart}";
|
|
|
|
var hEnd = res.EndLeaveGr.Hour < 10 ? $"0{res.EndLeaveGr.Hour}" : $"{res.EndLeaveGr.Hour}";
|
|
var mEnd = res.EndLeaveGr.Minute < 10 ? $"0{res.EndLeaveGr.Minute}" : $"{res.EndLeaveGr.Minute}";
|
|
res.EndHours = $"{hEnd}:{mEnd}";
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
public List<LeaveViewModel> search(LeaveSearchModel searchModel)
|
|
{
|
|
return _leaveRepository.search(searchModel);
|
|
}
|
|
|
|
public List<LeaveMainViewModel> searchClient(LeaveSearchModel searchModel)
|
|
{
|
|
return _leaveRepository.searchClient(searchModel);
|
|
}
|
|
|
|
public bool CheckIfValidToEdit(long id)
|
|
{
|
|
return _leaveRepository.CheckIfValidToEdit(id);
|
|
}
|
|
|
|
public OperationResult<bool> HasDailyLeave(long workshopId, long employeeId, string dateFa)
|
|
{
|
|
OperationResult<bool> op = new();
|
|
DateTime date = dateFa.ToGeorgianDateTime();
|
|
if (date == Tools.GetUndefinedDateTime())
|
|
return op.Failed("تاریخ وارد شده صحیح نمی باشد");
|
|
var result = _leaveRepository.HasDailyLeave(employeeId, workshopId, date);
|
|
return op.Succcedded(result);
|
|
}
|
|
|
|
public LeavePrintViewModel PrintOne(long id)
|
|
{
|
|
return _leaveRepository.PrintOne(id);
|
|
}
|
|
|
|
|
|
public GroupLeavePrintViewModel PrintPersonnelLeaveList(List<long> id)
|
|
{
|
|
var leaves = _leaveRepository.PrintAll(id);
|
|
|
|
|
|
var totalLeave = CalculateTotalLeaveTimeSpan(leaves);
|
|
|
|
return new GroupLeavePrintViewModel()
|
|
{
|
|
LeaveList = leaves,
|
|
TotalLeaveMessage = totalLeave.ToFarsiDaysAndHoursAndMinutes()
|
|
};
|
|
}
|
|
|
|
public OperationResult RemoveLeave(long id)
|
|
{
|
|
return _leaveRepository.RemoveLeave(id).GetAwaiter().GetResult();
|
|
}
|
|
|
|
public async Task<OperationResult> RemoveLeaveAsync(long id)
|
|
{
|
|
return await _leaveRepository.RemoveLeave(id);
|
|
}
|
|
|
|
public LeaveViewModel LeavOnChekout(DateTime starContract, DateTime endContract, long employeeId, long workshopId)
|
|
{
|
|
return _leaveRepository.LeavOnChekout(starContract, endContract, employeeId, workshopId);
|
|
}
|
|
|
|
public List<LeavePrintViewModel> PrintAll(List<long> id)
|
|
{
|
|
return _leaveRepository.PrintAll(id);
|
|
}
|
|
|
|
public List<LeaveViewModel> LastLeaveMain(LeaveSearchModel searchModel)
|
|
{
|
|
return _leaveRepository.LastLeaveMain(searchModel);
|
|
}
|
|
#region Pooya
|
|
public OperationResult ValidateNewLeaveWithExistingRollCalls(long workshopId, long employeeId, string paidLeaveType, DateTime start, DateTime end)
|
|
{
|
|
OperationResult op = new();
|
|
var rollCallsInDate = _rollCallRepository.GetEmployeeRollCallsHistoryAllInDates(workshopId, employeeId, start, end);
|
|
if (paidLeaveType == "ساعتی")
|
|
{
|
|
if (rollCallsInDate.Any(x => x.EndDate >= start && x.StartDate <= end))
|
|
return op.Failed("در این بازه زمانی رکورد حضور غیاب برای این شخص وجود دارد");
|
|
}
|
|
else
|
|
{
|
|
var rollCallsInDateGrouped = rollCallsInDate.GroupBy(x => x.DateGr.Date);
|
|
if (rollCallsInDateGrouped.Any(x => x.Key.Date >= start && x.Key.Date <= end))
|
|
return op.Failed("در این روز رکورد حضور غیاب برای این شخص وجود دارد");
|
|
|
|
}
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public TimeSpan GetEmployeeLeaveTimeSpanInDates(long workshopId, long employeeId, string startFa, string endFa,string type)
|
|
{
|
|
|
|
if (startFa.TryToGeorgianDateTime(out var start) == false || endFa.TryToGeorgianDateTime(out var end) == false)
|
|
return TimeSpan.Zero;
|
|
|
|
var leaveTotalTimeSpan = TimeSpan.Zero;
|
|
|
|
var leaves = _leaveRepository.GetByWorkshopIdEmployeeIdInDates(workshopId, employeeId, start, end);
|
|
//var timeSpanHourlyLeave = new TimeSpan(leaves.Where(x => x.PaidLeaveType != "روزانه").Sum(x => (x.EndLeaveGr - x.StartLeaveGr).Ticks));
|
|
//var dailyLeaveCount = leaves.Count(x => x.PaidLeaveType == "روزانه") * new TimeSpan(1, 0, 0, 0);
|
|
|
|
if (type == "paidLeave")
|
|
{
|
|
var timeSpanHourlyLeave = new TimeSpan(leaves.Where(x => x.PaidLeaveType == "ساعتی" && x.LeaveType == "استحقاقی")
|
|
.Sum(x => TimeOnly.Parse(x.LeaveHourses).Ticks));
|
|
var dailyLeaveTime = leaves.Where(x => x.PaidLeaveType == "روزانه" && x.LeaveType == "استحقاقی")
|
|
.Sum(x => Convert.ToInt32(x.LeaveHourses)) * TimeSpan.FromDays(1);
|
|
|
|
leaveTotalTimeSpan = timeSpanHourlyLeave + dailyLeaveTime;
|
|
}
|
|
else if (type == "sickLeave")
|
|
{
|
|
var timeSpanHourlyLeave = new TimeSpan(leaves.Where(x => x.PaidLeaveType == "ساعتی" && x.LeaveType == "استعلاجی")
|
|
.Sum(x => TimeOnly.Parse(x.LeaveHourses).Ticks));
|
|
|
|
var dailyLeaveTime = leaves.Where(x => x.PaidLeaveType == "روزانه" && x.LeaveType == "استعلاجی")
|
|
.Sum(x => Convert.ToInt32(x.LeaveHourses)) * TimeSpan.FromDays(1);
|
|
|
|
leaveTotalTimeSpan = timeSpanHourlyLeave + dailyLeaveTime;
|
|
}
|
|
else
|
|
{
|
|
var timeSpanHourlyLeave = new TimeSpan(leaves.Where(x => x.PaidLeaveType == "ساعتی").Sum(x => TimeOnly.Parse(x.LeaveHourses).Ticks));
|
|
var dailyLeaveTime = leaves.Where(x => x.PaidLeaveType == "روزانه").Sum(x => Convert.ToInt32(x.LeaveHourses)) * TimeSpan.FromDays(1);
|
|
leaveTotalTimeSpan = timeSpanHourlyLeave + dailyLeaveTime;
|
|
}
|
|
|
|
|
|
|
|
|
|
return leaveTotalTimeSpan;
|
|
}
|
|
|
|
|
|
|
|
private TimeSpan CalculateTotalLeaveTimeSpan(List<LeavePrintViewModel> leaves)
|
|
{
|
|
var timeSpanHourlyLeave = new TimeSpan(leaves.Where(x => x.PaidLeaveType != "روزانه").Sum(x => (x.EndLeaveGr - x.StartLeaveGr).Ticks));
|
|
var dailyLeaveCount = leaves.Count(x => x.PaidLeaveType == "روزانه") * new TimeSpan(1, 0, 0, 0);
|
|
return timeSpanHourlyLeave + dailyLeaveCount;
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region ForApi
|
|
|
|
public async Task<PagedResult<leaveListDto>> GetList(LeaveListSearchModel searchModel)
|
|
{
|
|
return await _leaveRepository.GetList(searchModel);
|
|
}
|
|
|
|
public async Task<List<GroupLeaveListDto>> GetGroupList(LeaveListSearchModel searchModel)
|
|
{
|
|
return await _leaveRepository.GetGroupList(searchModel);
|
|
}
|
|
|
|
public TimeSpan SumOfEmployeeLeaveTimeSpanInDates(long workshopId, long employeeId, string yearStr, string monthStr, LeaveType leaveType)
|
|
{
|
|
|
|
|
|
var startFa = $"{yearStr}/{monthStr:00}/01";
|
|
var endFa = startFa.FindeEndOfMonth();
|
|
|
|
if (startFa.TryToGeorgianDateTime(out var start) == false || endFa.TryToGeorgianDateTime(out var end) == false)
|
|
return TimeSpan.Zero;
|
|
|
|
var leaveTotalTimeSpan = TimeSpan.Zero;
|
|
|
|
var leaves = _leaveRepository.GetByWorkshopIdEmployeeIdInDates(workshopId, employeeId, start, end);
|
|
//var timeSpanHourlyLeave = new TimeSpan(leaves.Where(x => x.PaidLeaveType != "روزانه").Sum(x => (x.EndLeaveGr - x.StartLeaveGr).Ticks));
|
|
//var dailyLeaveCount = leaves.Count(x => x.PaidLeaveType == "روزانه") * new TimeSpan(1, 0, 0, 0);
|
|
|
|
if (leaveType == LeaveType.PaidLeave)
|
|
{
|
|
var timeSpanHourlyLeave = new TimeSpan(leaves.Where(x => x.PaidLeaveType == "ساعتی" && x.LeaveType == "استحقاقی")
|
|
.Sum(x => TimeOnly.Parse(x.LeaveHourses).Ticks));
|
|
var dailyLeaveTime = leaves.Where(x => x.PaidLeaveType == "روزانه" && x.LeaveType == "استحقاقی")
|
|
.Sum(x => Convert.ToInt32(x.LeaveHourses)) * TimeSpan.FromDays(1);
|
|
|
|
leaveTotalTimeSpan = timeSpanHourlyLeave + dailyLeaveTime;
|
|
}
|
|
else if (leaveType == LeaveType.SickLeave)
|
|
{
|
|
var timeSpanHourlyLeave = new TimeSpan(leaves.Where(x => x.PaidLeaveType == "ساعتی" && x.LeaveType == "استعلاجی")
|
|
.Sum(x => TimeOnly.Parse(x.LeaveHourses).Ticks));
|
|
|
|
var dailyLeaveTime = leaves.Where(x => x.PaidLeaveType == "روزانه" && x.LeaveType == "استعلاجی")
|
|
.Sum(x => Convert.ToInt32(x.LeaveHourses)) * TimeSpan.FromDays(1);
|
|
|
|
leaveTotalTimeSpan = timeSpanHourlyLeave + dailyLeaveTime;
|
|
}
|
|
else
|
|
{
|
|
var timeSpanHourlyLeave = new TimeSpan(leaves.Where(x => x.PaidLeaveType == "ساعتی").Sum(x => TimeOnly.Parse(x.LeaveHourses).Ticks));
|
|
var dailyLeaveTime = leaves.Where(x => x.PaidLeaveType == "روزانه").Sum(x => Convert.ToInt32(x.LeaveHourses)) * TimeSpan.FromDays(1);
|
|
leaveTotalTimeSpan = timeSpanHourlyLeave + dailyLeaveTime;
|
|
}
|
|
|
|
|
|
|
|
|
|
return leaveTotalTimeSpan;
|
|
}
|
|
|
|
public async Task<List<LeavePrintResponseViewModel>> PrintAllAsync(List<long> ids,long workshopId)
|
|
{
|
|
return await _leaveRepository.PrintAllAsync(ids,workshopId);
|
|
}
|
|
|
|
public async Task<LeavePrintResponseViewModel> PrintOneAsync(long id, long workshopId)
|
|
{
|
|
return (await _leaveRepository.PrintAllAsync([id],workshopId)).FirstOrDefault();
|
|
}
|
|
|
|
|
|
public async Task<OperationResult<CheckIsInvalidLeaveDto>> CheckIsInvalidLeave(string startLeaveDate, long workshopId)
|
|
{
|
|
var op = new OperationResult<CheckIsInvalidLeaveDto>();
|
|
var result = new CheckIsInvalidLeaveDto();
|
|
if (startLeaveDate.TryToGeorgianDateTime(out var startLeaveDateGr) == false)
|
|
{
|
|
return op.Failed("تاریخ وارد شده صحیح نیست");
|
|
}
|
|
|
|
if (startLeaveDateGr.DayOfWeek == DayOfWeek.Friday || _holidayItemRepository.GetHoliday(startLeaveDateGr))
|
|
{
|
|
result.IsHoliday = true;
|
|
}
|
|
|
|
if (result.IsHoliday)
|
|
{
|
|
var rollCallService = _rollCallServiceRepository.GetActiveServiceByWorkshopId(workshopId);
|
|
if (rollCallService != null)
|
|
{
|
|
result.CanCreateInvalid = rollCallService.HasCustomizeCheckoutService == "true";
|
|
}
|
|
}
|
|
|
|
return op.Succcedded(result);
|
|
}
|
|
|
|
|
|
public async Task<OperationResult> CreateLeave(CreateLeaveDto command)
|
|
{
|
|
TimeSpan shiftDuration = TimeSpan.Zero;
|
|
bool hasShiftDuration = false;
|
|
var startH = new TimeSpan();
|
|
var endH = new TimeSpan();
|
|
var op = new OperationResult();
|
|
if ((command.PaidLeaveType == PaidLeaveType.Daily && command.LeaveType == LeaveType.PaidLeave) || command.LeaveType == LeaveType.SickLeave)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(command.StartLeave))
|
|
{
|
|
return op.Failed("لطفا تاریخ شروع را وارد کنید");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(command.EndLeave))
|
|
{
|
|
return op.Failed("لطفا تاریخ پایان را وارد کنید");
|
|
}
|
|
}
|
|
else if (command.PaidLeaveType == PaidLeaveType.Hourly)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(command.StartLeave))
|
|
return op.Failed("لطفا تاریخ شروع را وارد کنید");
|
|
if (string.IsNullOrWhiteSpace(command.StartHoures) || string.IsNullOrWhiteSpace(command.EndHours))
|
|
return op.Failed("ساعت شروع و پایان نمیتواند خالی باشد");
|
|
|
|
string pattern = @"^([01]\d|2[0-3]):[0-5]\d$";
|
|
|
|
if (!Regex.IsMatch(command.StartHoures, pattern))
|
|
return op.Failed("لطفا ساعت شروع را به درستی وارد کنید");
|
|
|
|
if (!Regex.IsMatch(command.EndHours, pattern))
|
|
return op.Failed("لطفا ساعت پایان را به درستی وارد کنید");
|
|
|
|
startH = TimeSpan.Parse(command.StartHoures);
|
|
endH = TimeSpan.Parse(command.EndHours);
|
|
if (startH == endH)
|
|
return op.Failed("ساعت شروع و پایان نباید برابر باشد");
|
|
}
|
|
|
|
|
|
//if (command.LeaveType == LeaveType.SickLeave && string.IsNullOrWhiteSpace(command.StartLeave))
|
|
// return op.Failed("لطفا تاریخ شروع را وارد کنید");
|
|
//if (command.LeaveType == LeaveType.SickLeave && string.IsNullOrWhiteSpace(command.EndLeave))
|
|
// return op.Failed("لطفا تاریخ پایان را وارد کنید");
|
|
|
|
|
|
if (command.StartLeave.TryToGeorgianDateTime(out var start) == false)
|
|
{
|
|
return op.Failed("تاریخ شروع صحیح نیست");
|
|
}
|
|
|
|
var end = new DateTime();
|
|
if (command.PaidLeaveType == PaidLeaveType.Daily)
|
|
{
|
|
if (command.EndLeave.TryToGeorgianDateTime(out end) == false)
|
|
{
|
|
return op.Failed("تاریخ پایان صحیح نیست");
|
|
}
|
|
}
|
|
end = start;
|
|
|
|
|
|
var checkErr = _leaveRepository.CheckErrors(start, end, command.EmployeeId, command.WorkshopId, command.IsInvallid);
|
|
|
|
|
|
if (checkErr.HasChekout)
|
|
return op.Failed(checkErr.CheckoutErrMessage);
|
|
if (checkErr.HasNotContract)
|
|
return op.Failed(checkErr.ContractErrMessage);
|
|
if (checkErr.HasLeftWork)
|
|
return op.Failed(checkErr.LeftWorlErrMessage);
|
|
|
|
if (start > end)
|
|
return op.Failed("تارخ شروع از پایان بزرگتر است");
|
|
|
|
|
|
var totalhourses = "-";
|
|
if (command.LeaveType == LeaveType.PaidLeave && command.PaidLeaveType == PaidLeaveType.Hourly)
|
|
{
|
|
|
|
start = new DateTime(start.Year, start.Month, start.Day, startH.Hours, startH.Minutes, startH.Seconds);
|
|
end = new DateTime(start.Year, start.Month, start.Day, endH.Hours, endH.Minutes, endH.Seconds);
|
|
if (start > end)
|
|
end = end.AddDays(1);
|
|
|
|
var totalLeavHourses = (end - start);
|
|
var h = totalLeavHourses.Hours < 10 ? $"0{totalLeavHourses.Hours}" : $"{totalLeavHourses.Hours}";
|
|
var m = totalLeavHourses.Minutes < 10 ? $"0{totalLeavHourses.Minutes}" : $"{totalLeavHourses.Minutes}";
|
|
totalhourses = $"{h}:{m}";
|
|
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= start && x.EndLeave >= start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی"))
|
|
return op.Failed("برای ساعت شروع سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= end && x.EndLeave >= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی"))
|
|
return op.Failed("برای ساعت پایان سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave >= start && x.EndLeave <= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی"))
|
|
return op.Failed("در بازه زمانی وارد شده مرخصی ثبت شده است");
|
|
|
|
var end24 = endH.Hours == 0 && endH.Minutes == 0 ? end.AddDays(-1) : end;
|
|
if (_leaveRepository.Exists(x =>
|
|
(x.StartLeave.Date == start.Date || x.EndLeave.Date == end24.Date) && (x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه")))
|
|
return op.Failed("در بازه زمانی وارد شده مرخصی ثبت شده است");
|
|
}
|
|
else if (command.LeaveType == LeaveType.PaidLeave && command.PaidLeaveType == PaidLeaveType.Daily)
|
|
{
|
|
var totalLeavHourses = (end - start).TotalDays + 1;
|
|
totalhourses = $"{(int)totalLeavHourses}";
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= start && x.EndLeave >= start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه"))
|
|
return op.Failed("برای تاریخ شروع سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= end && x.EndLeave >= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه"))
|
|
return op.Failed("برای تاریخ پایان سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave >= start && x.EndLeave <= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه"))
|
|
return op.Failed("در بازه زمانی وارد شده مرخصی ثبت شده است");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave.Date >= start.Date && x.StartLeave.Date <= end.Date && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی"))
|
|
return op.Failed("دربازه تاریخ وارد شده مرخصی ساعتی ثبت شده است");
|
|
|
|
var employeeSettings = _employeeSettingsRepository.GetByEmployeeIdAndWorkshopIdIncludeGroupSettings(command.WorkshopId, command.EmployeeId);
|
|
|
|
var isActive = _rollCallEmployeeStatusApplication.IsActiveInPeriod(command.EmployeeId, command.WorkshopId, start, end);
|
|
var hasRollCall = isActive && employeeSettings.WorkshopShiftStatus == WorkshopShiftStatus.Rotating;
|
|
if (hasRollCall)
|
|
{
|
|
|
|
if ((end - start).TotalDays > 1 && employeeSettings.WorkshopShiftStatus != WorkshopShiftStatus.Regular)
|
|
{
|
|
return op.Failed("شما نمیتوانید بیشتر از یک روز مرخصی روزانه ثبت کنید");
|
|
}
|
|
|
|
if (employeeSettings.WorkshopShiftStatus == WorkshopShiftStatus.Rotating &&
|
|
command.SelectedShift == null)
|
|
{
|
|
return op.Failed("لطفا شیفت پرسنل را انتخاب کنید");
|
|
}
|
|
|
|
if (command.SelectedShift != null)
|
|
{
|
|
var validShiftStart = TimeOnly.TryParse(command.SelectedShift.StartTime, out var shiftStart);
|
|
var validShiftEnd = TimeOnly.TryParse(command.SelectedShift.EndTime, out var shiftEnd);
|
|
|
|
if (validShiftStart == false && validShiftEnd == false)
|
|
{
|
|
return op.Failed("شیفت های انتخاب شده معتبر نمیباشد");
|
|
}
|
|
|
|
var shiftStartDateTime = new DateTime(new DateOnly(), shiftStart);
|
|
var shiftEndDateTime = new DateTime(new DateOnly(), shiftEnd);
|
|
if (shiftEndDateTime <= shiftStartDateTime)
|
|
shiftEndDateTime = shiftEndDateTime.AddDays(1);
|
|
shiftDuration = shiftEndDateTime - shiftStartDateTime;
|
|
hasShiftDuration = true;
|
|
}
|
|
else if (employeeSettings is { WorkshopShiftStatus: WorkshopShiftStatus.Irregular })
|
|
{
|
|
if ((end - start).TotalDays > 1)
|
|
{
|
|
return op.Failed("شما نمیتوانید بیشتر از یک روز مرخصی روزانه ثبت کنید");
|
|
}
|
|
|
|
|
|
if (isActive)
|
|
{
|
|
shiftDuration = employeeSettings.IrregularShift.WorkshopIrregularShifts switch
|
|
{
|
|
WorkshopIrregularShifts.TwelveThirtySix => TimeSpan.FromHours(12),
|
|
WorkshopIrregularShifts.TwelveTwentyFour => TimeSpan.FromHours(12),
|
|
WorkshopIrregularShifts.TwentyFourFortyEight => TimeSpan.FromHours(24),
|
|
WorkshopIrregularShifts.TwentyFourTwentyFour => TimeSpan.FromHours(24),
|
|
_ => new TimeSpan()
|
|
};
|
|
hasShiftDuration = true;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
if (command.LeaveType == LeaveType.SickLeave)
|
|
{
|
|
var totalLeavHourses = (end - start).TotalDays + 1;
|
|
totalhourses = $"{(int)totalLeavHourses}";
|
|
command.PaidLeaveType = PaidLeaveType.Daily;
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= start && x.EndLeave >= start && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه"))
|
|
return op.Failed("برای تاریخ شروع سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave <= end && x.EndLeave >= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه"))
|
|
return op.Failed("برای تاریخ پایان سابقه مرخصی وجود دارد");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave >= start && x.EndLeave <= end && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "روزانه"))
|
|
return op.Failed("در بازه زمانی وارد شده مرخصی ثبت شده است");
|
|
if (_leaveRepository.Exists(x =>
|
|
x.StartLeave.Date >= start.Date && x.StartLeave.Date <= end.Date && x.EmployeeId == command.EmployeeId && x.WorkshopId == command.WorkshopId && x.PaidLeaveType == "ساعتی"))
|
|
return op.Failed("دربازه تاریخ وارد شده مرخصی ساعتی ثبت شده است");
|
|
}
|
|
var year = Convert.ToInt32(command.StartLeave.Substring(0, 4));
|
|
var month = Convert.ToInt32(command.StartLeave.Substring(5, 2));
|
|
var paidLeaveType = command.PaidLeaveType == PaidLeaveType.Daily ? "روزانه" : "ساعتی";
|
|
var leaveType = command.LeaveType == LeaveType.PaidLeave ? "استحقاقی" : "استعلاجی";
|
|
var validation = ValidateNewLeaveWithExistingRollCalls(command.WorkshopId, command.EmployeeId, paidLeaveType, start, end);
|
|
if (validation.IsSuccedded == false)
|
|
return validation;
|
|
|
|
var employeeFullName = _employeeRepository.GetDetails(command.EmployeeId).EmployeeFullName;
|
|
var workshopName = _workshopRepository.GetDetails(command.WorkshopId).WorkshopName;
|
|
var leave = new Company.Domain.LeaveAgg.Leave(start, end, totalhourses, command.WorkshopId, command.EmployeeId
|
|
, paidLeaveType, leaveType, employeeFullName, workshopName, command.IsAccepted, command.Decription,
|
|
year, month, shiftDuration, hasShiftDuration, command.IsInvallid);
|
|
await _leaveRepository.CreateAsync(leave);
|
|
await _leaveRepository.SaveChangesAsync();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
|
|
public async Task<OperationResult<RotatingShiftDto>> HasRotatingShift(long workshopId,long employeeId, string startLeaveDate)
|
|
{
|
|
var op = new OperationResult<RotatingShiftDto>();
|
|
var result = new RotatingShiftDto();
|
|
if (startLeaveDate.TryToGeorgianDateTime(out var startDateTimeGr) == false)
|
|
{
|
|
return op.Failed("تاریخ شروع صحیح نیست");
|
|
}
|
|
var employeeSettings = _employeeSettingsRepository.GetByEmployeeIdAndWorkshopIdIncludeGroupSettings(workshopId, employeeId);
|
|
//اگر گروه بندی نداشت
|
|
if (employeeSettings == null)
|
|
{
|
|
return op.Succcedded(result);
|
|
}
|
|
|
|
var isActive = _rollCallEmployeeStatusApplication.IsActiveInPeriod(employeeId, workshopId, startDateTimeGr, startDateTimeGr);
|
|
|
|
result.HasRollCall = isActive && employeeSettings.WorkshopShiftStatus == WorkshopShiftStatus.Rotating;
|
|
result.RotatingShifts = employeeSettings.CustomizeRotatingShifts.Select(x =>
|
|
new CustomizeRotatingShiftsViewModel()
|
|
{
|
|
EndTime = x.EndTime.ToString("HH:mm"),
|
|
StartTime = x.StartTime.ToString("HH:mm")
|
|
}).ToList();
|
|
return op.Succcedded(result);
|
|
}
|
|
#endregion
|
|
|
|
|
|
public async Task<LeaveListPrintDto> ListPrint(List<long> ids)
|
|
{
|
|
return await _leaveRepository.ListPrint(ids);
|
|
}
|
|
|
|
|
|
public async Task<string> GetHourlyLeaveDuration(string startHours, string endHours)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(startHours) || string.IsNullOrWhiteSpace(endHours))
|
|
return "";
|
|
|
|
var start = new DateTime();
|
|
var end = new DateTime();
|
|
try
|
|
{
|
|
|
|
start = Convert.ToDateTime(startHours);
|
|
end = Convert.ToDateTime(endHours);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
return "";
|
|
}
|
|
if (start > end || start == end)
|
|
{
|
|
end = end.AddDays(1);
|
|
}
|
|
|
|
var HourlyDate = (end - start);
|
|
var hours = (int)HourlyDate.TotalHours;
|
|
var minutes = HourlyDate.TotalMinutes % 60;
|
|
|
|
if (hours > 0 && minutes > 0)
|
|
{
|
|
return (hours + " " + "ساعت و" + " " + minutes + " " + "دقیقه");
|
|
}
|
|
else if (hours > 0 && minutes == 0)
|
|
{
|
|
return (hours + " " + "ساعت ");
|
|
|
|
}
|
|
else if (hours == 0 && minutes > 0)
|
|
{
|
|
return (minutes + " " + "دقیقه");
|
|
|
|
}
|
|
|
|
return ($"{hours}");
|
|
|
|
}
|
|
|
|
public async Task<string> GetDailyLeaveDuration(string startDate, string endDate)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(startDate) || string.IsNullOrWhiteSpace(endDate))
|
|
return "";
|
|
|
|
if (startDate.TryToGeorgianDateTime(out var start) == false || endDate.TryToGeorgianDateTime(out var end) == false)
|
|
return "";
|
|
|
|
|
|
|
|
if (end >= start)
|
|
{
|
|
var daysSpan = (end - start).TotalDays + 1;
|
|
return $"{(int)daysSpan} روز";
|
|
}
|
|
else
|
|
{
|
|
return "تاریخ پایان از تاریخ شروع کوچکتر است.";
|
|
}
|
|
}
|
|
} |