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

	$(function() {
		$('#deletemarked').click(function() {
			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() {
						document.page.action="?m=balancedel";
						document.page.target="";
						document.page.submit();
					});
					return false;
				{else}
					alertDialog($t("Some documents have been published already!"), this);
					return;
				{/if}
			}

			confirmDialog($t("Are you sure, you want to delete selected operation(s) and/or document item(s)?"), this).done(function() {
				document.page.action="?m=balancedel";
				document.page.target="";
				document.page.submit();
			});
			return false;
		});

		$('#search,#cat').change(function() {
			$('#sourcefileid').val('');
		});

		$('#print-invoices').click(function() {
			var print_invoices = $(this).is('#print-invoices');
			if ((print_invoices && !$('tr[data-doctype="{$smarty.const.DOC_INVOICE}"] :checked').length
					&& !$('tr[data-doctype="{$smarty.const.DOC_CNOTE}"] :checked').length)
					|| (!print_invoices && !$('tr[data-doctype="{$smarty.const.DOC_DNOTE}"] :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;

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

		$('#print-receipts').click(function() {
			if (!$('tr[data-doctype="{$smarty.const.DOC_RECEIPT}"] :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;

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

	function filter()
	{
		document.page.action="?m=balancelist";
		document.page.target="";
		document.page.submit();
	}

	{if ConfigHelper::checkConfig('phpui.short_pagescroller')}
	function pagechange1()
	{
		document.page.action="?m=balancelist&page=" + document.forms['page'].elements['page1'].value;
		document.page.target="";
		document.page.submit();
	}
	function pagechange2()
	{
		document.page.action="?m=balancelist&page=" + document.forms['page'].elements['page2'].value;
		document.page.target="";
		document.page.submit();
	}
	{/if}
</SCRIPT>
<FORM METHOD="POST" NAME="page" id="balancelist">
<INPUT type="submit" class="hiddenbtn">
<TABLE class="lmsbox lms-ui-background-cycle">
    <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: 1%;">
        <COL style="width: 91%;">
        <COL style="width: 1%;">
    </COLGROUP>
    <THEAD>
	<tr>
		<td>
			<strong>{trans("Date")}</strong>
		</td>
		<td>
			<strong>{trans("User")}</strong>
		</td>
		<td class="text-right">
			<strong>{trans("Liability")}</strong>
		</td>
		<td class="text-right">
			<strong>{trans("Income")}</strong>
		</td>
		<td class="text-right">
			<strong>{trans("Expense")}</strong>
		</td>
		<td class="text-center">&raquo;</td>
		<td class="text-right">
			<strong>{trans("After")}</strong>
		</td>
		<td>
			<strong>{trans("Customer")}</strong>
		</td>
		<td>
			<strong>{trans("Description")}</strong>
		</td>
		<td>
			{button icon="add" label="New Balance" href="?m=balanceadd"}
		</tdh>
	</tr>
	<TR>
		<TD colspan="10">
			{icon name="filter"}
			{if $listdata.cat == 'cashimport' && !is_array($listdata.search) && isset($sourcefilename)}
				<input type="hidden" name="sourcefileid" id="sourcefileid" value="{$listdata.search}">
			{/if}
			<select size="1" name="cat" id="cat" style="width: 150px">
				<option value=""{if $listdata.cat == ""} selected{/if}>{trans("— select category —")}</option>
				<option value="comment"{if $listdata.cat == "comment"} selected{/if}>{trans("comment")}</option>
				<option value="cdate"{if $listdata.cat == "cdate"} selected{/if}>{trans("date (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("document 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="customerid"{if $listdata.cat == "customerid"} selected{/if}>{trans("customer ID")}</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="documented"{if $listdata.cat == "documented"} selected{/if}>{trans("with document")}</option>
				<option value="notdocumented"{if $listdata.cat == "notdocumented"} selected{/if}>{trans("without document")}</option>
				<option value="cashimport"{if $listdata.cat == "cashimport"} selected{/if}>{trans("cash import file")}</option>
			</select>
			<input type="text" name="search" id="search"
				value="{if $listdata.cat == 'cashimport' && isset($sourcefilename)}{$sourcefilename}{else}{$listdata.search}{/if}"
				size="15" {tip text="Enter value of searching data and select category"}>
			<label>
				{trans("Type")}
				<select name="type" size="1">
					<option value="">{trans("<!operation-type>— any —")}</option>
					<option value="0"{if $listdata.type === "0"} selected{/if}>{trans("liability")}</option>
					<option value="1"{if $listdata.type === "1"} selected{/if}>{trans("income")}</option>
					<option value="2"{if $listdata.type === "2"} selected{/if}>{trans("expense")}</option>
				</select>
			</label>
			<label{if empty($sourcelist)} style="display: none;"{/if}>
				{trans("Source")}
				<select name="source" size="1">
					<option value="">{trans("<!cash-source>— any —")}</option>
					<option value="-1"{if $listdata.source == -1} selected{/if}>{trans("<!cash-source>— none —")}</option>
					{foreach $sourcelist as $item}
						<option value="{$item.id}"{if $listdata.source == $item.id} selected{/if}>{$item.name}</option>
					{/foreach}
				</select>
			</label>
			<label>
				{trans("Group")}
				<select size="1" name="group">
					<option value="0"{if !$listdata.group} selected{/if}>{trans("— all groups —")}</option>
					{foreach $grouplist as $group}
						<option value="{$group.id}"{if $listdata.group == $group.id} selected{/if}>{$group.name|trunescape:40}</option>
					{/foreach}
				</select>
			</label>
			<INPUT type="checkbox" name="groupexclude" id="groupexclude"{if $listdata.groupexclude} CHECKED{/if}><label
					for="groupexclude">{trans("exclude group")}</label>&nbsp;
			<span class="lms-ui-date-period-container">
				{date_period_preset from="#balancelist [name=\"from\"]" to="#balancelist [name=\"to\"]" time=true}
				<span>{trans("From:")}</span>
				<input type="text" name="from" size="14" maxlength="16"
					value="{if $listdata.from > 0}{$listdata.from|date_format:"Y/m/d H:i"}{/if}"
					placeholder="{trans("yyyy/mm/dd hh:mm")}"
					{tip class="lms-ui-datetime" text="Enter date in YYYY/MM/DD format (empty field means no limit) or click to choose it from calendar"}>
				<span>{trans("To:")}</span>
				<input type="text" name="to" size="14" maxlength="16"
					value="{if $listdata.to > 0}{$listdata.to|date_format:"Y/m/d H:i"}{/if}"
					placeholder="{trans("yyyy/mm/dd hh:mm")}"
					{tip class="lms-ui-datetime" text="Enter date in YYYY/MM/DD format (empty field means no limit) or click to choose it from calendar"}>
			</span>
			<A href="javascript:filter();">&raquo;&raquo;&raquo;</A><BR>
		</TD>
	</TR>
	{if $pagination->getTotal() != 0}
	<TR>
		<TD class="lms-ui-pagination" COLSPAN="10">
			{include file="pagination.html"}
		</TD>
	</TR>
	{/if}
    </THEAD>
    <TBODY class="lms-ui-multi-check">
	{foreach $balancelist as $balance}
	<TR class="highlight"{if $balance.docid} data-published="{$balance.published}"
			data-doctype="{$balance.doctype}"{/if}>
		<TD class="nobr">
			{$balance.time|date_format:"Y-m-d H:i"}
		</TD>
		<TD class="nobr">
			{if $balance.userid}{$balance.user|trunescape:16}{else}-{/if}
		</TD>
		<TD class="text-right nobr">
			{if $balance.covenant}{moneyf($balance.value*-1, $balance.currency)}{else}-{/if}
		</TD>
		<TD class="text-right nobr">
			{if !$balance.covenant && $balance.value > 0}{moneyf($balance.value, $balance.currency)}{else}-{/if}
		</TD>
		<TD class="text-right nobr">
			{if !$balance.covenant && $balance.value < 0}{moneyf($balance.value*-1, $balance.currency)}{else}-{/if}
		</TD>
		<TD class="text-center">
			&raquo;
		</TD>
		<TD class="text-right nobr">
			{if $balance.covenant}-{else}{moneyf($balance.after)}{/if}
		</TD>
		<TD class="text-right nobr">
			{if $balance.customerid}<A HREF="?m=customerinfo&id={$balance.customerid}">{$balance.customername|trunescape:20}</A>{else} - {/if}
		</TD>
		<TD>
			{$balance.comment|escape}
		</TD>
		<TD class="text-right nobr">
			{if $balance.docid}
				{if !empty($balance.ksefhash)}
					{hint icon="lock" url="?m=invoiceksefinfo&id={$balance.docid}" tooltip_class="lms-ui-ksef-qr-code"}
				{/if}
				{if $balance.doctype==$smarty.const.DOC_INVOICE}
					{if !$balance.published || ConfigHelper::checkPrivilege('published_document_modification')}
						{button type="link" icon="view" class="print-invoice lms-ui-hint-rollover"
							href="?m=invoice&id={$balance.docid}&dontpublish=1"
							data_url="?m=invoiceinfo&id={$balance.docid}"
							external=true
							icon_tip="View"}
					{/if}
					{button type="link" icon="print" class="print-invoice lms-ui-hint-rollover"
						href="?m=invoice&id={$balance.docid}"
						data_url="?m=invoiceinfo&id={$balance.docid}"
						external=true
						icon_tip="Invoice"}
				{elseif $balance.doctype==$smarty.const.DOC_CNOTE}
					{if !$balance.published || ConfigHelper::checkPrivilege('published_document_modification')}
						{button type="link" icon="view" class="print-invoice lms-ui-hint-rollover"
							href="?m=invoice&id={$balance.docid}&dontpublish=1"
							data_url="?m=number&id={$balance.docid}"
							external=true
							icon_tip="View"}
					{/if}
					{button type="link" icon="print" class="print-invoice lms-ui-hint-rollover"
						href="?m=invoice&id={$balance.docid}"
						data_url="?m=number&id={$balance.docid}"
						external=true
						icon_tip="Credit note"}
				{elseif $balance.doctype==$smarty.const.DOC_RECEIPT}
					{button type="link" icon="print" class="lms-ui-hint-rollover"
						href="?m=receipt&id={$balance.docid}"
						data_url="?m=number&id={$balance.docid}"
						external=true
						icon_tip="Cash receipt"}
				{elseif $balance.doctype==$smarty.const.DOC_DNOTE}
					{if !$balance.published || ConfigHelper::checkPrivilege('published_document_modification')}
						{button type="link" icon="view" class="lms-ui-hint-rollover"
							href="?m=note&id={$balance.docid}&dontpublish=1"
							data_url="?mumber&id={$balance.docid}"
							external=true
							icon_tip="View"}
					{/if}
					{button type="link" icon="print" class="lms-ui-hint-rollover"
						href="?m=note&id={$balance.docid}"
						data_url="?m=number&id={$balance.docid}"
						external=true
						icon_tip="Debit note"}
				{/if}
			{/if}
			<INPUT TYPE="checkbox" class="lms-ui-multi-check" NAME="marks[{$balance.id}]" VALUE="{$balance.id}">
		</TD>
	</TR>
	{foreachelse}
	<tr>
		<td colspan="10" class="empty-table">
			{trans("No such transactions found in database.")}
		</td>
	</tr>
	{/foreach}
    </TBODY>
    <TFOOT>
	<TR>
		<TD COLSPAN="8" class="nobr">
			{if ConfigHelper::checkPrivilege('finances_management') || ConfigHelper::checkPrivilege('financial_operations')}
				{button id="deletemarked" icon="delete" label="Delete"}
			{/if}
			{trans("Print")}
			{button icon="print" label="invoices" id="print-invoices"}
			{button icon="cash" label="cash receipts" id="print-receipts"}
			<label>
				<INPUT type="checkbox" name="which[{$smarty.const.DOC_ENTITY_ORIGINAL}]"
						value="{$smarty.const.DOC_ENTITY_ORIGINAL}"
						{if preg_match('/original/i', ConfigHelper::getConfig('invoices.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', ConfigHelper::getConfig('invoices.default_printpage'))} checked{/if}>
				{$_DOCENTITIES[$smarty.const.DOC_ENTITY_COPY]}
			</label>
			<label>
				<INPUT type="checkbox" name="which[{$smarty.const.DOC_ENTITY_DUPLICATE}]"
					   value="{$smarty.const.DOC_ENTITY_DUPLICATE}"
						{if preg_match('/duplicate/i', ConfigHelper::getConfig('invoices.default_printpage'))} checked{/if}>
				{$_DOCENTITIES[$smarty.const.DOC_ENTITY_DUPLICATE]}
			</label>
		</TD>
		<TD colspan="2" class="text-right nobr">
			<label>
				{trans("Check All")}
				<INPUT TYPE="checkbox" class="lms-ui-multi-check-all" NAME="allbox" VALUE="1">
			</label>
		</TD>
	</TR>
	{if $pagination->getTotal() != 0}
	<TR>
		<TD class="lms-ui-pagination" COLSPAN="10">
			{include file="pagination.html"}
		</TD>
	</TR>
	{/if}
	<TR>
		<TD colspan="2" class="text-right bold">
			{trans("Total:")}
		</TD>
		<TD class="text-right bold nobr">
			{$listdata.liability|money_format}
		</TD>
		<TD class="text-right bold nobr">
			{$listdata.income|money_format}
		</TD>
		<TD class="text-right bold nobr">
			{$listdata.expense|money_format}
		</TD>
		<TD colspan="2">&nbsp;</TD>
		<TD class="text-right bold nobr">
			{trans("Balance:")}
		</TD>
		<TD class="bold nobr">
			{$listdata.totalval|money_format}
		</TD>
		<TD>&nbsp;</TD>
	</TR>
    </TFOOT>
</TABLE>
</FORM>
{/block}
