using FrameworkDAL.CustomCode.GBPeriod; using FrameworkDAL.DTO.GBPeriod; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Framework; using GB5Shared.Resource.Response; using GB5Shared.Validation; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; namespace FrameworkBLL.GBPeriod { public class GBPeriodBLL : IGBPeriodBLL { private readonly IGBPeriodDAL _gbPeriodDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public GBPeriodBLL(IGBPeriodDAL gbPeriodDAL, AutoNumber AutoNumber, IValidation Validation) { _gbPeriodDAL = gbPeriodDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetGBPeriod(int gbPeriodId, LoginDTO loginDTO) { try { return await _gbPeriodDAL.GetGBPeriod(gbPeriodId, loginDTO); } catch (Exception) { throw; } } /// /// Inserts a new GBPeriod record or updates an existing one. /// Applies business rules such as auto-number assignment for new records. /// /// The GBPeriod data to save or update. /// The current user's login details. /// A message string indicating success and the GBPeriod ID. public async Task SaveGBPeriod(GBPeriodDTO GBPeriodDTO, LoginDTO LoginDTO) { if (GBPeriodDTO == null) throw new ArgumentNullException(nameof(GBPeriodDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(GBPeriodDTO.GBPeriodCode, nameof(GBPeriodDTO.GBPeriodCode)); await _Validation.NotEmpty(GBPeriodDTO.GBPeriodName, nameof(GBPeriodDTO.GBPeriodName)); GBPeriodDTO.GBPeriodModifiedById = LoginDTO.UserId; GBPeriodDTO.GBPeriodModifiedOn = DateTime.UtcNow; int initialId = GBPeriodDTO.GBPeriodId; int count = 1 + initialId; if (initialId == 0) { AutoNumberDTO AutoNumberDTO = await _AutoNumber.GetNumberAsync(count, "GBPeriod", LoginDTO); GBPeriodDTO.GBPeriodId = AutoNumberDTO.StartNumber; GBPeriodDTO.GBPeriodCreatedById = LoginDTO.UserId; GBPeriodDTO.GBPeriodCreatedOn = DateTime.UtcNow; await _gbPeriodDAL.SaveGBPeriod(GBPeriodDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {GBPeriodDTO.GBPeriodId}"; } else { await _gbPeriodDAL.UpdateGBPeriod(GBPeriodDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {GBPeriodDTO.GBPeriodId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && GBPeriodDTO.GBPeriodId > 0) await _AutoNumber.RollbackAutoNumber("GBPeriod", GBPeriodDTO.GBPeriodId, LoginDTO); throw; } } /// /// Deletes a GBPeriod record by its unique identifier. /// /// The ID of the GBPeriod to delete. /// The current user's login details. /// A message string indicating success or failure of the delete operation. public async Task DeleteGBPeriod(int GBPeriodId, LoginDTO LoginDTO) { try { return await _gbPeriodDAL.DeleteGBPeriod(GBPeriodId, LoginDTO); } catch (Exception) { throw; } } /// /// Retrieves a paginated list of GBPeriod records based on specified filter criteria. /// Can optionally return the total count of records instead of the list. /// /// The starting index of records to fetch (for pagination). /// The maximum number of records to fetch. /// Filter criteria for selecting GBPeriod records. /// The current user's login details. /// If true, returns the total count of matching records instead of the list. /// A JSON string representing either the list of GBPeriod records or the total count. public async Task GetSelectListGBPeriod(int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO loginDTO, bool isCount = false) { try { return await _gbPeriodDAL.GetSelectListGBPeriod(firstNumber, maxResult, criteriaDTO, loginDTO, isCount); } catch (Exception) { throw; } } /// /// Retrieves a paginated list of GBPeriodDetail records (sub-periods) across all GBPeriods. /// Can optionally return the total count of records instead of the list. /// /// The starting index of records to fetch (for pagination). /// The maximum number of records to fetch. /// Filter criteria for selecting GBPeriodDetail records. /// The current user's login details. /// If true, returns the total count of matching records instead of the list. /// A JSON string representing either the list of GBPeriodDetail records or the total count. public async Task GetSelectListGBPeriodDetail(int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO loginDTO, bool isCount = false) { try { return await _gbPeriodDAL.GetSelectListGBPeriodDetail(firstNumber, maxResult, criteriaDTO, loginDTO, isCount); } catch (Exception) { throw; } } /// /// Retrieves the full GBPeriod/GBPeriodDetail picklist (no paging) filtered by the supplied criteria. /// Mirrors the GB4 Gb4PeriodPicklist behavior used by legacy period-selection pickers. /// /// Filter criteria (e.g. PeriodType, SubPeriodName, PeiodFromDate, PeiodToDate). /// The current user's login details. /// A JSON string representing the list of matching GBPeriodDetail picklist records. public async Task Gb4PeriodPicklist(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _gbPeriodDAL.Gb4PeriodPicklist(criteriaDTO, loginDTO); } catch (Exception) { throw; } } } }