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); } } // ── LFA Payment Undo ── /// Every batch of the current payroll type, and whether each one can be undone. [HttpGet("getUndoBatches")] public ActionResult GetUndoBatches() { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); if (currentUser.UserType == EnumUserType.Employee) { return NotAllowed(); } try { return Ok(this._lfaPaymentService.GetUndoBatches(currentUser.PayrollTypeID.GetValueOrDefault())); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } /// LFA Bank Advice: the approved batches of the payroll type. [HttpGet("getBankAdviceBatches")] public ActionResult GetBankAdviceBatches() { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); if (currentUser.UserType == EnumUserType.Employee) { return NotAllowed(); } try { return Ok(this._lfaPaymentService.GetBankAdviceBatches(currentUser.PayrollTypeID.GetValueOrDefault())); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } [HttpGet("getBatchEmployees/{lfaPaymentID}")] public ActionResult GetBatchEmployees(int lfaPaymentID) { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); if (currentUser.UserType == EnumUserType.Employee) { return NotAllowed(); } try { return Ok(this._lfaPaymentService.GetBatchEmployees(lfaPaymentID, currentUser.PayrollTypeID.GetValueOrDefault())); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } /// Undoes the whole batch or the selected employees of it; the server checks everything again. [HttpPost("undoPayment")] public ActionResult UndoPayment(LFAPaymentUndoRequest request) { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); if (currentUser.UserType == EnumUserType.Employee) { return NotAllowed(); } try { return Ok(this._lfaPaymentService.Undo(request, currentUser.PayrollTypeID.GetValueOrDefault(), currentUser.UserID)); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } // ── LFA Payment Approval (as Salary Approve: Direct Approve or the approval workflow, Logic lfaapproval/workflow) ── /// Logic key, whether the login has an employee, and whether Work Flow Setup has LFA rules. [HttpGet("getApprovalInfo")] public ActionResult GetApprovalInfo() { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); if (currentUser.UserType == EnumUserType.Employee) { return NotAllowed(); } try { return Ok(this._lfaPaymentService.GetApprovalInfo(currentUser.EmployeeID)); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } [HttpGet("getApprovalBatches")] public ActionResult GetApprovalBatches() { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); if (currentUser.UserType == EnumUserType.Employee) { return NotAllowed(); } try { return Ok(this._lfaPaymentService.GetApprovalBatches(currentUser.PayrollTypeID.GetValueOrDefault())); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } [HttpGet("getFlowHistory/{lfaPaymentID}")] public ActionResult GetFlowHistory(int lfaPaymentID) { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); if (currentUser.UserType == EnumUserType.Employee) { return NotAllowed(); } try { return Ok(this._lfaPaymentService.GetFlowHistory(lfaPaymentID, currentUser.PayrollTypeID.GetValueOrDefault())); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } /// Direct Approve, when the workflow is off. [HttpPost("approveDirect")] public ActionResult ApproveDirect(LFAApprovalRequest request) { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); if (currentUser.UserType == EnumUserType.Employee) { return NotAllowed(); } try { return Ok(this._lfaPaymentService.ApproveDirect(request, currentUser.PayrollTypeID.GetValueOrDefault(), currentUser.UserID)); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } /// Submit for approval (or submit again after a revert), when the workflow is on. [HttpPost("submitForApproval")] public ActionResult SubmitForApproval(LFAApprovalRequest request) { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); if (currentUser.UserType == EnumUserType.Employee) { return NotAllowed(); } try { return Ok(this._lfaPaymentService.SubmitForApproval(request, currentUser.PayrollTypeID.GetValueOrDefault(), currentUser.EmployeeID, currentUser.UserID)); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } // The approver's side (/payment-approval/lfa). Approvers act as employees, as on Salary's Payment Approval // page, so these are open to every login; the service checks that the step is pending with the login. [HttpGet("getApproverBatch/{wfMovementTranID}")] public ActionResult GetApproverBatch(int wfMovementTranID) { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); try { return Ok(this._lfaPaymentService.GetApproverBatch(wfMovementTranID, currentUser.EmployeeID)); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } [HttpPost("approveFromApprover")] public ActionResult ApproveFromApprover(LFAApproverActionRequest request) { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); try { return Ok(this._lfaPaymentService.ApproveFromApprover(request, currentUser.EmployeeID, currentUser.UserID)); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } [HttpPost("revertFromApprover")] public ActionResult RevertFromApprover(LFAApproverActionRequest request) { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); try { return Ok(this._lfaPaymentService.RevertFromApprover(request, currentUser.EmployeeID)); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } } private ActionResult NotAllowed() { return StatusCode(StatusCodes.Status403Forbidden, "You are not allowed to process LFA payment."); } } }