public async Task <IActionResult> UpdateChartOfAccountAsync([FromBody] FinanceChartOfAccounts updatedChartOfAccount) { int currentUserInstituteId = await GetUserCurrentSelectedInstituteIdAsync(); ApplicationUser currentUser = await _userManager.FindByNameAsync(User.Identity.Name); return(Ok(await _financeManagementRepository.UpdateChartOfAccountAsync(currentUserInstituteId, updatedChartOfAccount, currentUser))); }
/// <summary> /// Method for updating an existing chart of account - RS /// </summary> /// <param name="currentUserInstituteId"></param> /// <param name="updatedChartOfAccount"></param> /// <returns></returns> public async Task <dynamic> UpdateChartOfAccountAsync(int currentUserInstituteId, FinanceChartOfAccounts updatedChartOfAccount, ApplicationUser currentUser) { List <FinanceChartOfAccounts> udpatedFinanceChartOfAccounts = new List <FinanceChartOfAccounts>(); List <FinanceChartOfAccounts> udpatedChildChartOfAccounts = new List <FinanceChartOfAccounts>(); FinanceChartOfAccounts existingChartOfAccount = await _imsDbContext.FinanceChartOfAccounts .FirstOrDefaultAsync(x => x.Id == updatedChartOfAccount.Id && x.InstituteId == currentUserInstituteId); if (existingChartOfAccount == null) { return(new { Message = "No Chart of Account exist with this id", HasError = true }); } else if (await _imsDbContext.FinanceChartOfAccounts.AnyAsync(x => x.InstituteId == currentUserInstituteId && x.Id != updatedChartOfAccount.Id && x.Code.ToLowerInvariant().Equals(updatedChartOfAccount.Code.ToLowerInvariant()))) { return(new { Message = "Chart of Account exist with this code", HasError = true }); } else if (await _imsDbContext.FinanceChartOfAccounts.AnyAsync(x => x.InstituteId == currentUserInstituteId && x.Id != updatedChartOfAccount.Id && x.Name.ToLowerInvariant().Equals(updatedChartOfAccount.Name.ToLowerInvariant()))) { return(new { Message = "Chart of Account exist with this name", HasError = true }); } else if (existingChartOfAccount.IsParent != updatedChartOfAccount.IsParent && existingChartOfAccount.Id == updatedChartOfAccount.ParentGroupId) { return(new { Message = "Can not set the chart of account as parent of itself", HasError = true }); } bool isAccountTypeChanged = (existingChartOfAccount.IsParent && existingChartOfAccount.AccountType != updatedChartOfAccount.AccountType); existingChartOfAccount.Name = updatedChartOfAccount.Name; existingChartOfAccount.AliasName = updatedChartOfAccount.AliasName; existingChartOfAccount.AccountType = updatedChartOfAccount.AccountType; existingChartOfAccount.Description = updatedChartOfAccount.Description; existingChartOfAccount.IsActive = updatedChartOfAccount.IsActive; existingChartOfAccount.IsParent = updatedChartOfAccount.IsParent; existingChartOfAccount.ParentGroupId = !existingChartOfAccount.IsParent ? updatedChartOfAccount.ParentGroupId : null; // Update child accounts' account type if (isAccountTypeChanged) { List <FinanceChartOfAccounts> childAccountsList = await _imsDbContext.FinanceChartOfAccounts.Where(x => x.ParentGroupId == existingChartOfAccount.Id).ToListAsync(); childAccountsList.ForEach(x => { x.AccountType = existingChartOfAccount.AccountType; }); udpatedChildChartOfAccounts = childAccountsList; } _imsDbContext.FinanceChartOfAccounts.Update(existingChartOfAccount); _imsDbContext.FinanceChartOfAccounts.UpdateRange(udpatedChildChartOfAccounts); await _imsDbContext.SaveChangesAsync(); return(new { Message = "Chart of Accounts updated successfully", HasError = false }); }
/// <summary> /// Method for adding a new chart of account - RS /// </summary> /// <param name="currentUserInstituteId"></param> /// <param name="newChartOfAccount"></param> /// <returns></returns> public async Task <dynamic> AddNewChartOfAccountAsync(int currentUserInstituteId, FinanceChartOfAccounts newChartOfAccount, ApplicationUser currentUser) { if (await _imsDbContext.FinanceChartOfAccounts.AnyAsync(x => x.InstituteId == currentUserInstituteId && x.Code.ToLowerInvariant().Equals(newChartOfAccount.Code.ToLowerInvariant()))) { return(new { Message = "Chart of Account exist with this code", HasError = true }); } else if (await _imsDbContext.FinanceChartOfAccounts.AnyAsync(x => x.InstituteId == currentUserInstituteId && x.Name.ToLowerInvariant().Equals(newChartOfAccount.Name.ToLowerInvariant()))) { return(new { Message = "Chart of Account exist with this name", HasError = true }); } newChartOfAccount.CreatedOn = DateTime.UtcNow; newChartOfAccount.CreatedBy = currentUser.Id; newChartOfAccount.InstituteId = currentUserInstituteId; _imsDbContext.FinanceChartOfAccounts.Add(newChartOfAccount); await _imsDbContext.SaveChangesAsync(); return(new { Message = "Chart of Accounts added successfully", HasError = false }); }