private void SetDebitOrCreditOnBalance(IAccountBalance balance)
        {
            var     openingbalance     = GetMostRecentPersistedBalance();
            decimal openingDebitValue  = openingbalance.Debit ?? new decimal(0);
            decimal openingCreditValue = openingbalance.Credit ?? new decimal(0);
            var     trans         = AllTransactionsForAccountFrom(openingbalance.Date);
            var     newDebits     = trans.Where(x => x.DebitAccount.Id == Id).Select(x => x.Amount).DefaultIfEmpty(0).Sum(); //Cant' use Sum(x => x.Amount) if there are no Transactions
            var     newCredits    = trans.Where(x => x.CreditAccount.Id == Id).Select(x => x.Amount).DefaultIfEmpty(0).Sum();
            var     currentDebit  = openingDebitValue + newDebits;
            var     currentCredit = openingCreditValue + newCredits;

            if (currentCredit == currentDebit)
            {
                switch (Type)
                {
                case AccountTypes.Income:
                    balance.Credit = new decimal(0);
                    balance.Debit  = null;
                    break;

                case AccountTypes.Expense:
                    balance.Credit = null;
                    balance.Debit  = new decimal(0);
                    break;

                case AccountTypes.Asset:
                    balance.Credit = null;
                    balance.Debit  = new decimal(0);
                    break;

                case AccountTypes.Liability:
                    balance.Credit = new decimal(0);
                    balance.Debit  = null;
                    break;

                default:
                    throw new DomainException("Unrecognised Account Type");
                }
            }
            else if (currentCredit > currentDebit)
            {
                balance.Credit = currentCredit - currentDebit;
            }
            else
            {
                balance.Debit = currentDebit - currentCredit;
            }
        }
Beispiel #2
0
 public AccountBalanceController(IAccountBalance accountBalance)
 {
     _accontBalance = accountBalance;
 }
Beispiel #3
0
 public bool CanWithdrawMoney(IAccountBalance balance, decimal moneyAmount)
 {
     return(IsCurrentBalanceAmountAboveMinimum(balance.Amount) &&
            HasNecessaryBalanceAmountToWithdrawMoney(balance.Amount, moneyAmount));
 }
 public void Setup()
 {
     _target = AccountBalance.CreateWithZeroAmount();
 }