{extends file="layout.html"}
{block name=title}LMS: {$layout.pagetitle|striphtml}{/block}
{block name=module_content}
<!--// $Id$ //-->
<H1>{$layout.pagetitle}</H1>

<style>

	#button-panel {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
	}

	#button-panel > * {
		padding-top: 0.2em;
		padding-left: 0.2em;
	}

	#button-panel > :nth-child(n+2) {
		margin-left: 0.2em;
	}

	#duplicate-box {
		display: flex;
	}

	#duplicate-box > :nth-child(2) {
		padding-left: 0.8em;
	}

	.all-attachment-view {
		display: none;
		padding-right: 0.3em;
	}

	.document-list {
		display: flex;
		flex-direction: column;
	}

	.document-attachment-list {
		display: flex;
		flex-direction: column;
	}

	.document-title {
		text-decoration: underline dotted;
	}

	.document-title > a {
		text-decoration: underline dotted;
	}

</style>

<script>

	{if $invoice.invoice}
	window.open('?m=invoice&id={$invoice.invoice}&which={$invoice.which}');
	{/if}

	function filter()
	{
		document.page.action="?m=invoicelist{if $proforma}&proforma=1{/if}";
		document.page.target="";
		document.page.submit();
	}

</script>

{$default_printpage = ConfigHelper::getConfig('invoices.default_printpage')}
{$offline_support = ConfigHelper::checkConfig('ksef.offline_support')}

