Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for inc_acc in income_account_types.split("/"):
sub_account_name = {
"D": "Dividend Income",
"CL": "Cap Gain (Long)",
"CS": "Cap Gain (Short)",
"I": "Interest Income",
}[inc_acc]
try:
sub_acc = income_account.children(name=sub_account_name)
except KeyError:
sub_acc = Account(sub_account_name, "INCOME", cur.base_currency, income_account)
try:
cdty_acc = sub_acc.children(name=symbol)
except KeyError:
cdty_acc = Account(symbol, "INCOME", cur, sub_acc)
inc_accounts.append(cdty_acc)
return acc, inc_accounts
inc_accounts = []
if income_account:
cur = cdty.base_currency
for inc_acc in income_account_types.split("/"):
sub_account_name = {
"D": "Dividend Income",
"CL": "Cap Gain (Long)",
"CS": "Cap Gain (Short)",
"I": "Interest Income",
}[inc_acc]
try:
sub_acc = income_account.children(name=sub_account_name)
except KeyError:
sub_acc = Account(sub_account_name, "INCOME", cur.base_currency, income_account)
try:
cdty_acc = sub_acc.children(name=symbol)
except KeyError:
cdty_acc = Account(symbol, "INCOME", cur, sub_acc)
inc_accounts.append(cdty_acc)
return acc, inc_accounts
income_account_types (str): "/" separated codes to drive the creation of income accounts
Returns:
:class:`piecash.core.account.Account`: a tuple with the account under the broker_account where the stock is held
and the list of income accounts.
"""
if cdty.namespace == "CURRENCY":
raise GnucashException("{} is a currency ! You can't create stock_accounts for currencies".format(cdty))
from .account import Account
symbol = cdty.mnemonic
try:
acc = broker_account.children(name=symbol)
except KeyError:
acc = Account(symbol, "STOCK", cdty, broker_account)
inc_accounts = []
if income_account:
cur = cdty.base_currency
for inc_acc in income_account_types.split("/"):
sub_account_name = {
"D": "Dividend Income",
"CL": "Cap Gain (Long)",
"CS": "Cap Gain (Short)",
"I": "Interest Income",
}[inc_acc]
try:
sub_acc = income_account.children(name=sub_account_name)
except KeyError:
sub_acc = Account(sub_account_name, "INCOME", cur.base_currency, income_account)
type="TRADING",
placeholder=1,
commodity=self.default_currency,
parent=self.root_account)
try:
nspc = trading.children(name=cdty.namespace)
except KeyError:
nspc = Account(name=namespace,
type="TRADING",
placeholder=1,
commodity=self.default_currency,
parent=trading)
try:
tacc = nspc.children(name=cdty.mnemonic)
except KeyError:
tacc = Account(name=mnemonic,
type="TRADING",
placeholder=0,
commodity=cdty,
parent=nspc)
# self.flush()
return tacc
if tacc: return tacc
from .account import Account
try:
trading = self.root_account.children(name="Trading")
except KeyError:
trading = Account(name="Trading",
type="TRADING",
placeholder=1,
commodity=self.default_currency,
parent=self.root_account)
try:
nspc = trading.children(name=cdty.namespace)
except KeyError:
nspc = Account(name=namespace,
type="TRADING",
placeholder=1,
commodity=self.default_currency,
parent=trading)
try:
tacc = nspc.children(name=cdty.mnemonic)
except KeyError:
tacc = Account(name=mnemonic,
type="TRADING",
placeholder=0,
commodity=cdty,
parent=nspc)
# self.flush()
return tacc
assert VERSION_FORMAT in version_supported, "The 'version_format'={} is not supported. " \
"Choose one of {}".format(VERSION_FORMAT,
list(version_supported.keys()))
for table_name, table_version in version_supported[VERSION_FORMAT].items():
s.add(Version(table_name=table_name, table_version=table_version))
# create book and merge with session
b = Book()
s.add(b)
adapt_session(s, book=b, readonly=False)
# create commodities and initial accounts
from .account import Account
b.root_account = Account(name="Root Account", type="ROOT",
commodity=factories.create_currency_from_ISO(currency),
book=b)
b.root_template = Account(name="Template Root", type="ROOT", commodity=None, book=b)
b.save()
return b
def preload(self):
# preload list of accounts
accounts = self.session.query(Account).options(joinedload("splits").joinedload("transaction"),
joinedload("children"),
joinedload("commodity"),
).all()
# load all splits
splits = self.session.query(Split).join(Transaction).options(
joinedload("account"),
joinedload("lot")) \
.order_by(Transaction.post_date, Split.value).all()
return accounts, splits
def trading_account(self, cdty):
"""Return the trading account related to the commodity. If it does not exist and the option
"Use Trading Accounts" is enabled, create it on the fly"""
key = namespace, mnemonic = cdty.namespace, cdty.mnemonic
if self._trading_accounts is None:
self._trading_accounts = {}
tacc = self._trading_accounts.get(key, None)
if tacc: return tacc
from .account import Account
try:
trading = self.root_account.children(name="Trading")
except KeyError:
trading = Account(name="Trading",
type="TRADING",
placeholder=1,
commodity=self.default_currency,
parent=self.root_account)
try:
nspc = trading.children(name=cdty.namespace)
except KeyError:
nspc = Account(name=namespace,
type="TRADING",
placeholder=1,
commodity=self.default_currency,
parent=trading)
try:
tacc = nspc.children(name=cdty.mnemonic)
except KeyError:
tacc = Account(name=mnemonic,
for table_name, table_version in version_supported[VERSION_FORMAT].items():
s.add(Version(table_name=table_name, table_version=table_version))
# create book and merge with session
b = Book()
s.add(b)
adapt_session(s, book=b, readonly=False)
# create commodities and initial accounts
from .account import Account
b.root_account = Account(name="Root Account", type="ROOT",
commodity=factories.create_currency_from_ISO(currency),
book=b)
b.root_template = Account(name="Template Root", type="ROOT", commodity=None, book=b)
b.save()
return b