{#
    ledger-pyreport
    Copyright © 2020  Lee Yingtong Li (RunasSudo)
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
#}

{% extends 'base_report.html' %}

{% macro print_rows(account, invert=False, level=0) %}
	<tr>
		<td style="padding-left: calc(2px + {{ level }}em);">
			{% if balance_sheets|length == 1 %}
				{% if account.name == config['current_year_earnings'] %}
					<a href="/pandl?{{ {'date_end': balance_sheets[0].date.strftime('%Y-%m-%d'), 'date_beg': balance_sheets[0].pstart.strftime('%Y-%m-%d'), 'compare': '0', 'cash': 'on' if cash else '', 'scope': 'pandl'}|urlencode }}">
				{% elif account.name == config['current_year_oci'] %}
					<a href="/pandl?{{ {'date_end': balance_sheets[0].date.strftime('%Y-%m-%d'), 'date_beg': balance_sheets[0].pstart.strftime('%Y-%m-%d'), 'compare': '0', 'cash': 'on' if cash else '', 'scope': 'oci'}|urlencode }}">
				{% else %}
					<a href="/transactions?{{ {'date_end': balance_sheets[0].date.strftime('%Y-%m-%d'), 'date_beg': balance_sheets[0].pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">
				{% endif %}
					{{ account.bits[-1] }}
				</a>
			{% else %}
				{{ account.bits[-1] }}
			{% endif %}
		</td>
		{% for balance_sheet in balance_sheets %}
			{% set amount = (-balance_sheet.get_balance(account) if invert else balance_sheet.get_balance(account)).exchange(report_commodity, True) %}
			<td>
				{% if not amount.near_zero %}
					{% if account.name == config['current_year_earnings'] %}
						{{ amount|a('/pandl?' + {'date_end': balance_sheet.date.strftime('%Y-%m-%d'), 'date_beg': balance_sheets[0].pstart.strftime('%Y-%m-%d'), 'compare': '0', 'cash': 'on' if cash else '', 'scope': 'both'}|urlencode) }}
					{% elif account.name == config['current_year_oci'] %}
						{{ amount|a('/pandl?' + {'date_end': balance_sheet.date.strftime('%Y-%m-%d'), 'date_beg': balance_sheet.pstart.strftime('%Y-%m-%d'), 'compare': '0', 'cash': 'on' if cash else '', 'scope': 'oci'}|urlencode) }}
					{% else %}
						{{ amount|a('/transactions?' + {'date_end': balance_sheet.date.strftime('%Y-%m-%d'), 'date_beg': balance_sheet.pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode) }}
					{% endif %}
						
				{% endif %}
			</td>
		{% endfor %}
	</tr>
	
	{% for child in account.children|sort(attribute='name') if child in accounts %}
		{{ print_rows(child, invert, level + 1) }}
	{% endfor %}
{% endmacro %}

{% macro do_accounts(root, label, invert, year_headers) %}
	{% for account_class in root.children|sort(attribute='name') if account_class in accounts %}
		<tr>
			{% if loop.first and year_headers %}
				<th class="h2">{{ account_class.bits[-1] }} {{ label }}</th>
				{% for balance_sheet in balance_sheets %}<th class="h2">{{ balance_sheet.label }}&nbsp;</th>{% endfor %}
			{% else %}
				<th class="h2" colspan="{{ balance_sheets|length + 1 }}">{{ account_class.bits[-1] }} {{ label }}</th>
			{% endif %}
		</tr>
		
		{% for account in account_class.children|sort(attribute='name') if account in accounts %}
			{{ print_rows(account, invert=invert) }}
		{% endfor %}
		
		<tr class="total">
			<td>Total {{ account_class.bits[-1] }} {{ label }}</td>
			{% for balance_sheet in balance_sheets %}<td>{{ (-balance_sheet.get_total(account_class) if invert else balance_sheet.get_total(account_class)).exchange(report_commodity, True)|a }}</td>{% endfor %}
		</tr>
		<tr><td colspan="2">&nbsp;</td></tr>
	{% endfor %}
	
	<tr class="total">
		<td>Total {{ label }}</td>
		{% for balance_sheet in balance_sheets %}<td>{{ (-balance_sheet.get_total(root) if invert else balance_sheet.get_total(root)).exchange(report_commodity, True)|a }}</td>{% endfor %}
	</tr>
{% endmacro %}

{% block title %}Balance Sheet as at {{ balance_sheets[0].date.strftime('%d %B %Y') }}{% endblock %}

{% block report %}
	<h1>Balance Sheet</h1>
	<h2>As at {{ balance_sheets[0].date.strftime('%d %B %Y') }}</h2>
	
	<table class="ledger onedesc">
		{# Assets #}
		<tr><th class="h1" colspan="{{ balance_sheets|length + 1 }}">Assets</th></tr>
		{{ do_accounts(ledger.get_account(config['assets_account']), 'Assets', False, True) }}
		<tr><td colspan="2">&nbsp;</td></tr>
		
		{# Liabilities #}
		{% if not cash %}
			<tr><th class="h1" colspan="{{ balance_sheets|length + 1 }}">Liabilities</th></tr>
			{{ do_accounts(ledger.get_account(config['liabilities_account']), 'Liabilities', True, False) }}
			<tr><td colspan="2">&nbsp;</td></tr>
		{% endif %}
		
		{# Equity #}
		<tr><th class="h1" colspan="{{ balance_sheets|length + 1 }}">Equity</th></tr>
		
		{% for account in ledger.get_account(config['equity_account']).children|sort(attribute='name') if account in accounts %}
			{{ print_rows(account, invert=True) }}
		{% endfor %}
		
		<tr class="total">
			<td>Total Equity</td>
			{% for balance_sheet in balance_sheets %}<td>{{ -balance_sheet.get_total(ledger.get_account(config['equity_account'])).exchange(report_commodity, True)|a }}</td>{% endfor %}
		</tr>
	</table>
{% endblock %}