using System;
using HRM.BO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace HRM.UI.Controllers.Payroll
{
///
/// Core Payroll > LFA. The Excel template and upload are in DataUploadController, with every other uploader.
///
[Route("api/lfa")]
[Authorize]
[ApiController]
public class LFAController : ControllerBase
{
private readonly ILFAPaymentService _lfaPaymentService;
public LFAController(ILFAPaymentService lfaPaymentService)
{
this._lfaPaymentService = lfaPaymentService;
}
/// The current salary month and the next batch number.
[HttpGet("getPaymentInfo")]
public ActionResult GetPaymentInfo()
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
if (currentUser.UserType == EnumUserType.Employee)
{
return NotAllowed();
}
try
{
return Ok(this._lfaPaymentService.GetInfo(currentUser.PayrollTypeID.GetValueOrDefault()));
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}
/// Calculates the rows again; nothing is saved.
[HttpPost("calculatePayment")]
public ActionResult CalculatePayment(LFAPaymentRequest request)
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
if (currentUser.UserType == EnumUserType.Employee)
{
return NotAllowed();
}
try
{
return Ok(this._lfaPaymentService.Calculate(request, currentUser.PayrollTypeID.GetValueOrDefault()));
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}
/// Saves the rows as a new batch, all or nothing. The payroll type and user come from the token.
[HttpPost("processPayment")]
public ActionResult ProcessPayment(LFAPaymentRequest request)
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
if (currentUser.UserType == EnumUserType.Employee)
{
return NotAllowed();
}
try
{
return Ok(this._lfaPaymentService.Process(request, currentUser.PayrollTypeID.GetValueOrDefault(), currentUser.UserID));
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}
[HttpGet("getPaymentHistory/{fromDate}/{toDate}")]
public ActionResult GetPaymentHistory(DateTime fromDate, DateTime toDate)
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
if (currentUser.UserType == EnumUserType.Employee)
{
return NotAllowed();
}
try
{
return Ok(this._lfaPaymentService.GetHistory(fromDate, toDate, currentUser.PayrollTypeID.GetValueOrDefault()));
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}
private ActionResult NotAllowed()
{
return StatusCode(StatusCodes.Status403Forbidden, "You are not allowed to process LFA payment.");
}
}
}