2013-12-11 08:23:44 +00:00
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from accounting.models import Transaction, Posting, Amount
|
|
|
|
from accounting.transport import AccountingDecoder, AccountingEncoder
|
|
|
|
|
|
|
|
# TODO: Client should be a class
|
|
|
|
|
|
|
|
HOST = None
|
|
|
|
|
|
|
|
|
|
|
|
def insert_paypal_transaction(amount):
|
|
|
|
t = Transaction(
|
|
|
|
date=datetime.today(),
|
|
|
|
payee='PayPal donation',
|
|
|
|
postings=[
|
|
|
|
Posting(account='Income:Donations:PayPal',
|
|
|
|
amount=Amount(symbol='$', amount=-amount)),
|
|
|
|
Posting(account='Assets:Checking',
|
|
|
|
amount=Amount(symbol='$', amount=amount))
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
response = requests.post(HOST + '/transaction',
|
|
|
|
headers={'Content-Type': 'application/json'},
|
|
|
|
data=json.dumps({'transactions': [t]},
|
|
|
|
cls=AccountingEncoder))
|
|
|
|
|
|
|
|
print(response.json(cls=AccountingDecoder))
|
|
|
|
|
|
|
|
|
|
|
|
def get_balance():
|
|
|
|
response = requests.get(HOST + '/balance')
|
|
|
|
|
|
|
|
balance = response.json(cls=AccountingDecoder)
|
|
|
|
|
|
|
|
_recurse_accounts(balance['balance_report'])
|
|
|
|
|
|
|
|
|
|
|
|
def _recurse_accounts(accounts, level=0):
|
|
|
|
for account in accounts:
|
|
|
|
print(' ' * level + ' + {account.name}'.format(account=account) +
|
|
|
|
' ' + '-' * (80 - len(str(account.name)) - level))
|
|
|
|
for amount in account.amounts:
|
|
|
|
print(' ' * level + ' {amount.symbol} {amount.amount}'.format(
|
|
|
|
amount=amount))
|
|
|
|
_recurse_accounts(account.accounts, level+1)
|
|
|
|
|
|
|
|
|
2013-12-11 14:12:08 +00:00
|
|
|
def get_register():
|
|
|
|
response = requests.get(HOST + '/register')
|
|
|
|
|
|
|
|
register = response.json(cls=AccountingDecoder)
|
|
|
|
|
|
|
|
for transaction in register['register_report']:
|
|
|
|
print('{date} {t.payee:.<69}'.format(
|
|
|
|
date=transaction.date.strftime('%Y-%m-%d'),
|
|
|
|
t=transaction))
|
|
|
|
|
|
|
|
for posting in transaction.postings:
|
|
|
|
print(' ' + posting.account +
|
|
|
|
' ' * (80 - len(posting.account) - len(posting.amount.symbol) -
|
|
|
|
len(str(posting.amount.amount)) - 1 - 1) +
|
|
|
|
posting.amount.symbol + ' ' + str(posting.amount.amount))
|
|
|
|
|
|
|
|
|
2013-12-11 08:23:44 +00:00
|
|
|
def main(argv=None, prog=None):
|
|
|
|
global HOST
|
|
|
|
if argv is None:
|
|
|
|
prog = sys.argv[0]
|
|
|
|
argv = sys.argv[1:]
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(prog=prog)
|
|
|
|
parser.add_argument('-p', '--paypal', type=Decimal)
|
2013-12-11 14:12:08 +00:00
|
|
|
parser.add_argument('-b', '--balance', action='store_true')
|
|
|
|
parser.add_argument('-r', '--register', action='store_true')
|
2013-12-11 08:23:44 +00:00
|
|
|
parser.add_argument('-v', '--verbosity',
|
|
|
|
default='WARNING',
|
|
|
|
help=('Filter logging output. Possible values:' +
|
|
|
|
' CRITICAL, ERROR, WARNING, INFO, DEBUG'))
|
|
|
|
parser.add_argument('--host', default='http://localhost:5000')
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
|
|
|
HOST = args.host
|
|
|
|
|
|
|
|
logging.basicConfig(level=getattr(logging, args.verbosity))
|
|
|
|
|
|
|
|
if args.paypal:
|
|
|
|
insert_paypal_transaction(args.paypal)
|
|
|
|
elif args.balance:
|
|
|
|
get_balance()
|
2013-12-11 14:12:08 +00:00
|
|
|
elif args.register:
|
|
|
|
get_register()
|
2013-12-11 08:23:44 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|