{% 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">

	{% if event %}
		<div class="btn-group float-right" role="group" aria-label="Event Links">
			<a href="{% url 'financesByEvent' %}" class="btn btn-primary">{% trans "Return to Event Summary" %}</a>
			<a href="{% url 'admin:core_event_change' event.id %}" class="btn btn-outline-primary">{% trans "Edit Event" %}</a>
		</div>	
	{% else %}
		<a href="{% url 'financesByMonth' %}" class="btn btn-primary float-right">{% trans "Return to Monthly Summary" %}</a>
	{% endif %}

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

	<div class="btn-group" role="group" aria-label="Financial Bases">
		<a href="{{ request.path }}?basis=accrualDate{% if startDate %}&amp;startDate={{ startDate|date:'Y-m-d' }}{% endif %}{% if endDate %}&amp;endDate={{ endDate|date:'Y-m-d' }}{% endif %}" type="button" class="btn btn-outline-secondary {% if basis == 'accrualDate' %}active{% endif %}">{% trans "Accrual Date (Default)" %}</a>
		<a href="{{ request.path }}?basis=paymentDate{% if startDate %}&amp;startDate={{ startDate|date:'Y-m-d' }}{% endif %}{% if endDate %}&amp;endDate={{ endDate|date:'Y-m-d' }}{% endif %}" type="button" class="btn btn-outline-secondary {% if basis == 'paymentDate' %}active{% endif %}">{% trans "Payment/Receipt Date" %}</a>
		<a href="{{ request.path }}?basis=submissionDate{% if startDate %}&amp;startDate={{ startDate|date:'Y-m-d' }}{% endif %}{% if endDate %}&amp;endDate={{ endDate|date:'Y-m-d' }}{% endif %}" 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>
{% if event %}
	<h4 class="text-center">
	{% if event.eventoccurrence_set.count == 1 %}{% trans "Date" %}{% else %}{% trans "Dates" %}{% endif %}:
	{% for occurrence in event.eventoccurrence_set.all %}
		{{occurrence.startTime|date:'N jS' }}{% if not forloop.last %}, {% endif %}
	{% endfor %}
	</h4>
{% endif %}

<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_if_none: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 }}{% spaceless %}
						{% if not item.received %}
							&nbsp;<span class="badge badge-warning">{% trans "Not Received" %}</span>
						{% endif %}{% endspaceless %}
					</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 }}{% spaceless %}
							{% if not item.received %}
								&nbsp;<span class="badge badge-warning">{% trans "Not Received" %}</span>
							{% endif %}{% endspaceless %}
						</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 payTo 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>
							{{ teacher.grouper.name|default:grouper_default }}
						</th>
						<td>{% get_item_by_key instructionExpenseInstructorTotals 'transactionparty' 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 }}{% spaceless %}
							{% if not item.paid %}
								&nbsp;<span class="badge badge-warning">{% trans "Unpaid" %}</span>
							{% elif not item.approved %}
								&nbsp;<span class="badge badge-danger">{% trans "Not Approved" %}</span>
							{% endif %}{% endspaceless %}
						</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 payTo 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 }}{% spaceless %}
							{% if not item.paid %}
								&nbsp;<span class="badge badge-warning">{% trans "Unpaid" %}</span>
							{% elif not item.approved %}
								&nbsp;<span class="badge badge-danger">{% trans "Not Approved" %}</span>
							{% endif %}{% endspaceless %}
						</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="3">{% 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="3">
							<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 "Pay To" %}</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.name }}</td>
						<td>
							{{ item.description }}{% spaceless %}
							{% if not item.paid %}
								&nbsp;<span class="badge badge-warning">{% trans "Unpaid" %}</span>
							{% elif not item.approved %}
								&nbsp;<span class="badge badge-danger">{% trans "Not Approved" %}</span>
							{% endif %}{% endspaceless %}
						</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 %}