{$currCount=count($usedCurrencies)}

<table class="engine-listing thead-inline previewTable">
    <thead class="inline">
	<tr>
		<th class="counter">#</th>
		<th class="nowrap">{$lang['index']['texts']['service']}</th>
		<th class="nowrap">{$lang['index']['texts']['invoice']}</th>
		<th class="nowrap">{$lang['index']['texts']['clientSupplier']}</th>
		{foreach from=$usedCurrencies item=item key=curr}
			<th class=nowrap "right">{$lang['index']['texts']['vatShort']|sprintf:$curr}</th>
		{/foreach}
		<th class="nowrap right">{$lang['index']['texts']['vatEquiv']|sprintf:$equivCurrency}</th>
	</tr>
    </thead>

    <tbody class="list">
	<tr>
		<td colspan="{$currCount+5}" class="rowsSeparator"></td>
	</tr>

	{* ========================================================================================= *}
	{* =================== CLIENT ROWS ========================================================= *}
	{* ========================================================================================= *}

	<tr class="normalSubtitle">
        <td style="color:#555555;background-color:#D4E2F4;padding:3px;" colspan="{$currCount+5}"><span class="{if !$clientData}incompleteBalance{/if}">{$lang['index']['texts']['incomeClientInvoice']}</span></td>
	</tr>

	{* ===================== LIST ROWS ========================================================= *}
	{foreach from=$clientData item=thisRow}
		<tr>
			<td class="counter">{counter name=clientRowsCounter}.</td>
			<td>{serviceLabel ref=$thisRow.idService format="%serviceName% #%id%"} {if $thisRow.refund}<span class="refund">[R]</span>{/if}</td>
			<td>{invoiceLabel ref=$thisRow.idInvoice format="%invoiceNumber% / %invoiceDateStd%"}</td>
			<td>
				{if $thisRow.idCorporate} {corporateLabel ref=$thisRow.idCorporate} {/if}
				{if $thisRow.idCustomer} {customerLabel ref=$thisRow.idCustomer} {/if}
			</td>
			{foreach from=$usedCurrencies item=item key=curr}
				<td class="right rowValue">
					{if $thisRow.currency==$curr}
						{$thisRow.value|priceFormat:$curr:true}
					{else}
						&nbsp;
					{/if}
				</td>
			{/foreach}
			<td class="right rowValue">{$thisRow._EQUIV_|priceFormat:$equivCurrency:true}</td>
		</tr>
	{/foreach}

	{* ===================== TOTAL ============================================================= *}
	<tr class="total">
		<td colspan="4" class="right" style="padding:3px;">{$lang['index']['texts']['totalIncome']}</td>
		{foreach from=$usedCurrencies item=item key=curr}
			<td class="right rowValue">
				{if $clientTotals[$curr]}
					{$clientTotals[$curr]|priceFormat:$curr:true}
				{else}
					&nbsp;
				{/if}
			</td>
		{/foreach}
		<td class="right rowValue">{$clientTotals._EQUIV_|priceFormat:$equivCurrency:true}</td>
	</tr>

	<tr>
		<td colspan="{$currCount+5}" class="rowsSeparator"></td>
	</tr>

	{* ========================================================================================= *}
	{* =================== SUPPLIER ROWS ======================================================= *}
	{* ========================================================================================= *}

	<tr class="normalSubtitle">
        <td style="color:#555555;background-color:#D4E2F4;padding:3px;" colspan="{$currCount+5}"><span class="{if !$supplierData}incompleteBalance{/if}">{$lang['index']['texts']['supplierInvoiceExpenses']}</span></td>
	</tr>

	{* ===================== LIST ROWS ========================================================= *}
	{foreach from=$supplierData item=thisRow}
		<tr>
			<td class="counter">{counter name=supplierRowsCounter}.</td>
			<td>
				{if $thisRow.serviceNamesList} {$thisRow.serviceNamesList|join:', '}
				{else} - {/if}
			</td>
			<td>{supplierInvoiceLabel ref=$thisRow.idInvoice format="%invoiceNumber% / %invoiceDateStd%"}</td>
			<td>{supplierLabel ref=$thisRow.idSupplier}</td>
			{foreach from=$usedCurrencies item=item key=curr}
				<td class="right rowValue">
					{if $thisRow.currency==$curr}
						{$thisRow.value|priceFormat:$curr:true}
					{else}
						&nbsp;
					{/if}
				</td>
			{/foreach}
			<td class="right rowValue">{$thisRow._EQUIV_|priceFormat:$equivCurrency:true}</td>
		</tr>
	{/foreach}

	{* ===================== TOTAL ============================================================= *}
	<tr class="total">
		<td colspan="4" style="padding:3px;" class="right">{$lang['index']['texts']['totalExpenses']}</td>
		{foreach from=$usedCurrencies item=item key=curr}
			<td class="right rowValue">
				{if $supplierTotals[$curr]}
					{$supplierTotals[$curr]|priceFormat:$curr:true}
				{else}
					&nbsp;
				{/if}
			</td>
		{/foreach}
		<td class="right rowValue">{$supplierTotals._EQUIV_|priceFormat:$equivCurrency:true}</td>
	</tr>

	<tr>
		<td colspan="{$currCount+5}" class="rowsSeparator"></td>
	</tr>

	{* ========================================================================================= *}
	{* =================== BALANCE ROW ========================================================= *}
	{* ========================================================================================= *}

	<tr class="normalSubtitle">
		<td style="color:#555555;background-color:#D4E2F4;padding:3px;" colspan="{$currCount+5}">{$lang['index']['texts']['realBalance']}</td>
	</tr>

	{* ===================== TOTAL ============================================================= *}
	<tr class="totalBalance">
		<td colspan="4" class="right total">{$lang['index']['texts']['totalBalance']}</td>
		{foreach from=$usedCurrencies item=item key=curr}
			<td class="right rowValue">
				{if $balanceTotals[$curr]}
					{$balanceTotals[$curr]|priceFormat:$curr:true}
				{else}
					&nbsp;
				{/if}
			</td>
		{/foreach}
        <td class="right rowValue"><span class="{if $balanceTotals._EQUIV_<=0}negativeBalance{/if}">{$balanceTotals._EQUIV_|priceFormat:$equivCurrency:true}</span></td></td>
	</tr>
    </tbody>
</table>