{% extends "base.html" %}

{% block title %}
	&raquo; Supplier Contracts &raquo; {{batch.contract.name}} &raquo;
 	Batches &raquo; {{batch.reference}}
{% endblock %}

{% block nav %}
 	<a href="/e/supplier_contracts">Supplier Contracts</a> &raquo; 
	<a href="/e/supplier_contracts/{{batch.contract.id}}"
		>{{batch.contract.name}}</a> &raquo; 
	<a
	 		href="/e/supplier_batches?supplier_contract_id={{batch.contract.id}}"
			>Batches</a> &raquo; 
	{{batch.reference}}
	[<a href="/e/supplier_batches/{{batch.id}}/edit">edit</a>]
{% endblock %}

{% block content %}

	<table>
		<thead>
			<tr>
				<th>Reference</th>
				<th>Description</th>
				<th>Number Of Bills</th>
				<th>Sum net GBP</th>
				<th>Sum VAT GBP</th>
				<th>VAT Breakdown</th>
				<th>Sum gross GBP</th>
				<th>Sum kWh</th>
				<th>CSV Of Bills</th>
				<th>Custom Reports</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>{{batch.reference}}</td>
				<td>{{batch.description}}</td>
				<td>{{ "{:,}".format(num_bills) }}</td>
				<td>{{ "£{:,}".format(sum_net_gbp) }}</td>
				<td>{{ "£{:,}".format(sum_vat_gbp) }}</td>
				<td>
					<table style="margin-top: revert;">
						<thead>
							<th>VAT %</th>
							<th>Net GBP</th>
							<th>VAT GBP</th>
						</thead>
						<tbody>
							{% for k, v in vat_breakdown.items()|sort %}
								<tr>
									<td>{{k}}%</td>
									<td>{{ "£{:,}".format(v.net) }}</td>
									<td>{{ "£{:,}".format(v.vat) }}</td>
								</tr>
							{% endfor %}
						</tbody>
					</table>
				</td>
				<td>{{ "£{:,}".format(sum_gross_gbp) }}</td>
				<td>{{ "{:,}".format(sum_kwh) }}</td>
				<td><a href="/reports/bills?batch_id={{batch.id}}" >Download</a></td>
				<td>
					{% if batch_reports %}
						<ul>
							{% for report in batch_reports %}
								<li>
									<a href="/local_reports/{{report.id}}/output?batch_id={{batch.id}}"
										>{{report.name}}</a>
								</li>
							{% endfor %}
						</ul>
					{% endif %}
				</td>
			</tr>
		</tbody>
	</table>

	<table>
		<caption>
			Billing Files
			[<a href="/e/supplier_batches/{{batch.id}}/upload_file">upload</a>]
		</caption>
		<thead>
			<tr>
				<th>View</th>
				<th>Filename</th>
				<th>Upload Timestamp</th>
				<th>Parser Name</th>
			</tr>
		</thead>
		<tbody>
			{% for file in batch.files %}
				<tr>
					<td><a href="/e/supplier_batch_files/{{file.id}}">View</a></td>
					<td>{{file.filename}}</td>
					<td>{{file.upload_timestamp|hh_format}}</td>
					<td>{{file.parser_name}}</td>
				</tr>
			{% endfor %}
		</tbody>
	</table>

	<table>
		<caption>Imports</caption>
		<thead>
			<th>View</th>
			<th>Id</th>
		</thead>
		<tbody>
			{% for id in importer_ids %}
				<tr>
					<td><a href="/e/supplier_bill_imports/{{id}}">View</a></td>
					<td>{{id}}</td>
				</tr>
			{% endfor %}
		</tbody>
	</table>

	<h3>Actions</h3>

	<ul>
		<li>
			<form hx-post="/e/supplier_batches/{{batch.id}}"
					hx-confirm="Are you sure you want to import the bills from the files into this batch?"
			>
				<fieldset>
					<button name="import_bills">Import Bills</button>
				</fieldset>
			</form>
		</li>
		<li>
			<form hx-post="/e/supplier_batches/{{batch.id}}"
					hx-confirm="Are you sure you want to delete all the bills in this batch?"
			>
				<fieldset>
					<button name="delete_bills">Delete Bills</button>
				</fieldset>
			</form>
		</li>
		<li>
			<form hx-post="/e/supplier_batches/{{batch.id}}"
					hx-confirm="Are you sure you want to delete all the bills in this batch and then re-import bills from the files?"
			>
				<fieldset>
					<button name="delete_import_bills">Re-Import Bills</button>
				</fieldset>
			</form>
		</li>
	</ul>

	<form action="/reports/111">
		<fieldset>
			<input type="hidden" name="batch_id" value="{{batch.id}}">
			<legend>Download McBill Check</legend>
			<label>MPAN Cores</label>
			{{input_textarea(
				'mpan_cores', '', 5, 40,
				placeholder='One on each line, includes all if left blank')}}
			<input type="submit" value="Download">
		</fieldset>
	</form>

	<table class="sticky">
		<caption>Bills
			[<a href="/e/supplier_batches/{{batch.id}}/add_bill">add</a>]
		</caption>	
		<thead>
			<tr>
				<th>View</th>
				<th>Reference</th>
				<th>Account</th>
				<th>Issued</th>
				<th>From</th>
				<th>To</th>
				<th>kWh</th>
				<th>Net</th>
				<th>VAT</th>
				<th>Gross</th>
				<th>Type</th>
			</tr>
		</thead>
		<tbody>
			{% for bill in bills %}
				<tr>
					<td><a href="/e/supplier_bills/{{bill.id}}">View</a></td>
					<td>{{bill.reference}}</td>
					<td>{{bill.account}}</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>
					<td>
						<a
								href="/bill_types/{{bill.bill_type.id}}"
								title="{{bill.bill_type.description}}"
								>{{bill.bill_type.code}}</a>
					</td>
				</tr>
			{% endfor %}
		</tbody>
	</table>

{% endblock %}
