Files
Backend-Api/ServiceHost/Areas/Client/Pages/Company/SalaryAid/Index.cshtml.cs
2025-04-16 20:04:57 +03:30

190 lines
5.2 KiB
C#

using _0_Framework.Application;
using CompanyManagment.App.Contracts.Error;
using CompanyManagment.App.Contracts.Loan;
using CompanyManagment.App.Contracts.Workshop;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Security.Claims;
using _0_Framework.Infrastructure;
using CompanyManagement.Infrastructure.Excel.SalaryAid;
using CompanyManagment.App.Contracts.SalaryAid;
using CompanyManagment.App.Contracts.Employee;
namespace ServiceHost.Areas.Client.Pages.Company.SalaryAid
{
[Authorize]
[NeedsPermission(SubAccountPermissionHelper.SalaryAidOperationsPermissionCode)]
public class IndexModel : PageModel
{
private readonly IPasswordHasher _passwordHasher;
private readonly ISalaryAidApplication _salaryAidApplication;
private readonly IEmployeeApplication _employeeApplication;
private readonly IAuthHelper _authHelper;
private readonly SalaryAidImportExcel _salaryAidImportExcel;
private readonly long _workshopId;
public string WorkshopFullName;
public int PageIndex = 0;
public IndexModel(IPasswordHasher passwordHasher, ISalaryAidApplication salaryAidApplication,
IEmployeeApplication employeeApplication, IHttpContextAccessor contextAccessor, IAuthHelper authHelper, SalaryAidImportExcel salaryAidImportExcel)
{
_passwordHasher = passwordHasher;
_salaryAidApplication = salaryAidApplication;
_employeeApplication = employeeApplication;
_authHelper = authHelper;
_salaryAidImportExcel = salaryAidImportExcel;
var workshopHash = _authHelper.GetWorkshopSlug();
_workshopId = _passwordHasher.SlugDecrypt(workshopHash);
if (_workshopId < 1)
throw new InvalidDataException("اختلال در کارگاه");
}
public IActionResult OnGet()
{
WorkshopFullName = _authHelper.GetWorkshopName();
return Page();
}
public IActionResult OnGetLoadDataAjax(SalaryAidSearchViewModel searchViewModel)
{
searchViewModel.WorkshopId = _workshopId;
var result = _salaryAidApplication.GetSearchList(searchViewModel);
return new JsonResult(new
{
success = true,
data = result,
pageIndex = result.Count()
});
}
public IActionResult OnGetLoadDataByEmployeeAjax(SalaryAidSearchViewModel searchViewModel)
{
searchViewModel.WorkshopId = _workshopId;
var result = _salaryAidApplication.GetSearchListAsGrouped(searchViewModel);
return new JsonResult(new
{
success = true,
data = result,
});
}
public async Task<IActionResult> OnGetEmployeeList()
{
var employees = await _employeeApplication.WorkedEmployeesInWorkshopSelectList(_workshopId);
return new JsonResult(new
{
success = true,
data = employees
});
}
public IActionResult OnGetCreate()
{
var command = new CreateSalaryAidViewModel();
return Partial("ModalCreateNewSalaryAid", command);
}
public IActionResult OnPostCreate(CreateSalaryAidViewModel command)
{
command.WorkshopId = _workshopId;
var result = _salaryAidApplication.Create(command);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message,
});
}
public IActionResult OnGetEdit(long id)
{
var command = _salaryAidApplication.GetDetails(id);
return Partial("ModalEditSalaryAid", command);
}
public IActionResult OnPostEdit(EditSalaryAidViewModel command)
{
command.WorkshopId = _workshopId;
var result = _salaryAidApplication.Edit(command);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message,
});
}
public IActionResult OnPostRemove(long id)
{
var result = _salaryAidApplication.Remove(id);
return new JsonResult(new
{
isSuccess = result.IsSuccedded,
message = result.Message,
});
}
public IActionResult OnGetImportExcel()
{
return Partial("ModalImportExcel");
}
public IActionResult OnGetDownloadExcelTemplate()
{
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Excel", "Templates", "SalaryAid", "SA-Template.xlsx");
var bytes = System.IO.File.ReadAllBytes(filePath);
return File(bytes,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"قالب مساعده.xlsx");
}
public IActionResult OnPostValidateExcel(IFormFile excel)
{
var validation = _salaryAidImportExcel.ReadAndValidateExcel(excel, _workshopId);
return new JsonResult(new
{
IsSuccess = !validation.Errors.Any(),
data = validation
});
}
public async Task<IActionResult> OnPostCreateFromExcelData(List<SalaryAidImportData> data)
{
var commands = data.Select(x => new CreateSalaryAidViewModel()
{
WorkshopId = x.WorkshopId,
Amount = x.Amount.ToMoney(),
EmployeeIds = [x.EmployeeId],
SalaryDateTime = x.SalaryAidDateTime,
NationalCode = x.NationalCode
}).ToList();
OperationResult result = await _salaryAidApplication.CreateRange(commands);
return new JsonResult(new
{
result.IsSuccedded,
result.Message
});
}
public IActionResult OnGetSearch(SalaryAidSearchViewModel searchModel)
{
searchModel.WorkshopId = _workshopId;
var result = _salaryAidApplication.GetSearchListAsGrouped(searchModel);
return new JsonResult(new
{
data = result
});
}
}
}