def cmd_db_init(settings): """ Initialize if the database file doest not exists, and update schema. """ sa = get_session(settings.dbref) # XXX: update schema.. metadata.create_all() accounting_acc = Account(name=ACCOUNT_ACCOUNTING) accounting_acc.init_defaults() expenses_acc = Account(name=ACCOUNT_EXPENSES) expenses_acc.init_defaults() credit_acc = Account(name=ACCOUNT_CREDIT) credit_acc.init_defaults() sa.add(accounting_acc) sa.add(expenses_acc) sa.add(credit_acc) sa.commit()
def cmd_balance_verify(settings, sa=None): """ Print balance for expence accounts. """ if not sa: sa = get_session(settings.dbref) expenses_acc = Account.all((Account.name.like(ACCOUNT_CREDIT+'%'),), sa=sa) balances = [ sa.query(func.sum(Mutation.amount))\ .filter( Mutation.from_account == acc.account_id ) .one() for acc in expenses_acc ] for i, (sub, (sub_balance,)) in enumerate(zip(expenses_acc, balances)): print i, sub.name, sub.iban or sub.nl_number or sub.nl_p_number, sub_balance return 0
def cmd_mutation_import(opts, settings): """ Import mutations from CSV, create accounts as needed. Indx with Year, Month. """ sa = get_session(settings.dbref) if settings.reset or lib.Prompt.ask("Purge mutations?"): sa.query(Mutation).delete() log.std("Purged all previous mutations") assert settings.input_format == 'csv', settings.input_format cache = confparse.Values(dict( accounts={}, years={}, months={} )) for csvfile in opts.args.file: reader = csv_reader(csvfile, [ 'line', 'date', 'accnr', 'amount', 'destacc', 'cat', 'destname', 'descr', 'descr2' ]) for line, date, accnr, amount, destacc, cat, destname, descr, descr2 in reader: from_account, to_account = None, None assert accnr, (line, date, amount, cat) # from_account if accnr not in cache.accounts: from_account = Account.for_nr(sa, accnr) if not from_account: from_account = Account(name=ACCOUNT_CREDIT+':'+accnr) from_account.init_defaults() from_account.set_nr(accnr) sa.add(from_account) sa.commit() cache.accounts[accnr] = from_account else: from_account = cache.accounts[accnr] assert from_account.account_id, (str(from_account), line, accnr, date, amount, cat) # credit account if not destacc: if cat == 'ba': # payment card checkout to_account = Account.for_checkout(sa, ACCOUNT_EXPENSES+':ba:'+descr) elif cat == 'ga': # atm withdrawal to_account = Account.for_withdrawal(sa, ACCOUNT_EXPENSES+':ga:'+descr) elif cat == 'db': # debet interest to_account = Account.for_name_type(sa, ACCOUNT_ACCOUNTING) else: print line, date, accnr, amount, cat, descr, descr2 assert not destname, (cat, destname, cat) continue # billing account elif destacc not in cache.accounts: to_account = Account.for_nr(sa, destacc) if not to_account: to_account = Account(name=ACCOUNT_EXPENSES+':'+destname) to_account.init_defaults() to_account.set_nr(destacc) sa.add(to_account) sa.commit() cache.accounts[destacc] = to_account else: to_account = cache.accounts[destacc] # get Year/Month y, m, d = map(int, ( date[:4], date[4:6], date[6:])) if y not in cache.years: pass mut = Mutation( from_account=from_account.account_id, to_account=to_account.account_id, year=y, month=m, day=d, amount=amount, description=descr+'\t'+descr2, category=cat) sa.add(mut) sa.commit() log.std("Import ready") cmd_balance_commit(settings)