{#
    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' %}

{% block title %}Account Transactions for the {{ period }}{% endblock %}

{% block report %}
	<h1>Account Transactions</h1>
	<h2 style="margin-bottom: 0;">For {{ account.name }}</h2>
	<h2>For the {{ period }}</h2>
	
	<table class="ledger">
		<tr>
			<th style="min-width: 5em;">Date</th>
			<th style="width: 100%;">Description</th>
			<th style="min-width: 1em;"></th> {# Dr/Cr #}
			<th style="text-align: right; min-width: 4em;">Amount</th>
			<th style="min-width: 2em;"></th> {# Commodity #}
			<th style="text-align: right; min-width: 4em;">Balance</th>
			<th style="min-width: 2em;"></th> {# Commodity #}
			<th style="min-width: 2em;"></th> {# Price #}
			<th style="min-width: 1em;"></th>
		</tr>
		
		{% set ns = namespace(balance=None) %}
		{% set prevlink = '/transactions?' + {'date_end': (date_beg - timedelta(days=1)).strftime('%Y-%m-%d'), 'date_beg': date_beg.replace(year=date_beg.year-1).strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else '', 'commodity': 'on'}|urlencode %}
		{% for amount in opening_balance.amounts %}
			<tr class="total">
				<td>{% if loop.first %}<a href="{{ prevlink }}">{{ date_beg.strftime('%Y-%m-%d') }}</a>{% endif %}</td>
				<td>{% if loop.first %}<a href="{{ prevlink }}">Opening Balance</a>{% endif %}</td>
				<td></td>
				<td></td>
				<td></td>
				{{ amount|abs|bt(True, prevlink) }}
				<td>{% if amount >= 0 %}Dr{% else %}Cr{% endif %}</td>
			</tr>
		{% else %}
			<tr class="total">
				<td><a href="{{ prevlink }}">{{ date_beg.strftime('%Y-%m-%d') }}</a></td>
				<td><a href="{{ prevlink }}">Opening Balance</a></td>
				<td></td>
				<td></td>
				<td></td>
				<td style="text-align: right;"><a href="{{ prevlink }}">0.00</a></td>
				<td></td>
				<td></td>
				<td>Dr</td>
			</tr>
		{% endfor %}
		{% set ns.balance = opening_balance %}
		
		{% for transaction in transactions %}
			{% set trn_url = '/transaction?' + {'tid': transaction.id, 'cash': 'on' if cash else ''}|urlencode %}
			{% for posting in transaction.postings if posting.account == account %}
				{% set ns.balance = ns.balance + posting.amount %}
			{% endfor %}
			{% for amount in ns.balance.amounts %}
				{% set posting = matching_posting(transaction, amount) %}
				<tr{% if loop.first %} style="border-top: 1px solid black;"{% endif %}>
					<td>{% if loop.first %}{% if transaction.id %}<a href="{{ trn_url }}">{% endif %}{{ transaction.date.strftime('%Y-%m-%d') }}{% if transaction.id %}</a>{% endif %}{% endif %}</td>
					<td>{% if loop.first %}{% if transaction.id %}<a href="{{ trn_url }}">{% endif %}{{ transaction.description }}{% if transaction.id %}</a>{% endif %}{% endif %}</td>
					{% if posting %}
						<td>{% if posting.amount >= 0 %}Dr{% else %}Cr{% endif %}</td>
						{{ posting.amount|abs|bt(False) }}
					{% else %}
						<td colspan="3"></td>
					{% endif %}
					{{ amount|abs|bt(True) }}
					<td>{% if amount >= 0 %}Dr{% else %}Cr{% endif %}</td>
				</tr>
			{% endfor %}
			{% set ns.balance = ns.balance.clean() %}
		{% endfor %}
		
		{% for amount in closing_balance.amounts %}
			<tr class="total explicit-rules" style="{% if loop.first %}border-top: 1pt solid black;{% endif %}{% if loop.last %}border-bottom: 1pt solid black;{% endif %}">
				<td>{% if loop.first %}{{ date_end.strftime('%Y-%m-%d') }}{% endif %}</td>
				<td>{% if loop.first %}Closing Balance{% endif %}</td>
				<td></td>
				<td></td>
				<td></td>
				{{ amount|abs|bt(True) }}
				<td>{% if amount >= 0 %}Dr{% else %}Cr{% endif %}</td>
			</tr>
		{% endfor %}
		
		<tr class="total">
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			{% set closing_balance = closing_balance.exchange(report_commodity, True) %}
			{{ closing_balance|abs|bt(True) }}
			<td>{% if closing_balance >= 0 %}Dr{% else %}Cr{% endif %}</td>
		</tr>
	</table>
{% endblock %}