/// <summary> /// Does assets transfer from one account to another account with given TransactionType /// </summary> /// <param name="fromAccount"></param> /// <param name="toAccount"></param> /// <param name="amount"></param> /// <param name="transactionTypeEnum"></param> private void TransferAssets( AccountDomainEntity fromAccount, AccountDomainEntity toAccount, decimal amount, TransactionTypeEnum transactionTypeEnum) { AccountTransactionDomainEntity transaction = null; bool isWithdrawOk = false; bool isDepositOk = false; try { if (fromAccount.AccountType.AccountTypeKey != toAccount.AccountType.AccountTypeKey) { throw new AccountManagementException.DifferentAccountTypesCouldNotTransferAssetsToEachOther(string .Format("FromAccountType: {0} - ToAccountType: {1}", fromAccount.AccountType.AccountTypeName, toAccount.AccountType.AccountTypeName)); } transaction = coreContext.New <AccountTransactionDomainEntity>() .With(fromAccount, toAccount, amount, transactionTypeEnum, TransactionStatusEnum.InProgress, null); transaction.Insert(); isWithdrawOk = fromAccount.Withdraw(amount); isDepositOk = toAccount.Deposit(amount); transaction.SetStatus(TransactionStatusEnum.Succeeded); transaction.Save(); var transactionDetailIn = transaction.CreateTransactionDetail(TransactionDirection.In); transactionDetailIn.Insert(); var transactionDetailOut = transaction.CreateTransactionDetail(TransactionDirection.Out); transactionDetailOut.Insert(); } catch (Exception ex) { if (transaction != null) { if (isWithdrawOk && isDepositOk == false) { fromAccount.Deposit(amount); transaction.SetStatus(TransactionStatusEnum.Failed); transaction.Save(); } } throw ex; } }
internal AccountDomainEntity WithdrawMoneyFromOwn(int accountId, decimal amount) { AccountTransactionDomainEntity transaction = null; AccountDomainEntity account = null; bool isWithdrawOK = false; try { account = GetAccountById(accountId); transaction = coreContext .New <AccountTransactionDomainEntity>() .With(account, null, amount, TransactionTypeEnum.AccountItself, TransactionStatusEnum.InProgress, account); transaction.Insert(); isWithdrawOK = account.Withdraw(amount); transaction.SetStatus(TransactionStatusEnum.Succeeded); transaction.Save(); var transactionDetail = transaction.CreateTransactionDetail(TransactionDirection.Out); transactionDetail.Insert(); } catch (Exception ex) { if (transaction != null && isWithdrawOK == false) { transaction.SetStatus(TransactionStatusEnum.Failed); transaction.Save(); } throw ex; } return(account); }