Beispiel #1
0
 def test_balance_incomplete_postings__fill2(self):
     entry, errors = self.get_incomplete_entry("""
       option "booking_algorithm" "SIMPLE"
       2013-02-23 * "Something"
         Liabilities:CreditCard     -50 USD
         Liabilities:CreditCard     -50 CAD
         Expenses:Restaurant
     """)
     self.assertFalse(errors)
     self.assertEqual(4, len(entry.postings))
     self.assertEqual(entry.postings[2].account, 'Expenses:Restaurant')
     self.assertEqual(entry.postings[3].account, 'Expenses:Restaurant')
     self.assertEqual(position.get_position(entry.postings[2]),
                      position.from_string('50 USD'))
     self.assertEqual(position.get_position(entry.postings[3]),
                      position.from_string('50 CAD'))
Beispiel #2
0
 def test_balance_incomplete_postings__cost(self):
     entry, errors = self.get_incomplete_entry("""
       option "booking_algorithm" "SIMPLE"
       2013-02-23 * "Something"
         Assets:Invest     10 MSFT {43.23 USD}
         Assets:Cash
     """)
     self.assertFalse(errors)
     self.assertEqual(2, len(entry.postings))
     self.assertEqual(entry.postings[1].account, 'Assets:Cash')
     self.assertEqual(position.get_position(entry.postings[1]),
                      position.from_string('-432.30 USD'))
 def get_inventory(self, account, date):
     inventory = Inventory()
     for entry in self.entries:
         if date is not None and entry.date > date:
             break
         if not isinstance(entry, Transaction):
             continue
         for posting in entry.postings:
             if posting.account != account:
                 continue
             inventory.add_position(get_position(posting))
     return inventory
Beispiel #4
0
    def test_balance_incomplete_postings__insert_rounding(self):
        entry, errors = self.get_incomplete_entry("""
          option "booking_algorithm" "SIMPLE"
          option "account_rounding" "RoundingError"

          2013-02-23 * "Something"
            Assets:Invest     1.245 RGAGX {43.23 USD}
            Assets:Cash      -53.82 USD
        """)
        self.assertFalse(errors)
        self.assertEqual(3, len(entry.postings))
        self.assertEqual(entry.postings[2].account, 'Equity:RoundingError')
        self.assertEqual(position.get_position(entry.postings[2]),
                         position.from_string('-0.00135 USD'))
Beispiel #5
0
def main():
    date_parser = lambda s: dateutil.parser.parse(s).date()
    parser = argparse.ArgumentParser(description=__doc__.strip())

    parser.add_argument('-s',
                        '--start-date',
                        action='store',
                        type=date_parser,
                        help="Start date to timespan to filter")
    parser.add_argument('-e',
                        '--end-date',
                        action='store',
                        type=date_parser,
                        help="End date of timespan to filter")

    parser.add_argument('filename', help='Beancount ledger file')
    args = parser.parse_args()

    entries, _, options_map = loader.load_file(args.filename)

    if args.start_date:
        entries = (entry for entry in entries if entry.date >= args.start_date)
    if args.end_date:
        entries = (entry for entry in entries if entry.date < args.end_date)

    balances = collections.defaultdict(inventory.Inventory)
    rows = [('open_date', 'open_posting', 'close_date', 'close_posting',
             'close_price')]
    for entry in data.filter_txns(entries):
        for posting in entry.postings:
            account_balance = balances[posting.account]
            closing = position.get_position(posting)
            price = posting.price
            opening, booking = account_balance.add_position(posting)
            if posting.cost is not None and booking == inventory.MatchResult.REDUCED:
                rows.append(
                    (opening.cost.date, opening, entry.date, closing, price))

    table = petl.wrap(rows)
    print(table.lookallstr())