Beispiel #1
0
class Report(object):
    def __init__(self, ledger, rules, filter, entry_processor, template):
        self.ledger_processor = LedgerProcessor(ledger, rules)
        self.ledger_processor.add_listener(self)
        self.filter = filter
        self.entry_processor = entry_processor
        self.template = template

    def generate(self):
        self.ledger_processor.run()
        self.entry_processor.post_process()
        return self.result()

    def on_transaction(self, transaction, entries):
        for entry in entries:
            if self.filter(transaction, entry):
                self.entry_processor.process_entry(transaction, entry)

    def result(self):
        return self.entry_processor.result

    @classmethod
    def balance(cls, ledger, rules, filter, sorting):
        return cls(ledger, rules, filter, BalanceEntryProcessor(),
                template=BalanceTemplate())

    @classmethod
    def register(cls, ledger, rules, filter, sorting):
        return cls(ledger, rules, filter, RegisterEntryProcessor(sorting),
                template=RegisterTemplate())
Beispiel #2
0
    def testInclude(self):
        ledger = self.parser.parse_ledger(fixture_path("simple.dat"))
        processor = LedgerProcessor(ledger, self.rules)
        collector = TransactionCollector()
        processor.add_listener(collector)

        processor.run()
        processor.include("extra.dat")

        self.assertEqual(3, len(collector.transactions))
Beispiel #3
0
    def testAccountPrefix(self):
        ledger = self.parser.parse_ledger(fixture_path("simple.dat"))
        processor = LedgerProcessor(ledger, self.rules)
        collector = TransactionCollector()
        processor.add_listener(collector)

        processor.add_account_prefix("Business")
        processor.run()

        for transaction, entries in collector.transactions:
            for entry in entries:
                self.assertEqual("Business", entry.account.root().name)
Beispiel #4
0
    def testCompact(self):
        ledger = self.parser.parse_ledger(fixture_path("simple.dat"))
        processor = LedgerProcessor(ledger, self.rules)
        entry = ledger.transactions[1].entries[0]
        self.assertEqual(Value.parse("35 EUR"), entry.amount)
        # add entry
        entry2 = Entry(entry.account, Value.parse("-5 EUR"))
        ledger.transactions[1].entries.append(entry2)
        # rebalance transaction
        ledger.transactions[1].entries[1].amount += Value.parse("5 EUR")

        collector = TransactionCollector()
        processor.add_listener(collector)
        processor.run()

        self.assertEqual(2, len(collector.transactions))
        _, entries = collector.transactions[1]
        self.assertEqual(2, len(entries))

        self.assertEqual(Value.parse("30 EUR"), entries[0].amount)