Files
Backend-Api/ServiceHost/Areas/Client/Pages/Company/Employees/Index.cshtml.cs
2024-10-05 19:27:04 +03:30

485 lines
18 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using _0_Framework.Application;
using Company.Domain.CheckoutAgg;
using Company.Domain.ContractAgg;
using Company.Domain.EmployeeAgg;
using CompanyManagment.App.Contracts.Checkout;
using CompanyManagment.App.Contracts.Contract;
using CompanyManagment.App.Contracts.Employee;
using CompanyManagment.App.Contracts.EmployeeChildren;
using CompanyManagment.App.Contracts.Job;
using CompanyManagment.App.Contracts.Leave;
using CompanyManagment.App.Contracts.LeftWork;
using CompanyManagment.App.Contracts.LeftWorkInsurance;
using CompanyManagment.App.Contracts.PersonnleCode;
using CompanyManagment.App.Contracts.Workshop;
using CompanyManagment.Application;
using MD.PersianDateTime.Standard;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
namespace ServiceHost.Areas.Client.Pages.Company.Employees
{
[Authorize]
public class IndexModel : PageModel
{
[TempData]
public string Message { get; set; }
// public ConnectedPersonnelViewModel Employees;
public PersonnelInfoViewModel Employees;
public bool HasEmployees;
public long WorkshopId { get; set; }
public long EmployeeId { get; set; }
public string WorkshopFullName { get; set; }
public string EmployeeFullName { get; set; }
// public EditWorkshop workshop;
[BindProperty]
public List<string> listArray { get; set; }
public EmployeeSearchModel SearchModel;
public ConnectedPersonnelViewModel ConnectedPersonnel;
private readonly IWorkshopApplication _workshopApplication;
private readonly IEmployeeApplication _employeeApplication;
private readonly IContractApplication _contractApplication;
private readonly ICheckoutApplication _checkoutApplication;
private readonly ILeaveApplication _leaveApplication;
private readonly IPasswordHasher _passwordHasher;
public IndexModel(IWorkshopApplication workshopApplication, IEmployeeApplication employeeApplication, IContractApplication contractApplication, ICheckoutApplication checkoutApplication, IPasswordHasher passwordHasher, ILeaveApplication leaveApplication)
{
_workshopApplication = workshopApplication;
_employeeApplication = employeeApplication;
_contractApplication = contractApplication;
_checkoutApplication = checkoutApplication;
_passwordHasher = passwordHasher;
_leaveApplication = leaveApplication;
}
public IActionResult OnGet(EmployeeSearchModel searchModel)
{
var workshopSlugCliam = User.FindFirstValue("WorkshopSlug");
var id = _passwordHasher.SlugDecrypt(workshopSlugCliam);
if (id > 0)
{
var workshop = _workshopApplication.GetWorkshopInfo(id);
WorkshopFullName = workshop.WorkshopFullName;
WorkshopId = workshop.Id;
var personnelSearchModel = new PersonnelInfoSearchModel()
{
WorkshopId = id,
};
var result = _workshopApplication.GetPersonnelInfo(personnelSearchModel);
var r = result.GroupBy(x => x.EmployeeId).Select(x => x.First()).ToList();
if (!string.IsNullOrWhiteSpace(searchModel.EmployeeFullName))
r = r.Where(x => x.FullName.Contains(searchModel.EmployeeFullName)).ToList();
if (!string.IsNullOrWhiteSpace(searchModel.NationalCode))
r = r.Where(x => x.NationalCode == searchModel.NationalCode).ToList();
Employees = new PersonnelInfoViewModel()
{
PersonnelInfoViewModels = r.OrderBy(x => x.Black ? 1 : 0).ThenBy(x => x.PersonnelCode).ToList(),
};
HasEmployees = Employees.PersonnelInfoViewModels.Count > 0 ? true : false;
return Page();
}
else
{
return NotFound();
}
}
public IActionResult OnGetPrintAllPersonnelInfo(string idlist)
{
var workshopSlugCliam = User.FindFirstValue("WorkshopSlug");
long workshopId = _passwordHasher.SlugDecrypt(workshopSlugCliam);
if (workshopId > 0)
{
var search = new PersonnelInfoSearchModel()
{
WorkshopId = workshopId,
};
var result = _workshopApplication.GetPersonnelInfo(search)
.GroupBy(x => x.FullName)
.Select(x => x.First())
.ToList();
if (!string.IsNullOrWhiteSpace(idlist))
{
var ListEmployeeId = idlist.ExtractNumbers();
result = result.Where(x => ListEmployeeId.Contains(x.EmployeeId)).ToList();
}
var final = new PersonnelInfoPrintViewModel()
{
PersonnelInfoPrintList = result.OrderBy(x => x.Black ? 1 : 0).ThenBy(x => x.PersonnelCode).ToList(),
};
return Partial("PrintAllPersonnelInfo", final);
}
else
{
return NotFound();
}
}
public IActionResult OnGetPrintAllPersonnelInfoMobile(string idlist)
{
var workshopSlugCliam = User.FindFirstValue("WorkshopSlug");
long workshopId = _passwordHasher.SlugDecrypt(workshopSlugCliam);
if (workshopId > 0)
{
var search = new PersonnelInfoSearchModel()
{
WorkshopId = workshopId,
};
var result = _workshopApplication.GetPersonnelInfo(search)
.GroupBy(x => x.FullName)
.Select(x => x.First())
.ToList();
if (!string.IsNullOrWhiteSpace(idlist))
{
var ListEmployeeId = idlist.ExtractNumbers();
result = result.Where(x => ListEmployeeId.Contains(x.EmployeeId)).ToList();
}
var final = new PersonnelInfoPrintViewModel()
{
PersonnelInfoPrintList = result.OrderBy(x => x.Black ? 1 : 0).ThenBy(x => x.PersonnelCode).ToList(),
};
return Partial("PrintAllPersonnelInfoMobile", final);
}
else
{
return NotFound();
}
}
public IActionResult OnGetPrintAllDetailsPersonnelInfo(long workshopID, string idlist)
{
var search = new PersonnelInfoSearchModel()
{
WorkshopId = workshopID,
};
var result = _workshopApplication.GetPersonnelInfo(search)
.GroupBy(x => x.FullName)
.Select(x => x.First())
.ToList();
if (!string.IsNullOrWhiteSpace(idlist))
{
var ListEmployeeId = idlist.ExtractNumbers();
result = result.Where(x => ListEmployeeId.Contains(x.EmployeeId)).ToList();
}
var final = new PersonnelInfoPrintViewModel()
{
PersonnelInfoPrintList = result.OrderBy(x => x.Black ? 1 : 0).ThenBy(x => x.PersonnelCode).ToList(),
};
return Partial("PrintAllDetailsPersonnelInfo", final);
}
public IActionResult OnGetPrintAllDetailsPersonnelInfoMobile(long workshopID, string idlist)
{
var search = new PersonnelInfoSearchModel()
{
WorkshopId = workshopID,
};
var result = _workshopApplication.GetPersonnelInfo(search)
.GroupBy(x => x.FullName)
.Select(x => x.First())
.ToList();
if (!string.IsNullOrWhiteSpace(idlist))
{
var ListEmployeeId = idlist.ExtractNumbers();
result = result.Where(x => ListEmployeeId.Contains(x.EmployeeId)).ToList();
}
var final = new PersonnelInfoPrintViewModel()
{
PersonnelInfoPrintList = result.OrderBy(x => x.Black ? 1 : 0).ThenBy(x => x.PersonnelCode).ToList(),
};
return Partial("PrintAllDetailsPersonnelInfoMobile", final);
}
public IActionResult OnGetContractCheckoutStatus()
{
var workshopSlugCliam = User.FindFirstValue("WorkshopSlug");
long workshopId = _passwordHasher.SlugDecrypt(workshopSlugCliam);
if (workshopId > 0)
{
var workshopName = _workshopApplication.GetDetails(workshopId);
var command = new ContractStatusViewModel();
command.WorkshopName = workshopName.WorkshopFullName;
command.HavingContract = false;
return Partial("ContractCheckoutStatus", command);
}
else
{
return NotFound();
}
}
public IActionResult OnGetContractCheckoutStatusAjax(long employeeId)
{
var workshopSlugCliam = User.FindFirstValue("WorkshopSlug");
long workshopId = _passwordHasher.SlugDecrypt(workshopSlugCliam);
if (workshopId > 0)
{
var workshopName = _workshopApplication.GetDetails(workshopId);
var employeeName = _employeeApplication.GetDetails(employeeId);
var command = new ContractStatusViewModel();
var contractSearch = new ContractSearchModel()
{
EmployeeId = employeeId,
WorkshopIds = workshopId,
};
var checkoutSearch = new CheckoutSearchModel()
{
EmployeeId = employeeId,
WorkshopId = workshopId,
};
var contractResult = _contractApplication.SearchForClient(contractSearch);
var checkoutResult = _checkoutApplication.SimpleSearch(checkoutSearch);
if (contractResult.Count > 0)
{
var divideList = new List<ContractStatusDivide>();
var ChekoutDivideList = new List<CheckoutStatusDivide>();
var groupList = new List<ContractStatusGroup>();
foreach (var item in contractResult)
{
var divide = new ContractStatusDivide()
{
ContractStart = item.ContarctStart,
ContractEnd = item.ContractEnd,
Year = item.ContarctStart.Substring(0, 4),
MonthStart = Convert.ToInt32(item.ContarctStart.Substring(5, 2)),
MonthEnd = Convert.ToInt32(item.ContractEnd.Substring(5, 2)),
SignaturStatus = item.Signature,
};
divideList.Add(divide);
}
foreach (var item in checkoutResult)
{
var CheckoutDivide = new CheckoutStatusDivide()
{
CheckoutStart = item.ContractStart,
CheckoutEnd = item.ContractEnd,
Year = item.ContractStart.Substring(0, 4),
MonthStart = Convert.ToInt32(item.ContractStart.Substring(5, 2)),
MonthEnd = Convert.ToInt32(item.ContractEnd.Substring(5, 2)),
SignaturStatus = item.Signature,
};
ChekoutDivideList.Add(CheckoutDivide);
}
var contractByYear = divideList.GroupBy(x => x.Year).Select(x => x.First()).ToList();
foreach (var year in contractByYear)
{
var divided = divideList.Where(x => x.Year == year.Year).ToList();
var chDivided = ChekoutDivideList.Where(x => x.Year == year.Year).ToList();
if (chDivided.Count < 1)
{
chDivided = new List<CheckoutStatusDivide>();
}
var group = new ContractStatusGroup()
{
Year = year.Year,
ContractStatusDivideList = divided,
CheckoutStatusDivideList = chDivided,
};
groupList.Add(group);
}
command.EmployeeName = employeeName.EmployeeFullName;
command.WorkshopName = workshopName.WorkshopFullName;
command.HavingContract = true;
command.ContractStatusGroupList = groupList;
}
else
{
command.EmployeeName = employeeName.EmployeeFullName;
command.WorkshopName = workshopName.WorkshopFullName;
command.HavingContract = false;
}
return new JsonResult(new { command });
}
else
{
return NotFound();
}
}
public IActionResult OnGetEmployeeList()
{
var workshopSlug = User.FindFirstValue("WorkshopSlug");
var workshopId = _passwordHasher.SlugDecrypt(workshopSlug);
if (workshopId > 0)
{
var result = _workshopApplication.GetConnectedPersonnels(workshopId);
var r = result.GroupBy(x => x.PersonName).Select(x => x.First()).ToList();
ConnectedPersonnel = 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();
//ConnectedPersonnel = new ConnectedPersonnelViewModel()
//{
// ConnectedPersonnelViewModels = filteredBlackResult,
//};
return new JsonResult(new { ConnectedPersonnel });
}
else
{
return new JsonResult(new { ConnectedPersonnel = new ConnectedPersonnelViewModel() });
}
}
public IActionResult OnGetLeaveCreate()
{
var workshopSlugCliam = User.FindFirstValue("WorkshopSlug");
var workshopId = _passwordHasher.SlugDecrypt(workshopSlugCliam);
if (workshopId > 0)
{
var command = new CreateLeave()
{
WorkshopId = workshopId,
};
return Partial("LeaveCreateModal", command);
}
else
{
return NotFound();
}
}
public IActionResult OnPostLeaveSave(CreateLeave command)
{
var result = _leaveApplication.Create(command);
return new JsonResult(new
{
IsSuccedded = result.IsSuccedded,
message = result.Message,
});
}
#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
}
}