EchoTex_Payroll/HRM.UI/Controllers/Payroll/OverTimeController.cs
2024-10-14 10:01:49 +06:00

533 lines
17 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HRM.BO;
using HRM.DA;
namespace HRM.UI.Controllers.Payroll
{
[Route("api/Overtime")]
[ApiController]
public class OverTimeController : ControllerBase
{
private readonly ITermParameterService _termParameterService;
private readonly IEmployeeOverTimeService _employeeOverTimeService;
private readonly IOTProcessService _oTProcessService;
public OverTimeController(ITermParameterService termParameterService,
IEmployeeOverTimeService employeeOverTimeService,
IOTProcessService oTProcessService)
{
this._termParameterService = termParameterService;
this._employeeOverTimeService = employeeOverTimeService;
this._oTProcessService = oTProcessService;
}
// Term Parameter
[HttpGet("getTermParameter/{id}")]
public ActionResult getTermParameter(int id)
{
TermParameter termParameter = new TermParameter();
try
{
termParameter = _termParameterService.Get(id);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(termParameter);
}
[HttpGet("GetUsedGrades/{termID}")]
public ActionResult GetUsedGrades(int termID)
{
List<TermParameter.TermEntityGrade> items = new List<TermParameter.TermEntityGrade>();
try
{
items = _termParameterService.GetUsedGrades(termID);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(items);
}
[HttpGet("getAllTermParameters")]
public ActionResult getAllTermParameters()
{
List<TermParameter> termParameters = new List<TermParameter>();
CurrentUser ouser = CurrentUser.GetCurrentUser(HttpContext.User);
try
{
termParameters = _termParameterService.GetByPayrollTypeID((int)ouser.PayrollTypeID);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(termParameters);
}
[HttpGet("getAllTermParameterByGradeID/{id}")]
public ActionResult getAllTermParameterByGradeID(int id)
{
List<TermParameter> termParameters = new List<TermParameter>();
try
{
termParameters = _termParameterService.GetByGradeID(id);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(termParameters);
}
[HttpGet("getTermParameterGrade/{ntpID}")]
public ActionResult GetTermParameterGrade(int ntpID)
{
List<TermParameter.TermEntityGrade> termEntityGrades = new List<TermParameter.TermEntityGrade>();
try
{
termEntityGrades = _termParameterService.GetGrades(ntpID);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(termEntityGrades);
}
[HttpPost]
[Route("saveTermParameter")]
public ActionResult saveTermParameter(TermParameter termParameter)
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
try
{
termParameter.PayrollTypeID = (int) currentUser.PayrollTypeID;
if (termParameter.IsNew == true)
{
termParameter.CreatedBy = currentUser.UserID;
termParameter.CreatedDate = DateTime.Today;
}
else
{
termParameter.ModifiedBy = currentUser.UserID;
termParameter.ModifiedDate = DateTime.Today;
}
_termParameterService.Save(termParameter);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(termParameter);
}
[HttpPost]
[Route("deleteTermParameter")]
public ActionResult deleteTermParameter(TermParameter param)
{
try
{
_termParameterService.Delete(param.ID);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok();
}
// Employee Overtime
[HttpGet("getEmployeeOvertimeById/{id}")]
public ActionResult GetEmployeeOvertimeById(int id)
{
EmployeeOverTime employeeOverTime = new EmployeeOverTime();
try
{
employeeOverTime = _employeeOverTimeService.Get(id);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(employeeOverTime);
}
[HttpGet("getEmployeeOvertime")]
public ActionResult GetEmployeeOvertime()
{
List<EmployeeOverTime> employeeOverTimes = new List<EmployeeOverTime>();
try
{
employeeOverTimes = _employeeOverTimeService.Get();
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(employeeOverTimes);
}
[HttpGet("getEmployeeOvertimeByEmpId/{empId}")]
public ActionResult getEmployeeOvertimeByEmpId(int empId)
{
List<EmployeeOverTime> employeeOverTimes = new List<EmployeeOverTime>();
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
try
{
employeeOverTimes =
_employeeOverTimeService.GetByEmpID(empId, (DateTime) currentUser.NextPayProcessDate);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(employeeOverTimes);
}
[HttpGet("getEmployeeOverTimeByMonthAndPayrollTypeId/{otMonth}")]
public ActionResult GetEmployeeOverTimeByMonthAndPayrollTypeId(DateTime otMonth)
{
List<EmployeeOverTime> employeeOverTimes = new List<EmployeeOverTime>();
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
int payrollTypeID = currentUser.PayrollTypeID.GetValueOrDefault();
try
{
employeeOverTimes = _employeeOverTimeService.Get(otMonth, payrollTypeID);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(employeeOverTimes);
}
[HttpPost]
[Route("saveEmployeeOvertimes")]
public ActionResult SaveEmployeeOvertimes(List<EmployeeOverTime> employeeOverTimes)
{
try
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
employeeOverTimes.ForEach(x =>
{
x.PayrollTypeID = (int) currentUser.PayrollTypeID;
x.MonthDate = (DateTime) currentUser.NextPayProcessDate;
x.OTMonth = GlobalExtensions.LastDateOfMonth(x.OTMonth);
x.CreatedBy = currentUser.UserID;
x.CreatedDate = DateTime.Today;
});
_employeeOverTimeService.Save(employeeOverTimes);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok();
}
[HttpPost]
[Route("saveEmployeeOvertime")]
public ActionResult SaveEmployeeOvertime(EmployeeOverTime employeeOverTime)
{
try
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
employeeOverTime.PayrollTypeID = (int) currentUser.PayrollTypeID;
employeeOverTime.MonthDate = (DateTime)currentUser.NextPayProcessDate;
employeeOverTime.OTMonth = GlobalExtensions.LastDateOfMonth(employeeOverTime.OTMonth);
employeeOverTime.CreatedBy = currentUser.UserID;
employeeOverTime.CreatedDate = DateTime.Today;
EmployeeOverTime existEmployeeOT = new EmployeeOverTimeService().GetEmpOvertimeByOTMonth(employeeOverTime.EmployeeID, employeeOverTime.OTMonth, employeeOverTime.TermID, employeeOverTime.TermParameterID);
if (existEmployeeOT != null)
{
employeeOverTime.ID = existEmployeeOT.ID;
}
_employeeOverTimeService.Save(employeeOverTime);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok();
}
[HttpPost]
[Route("saveEmployeeOvertimeList")]
public ActionResult SaveEmployeeOvertimeList(List<EmployeeOverTime> employeeOverTimes)
{
try
{
_employeeOverTimeService.Save(employeeOverTimes);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(employeeOverTimes);
}
[HttpPost]
[Route("saveEmployeeOvertimeByExcel")]
public ActionResult SaveEmployeeOvertimeByExcel(List<EmployeeOverTime> employeeOverTimes)
{
try
{
_employeeOverTimeService.SaveByExcel(employeeOverTimes);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(employeeOverTimes);
}
[HttpPost]
[Route("deleteEmployeeOvertimeByMonth")]
public ActionResult DeleteEmployeeOvertimeByMonth(DateTime otMonth)
{
try
{
_employeeOverTimeService.DeleteByMonth(otMonth);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok();
}
[HttpPost]
[Route("deleteEmployeeOvertime")]
public ActionResult deleteEmployeeOvertime(EmployeeOverTime employeeOverTime)
{
try
{
_employeeOverTimeService.Delete(employeeOverTime);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok();
}
// OT process
[HttpGet("getOtProcessById/{id}")]
public ActionResult GetOtProcessById(int id)
{
OTProcess oTProcess = new OTProcess();
try
{
oTProcess = _oTProcessService.Get(id);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(oTProcess);
}
[HttpGet("getAllOtProcess")]
public ActionResult GetAllOtProcess()
{
List<OTProcess> oTProcesses = new List<OTProcess>();
try
{
oTProcesses = _oTProcessService.Get();
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(oTProcesses);
}
[HttpGet("getOtProcessByMonthDate/{monthDate}")]
public ActionResult GetOtProcessByMonthDate(DateTime monthDate)
{
List<OTProcess> oTProcesses = new List<OTProcess>();
try
{
oTProcesses = _oTProcessService.Get(monthDate);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(oTProcesses);
}
[HttpGet("getOtProcessByOTMonth/{empIds}/{months}")]
public ActionResult GetOtProcessByOTMonth(string empIds, string months)
{
List<OTProcess> oTProcesses = new List<OTProcess>();
try
{
oTProcesses = _oTProcessService.GetbyOtMonth(empIds, months);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(oTProcesses);
}
[HttpGet("getOtProcessDataByMonthAndDeptID/{month}/{deptID}")]
public ActionResult GetOtProcessDataByMonthAndDeptID(DateTime month, string deptID)
{
List<OTProcess> oTProcesses = new List<OTProcess>();
try
{
oTProcesses = _oTProcessService.GetOTProcessDataByMonthAndDeptID(month, deptID);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(oTProcesses);
}
[HttpGet("isOtProcessed/{monthDate}")]
public ActionResult IsOtProcessed(string monthDate)
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
DateTime dMonthDate = GlobalFunctions.GetApiDefaultDateData(monthDate);
int payrollTypeID = currentUser.PayrollTypeID.GetValueOrDefault();
bool result;
try
{
result = _oTProcessService.IsProcessed(dMonthDate, payrollTypeID);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(result);
}
[HttpPost]
[Route("OTProcess")]
public ActionResult OTProcess(dynamic item)
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
try
{
this._oTProcessService.Process(null, currentUser.UserID, (int) currentUser.PayrollTypeID);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(true);
}
[HttpPost]
[Route("saveOtProcess")]
public ActionResult SaveOtProcess(OTProcess oTProcess)
{
try
{
_oTProcessService.Save(oTProcess);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(oTProcess);
}
[HttpPost]
[Route("saveOtProcessList")]
public ActionResult SaveOtProcessList(List<OTProcess> oTProcesses)
{
try
{
_oTProcessService.Save(oTProcesses);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(oTProcesses);
}
[HttpPost]
[Route("deleteOtProcess")]
public ActionResult DeleteOtProcess(int id)
{
try
{
_oTProcessService.Delete(id);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok();
}
[HttpPost]
[Route("undoOTProcess")]
public ActionResult undoOTProcess(dynamic item)
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
int payrollTypeID = currentUser.PayrollTypeID.GetValueOrDefault();
try
{
_oTProcessService.Delete((DateTime) currentUser.NextPayProcessDate, payrollTypeID);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok();
}
}
}