{% extends "base.html.twig" %}

{% trans_default_domain 'core' %}

{% block actionheader %}
{% endblock %}

{% block searchform_container %}
{% endblock %}

{% block body %}

<div class="container-fluid">

    <div class="row">
        <div class="col-md-12">

            <div class="panel panel-primary">

                <div class="panel-heading clearfix">

                    <strong style="font-size:18px;">
                        <span class="glyphicon glyphicon-shopping-cart"></span>
                        Compra #{{ compra.id }}
                    </strong>

                    <div class="pull-right">

                        <a href="{{ path('compras_index') }}"
                           class="btn btn-default btn-sm">

                            <span class="glyphicon glyphicon-arrow-left"></span>
                            Volver

                        </a>

                        {% if compra.estado != 'ANULADA' %}

                            <a href="{{ path('compras_editar', {'id': compra.id}) }}"
                               class="btn btn-warning btn-sm">

                                <span class="glyphicon glyphicon-edit"></span>
                                Editar

                            </a>

                            <form method="post"
                                  action="{{ path('compras_eliminar', {'id': compra.id}) }}"
                                  style="display:inline;"
                                  onsubmit="return confirm('¿Está seguro de eliminar esta compra? Esta acción también revertirá el inventario.');">

                                <input type="hidden"
                                       name="_token"
                                       value="{{ csrf_token('eliminar_compra_' ~ compra.id) }}">

                                <button type="submit"
                                        class="btn btn-danger btn-sm">

                                    <span class="glyphicon glyphicon-trash"></span>
                                    Eliminar

                                </button>

                            </form>

                        {% endif %}

                    </div>

                </div>


                <div class="panel-body">


                    <!-- INFORMACION GENERAL -->

                    <div class="row">

                        <div class="col-md-4">

                            <div class="form-group">

                                <label>Proveedor</label>

                                <div class="form-control"
                                     style="height:auto; min-height:34px;">

                                    {% if compra.provider %}

                                        <strong>
                                            {{ compra.provider.name }}
                                        </strong>

                                        {% if compra.provider.identification %}
                                            <br>
                                            <small>
                                                {{ compra.provider.identification }}
                                            </small>
                                        {% endif %}

                                    {% else %}

                                        -

                                    {% endif %}

                                </div>

                            </div>

                        </div>


                        <div class="col-md-2">

                            <div class="form-group">

                                <label>Fecha</label>

                                <div class="form-control">
                                    {{ compra.fecha|date('Y-m-d') }}
                                </div>

                            </div>

                        </div>


                        <div class="col-md-2">

                            <div class="form-group">

                                <label>Tipo documento</label>

                                <div class="form-control">

                                    {% if compra.tipoDocumento == '01' %}
                                        Factura
                                    {% elseif compra.tipoDocumento == '03' %}
                                        Liquidación de compra
                                    {% elseif compra.tipoDocumento == '04' %}
                                        Nota de crédito
                                    {% elseif compra.tipoDocumento == '05' %}
                                        Nota de débito
                                    {% else %}
                                        {{ compra.tipoDocumento }}
                                    {% endif %}

                                </div>

                            </div>

                        </div>


                        <div class="col-md-2">

                            <div class="form-group">

                                <label>Serie</label>

                                <div class="form-control">
                                    {{ compra.serie ?: '-' }}
                                </div>

                            </div>

                        </div>


                        <div class="col-md-2">

                            <div class="form-group">

                                <label>Número</label>

                                <div class="form-control">
                                    {{ compra.numeroDocumento }}
                                </div>

                            </div>

                        </div>

                    </div>


                    <hr>


                    <h4>
                        Detalle de la compra
                    </h4>


                    <div class="table-responsive">

                       <table class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>Producto</th>
            <th class="text-right">Cantidad</th>
            <th class="text-right">Precio 1</th>
            <th class="text-right">Precio 2</th>
            <th class="text-right">Descuento</th>
            <th class="text-right">IVA</th>
            <th>Percha</th>
            <th>Lote</th>
            <th>Fecha caduca</th>
            <th class="text-right">Total</th>
        </tr>
    </thead>

    <tbody>
    {% for item in compra.items %}

        {% set existencia = null %}

        {% if item.product %}
            {% set existencia = item.product.inventarioExistencia %}
        {% endif %}

        <tr>
            <td>
                {% if item.product %}
                    <strong>{{ item.product.reference }}</strong>

                    {% if item.descripcion %}
                        - {{ item.descripcion }}
                    {% endif %}
                {% else %}
                    {{ item.descripcion }}
                {% endif %}
            </td>

            <td class="text-right">
                {{ item.cantidad|number_format(6, '.', ',') }}
            </td>

            <td class="text-right">
                $ {{ item.precioUnitario|number_format(6, '.', ',') }}
            </td>

            <td class="text-right">
                {% if item.product and item.product.price2 is not null %}
                    $ {{ item.product.price2|number_format(6, '.', ',') }}
                {% else %}
                    -
                {% endif %}
            </td>

            <td class="text-right">
                $ {{ item.descuento|number_format(2, '.', ',') }}
            </td>

            <td class="text-right">
                {{ item.porcentajeIva|number_format(2, '.', ',') }}%
                <br>
                <small>
                    $ {{ item.iva|number_format(2, '.', ',') }}
                </small>
            </td>

            <td>
                {% if existencia and existencia.percha %}
                    {{ existencia.percha }}
                {% else %}
                    -
                {% endif %}
            </td>

            <td>
                {% if existencia and existencia.lote %}
                    {{ existencia.lote }}
                {% else %}
                    -
                {% endif %}
            </td>

            <td>
                {% if existencia and existencia.fechaCaduca %}
                    {{ existencia.fechaCaduca|date('d/m/Y') }}
                {% else %}
                    -
                {% endif %}
            </td>

            <td class="text-right">
                <strong>
                    $ {{ item.total|number_format(2, '.', ',') }}
                </strong>
            </td>
        </tr>

    {% endfor %}
    </tbody>
