{% extends "base.html" %}

{% block title %}
	MOP Contracts &raquo; {{batch.contract.name}} &raquo;
 	Batches &raquo; {{batch.reference}} &raquo; Bill Imports &raquo;
 	{{importer_id}}
{% endblock %}

{% block nav %}
	<a href="/mop_contracts">MOP Contracts</a> &raquo;
	<a href="/mop_contracts/{{batch.contract.id}}">{{batch.contract.name}}</a>
 	&raquo;
	<a href="/mop_batches?mop_contract_id={{batch.contract.id}}">Batches</a> &raquo;
	<a href="/mop_batches/{{batch.id}}">{{batch.reference}}</a> &raquo;
 	Import {{importer_id}}
{% endblock %}

{% block content %}
	<!-- Layer Section -->
	<div class="layer-section">
		<!-- Section Header -->
		<div class="section-header">
			<h2>Bill Import</h2>	
		</div>
		<!-- End Section Header -->
		<!-- Section Content -->
		<div class="section-content">
			<div class="section-data">
				{% if is_alive %}
					<p>Still running. Refresh the page to see latest progress.</p>
				{% endif %}

				{% if status is defined %}
					<p>{{status}}</p>
				{% endif %}

				<ul>
					{% for msg in log %}
						<li>{{msg}}</li>
					{% endfor %}
				</ul>
				
				{% if failed_bills|length > 0 %}
					<table>
						<caption>Failed Bills</caption>
						<thead>
							<tr>
								{% for k, v in failed_bills[0]|dictsort %}
									<th>{{k}}</th>
								{% endfor %}
							</tr>
						</thead>
						<tbody>
							{% for bill in failed_bills %}
								<tr>
									{% for k, v in bill|dictsort %}
										<td>{{bill[k]}}</td>
									{% endfor %}
								</tr>
							{% endfor %}
						</tbody>
					</table>
				{% endif %}

				{% if successful_bills|length > 0 %}
					<table>
						<caption>Successful Bills</caption>
						<thead>
							<tr>
								<th>Reference</th>
								<th>Account</th>
								<th>Bill Type</th>
								<th>MPANs</th>
								<th>Issue Date</th>
								<th>Start Date</th>
								<th>Finish Date</th>
								<th>kWh</th>
								<th>Net</th>
								<th>VAT</th>
								<th>Gross</th>
								{% for i in range(successful_max_registers) %}
									<th>R{{loop.index}} MPAN</th>
									<th>R{{loop.index}} Meter Serial Number</th>
									<th>R{{loop.index}} Coefficient</th>
									<th>R{{loop.index}} Units</th>
									<th>R{{loop.index}} TPR</th>
									<th>R{{loop.index}} Previous Read Date</th>
									<th>R{{loop.index}} Previous Read Value</th>
									<th>R{{loop.index}} Previous Read Type</th>
									<th>R{{loop.index}} Present Read Date</th>
									<th>R{{loop.index}} Present Read Value</th>
									<th>R{{loop.index}} Present Read Type</th>
								{% endfor %}
								<th>Breakdown</th>
							</tr>
						</thead>
						<tbody>
							{% for bill in successful_bills %}
								<tr>
									<td>{{bill.reference}}</td>
									<td>{{bill.account}}</td>
									<td>{{bill.bill_type_code}}</td>
									<td>{{bill.mpans}}</td>
									<td>{{bill.issue_date|hh_format}}</td>
									<td>{{bill.start_date|hh_format}}</td>
									<td>{{bill.finish_date|hh_format}}</td>
									<td>{{bill.kwh}}</td>
									<td>{{bill.net}}</td>
									<td>{{bill.vat}}</td>
									<td>{{bill.gross}}</td>
									{% for read in bill.reads %}
										<td>{{read.mpan}}</td>
										<td>{{read.msn}}</td>
										<td>{{read.coefficient}}</td>
										<td>{{read.units}}</td>
										<td>{{read.tpr_code}}</td>
										<td>{{read.prev_date|hh_format}}</td>
										<td>{{read.prev_value}}</td>
										<td>{{read.prev_type_code}}</td>
										<td>{{read.pres_date|hh_format}}</td>
										<td>{{read.pres_value}}</td>
										<td>{{read.pres_type_code}}</td>
									{% endfor %}
									<td>{{bill.breakdown}}</td>
								</tr>
							{% endfor %}
						</tbody>
					</table>
				{% endif %}
			</div>
		</div>
	</div>
{% endblock %}
