Files
Backend-Api/ServiceHost/Areas/Client/Pages/Company/Employees/Leave.cshtml.cs

494 lines
17 KiB
C#

using _0_Framework.Application;
using Company.Domain.EmployeeAgg;
using Company.Domain.empolyerAgg;
using CompanyManagment.App.Contracts.Employee;
using CompanyManagment.App.Contracts.Leave;
using CompanyManagment.App.Contracts.SmsResult;
using CompanyManagment.App.Contracts.Workshop;
using CompanyManagment.App.Contracts.YearlySalary;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Security.Claims;
using System.Text.RegularExpressions;
using _0_Framework.Infrastructure;
using _0_Framework.Domain.CustomizeCheckoutShared.Enums;
using CompanyManagment.App.Contracts.CustomizeWorkshopSettings;
using CompanyManagment.App.Contracts.HolidayItem;
using CompanyManagment.App.Contracts.RollCallEmployeeStatus;
using CompanyManagment.App.Contracts.RollCallService;
namespace ServiceHost.Areas.Client.Pages.Company.Employees
{
[Authorize]
[NeedsPermission(SubAccountPermissionHelper.LeaveListPermissionCode)]
public class LeaveModel : PageModel
{
public List<LeaveMainViewModel> LeaveSearch;
public EditLeave LeaveDetails;
public string WorkshopFullName;
public long WorkshopId;
public long EmployeeId;
public long EditId;
public string EmployeeFullName;
public ConnectedPersonnelViewModel personleList;
public List<string> YearlyList;
public int Year;
public int Month;
public bool IsEmployeeActive;
public LeaveSearchModel SearchModel;
public GroupLeavePrintViewModel sendIds;
public List<long> printIdList;
public List<LeavePrintViewModel> AllPrintList;
private readonly IWorkshopApplication _workshopApplication;
private readonly IEmployeeApplication _employeeApplication;
private readonly ILeaveApplication _leaveApplication;
private readonly IYearlySalaryApplication _yearlySalaryApplication;
private readonly IAuthHelper _authHelper;
private readonly IPasswordHasher _passwordHasher;
private readonly ICustomizeWorkshopSettingsApplication _customizeWorkshopSettingsApplication;
private readonly IRollCallEmployeeStatusApplication _rollCallEmployeeStatusApplication;
private readonly IHttpContextAccessor _contextAccessor;
private readonly long _workshopId;
private readonly IHolidayItemApplication _holidayItemApplication;
private readonly IRollCallServiceApplication _rollCallServiceApplication;
public int PageIndex;
public bool HasCustomizeCheckout { get; set; }
#region Initial Data
public LeaveModel(IWorkshopApplication workshopApplication, IEmployeeApplication employeeApplication, IAuthHelper authHelper, ILeaveApplication leaveApplication, IYearlySalaryApplication yearlySalaryApplication, IPasswordHasher passwordHasher, IHttpContextAccessor contextAccessor, IRollCallEmployeeStatusApplication rollCallEmployeeStatusApplication, ICustomizeWorkshopSettingsApplication customizeWorkshopSettingsApplication, IHolidayItemApplication holidayItemApplication, IRollCallServiceApplication rollCallServiceApplication)
{
_workshopApplication = workshopApplication;
_employeeApplication = employeeApplication;
_leaveApplication = leaveApplication;
_yearlySalaryApplication = yearlySalaryApplication;
_passwordHasher = passwordHasher;
_contextAccessor = contextAccessor;
_rollCallEmployeeStatusApplication = rollCallEmployeeStatusApplication;
_customizeWorkshopSettingsApplication = customizeWorkshopSettingsApplication;
_holidayItemApplication = holidayItemApplication;
_rollCallServiceApplication = rollCallServiceApplication;
_authHelper = authHelper;
var workshopHash = _authHelper.GetWorkshopSlug();
_workshopId = _passwordHasher.SlugDecrypt(workshopHash);
if (_workshopId < 1)
throw new InvalidDataException("اختلال در کارگاه");
}
#endregion
#region Get Leave Data
public IActionResult OnGet(LeaveSearchModel searchModel, string editId)
{
if (_workshopId > 0)
{
//long employeeId = _passwordHasher.SlugDecrypt(employeeHash);
long employeeId = searchModel.EmployeeId;
var rollCallServiceViewModel = _rollCallServiceApplication.GetActiveServiceByWorkshopId(_workshopId);
HasCustomizeCheckout = rollCallServiceViewModel is { HasCustomizeCheckoutService: "true" };
if (employeeId >= 0)
{
var workshop = _workshopApplication.GetDetails(_workshopId);
var search = new LeaveSearchModel()
{
EmployeeId = employeeId,
WorkshopId = _workshopId,
LeaveType = searchModel.LeaveType,
Year = searchModel.Year,
Month = searchModel.Month,
StartLeave = searchModel.StartLeave,
EndLeave = searchModel.EndLeave,
IsInvalid = searchModel.IsInvalid
};
Year = searchModel.Year;
Month = searchModel.Month;
SearchModel = search;
if (employeeId > 0)
{
var employee = _employeeApplication.GetDetails(employeeId);
LeaveSearch = _leaveApplication.searchClient(search);
EmployeeFullName = employee.EmployeeFullName;
IsEmployeeActive = true;
}
else
{
LeaveSearch = new List<LeaveMainViewModel>();
IsEmployeeActive = false;
}
WorkshopFullName = workshop.WorkshopFullName;
WorkshopId = _workshopId;
EmployeeId = employeeId;
YearlyList = _yearlySalaryApplication.GetYears();
EditId = string.IsNullOrWhiteSpace(editId) ? 0 : Convert.ToInt64(editId);
return Page();
}
else
{
return NotFound();
}
}
else
{
return NotFound();
}
}
public IActionResult OnGetLastLeave(LeaveSearchModel searchModel)
{
var result = _leaveApplication.LastLeaveMain(searchModel);
int pageIndexCount = result.Count();
return new JsonResult(new
{
data = result,
pageIndex = pageIndexCount
});
}
#endregion
#region CreateCreate Leave
public IActionResult OnGetCreateLeave(long employeeId, long workshopId)
{
var command = new CreateLeave()
{
EmployeeId = employeeId,
WorkshopId = workshopId
};
return Partial("CreateLeave", command);
}
#endregion
#region Save Leave
public IActionResult OnPostSaveLeave(CreateLeave command)
{
var result = _leaveApplication.Create(command);
return new JsonResult(new
{
IsSuccedded = result.IsSuccedded,
message = result.Message,
PrintID = result.SendId
});
}
public IActionResult OnPostCheckIsInvalidLeave(string startDate)
{
bool isInHoliday = false;
bool canCreateInvalid = false;
var dateTimeGr = startDate.ToGeorgianDateTime();
if (dateTimeGr.DayOfWeek == DayOfWeek.Friday)
{
isInHoliday = true;
}else if (_holidayItemApplication.IsHoliday(dateTimeGr))
{
isInHoliday = true;
};
if (isInHoliday)
{
var rollCallService = _rollCallServiceApplication.GetActiveServiceByWorkshopId(_workshopId);
if (rollCallService != null)
{
canCreateInvalid =rollCallService.HasCustomizeCheckoutService == "true";
}
}
return new JsonResult(new
{
isInHoliday,
canCreateInvalid
});
}
#endregion
#region Check If Valide To Edit
public IActionResult OnGetCheckIfValidToEdit(long id)
{
var check = _leaveApplication.CheckIfValidToEdit(id);
if (check)
{
return new JsonResult(new
{
validToEdit = true
});
}
else
{
return new JsonResult(new
{
validToEdit = false
});
}
}
#endregion
#region Edit Leave
public IActionResult OnGetEditLeave(long id)
{
var res = _leaveApplication.GetDetails(id);
return Partial("EditLeave", res);
}
#endregion
#region Edit Leave
public IActionResult OnPostEditLeave(EditLeave command)
{
if (ModelState.IsValid)
{
}
var result = _leaveApplication.Edit(command);
var res = result.IsSuccedded;
return new JsonResult(new
{
IsSuccedded = res,
message = result.Message,
employeeId = command.EmployeeId,
workshopId = command.WorkshopId,
hd = 1
});
}
#endregion
#region Compute Leave Hourly by ajax
public IActionResult OnGetComputeLeaveHourly(string startHours, string endHours)
{
var start = Convert.ToDateTime(startHours);
var end = Convert.ToDateTime(endHours);
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 new JsonResult(new
{
res = hours + " " + "ساعت و" + " " + minutes + " " + "دقیقه",
});
}
else if (hours > 0 && minutes == 0)
{
return new JsonResult(new
{
res = hours + " " + "ساعت ",
});
}
else if (hours == 0 && minutes > 0)
{
return new JsonResult(new
{
res = minutes + " " + "دقیقه",
});
}
return new JsonResult(new
{
totalHours = $"{hours}",
});
}
#endregion
#region Compute Leave Daily by ajax
public IActionResult OnGetComputeLeaveDaily(string startDay, string endDay)
{
if (!string.IsNullOrWhiteSpace(startDay) && !string.IsNullOrWhiteSpace(endDay))
{
var start = startDay.ToGeorgianDateTime();
var end = endDay.ToGeorgianDateTime();
if (end >= start)
{
var daysSpan = (end - start).TotalDays + 1;
return new JsonResult(new
{
res = $"{(int)daysSpan} روز",
});
}
else
{
return new JsonResult(new
{
status = false,
res = "تاریخ پایان از تاریخ شروع کوچکتر است.",
});
}
}
return new JsonResult(new
{
res = "",
});
}
#endregion
#region Remove Paid Leave
public IActionResult OnPostRemovePaidLeave(long id, long EmployeeId, long WorkshopId)
{
var result = _leaveApplication.RemoveLeave(id);
var res = result.IsSuccedded;
return new JsonResult(new
{
IsSuccedded = res,
message = result.Message,
employeeId = EmployeeId,
workshopId = WorkshopId,
hd = 1
});
}
#endregion
#region PrintOne
public IActionResult OnGetPrintOne(long id)
{
var res = _leaveApplication.PrintOne(id);
return Partial("PrintOne", res);
}
#endregion
#region PrintOneMobile
public IActionResult OnGetPrintOneMobile(long id)
{
var res = _leaveApplication.PrintOne(id);
return Partial("PrintOneMobile", res);
}
#endregion
#region LeavePrintAllList
public IActionResult OnGetLeavePrintAllList(string idlist)
{
var ids = ExtractNumbers(idlist);
var res = _leaveApplication.PrintPersonnelLeaveList(ids);
return Partial("PrintAllList", res);
}
static List<long> ExtractNumbers(string input)
{
List<long> numbers = new List<long>();
string pattern = @"\d+"; // Matches one or more digits
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
if (long.TryParse(match.Value, out long number))
{
numbers.Add(number);
}
}
return numbers;
}
#endregion
#region Load All Personal by ajax (EmployeeList)
public async Task<IActionResult> OnGetEmployeeList(long workshopId)
{
var employees = await _employeeApplication.WorkedEmployeesInWorkshopSelectList(workshopId);
//var result = _workshopApplication.GetConnectedPersonnels(workshopId);
// var r = result.GroupBy(x => x.PersonName).Select(x => x.First()).ToList();
// personleList = new ConnectedPersonnelViewModel()
// {
// ConnectedPersonnelViewModels = r.OrderBy(x => x.Black ? 1 : 0).ThenBy(x => x.PersonelCode).ToList(),
// };
// لیست پرسنلی که قرارداد ندارند
//var filteredBlackResult = _workshopApplication.GetConnectedPersonnels(workshopId)
// .Where(x => !x.Black && x.ContractPerson)
// .GroupBy(x => x.EmployeeId)
// .Select(x => x.First())
// .OrderBy(x => x.PersonelCode)
// .ToList();
//personleList = new ConnectedPersonnelViewModel()
//{
// ConnectedPersonnelViewModels = filteredBlackResult,
//};
//return new JsonResult(new { personleList });
return new JsonResult(new
{
success = true,
data = employees
});
}
#endregion
#region Calculate total leave in dates
public IActionResult OnGetEmployeeTotalLeave(long employeeId, string startFa, string endFa, int month, int year,
string type)
{
//اگر بازه تاریخ خالی بود از سال و ماه استفاده می شود
if (string.IsNullOrWhiteSpace(startFa) && string.IsNullOrWhiteSpace(endFa))
{
startFa = $"{year}/{month:00}/01";
endFa = startFa.FindeEndOfMonth();
}
TimeSpan timeSpan = _leaveApplication.GetEmployeeLeaveTimeSpanInDates(_workshopId, employeeId, startFa, endFa, type);
string message = timeSpan.ToFarsiDaysAndHoursAndMinutes();
return new JsonResult(new
{
Message = message
});
}
#endregion
#region RotatingShift
public IActionResult OnGetHasRotatingShift(long employeeId, string startDateTime)
{
if (startDateTime.TryToGeorgianDateTime(out var startDateTimeGr) == false)
{
return new JsonResult(new
{
HasRollCall = false,
Message = "تاریخ وارد شده نامعتبر میباشد",
Shifts = (List<object>)[]
});
}
var workshopSlug = User.FindFirst("WorkshopSlug")?.Value;
long workshopId = _passwordHasher.SlugDecrypt(workshopSlug);
var employeeSettings = _customizeWorkshopSettingsApplication.GetByEmployeeIdAndWorkshopIdIncludeGroupSettings(workshopId, employeeId);
if (employeeSettings == null)
{
return new JsonResult(new
{
HasRollCall = false,
Shifts = (List<object>)[],
});
}
var isActive = _rollCallEmployeeStatusApplication.IsActiveInPeriod(employeeId, workshopId, startDateTimeGr, startDateTimeGr);
return new JsonResult(new
{
HasRollCall = isActive && employeeSettings.WorkshopShiftStatus == WorkshopShiftStatus.Rotating,
Shifts = employeeSettings.CustomizeRotatingShiftsViewModels
});
}
#endregion
}
}