</table>

                    </div>


                    <!-- TOTALES -->

                    <div class="row">

                        <div class="col-md-4 col-md-offset-8">

                            <table class="table table-bordered">

                                <tr>

                                    <th>
                                        Subtotal
                                    </th>

                                    <td class="text-right">
                                        $ {{ compra.subtotal|number_format(2, '.', ',') }}
                                    </td>

                                </tr>


                                <tr>

                                    <th>
                                        Descuento
                                    </th>

                                    <td class="text-right">
                                        $ {{ compra.descuento|number_format(2, '.', ',') }}
                                    </td>

                                </tr>


                                <tr>

                                    <th>
                                        Base IVA
                                    </th>

                                    <td class="text-right">
                                        $ {{ compra.baseIva|number_format(2, '.', ',') }}
                                    </td>

                                </tr>


                                <tr>

                                    <th>
                                        Base 0%
                                    </th>

                                    <td class="text-right">
                                        $ {{ compra.baseCero|number_format(2, '.', ',') }}
                                    </td>

                                </tr>


                                <tr>

                                    <th>
                                        IVA
                                    </th>

                                    <td class="text-right">
                                        $ {{ compra.iva|number_format(2, '.', ',') }}
                                    </td>

                                </tr>


                                <tr>

                                    <th>
                                        Total
                                    </th>

                                    <td class="text-right">

                                        <strong style="font-size:18px;">
                                            $ {{ compra.total|number_format(2, '.', ',') }}
                                        </strong>

                                    </td>

                                </tr>

                            </table>

                        </div>

                    </div>


                    {% if compra.observacion %}

                        <hr>

                        <div class="form-group">

                            <label>
                                Observación
                            </label>

                            <div class="well">
                                {{ compra.observacion|nl2br }}
                            </div>

                        </div>

                    {% endif %}


                    <div class="row">

                        <div class="col-md-6">

                            <small class="text-muted">

                                Estado:
                                <strong>{{ compra.estado }}</strong>

                            </small>

                        </div>

                        <div class="col-md-6 text-right">

                            {% if compra.fechaActualizacion %}

                                <small class="text-muted">

                                    Última actualización:
                                    {{ compra.fechaActualizacion|date('Y-m-d H:i:s') }}

                                </small>

                            {% endif %}

                        </div>

                    </div>


                </div>

            </div>

        </div>
    </div>

</div>

{% endblock %}