<FORM METHOD="POST" NAME="page">
<INPUT type="submit" class="hiddenbtn">
<TABLE class="lmsbox lms-ui-background-cycle">
    <COLGROUP>
        <COL style="width: 1%" span="6">
        <COL style="width: 93%">
        <COL style="width: 1%">
    </COLGROUP>
    <THEAD>
	<TR>
		<TD class="nobr">
			<A href="?m=invoicelist&o=id{if $listdata.direction == "asc" && $listdata.order == "id"},desc{/if}">{trans("ID:")}</A> {if $listdata.order == "id"}<IMG src="img/{if $listdata.direction == "desc"}asc{else}desc{/if}_order.gif" alt="">{/if}
		</TD>
		<TD class="nobr">
			<A href="?m=invoicelist&o=cdate{if $listdata.direction == "asc" && $listdata.order == "cdate"},desc{/if}">{trans("Date:")}</A> {if $listdata.order == "cdate"}<IMG src="img/{if $listdata.direction == "desc"}asc{else}desc{/if}_order.gif" alt="">{/if}
		</TD>
		<TD class="nobr">
			<A href="?m=invoicelist&o=number{if $listdata.direction == "asc" && $listdata.order == "number"},desc{/if}">{trans("Number:")}</A> {if $listdata.order == "number"}<IMG src="img/{if $listdata.direction == "desc"}asc{else}desc{/if}_order.gif" alt="">{/if}
		</TD>
		<TD class="nobr">
			<A href="?m=invoicelist&o=value{if $listdata.direction == "asc" && $listdata.order == "value"},desc{/if}">{trans("Value:")}</A> {if $listdata.order == "value"}<IMG src="img/{if $listdata.direction == "desc"}asc{else}desc{/if}_order.gif" alt="">{/if}
		</TD>
		<TD class="nobr">
			<A href="?m=invoicelist&o=count{if $listdata.direction == "asc" && $listdata.order == "count"},desc{/if}">{trans("Items:")}</A> {if $listdata.order == "count"}<IMG src="img/{if $listdata.direction == "desc"}asc{else}desc{/if}_order.gif" alt="">{/if}
		</TD>
		<TD class="nobr">
			<A href="?m=invoicelist&o=netflag{if $listdata.direction == "asc" && $listdata.order == "netflag"},desc{/if}">{trans("Accounting:")}</A> {if $listdata.order == "netflag"}<IMG src="img/{if $listdata.direction == "desc"}asc{else}desc{/if}_order.gif" alt="">{/if}
		</TD>
		<TD class="nobr">
			<A href="?m=invoicelist&o=name{if $listdata.direction == "asc" && $listdata.order == "name"},desc{/if}">{trans("For:")}</A> {if $listdata.order == "name"}<IMG src="img/{if $listdata.direction == "desc"}asc{else}desc{/if}_order.gif" alt="">{/if}
		</TD>
		<TD>
			&nbsp;
		</TD>
	</TR>
	<tr>
		<td colspan="8">
			<div class="lms-ui-filter-container">
				<div class="lms-ui-filter-definition">
					{icon name="filter"}
					{customerlist
						selectname="cid"
						version="2"
						inputname="customer"
						selected=$listdata.customer
					}
					<input type="text" name="search" value="{$listdata.search}" size="15"
						{tip text="Enter value of searching data and select category"}>
					<select size="1" name="cat">
						<option value=""{if $listdata.cat == ""} selected{/if}>{trans("— select filter category —")}</option>
						<option value="cdate"{if $listdata.cat == "cdate"} selected{/if}>{trans("day (YYYY/MM/DD)")}</option>
						<option value="month"{if $listdata.cat == "month"} selected{/if}>{trans("month (YYYY/MM)")}</option>
						<option value="year"{if $listdata.cat == "year"} selected{/if}>{trans("year (YYYY)")}</option>
						<option value="number"{if $listdata.cat == "number"} selected{/if}>{trans("number")}</option>
						<option value="value"{if $listdata.cat == "value"} selected{/if}>{trans("value")}</option>
						<option value="name"{if $listdata.cat == "name"} selected{/if}>{trans("customer name")}</option>
						<option value="ten"{if $listdata.cat == "ten"} selected{/if}>{trans("TEN")}</option>
						<option value="address"{if $listdata.cat == "address"} selected{/if}>{trans("address")}</option>
						<option value="cnotes"{if $listdata.cat == "cnotes"} selected{/if}>{trans("show only credit notes")}</option>
					</select>&nbsp;
					<label>
						{trans("Plan:")}
						<input type="hidden" name="numberplanid" value="all">
						<select name="numberplanid[]" class="lms-ui-multiselect" {tip text="Select numbering plan"} multiple data-default-value="{trans("— all —")}">
							{foreach $numberplans as $plan}
								{$period = $plan.period}
								<option value="{$plan.id}"{if !empty($listdata.numberplanid) && in_array($plan.id, $listdata.numberplanid)} selected{/if}
									>{number number=$plan.next template=$plan.template time=$invoice.cdate} ({$_NUM_PERIODS.$period})</option>
							{/foreach}
						</select>
					</label>
					<label>
						{trans("Division")}
						<select size="1" name="divisionid" {tip text="Select division"}>
							<option value="0">{trans("— all —")}</option>
							{foreach $divisions as $division}
								<option value="{$division.id}"{if $division.id == $listdata.divisionid} selected{/if}>{$division.label|escape}</option>
							{/foreach}
						</select>
					</label>
					<label>
						{trans("Group")}
						<input type="hidden" name="group" value="all">
						<select size="1" name="group[]" class="lms-ui-multiselect" multiple data-default-value="{trans("— all groups —")}">
							{foreach $grouplist as $group}
								<option value="{$group.id}"{if !empty($listdata.group) && in_array($group.id, $listdata.group)} selected{/if}
									>{$group.name|truncate:30:"&hellip;":true}</option>
							{/foreach}
						</select>
					</label>
					<label>
						<input type="checkbox" name="groupexclude"{if $listdata.groupexclude} checked{/if}>
						{trans("exclude group")}
					</label>
					<label>
						{trans("Send to email")}
						<select size="1" name="sendtoemail">
							<option value="">{trans("- any -")}</option>
							<option value="notsent"{if $listdata.sendtoemail == 'notsent'} selected{/if}>{trans("not sent")}</option>
							<option value="withoutconsent"{if $listdata.sendtoemail == 'withoutconsent'} selected{/if}>{trans("without consent")}</option>
						</select>
					</label>
					<label>
						{trans("<!ksef>KSeF status")}
						<select name="ksefstatus">
							<option value="">{trans("<!ksef-status>- any -")}</option>
							<option value="1"{if $listdata.ksefstatus == 1} selected{/if}>{trans("<!ksef-status>excluded")}</option>
							<option value="2"{if $listdata.ksefstatus == 2} selected{/if}>{trans("<!ksef-status>not sent yet")}</option>
							<option value="3"{if $listdata.ksefstatus == 3} selected{/if}>{trans("<!ksef-status>sent")}</option>
							<option value="4"{if $listdata.ksefstatus == 4} selected{/if}>{trans("<!ksef-status>error")}</option>
							<option value="5"{if $listdata.ksefstatus == 5} selected{/if}>{trans("<!ksef-status>assigned number")}</option>
						</select>
					</label>
					<label>
						<input type="checkbox" name="hideclosed"{if $listdata.hideclosed} checked{/if}>
						{trans("hide accounted<!plural>")}
					</label>
					<label {tip text="translates into JPK MPP flag"}>
						<input type="checkbox" name="splitpayment"{if $listdata.splitpayment} checked{/if}>
						{trans("split payment")}
					</label>
					<label {tip text="translates into JPK FP flag"}>
						<input type="checkbox" name="withreceipt"{if $listdata.withreceipt} checked{/if}>
						{trans("with receipt<!plural>")}
					</label>
					<label {tip text="translates into JPK EE flag"}>
						<input type="checkbox" name="telecomservice"{if $listdata.telecomservice} checked{/if}>
						{trans("<!invoice>telecommunication service")}
					</label>
					<label {tip text="translates into JPK TP flag"}>
						<input type="checkbox" name="relatedentity"{if $listdata.relatedentity} checked{/if}>
						{trans("related entity")}
					</label>
					{button type="link" icon="next" onclick="filter();"}
				</div>
			</div>
		</td>
	</tr>
	{if $pagination->getTotal() != 0}
	<TR>
		<TD class="lms-ui-pagination" COLSPAN="8">
			{include file="pagination.html"}
		</TD>
	</TR>
	{/if}
    </THEAD>
    <TBODY class="lms-ui-multi-check">
	{$value_by_currencies = array()}
	{foreach $invoicelist as $invoice}
	{assign var=invoiceid value=$invoice.id}
	{number number=$invoice.number template=$invoice.template time=$invoice.cdate assign=number customerid=$invoice.customerid}
	<TR class="highlight{if $invoice.closed} blend{elseif $invoice.cancelled} cancel{/if}" data-published="{$invoice.published}"
			data-number="{$number}" data-customername="{$invoice.name|escape:"javascript"|escape:"html"}"
			data-senddate="{if $invoice.senddate}{$invoice.senddate|date_format:"Y-m-d H:i"}{else}{$invoice.senddate}{/if}"
			data-refdocs="{if empty($invoice.documentreferenced)}0{else}1{/if}"
			data-ksef="{if empty($invoice.ksefhash)}0{else}1{/if}"
			data-ksef-submit="{if empty($invoice.ksefsubmit)}0{else}1{/if}">
		<TD>
			{$invoice.id|string_format:"%06d"}
		</TD>
		<TD class="nobr">
			{$invoice.cdate|date_format:"Y-m-d H:i"}
		</TD>
		<TD class="text-right nobr">
			{$number}
		</TD>
		<TD class="text-right nobr">
			{moneyf($invoice.value, $invoice.currency)}
			{if isset($value_by_currencies[$invoice.currency])}
				{$value_by_currencies[$invoice.currency] = $value_by_currencies[$invoice.currency] + $invoice.value}
			{else}
				{$value_by_currencies[$invoice.currency] = $invoice.value}
			{/if}
		</TD>
		<TD class="text-right nobr">
			{$invoice.count}
		</TD>
		<TD class="text-right nobr">
			{if $invoice.netflag}
				{trans('net')}
			{else}
				{trans('gross')}
			{/if}
		</TD>
		<TD>
			<A HREF="?m=customerinfo&id={$invoice.customerid}"{if $invoice.closed} class="blend"{/if}>{$invoice.name}, {$invoice.address}, {$invoice.zip} {$invoice.city}{if $invoice.country != ""}, {t}{$invoice.country}{/t}{/if}</A>
		</TD>
		<TD class="text-right nobr">
			{foreach $invoice.customlinks as $link}
				<A href="{$link.url}" rel="external"{if isset($link.onclick)} onclick="{$link.onclick}"{/if}>{if isset($link.icon)}<IMG src="{$link.icon}" alt="{$link.label}" {$link.tip}>{else}{$link.label}{/if}</A>
			{/foreach}
			{if !empty($invoice.ksefhash)}
				{if !empty($invoice.ksefstatus) && ($invoice.ksefstatus < 200 || $invoice.ksefstatus >= 300)}
					{$class = "lms-ui-error"}
				{else}
					{$class = ""}
				{/if}
				{hint icon="lock" url="?m=invoiceksefinfo&id={$invoiceid}" tooltip_class="lms-ui-ksef-qr-code" class=$class}
			{/if}
			{if $invoice.type == $smarty.const.DOC_INVOICE_PRO && !$invoice.closed}
				{if ConfigHelper::checkPrivilege('finances_management') || ConfigHelper::checkPrivilege('financial_operations')}
					{button type="link" icon="transform" href="?m=invoicenew&id={$invoice.id}&action=init"
						tip="Convert to invoice"}
				{/if}
			{/if}
			{if $invoice.sendinvoices && !$invoice.cancelled && ($invoice.type == $smarty.const.DOC_INVOICE || $invoice.type == $smarty.const.DOC_CNOTE || $invoice.type == $smarty.const.DOC_INVOICE_PRO)}
				{if empty($invoice.ksefsubmission) || $invoice.ksefstatus == 200 || $offline_support && isset($invoice.ksefstatus) && $invoice.ksefstatus == 0}
					{textformat assign="tip" wrap=5000}
						{if $invoice.senddate}{trans("Send invoice again")}<br><strong>{t a=$invoice.senddate|date_format:"Y-m-d H:i"}(already sent on $a){/t}</strong>{else}{trans("Send invoice")}{/if}
					{/textformat}
					{button type="link" icon="{if $invoice.senddate}mailsent{else}mail{/if}" class="send-invoice" href="?m=invoicesend&id={$invoice.id}"
						tip=$tip}
				{/if}
			{/if}
			{if $invoice.type == $smarty.const.DOC_INVOICE || $invoice.type == $smarty.const.DOC_INVOICE_PRO || $invoice.type == $smarty.const.DOC_CNOTE}
				{if $invoice.type == $smarty.const.DOC_INVOICE || $invoice.type == $smarty.const.DOC_CNOTE}
					{if !$invoice.cancelled && ($invoice.type == $smarty.const.DOC_INVOICE || ($invoice.type == $smarty.const.DOC_CNOTE && !$invoice.referenced))}
						{if ConfigHelper::checkPrivilege('finances_management') || ConfigHelper::checkPrivilege('financial_operations')}
							{if $invoice.type == $smarty.const.DOC_INVOICE}
								{button type="link" icon="clone" href="?m=invoicenew&action=init&id={$invoice.id}&clone=1"
									tip="Clone"}
							{/if}
							{button type="link" icon="correction" href="?m=invoicenote&id={$invoice.id}&action=init" tip="Revise"}
						{/if}
					{/if}
					{if ($invoice.type == $smarty.const.DOC_INVOICE || $invoice.type == $smarty.const.DOC_INVOICE_PRO) && !$invoice.closed && !$invoice.referenced && !$invoice.archived}
						{if !$invoice.published || ConfigHelper::checkPrivilege('published_document_modification')}
							{if ConfigHelper::checkPrivilege('finances_management') || ConfigHelper::checkPrivilege('financial_operations')}
								{if empty($invoice.ksefhash) && empty($invoice.ksefsubmit)}
									{button type="link" icon="delete" class="delete-invoice" tip="Delete"
										href="?m=invoicedel&id={$invoice.id}"}
								{/if}
							{/if}
						{/if}
					{/if}
				{else}
					{if ConfigHelper::checkPrivilege('finances_management') || ConfigHelper::checkPrivilege('financial_operations')}
						{if empty($invoice.ksefhash) && empty($invoice.ksefsubmit)}
							{button type="link" icon="delete" tip="Delete" class="delete-proforma-invoice" href="?m=invoicedel&id={$invoice.id}"}
						{/if}
					{/if}
				{/if}
			{/if}
			{if !$invoice.closed && !$invoice.archived}
				{if $invoice.type == $smarty.const.DOC_CNOTE}
					{if !$invoice.referenced && (!$invoice.published || ConfigHelper::checkPrivilege('published_document_modification'))}
						{if ConfigHelper::checkPrivilege('finances_management') || ConfigHelper::checkPrivilege('financial_operations')}
							{if empty($invoice.ksefhash) && empty($invoice.ksefsubmit)}
								{button type="link" icon="delete" class="delete-invoice" tip="Delete"
									href="?m=invoicedel&id={$invoice.id}"}
							{/if}
						{/if}
					{/if}
				{/if}
				{if !$invoice.cancelled}
					{if ($invoice.type == $smarty.const.DOC_INVOICE || $invoice.type == $smarty.const.DOC_INVOICE_PRO)}
						{if $invoice.type == $smarty.const.DOC_INVOICE}
							{if !$invoice.referenced && (!$invoice.published || ConfigHelper::checkPrivilege('published_document_modification'))}
								{if ConfigHelper::checkPrivilege('finances_management') || ConfigHelper::checkPrivilege('financial_operations')}
									{if empty($invoice.ksefhash) && empty($invoice.ksefsubmit)
										|| !empty($invoice.ksefstatus) && ($invoice.ksefstatus < 200 || $invoice.ksefstatus >= 300)}
										{button type="link" icon="edit"
											href="?m=invoiceedit&id={$invoice.id}&action=edit" class="edit-invoice" tip="Edit"}
									{/if}
								{/if}
							{/if}
						{else}
							{if ConfigHelper::checkPrivilege('finances_management') || ConfigHelper::checkPrivilege('financial_operations')}
								{if empty($invoice.ksefhash) && empty($invoice.ksefsubmit)}
									{button type="link" icon="edit" tip="Edit"
										href="?m=invoiceedit&id={$invoice.id}&action=edit{if $invoice.type == $smarty.const.DOC_INVOICE_PRO}&proforma=1{/if}"}
								{/if}
							{/if}
						{/if}
					{elseif $invoice.type == $smarty.const.DOC_CNOTE}
						{if !$invoice.referenced && (!$invoice.published || ConfigHelper::checkPrivilege('published_document_modification'))}
							{if ConfigHelper::checkPrivilege('finances_management') || ConfigHelper::checkPrivilege('financial_operations')}
								{if empty($invoice.ksefhash) && empty($invoice.ksefsubmit)}
									{button type="link" icon="edit" tip="Edit" class="edit-invoice"
										href="?m=invoicenoteedit&id={$invoice.id}&action=edit"}
								{/if}
							{/if}
						{/if}
					{/if}
				{/if}
			{/if}
			{if !$invoice.cancelled}
				{if $invoice.type != $smarty.const.DOC_INVOICE_PRO && !$invoice.archived && !$invoice.referenced && (!$invoice.published || ConfigHelper::checkPrivilege('published_document_modification'))}
					{if ConfigHelper::checkPrivilege('finances_management') || ConfigHelper::checkPrivilege('financial_operations')}
						{if empty($invoice.ksefhash) && empty($invoice.ksefsubmit) || !empty($invoice.ksefstatus) && $invoice.ksefstatus >= 400 && $invoice.ksefstatus < 500}
							{button type="link" icon="cancel" tip="Cancel" class="cancel-invoice"
								href="?m=invoicecancel&id={$invoice.id}"}
						{/if}
					{/if}
				{/if}
			{elseif $invoice.cancelled}
				{if ConfigHelper::checkPrivilege('finances_management') || ConfigHelper::checkPrivilege('financial_operations')}
					{button type="link" icon="back" tip="Recover" href="?m=invoicecancel&id={$invoice.id}&recover=1"
						class="recover-invoice"}
				{/if}
			{/if}
			{if !$invoice.cancelled}
				{if $invoice.type == $smarty.const.DOC_INVOICE || $invoice.type == $smarty.const.DOC_CNOTE || $invoice.type == $smarty.const.DOC_INVOICE_PRO}
					{if !$invoice.archived}
						{if ConfigHelper::checkPrivilege('trade_document_archiving')}
							{button type="link" icon="archive" tip="Archive" href="?m=invoicearchive&id={$invoice.id}"}
						{/if}
					{else}
						{if ConfigHelper::checkPrivilege('trade_document_unarchiving')}
							{button type="link" icon="restore" tip="Unarchive" href="?m=invoiceunarchive&id={$invoice.id}"}
						{/if}
					{/if}
				{/if}
			{/if}
			{if $invoice.type == $smarty.const.DOC_INVOICE || $invoice.type == $smarty.const.DOC_INVOICE_PRO}
				{if $invoice.type == $smarty.const.DOC_INVOICE && (!$invoice.published || ConfigHelper::checkPrivilege('published_document_modification'))}
					{button type="link" icon="view" tip="Preview" class="print-invoice" href="?m=invoice&id={$invoice.id}&dontpublish=1"}
				{/if}
				{button type="link" icon="print" tip="Print" class="print-invoice" href="?m=invoice&id={$invoice.id}"}
			{/if}
			{if $invoice.type == $smarty.const.DOC_CNOTE}
				{if !$invoice.published || ConfigHelper::checkPrivilege('published_document_modification')}
					{button type="link" icon="view" tip="Preview" class="print-invoice" href="?m=invoice&id={$invoice.id}&dontpublish=1"}
				{/if}
				{button type="link" icon="print" tip="Print" class="print-invoice" href="?m=invoice&id={$invoice.id}"}
			{/if}
			{if !empty($invoice.refdocs)}
				{button type="link" icon="more" class="all-attachment-toggle" tip="related documents"}
			{/if}
			<INPUT TYPE="checkbox" NAME="marks[{$invoice.id}]" VALUE="{$invoice.id}" class="lms-ui-multi-check"{if $marks.$invoiceid} checked{/if}>
			{if !empty($invoice.documentreferenced)}
				<div class="all-attachment-view">
					{foreach $invoice.refdocs as $docid => $refdoc}
						<div class="document-list">
							{if count($refdoc.attachments) == 1}
								<div class="document-title">
									{$attachment = reset($refdoc.attachments)}
									{$attachmentid = key($refdoc.attachments)}
									{capture assign="attachment_text"}{t a=$_DOCTYPES[$refdoc.type] b=$refdoc.fullnumber c=$refdoc.cdate|date_format:"Y-m-d"}$a no. $b issued on $c{/t}{/capture}
									{documentview id="{$docid}-{$attachmentid}" type=$attachment.contenttype name=$attachment.filename
										url="?m=documentview&id={$docid}&attachmentid={$attachmentid}" external=true
										text="{$attachment_text} <i class=\"lms-ui-icon-view{if preg_match('/pdf/',$attachment.contenttype)} pdf{elseif preg_match('/csv|excel|spreadsheet/',$attachment.contenttype)} xls{/if}\"></i>"}
									{if $attachment.type == -1}<i class="lms-ui-icon-user" title="<strong>{t a=date("Y/m/d H:i", $attachment.cdate)}Sent by customer at $a{/t}</strong>"></i>{/if}
								</div>
							{else}
								<div class="document-title">
									{t a=$_DOCTYPES[$refdoc.type] b=$refdoc.fullnumber c=$refdoc.cdate|date_format:"Y-m-d"}$a no. $b issued on $c{/t}
								</div>
								<div class="document-attachment-list">
									{foreach $refdoc.attachments as $attachmentid => $attachment}
										<div class="document-attachment">
											{documentview id="{$docid}-{$attachmentid}" type=$attachment.contenttype name=$attachment.filename
												url="?m=documentview&id={$docid}&attachmentid={$attachmentid}" external=true
												text="{$attachment.filename} <i class=\"lms-ui-icon-view{if preg_match('/pdf/',$attachment.contenttype)} pdf{elseif preg_match('/csv|excel|spreadsheet/',$attachment.contenttype)} xls{/if}\"></i>"}
											{if $attachment.type == -1}<i class="lms-ui-icon-user" title="<strong>{t a=date("Y/m/d H:i", $attachment.cdate)}Sent by customer at $a{/t}</strong>"></i>{/if}
										</div>
									{/foreach}
								</div>
							{/if}
						</div>
					{/foreach}
				</div>
			{/if}
		</TD>
	</TR>
	{foreachelse}
	<tr>
		<td class="empty-table" colspan="8">
			{trans("No such invoices in database.")}
		</td>
	</tr>
	{/foreach}
    </TBODY>
    <TFOOT>
		{if !empty($value_by_currencies)}
			<tr>
				<td colspan="3" class="text-right valign-top">
					<strong>{trans("Total:")}</strong>
				</td>
				<td class="text-right">
					{foreach $value_by_currencies as $currency => $value}
						<strong class="nobr">{moneyf($value, $currency)}</strong>
						{if !$value@last}<br>{/if}
					{/foreach}
				</td>
				<td colspan="4">
				</td>
			</tr>
		{/if}
	{if $pagination->getTotal() != 0}
	<TR>
		<TD class="lms-ui-pagination" COLSPAN="8">
			{include file="pagination.html"}
		</TD>
	</TR>
	{/if}
	<TR>
		<TD COLSPAN="8">
			<TABLE WIDTH="100%">
				<TR>
					<TD class="text-left" id="button-panel">
						{if !$proforma}
							{button icon="cash" label="Check/Uncheck as accounted" id="account-invoices"}
							{button icon="add" label="New invoice" href="?m=invoicenew{if $listdata.cat == 'customerid'}&customerid={$listdata.search}{/if}&action=init"}
						{else}
							{button icon="add" label="New Pro Forma" href="?m=invoicenew{if $listdata.cat == 'customerid'}&customerid={$listdata.search}{/if}&action=init&proforma=1"}
						{/if}
						{button icon="mail" id="send-invoices" label="Send invoices"}
						{button icon="delete" id="delete-invoices" label="Delete"}
						{if ConfigHelper::checkPrivilege('trade_document_archiving')}
							{button icon="archive" id="archive-invoices" label="Archive"}
						{/if}
						{if ConfigHelper::checkPrivilege('trade_document_unarchiving')}
							{button icon="restore" id="unarchive-invoices" label="Unarchive"}
						{/if}
						{button icon="print" label="Print" id="print-invoices"}
						<label>
							<INPUT type="checkbox" name="which[{$smarty.const.DOC_ENTITY_ORIGINAL}]"
									value="{$smarty.const.DOC_ENTITY_ORIGINAL}"
									{if preg_match('/original/i', $default_printpage)} checked{/if}>
							{$_DOCENTITIES[$smarty.const.DOC_ENTITY_ORIGINAL]}
						</label>
						<label>
							<INPUT type="checkbox" name="which[{$smarty.const.DOC_ENTITY_COPY}]"
									value="{$smarty.const.DOC_ENTITY_COPY}"
									{if preg_match('/copy/i', $default_printpage)} checked{/if}>
							{$_DOCENTITIES[$smarty.const.DOC_ENTITY_COPY]}
						</label>
						<div id="duplicate-box">
							<label>
								<INPUT type="checkbox" name="which[{$smarty.const.DOC_ENTITY_DUPLICATE}]" id="duplicate-checkbox"
										value="{$smarty.const.DOC_ENTITY_DUPLICATE}"
										{if preg_match('/duplicate/i', $default_printpage)} checked{/if}>
								{$_DOCENTITIES[$smarty.const.DOC_ENTITY_DUPLICATE]}
							</label>
							<div id="duplicate-box-date-selector"
									{if !preg_match('/duplicate/i', $default_printpage)} style="display: none;"{/if}>
								<input type="text" name="duplicate-date" class="lms-ui-date lms-ui-date-unix">
							</div>
						</div>
					</TD>
					<TD class="text-right nobr">
						<label>
							{trans("Check All")}
							<INPUT TYPE="checkbox" NAME="allbox" class="lms-ui-multi-check-all" VALUE="1">
						</label>
					</TD>
				</TR>
			</TABLE>
		</TD>
	</TR>
    </TFOOT>
