\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=3cm]{geometry}
\setlength{\parindent}{0pt} %--don't indent paragraphs
\begin{document}
\centerline{\Large\bfseries Capital Gains Tax Calculations for \VAR{ report.tax_year }-\VAR{ ("{:02d}".format((report.tax_year + 1) % 100)) }}

\BLOCK{ set total_gain = namespace(value=Decimal(0)) }
\BLOCK{ set total_loss = namespace(value=Decimal(0)) }
\BLOCK{ set acquisition_count = namespace(value=0) }
\BLOCK{ set disposal_count = namespace(value=0) }
\BLOCK{ set dividend_count = namespace(value=0) }
\BLOCK{ set interest_count = namespace(value=0) }
\BLOCK{ set eri_count = namespace(value=0) }

\section*{Capital Gains Events}

\BLOCK{ for date_index, symbol_dict in report.calculation_log.items() }

\subsection*{\VAR{ date_index.strftime("%d %B %Y") }}
\BLOCK{ for symbol, entries in symbol_dict.items() }
\BLOCK{ if symbol.startswith("sell$") }
    \BLOCK{ set symbol = symbol[5:] }
    \BLOCK{ set is_disposal = True }
\BLOCK{ elif symbol.startswith("buy$") }
    \BLOCK{ set symbol = symbol[4:] }
    \BLOCK{ set is_aquisition = True }
\BLOCK{ elif symbol.startswith("spin-off$") }
    \BLOCK{ set symbol = symbol[9:] }
    \BLOCK{ set is_spin_off = True }
\BLOCK{ elif symbol.startswith("excess-reported-income$") }
    \BLOCK{ set symbol = symbol[23:] }
    \BLOCK{ set is_eri = True }
\BLOCK{ endif }
\BLOCK{ set overall_quantity = entries|sum(attribute="quantity") }
\BLOCK{ set overall_fees = round_decimal(entries|sum(attribute="fees"), 2) }
\BLOCK{ if is_disposal }
    \BLOCK{ set overall_disposed = round_decimal(entries|sum(attribute="amount"), 2) }
    \BLOCK{ set overall_gain = round_decimal(entries|sum(attribute="gain"), 2) }
    \BLOCK{ set disposal_count.value = disposal_count.value + 1 }
    \subsubsection*{
        Disposal \VAR{ disposal_count.value }: \VAR{ strip_zeros(overall_quantity) } units of \VAR{ symbol } for £\VAR{ "{:,}".format(overall_disposed) }
        (£\VAR{ "{:,}".format(round_decimal(overall_disposed/overall_quantity, 2)) }/unit)%
        \BLOCK{ if overall_fees > 0 }
            , after £\VAR{ "{:,}".format(overall_fees) } fees
        \BLOCK{ endif }
    }
    \BLOCK{ if overall_gain > 0 -}
        Chargeable \textbf{gain} is £\VAR{ "{:,}".format(overall_gain) }

        \BLOCK{ set total_gain.value = total_gain.value + overall_gain }
        Capital gain to date is £\VAR{ "{:,}".format(total_gain.value) }
    \BLOCK{ else }
        Chargeable \textbf{loss} is £\VAR{ "{:,}".format(-overall_gain) }

        \BLOCK{ set total_loss.value = total_loss.value - overall_gain }
        Capital loss to date is £\VAR{ "{:,}".format(total_loss.value) }
    \BLOCK{ endif }
\BLOCK{ elif is_aquisition } % if is_aquisition
    \BLOCK{ set acquisition_cost = round_decimal(entries[0].allowable_cost, 2) }
    \BLOCK{ if overall_quantity > 0 }
        \BLOCK{ set acquisition_count.value = acquisition_count.value + 1 }
        \subsubsection*{
            Acquisition \VAR{ acquisition_count.value }: \VAR{ strip_zeros(overall_quantity) } units of \VAR{ symbol } for £\VAR{ "{:,}".format(acquisition_cost) }
            (£\VAR{ "{:,}".format(round_decimal(acquisition_cost/overall_quantity, 2)) }/unit)%
            \BLOCK{ if overall_fees > 0 }
                , including £\VAR{ "{:,}".format(overall_fees) } fees
            \BLOCK{ endif }
        }
    \BLOCK{ else }
        \subsubsection*{Management fee for \VAR{ symbol } of £\VAR{ "{:,}".format(acquisition_cost) }}
    \BLOCK{ endif}
\BLOCK{ elif is_spin_off } % if is_spin_off
    \BLOCK{ set acquisition_cost = round_decimal(entries[0].allowable_cost, 2) }
    \BLOCK{ if overall_quantity > 0 }
        \BLOCK{ set acquisition_count.value = acquisition_count.value + 1 }
        \subsubsection*{
            Spin-off adjustment \VAR{ acquisition_count.value }: \VAR{ entries[0].spin_off.source } cost adjusted because of \VAR{ entries[0].spin_off.dest } shares spinned off
        }
    \BLOCK{ endif }
