using LCU.API.Internal.Operations.IncentiveApi.Services; using LCU.API.Internal.Operations.IncentiveApi.Services.Payout; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.FeatureManagement.Mvc; using LCU.API.Internal.Operations.IncentiveApi.Authorization; using LCU.API.Internal.Operations.IncentiveApi.Configuration; using LCU.API.Internal.Operations.IncentiveApiLib.Services; using LCU.Library.Internal.IncentiveApiModels.Common; using LCU.API.Internal.Operations.IncentiveApiLib.Persistence; using LCU.Library.Internal.IncentiveApiModels.Requests; namespace LCU.API.Internal.Operations.IncentiveApi.Controllers; [FeatureGate(FeatureFlags.EnableControllers)] [ApiController] [Route("api/[controller]")] [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public class PayoutController : ControllerBase { private readonly IPayoutService _payoutService; private readonly IUserService _userService; private readonly IActivityService _activityService; private readonly ICurrentUserService _currentUser; public PayoutController(IPayoutService payoutService, IUserService userService, ICurrentUserService currentUser, IActivityService activityService) { _payoutService = payoutService; _userService = userService; _currentUser = currentUser; _activityService = activityService; } [HttpGet("{employeeId}", Name = "GetEmployeePayout")] [RequirePermission("VIEW_DASHBOARD")] public async Task GetEmployeePayout(string employeeId, [FromQuery] int year, [FromQuery] int month) { try { var searchYear = year > 0 ? year : DateTime.Now.Year; var searchMonth = month > 0 ? month : DateTime.Now.Month; var employee = await _userService.GetUserByEmployeeIdAsync(employeeId); if (employee == null) { return NoContent(); } var result = await _payoutService.RetrieveCombinedDepartmentPayouts(employee, new MonthRange(searchYear, searchMonth)); return Ok( result); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name}"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpGet("me", Name = "GetCurrentUserPayout")] [RequirePermission("VIEW_DASHBOARD")] public async Task GetCurrentUserPayout( [FromQuery] int year, [FromQuery] int month) { try { var searchYear = year > 0 ? year : DateTime.Now.Year; var searchMonth = month > 0 ? month : DateTime.Now.Month; var appUser = _currentUser.GetCurrentUser(); if (appUser?.Email == null) { return Unauthorized("No AD User Found."); } var employee = await _userService.GetUserByAdEmailAsync(appUser.Email); if (employee == null) { return NoContent(); } var result = await _payoutService.CreateCombinedDepartmentPayouts(employee, new MonthRange(searchYear, searchMonth)); return Ok(result); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name}"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpGet("export", Name = "ExportMonthlyPayout")] [RequirePermission("GENERATE_PAYOUT_FILES")] public async Task ExportMonthlyPayout([FromQuery] int year, [FromQuery] int month) { try { var searchYear = year > 0 ? year : DateTime.Today.Year; var searchMonth = month > 0 ? month : DateTime.Today.Month; var appUser = _currentUser.GetCurrentUser(); if (appUser?.Email == null) { return Unauthorized("No AD User Found."); } var employee = await _userService.GetUserByAdEmailAsync(appUser.Email); if (employee == null) { return NoContent(); } var period = new DateTime(searchYear, searchMonth, 1); var payouts = await _payoutService.RetrieveMonthlyIncentivePayouts(period); return Ok(payouts); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name}"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpGet("runMonthly", Name = "RunMonthlyPayout")] [RequirePermission("GENERATE_PAYOUT_FILES")] public async Task RunMonthlyPayout([FromQuery] int monthsBack = 0) { try { var appUser = _currentUser.GetCurrentUser(); if (appUser?.Email == null) { return Unauthorized("No AD User Found."); } var employee = await _userService.GetUserByAdEmailAsync(appUser.Email); if (employee == null) { return NoContent(); } var request = new PayoutIncentiveRequest() { MonthsBack = monthsBack }; var payouts = await _payoutService.CreateMonthlyIncentivePayouts(request); return Ok(payouts); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name}"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpGet("runAccounting", Name = "RunMonthlyAccounting")] [RequirePermission("GENERATE_PAYOUT_FILES")] public async Task RunMonthlyAccounting([FromQuery] int monthsBack = 0) { try { var appUser = _currentUser.GetCurrentUser(); if (appUser?.Email == null) { return Unauthorized("No AD User Found."); } var employee = await _userService.GetUserByAdEmailAsync(appUser.Email); if (employee == null) { return NoContent(); } var period = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1); period = period.AddMonths( -1 * monthsBack); var range = new MonthRange(period); var payouts = await _payoutService.CreateMonthlyAccountingPayouts(range); return Ok(payouts); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name}"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpGet("getAccounting", Name = "GetMonthlyAccounting")] [RequirePermission("GENERATE_PAYOUT_FILES")] public async Task GetMonthlyAccounting([FromQuery] DateTime period) { try { var appUser = _currentUser.GetCurrentUser(); if (appUser?.Email == null) { return Unauthorized("No AD User Found."); } var employee = await _userService.GetUserByAdEmailAsync(appUser.Email); if (employee == null) { return NoContent(); } var range = new MonthRange(period); var payouts = await _payoutService.RetrieveMonthlyAccountingPayouts(range); return Ok(payouts); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name}"); return StatusCode(StatusCodes.Status500InternalServerError); } } [HttpGet("runMonthlySingle", Name = "RunMonthlyPayoutSingle")] [RequirePermission("GENERATE_PAYOUT_FILES")] public async Task RunMonthlyPayoutSingle([FromQuery] string employeeId, [FromQuery] int monthsBack = 0) { try { var appUser = _currentUser.GetCurrentUser(); if (appUser?.Email == null) { return Unauthorized("No AD User Found."); } var employee = await _userService.GetUserByEmployeeIdAsync(employeeId); if (employee == null) { return NoContent(); } var period = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1); period = period.AddMonths( -1 * monthsBack); var range = new MonthRange(period); var payouts = await _payoutService.CreateCombinedDepartmentPayouts(employee, range); return Ok(payouts); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name}"); return StatusCode(StatusCodes.Status500InternalServerError); } } }