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

{% block content %}

<h3 class="my-4">{% trans "Invoice #" %}{{ invoice.id }}
{% blocktrans with name=invoice.fullName %}
	for {{ name }}
{% endblocktrans %}
</h3>

{% getReturnPage as returnPage %}
{% if returnPage.url %}
	<p><a class="btn btn-outline-secondary" href="{{ returnPage.url }}">&laquo;&nbsp;{% trans "Return to" %} {{ returnPage.title }}</a></p>
{% endif %}


<dl class="card card-body">
	<dt>{% trans "Date &amp; Time" %}:</dt><dd>{{ invoice.creationDate|date:'DATETIME_FORMAT' }}</dd>
	<dt>{% trans "Net Amount Paid" %}:</dt><dd>{{ invoice.amountPaid }}</dd>
	<dt>{% trans "Price with Discounts" %}:</dt><dd>{{ invoice.total }}</dd>
	<dt>{% trans "Paid Online" %}:</dt><dd>{{ invoice.paidOnline|yesno }}</dd>
</dl>

{% if payments %}
	<div class="card">
		<h6 class="card-header">{% trans "Payment Details" %}</h6>
		<div class="card-body">
			{% for payment in payments %}
				{{ payment.methodName }}: #{{ payment.recordId }}<br />
				{% if not payment.refundable %}
					<strong>{% trans "Note" context "alert" %}:</strong> {% trans "This payment does not appear to be automatically refundable.  Please manually refund the money to this student after filling out this form." %}<br />
				{% endif %}
			{% endfor %}
		</div>
	</div>
{% endif %}

<hr />

<form id="refund_form" method="post" action="">{% csrf_token %}

<div class="my-4">
<input type="submit" class="btn btn-primary float-right" value="{% trans 'Process Refund' %}" />
<h3>{% trans "Total Refund Amount" %}: {{ currencySymbol }}<span id="total_refund_amount">{{ invoice.refunds|floatformat:2 }}</span></h3>
</div>

{% if invoice.revenueNotYetReceived %}
	<div class="alert alert-danger" role="alert">{% trans "Revenue has been recorded for this invoice that has not yet been marked as received.  In determining any refund amount, please account for revenue that has not yet been received.  Items with revenue not yet marked as received are highlighted below." %}</div>
{% endif %}
{% if invoice.unpaid %}
	<div class="alert alert-danger" role="alert">{% trans "This invoice is not yet marked as paid.  In determining any refund amount, please account for revenue that has not yet been received." %}</div>
{% endif %}

{% if form.non_field_errors %}
	<div class="alert alert-danger" role="alert">
		{{ form.non_field_errors }}
	</div>
{% endif %}
{% if form.id.errors %}
	<div class="alert alert-danger" role="alert">
		<strong>{% trans "Error with ID" %}:</strong>
		{{ form.id.errors }}
	</div>
{% endif %}

{% if form.total_refund_amount.errors %}
	<div class="alert alert-danger" role="alert">
		<strong>{% trans "Error with Total Refund Amount" %}:</strong>
		{{ form.total_refund_amount.errors }}
	</div>
{% endif %}

{% if form.initial_refund_amount.errors %}
	<div class="alert alert-danger" role="alert">
		<strong>{% trans "Error with Initial Refund Amount" %}:</strong>
		{{ form.initial_refund_amount.errors }}
	</div>
{% endif %}

{{ form.id }}
{{ form.total_refund_amount }}
{{ form.initial_refund_amount }}

	<h6>{% trans "Item Details" %}</h6>
	<table class="regrefundtable table table-bordered">
	<thead>
		<tr>
			<th>{% trans "Item ID" %}</th>
			<th>{% trans "Item Name" %}</th>
			<th>{% trans "Dates" %}</th>
			<th>{% trans "Price (Gross)" %}</th>
			<th>{% trans "Price (Net)" %}</th>
			{% if invoice.taxes > 0 and invoice.buyerPaysSalesTax %}
				<th>{% trans "Taxes" %}</th>
			{% endif %}
			<th>{% trans "Cancelled" %}</th>
			<th>{% trans "Refunds/Adjustments" %}</th>
		</tr>
	</thead>
	<tbody>
	{% for item in invoice.invoiceitem_set.all %}
		<tr {%if item.revenueNotYetReceived %}class="warning"{% endif %}>
			<td>{{ item.id }}</td>
			<td>{{ item.name }}</td>
			<td>
				{% if item.eventRegistration %}
					{% for occ in item.eventRegistration.event.eventoccurrence_set.all %}
						{{ occ.startTime|date:"M. jS" }}{% if not forloop.last %}, {% endif %}
					{% endfor %}
				{% endif %}
			</td>
			<td id="item_grossTotal_{{ item.id }}">{{ currencySymbol }}{{ item.grossTotal|floatformat:2 }}</td>
			<td id="item_total_{{ item.id }}">{{ currencySymbol }}{{ item.total|floatformat:2 }}</td>
			{% if invoice.taxes > 0 and invoice.buyerPaysSalesTax %}
				<td id="item_taxes_{{ item.id }}">{{ currencySymbol }}{{ item.taxes|floatformat:2 }}</td>
			{% endif %}
			<td>{% get_field_for_object 'item_cancelled' item.id form as field_cancelled %}{{ field_cancelled }}</td>
			<td>{% get_field_for_object 'item_refundamount' item.id form as field_refundAmount %}{{ field_refundAmount }} {{ field_cancelled.errors }}{{ field_refundAmount.errors }}</td>
		</tr>
	{% endfor %}
	</tbody>
	</table>

