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

236 lines
8.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using HRM.BO;
using HRM.DA;
using HRM.UI.DTOs.Training;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static iTextSharp.text.pdf.AcroFields;
namespace HRM.UI.Controllers
{
[ApiController]
[Route("api/Training")]
[Authorize]
public class TrainingController : ControllerBase
{
private readonly ITrainingTypeService _trainingTypeService;
private readonly ITrainingService _trainingService;
private readonly IInstitutionService _institueService;
private readonly INatureOfTrainingService _natureOfTrainingService;
private readonly ITrainingScheduleService _trainingScheduleService;
public TrainingController(ITrainingTypeService trainingType, ITrainingService training,
IInstitutionService institutionService, INatureOfTrainingService natureOfTrainingService, ITrainingScheduleService trainingScheduleService)
{
_trainingTypeService = trainingType;
this._trainingService = training;
this._institueService = institutionService;
this._natureOfTrainingService = natureOfTrainingService;
_trainingScheduleService = trainingScheduleService;
}
[HttpGet("GetAllTrainingType/{status}/{code}/{name}")]
public ActionResult GetAllTrainingType(EnumStatus status, string code, string name)
{
code = GlobalFunctions.GetApiDefaultData(code);
name = GlobalFunctions.GetApiDefaultData(name);
List<TrainingType> items = new List<TrainingType>();
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
int payrollTypeId = currentUser.PayrollTypeID.GetValueOrDefault();
try
{
items = this._trainingTypeService.Get(status);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(items);
}
[HttpGet("GetTrainingybyTypeID/{traininTypeID}")]
public ActionResult GetTrainingybyTypeID(int traininTypeID)
{
List<Training> items = new List<Training>();
try
{
items = this._trainingService.GetbyTypeID(traininTypeID);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(items);
}
[HttpGet("GetAllTraining/{status}/{code}/{name}")]
public ActionResult GetAllTraining(EnumStatus status, string code, string name)
{
code = GlobalFunctions.GetApiDefaultData(code);
name = GlobalFunctions.GetApiDefaultData(name);
List<Training> items = new List<Training>();
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
int payrollTypeId = currentUser.PayrollTypeID.GetValueOrDefault();
try
{
items = this._trainingService.Get();
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(items);
}
[HttpGet("GetAllInstitues/{status}/{code}/{name}")]
public ActionResult GetAllInstitues(EnumStatus status, string code, string name)
{
code = GlobalFunctions.GetApiDefaultData(code);
name = GlobalFunctions.GetApiDefaultData(name);
List<Institution> items = new List<Institution>();
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
int payrollTypeId = currentUser.PayrollTypeID.GetValueOrDefault();
try
{
items = this._institueService.Get();
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(items);
}
[HttpGet("GetAllNatureOfTraining/{status}/{code}/{name}")]
public ActionResult GetAllNatureOfTraining(EnumStatus status, string code, string name)
{
code = GlobalFunctions.GetApiDefaultData(code);
name = GlobalFunctions.GetApiDefaultData(name);
List<NatureOfTraining> items = new List<NatureOfTraining>();
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
int payrollTypeId = currentUser.PayrollTypeID.GetValueOrDefault();
try
{
items = this._natureOfTrainingService.Get();
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(items);
}
[HttpPost]
[Route("savetrainingschedule")]
public ActionResult savetrainingschedule(TrainingSchedule ob)
{
try
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
ob.CreatedBy = currentUser.UserID;
ob.CreatedDate = DateTime.Today;
if(ob.EnrolledStartDate== DateTime.MinValue)
{
ob.EnrolledStartDate = null;
}
if (ob.EnrolledEndDate == DateTime.MinValue)
{
ob.EnrolledEndDate = null;
}
_trainingScheduleService.Save(ob);
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
}
return Ok();
}
[HttpPost("getTrainingSchedule")]
public ActionResult GetTrainingSchedule(dynamic data)
{
List<TrainingSchedule> list = new List<TrainingSchedule>();
var items = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data));
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
int payrollTypeId = currentUser.PayrollTypeID.GetValueOrDefault();
DateTime? fromDate =null;
DateTime? toDate = null;
if (items["fromDate"] != null)
fromDate = (DateTime)items["fromDate"].ToObject<DateTime>();
if (items["toDate"] != null)
toDate = (DateTime)items["toDate"].ToObject<DateTime>();
int trainingId = items["trainingId"].ToObject<int>();
int trainingTypeId = items["trainingTypeId"].ToObject<int>();
try
{
list = _trainingScheduleService.GetTrainingSchedule(trainingId, trainingTypeId, fromDate, toDate);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(list);
}
[HttpGet("getTrainingScheduleById/{id}")]
public ActionResult getTrainingScheduleById(int id)
{
TrainingSchedule item = null;
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
int payrollTypeId = currentUser.PayrollTypeID.GetValueOrDefault();
try
{
List<Department> departments = new DepartmentService().Get(EnumStatus.Regardless, (int)payrollTypeId);
item = this._trainingScheduleService.Get(id);
if(item != null && item.EnrolledTrainingEmployees != null && item.EnrolledTrainingEmployees.Count > 0)
{
foreach(var x in item.EnrolledTrainingEmployees)
{
x.Employee = new Employee();
x.Employee = new EmployeeService().Get(x.EmployeeID);
var department = departments.FirstOrDefault(d => d.ID == x.Employee.DepartmentID);
if (department != null)
x.Employee.DepartmentName = department.Name;
}
}
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(item);
}
[HttpGet]
[Route("deleteTrainingByID/{id}")]
public ActionResult deleteTrainingByID(int id)
{
try
{
this._trainingScheduleService.Delete(id);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok();
}
}
}