{extends file=$smarty.const.SMARTY_LAYOUT_MAIN_POPUP}

{block name="css" append}
<style>
	table.cashbook { width:100%; border-collapse:collapse; border:2px solid #CECECE; font-size:11px; }
	table.cashbook th { padding:6px 4px; background:#E6E6E6; color:#181f50; border-right:1px solid #dfdfdf; white-space:nowrap; font-weight:normal; text-align:left; }
	table.cashbook th.right { text-align:right; }
	table.cashbook td { padding:3px 4px; background:#fff; border-right:1px solid #e6e6e6; border-bottom:1px solid #e6e6e6; vertical-align:middle; }
	table.cashbook td.sel { background:#EEF4F9; }
	table.cashbook tr.balance td { background:#E6F0F7; }
	table.cashbook tr.balance td.sel { background:#DCE6ED; }
	table.cashbook tr.balance.closed td { font-weight:bold; background:#C5D9EA; }
	table.cashbook tr.balance.closed td.sel { background:#BAD0E2; }
	table.cashbook tr.separator td { height:8px; background:#FAFAFA; border-bottom:2px solid #d0d8df; padding:0; }
	table.cashbook td.negative { color:red; }
	table.cashbook tr.extra td { color:#888 !important; font-weight:bold !important; }
	table.cashbook td.right { text-align:right; }
	table.cashbook td.nowrap { white-space:nowrap; }
	table.cashbook td.italic { font-style:italic; }
	table.cashbook .center { text-align:center; }
	div.cashbook-export { margin-top:8px; padding-bottom:4px; text-align:center; }
	table.cashbook a.icon-lock,
	table.cashbook a.icon-unlock,
	table.cashbook a.icon-lock-disabled {
		display: inline-block;
		background-image: url('{$smarty.const.THEME_URL}styles/listing/optionsBox/icons.Tina3AquaOptionBox_.1b70.20180503.png');
		background-repeat: no-repeat;
		width: 13px;
		height: 16px;
		vertical-align: middle;
	}
	table.cashbook a.icon-lock-disabled { background-position: 0 0; cursor: default; opacity: 0.4; }
	table.cashbook a.icon-lock          { background-position: -15px 0; cursor: pointer; }
	table.cashbook a.icon-lock:hover    { background-position: -30px 0; }
	table.cashbook a.icon-unlock        { background-position: -60px 0; cursor: pointer; }
	table.cashbook a.icon-unlock:hover  { background-position: -45px 0; }
</style>
{/block}

{block name="content"}
{if $processedDocs}

	{assign var=extraCurrencies value=[]}
	{foreach $processedDocs as $dayData}
		{foreach from=$dayData.docs item=thisDoc}
			{if $thisDoc.value && !($thisDoc.currency|in_array:$selectedCurrencies) && !($thisDoc.currency|in_array:$extraCurrencies)}
				{append var=extraCurrencies value=$thisDoc.currency}
			{/if}
		{/foreach}
	{/foreach}

	{assign var=currenciesCount value=($selectedCurrencies|count)+($extraCurrencies|count)}

	<div id="listingContainer" class="engine-listing-container">
	<table class="cashbook">
		<tr>
			<th>{$lang.index.tableHeader.date}</th>
			<th>{$lang.index.tableHeader.category}</th>
			<th>{$lang.index.tableHeader.type}</th>
			<th>{$lang.index.tableHeader.number}</th>
			<th>{$lang.index.tableHeader.partner}</th>
			<th>{$lang.index.tableHeader.responsible}</th>
			<th>{$lang.index.tableHeader.explanations}</th>
			{foreach from=$selectedCurrencies key=curr item=thisCurrency}
				<th class="right">{$thisCurrency}</th>
			{/foreach}
			{foreach from=$extraCurrencies item=thisCurrency}
				<th class="right">{$thisCurrency}</th>
			{/foreach}
			{if $showCurrExch}
				<th class="right" style="width:5%">{$lang.index.tableHeader.currencyExchange}</th>
			{/if}
			{if $showEquivDefCurr}
				<th class="right" style="width:5%">{$lang.index.tableHeader.equivDefCurr}</th>
			{/if}
			{if $showEquivWorkCurr}
				<th class="right" style="width:5%">{$lang.index.tableHeader.equivWorkCurr}</th>
			{/if}
			<th style="width:1%">{$lang.index.tableHeader.options}</th>
		</tr>

		{foreach $processedDocs as $thisDate => $dayData}

			{* Document rows *}
			{foreach from=$dayData.docs key=docKey item=thisDoc}
				{if !$selectedCurrencies || $thisDoc.currency|in_array:$selectedCurrencies || $thisDoc.value}
					<tr{if !($thisDoc.currency|in_array:$selectedCurrencies)} class="extra"{/if}>
						<td class="nowrap sel">{$thisDate|dateTimeFormat:'date'}</td>
						<td>{$_LANG.cashBook.listing.documentCategories[$thisDoc.category]}</td>
						<td>{$thisDoc.documentTypeName}</td>
						<td>{$thisDoc.number}</td>
						<td>
							{if $thisDoc.supplierName}{$thisDoc.supplierName}
							{elseif $thisDoc.clientName}{$thisDoc.clientName}
							{else}{$thisDoc.partner}{/if}
						</td>
						<td>{if $thisDoc.idResponsible}{\Entity\User::select($thisDoc.idResponsible)->name}{else}-{/if}</td>
						<td>{$thisDoc.explanations}</td>
						{foreach from=$selectedCurrencies key=curr item=thisCurrency}
							<td class="nowrap right sel{if $thisDoc.value < 0} negative{/if}">
								{if $thisDoc.currency == $thisCurrency}{$thisDoc.value|priceFormat}{/if}
							</td>
						{/foreach}
						{foreach from=$extraCurrencies item=thisCurrency}
							<td class="nowrap right sel{if $thisDoc.currency == $thisCurrency && $thisDoc.value < 0} negative{/if}">
								{if $thisDoc.currency == $thisCurrency}{$thisDoc.value|priceFormat}{/if}
							</td>
						{/foreach}
						{if $showCurrExch}
							<td class="nowrap right sel">{foreach $dayData.exchangeRates[$thisDoc.currency] as $toCurr => $rate}{$rate|number_format:4} {/foreach}</td>
						{/if}
						{if $showEquivDefCurr}
							<td class="nowrap right sel{if $thisDoc.eqvDefValue < 0} negative{/if}">{$thisDoc.eqvDefValue|priceFormat}</td>
						{/if}
						{if $showEquivWorkCurr}
							<td class="nowrap right sel{if $thisDoc.eqvWorkValue < 0} negative{/if}">{$thisDoc.eqvWorkValue|priceFormat}</td>
						{/if}
						<td></td>
					</tr>
				{/if}
			{/foreach}

			{* Daily balance row *}
			{assign var=thisDateFormatted value=$thisDate|dateTimeFormat:'date'}
			<tr class="balance{if $dayData.closed} closed{/if}"{if $dayData.closed && $dayData.closedBy} tooltip="{$lang.index.tooltips.closedBalance|sprintf:$dayData.closedBy:$dayData.closedAt}"{/if}>
				<td class="sel">{$thisDateFormatted}</td>
				<td colspan="6" class="right">
					{if $dayData.closed}
						<b>{$lang.index.tableContent.dayTotal}</b> ({$thisDateFormatted}):
					{else}
						{$lang.index.tableContent.dayTotal} ({$thisDateFormatted}):
					{/if}
				</td>
				{foreach from=$selectedCurrencies key=curr item=thisCurrency}
					<td class="nowrap sel italic right{if $dayData.totals[$thisCurrency] < 0} negative{/if}">
						{$dayData.totals[$thisCurrency]|priceFormat}
					</td>
				{/foreach}
				{foreach from=$extraCurrencies item=thisCurrency}
					<td class="nowrap sel italic right{if $dayData.totals[$thisCurrency] < 0} negative{/if}">
						{$dayData.totals[$thisCurrency]|priceFormat}
					</td>
				{/foreach}
				{if $showCurrExch}
					<td class="nowrap sel italic right">&nbsp;</td>
				{/if}
				{if $showEquivDefCurr}
					<td class="nowrap sel italic right{if $dayData.eqvDefTotal < 0} negative{/if}">
						{$dayData.eqvDefTotal|priceFormat}
					</td>
				{/if}
				{if $showEquivWorkCurr}
					<td class="nowrap sel italic right{if $dayData.eqvWorkTotal < 0} negative{/if}">
						{$dayData.eqvWorkTotal|priceFormat}
					</td>
				{/if}
				<td>
					{if $dayData.closed && $thisDate == $lastClosedDate}
						<a href="#"
							class="icon-unlock"
							tooltip="{$lang.index.tooltips.unblock|sprintf:$thisDateFormatted}"
							onclick="MVC.callbackWithConfirmation(
								'{$lang.index.confirmations.unblockTitle}',
								'{$lang.index.confirmations.unblock|sprintf:$thisDateFormatted|escape:'javascript'}',
								function() { cbSubmitAction('unblock', '{$thisDate}'); }
							); return false;"
						></a>
					{elseif !$dayData.closed && ($lastClosedDate === null || $thisDate > $lastClosedDate)}
						<a href="#"
							class="icon-lock"
							tooltip="{$lang.index.tooltips.block|sprintf:$thisDateFormatted}"
							onclick="MVC.callbackWithConfirmation(
								'{$lang.index.confirmations.blockTitle}',
								'{$lang.index.confirmations.block|sprintf:$thisDateFormatted|escape:'javascript'}',
								function() { cbSubmitAction('block', '{$thisDate}'); }
							); return false;"
						></a>
					{else}
						<a class="icon-lock-disabled" tooltip="{$lang.index.tooltips.cantUnblockOld}"></a>
					{/if}
				</td>
			</tr>

			{* Separator *}
			<tr class="separator">
				<td></td>
				<td colspan="6"></td>
				<td colspan="{$currenciesCount}"></td>
				{if $showCurrExch}<td></td>{/if}
				{if $showEquivDefCurr}<td></td>{/if}
				{if $showEquivWorkCurr}<td></td>{/if}
				<td></td>
			</tr>

		{/foreach}
	</table>
	</div>

	<div class="cashbook-export">
		{if $cashBookExport.xls.defaultTemplate}
			<button type="button" onclick="$('cbExportFormatField').value='xls'; cbSubmitAction('export', '');">
				<img src="{$smarty.const.SMARTY_BLANKGIF}" alt="" align="absmiddle" class="imgIcon icon-xls" />&nbsp;{$lang.index.exportButton}
			</button>
		{/if}
		{if $cashBookExport.html.defaultTemplate}
			<button type="button" onclick="$('cbExportFormatField').value='html'; cbSubmitAction('export', '');">
				<img src="{$smarty.const.SMARTY_BLANKGIF}" alt="" align="absmiddle" class="imgIcon icon-print" />&nbsp;{$lang.index.exportHtmlButton}
			</button>
		{/if}
	</div>

{else}
	<p class="noResults">{$lang.index.noData}</p>
{/if}

{* Hidden form used by block/unblock/export actions *}
<form id="cbActionForm" method="post" style="display:none;">
	<input type="hidden" name="office[id]"     value="{$idOffice}">
	<input type="hidden" name="period[start]"  value="{$period.start}">
	<input type="hidden" name="period[end]"    value="{$period.end}">
	{foreach from=$selectedCurrencies key=curr item=v}
		<input type="hidden" name="currency[{$curr}]" value="{$curr}">
	{/foreach}
	<input type="hidden" name="equivDefCurr"        value="{$showEquivDefCurr ? 1 : 0}">
	<input type="hidden" name="equivWorkCurr"        value="{$showEquivWorkCurr ? 1 : 0}">
	<input type="hidden" name="currExchDisplay" value="{$showCurrExch ? 1 : 0}">
	<input type="hidden" id="cbActionField"      name="action"       value="">
	<input type="hidden" id="cbBlockDateField"   name="blockDate"    value="">
	<input type="hidden" id="cbExportFormatField" name="exportFormat" value="">
</form>

{/block}

{block name="js" append}
<script type="text/javascript">
	function cbSubmitAction(action, blockDate) {
		$('cbActionField').value    = action;
		$('cbBlockDateField').value = blockDate;
		$('cbActionForm').submit();
	}
</script>
{/block}