<div class="form-group">
    {{ form.comments.errors }}
    {{ form.comments.label_tag }}<br />
    {{ form.comments }}
    <p class="help-block">{{ form.comments.help_text }}</p>
</div>
</form>


<hr />

{% if returnPage.url %}
	<p><a class="btn btn-outline-secondary" href="{{ returnPage.url }}">&laquo;&nbsp;{% trans "Return to" %} {{ returnPage.title }}</a></p>
{% else %}
	<p>
		<a class="btn btn-outline-secondary" href="{% url 'registration' %}">{% trans "Class Registration Page" %}</a>
		<a class="btn btn-outline-secondary" href="{% url 'viewregistrations_selectevent' %}">{% trans "Select Another Event" %}</a>
	</p>
{% endif %}

{% addtoblock "js" %}
<script type="text/javascript">
	var $ = django.jQuery;

	$(document).ready(function(){
		$('input[id*=_refundamount_]').change(function(){
			var totalRefund = 0;

			$('input[id*=_refundamount_]').each(function(i,n){
				totalRefund += parseFloat($(n).val());
			});

			$('#total_refund_amount').html(totalRefund.toFixed(2));
			$('#id_total_refund_amount').val(totalRefund);
		});

		$('input[id*=_cancelled_]').change(function(){
			if (this.checked) {
				var thisType = this.id.split("_")[1];
				var thisId = this.id.split("_")[3];
				var total = parseFloat($("#" + thisType + "_total_" + thisId).html().replace("{{ currencySymbol }}",""));
				{% if invoice.taxes > 0 and invoice.buyerPaysSalesTax %}
					var taxes = parseFloat($("#" + thisType + "_taxes_" + thisId).html().replace("{{ currencySymbol }}",""));
					var refundable = total + taxes;
				{% else %}
					var refundable = total;
				{% endif %}

				$("#id_" + thisType + "_refundamount_" + thisId).val(refundable.toFixed(2));
			}


			var totalRefund = 0;

			$('input[id*=_refundamount_]').each(function(i,n){
				totalRefund += parseFloat($(n).val());
			});

			$('#total_refund_amount').html(totalRefund.toFixed(2));
			$('#id_total_refund_amount').val(totalRefund);
		});

		$('input[type=submit]').click(function( event ){
			var alertFlag = false;

			$('input[id*=_cancelled_]:checked').each(function(i,n){
				var thisType = n.id.split("_")[1];
				var thisId = n.id.split("_")[3];
				var thisRefund = parseFloat($("#id_" + thisType + "_refundamount_" + thisId).val());
				var total = parseFloat($("#" + thisType + "_total_" + thisId).html().replace("$",""));

				{% if invoice.taxes > 0 and invoice.buyerPaysSalesTax %}
					var taxes = parseFloat($("#" + thisType + "_taxes_" + thisId).html().replace("{{ currencySymbol }}",""));
					var refundable = total + taxes;
				{% else %}
					var refundable = total;
				{% endif %}

				if (thisRefund != refundable) {
					alertFlag = true;
				}

			});

			if (alertFlag == true) {
				if (confirm("{% trans 'Cancelled classes are not fully refunded. Confirm that this is desired?' %}")) {
					$('#refund_form').submit();
				}
				else {
					event.preventDefault();
				}
			}
			else {
				$('#refund_form').submit();
			}
		});

	});
</script>
{% endaddtoblock %}

{% endblock %}