\BLOCK{ elif is_eri } % if is_eri
    \BLOCK{ set acquisition_cost = round_decimal(entries[0].allowable_cost, 2) }
    \BLOCK{ if overall_quantity > 0 }
        \subsubsection*{
            Excess Reported Income: \VAR{ symbol } cost increased by £\VAR{ "{:,}".format(acquisition_cost) }
            (£\VAR{ "{:,}".format(round_decimal(entries[0].eris[0].price, 4)) }/unit)%
        }
    \BLOCK{ endif }
\BLOCK{ endif }

\begin{enumerate}
\BLOCK{ for entry in entries }
\item \textbf{\VAR{ entry.rule_type.name.replace("_", " ") }}. Quantity: \VAR{ strip_zeros(entry.quantity) },
\BLOCK{ if is_disposal }
    \BLOCK{ if entry.quantity < overall_quantity }
        disposal proceeds: £\VAR{ "{:,}".format(round_decimal(entry.amount, 2)) },
    \BLOCK{ endif }
    allowable
    \BLOCK{ if entry.rule_type.name == "BED_AND_BREAKFAST" }
        cost on \mbox{\VAR{ entry.bed_and_breakfast_date_index.strftime("%d %b %Y") }:}
    \BLOCK{ else }
        cost:
    \BLOCK{ endif }
    £\VAR{ "{:,}".format(round_decimal(entry.allowable_cost, 2)) },
    \BLOCK{ if entry.gain > 0}
        \mbox{gain: £\VAR{ "{:,}".format(round_decimal(entry.gain, 2)) }.}
    \BLOCK{ else }
        \mbox{loss: £\VAR{ "{:,}".format(round_decimal(-entry.gain, 2)) }.}
    \BLOCK{ endif }
\BLOCK{ elif is_spin_off }
    original cost of \VAR{ entry.spin_off.source }: £\VAR{ "{:,}".format(round_decimal(-entry.amount, 2))}, after spin-off: £\VAR{ "{:,}".format(round_decimal(entry.new_pool_cost, 2))}
\BLOCK{ elif is_eri }
    original cost of \VAR{ symbol }: £\VAR{ "{:,}".format(round_decimal(-entry.amount, 2))}, after income distribution: £\VAR{ "{:,}".format(round_decimal(entry.new_pool_cost, 2))}%
\BLOCK{ else } % if is_aquisition
    actual cost: £\VAR{ "{:,}".format(round_decimal(-entry.amount, 2)) }.
\BLOCK{ endif}

\BLOCK{ if is_disposal }
    Number of units left in the pool:
\BLOCK{ else }
    Number of units in the pool:
\BLOCK{ endif }
\VAR{ strip_zeros(entry.new_quantity) }%
\BLOCK{ if entry.new_quantity > 0 },
    new pool cost: £\VAR{ "{:,}".format(round_decimal(entry.new_pool_cost, 2)) }
    (£\VAR{ "{:,}".format(round_decimal(entry.new_pool_cost/entry.new_quantity, 2)) }/unit)%
    \BLOCK{ if is_aquisition and entry.spin_off}

    Cost basis calculated based on cost of \VAR{entry.spin_off.source} share pool from which \VAR{ symbol } shares were spinned off%
    \BLOCK{ endif }
    \BLOCK{ if is_aquisition }
        \BLOCK{ for eri in entry.eris }
            \BLOCK{ set eri_cost = round_decimal(eri.price * entry.quantity, 2) }
            \BLOCK{ set eri_date = eri.date.strftime("%d %b %Y") }

    Cost basis increased by £\VAR{ "{:,}".format(eri_cost)}
    (£\VAR{ "{:,}".format(round_decimal(eri.price, 4)) }/unit)%
    , due to exccess reported income on \mbox{\VAR{ eri_date }}%
        \BLOCK{ endfor }
    \BLOCK{ endif }
\BLOCK{ endif }.

\BLOCK{ endfor}
\end{enumerate}

\BLOCK{ endfor }

\BLOCK{ endfor }

