{% set absUrl = app.request.getSchemeAndHttpHost() ~ app.request.baseUrl|replace({'/app_dev.php': ''}) %}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="{{ absUrl ~ asset('bundles/rxconnexion/admin/css/report/report_wkpdf.css') }}" />
</head>
<body>
<h2><strong>{{ page_title }}</strong></h2>
<table class="report-data" width="100%">
    <thead class="header">
    <tr>
        <th>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>Note</th>
    </tr>
    </thead>
    <tbody>
    {% set totalCount = 0 %}
    {% set totalBalance = 0 %}
    {% set accountPhase = null %}
    {% set billingDateDays = 0 %}
    {% set billingDateSec = 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 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 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>{{ 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 %}
    </tbody>
</table>
<div style="font-weight: bold">Total Records: {{ totalCount }}</div>
<h5 class="report-header">Account Receivable (Summary)</h5>
<table class="report-data" width="100%">
    <thead class="header">
    <tr>
        <th>Billing Branch</th>
        <th style="text-align: right">Count</th>
        <th style="text-align: right">Accounts Receivable</th>
    </tr>
    </thead>
    <tbody>
    {% for record2 in records2 %}
        {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
        <tr class="{{ trC }}">
            <td>{{ record2.healthFacility }}</td>
            <td style="text-align: right">{{ record2.totalCount }}</td>
            <td style="text-align: right">{{ record2.totalBalance|number_format(2, '.', ',') }}</td>
        </tr>
    {% endfor %}
    <tr>
        <td></td>
        <td style="font-weight: bold; text-align: right">Total: {{ totalCount }}</td>
        <td style="font-weight: bold; text-align: right">Total: {{ totalBalance|number_format(2, '.', ',') }}</td>
    </tr>
    </tbody>
</table>
</body>
</html>