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

{% block content %}

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

<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>
	{% if registration %}<dt>{% trans "Student" %}:</dt><dd>{{ registration.student|yesno }}</dd>{% endif %}
</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 registration 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>
				{% for occ in item.finalEventRegistration.event.eventoccurrence_set.all %}
					{{ occ.startTime|date:"M. jS" }{% if not forloop.last %}, {% endif %}
				{% endfor %}
			</td>
			<td id="item_price_{{ item.id }}">{{ currencySymbol }}{{ item.grossTotal|floatformat:2 }}</td>
			<td id="item_netPrice_{{ 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 />

<p>
	<a class="btn btn-secondary btn-sm" href="{% url 'registration' %}">{% trans "Class Registration Page" %}</a>
	<a class="btn btn-secondary btn-sm" href="{% url 'viewregistrations_selectevent' %}">{% trans "Select Another Event" %}</a>
</p>

{% addtoblock "js" %}
<script type="text/javascript">
	$(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 netPrice = parseFloat($("#" + thisType + "_netPrice_" + thisId).html().replace("{{ currencySymbol }}",""));
				{% if invoice.taxes > 0 and invoice.buyerPaysSalesTax %}
					var taxes = parseFloat($("#" + thisType + "_taxes_" + thisId).html().replace("{{ currencySymbol }}",""));
					var refundable = netPrice + taxes;
				{% else %}
					var refundable = netPrice;
				{% 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 netPrice = parseFloat($("#" + thisType + "_netPrice_" + thisId).html().replace("$",""));

				{% if invoice.taxes > 0 and invoice.buyerPaysSalesTax %}
					var taxes = parseFloat($("#" + thisType + "_taxes_" + thisId).html().replace("{{ currencySymbol }}",""));
					var refundable = netPrice + taxes;
				{% else %}
					var refundable = netPrice;
				{% 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 %}