</TABLE>
</FORM>

<script>

	$('.delete-invoice').click(function () {
		var published = parseInt($(this).closest('tr').attr('data-published'));
		var number = $(this).closest('tr').attr('data-number');
		var refdocs = parseInt($(this).closest('tr').attr('data-refdocs'));
		if (published) {
			confirmDialog($t('Document has been published already! Are you sure you want to remove it despite this?'), this).done(function () {
				confirmDialog($t("Are you sure, you want to remove invoice '$a' from database?", number), this).done(function () {
					confirmDialog($t("Are you sure, you want to also remove related documents?"), this).done(function () {
						location.href = $(this).attr('href') + '&documents=1';
					}).fail(function() {
						location.href = $(this).attr('href');
					});
				});
			});
		} else {
			confirmDialog($t("Are you sure, you want to remove invoice '$a' from database?", number), this).done(function () {
				if (refdocs) {
					confirmDialog($t("Are you sure, you want to also remove related documents?"), this).done(function () {
						location.href = $(this).attr('href') + '&documents=1';
					}).fail(function() {
						location.href = $(this).attr('href');
					});
				} else {
					location.href = $(this).attr('href');
				}
			});
		}
		return false;
	});

	$('#delete-invoices').click(function() {
		if (!$(this).closest('tfoot').siblings('.lms-ui-multi-check').find('input.lms-ui-multi-check:checked').length) {
			alertDialog($t('No document of given type has been selected!'), this);
			return;
		}

		if ($('tr[data-published="1"] :checked').length) {
			{if ConfigHelper::checkPrivilege('published_document_modification')}
			confirmDialog($t("Some documents have been published already! Are you sure you want to delete them despite this?"), this)
					.done(function () {
						confirmDialog($t("Are you sure, you want to delete selected documents?"), this)
								.done(function () {
									if ($('tr[data-refdocs="1"] :checked').length) {
										confirmDialog($t("Are you sure, you want to also remove related documents?"), this).done(function () {
											document.page.action = "?m=invoicedel&documents=1";
											document.page.target = "";
											document.page.submit();
										}).fail(function () {
											document.page.action = "?m=invoicedel";
											document.page.target = "";
											document.page.submit();
										});;
									} else {
										document.page.action = "?m=invoicedel";
										document.page.target = "";
										document.page.submit();
									}
								});
					});
			return;
			{else}
			alertDialog($t("Some documents have been published already!"), this);
			return;
			{/if}
		}

		confirmDialog($t("Are you sure, you want to delete selected documents?"), this)
				.done(function() {
					if ($('tr[data-refdocs="1"] :checked').length) {
						confirmDialog($t("Are you sure, you want to also remove related documents?"), this).done(function () {
							document.page.action = "?m=invoicedel&documents=1";
							document.page.target = "";
							document.page.submit();
						}).fail(function () {
							document.page.action = "?m=invoicedel";
							document.page.target = "";
							document.page.submit();
						});;
					} else {
						document.page.action = "?m=invoicedel";
						document.page.target = "";
						document.page.submit();
					}
				});
	});

	$('.delete-proforma-invoice').click(function () {
		var number = $(this).closest('tr').attr('data-number');
		confirmDialog($t("Are you sure, you want to remove pro forma invoice '$a' from database?", number), this).done(function () {
			location.href = $(this).attr('href');
		});
		return false;
	});

	$('.edit-invoice').click(function () {
		var published = parseInt($(this).closest('tr').attr('data-published'));
		if (published) {
			confirmDialog($t('Document has been published already! Are you sure you want to edit it despite this?'), this).done(function () {
				location.href = $(this).attr('href');
			});
			return false;
		} else {
			location.href = $(this).attr('href');
			return false;
		}
	});

	$('.cancel-invoice').click(function () {
		var published = parseInt($(this).closest('tr').attr('data-published'));
		var number = $(this).closest('tr').attr('data-number');
		if (published) {
			confirmDialog($t('Document has been published already! Are you sure you want to cancel it despite this?'), this).done(function () {
				confirmDialog($t("Are you sure, you want to cancel invoice '$a'?", number), this).done(function () {
					location.href = $(this).attr('href');
				});
			});
		} else {
			confirmDialog($t("Are you sure, you want to cancel invoice '$a'?", number), this).done(function () {
				location.href = $(this).attr('href');
			});
		}
		return false;
	});

	$('.recover-invoice').click(function () {
		var number = $(this).closest('tr').attr('data-number');
		confirmDialog($t("Are you sure, you want to recover cancelled invoice '$a'?", number), this).done(function () {
			location.href = $(this).attr('href');
		});
		return false;
	});

	$('.send-invoice').click(function () {
		var customername = $(this).closest('tr').attr('data-customername');
		var senddate = $(this).closest('tr').attr('data-senddate');
		if (parseInt(senddate) === 0) {
			confirmDialog($t("Are you sure, you want to send invoice to customer '$a'?", customername), this).done(function () {
				window.open($(this).attr('href'));
			});
		} else {
			confirmDialog($t("Invoice was sent to customer '$a' already on $b. Are you sure, you want to send invoice again?", customername, senddate), this).done(function () {
				window.open($(this).attr('href'));
			});
		}
		return false;
	});

	$('#account-invoices').click(function () {
		if (!$(this).closest('tfoot').prev('.lms-ui-multi-check').find('input.lms-ui-multi-check:checked').length) {
			alertDialog($t('No document of given type has been selected!'), this);
			return;
		}

		confirmDialog($t('Are you sure, you want to check/uncheck selected invoices as accounted?'), this).done(function () {
			document.page.action = "?m=invoicepaid";
			document.page.target = "";
			document.page.submit();
		});
	});

	$('#send-invoices').click(function () {
		if (!$(this).closest('tfoot').prev('.lms-ui-multi-check').find('input.lms-ui-multi-check:checked').length) {
			alertDialog($t('No document of given type has been selected!'), this);
			return;
		}

		confirmDialog($t("Are you sure, you want to send invoices to customers?"), this).done(function () {
			var add = "";
			var which = 0;
			$('form[name="page"] [name^="which"]:checked').each(function() {
				which += parseInt($(this).val());
			});
			add += "&which=" + which;
			if (which & {$smarty.const.DOC_ENTITY_DUPLICATE}) {
				var duplicate_date = parseInt($('[name="duplicate-date"]').val());
				if (duplicate_date > 0) {
					add += "&duplicate-date=" + duplicate_date;
				}
			}

			document.page.action = "?m=invoicesend&marks=invoice" + add;
			document.page.target = "_blank";
			document.page.submit();
		});
	});

	$('#archive-invoices').click(function () {
		if (!$(this).closest('tfoot').prev('.lms-ui-multi-check').find('input.lms-ui-multi-check:checked').length) {
			alertDialog($t('No document of given type has been selected!'), this);
			return;
		}

		confirmDialog($t("Are you sure, you want to archive selected invoices?"), this).done(function () {
			document.page.action = "?m=invoicearchive&marks=invoice";
			document.page.target = "";
			document.page.submit();
		});
	});

	$('#unarchive-invoices').click(function () {
		if (!$(this).closest('tfoot').prev('.lms-ui-multi-check').find('input.lms-ui-multi-check:checked').length) {
			alertDialog($t('No document of given type has been selected!'), this);
			return;
		}

		confirmDialog($t("Are you sure, you want to unarchive selected invoices?"), this).done(function () {
			document.page.action = "?m=invoiceunarchive&marks=invoice";
			document.page.target = "";
			document.page.submit();
		});
	});

	$('.print-invoice').click(function() {
		var add = "";
		var which = 0;
		$('form[name="page"] [name^="which"]:checked').each(function() {
			which += parseInt($(this).val());
		});
		add += "&which=" + which;
		if (which & {$smarty.const.DOC_ENTITY_DUPLICATE}) {
			var duplicate_date = parseInt($('[name="duplicate-date"]').val());
			if (duplicate_date > 0) {
				add += "&duplicate-date=" + duplicate_date;
			}
		}

		window.open($(this).attr('href') + add);
		return false;
	});

	$('#print-invoices').click(function() {
		if (!$(this).closest('tfoot').prev('.lms-ui-multi-check').find('input.lms-ui-multi-check:checked').length) {
			alertDialog($t('No document of given type has been selected!'), this);
			return;
		}

		var add = "";
		var which = 0;
		$('form[name="page"] [name^="which"]:checked').each(function() {
			which += parseInt($(this).val());
		});
		add += "&which=" + which;
		if (which & {$smarty.const.DOC_ENTITY_DUPLICATE}) {
			var duplicate_date = parseInt($('[name="duplicate-date"]').val());
			if (duplicate_date > 0) {
				add += "&duplicate-date=" + duplicate_date;
			}
		}

		document.page.action="?m=invoice&print=cached" + add;
		document.page.target="_blank";
		document.page.submit();
	});

	$('#duplicate-checkbox').click(function () {
		$('#duplicate-box-date-selector').toggle($(this).prop('checked'));
	});

	$(function() {
		$('.all-attachment-toggle').click(function() {
			var attachmentView = $(this).siblings('.all-attachment-view');
			attachmentView.toggle();
			$(this).find('i').attr('class', 'lms-ui-icon-' + (attachmentView.is(':visible') ? 'less' : 'more'));
		});
	});

</script>

{/block}
