Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if acc.commodity:
print(acc.commodity.base_currency)
bal = acc.get_balance()
print("{} : {} {}".format(acc.fullname, bal, acc.commodity.mnemonic))
fsdsfsd
# with create_book("test_simple_transaction.gnucash",overwrite=True) as s:
with create_book(currency="EUR") as s:
EUR = s.commodities.get(mnemonic="EUR")
acc1 = Account(name="acc1",
commodity=EUR,
parent=s.book.root_account,
account_type="BANK")
acc2 = Account(name="acc2",
commodity=EUR,
parent=s.book.root_account,
account_type="BANK",
commodity_scu=10)
s.session.flush()
tr = Transaction(currency=EUR,
description="foo",
splits=[
Split(value=Decimal("1.2345"),
account=acc1),
Split(value=Decimal("-1.2345"),
account=acc2),
])
s.session.flush()
import pytest
from piecash import create_book, Account, Transaction, Split, GncValidationError
# create new book
with create_book() as book:
ra = book.root_account
eur = book.default_currency
# number of accounts
N = 5
# number of transactions
T = 100
# create accounts
accounts = [Account("account {}".format(i), "ASSET", eur, parent=ra)
for i in range(N)]
# create transactions
for i, v in enumerate(random.randrange(10) for j in range(T)):
tx = Transaction(eur,
"transaction {}".format(i),
)
Split(accounts[random.randrange(N)], value=v, transaction=tx)
Split(accounts[random.randrange(N)], value=-v, transaction=tx)
book.save()
# select two accounts
acc = accounts[0]
tacc = accounts[1]
# move all splits from account acc to account tacc
for spl in list(acc.splits):
def find_by_name(self, term: str, include_placeholders: bool = False) -> List[Account]:
""" Search for account by part of the name """
query = (
self.query
.filter(Account.name.like('%' + term + '%'))
.order_by(Account.name)
)
# Exclude placeholder accounts?
if not include_placeholders:
query = query.filter(Account.placeholder == 0)
# print(generic.get_sql(query))
return query.all()
def add_accounts(self):
book = piecash.open_book(uri_conn=self.dbname, readonly=False, do_backup=False)
USD = book.commodities.get(mnemonic='USD')
types = self.types
# Create level=1 Gnucash accounts
for type, values in types.iteritems():
acc = piecash.Account(name=values['name'],
type=values['gnucash_type'],
parent=book.root_account,
commodity=USD,
placeholder=True)
try:
book.save()
except ValueError:
#print '%s already exists!' % acc.name
book.cancel()
# Create level=2 Gnucash accounts for Mint accounts
for account in self.mint.get_accounts():
if account['accountType'].upper() == 'CREDIT':
parent = book.accounts(name=types['LIABILITY']['name'])
else:
parent = book.accounts(name=types['ASSET']['name'])
def __load_income_in_period_query(
book: Book, account_ids: List[hex], in_model) -> List[Split]:
""" Load all data by using the query directly """
date_from = in_model.date_from
date_to = in_model.date_to
log(DEBUG, "fetching data for period %s - %s", date_from, date_to)
query = (book.query(Split)
.join(Transaction)
.join(Account)
.filter(Transaction.post_date >= date_from, Transaction.post_date <= date_to,
Account.guid.in_(account_ids))
.order_by(Transaction.post_date)
)
if in_model.currency:
query = (query
.join(Commodity)
.filter(Commodity.mnemonic == in_model.currency)
)
return query.all()
def get_by_fullname(self, fullname: str) -> Account:
""" Loads account by full name """
# get all accounts and iterate, comparing the fullname. :S
query = (
self.book.session.query(Account)
)
# generic.get_sql()
# print(sql)
all_accounts = query.all()
for account in all_accounts:
if account.fullname == fullname:
return account
# else
return None
#!/usr/bin/env python
## @file
# @brief Example Script simple sqlite create
# @ingroup python_bindings_examples
from __future__ import print_function
import os
from piecash import create_book, Account, Commodity, open_book
filename = os.path.abspath('test.blob')
with create_book(filename) as s:
a = Account(parent=s.book.root_account,
name="wow",
type="ASSET",
commodity=Commodity.create_currency_from_ISO("CAD"))
s.save()
with open_book(filename) as s:
print(s.book.root_account.children)
print(s.commodities.get(mnemonic="CAD"))
os.remove(filename)
commodity=eur,
value=Decimal("4234.342"),
)
s1.session.add(p)
print(p.value)
print(p.value_denom)
print(p.value_num)
with b1:
print(get_active_session())
with b2:
print(get_active_session())
print(get_active_session())
with b1:
acc = Account(name="foo")
print(s1.session.new)