{% set purchaseOrderBreakdown = purchaseInvoice.purchaseOrderbreakdown %}
{% set purchaseOrder = purchaseOrderBreakdown.purchaseOrder %}
{% set materialRequisition = purchaseOrder.materialRequisition %}
{% extends '@RxConnexion/Admin/content.html.twig' %}
{% block content %}
    <div class="row max-width-initial">
        <div class="small-12 columns">
            <nav aria-label="You are here:" role="navigation">
                <ul class="breadcrumbs">
                    <li><a href="{{ path('material_requisition_purchase_invoice') }}">Purchase Invoice</a></li>
                    <li>
                        <span class="show-for-sr">Current: </span> Details
                    </li>
                </ul>
            </nav>
            {%
                include '@RxConnexion/MaterialRequisition/purchase_invoice_overview.html.twig' with
                {
                    purchaseInvoice: purchaseInvoice,
                    purchaseOrderBreakdown: purchaseOrderBreakdown,
                    purchaseOrder: purchaseOrder,
                    materialRequisition: materialRequisition
                }
            %}
            <div class="content-container has-loading">
                <div class="content-container-content hide">
                    {% if purchaseInvoice.status == 'Pending' or purchaseInvoice.status == 'Pending Payment' %}
                        {% if userData.type == 'Super Admin' or 'Admin Purchase Invoice Send' in userData.accesses %}
                            <div class="text-right">
                                {% if purchaseInvoice.status == 'Pending Payment' %}
                                    <a href="javascript:void(0)" class="button small" id="href-purchaseInvoice-send" data-url="{{ path('material_requisition_purchase_invoice_send_after_credit') }}" data-open="send-ask-modal" style="margin: 0 0 10px 10px;">Complete Transaction</a>
                                {% else %}
                                    <a href="javascript:void(0)" class="button small" id="href-purchaseInvoice-send" data-url="{{ path('material_requisition_purchase_invoice_send') }}" data-open="send-ask-modal" style="margin: 0 0 10px 10px;">Complete Transaction</a>
                                {% endif %}
                            </div>
                        {% endif %}
                    {% endif %}
                    <ul class="tabs custom-tabs" data-tabs id="tabs-1">
                        <li class="tabs-title is-active"><a href="#panel-1-payment" aria-selected="true">Payment</a></li>
                        <li class="tabs-title"><a href="#panel-1-item" aria-selected="false">Items</a></li>
                        <li class="tabs-title"><a href="#panel-1-delivery" aria-selected="false">Delivery</a></li>
                        {% if purchaseInvoice.hasCredit %}
                            <li class="tabs-title"><a href="#panel-1-credit-payment" aria-selected="false">Credit Payment</a></li>
                        {% endif %}
                        {% if userData.type == 'Super Admin' or 'Admin Purchase Invoice Other Cost' in userData.accesses %}
                            <li class="tabs-title"><a href="#panel-other-cost" aria-selected="false">Other Cost</a></li>
                        {% endif %}
                    </ul>
                    <div class="tabs-content custom-tabs-content" data-tabs-content="tabs-1">
                        <div class="tabs-panel is-active" id="panel-1-payment">
                            {% if purchaseInvoice.status == 'Pending' and (userData.type == 'Super Admin' or 'Admin Purchase Invoice Payment New' in userData.accesses) %}
                                <div class="text-right">
                                    <a class="href-new-action" href="{{ path('material_requisition_purchase_invoice_payment_form' , {purchaseInvoice_id: purchaseInvoice.idencoded}) }}">New Payment</a>
                                </div>
                            {% endif %}
                            <table class="display responsive" id="purchaseInvoicePayment-datalist" width="100%">
                                <thead>
                                <tr>
                                    <th>Payment Date</th>
                                    {#<th>Payment Method</th>#}
                                    <th class="text-right">Amount</th>
                                    <th>Check No.</th>
                                    <th>Check Date</th>
                                    {#<th>Cleared Date</th>#}
                                    <th>Bank</th>
                                    <th>Action</th>
                                </tr>
                                </thead>
                                <tbody>
                                {% set totalAmount = 0 %}
                                {% for payment in purchaseInvoice.purchaseInvoicePayments %}
                                    {% set bank = payment.bank %}
                                    {% set totalAmount = totalAmount + payment.amount %}
                                    <tr>
                                        <td>{{ payment.getpaymentdateasstring }}</td>
                                        {#<td>{{ payment.paymentmethod }}</td>#}
                                        <td class="text-right">{{ payment.amount|number_format(2,'.',',') }}</td>
                                        <td>{{ payment.checkno }}</td>
                                        <td>{{ payment.checkdateasstring }}</td>
                                        {#<td>{{ payment.cleareddateasstring }}</td>#}
                                        <td>{{ bank ? bank.description : '' }}</td>
                                        <td>
                                            {#<a class="action-button-style" href="{{ path('transaction_print_voucher', {id: payment.transactionLink.transaction.idEncoded}) }}">Print Voucher</a>#}
                                            {% if purchaseInvoice.status == 'Pending' or purchaseInvoice.status == 'Pending Payment' and (userData.type == 'Super Admin' or 'Admin Purchase Invoice Credit Payment New' in userData.accesses) %}
                                                <a class="action-button-style" href="{{ path('material_requisition_purchase_invoice_payment_form' , {purchaseInvoice_id: purchaseInvoice.idencoded, action: 'u', id: payment.idencoded}) }}">Update</a>
                                            {% endif %}
                                        </td>
                                    </tr>
                                {% endfor %}
                                <tr>
                                    {#<td></td>#}
                                    <td class="text-right">Total Payment</td>
                                    <td class="text-right">{{ totalAmount|number_format(2,'.',',') }}</td>
                                    <td></td>
                                    {#<td></td>#}
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                        <div class="tabs-panel" id="panel-1-item">
                            <table class="display responsive" id="purchaseOrderBreakdownItem-datalist" width="100%">
                                <thead>
                                <tr>
                                    <th>Item</th>
                                    <th class="text-right">Qty. in P.O.</th>
                                    <th class="text-right">Qty. Received</th>
                                    <th class="text-right">Price</th>
                                    <th class="text-right">Total Price</th>
                                </tr>
                                </thead>
                                <tbody>
                                {% set tPrice = 0 %}
                                {% for pobi in purchaseOrderBreakdown.purchaseorderbreakdownitems %}
                                    {% set price = pobi.totalprice %}
                                    {% set tPrice = tPrice + price %}
                                    <tr>
                                        <td>{{ pobi.purchaseorderitem.materialrequisitionitem.item.description }}</td>
                                        {#
                                                                                <td>{{ pobi.quantityforreceiving|number_format(2, '.', ',') ~ '/ ' ~ pobi.itemunitmeasurement.description }}</td>
                                        #}
                                        <td class="text-right">{{ pobi.quantity|number_format(0, '.', ',')}}</td>
                                        <td class="text-right">{{ pobi.quantityReceived|number_format(0, '.', ',')}}</td>
                                        <td class="text-right">{{ pobi.purchaseorderitem.price|number_format(2, '.', ',') }}</td>
                                        <td class="text-right">{{ price|number_format(2, '.', ',') }}</td>
                                        {# <td>{{ pobi.quantityreceived|number_format(2, '.', ',') ~ '/ ' ~ pobi.itemunitmeasurement.description }}</td>#}
                                    </tr>
                                {% endfor %}
                                <tr>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td class="text-right">Total</td>
                                    <td class="text-right">{{ tPrice|number_format(2, '.', ',') }}</td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                        <div class="tabs-panel" id="panel-1-delivery">
                            <table class="display responsive" id="purchaseOrderBreakdownItemDelivery-datalist" width="100%">
                                <thead>
                                <tr>
                                    <th>Item</th>
                                    <th>DR Date</th>
                                    <th>DR No.</th>
                                    <th>Invoice Date</th>
                                    <th>Invoice No.</th>
                                    <th class="text-right">Qty. Received</th>
                                    <th class="text-right">Price</th>
                                    <th class="text-right">Total Amount</th>
                                </tr>
                                </thead>
                                <tbody>
                                {% set totalAmt = 0 %}
                                {% for pobi in purchaseOrderBreakdown.purchaseorderbreakdownitems %}
                                    {% for pobid in pobi.purchaseOrderBreakdownItemDeliveries %}
                                        {% set price = pobi.purchaseOrderItem.price %}
                                        {% set tPrice = pobid.quantity * price %}
                                        {% set totalAmt = totalAmt + tPrice %}
                                        <tr>
                                            <td>{{ pobi.purchaseorderitem.materialrequisitionitem.item.description }}</td>
                                            <td>{{ pobid.drDateAsString }}</td>
                                            <td>{{ pobid.drNo }}</td>
                                            <td>{{ pobid.invoiceDateAsString }}</td>
                                            <td>{{ pobid.invoiceNo }}</td>
                                            <td class="text-right">{{ pobid.quantity|number_format(0, '.', ',')}}</td>
                                            <td class="text-right">{{ price|number_format(2, '.', ',')}}</td>
                                            <td class="text-right">{{ tPrice|number_format(2, '.', ',')}}</td>
                                        </tr>
                                    {% endfor %}
                                {% endfor %}
                                <tr>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td class="text-right">Total</td>
                                    <td class="text-right">{{ totalAmt|number_format(2, '.', ',') }}</td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                        {% if purchaseInvoice.hasCredit %}
                            <div class="tabs-panel" id="panel-1-credit-payment">
                                {% if purchaseInvoice.status == 'Pending' or purchaseInvoice.status == 'Pending Payment' and (userData.type == 'Super Admin' or 'Admin Purchase Invoice Credit Payment New' in userData.accesses) %}
                                    <div class="text-right">
                                        <a class="href-new-action" href="{{ path('material_requisition_purchase_invoice_credit_payment_form' , {purchaseInvoice_id: purchaseInvoice.idencoded}) }}">New Payment</a>
                                    </div>
                                {% endif %}
                                <table class="display responsive" id="purchaseInvoiceCreditPayment-datalist" width="100%">
                                    <thead>
                                    <tr>
                                        <th>Payment Date</th>
                                        <th>Payment Method</th>
                                        <th>Amount</th>
                                        <th>Check No.</th>
                                        <th>Check Date</th>
                                        <th>Cleared Date</th>
                                        <th>Bank</th>
                                        <th>Action</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    {% set totalAmount = 0 %}
                                    {% for payment in purchaseInvoice.purchaseInvoiceCreditPayments %}
                                        {% set totalAmount = totalAmount + payment.amount %}
                                        <tr>
                                            <td>{{ payment.getpaymentdateasstring }}</td>
                                            <td>{{ payment.paymentmethod }}</td>
                                            <td>{{ payment.amount | number_format(2,'.',',') }}</td>
                                            <td>{{ payment.checkno }}</td>
                                            <td>{{ payment.checkdateasstring }}</td>
                                            <td>{{ payment.cleareddateasstring }}</td>
                                            <td>{{ payment.bank ? payment.bank.description : '' }}</td>
                                            <td>
                                                {% if purchaseInvoice.status == 'Pending' or purchaseInvoice.status == 'Pending Payment'  and (userData.type == 'Super Admin' or 'Admin Purchase Invoice Credit Payment Update' in userData.accesses) %}
                                                    <a class="action-button-style" href="{{ path('material_requisition_purchase_invoice_credit_payment_form' , {purchaseInvoice_id: purchaseInvoice.idencoded, action: 'u', id: payment.idencoded}) }}">Update</a>
                                                {% endif %}
                                            </td>
                                        </tr>
                                    {% endfor %}
                                    <tr>
                                        <td></td>
                                        <td>Total Payment</td>
                                        <td>{{ totalAmount|number_format(2,'.',',') }}</td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>

                        {% endif %}
                        <div class="tabs-panel" id="panel-other-cost">
                            <div class="text-right">
                                {% if purchaseInvoice.status == 'Pending' and (userData.type == 'Super Admin' or 'Admin Purchase Invoice Other Cost New' in userData.accesses) %}
                                    <a href="javascript:void(0)" class="other-cost-action href-new-action" data-action="n" data-id="0">Add New</a>
                                {% endif %}
                            </div>
                            <table class="display responsive" id="otherCost-datalist" width="100%">
                                <thead>
                                <tr>
                                    <th>Other Cost</th>
                                    <th>Amount</th>
                                    <th>Action</th>
                                </tr>
                                </thead>
                                <tbody>
                                {% set totalOtherCost = 0 %}
                                {% for otherCost in purchaseInvoice.purchaseInvoiceOtherCosts %}
                                    {% set totalOtherCost = totalOtherCost + otherCost.amount %}
                                    <tr>
                                        <td>{{ otherCost.description }}</td>
                                        <td>{{ otherCost.amount|number_format(2, '.', ',') }}</td>
                                        <td>
                                            {% if purchaseInvoice.status == 'Pending' or purchaseInvoice.status == 'Pending Payment' and (userData.type == 'Super Admin' or 'Admin Purchase Invoice Other Cost Update' in userData.accesses) %}
                                                <a href="javascript:void(0)" class="other-cost-action action-button-style" data-action="u" data-id="{{ otherCost.idEncoded }}">Update</a>
                                            {% endif %}
                                        </td>
                                    </tr>
                                {% endfor %}
                                </tbody>
                                <tfoot>
                                <tr>
                                    <td class="text-right">Total</td>
                                    <td>{{ totalOtherCost|number_format(2, '.', ',') }}</td>
                                    <td></td>
                                </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="reveal small" id="modal" data-reveal>
        <div id="modal-content"></div>
        <button class="close-button" data-close aria-label="Close modal" type="button">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    <div class="reveal small" id="send-ask-modal" data-reveal>
        <h4>Are you sure you want to continue?</h4>
        <hr class="max-width-initial">
        <div class="button-container text-right">
            <a href="javascript:void(0);" id="btn-send-ask-modal-continue" data-action="" class="button">&nbsp;&nbsp;Yes&nbsp;&nbsp;</a>
            <a href="javascript:void(0);" class="button secondary no-margin-right" data-close>Cancel</a>
        </div>
        <button class="close-button" data-close aria-label="Close modal" type="button">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    {% include '@RxConnexion/Admin/js.html.twig' %}
    <script language="javascript">
        //<![CDATA[
        purchaseInvoice_view.settings.purchaseInvoice_id = '{{ purchaseInvoice.idencoded }}';
        purchaseInvoice_view.settings.otherCost_form = '{{ path('material_requisition_purchase_invoice_other_cost_form') }}';
        purchaseInvoice_view.settings.sendUrl = jQ('#href-purchaseInvoice-send').data('url');
        jQ(document).ready(purchaseInvoice_view.init);
        //]]>
    </script>
{% endblock %}