{% extends "cms/admin_home.html" %}
{% load danceschool_tags sekizai_tags i18n %}

{% block content %}

{% trans "Other/Not Specified" as grouper_default %}

<div class="row">
<div class="col-12">

	<a href="{% url 'financesByMonth' %}" class="btn btn-primary float-right">{% trans "Return to Monthly Summary" %}</a>

	<h4>{% trans "Choose Financial Basis" %}</h4>

	<div class="btn-group" role="group" aria-label="Financial Bases">
	  <a href="{% url 'financialDetailView' year month %}" type="button" class="btn btn-outline-secondary {% if basis == 'accrualDate' %}active{% endif %}">{% trans "Accrual Date (Default)" %}</a>
	  <a href="{% url 'financialDetailView' year month %}?basis=paymentDate" type="button" class="btn btn-outline-secondary {% if basis == 'paymentDate' %}active{% endif %}">{% trans "Payment/Receipt Date" %}</a>
	  <a href="{% url 'financialDetailView' year month %}?basis=submissionDate" type="button" class="btn btn-outline-secondary {% if basis == 'submissionDate' %}active{% endif %}">{% trans "Submission Date" %}</a>
	</div>

<hr />

</div>
</div>

<div class="row">
<div class="col-12">

<h1 class="my-4 text-center">{% trans "Detailed Financial Statement" %}: {{ rangeTitle }}</h1>

<h3 class="mt-5 mb-3">{% trans "Period Summary" %}</h3>

<table class="table table-bordered">
	<tr>
		<th>{% trans "Total Revenues" %}</th><td>{{ currencySymbol }}{{ totalRevenues|floatformat:"2" }}</td>
	</tr>
	<tr>
		<th>{% trans "Total Expenses" %}</th><td>{{ currencySymbol }}{{ totalExpenses|floatformat:"2" }}</td>
	</tr>
	<tr style="border-top: solid 2px;" class="info">
		<th style="font-size: 1.2em;">{% trans "Net Profit" %}</th><th style="font-size: 1.2em;">{{ currencySymbol }}{{ netProfit|floatformat:"2" }}</th>
	</tr>	
</table>

<h3 class="mt-5 mb-3">{% trans "Revenues" %}</h3>

<table class="table table-hover">
	<thead class="thead-default">
		<tr>
			<th colspan="2">{% trans "Registrations:" %}</th>
			<th>{{ currencySymbol }}{{ totalRegistrationRevenues|floatformat:"2" }} </th>
		</tr>
	</thead>
	{% regroup registrationRevenueItems by event as event_item_list %}
		{% for event in event_item_list %}
			<tbody>
				<tr data-toggle="collapse" data-target="#accordionevent_{{ event.grouper.id }}" class="clickable">
					<th colspan="2">
						<i class="fa fa-plus-circle mx-2" aria-hidden="true"></i>{{ event.grouper|default:grouper_default }}
					</th>
					<td>{% get_item_by_key registrationRevenueEventTotals 'id' event.grouper.id as cat %} {{ currencySymbol }}{{ cat.event_net|default:registrationRevenueOtherTotal.event_net|floatformat:"2" }}</td>
				</tr>
			</tbody>
			<tbody id="accordionevent_{{ event.grouper.id }}" class="collapse">
			<tr>
				<th>{% trans "Date" %}</th>
				<th>{% trans "Description" %}</th>
				<th>{% trans "Amount" %}</th>
			</tr>
			{% for item in event.list|dictsort:"basisDate" %}
				<tr>
					<td>{% if 'financial.change_revenueitem' in perms %}<a href="{% url 'admin:financial_revenueitem_change' item.id %}">{{ item.basisDate|date:"M. d, Y" }}</a>{% else %}{{ item.basisDate|date:"M. d, Y" }}{% endif %}</td>
					<td>{{ item.description }}</td>
					<td>{{ currencySymbol }}{{ item.net|floatformat:"2" }}</td>
				</tr>
			{% endfor %}
			</tbody>
		{% endfor %}
</table>

