563 lines
30 KiB
C#
563 lines
30 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using _0_Framework.Application;
|
|
using _0_Framework.Domain.CustomizeCheckoutShared.Enums;
|
|
using Company.Domain.CustomizeWorkshopEmployeeSettingsAgg;
|
|
using Company.Domain.EmployeeAgg;
|
|
using Company.Domain.LeaveAgg;
|
|
using Company.Domain.RollCallAgg;
|
|
using Company.Domain.WorkshopAgg;
|
|
using CompanyManagment.App.Contracts.Leave;
|
|
using CompanyManagment.App.Contracts.RollCallEmployeeStatus;
|
|
|
|
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;
|
|
|
|
|
|
public LeaveApplication(ILeaveRepository leaveRepository, IEmployeeRepository employeeRepository, IWorkshopRepository workshopRepository, IRollCallRepository rollCallRepository, ICustomizeWorkshopEmployeeSettingsRepository employeeSettingsRepository, IRollCallEmployeeStatusApplication rollCallEmployeeStatusApplication)
|
|
{
|
|
_leaveRepository = leaveRepository;
|
|
_employeeRepository = employeeRepository;
|
|
_workshopRepository = workshopRepository;
|
|
_rollCallRepository = rollCallRepository;
|
|
_employeeSettingsRepository = employeeSettingsRepository;
|
|
_rollCallEmployeeStatusApplication = rollCallEmployeeStatusApplication;
|
|
}
|
|
|
|
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);
|
|
|
|
// 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 (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 Leave(start, end, totalhourses, command.WorkshopId, command.EmployeeId
|
|
, command.PaidLeaveType, command.LeaveType, employeeFullName, workshopName, command.IsAccepted, command.Decription, year, month, shiftDuration, hasShiftDuration);
|
|
_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);
|
|
|
|
|
|
if (checkErr.HasChekout)
|
|
return op.Failed(checkErr.CheckoutErrMessage);
|
|
if (checkErr.HasLeftWork)
|
|
return op.Failed(checkErr.LeftWorlErrMessage);
|
|
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);
|
|
}
|
|
|
|
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)
|
|
{
|
|
|
|
if (startFa.TryToGeorgianDateTime(out var start) == false || endFa.TryToGeorgianDateTime(out var end) == false)
|
|
return 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);
|
|
return timeSpanHourlyLeave + dailyLeaveCount;
|
|
}
|
|
|
|
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
|
|
} |