summaryrefslogtreecommitdiff
path: root/autoxact.cc
blob: 4de8122dab813b3d414b54c35b433051cabd220a (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#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

#ifdef USE_BOOST_PYTHON

#include <boost/python.hpp>
#include <Python.h>

using namespace boost::python;
using namespace ledger;

void export_autoxact() {
  def("handle_auto_xacts", handle_auto_xacts);
}

#endif // USE_BOOST_PYTHON