Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def render_postings_table(postings, style,
filterfun=None,
acc_checks=None,
amount_overrides=None):
table = TABLE(
THEAD(
TR(TH("Date"), TH("F"), TH("Description/Posting"),
TH(""), TH("Amount"), TH("Balance"))),
CLASS='txntable')
# Get the list of transactions that relate to the postings.
txns = set(post.txn for post in postings)
balance = Wallet()
for txn in sorted(txns):
if filterfun is not None and filterfun(txn) is True:
continue
if acc_checks is not None:
register_insert_checks(acc_checks, table, txn.actual_date)
try:
sty = 'background-color: %s' % flag_colors[txn.flag]
except KeyError:
sty = ''
# Sum the balance of the selected postings from this transaction.
txn_amount = Wallet()
for post in txn.postings:
if post in postings:
balance = Wallet()
for txn in sorted(txns):
if filterfun is not None and filterfun(txn) is True:
continue
if acc_checks is not None:
register_insert_checks(acc_checks, table, txn.actual_date)
try:
sty = 'background-color: %s' % flag_colors[txn.flag]
except KeyError:
sty = ''
# Sum the balance of the selected postings from this transaction.
txn_amount = Wallet()
for post in txn.postings:
if post in postings:
if amount_overrides and post in amount_overrides:
amt = amount_overrides[post]
else:
amt = post.amount
txn_amount += amt
# Add this amount to the balance.
balance += txn_amount
# Display the transaction line.
desc = []
if txn.payee:
desc.append(A(txn.payee, href=umap('@@LedgerPayee', txn.payee_key),
CLASS='payee'))
balance = Wallet()
for txn in sorted(txns):
if filterfun is not None and filterfun(txn) is True:
continue
if acc_checks is not None:
register_insert_checks(acc_checks, table, txn.actual_date)
try:
sty = 'background-color: %s' % flag_colors[txn.flag]
except KeyError:
sty = ''
# Sum the balance of the selected postings from this transaction.
txn_amount = Wallet()
for post in txn.postings:
if post in postings:
if amount_overrides and post in amount_overrides:
amt = amount_overrides[post]
else:
amt = post.amount
txn_amount += amt
# Add this amount to the balance.
balance += txn_amount
# Display the transaction line.
desc = []
if txn.payee:
desc.append(A(txn.payee, href=umap('@@PayeeLedger', txn.payee_key),
CLASS='payee'))
for bt in ledger.booked_trades:
legs_table = TABLE(
THEAD(
TR(TH("Date"), TH("Units"), TH("Price"),
TH("Amount"), TH("Exchange Rate"), TH("Report Amount (target CCY)"))),
CLASS="trades")
for leg in bt.legs:
legs_table.add(
TR(TD(str(leg.post.actual_date)),
TD(hwallet(Wallet(bt.comm_book, leg.amount_book))),
TD(hwallet(Wallet(leg.comm_price, leg.price))),
TD(hwallet(Wallet(leg.comm_price, leg.amount_price))),
TD(hwallet(Wallet('%s/%s' % (leg.comm_price, bt.comm_target or '-'), leg.xrate))),
TD(hwallet(Wallet(bt.comm_target or leg.comm_price, leg.amount_target))),
))
post_book = bt.post_book
legs_table.add(
TR(TD('Gain(+) / Loss(-)'),
TD(),
TD(),
TD(hwallet(bt.post_book.amount_orig)),
TD(),
TD(hwallet(bt.post_book.amount)),
))
postings = [x.post for x in bt.legs]
overrides = dict((x.post, Wallet(bt.comm_book, x.amount_book))
post.cost = Wallet(ccom, cnum)
add_commodity(ccom)
else:
assert mo.group(5) is None, mo.groups()
assert mo.group(8) is None, mo.groups()
# Compute the price from the explicit cost.
if post.cost is not None:
if post.price is None:
post.price = Wallet(ccom, cnum/anum)
# Compute the cost from the explicit price.
elif post.price is not None:
post.cost = Wallet(pcom, anum*pnum)
# Compute the cost directly from the amount.
else:
post.cost = post.amount
if post.cost is not None:
post.cost = Wallet(post.cost) # copy
# Look for date overrides in the note field.
if post.note:
mo = search_notedate(post.note)
if mo:
# Set the posting's date according to the
# dates in the note.
actual = mo.group(1,2,3)
if actual[0]:
def check_postings_balance(self, postings):
"""
Check that the given list of postings balance and automatically fill-in
for missing ones.
"""
if not postings:
return
# Note: we assume that we've already set the cost to the amount if
# there was no price defined, so we can just use the cost here (in
# convert_wallets()).
cost = Wallet()
noamount = None
for post in postings:
if post.cost is not None:
cost += post.cost
else:
if noamount is None:
noamount = post
else:
self.log(CRITICAL, "More than one missing amounts.", post)
post.cost = Wallet() # patch it up.
if noamount:
# Fill in the missing amount.
diff = -cost
noamount.amount = noamount.cost = diff
cost += diff
amount_target = amount_price * xrate
btrade.add_leg(post, amount_book,
price, comm_price,
amount_price, xrate, amount_target)
pnl_price += amount_price
pnl_target += amount_target
assert pnl_price == inv.reset_pnl() # Sanity check.
w_price = Wallet(comm_price, -pnl_price)
w_target = Wallet(comm_target or comm_price, -pnl_target)
if not hasattr(post_book, 'amount_orig'):
post_book.amount_orig = Wallet()
post_book.amount_orig += w_price
post_book.amount += w_target
post_book.flag = 'B'
post_book.note = 'BOOKED'
booked[:] = []
self.booked_trades.append(btrade)
self.booked_trades.sort()
def render_trial_field(ledger, aname, conversions=None):
"""
Render a trial balance of the accounts tree using a particular field.
"""
table = TABLE(id='balance', CLASS='accounts treetable')
table.add(THEAD(TR(TH("Account"), TH("Amount"), TH(), TH("Cum. Sum"))))
it = iter(itertree(ledger.get_root_account()))
sum_ = Wallet()
for acc, td1, tr, skip in treetable_builder(table, it):
if len(acc) == 0:
skip()
continue
td1.add(
A(acc.name, href=umap('@@JournalAccount', webaccname(acc.fullname)),
CLASS='accomp'))
lbal = acc.balances.get(aname, None)
bal = acc.balances_cumul.get(aname, None)
if lbal.isempty() and bal.isempty():
skip()
continue
if lbal is not None:
lbal = lbal.convert(conversions).round()