\BLOCK{ if report.calculation_log_yields }
    \clearpage
    \section*{Interest and Dividends Events}

    \BLOCK{ for date_index, symbol_dict in report.calculation_log_yields.items() }

    \subsection*{\VAR{ date_index.strftime("%d %B %Y") }}
    \BLOCK{ for symbol, entries in symbol_dict.items() }
    \BLOCK{ if symbol.startswith("dividend$") }
        \BLOCK{ set symbol = symbol[9:] }
        \BLOCK{ set is_dividend = True }
    \BLOCK{ elif symbol.startswith("interestUK$") }
        \BLOCK{ set symbol = symbol[11:] }
        \BLOCK{ set is_interest = True }
        \BLOCK{ set interest_str = "UK" }
    \BLOCK{ elif symbol.startswith("interest") }
        \BLOCK{ set symbol = symbol.split('$')[0][8:] }
        \BLOCK{ set is_interest = True }
        \BLOCK{ set interest_str = "Foreign" }
    \BLOCK{ elif symbol.startswith("excess-reported-income-distribution$") }
        \BLOCK{ set symbol = symbol[36:] }
        \BLOCK{ set is_eri_tax = True }
    \BLOCK{ endif }
    \BLOCK{ if is_dividend }
        \BLOCK{ set type_str = "" }
        \BLOCK{ set dividend_count.value = dividend_count.value + 1 }
        \BLOCK{ if entries[0].dividend.is_interest }
            \BLOCK{ set type_str = " (interest)" }
        \BLOCK{ endif }
        \textbf{Dividend \VAR{ dividend_count.value }:} \VAR{ symbol + type_str } for £\VAR{ "{:,}".format(round_decimal(entries[0].amount, 2)) }
        \BLOCK{ if not entries[0].dividend.is_interest }
            (tax paid at source: £\VAR{ "{:,}".format(round_decimal(-entries[0].dividend.tax_at_source, 2)) })
        \BLOCK{ endif }
        \BLOCK{ if entries[0].dividend.tax_treaty }
            , tax treaty amount: £\VAR{ "{:,}".format(round_decimal(entries[0].dividend.tax_treaty_amount, 2)) } (treaty rate for \VAR{ entries[0].dividend.tax_treaty.country }: \VAR{ round_decimal(100 * entries[0].dividend.tax_treaty.treaty_rate, 2) }\%)
        \BLOCK{ endif }
    \BLOCK{ elif is_interest } % if is_interest
        \BLOCK{ set interest_count.value = interest_count.value + 1 }
        \textbf{Interest \VAR{ interest_str } \VAR{ interest_count.value }:} \VAR{ symbol } for £\VAR{ "{:,}".format(round_decimal(entries[0].amount, 2)) }
    \BLOCK{ elif is_eri_tax } % if is_eri_tax
        \BLOCK{ if entries[0].eris[0].is_interest }
            \BLOCK{ set type_str = " (interest)" }
        \BLOCK{ else }
            \BLOCK{ set type_str = " (dividend)" }
        \BLOCK{ endif }
        \BLOCK{ set eri_income = round_decimal(entries[0].amount, 2) }
        \BLOCK{ set eri_count.value = eri_count.value + 1 }
        \textbf{Excess Reported Income Distribution \VAR{ eri_count.value }}: \VAR{ symbol + type_str } for £\VAR{ "{:,}".format(eri_income) }
        (£\VAR{ "{:,}".format(round_decimal(entries[0].eris[0].price, 4)) }/unit)%
    \BLOCK{ endif }

    \BLOCK{ endfor }

    \BLOCK{ endfor }
\BLOCK{ endif }

\clearpage
\section*{Overall - Capital Gains}
\subsection*{Number of acquisitions: \VAR{ acquisition_count.value }}
\subsection*{Number of disposals: \VAR{ disposal_count.value }}
\subsection*{Total disposal proceeds: £\VAR{ "{:,}".format(report.disposal_proceeds) }}
\subsection*{Total capital gain before loss: £\VAR{ "{:,}".format(total_gain.value) }}
\subsection*{Total capital loss: £\VAR{ "{:,}".format(total_loss.value) }}
\subsection*{Total capital gain after loss: £\VAR{ "{:,}".format(total_gain.value - total_loss.value) }}
\vspace{2em}
\section*{Overall - Dividends and Interests}
\subsection*{Number of dividend distributions: \VAR{ dividend_count.value }}
\subsection*{Number of interest transactions: \VAR{ interest_count.value }}
\subsection*{Number of excess reported income distributions: \VAR{ eri_count.value }}
\subsection*{Total amount of dividends proceeds: £\VAR{ "{:,}".format(round_decimal(report.total_dividends_amount(), 2)) }}
\BLOCK{ if report.total_eri_amount(False) > 0 }
\subsubsection*{   (including £\VAR{ "{:,}".format(round_decimal(report.total_eri_amount(False), 2))} from excess reported income distributions)}
\BLOCK{ endif }
\subsection*{Total amount of dividends tax allowance due to double taxation treaties £\VAR{ "{:,}".format(round_decimal(report.total_dividend_taxes_in_tax_treaties_amount(), 2)) }}
\subsection*{Total amount of dividends tax yearly allowance £\VAR{ "{:,}".format(round_decimal(report.dividend_allowance or Decimal(), 2)) }}
\subsection*{Total amount of taxable dividends proceeds: £\VAR{ "{:,}".format(round_decimal(report.total_dividend_taxable_gain(), 2)) }}
\subsection*{Total amount of UK interests proceeds: £\VAR{ "{:,}".format(report.total_uk_interest) }}
\subsection*{Total amount of foreign interests proceeds: £\VAR{ "{:,}".format(report.total_foreign_interest) }}
\BLOCK{ if report.total_eri_amount(True) > 0 }
\subsubsection*{   (including £\VAR{ "{:,}".format(round_decimal(report.total_eri_amount(True), 2))} from excess reported income distributions)}
\BLOCK{ endif }
\end{document}
