{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %}

{% import "macro_functions.phtml" as mf %}

{% set nr = invoice.serie ~ "%05s"|format(invoice.nr) %}

{% block meta_title %}{{ 'Proforma invoice'|trans }}{% endblock %}

{% block breadcrumb %}
    <li><a href="{{ '/invoice'|link }}">{{ 'Invoices'|trans }}</a> <span class="divider">/</span></li>
    <li class="active">{% if invoice.status == 'paid' %} {{ 'Receipt'|trans }} {% else %}  {{ 'Invoice'|trans }} {% endif %}{{ nr }}</li>
{% endblock %}

{% block content %}
    {% set seller = invoice.seller %}
    {% set buyer = invoice.buyer %}
    {% set company  = guest.system_company %}

    {% if invoice.status == 'unpaid' %}
        <div class="row">
            <article class="span12 data-block decent">
                <div class="data-container">
                    <header>
                        <h2>{{ 'Payment methods'|trans }}</h2>
                        <p>{{ 'Please choose payment type and pay for your chosen products.'|trans }}</p>
                    </header>
                    <form method="post" action="{{ 'api/guest/invoice/payment'|link }}" class="api-form" data-api-redirect="{{ ('invoice/'~invoice.hash)|link({ 'auto_redirect': 1 }) }}">
                        <input type="hidden" name="hash" value="{{ invoice.hash }}"/>
                        {% for gtw in guest.invoice_gateways %}
                        {% if invoice.currency in gtw.accepted_currencies %}
                        {% set banklink = 'invoice/banklink'|link %}
                        <button type="button"  class="logo-{{gtw.code}} hover-popover" type="radio" name="gateway_id" gateway_id="{{ gtw.id }}" data-toggle="tooltip" title="{{ 'Pay with'|trans }} {{gtw.title}}" onclick="window.location.replace('{{banklink}}/{{invoice.hash}}/{{gtw.id}}')")></button>
                        {% endif %}
                        {% endfor %}
                        <input type="hidden" name="gateway_id" id="gateway_id">
                    </form>
                </div>
            </article>
        </div>
    {% endif %}