{% if otherRevenueItems %}
	<table class="table table-hover">
		<thead class="thead-default">
			<tr>
				<th colspan="2">{% trans "Other Revenues" %}:</th>
				<th>{{ currencySymbol }}{{ totalOtherRevenues|floatformat:"2" }} </th>
			</tr>
		</thead>
		{% regroup otherRevenueItems by category as other_item_list %}
			{% for category in other_item_list %}
				<tbody>
					<tr data-toggle="collapse" data-target="#accordionotherrevenue_{{ category.grouper.id }}" class="clickable">
						<th colspan="2">
							<i class="fa fa-plus-circle mx-2" aria-hidden="true"></i> {{ category.grouper|default:grouper_default }}
						</th>
						<td>{% get_item_by_key revenueCategoryTotals 'id' category.grouper.id as cat %} {{ currencySymbol }}{{ cat.category_net|floatformat:"2" }}</td>
					</tr>
				</tbody>
				<tbody id="accordionotherrevenue_{{ category.grouper.id }}" class="collapse">
					<tr>
						<th>{% trans "Date" %}</th>
						<th>{% trans "Description" %}</th>
						<th>{% trans "Amount" %}</th>
					</tr>		
				{% for item in category.list|dictsort:"basisDate" %}
					<tr>
						<td>{% if 'financial.change_revenueitem' in perms %}<a href="{% url 'admin:financial_revenueitem_change' item.id %}">{{ item.basisDate|date:"M. d, Y" }}</a>{% else %}{{ item.basisDate|date:"M. d, Y" }}{% endif %}</td>
						<td>{{ item.description }}</td>
						<td>{{ currencySymbol }}{{ item.net|floatformat:"2"  }}</td>
					</tr>
				{% endfor %}
				</tbody>
			{% endfor %}
	</table>
{% endif %}

<hr />

<h3 class="mt-5 mb-3">{% trans "Expenses" %}</h3>

{% if instructionExpenseItems %}
	<table class="table table-hover">
		<thead class="thead-default">
			<tr>
				<th colspan="2">{% trans "Instruction" %}:</th>
				<th>{{ currencySymbol }}{{ totalInstructionExpenses|floatformat:"2" }}</th>
			</tr>
		</thead>
		{% regroup instructionExpenseItems by payToUser as teacher_item_list %}
			{% for teacher in teacher_item_list %}
				<tbody>
					<tr data-toggle="collapse" data-target="#accordioninstructor_{{ teacher.grouper.id }}" class="clickable">
						<th colspan="2">
							<i class="fa fa-plus-circle mx-2" aria-hidden="true"></i>
							{% if teacher.grouper.first_name or teacher.grouper.last_name %}
								{{ teacher.grouper.first_name }} {{ teacher.grouper.last_name }}
							{% else %}
								{{ grouper_default }}
							{% endif %}
						</th>
						<td>{% get_item_by_key instructionExpenseInstructorTotals 'id' teacher.grouper.id as cat %} {{ currencySymbol }}{{ cat.instructor_net|default:instructionExpenseOtherTotal.instructor_net|floatformat:"2" }}</td>
					</tr>
				</tbody>
				<tbody id="accordioninstructor_{{ teacher.grouper.id }}" class="collapse">
					<tr>
						<th>{% trans "Date" %}</th>
						<th>{% trans "Description" %}</th>
						<th>{% trans "Amount" %}</th>
					</tr>		
				{% for item in teacher.list|dictsort:"basisDate" %}
					<tr>
						<td>
						{% if 'financial.change_expenseitem' in perms %}<a href="{% url 'admin:financial_expenseitem_change' item.id %}">{{ item.basisDate|date:"M. d, Y" }}</a>{% else %}{{ item.basisDate|date:"M. d, Y" }}{% endif %}
						</td>
						<td>{{ item.description }}</td>
						<td>{{ currencySymbol }}{{ item.net|floatformat:"2"  }}</td>
					</tr>
				{% endfor %}
				</tbody>
			{% endfor %}
	</table>
{% endif %}

