Exemple #1
0
        public bool Post(Transaction transaction)
        {
            var relatedAccounts = transaction
                .RelatedAccounts()
                .Distinct()
                .ToArray();

            foreach (var account in relatedAccounts)
            {
                var budgets = GetBudgetsForAccount(account.Id).ToArray();
                if (budgets.Length == 0)
                    continue;

                ProcessBudgetPayout(transaction, account, budgets);
            }

            if (transaction.Check())
            {
                transaction.Id = TransactionId.From(transaction);
                if (_repository.Post(transaction))
                {
                    foreach (var debitAmount in transaction.Debit)
                        debitAmount.Post();

                    foreach (var creditAmount in transaction.Credit)
                        creditAmount.Post();
                }

                return true;
            }

            return false;
        }
Exemple #2
0
        private void ProcessBudgetPayout(Transaction transaction, Account account, IEnumerable<Budget> budgets)
        {
            Action<Account> setBudgetAccount;
            Account debit = null, credit = null;

            if (transaction.IsDebitAccount(account))
            {
                setBudgetAccount = a => { credit = a; };
                debit = this["Budget"];
            }
            else
            {
                setBudgetAccount = a => { debit = a; };
                credit = this["Budget"];
            }

            foreach (var budget in budgets)
            {
                setBudgetAccount(this[budget.BudgetAccount.Id]);
                transaction.Debit.Add(new Amount(debit, EntryType.Debit, transaction.Amount));
                transaction.Credit.Add(new Amount(credit, EntryType.Credit, transaction.Amount));
            }
        }
Exemple #3
0
        public static string From(Transaction transaction)
        {
            var date = transaction.Date;
            var amount = transaction.Amount;
            var reference = transaction.Reference;
            var description = transaction.Description;

            return From(date, amount, reference, description);
        }