<?php foreach ($data->get('subs') as $index => $sub) { 
	if(empty($sub['sid']) && empty($sub['totals']['after_vat'])) { continue; }
?>
	<div class="break"></div>
	<div class="container">
		<div class="section section-invoice-head">
			<div class="table">
				<table>
					<tbody><tr>
							<td class="page-title"><h3 class="inline">INVOICE</h3></td>
							<td>
								<span class="label">Customer</span>
								<h3 class="inline"><?php echo $data->get('attributes')['firstname'] . ' ' . $data->get('attributes')['lastname']; ?></h3>
							</td>
							<td>
								<span class="label">Invoice#</span>
								<span><?php echo $data->get('invoice_id'); ?></span>
							</td>
							<td>
								<span class="label">Period</span>
								<h3 class="inline"><?php echo Billrun_Util::billrunKeyToPeriodSpan($data->get('billrun_key'),$span_date_format, !empty($data->get('invoicing_day')) ? $data->get('invoicing_day') : null); ?></h3>
							</td>
						</tr>
					</tbody></table>
			</div><!-- /.table -->
		</div><!-- /.section .section-invoice-head -->

		<div class="section section-invoice-base">
		<h2>Subscriber#<?php echo ($index + 1); ?>:</h2>
		<h1><?php echo $sub['firstname'] . ' ' . $sub['lastname']; ?></h1>
		<p class="label"><?php echo (!empty($sub['desc']) ? $sub['desc'] : ''); ?></p>
		</div><!-- /.section .section-invoice-base -->

		<div class="section section-invoice-details">
			<div class="section-head">
				<h2 class="section-title">Subscriber summary</h2><!-- /.section-title -->
			</div><!-- /.section-head -->

			<div class="section-body">
				<div class="table table-default table-default-primary">
					<table>
						<thead>
							<tr>
								<th>Description</th>
								<th>Amount</th>
							</tr>
						</thead>

						<tbody>
						<?php foreach ($details_keys as $title => $key) { ?>
							<?php if (empty($sub['totals'][$key])) { continue; } ?>
							<tr class="<?php echo (end($details_keys) === $key ? 'last' : '') ?>">
								<td><?php echo ucfirst($title); ?></td>
								<td><?php echo $this->currencySymbol() . number_format($sub['totals'][$key]['before_vat'], 2); ?></td>
							</tr>
						<?php } ?>
							<tr class="total">
								<td>SUBTOTAL</td>
								<td><strong><?php echo $this->currencySymbol() . number_format($sub['totals']['before_vat'], 2); ?></strong></td>
							</tr>
							<?php echo $this->render($this->tax_template,array('data' => $sub['totals']))	?>

							<tr class="grand-total">
								<td>GRAND TOTAL</td>
								<td><?php echo $this->currencySymbol() . number_format($sub['totals']['after_vat'], 2); ?></td>
							</tr>
						</tbody>
					</table>
				</div><!-- /.table table-default table-default-primary -->
			</div><!-- /.section-body -->
		</div><!-- /.section section-invoice-total -->
		
		<!---- break the page to the detail section -->
		<div class="break"></div>
	<?php if($render_usage_details) { ?>
		<div class="section section-invoice-details">
			<div class="section-head">
				<h2 class="section-title">Subscriber line items</h2><!-- /.section-title -->
			</div><!-- /.section-head -->

			<div class="section-body">
				<?php 
					$sidLines = array_filter($this->lines,function ($l) use ($sub) {return $l['sid'] == $sub['sid'];});
					$subFlatTabels = !empty($this->invoice_flat_tabels[$sub['sid']]) ? $this->invoice_flat_tabels[$sub['sid']] : [];
					if(!empty($sidLines)) {
						echo $this->render($this->details_table_template,array('lines'=> $sidLines,'lines_detail_types' => $flat_line_types, 'tabels' => $subFlatTabels )); 
					}
				?>
			</div><!-- /.section-body -->
		</div>
		
		<div class="section section-invoice-details">
			<div class="section-head">
				<h2 class="section-title">Usage details</h2><!-- /.section-title -->
			</div><!-- /.section-head -->

			<div class="section-body">
				<?php 
						if(!empty($sidLines)) {
							$subUsageTabels = !empty($this->invoice_usage_tabels[$sub['sid']]) ? $this->invoice_usage_tabels[$sub['sid']] : [];
							echo $this->render($this->details_table_template,array('lines'=> $sidLines,'lines_detail_types' => $usage_line_types, 'tabels' => $subUsageTabels) ); 
						}
				?>
			</div><!-- /.section-body -->
		</div>
	<?php } ?>
	</div>
<?php } ?>
