summaryrefslogtreecommitdiff
path: root/autoxact.cc
blob: cc07ad2f3395e032adbbc5a52ddb820d245ffbf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "autoxact.h"

namespace ledger {

void automated_transaction_t::extend_entry(entry_t * entry)
{
  transactions_deque initial_xacts(entry->transactions.begin(),
				   entry->transactions.end());

  for (transactions_deque::iterator i = initial_xacts.begin();
       i != initial_xacts.end();
       i++)
    if (predicate(**i))
      for (transactions_deque::iterator t = transactions.begin();
	   t != transactions.end();
	   t++) {
	amount_t amt;
	if ((*t)->amount.commodity().symbol.empty())
	  amt = (*i)->amount * (*t)->amount;
	else
	  amt = (*t)->amount;

	transaction_t * xact
	  = new transaction_t((*t)->account, amt,
			      (*t)->flags | TRANSACTION_AUTO);
	entry->add_transaction(xact);
      }
}

automated_transactions_t * current_auto_xacts = NULL;

bool handle_auto_xacts(entry_t * entry)
{
  if (current_auto_xacts &&
      ! current_auto_xacts->automated_transactions.empty())
    current_auto_xacts->extend_entry(entry);

  return true;
}

} // namespace ledger