{#  DrCr: Web-based double-entry bookkeeping framework
    Copyright (C) 2022  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.html' %}
{% block title %}Account transactions{% endblock %}

{% block content %}
	<h1 class="h2 my-4">Account transactions</h1>
	
	<div class="mb-2">
		<a href="?account={{ account }}&commodity-detail=0" class="btn btn-outline-secondary">Hide commodity detail</a>
	</div>
	
	<table class="table table-sm table-borderless">
		<thead>
			<tr>
				<th>Date</th>
				<th>Description</th>
				<th></th>
				<th class="text-end">Amount</th>
				<th class="text-end">Balance</th>
				<th></th>
			</tr>
		</thead>
		<tbody>
			{% for transaction in transactions %}
				<tr style="border-width:1px 0 0 0">
					<td>{{ transaction.dt.strftime('%Y-%m-%d') }}</td>
					<td>{{ transaction.description }}</td>
					<td></td>
					<td></td>
					<td></td>
					<td></td>
				</tr>
				{% for posting in transaction.postings if posting.account == account %}
					{% set _ = running_total.add(posting.amount()) %}
				{% endfor %}
				{% for amount in running_total.amounts %}
					{# FIXME: Assumes at most one posting per commodity #}
					{% for posting in transaction.postings if posting.commodity == amount.commodity and posting.account == account %}
						<tr>
							<td></td>
							<td></td>
							<td>{{ 'Dr' if posting.quantity >= 0 else 'Cr' }}</td>
							<td class="text-end">{{ (posting.amount()|abs).format(True) }}</td>
							<td class="text-end">{{ (amount|abs).format(True) }}</td>
							<td>{{ 'Dr' if amount.quantity >= 0 else 'Cr' }}</td>
						</tr>
					{% else %}
						<tr>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td class="text-end">{{ (amount|abs).format(True) }}</td>
							<td>{{ 'Dr' if amount.quantity >= 0 else 'Cr' }}</td>
						</tr>
					{% endfor %}
				{% endfor %}
				{% set _ = running_total.clean() %}
			{% endfor %}
		</tbody>
	</table>
{% endblock %}