{include="header"}

{if condition="$notice != ''"}
    <div id="notice"><p>{$notice}</p></div>
{/if}

<div id="quick_summary">
    <h2>{$i18n['balance']}</h2>
    {if condition="$current_user->getAdmin()"}
    <p class="center">{$i18n['home_table_caption']}
        <table id="balance_table">
            <tr>
                <th>{$i18n['owes']}\{$i18n['to']}</th>
                {loop="users"}
                <th>{$value->getDisplayName()}</th>
                {/loop}
            </tr>
            {loop="users"}
                <tr>
                    <th>{$value->getDisplayName()}</th>
                    {loop="users"}
                        {if condition="$balances[$value1->getId()][$value2->getId()] === 'X'"}
                        <td class="cell-disabled"></td>
                        {elseif condition="$balances[$value1->getId()][$value2->getId()] === '-' || ($value2->getId() != $current_user->getId() && !$current_user->getAdmin())"}
                            <td>{$balances[$value1->getId()][$value2->getId()]}</td>
                        {else}
                            <td><a href="?do=payall&amp;from={$value1->getId()}&amp;to={$value2->getId()}&amp;token={$token}">{$balances[$value1->getId()][$value2->getId()]}</a></td>
                        {/if}
                    {/loop}
                </tr>
            {/loop}
        </table>
    {else}
        <table>
            <tr>
                <th>{$i18n['users']}</th>
                <th>{$i18n['you_owe_him']}</th>
                <th>{$i18n['he_owes_you']}/th>
            </tr>
            {loop="$users"}
                {if condition="$balances[$value->getId()][$current_user->getId()] !== 'X' && $balances[$value->getId()][$current_user->getId()] !== '-'"}
                    </tr>
                        <td>{$value->getDisplayName()}</td>
                        <td>-</td>
                        <td>{$balances[$value->getId()][$current_user->getId()]}{$currency}</td>
                    </tr>
                {elseif condition="$balances[$current_user->getId()][$value->getId()] !== 'X' && $balances[$current_user->getId()][$value->getId()] !== '-'"}
                    </tr>
                        <td>{$value->getDisplayName()}</td>
                        <td>{$balances[$current_user->getId()][$value->getId()]}{$currency}</td>
                        <td>-</td>
                    </tr>
                {/if}
            {/loop}
        </table>
        <p class="center">{$i18n['balance']}{$i18n['before_double_ponctuation']}: {if condition="$user_balance > 0"}+{/if}{$user_balance}{$currency}</p>
    {/if}
</div>
<div id="detailed_summary">
    <h2>{$i18n['detailed_list_bills']}{if condition="$all == 0"} {$i18n['for_last_month']}{/if}</h2>

    {if condition="$all == 0"}
    <p class="center"><a href="?all=1">{$i18n['display_all_bills']}</a></p>
    {else}
    <p class="center"><a href="?all=0">{$i18n['only_last_month_bills']}</a></p>
    {/if}

    {if condition="$invoices !== false && count($invoices)>=1"}
        <table id="list_expenses">
            <tr>
                <th>{$i18n['date']}</th>
                <th>{$i18n['paid_by']}</th>
                <th>{$i18n['users_in']}</th>
                <th>{$i18n['amount']}</th>
                <th>{$i18n['what']}{$i18n['before_double_ponctuation']}?</th>
                <th>{$i18n['edit']}</th>
                <th>{$i18n['delete']}</th>
            </tr>
            {loop="invoices"}
                <tr>
                    <td>{$value->getDate('d/m/Y A')}</td>
                    <td>{$users[$value->getBuyer()]->getDisplayName()}</td>
                    <td>
                        {loop="$value->getUsersIn()->get()"}
                            {$users[$key2]->getDisplayName()}
                            {if condition="$value2 > 1"}
                                ({$value2} {$i18n['guests']})
                            {elseif condition="$value2 == 1"}
                                ({$value2} {$i18n['guest']})
                            {/if}
                            {if condition="$value1->getBuyer() != $key2"}
                                -
                                {if condition="$paybacks[$value1->getId()] === false || !in_array($key2, array_keys($paybacks[$value1->getId()]))"}
                                    {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
                                    <a href="?do=confirm_payback&amp;from={$key2}&amp;to={$value1->getBuyer()}&amp;invoice_id={$value1->getId()}&amp;token={$token}" title="{$i18n['confirm_payback']}">
                                    {/if}
                                        {$i18n['remains']} {$value1->getAmountPerPerson($key2)} {$currency}
                                    {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
                                        </a>
                                    {/if}
                                {else}
                                    {if condition="$paybacks[$value1->getId()][$key2]->getAmount() == $value1->getAmountPerPerson($key2)"}
                                        {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
                                            <a href="?do=delete_payback&amp;from={$key2}&amp;to={$value1->getBuyer()}&amp;invoice_id={$value1->getId()}&amp;token={$token}" title="{$i18n['delete_payback']}">
                                        {/if}
                                            {$i18n['paid']}
                                        {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
                                            </a>
                                        {/if}
                                    {else}
                                        {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
                                            <a href="?do=confirm_payback&amp;from={$key2}&amp;to={$value1->getBuyer()}&amp;invoice_id={$value1->getId()}&amp;payback_id={$paybacks[$value1->getId()][$key2]->getId()}&amp;token={$token}" title="{$i18n['confirm_payback']}">
                                        {/if}
                                            {$i18n['remains']} {$value1->getAmountPerPerson($key2) - $paybacks[$value1->getId()][$key2]->getAmount()}{$currency}
                                        {if condition="$current_user->getId() == $value1->getBuyer() || $current_user->getAdmin()"}
                                            </a>
                                        {/if}
                                    {/if}
                                {/if}
                            {/if}
                            <br/>
                        {/loop}
                    </td>
                    <td>{$value->getAmount()}{$currency}</td>
                    <td>{$value->getWhat()}</td>
                    <td>
                        {if condition="$value->getBuyer() == $current_user->getId() || $current_user->getAdmin()"}
                            <a href="index.php?do=edit_invoice&id={$value->getId()}">{$i18n['edit']}</a>
                        {else}
                            -
                        {/if}
                    </td>
                    <td>
                        {if condition="$value->getBuyer() == $current_user->getId() || $current_user->getAdmin()"}
                            <a href="index.php?do=delete_invoice&id={$value->getId()}&token={$token}">{$i18n['delete']}</a>
                        {else}
                            -
                        {/if}
                    </td>
                </tr>
            {/loop}
        </table>
    {else}
    <p class="center">{$i18n['no_bills']}.</p>
    {/if}
</div>
{include="footer"}