{% if venueExpenseItems %}
	<table class="table table-hover">
		<thead class="thead-default">
			<tr>
				<th colspan="2">{% trans "Venue Rental" %}:</th>
				<th>{{ currencySymbol }}{{ totalVenueExpenses|floatformat:"2" }}</th>
			</tr>
		</thead>
		{% regroup venueExpenseItems by payToLocation as venue_item_list %}
			{% for venue in venue_item_list %}
				<tbody>
					<tr data-toggle="collapse" data-target="#accordionlocation_{{ venue.grouper.id }}" class="clickable">
						<th colspan="2">
							<i class="fa fa-plus-circle mx-2" aria-hidden="true"></i> {{ venue.grouper.name|default:grouper_default }}
						</th>
						<td>{% get_item_by_key venueExpenseVenueTotals 'id' venue.grouper.id as cat %} {{ currencySymbol }}{{ cat.location_net|default:venueExpenseOtherTotal.location_net|floatformat:"2" }}</td>
					</tr>
				</tbody>
				<tbody id="accordionlocation_{{ venue.grouper.id }}" class="collapse">
					<tr>
						<th>{% trans "Date" %}</th>
						<th>{% trans "Description" %}</th>
						<th>{% trans "Amount" %}</th>
					</tr>		
				{% for item in venue.list|dictsort:"basisDate" %}
					<tr>
						<td>{% if 'financial.change_expenseitem' in perms %}<a href="{% url 'admin:financial_expenseitem_change' item.id %}">{{ item.basisDate|date:"M. d, Y" }}</a>{% else %}{{ item.basisDate|date:"M. d, Y" }}{% endif %}</td>
						<td>{{ item.description }}</td>
						<td>{{ currencySymbol }}{{ item.net|floatformat:"2"  }}</td>
					</tr>
				{% endfor %}
				</tbody>
			{% endfor %}
	</table>
{% endif %}


{% if otherExpenseItems %}
	<table class="table table-hover">
		<thead class="thead-default">
			<tr>
				<th colspan="2">{% trans "Other Expenses" %}:</th>
				<th>{{ currencySymbol }}{{ totalOtherExpenses|floatformat:"2" }}</th>
			</tr>
		</thead>
		{% regroup otherExpenseItems by category as category_item_list %}
			{% for category in category_item_list %}
				<tbody>
					<tr data-toggle="collapse" data-target="#accordionotherexpense_{{ category.grouper.id }}" class="clickable">
						<th colspan="2">
							<i class="fa fa-plus-circle mx-2" aria-hidden="true"></i> {{ category.grouper.name|default:grouper_default }}
						</th>
						<td>{% get_item_by_key expenseCategoryTotals 'id' category.grouper.id as cat %} {{ currencySymbol }}{{ cat.category_net|floatformat:"2" }}</td>
					</tr>
				</tbody>
				<tbody id="accordionotherexpense_{{ category.grouper.id }}" class="collapse">
					<tr>
						<th>{% trans "Date" %}</th>
						<th>{% trans "Description" %}</th>
						<th>{% trans "Amount" %}</th>
					</tr>		
				{% for item in category.list|dictsort:"basisDate" %}
					<tr>
						<td>{% if 'financial.change_expenseitem' in perms %}<a href="{% url 'admin:financial_expenseitem_change' item.id %}">{{ item.basisDate|date:"M. d, Y" }}</a>{% else %}{{ item.basisDate|date:"M. d, Y" }}{% endif %}</td>
						<td>{{ item.payTo }}</td>
						<td>{{ item.description }}</td>
						<td>{{ currencySymbol }}{{ item.net|floatformat:"2"  }}</td>
					</tr>
				{% endfor %}
				</tbody>
			{% endfor %}
	</table>
{% endif %}

</div>
</div>

{% addtoblock "css" %}
	<style type="text/css">
		thead tr {
			font-size: 1.1em;
		}

		th .fa {
			font-size: 0.8em;
			color: #888;
		}
	</style>
{% endaddtoblock %}

{% addtoblock "js" %}
<script type="text/javascript">
	$(document).ready(function() {
		$('.collapse').on('shown.bs.collapse', function(){
		$(this).prev('tbody').find(".fa-plus-circle").removeClass("fa-plus-circle").addClass("fa-minus-circle");
		}).on('hidden.bs.collapse', function(){
		$(this).prev('tbody').find(".fa-minus-circle").removeClass("fa-minus-circle").addClass("fa-plus-circle");
		});
	});
</script>
{% endaddtoblock %}
{% endblock %}