2020-01-14 00:06:14 +11:00
|
|
|
# ledger-pyreport
|
|
|
|
# Copyright © 2020 Lee Yingtong Li (RunasSudo)
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2020-03-20 21:43:05 +11:00
|
|
|
from .config import config
|
|
|
|
from .model import *
|
|
|
|
|
2020-01-14 00:06:14 +11:00
|
|
|
import csv
|
2020-02-15 15:50:33 +11:00
|
|
|
from datetime import datetime, timedelta
|
2020-01-14 00:06:14 +11:00
|
|
|
from decimal import Decimal
|
2020-02-15 11:47:10 +11:00
|
|
|
import re
|
2020-01-14 00:06:14 +11:00
|
|
|
import subprocess
|
|
|
|
|
2020-02-15 11:47:10 +11:00
|
|
|
# Helper commands to run Ledger
|
2020-01-14 00:06:14 +11:00
|
|
|
|
|
|
|
def run_ledger(*args):
|
2020-03-20 21:43:05 +11:00
|
|
|
ledger_args = ['ledger', '--args-only', '--file', config['ledger_file'], '--date-format', '%Y-%m-%d', '--unround'] + config['ledger_args'] + list(args)
|
2020-02-15 15:50:33 +11:00
|
|
|
proc = subprocess.Popen(ledger_args, encoding='utf-8', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2020-01-14 00:06:14 +11:00
|
|
|
stdout, stderr = proc.communicate()
|
|
|
|
|
|
|
|
if stderr:
|
|
|
|
raise Exception(stderr)
|
|
|
|
|
|
|
|
return stdout
|
|
|
|
|
|
|
|
def run_ledger_date(date, *args):
|
2020-03-29 21:46:55 +11:00
|
|
|
if date is None:
|
|
|
|
return run_ledger(*args)
|
2020-01-14 00:06:14 +11:00
|
|
|
return run_ledger('--end', (date + timedelta(days=1)).strftime('%Y-%m-%d'), *args)
|
|
|
|
|
2020-02-15 11:47:10 +11:00
|
|
|
# General financial logic
|
2020-01-14 00:06:14 +11:00
|
|
|
|
2020-02-15 11:47:10 +11:00
|
|
|
def financial_year(date):
|
|
|
|
pstart = date.replace(day=1, month=7)
|
|
|
|
if pstart > date:
|
|
|
|
pstart = pstart.replace(year=pstart.year - 1)
|
|
|
|
return pstart
|
|
|
|
|
|
|
|
# Ledger logic
|
|
|
|
|
2020-03-29 21:46:55 +11:00
|
|
|
csv.register_dialect('ledger', doublequote=False, escapechar='\\')
|
|
|
|
|
2020-02-15 11:47:10 +11:00
|
|
|
def parse_amount(amount):
|
2020-03-20 21:43:05 +11:00
|
|
|
if '{' in amount:
|
|
|
|
amount_str = amount[:amount.index('{')].strip()
|
|
|
|
price_str = amount[amount.index('{')+1:amount.index('}')].strip()
|
|
|
|
else:
|
|
|
|
amount_str = amount
|
|
|
|
price_str = None
|
2020-02-15 11:47:10 +11:00
|
|
|
|
2020-03-20 21:43:05 +11:00
|
|
|
if amount_str[0] in list('0123456789-'):
|
2020-04-04 04:15:32 +11:00
|
|
|
# Commodity follows number
|
2020-03-20 21:43:05 +11:00
|
|
|
bits = amount_str.split()
|
|
|
|
amount_num = Decimal(bits[0])
|
2020-04-04 04:15:32 +11:00
|
|
|
commodity = Commodity(bits[1].strip('"'), False)
|
2020-03-20 21:43:05 +11:00
|
|
|
else:
|
2020-04-04 04:15:32 +11:00
|
|
|
# Commodity precedes number
|
|
|
|
commodity = Commodity(amount_str[0], True)
|
2020-03-20 21:43:05 +11:00
|
|
|
amount_num = Decimal(amount_str[1:])
|
2020-02-15 11:47:10 +11:00
|
|
|
|
2020-03-20 21:43:05 +11:00
|
|
|
if price_str:
|
2020-04-04 04:15:32 +11:00
|
|
|
commodity.price = parse_amount(price_str)
|
2020-02-15 11:47:10 +11:00
|
|
|
|
2020-04-04 04:15:32 +11:00
|
|
|
return Amount(amount_num, commodity)
|
2020-01-14 00:06:14 +11:00
|
|
|
|
2020-03-20 21:43:05 +11:00
|
|
|
def get_pricedb():
|
|
|
|
output = run_ledger('prices', '--prices-format', '%(quoted(format_date(date))),%(quoted(display_account)),%(quoted(display_amount))\n')
|
2020-02-15 11:47:10 +11:00
|
|
|
|
2020-03-20 21:43:05 +11:00
|
|
|
prices = []
|
2020-02-15 11:47:10 +11:00
|
|
|
|
2020-03-29 21:46:55 +11:00
|
|
|
reader = csv.reader(output.splitlines(), dialect='ledger')
|
2020-04-04 04:15:32 +11:00
|
|
|
for date_str, commodity, price_str in reader:
|
|
|
|
prices.append((datetime.strptime(date_str, '%Y-%m-%d'), commodity.strip('"'), parse_amount(price_str)))
|
2020-02-15 11:47:10 +11:00
|
|
|
|
2020-03-20 21:43:05 +11:00
|
|
|
return prices
|
2020-01-14 00:06:14 +11:00
|
|
|
|
2020-03-20 21:43:05 +11:00
|
|
|
def raw_transactions_at_date(date):
|
|
|
|
ledger = Ledger(date)
|
|
|
|
ledger.prices = get_pricedb()
|
2020-01-14 00:06:14 +11:00
|
|
|
|
2020-04-15 11:11:46 +10:00
|
|
|
output = run_ledger_date(date, 'csv', '--csv-format', '%(quoted(parent.id)),%(quoted(format_date(date))),%(quoted(parent.code)),%(quoted(payee)),%(quoted(account)),%(quoted(display_amount)),%(quoted(comment)),%(quoted(state))\n')
|
2020-01-14 00:06:14 +11:00
|
|
|
|
2020-03-29 21:46:55 +11:00
|
|
|
reader = csv.reader(output.splitlines(), dialect='ledger')
|
2020-04-15 11:11:46 +10:00
|
|
|
for trn_id, date_str, code, payee, account_str, amount_str, comment, state_str in reader:
|
2020-03-20 21:43:05 +11:00
|
|
|
if not ledger.transactions or trn_id != ledger.transactions[-1].id:
|
2020-03-29 21:46:55 +11:00
|
|
|
transaction = Transaction(ledger, trn_id, datetime.strptime(date_str, '%Y-%m-%d'), payee, code=code)
|
2020-03-20 21:43:05 +11:00
|
|
|
ledger.transactions.append(transaction)
|
|
|
|
else:
|
|
|
|
# Transaction ID matches: continuation of previous transaction
|
|
|
|
transaction = ledger.transactions[-1]
|
|
|
|
|
2020-03-29 21:46:55 +11:00
|
|
|
if ';' in comment:
|
|
|
|
comment = comment[comment.index(';')+1:].strip()
|
|
|
|
|
2020-04-04 04:28:43 +11:00
|
|
|
amount = parse_amount(amount_str)
|
2020-04-15 11:11:46 +10:00
|
|
|
posting = Posting(transaction, ledger.get_account(account_str), amount, comment=comment, state=Posting.State(int(state_str)))
|
2020-03-20 21:43:05 +11:00
|
|
|
transaction.postings.append(posting)
|
2020-04-04 04:28:43 +11:00
|
|
|
|
|
|
|
if amount.commodity.name not in ledger.commodities:
|
|
|
|
ledger.commodities[amount.commodity.name] = amount.commodity.strip_price()
|
2020-01-14 00:06:14 +11:00
|
|
|
|
2020-03-20 21:43:05 +11:00
|
|
|
return ledger
|