{% extends '@RxConnexion/Admin/global.html.twig' %}
{% block body %}
    <h5 class="report-header" style="width: 140%;">{{ page_title }}</h5>
    <table class="report-data" width="140%">
        <thead class="header">
        <tr>
            <th width="7%">Patient</th>
            <th>Payer</th>
            <th>Billing Branch</th>
            <th>Billing Branch Address</th>
            <th>Company</th>
            <th>Account Phase</th>
            <th>Invoice No.</th>
            <th>Date</th>
            <th>Reference No.</th>
            <th>Last Payment</th>
            <th>Last Payment Amount</th>
            <th>Outstanding Balance</th>
            <th>Total Invoice</th>
            <th>Next Due date (Payment Plan)</th>
            <th>Demand Letter Date</th>
            <th>Date Claim to Insurance</th>
            <th>Date last called</th>
            <th>Endorsed To</th>
            <th width="30%">Note</th>
        </tr>
        </thead>
        <tbody>
        {% set totalCount = 0 %}
        {% set totalBalance = 0 %}
        {% set accountPhase = null %}
        {% set billingDateDays = 0 %}
        {% set billingDateSec = 0 %}
        {% set totalLastPayment = 0 %}
        {% set totalAmtBalance = 0 %}
        {% set totalAmtInvoice = 0 %}

        {% for record in records %}
            {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
            {% set totalCount = totalCount + 1 %}
            {% set totalBalance = totalBalance + record.amtBalance %}

            {% set totalLastPayment = totalLastPayment + record.lastPaymentAmt %}
            {% set totalAmtBalance = totalAmtBalance + record.amtBalance %}
            {% set totalAmtInvoice = totalAmtInvoice + record.amtDue %}


            {% set day = date(record.billingDate).diff(date('now')).days %}

            {% if day <= 29 %}
                {% set accountPhase = 'Current' %}
            {% elseif day <= 59 %}
                {% set accountPhase = '30 Days' %}
            {% elseif day <= 89 %}
                {% set accountPhase = '60 Days' %}
            {% elseif day <= 119 %}
                {% set accountPhase = '90 Days' %}
            {% else %}
                {% set accountPhase = '120 Days or More' %}
            {% endif %}

            <tr class="{{ trC }}">
                <td><a href="{{ path('patient_detail', {patient_id : record.patientId|base64Encode}) }}" target="_blank" style="color: {{ record.isFortyFiveDays == '1' ? 'red' : '' }}">{{ record.patient }}</a></td>
                <td>{{ record.payer }}</td>
                <td>{{ record.healthFacility_desc }}</td>
                <td>{{ record.healthFacilityForAddress_desc }}</td>
                <td>{{ record.notNullHealthFacility_companyDesc }}</td>
                <td>{{ accountPhase }}</td>
                <td>{{ record.invoiceNo }}</td>
                <td>{{ record.billingDate }}</td>
                <td>{{ record.id }}</td>
                <td>{{ record.lastPaymentDate }}</td>
                <td class="text-right">{{ record.lastPaymentAmt|number_format(2, '.', ',') }}</td>
                <td class="text-right">{{ record.amtBalance|number_format(2, '.', ',') }}</td>
                <td class="text-right">{{ record.amtDue|number_format(2, '.', ',') }}</td>
                <td>{{ record.patientBilling_nextDue }}</td>
                <td>{{ record.patientBilling_demandLetterDate }}</td>
                <td>{{ record.patientBilling_insuranceClaimDate }}</td>
                <td>{{ record.lastCalled }}</td>
                <td>{{ record.billing_endorsedTo }}</td>
                <td>{{ record.note|raw }}</td>
            </tr>
        {% endfor %}
            <tr class="">
                <td><strong>Total:</strong></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td class="text-right"><strong>{{ totalLastPayment|number_format(2, '.', ',') }}</strong></td>
                <td class="text-right"><strong>{{ totalAmtBalance|number_format(2, '.', ',') }}</strong></td>
                <td class="text-right"><strong>{{ totalAmtInvoice|number_format(2, '.', ',') }}</strong></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </tbody>
    </table>
    <div style="font-weight: bold">Total Records: {{ totalCount }}</div>

    {% set sumTotalCount = 0 %}
    {% set sumTotalAr = 0 %}
    <h5 class="report-header">Account Receivable (Summary)</h5>
    <table class="report-data" width="100%">
        <thead class="header">
        <tr>
            <th>Billing Branch</th>
            <th class="text-right">Count</th>
            <th class="text-right">Accounts Receivable</th>
        </tr>
        </thead>
        <tbody>
        {% for record2 in records2 %}
            {% set sumTotalCount = sumTotalCount + record2.totalCount %}
            {% set sumTotalAr = sumTotalAr + record2.totalBalance %}
            {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
            <tr class="{{ trC }}">
                <td>{{ record2.healthFacility }}</td>
                <td class="text-right">{{ record2.totalCount }}</td>
                <td class="text-right">{{ record2.totalBalance|number_format(2, '.', ',') }}</td>
            </tr>
        {% endfor %}
            <tr>
                <td></td>
                <td class="text-right" style="font-weight: bold">Total: {{ sumTotalCount }}</td>
                <td class="text-right" style="font-weight: bold">Total: {{ sumTotalAr|number_format(2, '.', ',') }}</td>
            </tr>
        </tbody>
    </table>
    <div></div>
    <div></div>

    {% include '@RxConnexion/Admin/js.html.twig' %}
    <script>
        //<![CDATA[
        jQ(document).ready(function(){
            report_preview.init();
        });
        //]]>
    </script>
{% endblock %}