Example #1
0
        public async Task UpdateBalanceMetadataForInputAccounts(ChartOfAccountNew subLvlAccount)
        {
            var accounts = await _dbContext.ChartOfAccountNew.Where(x => x.ParentID == subLvlAccount.ChartOfAccountNewId).ToListAsync();

            var accType = await _dbContext.AccountType.FirstOrDefaultAsync(x => x.AccountTypeId == subLvlAccount.AccountTypeId);

            if (accType != null)
            {
                var accHeadType = await _dbContext.AccountHeadType.FirstOrDefaultAsync(x => x.AccountHeadTypeId == accType.AccountHeadTypeId);

                subLvlAccount.IsCreditBalancetype = accHeadType.IsCreditBalancetype;
            }

            _dbContext.ChartOfAccountNew.Update(subLvlAccount);
            await _dbContext.SaveChangesAsync();

            foreach (var account in accounts)
            {
                account.IsCreditBalancetype = subLvlAccount.IsCreditBalancetype;
                account.AccountTypeId       = subLvlAccount.AccountTypeId;
                account.AccountFilterTypeId = subLvlAccount.AccountFilterTypeId;
            }
            _dbContext.ChartOfAccountNew.UpdateRange(accounts);
            await _dbContext.SaveChangesAsync();
        }
