using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.Validation; using MMDAL.CustomCode.MaterialAccount; using MMDAL.DTO.MaterialAccount; using MMDAL.DTO.MeasureUnit; namespace MMBLL.MaterialAccount { public class MaterialAccountBLL : IMaterialAccountBLL { private readonly IMaterialAccountDAL _MaterialAccountDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public MaterialAccountBLL(IMaterialAccountDAL MaterialAccountDAL, AutoNumber AutoNumber, IValidation Validation) { _MaterialAccountDAL = MaterialAccountDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetMaterialAccount(int MaterialAccountId, LoginDTO LoginDTO) { try { return await _MaterialAccountDAL.GetMaterialAccount(MaterialAccountId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListMaterialAccount(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _MaterialAccountDAL.GetSelectListMaterialAccount(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task SaveMaterialAccount(MaterialAccountDTO MaterialAccountDTO, LoginDTO LoginDTO) { if (MaterialAccountDTO == null) throw new ArgumentNullException(nameof(MaterialAccountDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(MaterialAccountDTO.MaterialAccountCode, nameof(MaterialAccountDTO.MaterialAccountCode)); await _Validation.NotEmpty(MaterialAccountDTO.MaterialAccountName, nameof(MaterialAccountDTO.MaterialAccountName)); MaterialAccountDTO.MaterialAccountModifiedById = LoginDTO.UserId; MaterialAccountDTO.MaterialAccountModifiedOn = DateTime.UtcNow; bool isNew = MaterialAccountDTO.MaterialAccountId == 0; if (isNew) { int detailCount = MaterialAccountDTO.MaterialAccountDetailArray?.Count ?? 0; int totalRequired = 1 + detailCount; autoNumberDTO = await _AutoNumber.GetNumberAsync(totalRequired, "MATERIALACCOUNT", LoginDTO); int nextId = autoNumberDTO.StartNumber; MaterialAccountDTO.MaterialAccountId = nextId++; MaterialAccountDTO.MaterialAccountCreatedById = LoginDTO.UserId; MaterialAccountDTO.MaterialAccountCreatedOn = DateTime.UtcNow; if (MaterialAccountDTO.MaterialAccountDetailArray?.Count > 0) { foreach (var detail in MaterialAccountDTO.MaterialAccountDetailArray) { detail.MaterialAccountDetailId = nextId++; detail.MaterialAccountId = MaterialAccountDTO.MaterialAccountId; } } await _MaterialAccountDAL.SaveMaterialAccount(MaterialAccountDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessCode} {MaterialAccountDTO.MaterialAccountCode}"; } else { await _MaterialAccountDAL.UpdateMaterialAccount(MaterialAccountDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessCode} {MaterialAccountDTO.MaterialAccountCode}."; } } catch { if (autoNumberDTO != null && MaterialAccountDTO.MaterialAccountId > 0) { await _AutoNumber.RollbackAutoNumber("MATERIALACCOUNT", autoNumberDTO.StartNumber, LoginDTO); } throw; } } public async Task DeleteMaterialAccount(int MaterialAccountId, LoginDTO LoginDTO) { try { var detailsResult = await _MaterialAccountDAL.DeleteMaterialAccountDetail(MaterialAccountId, LoginDTO); if (detailsResult <= 0) return $"{ErrorResponse.DeleteNotFoundMessage}"; var parentResult = await _MaterialAccountDAL.DeleteMaterialAccount(MaterialAccountId, LoginDTO); if (parentResult <= 0) return $"{ErrorResponse.DeleteNotFoundMessage}"; return $"{SuccessResponse.DeleteSuccessMessage}"; } catch (Exception ex) { throw new Exception($"Error While Deleting MaterialAccount ID {MaterialAccountId}", ex); } } } }