Exemple #1
0
        public async Task <ResultDto> WithdrawCash(CreditCardDto creditCardDto, decimal amount)
        {
            var creditCard = await _unitOfWork.CreditCards.GetFirstWhereAsync(card => card.Number == creditCardDto.Number);

            if (creditCard.CreditAccount.Balance < amount)
            {
                return(new ResultDto
                {
                    Success = false,
                    Message = "Insufficient funds."
                });
            }

            var cashboxAccount = await _unitOfWork.Accounts.GetBankCashboxAccount();

            var transaction = await _transactionsService.CreateTransaction(creditCard.CreditAccount, cashboxAccount, amount);

            cashboxAccount.CreditValue += amount;

            await _unitOfWork.SaveAsync();

            return(new ResultDto {
                Success = true, Message = transaction.ID.ToString()
            });
        }
Exemple #2
0
        public async Task <ResultDto> CreateDepositAsync(CreateDepositDto createDepositDto)
        {
            if (createDepositDto.Amount <= 0)
            {
                return(new ResultDto {
                    Success = false, Message = "Amount should be greater than 0"
                });
            }

            using (var transaction = _unitOfWork.BeginTransaction())
            {
                try
                {
                    var deposit     = _mapper.Map <Deposit>(createDepositDto);
                    var depositPlan = await _unitOfWork.DepositPlans.FindAsync(createDepositDto.DepositPlanID);

                    deposit.StartDate = _dateService.Today;
                    deposit.EndDate   = _dateService.Today.AddDays(depositPlan.DayPeriod);

                    var(mainAccount, percentAccount) = await _accountsService.OpenDepositAccountsAsync(deposit.ClientID);

                    deposit.MainAccount    = mainAccount;
                    deposit.PercentAccount = percentAccount;

                    await _unitOfWork.SaveAsync();

                    decimal amount         = createDepositDto.Amount;
                    var     cashboxAccount = await _unitOfWork.Accounts.GetBankCashboxAccount();

                    var developmentFundAccount = await _unitOfWork.Accounts.GetBankDevelopmentFundAccount();

                    cashboxAccount.DebitValue += amount;
                    await _transactionsService.CreateTransaction(cashboxAccount, mainAccount, amount);

                    await _transactionsService.CreateTransaction(mainAccount, developmentFundAccount, amount);

                    _unitOfWork.Deposits.Add(deposit);
                    await _unitOfWork.SaveAsync();

                    await transaction.CommitAsync();

                    return(new ResultDto {
                        Success = true
                    });
                }
                catch (Exception ex)
                {
                    await transaction.RollbackAsync();

                    return(new ResultDto {
                        Success = false, Message = ex.Message
                    });
                }
            }
        }
Exemple #3
0
        public async Task <ResultDto> CreateCreditAsync(CreateCreditDto createCreditDto)
        {
            if (createCreditDto.Amount <= 0)
            {
                return(new ResultDto {
                    Success = false, Message = "Amount should be greater than 0"
                });
            }

            using (var transaction = _unitOfWork.BeginTransaction())
            {
                try
                {
                    var credit     = _mapper.Map <Credit>(createCreditDto);
                    var creditPlan = await _unitOfWork.CreditPlans.FindAsync(createCreditDto.CreditPlanID);

                    credit.StartDate = _dateService.Today;
                    credit.EndDate   = _dateService.Today.AddMonths(creditPlan.MonthPeriod);

                    var(mainAccount, percentAccount) = await _accountsService.OpenCreditAccountsAsync(credit.ClientID);

                    credit.MainAccount    = mainAccount;
                    credit.PercentAccount = percentAccount;

                    await _unitOfWork.SaveAsync();

                    decimal amount         = createCreditDto.Amount;
                    var     cashboxAccount = await _unitOfWork.Accounts.GetBankCashboxAccount();

                    var developmentFundAccount = await _unitOfWork.Accounts.GetBankDevelopmentFundAccount();

                    await _transactionsService.CreateTransaction(developmentFundAccount, mainAccount, amount);

                    _unitOfWork.Credits.Add(credit);

                    var creditCard = new CreditCard
                    {
                        CreditAccount = mainAccount,
                        EndDate       = credit.EndDate,
                        OwnerID       = credit.ClientID,

                        PIN    = $"{_randomizer.Next(0, 9999):0000}",
                        Number = await _creditCardsService.GenerateCreditCardNumber()
                    };

                    _unitOfWork.CreditCards.Add(creditCard);
                    await _unitOfWork.SaveAsync();

                    await transaction.CommitAsync();

                    return(new ResultDto {
                        Success = true
                    });
                }
                catch (Exception ex)
                {
                    await transaction.RollbackAsync();

                    return(new ResultDto {
                        Success = false, Message = ex.Message
                    });
                }
            }
        }