383 lines
21 KiB
C#
383 lines
21 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Text.RegularExpressions;
|
|
using _0_Framework.Application;
|
|
using Company.Domain.EmployeeAgg;
|
|
using Company.Domain.LeaveAgg;
|
|
using Company.Domain.WorkshopAgg;
|
|
using CompanyManagment.App.Contracts.Leave;
|
|
|
|
namespace CompanyManagment.Application;
|
|
|
|
public class LeaveApplication : ILeaveApplication
|
|
{
|
|
private readonly ILeaveRepository _leaveRepository;
|
|
private readonly IEmployeeRepository _employeeRepository;
|
|
private readonly IWorkshopRepository _workshopRepository;
|
|
|
|
public LeaveApplication(ILeaveRepository leaveRepository, IEmployeeRepository employeeRepository, IWorkshopRepository workshopRepository)
|
|
{
|
|
_leaveRepository = leaveRepository;
|
|
_employeeRepository = employeeRepository;
|
|
_workshopRepository = workshopRepository;
|
|
}
|
|
|
|
public OperationResult Create(CreateLeave command)
|
|
{
|
|
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("دربازه تاریخ وارد شده مرخصی ساعتی ثبت شده است");
|
|
}
|
|
|
|
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 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);
|
|
_leaveRepository.Create(leave);
|
|
_leaveRepository.SaveChanges();
|
|
|
|
var id = leave.id;
|
|
return op.Succcedded(sendId: id);
|
|
}
|
|
|
|
public OperationResult Edit(EditLeave command)
|
|
{
|
|
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.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 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);
|
|
_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> HasLeave(long workshopId, long employeeId, string dateFa)
|
|
{
|
|
OperationResult<bool> op = new();
|
|
DateTime date = dateFa.ToGeorgianDateTime();
|
|
if (date == Tools.GetUndefinedDateTime())
|
|
return op.Failed("تاریخ وارد شده صحیح نمی باشد");
|
|
var result = _leaveRepository.HasLeave(employeeId, workshopId, date);
|
|
return op.Succcedded(result);
|
|
}
|
|
|
|
public LeavePrintViewModel PrintOne(long id)
|
|
{
|
|
return _leaveRepository.PrintOne(id);
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
}
|
|
} |