{% extends 'base_rigs.html' %}

{% block title %}Invoice {{ object.pk }}{% endblock %}

{% block content %}
    <div class="col-sm-12">
        <div class="row">
            <div class="col-sm-8">
                <h2>Invoice {{ object.pk }} ({{ object.invoice_date|date:"d/m/Y"}})</h2>
            </div>

            <div class="col-sm-4 text-right">
                <div class="btn-group btn-page">
                <a href="{% url 'invoice_delete' object.pk %}" class="btn btn-default" title="Delete Invoice">
                    <span class="glyphicon glyphicon-remove"></span> <span
                            class="hidden-xs">Delete</span>
                </a>
                <a href="{% url 'invoice_void' object.pk %}" class="btn btn-default" title="Void Invoice">
                    <span class="glyphicon glyphicon-ban-circle"></span> <span
                            class="hidden-xs">Void</span>
                </a>
                <a href="{% url 'invoice_print' object.pk %}" target="_blank" title="Print Invoice" class="btn btn-default"><span
                            class="glyphicon glyphicon-print"></span> <span
                            class="hidden-xs">Print</span></a>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-6">
                <div class="panel panel-default">
                    <div class="panel-heading">Invoice Details</div>
                    <div class="panel-body">
                        {% if object.event.organisation %}
                            {{ object.event.organisation.name }}<br/>
                            {{ object.event.organisation.address|linebreaksbr }}
                        {% else %}
                            {{ object.event.person.name }}<br/>
                            {{ object.event.person.address|linebreaksbr }}
                        {% endif %}
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="panel panel-{% if object.is_closed %}success{% else %}warning{% endif %}">
                    <div class="panel-heading">Event Details<span class="pull-right">
                        {% if object.void %}(VOID){% elif object.is_closed %}(PAID){% else %}(OUTSTANDING){% endif %}
                        </span>
                    </div>
                    <div class="panel-body">
                        <dl class="dl-horizontal">
                            <dt>Event Number</dt>
                            <dd>N{{ object.event.pk|stringformat:"05d" }}</dd>

                            <dt>Event</dt>
                            <dd>{{ object.event.name }}</dd>

                            <dt>Event Venue</dt>
                            <dd>{{ object.event.venue }}</dd>

                            <dt>Event MIC</dt>
                            <dd>{{ object.event.mic.name }}</dd>

                            <dt>Event Starts</dt>
                            <dd>{{ object.event.start_date|date:"d M Y" }} {{ object.event.start_time|date:"H:i" }}</dd>

                            <dt>Event Ends</dt>
                            <dd>{{ object.event.end_date|date:"d M Y" }} {{ object.event.end_time|date:"H:i" }}</dd>

                            <dt>Status</dt>
                            <dd>{{ object.event.get_status_display }}</dd>

                            {% if object.event.dry_hire %}
                                <dd>&nbsp;</dd>
                                <dt>Checked In By</dt>
                                <dd>{{ object.checked_in_by.name }}</dd>
                            {% endif %}

                            <dd>&nbsp;</dd>

                            <dt>Authorised</dt>
                            <dd>{{ object.event.authorised|yesno:"Yes,No" }}</dd>

                            <dt>Authorised by</dt>
                            <dd>
                                {%  if object.event.authorised %}
                                    {{ object.event.authorisation.name }}
                                    (<a href="mailto:{{ object.event.authorisation.email }}">{{ object.event.authorisation.email }}</a>)
                                {% endif %}
                            </dd>

                            {% if object.event.internal %}
                                {# internal #}
                                <dt>Uni ID</dt>
                                <dd>{{ object.event.authorisation.uni_id }}</dd>

                                <dt>Account code</dt>
                                <dd>{{ object.event.authorisation.account_code }}</dd>
                            {% else %}
                                <dt>PO</dt>
                                <dd>{{ object.event.purchase_order }}</dd>
                            {% endif %}

                            <dt>Authorised at</dt>
                            <dd>{{ object.event.authorisation.last_edited_at }}</dd>

                            <dt>Authorised amount</dt>
                            <dd>
                                {% if object.event.authorised %}
                                    £ {{ object.event.authorisation.amount|floatformat:"2" }}
                                {% endif %}
                            </dd>

                            <dt>Authorisation request sent by</dt>
                            <dd>{{ object.authorisation.sent_by }}</dd>
                        </dl>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-6">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="pull-right">
                            <a href="{% url 'payment_create' %}?invoice={{ object.pk }}"
                               class="btn btn-default modal-href"
                               data-target="#{{ form.person.id_for_label }}">
                                <span class="glyphicon glyphicon-plus"></span> Add
                            </a>
                        </div>
                        <table class="table table-hover">
                            <thead>
                            <tr>
                                <td>Date</td>
                                <td>Amount</td>
                                <td>Method</td>
                                <td></td>
                            </tr>
                            </thead>
                            <tbody>
                            {% for payment in object.payment_set.all %}
                                <tr>
                                    <td>{{ payment.date }}</td>
                                    <td>{{ payment.amount|floatformat:2 }}</td>
                                    <td>{{ payment.get_method_display }}</td>
                                    <td>
                                        <a href="{% url 'payment_delete' payment.pk %}"
                                           class="btn btn-small btn-danger"><span class="glyphicon glyphicon-remove"</a>
                                    </td>
                                </tr>
                            {% endfor %}
                            <tr>
                                <td class="text-right"><strong>Balance:</strong></td>
                                <td>{{ object.balance|floatformat:2 }}</td>
                                <td></td>
                                <td></td>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="col-sm-6">
                <div class="panel panel-default">
                    <div class="panel-body">
                        {% with object.event as object %}
                            {% include 'RIGS/item_table.html' %}
                        {% endwith %}
                    </div>
                </div>
            </div>
        </div>

    </div>
{% endblock %}