Example #2
0
        public async Task <ApiResponse> Handle(EditChartOfAccountCommand request, CancellationToken cancellationToken)
        {
            ApiResponse response = new ApiResponse();

            try
            {
                if (request != null)
                {
                    ChartOfAccountNew accountDetail = await _dbContext.ChartOfAccountNew.FirstOrDefaultAsync(x => x.ChartOfAccountNewId == request.ChartOfAccountNewId);

                    if (accountDetail != null)
                    {
                        accountDetail.AccountName         = request.AccountName?.Trim();
                        accountDetail.AccountTypeId       = request.AccountTypeId;
                        accountDetail.AccountFilterTypeId = request.AccountFilterTypeId;

                        accountDetail.ModifiedDate = request.ModifiedDate;
                        accountDetail.IsDeleted    = false;

                        await _dbContext.SaveChangesAsync();

                        if (accountDetail.AccountLevelId == (int)AccountLevels.SubLevel)
                        {
                            bool inputLevelAccountExists = await _dbContext.ChartOfAccountNew.AnyAsync(x => x.IsDeleted == false && x.ParentID == accountDetail.ChartOfAccountNewId);

                            //Update only if input level account exists on sub level account
                            if (inputLevelAccountExists)
                            {
                                // Updated all input-level accounts' account types and balance types if true
                                await UpdateBalanceMetadataForInputAccounts(accountDetail);
                            }
                        }

                        response.StatusCode = StaticResource.successStatusCode;
                        response.Message    = StaticResource.SuccessText;
                    }
                    else
                    {
                        response.StatusCode = StaticResource.failStatusCode;
                        response.Message    = StaticResource.NoDataFound;
                    }
                }
            }
            catch (Exception ex)
            {
                response.StatusCode = StaticResource.failStatusCode;
                response.Message    = ex.Message;
            }
            return(response);
        }
        public async Task <ApiResponse> Handle(DeleteChartOfAccountCommand request, CancellationToken cancellationToken)
        {
            ApiResponse response = new ApiResponse();

            try
            {
                ChartOfAccountNew accountDetail = await _dbContext.ChartOfAccountNew.FirstOrDefaultAsync(x => x.ChartOfAccountNewId == request.AccountId);

                if (accountDetail != null)
                {
                    if (accountDetail.AccountLevelId == (int)AccountLevels.InputLevel ? !await CheckTransactionExistOrNot(request.AccountId) : !await CheckChildAccountExistOrNot(request.AccountId))
                    {
                        accountDetail.IsDeleted    = true;
                        accountDetail.ModifiedById = request.ModifiedById;
                        accountDetail.ModifiedDate = DateTime.UtcNow;

                        await _dbContext.SaveChangesAsync();

                        response.StatusCode = StaticResource.successStatusCode;
                        response.Message    = StaticResource.SuccessText;
                    }
                    else
                    {
                        response.StatusCode = StaticResource.failStatusCode;
                        response.Message    = accountDetail.AccountLevelId == (int)AccountLevels.InputLevel ? StaticResource.DeleteAllTransactions : StaticResource.DeleteAllChildAccount;
                    }
                }
                else
                {
                    response.StatusCode = StaticResource.failStatusCode;
                    response.Message    = StaticResource.AccountNotFound;
                }
            }
            catch (Exception ex)
            {
                response.StatusCode = StaticResource.failStatusCode;
                response.Message    = StaticResource.SomethingWrong + ex.Message;
            }
            return(response);
        }
        public async Task <ApiResponse> Handle(AddChartOfAccountCommand request, CancellationToken cancellationToken)
        {
            ApiResponse response = new ApiResponse();

            try
            {
                if (request != null)
                {
                    if (request.AccountName != null)
                    {
                        request.AccountName = request.AccountName.Trim();
                    }
                }

                //Main Level
                if (request.AccountLevelId == (int)AccountLevels.MainLevel)
                {
                    int levelcount = await _dbContext.ChartOfAccountNew.CountAsync(x => x.AccountLevelId == (int)AccountLevels.MainLevel && x.IsDeleted == false);

                    if (levelcount < (int)AccountLevelLimits.MainLevel)
                    {
                        bool isCOACodeExists = await _dbContext.ChartOfAccountNew.AnyAsync(x => x.ChartOfAccountNewCode == request.ChartOfAccountNewCode && x.IsDeleted == false);

                        if (isCOACodeExists)
                        {
                            throw new Exception("Account Code Already Exists!!!");
                        }

                        ChartOfAccountNew obj = new ChartOfAccountNew();

                        obj.AccountLevelId        = (int)AccountLevels.MainLevel;
                        obj.AccountHeadTypeId     = request.AccountHeadTypeId;
                        obj.ParentID              = -1;
                        obj.AccountName           = request.AccountName;
                        obj.ChartOfAccountNewCode = request.ChartOfAccountNewCode;
                        obj.CreatedById           = request.CreatedById;
                        obj.CreatedDate           = request.CreatedDate;
                        obj.IsDeleted             = false;

                        await _dbContext.ChartOfAccountNew.AddAsync(obj);

                        await _dbContext.SaveChangesAsync();

                        obj.ParentID = obj.ChartOfAccountNewId;

                        await _dbContext.SaveChangesAsync();

                        response.data.ChartOfAccountNewDetail = obj;
                        response.StatusCode = StaticResource.successStatusCode;
                        response.Message    = StaticResource.SuccessText;
                    }
                    else
                    {
                        response.StatusCode = StaticResource.failStatusCode;
                        response.Message    = StaticResource.ExceedLevelCount;
                    }
                }

                //Control Level
                else if (request.AccountLevelId == (int)AccountLevels.ControlLevel)
                {
                    int levelcount = await _dbContext.ChartOfAccountNew.CountAsync(x => x.AccountLevelId == (int)AccountLevels.ControlLevel && x.ParentID == request.ParentID);

                    if (levelcount < (int)AccountLevelLimits.ControlLevel)
                    {
                        ChartOfAccountNew obj = new ChartOfAccountNew();

                        bool isCOACodeExists = await _dbContext.ChartOfAccountNew.AnyAsync(x => x.ChartOfAccountNewCode == request.ChartOfAccountNewCode && x.IsDeleted == false);

                        if (isCOACodeExists)
                        {
                            throw new Exception("Account Code Already Exists!!!");
                        }

                        ChartOfAccountNew parentPresent = await _dbContext.ChartOfAccountNew.FirstOrDefaultAsync(x => x.ChartOfAccountNewId == request.ParentID);

                        if (parentPresent != null)
                        {
                            obj.AccountLevelId        = (int)AccountLevels.ControlLevel;
                            obj.AccountHeadTypeId     = request.AccountHeadTypeId;
                            obj.ParentID              = request.ParentID;
                            obj.ChartOfAccountNewCode = request.ChartOfAccountNewCode;
                            obj.AccountName           = request.AccountName;
                            obj.CreatedById           = request.CreatedById;
                            obj.CreatedDate           = request.CreatedDate;
                            obj.IsDeleted             = false;

                            await _dbContext.ChartOfAccountNew.AddAsync(obj);

                            await _dbContext.SaveChangesAsync();

                            response.data.ChartOfAccountNewDetail = obj;
                            response.StatusCode = StaticResource.successStatusCode;
                            response.Message    = StaticResource.SuccessText;
                        }
                        else
                        {
                            response.StatusCode = StaticResource.failStatusCode;
                            response.Message    = StaticResource.ParentIdNotPresent;
                        }
                    }
                    else
                    {
                        response.StatusCode = StaticResource.failStatusCode;
                        response.Message    = StaticResource.ExceedLevelCount;
                    }
                }

                //Sub Level
                else if (request.AccountLevelId == (int)AccountLevels.SubLevel)
                {
                    int levelcount = await _dbContext.ChartOfAccountNew.CountAsync(x => x.AccountLevelId == (int)AccountLevels.SubLevel && x.ParentID == request.ParentID);

                    if (levelcount < (int)AccountLevelLimits.SubLevel)
                    {
                        ChartOfAccountNew obj = new ChartOfAccountNew();

                        bool isCOACodeExists = await _dbContext.ChartOfAccountNew.AnyAsync(x => x.ChartOfAccountNewCode == request.ChartOfAccountNewCode && x.IsDeleted == false);

                        if (isCOACodeExists)
                        {
                            throw new Exception("Account Code Already Exists!!!");
                        }

                        ChartOfAccountNew parentPresent = await _dbContext.ChartOfAccountNew.FirstOrDefaultAsync(x => x.ChartOfAccountNewId == request.ParentID);

                        if (parentPresent != null)
                        {
                            obj.AccountLevelId        = (int)AccountLevels.SubLevel;
                            obj.AccountHeadTypeId     = request.AccountHeadTypeId;
                            obj.ParentID              = request.ParentID;
                            obj.AccountName           = request.AccountName;
                            obj.ChartOfAccountNewCode = request.ChartOfAccountNewCode;

                            obj.AccountFilterTypeId = request.AccountFilterTypeId != null ? request.AccountFilterTypeId : null;  //dropdown

                            if (request.AccountTypeId != null)
                            {
                                obj.AccountTypeId = request.AccountTypeId;
                                // Get balance type.
                                obj.IsCreditBalancetype = await GetAccountBalanceTypeByAccountType((int)obj.AccountTypeId);
                            }
                            else
                            {
                                obj.AccountTypeId       = null;
                                obj.IsCreditBalancetype = null;
                            }

                            obj.CreatedById = request.CreatedById;
                            obj.CreatedDate = request.CreatedDate;
                            obj.IsDeleted   = false;

                            await _dbContext.ChartOfAccountNew.AddAsync(obj);

                            await _dbContext.SaveChangesAsync();

                            response.data.ChartOfAccountNewDetail = obj;
                            response.StatusCode = StaticResource.successStatusCode;
                            response.Message    = StaticResource.SuccessText;
                        }
                        else
                        {
                            response.StatusCode = StaticResource.failStatusCode;
                            response.Message    = StaticResource.ParentIdNotPresent;
                        }
                    }
                    else
                    {
                        response.StatusCode = StaticResource.failStatusCode;
                        response.Message    = StaticResource.ExceedLevelCount;
                    }
                }

                //Input Level
                else if (request.AccountLevelId == (int)AccountLevels.InputLevel)
                {
                    int levelcount = await _dbContext.ChartOfAccountNew.CountAsync(x => x.AccountLevelId == (int)AccountLevels.InputLevel && x.ParentID == request.ParentID);

                    if (levelcount < (int)AccountLevelLimits.InputLevel)
                    {
                        bool isCOACodeExists = await _dbContext.ChartOfAccountNew.AnyAsync(x => x.ChartOfAccountNewCode == request.ChartOfAccountNewCode && x.IsDeleted == false);

                        if (isCOACodeExists)
                        {
                            throw new Exception("Account Code Already Exists!!!");
                        }

                        ChartOfAccountNew obj = new ChartOfAccountNew();

                        ChartOfAccountNew parentPresent = await _dbContext.ChartOfAccountNew.FirstOrDefaultAsync(x => x.ChartOfAccountNewId == request.ParentID);

                        if (parentPresent != null)
                        {
                            obj.AccountLevelId        = (int)AccountLevels.InputLevel;
                            obj.AccountHeadTypeId     = request.AccountHeadTypeId;
                            obj.ParentID              = request.ParentID;
                            obj.ChartOfAccountNewCode = request.ChartOfAccountNewCode;
                            obj.AccountName           = request.AccountName;
                            obj.CreatedById           = request.CreatedById;
                            obj.CreatedDate           = request.CreatedDate;

                            // set account type and balance type
                            obj.AccountTypeId       = parentPresent.AccountTypeId;
                            obj.IsCreditBalancetype = parentPresent.IsCreditBalancetype;
                            obj.AccountFilterTypeId = parentPresent.AccountFilterTypeId;
                            obj.IsDeleted           = false;

                            await _dbContext.ChartOfAccountNew.AddAsync(obj);

                            await _dbContext.SaveChangesAsync();

                            response.data.ChartOfAccountNewDetail = obj;
                            response.StatusCode = StaticResource.successStatusCode;
                            response.Message    = StaticResource.SuccessText;
                        }
                        else
                        {
                            response.StatusCode = StaticResource.failStatusCode;
                            response.Message    = StaticResource.ParentIdNotPresent;
                        }
                    }
                    else
                    {
                        response.StatusCode = StaticResource.failStatusCode;
                        response.Message    = StaticResource.ExceedLevelCount;
                    }
                }
            }
            catch (Exception ex)
            {
                response.StatusCode = StaticResource.failStatusCode;
                response.Message    = StaticResource.SomethingWrong + ex.Message;
            }
            return(response);
        }