<?php
/**
 * This is a core PHP file. For customization, create a file in the folder /custom/.
 * Files with the same filename in this folder will automatically be loaded instead of the current file
 */
?>
<div class="col-xl-9 col-lg-8">
<div class="main-content p-a-2" >

	<?php if(isset($message)){ echo $message; } ?>

	<h1><?php
		echo __('invoice') . ' ' . normalize($invoice_object->InvoiceCode);
		if($invoice_object->Authorisation == 'yes')
		{
			echo ' ' . (($invoice_object->Status == 4) ? __('direct debit paid invoice') : __('direct debit invoice'));
		}
		?>
	</h1>

	<div class="row m-t-1">

		<div class="col-sm-12">

			<a class="btn btn-secondary action-btn m-r" href="<?php echo __SITE_URL . '/' . __('invoice', 'url') . '/' . __('download', 'url') . '/' . $invoice_object->id; ?>">
				<span class="fas fa-print fa-2x"></span><br />
				<div><?php echo __('download invoice'); ?></div>
			</a>
			<?php
			if($invoice_object->Authorisation != 'yes' && ($invoice_object->Status == 2 || $invoice_object->Status == 3) && !empty(Settings_Model::$payment_methods))
			{
				?>
				<a class="btn btn-secondary action-btn" href="<?php echo __SITE_URL . '/' . __('invoice', 'url') . '/' . __('payOnline', 'url') . '/' . $invoice_object->id; ?>" target="_blank">
					<span class="fas fa-euro-sign fa-2x"></span>
					<br/>
					<div><?php echo __('pay invoice'); ?></div>
				</a>
				<?php
			}
			?>
		</div>
	</div>

	<h2><?php echo __('invoice'); ?></h2>

	<div class="row">

		<div class="col-md-6 col-sm-12 m-b-3">
			<?php echo show_client_address($invoice_object); ?>
		</div>

		<div class="col-md-6 col-sm-12 m-b-2">

			<dl class="dl-horizontal">
				<dt class="col-sm-5"><?php echo __('invoice date'); ?></dt>
				<dd class="col-sm-7"><?php echo rewrite_date_db2site($invoice_object->Date); ?></dd>
				<?php
				if($invoice_object->Status == 2 || $invoice_object->Status == 3)
				{
					?>
					<dt class="col-sm-5"><?php echo __('due date'); ?></dt>
					<dd class="col-sm-7"><?php echo rewrite_date_db2site($invoice_object->PayBefore); ?></dd>
					<?php
				}
				?>
				<dt class="col-sm-5"><?php echo __('status'); ?></dt>
				<dd class="col-sm-7"><?php echo __($invoice_object->Status, 'array_invoicestatus') . (($invoice_object->Status == 4) ? ' (' . rewrite_date_db2site($invoice_object->PayDate) . ')' : ''); ?></dd>

				<?php
				if($invoice_object->ReferenceNumber)
				{
					?>
					<dt class="col-sm-5"><?php echo __('reference number'); ?></dt>
					<dd class="col-sm-7"><?php echo normalize($invoice_object->ReferenceNumber); ?></dd>
					<?php
				}
				?>
			</dl>
		</div>

		<?php
		if(!empty($invoice_object->Attachments))
		{
			?>
			<div class="col-sm-12 m-b-3">
				<strong><?php echo __('attachments'); ?>:</strong><br />
				<?php
				foreach($invoice_object->Attachments as $_attachment)
				{
					?>
					<a class="p-r-2 text-nowrap text-truncate" href="<?php echo __SITE_URL . '/' . __('invoice', 'url') . '/' . __('downloadAttachment', 'url') . '/' . $invoice_object->id . '/' . $_attachment['Identifier']; ?>">
						<span class="fas fa-file"></span> <?php echo normalize($_attachment['Filename']); ?>
					</a>
					<br class="hidden-sm-up" />
					<?php
				}
				?>
			</div>
			<?php
		}
		?>
	</div>

	<h2 class="hidden-sm-up"><?php echo __('invoice lines'); ?></h2>

	<div class="row small-gutter strong hidden-xs-down">
		<div class="col-sm-2 hidden-sm-down"><?php echo __('date') ?></div>
		<div class="col-sm-2 col-md-1"><?php echo __('quantity') ?></div>
		<div class="col-sm-6 col-md-6"><?php echo __('description') ?></div>
		<div class="col-sm-4 col-md-3 text-xs-right"><?php echo ($invoice_object->VatCalcMethod == 'incl') ? __('amountIncl') : __('amountExcl'); ?></div>
	</div>

	<hr />

	<?php

	if(!empty($invoice_object->InvoiceLines))
	{
		foreach($invoice_object->InvoiceLines as $k => $v)
		{
			if(is_numeric($k))
			{
				/** INVOICE LINE */
				?>
				<div class="row small-gutter m-t-1">

					<div class="col-sm-2 col-xs-8 hidden-sm-down">
						<?php
						if(!($v['PriceExcl'] === '' || $v['PriceExcl'] == 0) || $v['ProductCode'])
						{
							echo rewrite_date_db2site($v['Date']);
						}
						?>
					</div>

					<div class="col-sm-2 col-md-1 hidden-xs-down">
						<?php
						if(!($v['PriceExcl'] === '' || $v['PriceExcl'] == 0) || $v['ProductCode'])
						{
							echo showNumber($v['Number']) . ' ' . normalize($v['NumberSuffix']);
						}
						?>
					</div>

					<div class="col-sm-7 col-xs-8">
						<?php
						if($v['Number'] != 1 || $v['NumberSuffix'])
						{
							// only show amount on mobile if it is not one or has a suffix
							$suffix = ($v['NumberSuffix']) ? normalize($v['NumberSuffix']) : 'x';
							echo '<span class="hidden-sm-up">' . showNumber($v['Number']) . ' ' . $suffix . ' &nbsp;</span>';
						}

						echo (trim($v['Description'])) ? nl2br(parseHTMLTags(normalize($v['Description']))) : '&nbsp;';

						if((!($v['PriceExcl'] === '' || $v['PriceExcl'] == 0) || $v['ProductCode']) && $v['Periodic'] != '')
						{
							echo '<br/><i>';
							echo '<span class="hidden-sm-down">' . __('period') . ': </span>';
							echo rewrite_date_db2site($v['StartPeriod']);
							echo ' ' . __('till') . ' ';
							echo rewrite_date_db2site($v['EndPeriod']);
							echo '<span class="hidden-sm-down"> (' . $v['Periods'] . ' ' . __($v['Periodic'], (($v['Periods'] > 1) ? 'array_periodic_multi' : 'array_periodic')) . ')</span>';
							echo '</i>';
						}
						?>
					</div>

					<div class="col-sm-3 col-md-2 col-xs-4">
						<?php
						if(!($v['PriceExcl'] === '' || $v['PriceExcl'] == 0) || $v['ProductCode'])
						{
							?>
							<div class="row small-gutter">
								<div class="col-xs-2 hidden-xs-down">
									<?php echo Settings_Model::get('CURRENCY_SIGN_LEFT'); ?>
								</div>
								<div class="col-xs-10 text-xs-right text-nowrap">
									<span class="hidden-sm-up">
										<?php
											echo Settings_Model::get('CURRENCY_SIGN_LEFT') . ' ';
										?>
									</span>
									<?php
										echo money(($invoice_object->VatCalcMethod == 'incl') ? $v['NoDiscountAmountIncl'] : $v['NoDiscountAmountExcl'], FALSE);
										if(Settings_Model::get('CURRENCY_SIGN_RIGHT'))
										{
											echo ' ' . Settings_Model::get('CURRENCY_SIGN_RIGHT');
										}
									?>
								</div>
							</div>

							<?php
						}
						?>
					</div>
				</div>

				<?php
				/** DISCOUNT LINE */
				if($v['DiscountPercentage'] > 0)
				{
					?>
					<div class="row small-gutter m-t-0">

						<div class="col-sm-2 col-md-3 hidden-xs-down">&nbsp;</div>

						<div class="col-sm-7 col-xs-8">
							<i><?php echo sprintf(__('discount on product'), showNumber(round($v['DiscountPercentage'], 2)) . '%'); ?></i>
						</div>

						<div class="col-sm-3 col-md-2 col-xs-4">
							<div class="row small-gutter">
								<div class="col-xs-2 hidden-xs-down">
									<?php echo Settings_Model::get('CURRENCY_SIGN_LEFT'); ?>
								</div>
								<div class="col-xs-10 text-xs-right text-nowrap">
									<span class="hidden-sm-up">
										<?php
										echo Settings_Model::get('CURRENCY_SIGN_LEFT') . ' ';
										?>
									</span>
									<?php
									echo money(($invoice_object->VatCalcMethod == 'incl') ? $v['DiscountAmountIncl'] : $v['DiscountAmountExcl'], FALSE);
									if(Settings_Model::get('CURRENCY_SIGN_RIGHT'))
									{
										echo ' ' . Settings_Model::get('CURRENCY_SIGN_RIGHT');
									}
									?>
								</div>
							</div>
						</div>
					</div>
					<?php
				}

				// dont show line on last invoice line
				if(count($invoice_object->InvoiceLines) < $key--)
				{
					?>
					<hr class="hidden-sm-up" />
					<?php
				}
			}
		}

		if($invoice_object->Discount > 0)
		{
			?>
			<div class="row small-gutter m-t-2">
				<div class="col-sm-2 col-md-3 hidden-xs-down">&nbsp;</div>

				<div class="col-sm-7 col-xs-8">
					<?php
					echo sprintf(__('discount on invoice'), showNumber($invoice_object->Discount) . '%');
					?>
				</div>

				<div class="col-sm-3 col-md-2 col-xs-4">
					<div class="row small-gutter">
						<div class="col-xs-2 hidden-xs-down">
							<?php echo Settings_Model::get('CURRENCY_SIGN_LEFT'); ?>
						</div>
						<div class="col-xs-10 text-xs-right text-nowrap">
							<span class="hidden-sm-up">
								<?php
									echo Settings_Model::get('CURRENCY_SIGN_LEFT') . ' ';
								?>
							</span>
							<?php
							echo money(($invoice_object->VatCalcMethod == 'incl') ? $invoice_object->AmountDiscountIncl : $invoice_object->AmountDiscount, FALSE);
							if(Settings_Model::get('CURRENCY_SIGN_RIGHT'))
							{
								echo ' ' . Settings_Model::get('CURRENCY_SIGN_RIGHT');
							}
							?>
						</div>
					</div>
				</div>
			</div>

			<?php
		}
		?>
		<div class="row small-gutter m-t-1">
			<div class="col-xs-7 col-sm-9 col-md-10"></div>
			<div class="col-xs-5 col-sm-3 col-md-2">
				<hr />
			</div>
		</div>

		<?php
		if(!empty(Settings_Model::$taxpercentages) || !empty(Settings_Model::$total_taxpercentages))
		{
			echo generate_amount_totals(__('amountExcl'), money($invoice_object->AmountExcl, FALSE));
		}

		asort(Settings_Model::$taxpercentages);

		foreach(Settings_Model::$taxpercentages as $key => $value)
		{
			if(isset($invoice_object->UsedTaxrates[(string)(float)$key]['AmountTax']) && (float)$key > 0)
			{
				$tax_label = (isset(Settings_Model::$taxpercentages_info[(string)(float)$key]['label'])) ? Settings_Model::$taxpercentages_info[(string)(float)$key]['label'] : '';
				echo generate_amount_totals(normalize($tax_label), money($invoice_object->UsedTaxrates[(string)(float)$key]['AmountTax'], FALSE));
			}
		}

		if(isset($invoice_object->TaxRate_Label) && $invoice_object->TaxRate_Label)
		{
			echo generate_amount_totals(normalize($invoice_object->TaxRate_Label), money($invoice_object->TaxRate_Amount, FALSE));
		}


		if(empty(Settings_Model::$taxpercentages) && empty($array_total_taxpercentages))
		{
			$total_label = '<strong>' . __('amount') . '</strong>';
		}
		else
		{
			$total_label = '<strong>' . __('amountIncl') . '</strong>';
		}
		echo generate_amount_totals($total_label, money($invoice_object->AmountIncl, FALSE));


		// When the invoice is only partly paid, show the open amount
		if($invoice_object->Status == '3')
		{
			echo '<br />';

			echo generate_amount_totals(__('already paid'), money($invoice_object->AmountPaid, FALSE));

			echo generate_amount_totals('<strong>' .__('open amount to be paid') . '</strong>', money($invoice_object->AmountIncl - $invoice_object->AmountPaid, FALSE));
		}
	}
	?>

</div>
</div>