<div class="row">
    <article class="span12 data-block">
        <div class="data-container">

        <header>
            <h1>{% if invoice.status == 'paid' %} {{ 'Receipt'|trans }} {% else %}  {{ 'Invoice'|trans }} {% endif %} - {{ nr }}</h1><br/>
            {{ 'You can print this invoice or export it to PDF file by clicking on corresponding button.'|trans }}
            <ul class="data-header-actions">
                <li>
                    <a href="{{ 'invoice/pdf'|link }}/{{ invoice.hash }}" class="btn btn-alt btn-inverse">{{ 'PDF'|trans }}</a>
                </li>
                <li>
                    <a href="{{ 'invoice/print'|link }}/{{ invoice.hash }}" target="_blank" class="btn btn-alt btn-inverse">{{ 'Print'|trans }}</a>
                </li>
            </ul>
        </header>

            <section>
                <div class="row-fluid">
                    <div class="span4">
                        {% if company.logo_url %}
                        <img src="{{ company.logo_url }}" alt="Logo">
                        {% endif %}
                        <dl class="dl-horizontal">
                            <dt>{{ 'Invoice number'|trans }}:</dt>
                            <dd>{{ nr }}</dd>
                            <dt>{{ 'Invoice date'|trans }}:</dt>
                            <dd>{% if invoice.paid_at %}
                                {{ invoice.paid_at|bb_date }}
                                {% else %}
                                {{ invoice.created_at|bb_date }}
                                {% endif %}
                            </dd>
                            <dt>{{ 'Due date'|trans }}:</dt>
                            <dd>{% if invoice.due_at %}
                                {{ invoice.due_at|bb_date }}
                                {% else %}
                                -----
                                {% endif %}
                            </dd>
                            <dt>{{ 'Invoice status'|trans }}:</dt>
                            <dd>
                                <span class="label {% if invoice.status == 'paid' %} label-success{% elseif invoice.status == 'unpaid' %}label-warning{% endif %}">
                                      {{ invoice.status|capitalize }}
                                </span>
                            </dd>
                        </dl>
                    </div>
                    <div class="span4">
                        <div class="well small">
                            <h4>{{ 'Company'|trans }}</h4>
                            <dl class="dl-horizontal">
                                {% if seller.company %}
                                <dt>{{ 'Name'|trans }}:</dt>
                                <dd>{{ seller.company }}</dd>
                                {% endif %}

                                {% if seller.company_vat%}
                                <dt>{{ 'VAT'|trans }}:</dt>
                                <dd>{{ seller.company_vat }}</dd>
                                {% endif %}

                                {% if seller.address%}
                                <dt>{{ 'Address'|trans }}:</dt>
                                <dd>{{ seller.address }}</dd>
                                {% endif %}

                                {% if seller.phone %}
                                <dt>{{ 'Phone'|trans }}:</dt>
                                <dd>{{ seller.phone }}</dd>
                                {% endif %}

                                {% if seller.email %}
                                <dt>{{ 'Email'|trans }}:</dt>
                                <dd>{{ seller.email }}</dd>
                                {% endif %}

                                {% if seller.account_number %}
                                <dt>{{ 'Account'|trans }}:</dt>
                                <dd>{{ seller.account_number }}</dd>
                                {% endif %}

                                {% if seller.note %}
                                <dt>{{ 'Note'|trans }}:</dt>
                                <dd>{{ seller.note }}</dd>
                                {% endif %}
                            </dl>
                        </div>
                    </div>

                    <div class="span4">
                        <div class="well small">
                            <h4>{{ 'Billing & Delivery address'|trans }}</h4>
                            <dl class="dl-horizontal">
                                {% if buyer.first_name or buyer.last_name %}
                                <dt>{{ 'Name'|trans }}:</dt>
                                <dd>{{ buyer.first_name }} {{ buyer.last_name }}</dd>
                                {% endif %}

                                {% if buyer.company %}
                                <dt>{{ 'Company'|trans }}:</dt>
                                <dd>{{ buyer.company }}</dd>
                                {% endif %}

                                {% if buyer.company_number %}
                                <dt>{{ 'Company number'|trans }}:</dt>
                                <dd>{{ buyer.company_number }}</dd>
                                {% endif %}

                                {% if buyer.company_vat %}
                                <dt>{{ 'Company VAT'|trans }}:</dt>
                                <dd>{{ buyer.company_vat }}</dd>
                                {% endif %}

                                {% if buyer.address %}
                                <dt>{{ 'Address'|trans }}:</dt>
                                <dd>{{ buyer.address }}</dd>
                                <dd>{{ buyer.city }}, {{ buyer.state }}</dd>
                                <dd>{{ buyer.zip }}, {{ guest.system_countries[buyer.country] }}</dd>
                                {% endif %}

                                {% if buyer.phone %}
                                <dt>{{ 'Phone'|trans }}:</dt>
                                <dd>{{ buyer.phone }}</dd>
                                {% endif %}
                            </dl>
                        </div>
                    </div>
                </div>

                {% if invoice.text_1 %}
                    <div class="well">
                        {{ invoice.text_1|markdown }}
                    </div>
                {% endif %}

                <table class="table table-striped table-bordered table-condensed table-hover">
                    <thead>
                        <tr>
                            <th>{{ '#'|trans }}</th>
                            <th>{{ 'Title'|trans }}</th>
                            <th>{{ 'Price'|trans }}</th>
                            <th>{{ 'Total'|trans }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for i, item in invoice.lines %}
                        <tr>
                            <td>{{ i+1 }}.</td>
                            <td>
                                {% if item.order_id%}
                                <a href="{{ '/order/service'|link }}/manage/{{ item.order_id }}">{{ item.title }}</a>
                                {% else %}
                                {{ item.title }} 
                                {% endif %}
                                {% if item.quantity > 1 %}
                                x {{ item.quantity }} {{ item.unit }}
                                {% endif %}
                            </td>
                            <td>
                                {{ item.price|money(invoice.currency) }}
                            </td>
                            <td>{{ item.total|money(invoice.currency) }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>

                </table>

                <div class="row-fluid">
                    <div class="span4 offset8">
                        <table class="table table-bordered table-striped">
                            {% if invoice.tax > 0 %}
                            <tr>
                                <td>{{ invoice.taxname }} {{ invoice.taxrate }}%</td>
                                <td>{{ invoice.tax | money(invoice.currency) }}</td>
                            </tr>
                            {% endif %}
                            {% if invoice.discount > 0 %}
                            <tr>
                                <td>{{ 'Discount'|trans }}</td>
                                <td>{{ invoice.discount | money(invoice.currency) }}</td>
                            </tr>
                            {% endif %}
                            <tr>
                                <td><strong>{{ 'Total'|trans }}</strong></td>
                                <td><strong>{{ invoice.total | money(invoice.currency) }}</strong></td>
                            </tr>
                        </table>
                    </div>
                </div>

                {% if invoice.text_2 %}
                    <div class="well">
                        {{ invoice.text_2|markdown }}
                    </div>
                {% endif %}
            </section>
        </div>
    </article>
</div>
{% endblock %}

{% block js%}
<script>
    $(function() {
        $(".hover-popover").tooltip({
            placement: 'top'
        });
    });
</script>
{% endblock%}
