{extends file="layout.html"}
{block name="title"}LMS: {$layout.pagetitle|striphtml}{/block}
{block name="module_content"}
	{$default_currency = Localisation::getDefaultCurrency()}
	<style>

		#ksef-invoices > * > tr > td {
			padding-left: 1em;
			padding-right: 1em;
		}

		#ksef-invoices tbody {
			border-bottom-style: none;
		}

		#ksef-invoices tfoot {
			border-top-style: none;
		}

		#ksef-invoices tfoot > tr {
			border-top-style: none;
		}

		#ksef-invoices tfoot td:first-child {
			padding-right: 0;
		}

		#ksef-invoices pre {
			margin-block: 0;
			font-style: italic;
		}

		#ksef-invoices .values {
			font-style: italic;
		}

		#ksef-invoices .order-toggle:hover,
		#ksef-invoices .order-toggle:active {
			text-decoration: none;
			color: inherit;
		}

		#ksef-invoices .order-toggle .sort-order-icon.hidden {
			display: none;
		}

		#ksef-invoices .blend .lms-ui-hint-toggle i::before {
			color: inherit !important;
		}

		#ksef-invoices .blend .red,
		#ksef-invoices .blend .about-to-expire {
			color: inherit !important;
		}

		#ksef-invoices .about-to-expire {
			color: darkorange !important;
		}

		#filter-panel {
			display: flex;
			flex-grow: 1;
			width: 100%;
			flex-direction: column;
		}

		#filter-panel .filter-panel-row {
			display: flex;
			justify-content: flex-start;
		}

		#ksef-invoices > thead > tr:not(:first-child) {
			border-top: none;
		}

		#ksef-invoices .division-panel-row > * {
			white-space: nowrap;
		}

		#ksef-invoices .seller {
			min-width: 22em;
		}

		#ksef-invoices .buyer {
			min-width: 20em;
		}

		#ksef-invoices .bank-account {
			min-width: 20em;
			text-align: right;
		}

		#empty-table-row {
			border-bottom-style: solid;
		}

		[data-action].hidden {
			display: none;
		}

		td > [data-action] {
			margin-left: 0.2em !important;
			margin-right: 0.2em !important;
		}

		td > .lms-ui-link-button {
			margin-left: 0.2em !important;
			margin-right: 0.2em !important;
		}

		#temporary-table {
			display: none;
		}

		#ksef-invoices .notes-info.hidden {
			display: none;
		}

		#ksef-invoices .ksef-invoice-tags > * {
			margin-left: 0.1em;
			margin-right: 0.1em;
			margin-top: 0.1em;
			margin-bottom: 0.1em;
			padding-left: 0.2em;
			padding-right: 0.2em;
			padding-top: 0.3em;
			padding-bottom: 0.3em;
			background-color: #CEBD9B;
			background-image: none;
			font-weight: bold;
			border: 1px solid #615847;
			border-radius: 4px;
			box-sizing: border-box;
			display: inline-block;
			position: relative;
			max-width: 100%;
			overflow: hidden;
			text-overflow: ellipsis;
			vertical-align: bottom;
			white-space: nowrap;
		}

		#ksef-invoices .ksef-invoice-tags > .ksef-invoice-tag-name-edit {
			display: none;
			padding-left: 0;
			padding-right: 0;
			padding-top: 0;
			padding-bottom: 0;
		}

		#ksef-invoices .ksef-invoice-tags > .ksef-invoice-tag-name-edit > input {
			padding-left: 0.2em;
			padding-right: 0.2em;
			padding-top: 0.3em;
			padding-bottom: 0.3em;
		}

		#ksef-invoices .filter-panel-row:nth-child(2) > *:not(:first-child) {
			margin-left: 0.5em;
		}

	</style>

	<script>

		$(function() {
			function sortByProperty(property, order) {
				const rows = $('#ksef-invoices > tbody > tr[data-id]').get();

				rows.sort(function (a, b) {
					var valueA = $(a).attr('data-' + property);
					var valueB = $(b).attr('data-' + property);
					var idA = parseInt($(a).attr('data-id'));
					var idB = parseInt($(b).attr('data-id'));

					if (property === 'issue-date') {
						valueA = parseInt(valueA);
						valueB = parseInt(valueB);
					} else if (property === 'gross-value') {
						valueA = parseFloat(valueA);
						valueB = parseFloat(valueB);
					}

					if (valueA === valueB) {
						return order === 'asc'
							? idA - idB
							: idB - idA;
					} else {
						return order === 'asc'
							? valueA - valueB
							: valueB - valueA;
					}
				});

				$.each(rows, function (_, row) {
					$('#ksef-invoices > tbody').append(row);
				});
			}

			$('[data-order-property]').click(function() {
				var orderProperty = $(this).attr('data-order-property');
				var sortOrderIcon = $(this).find('.sort-order-icon');
				if (!sortOrderIcon.is('.lms-ui-icon-asc-order,.lms-ui-icon-desc-order')) {
					sortOrderIcon.addClass('lms-ui-icon-asc-order');
				} else {
					sortOrderIcon.toggleClass('lms-ui-icon-asc-order').toggleClass('lms-ui-icon-desc-order');
				}
				$('#ksef-invoices .order-toggle:not([data-order-property="' + orderProperty + '"])').find('.sort-order-icon').addClass('hidden');
				sortOrderIcon.removeClass('hidden');

				var sortOrder = orderProperty + ' ' + (sortOrderIcon.is('.lms-ui-icon-asc-order') ? 'asc' : 'desc');
				setStorageItem('ksef-purchase-invoices-sort-order', sortOrder, 'session')
				sortByProperty(orderProperty, sortOrderIcon.is('.lms-ui-icon-asc-order') ? 'asc' : 'desc');
			});

			function restoreSortOrder() {
				var sortOrder = getStorageItem('ksef-purchase-invoices-sort-order', 'session');

				if (!sortOrder) {
					sortOrder ='{ConfigHelper::getConfig('ksef.purchase_invoice_default_order', 'issue-date asc')}';
					if (sortOrder.match(/^(issue-date|gross-value|pay-date)(,|\s+)(asc|desc)$/)) {
						sortOrder.replace(',', ' ');
					} else {
						sortOrder = 'issue-date asc';
					}
				}

				var sortParts = sortOrder.split(' ');
				sortByProperty(sortParts[0], sortParts[1]);

				return sortParts;
			}

			function recalculateSummary() {
				var totalNetValue = 0.0;
				var totalVatValue = 0.0;
				var totalGrossValue = 0.0;
				var invoices = $('#ksef-invoices > tbody > tr[data-net-value]');
				invoices.each(function() {
					var currencyValue = parseFloat($(this).attr('data-currency-value'));
					totalNetValue += parseFloat($(this).attr('data-net-value')) * currencyValue;
					totalVatValue += parseFloat($(this).attr('data-vat-value')) * currencyValue;
					totalGrossValue += parseFloat($(this).attr('data-gross-value')) * currencyValue;
				});

				var summary = $('#ksef-invoices > tfoot');
				summary.toggle(invoices.length > 0);
				summary.find('#total-net-value').text(sprintf("%.2f %s", totalNetValue, '{$default_currency}').replace('.', ','));
				summary.find('#total-vat-value').text(sprintf("%.2f %s", totalVatValue, '{$default_currency}').replace('.', ','));
				summary.find('#total-gross-value').text(sprintf("%.2f %s", totalGrossValue, '{$default_currency}').replace('.', ','));
			}

			function applyFilter(filter) {
				if (typeof(filter) !== 'object') {
					var filter = {};
					var divisionId = getStorageItem('ksef-purchase-invoices-division-id', 'session');
					filter.divisionId = divisionId ? divisionId : '';
					var posting = getStorageItem('ksef-purchase-invoices-posting', 'session');
					filter.posting = posting && posting.length ? posting : '';
					var settled = getStorageItem('ksef-purchase-invoices-settled', 'session');
					filter.settled = settled && settled.length ? settled : '';
					var sellerTen = getStorageItem('ksef-purchase-invoices-seller-ten', 'session');
					filter.sellerTen = sellerTen ? sellerTen : '';
				}

				var ksefInvoices = $('#ksef-invoices > tbody');
				var temporaryTable = $('#temporary-table > tbody');
				var filterSelector = [];

				if (filter.hasOwnProperty('divisionId')) {
					if (filter.divisionId.length) {
						filterSelector.push('[data-division-id="' + filter.divisionId + '"]');
					} else {
						filterSelector.push('[data-division-id]');
					}
				} else {
					filterSelector.push('[data-division-id]');
				}

				if (filter.hasOwnProperty('posting')) {
					var posting = filter.posting;
					if (posting.length) {
						filterSelector.push('[data-posting="' + posting + '"]');
					} else {
						filterSelector.push('[data-posting]');
					}
				} else {
					filterSelector.push('[data-posting]');
				}

				if (filter.hasOwnProperty('settled')) {
					var settled = filter.settled;
					if (settled.length) {
						filterSelector.push('[data-settled="' + settled + '"]');
					} else {
						filterSelector.push('[data-settled]');
					}
				} else {
					filterSelector.push('[data-settled]');
				}

				if (filter.hasOwnProperty('sellerTen')) {
					var sellerTen = filter.sellerTen;
					if (sellerTen.length) {
						filterSelector.push('[data-seller-ten="' + sellerTen + '"]');
					} else {
						filterSelector.push('[data-seller-ten]');
					}
				} else {
					filterSelector.push('[data-seller-ten]');
				}

				$(temporaryTable).find('tr' + filterSelector.join('')).appendTo(ksefInvoices);

				$(temporaryTable).append(ksefInvoices.find('tr[data-id]').filter(':not(' + filterSelector.join('') + '):not(#empty-table-row)'));

				var emptyTable = !ksefInvoices.find('[data-id]').length;
				$('#empty-table-row').appendTo(emptyTable ? ksefInvoices : temporaryTable);

				restoreSortOrder();
				recalculateSummary();

				return filter;
			}

			$('#clear-filter').click(function() {
				$.each(
					[
						'ksef-purchase-invoices-division-id',
						'ksef-purchase-invoices-posting',
						'ksef-purchase-invoices-settled',
						'ksef-purchase-invoices-seller-ten'
					],
					function(_, itemName) {
						removeStorageItem(itemName, 'session');
					}
				);

				$('#division-id').val('');

				$('#posting').prop('checked', false);

				$('#unsettled').prop('checked', false);

				$('#seller-ten').val('');
				updateAdvancedSelectsTest('#seller-ten');

				applyFilter();
			});

			$('#division-id').change(function() {
				var filter = {
					divisionId: $(this).val(),
					posting: $('#posting').prop('checked') ? '1' : '',
					settled: $('#unsettled').prop('checked') ? '0' : '',
					sellerTen: $('#seller-ten').val()
				}

				applyFilter(filter);

				setStorageItem('ksef-purchase-invoices-division-id', filter.divisionId, 'session');
			});

			$('#posting').change(function() {
				var filter = {
					posting: $(this).prop('checked') ? '1' : '',
					divisionId: $('#division-id').val(),
					settled: $('#unsettled').prop('checked') ? '0' : '',
					sellerTen: $('#seller-ten').val()
				};

				applyFilter(filter);

				setStorageItem('ksef-purchase-invoices-posting', filter.posting, 'session');
			});

			$('#unsettled').change(function() {
				var filter = {
					posting: $('#posting').prop('checked') ? '1' : '',
					divisionId: $('#division-id').val(),
					settled: $('#unsettled').prop('checked') ? '0' : '',
					sellerTen: $('#seller-ten').val()
				};
				applyFilter(filter);


				setStorageItem('ksef-purchase-invoices-settled', filter.settled, 'session');
			});

			$('#seller-ten').change(function() {
				var filter = {
					divisionId: $('#division-id').val(),
					posting: $('#posting').prop('checked') ? '1' : '',
					settled: $('#unsettled').prop('checked') ? '0' : '',
					sellerTen: $(this).val()
				}

				applyFilter(filter);

				setStorageItem('ksef-purchase-invoices-seller-ten', filter.sellerTen, 'session');
			});

			var sortParts = restoreSortOrder();

			$('#ksef-invoices [data-order-property="' + sortParts[0] + '"]')
				.find('.sort-order-icon')
				.toggleClass('lms-ui-icon-asc-order', sortParts[1] === 'asc')
				.toggleClass('lms-ui-icon-desc-order', sortParts[1] === 'desc')
				.removeClass('hidden');

			var filter = applyFilter();

			if (filter.divisionId) {
				$('#division-id').val(filter.divisionId);
			}

			if (filter.posting.length && filter.posting === '1') {
				$('#posting').prop('checked', true);
			}

			if (filter.settled.length && filter.settled === '0') {
				$('#unsettled').prop('checked', true);
			}

			if (filter.sellerTen) {
				$('#seller-ten').val(filter.sellerTen);
				updateAdvancedSelectsTest('#seller-ten');
			}

			$('[data-action]').click(function() {
				var that = $(this);
				var form = $('#ksef-purchase-invoice-edit-form');
				var action = $(this).attr('data-action');
				form.find('[name="action"]').val(action);
				form.find('[name="id"]').val($(this).closest('[data-id]').attr('data-id'));

				let formData = new FormData(form.get(0));

				$.ajax({
					url: '?m=ksefpurchaseinvoiceedit&api=1',
					type: 'POST',
					data: formData,
					processData: false,
					contentType: false,
					dataType: "json",
					success: function(data) {
						if (data.hasOwnProperty('error')) {
							console.error('Error:', data.error);
							return;
						}
						$(that).hide();

						switch (action) {
							case 'ignore':
								$(that).siblings('[data-action="restore"]').show();
								$(that).closest('[data-id]').addClass('cancel').attr('data-posting', '0');
								applyFilter();
								break;
							case 'restore':
								$(that).siblings('[data-action="ignore"]').show();
								$(that).closest('[data-id]').removeClass('cancel').attr('data-posting', '1');
								applyFilter();
								break;
							case 'settle':
								$(that).siblings('[data-action="unsettle"]').show();
								$(that).closest('[data-id]').addClass('blend').attr('data-settled', '1');
								applyFilter();
								break;
							case 'unsettle':
								$(that).siblings('[data-action="settle"]').show();
								$(that).closest('[data-id]').removeClass('blend').attr('data-settled', '0');
								applyFilter();
								break;
						}
					},
					error: function(xhr) {
						console.error('Error:', xhr.responseText);
					}
				});
			});

			$(document).click(function(e) {
				if (!$(e.target).closest('[data-tag-id]').length
					&& !$(e.target).closest('.lms-ui-button').length) {
					$('#ksef-invoices .ksef-invoice-tag-name-edit').hide().siblings('.ksef-invoice-tag-name').show();
				}
			});

			$('.ksef-invoice-tags').on('click', '.ksef-invoice-tag-name', function(e) {
				var that = $(this);
				var tagId = that.attr('data-tag-id');
				var tagName = that.text();
				e.preventDefault();
				$('#ksef-invoices .ksef-invoice-tag-name').show();
				that.hide();
				$('#ksef-invoices .ksef-invoice-tag-name-edit').hide();
				that.siblings('.ksef-invoice-tag-name-edit[data-tag-id="' + tagId + '"]').show().find('input').val(tagName).focus();
			}).on('keydown', '.ksef-invoice-tag-name-edit', function(e) {
				var that = $(this);
				switch (e.key) {
					case 'Enter':
						var input = that.find('input');
						var tagId = that.attr('data-tag-id')
						var tagName = input.val();
						$.ajax({
							url: '?m=ksefpurchaseinvoiceedit&api=1',
							type: 'POST',
							data: {
								action: 'rename-tag',
								id: tagId,
								name: tagName
							},
							dataType: "json",
							success: function(data) {
								if (data.hasOwnProperty('error')) {
									console.error('Error:', data.error);
									return;
								}

								$('#ksef-invoices .ksef-invoice-tag-name[data-tag-id="' + tagId + '"]').text(tagName);

								that.hide().siblings('.ksef-invoice-tag-name').show();
							}
						});
						break;
					case 'Escape':
						that.hide().siblings('.ksef-invoice-tag-name').show();
						break;
				}
			});
		});

	</script>

	<h1>{$layout.pagetitle}</h1>

	<form name="ksef-invoices-form" id="ksef-invoices-form" method="POST">
	</form>

	<form name="ksef-purchase-invoice-edit-form" id="ksef-purchase-invoice-edit-form" method="POST">
		<input type="hidden" name="id">
		<input type="hidden" name="action">
	</form>

	<table class="lmsbox lms-ui-background-cycle" id="ksef-invoices">
		<colgroup>
			<col style="width: 1%;">
			<col style="width: 1%;">
			<col style="width: 1%;">
			<col style="width: 1%;">
			<col style="width: 1%;">
			<col style="width: 1%;">
			<col style="width: 1%;">
			<col style="width: 97%;">
		</colgroup>
		<thead>
			<tr>
				<td colspan="8">
					<div id="filter-panel">
						<div class="filter-panel-row">
							<span class="lms-ui-date-period-container">
								<strong>{trans("Period")}</strong>
								<span>{trans("from")}</span>
								<input type="text" name="start-date" id="start-date" size="10" maxlength="10"
									value="{$start_date|date_format:"Y/m/d"}"
									form="ksef-invoices-form"
									placeholder="{trans("yyyy/mm/dd")}"
									{tip class="lms-ui-date" text="Enter date in 'yyyy/mm/dd' format or click to choose it from calendar"}>
								<span>{trans("do")}</span>
								<input type="text" name="end-date" id="end-date" size="10" maxlength="10"
									value="{$end_date|date_format:"Y/m/d"}"
									form="ksef-invoices-form"
									placeholder="{trans("yyyy/mm/dd")}"
									{tip class="lms-ui-date" text="Enter date in 'yyyy/mm/dd' format (empty field means current date) or click to choose it from calendar"}>
								{date_period_preset from="#start-date" to="#end-date"}
								{button type="submit" icon="apply" label="Apply" id="period-apply-button" form="ksef-invoices-form"}
							</span>
						</div>
						<div class="filter-panel-row">
							{button type="link" icon="delete" id="clear-filter"}
							<label>
								<strong>{trans("Division")}<strong>
								<select name="division-id" id="division-id" form="ksef-invoices-form">
									<option value="">{trans("— all —")}</option>
									{foreach $divisions as $division}
										<option value="{$division.id}">{$division.label|escape}</option>
									{/foreach}
								</select>
							</label>
							{if !empty($sellers)}
								<label>
									<strong>{trans("<!ksef>Seller")}</strong>
									<select name="seller-ten" id="seller-ten" form="ksef-invoices-form" class="lms-ui-advanced-select-test">
										<option value="">{trans("— all —")}</option>
										{foreach $sellers as $seller}
											<option value="{$seller.ten}">{t a=$seller.name|escape b=$seller.ten}<!ksef>$a (TEN $b){/t}</option>
										{/foreach}
									</select>
								</label>
							{/if}
							<label>
								<input type="checkbox" name="posting" id="posting" form="ksef-invoices-form">
								{trans("<!ksef>posted")}
							</label>
							<label>
								<input type="checkbox" name="unsettled" id="unsettled" form="ksef-invoices-form">
								{trans("<!ksef>unsettled")}
							</label>
						</div>
					</div>
				</td>
			</tr>
			<tr>
				<td class="bold nobr">
					<a class="order-toggle" data-order-property="issue-date">
						{trans("Date")}
						{icon name="dunno" class="sort-order-icon hidden" fw=false}
					</a>
					<br>
					{trans("<!ksef>Document type/number")}
					<br>
					{trans("KSeF number")}
					<br>
					{trans("<!ksef>Tags")}
				</td>
				<td class="bold nobr">
					{trans("<!ksef>Seller name")}
					<br>
					{trans("<!ksef>Seller TEN")}
				</td>
				<td class="bold nobr">
					{trans("Division")}
					<br>
					{trans("<!ksef>Buyer name")}
				</td>
				<td class="bold nobr text-right">
					{trans("<!ksef>Net value")}
					<br>
					{trans("<!ksef>VAT value")}
					<br>
					<a class="order-toggle" data-order-property="gross-value">
						{$class = ""}
						{if $order == "gross_value asc"}
							{$icon_name = "asc-order"}
						{elseif $order == "gross_value desc"}
							{$icon_name = "desc-order"}
						{else}
							{$icon_name = "asc-order"}
							{$class="hidden"}
						{/if}
						{icon name="dunno" class="sort-order-icon hidden" fw=false}
						{trans("<!ksef>Gross value")}
					</a>
				</td>
				<td class="bold nobr text-right">
					{trans("<!ksef>Bank name")}
					<br>
					{trans("<!ksef>Bank account")}
				</td>
				<td class="bold nobr text-center">
					{trans("<!ksef>Payment type")}
					<br>
					<a class="order-toggle" data-order-property="pay-date">
						{trans("<!ksef>Payment date")}
						{icon name="dunno" class="sort-order-icon hidden" fw=false}
					</a>
				</td>
				<td colspan="2">
				</td>
			</tr>
		</thead>
		<tbody>
			{foreach $invoices as $invoice}
				<tr data-issue-date="{$invoice.issue_date}"
					data-currency-value="{$invoice.currency_value}"
					data-net-value="{$invoice.net_amount}"
					data-vat-value="{$invoice.vat_amount}"
					data-gross-value="{$invoice.gross_amount}"
					data-pay-date="{$invoice.pay_date}"
					data-id="{$invoice.id}"
					data-division-id="{$invoice.division_id}"
					data-posting="{if empty($invoice.posting)}0{else}1{/if}"
					data-settled="{if empty($invoice.settled)}0{else}1{/if}"
					data-seller-ten="{$invoice.seller_ten}"
					class="{if empty($invoice.posting)} cancel{/if}{if !empty($invoice.settled)} blend{/if}">
					<td>
						<span class="nobr">
							<strong>{$invoice.issue_date|date_format:"Y/m/d"}</strong>
						</span>
						<br>
						<span class="nobr">
							{capture assign="document_type_number"}{strip}
								{$invoice.invoice_type_name}&nbsp;{$invoice.invoice_number}
							{/strip}{/capture}
							{$document_type_number}
							{*icon name="copy" class="lms-ui-button-clipboard fa-fw" data_clipboard_text=$document_type_number*}
							{icon name="copy" class="lms-ui-button-clipboard fa-fw" data_clipboard_text=$invoice.invoice_number}
						</span>
						<pre class="nobr">{$invoice.ksef_number}</pre>
						<span class="ksef-invoice-tags">{strip}
							{if !empty($invoice.tags)}
								{foreach $invoice.tags as $tagId => $tagName}
									<span class="ksef-invoice-tag-name" data-tag-id="{$tagId}">{$tagName|escape}</span>
									<span class="ksef-invoice-tag-name-edit" data-tag-id="{$tagId}"><input type="text" size="35" value="{$tagName|escape}"></span>
								{/foreach}
							{/if}
						{/strip}</span>
					</td>
					<td class="seller">
						<strong>{$invoice.seller_name|escape}</strong>
						<br>
						{$invoice.seller_ten}
					</td>
					<td class="buyer">
						<a href="?m=divisionedit&id={$invoice.division_id}">
							{if empty($invoice.division_label)}
								{$invoice.division_shortname|escape}
							{else}
								{$invoice.division_label|escape}
							{/if}
						</a>
						<br>
						{$invoice.buyer_name|escape}
					</td>
					<td class="text-right nobr values">
						{moneyf($invoice.net_amount, $invoice.currency)}
						<br>
						{icon name="copy" class="lms-ui-button-clipboard fa-fw" data_clipboard_text=$invoice.vat_amount|string_format:"%.02f"}
						{moneyf($invoice.vat_amount, $invoice.currency)}
						<br>
						{icon name="copy" class="lms-ui-button-clipboard fa-fw" data_clipboard_text=$invoice.gross_amount|string_format:"%.02f"}
						{moneyf($invoice.gross_amount, $invoice.currency)}
					</td>
					<td class="bank-account">
						{if empty($invoice.bank_name)}
							—
						{else}
							{$invoice.bank_name|escape}
						{/if}
						<br>
						{if empty($invoice.bank_account)}
							—
						{else}
							{$bank_account = preg_replace('/[^0-9]/', '', $invoice.bank_account)}
							{icon name="copy" class="lms-ui-button-clipboard fa-fw" data_clipboard_text=$bank_account}
							{format_bankaccount($bank_account)}
						{/if}
					</td>
					<td class="nobr text-center">
						{if empty($invoice.pay_type)}
							{trans("<!ksef>Paid")}
							<br>
							{if empty($invoice.pay_date)}
								—
							{else}
								<strong>
									{$invoice.pay_date|date_format:"Y/m/d"}
								</strong>
							{/if}
						{else}
							{$invoice.pay_type_name}
							<br>
							{capture assign="pay_date"}{strip}
								<span class="bold{if $invoice.expired} red{elseif $invoice.about_to_expire} about-to-expire{/if}">
									{$invoice.pay_date|date_format:"Y/m/d"}
									{if !empty($invoice.days_to_expire)}
										<br>
										({t a=$invoice.days_to_expire}$a days{/t})
									{/if}
								</span>
							{/strip}{/capture}
							{$pay_date}
						{/if}
					</td>
					<td>
						{if empty($invoice.notes)}
							{$class = "notes-info hidden"}
						{else}
							{$class = "notes-info"}
						{/if}
						{hint mode=LMSSmartyPlugins::HINT_TYPE_ROLLOVER icon="more-info" text=$invoice.notes|nl2br class=$class}
					</td>
					<td class="text-left nobr">
						{hint url="?m=ksefpurchaseinvoicenotes&id={$invoice.id}" icon="info" class="lms-ui-link-button notes-button" tip="Invoice textual description"}
						{hint url="?m=ksefpurchaseinvoicetags&id={$invoice.id}" icon="tags" class="lms-ui-link-button tag-button" tip="Invoice tag management"}
						{if empty($invoice.itemcount)}
							{$disabled = true}
						{else}
							{$disabled = false}
						{/if}
						{hint url="?m=ksefpurchaseinvoiceinfo&id={$invoice.id}" icon="view" class="lms-ui-link-button" disabled=$disabled tip="Show invoice items"}
						{if empty($invoice.bank_account)}
							{$disabled = true}
						{else}
							{$disabled = false}
						{/if}
						{hint url="?m=invoiceksefinfo&id={$invoice.id}&purchase=1" icon="print" class="lms-ui-link-button" tip="<!ksef>Document details"}
						{hint url="?m=ksefpurchaseinvoiceinfo&id={$invoice.id}&qr2pay=1" icon="qrcode" class="lms-ui-link-button" disabled=$disabled tip="Show QR2Pay code"}
						{$posting = !empty($invoice.posting)}
						{if $posting}
							{$restore_button_class = "hidden"}
							{$ignore_button_class = ""}
						{else}
							{$restore_button_class = ""}
							{$ignore_button_class = "hidden"}
						{/if}
						{button type="button" icon="restore" tip="<!ksef>Post document" data-action="restore" class=$restore_button_class}
						{button type="button" icon="ignore" tip="<!ksef>Don't post document" data-action="ignore" class=$ignore_button_class}
						{$settled = !empty($invoice.settled)}
						{if $settled}
							{$settle_button_class = "hidden"}
							{$unsettle_button_class = ""}
						{else}
							{$settle_button_class = ""}
							{$unsettle_button_class = "hidden"}
						{/if}
						{button type="button" icon="settle" tip="<!ksef>Settle document" data-action="settle" class=$settle_button_class}
						{button type="button" icon="open" tip="<!ksef>Unsettle document" data-action="unsettle" class=$unsettle_button_class}
					</td>
				</tr>
			{/foreach}
			<tr id="empty-table-row">
				<td colspan="8" class="empty-table">
					{trans("<!ksef>No purchase invoices matching filter criteria.")}
				</td>
			</tr>
		</tbody>
		<tfoot>
			<tr class="dark">
				<td colspan="3" class="text-right">
					<strong>{trans("Total")}</strong>
				</td>
				<td class="text-right nobr values">
					<span id="total-net-value"></span>
					<br>
					<span id="total-vat-value"></span>
					<br>
					<span id="total-gross-value"></span>
				</td>
				<td colspan="4"></td>
			</tr>
		</tfoot>
	</table>
	<table id="temporary-table">
		<tbody>
		</tbody>
	</table>
{/block}
