{% extends '@RxConnexion/Admin/global.html.twig' %}
{% block body %}
    <h5 class="report-header">{{ page_title }}</h5>
    <table class="report-data" width="100%">
        <thead class="header">
        <tr>
            <th>Invoice No.</th>
            <th>Date</th>
            <th>Patient</th>
            <th>Company</th>
            <th>Branch</th>
            <th>Branch Address</th>
            <th>Amount</th>
            <th>Amount Paid</th>
            <th>Amount Refunded</th>
            <th>Balance</th>
            <th>Status</th>
            <th>Note</th>
        </tr>
        </thead>
        <tbody>
        {% set totalCount = 0 %}
        {% set totalAmt = 0 %}
        {% set totalPaid = 0 %}
        {% set totalRefunded = 0 %}
        {% set totalBalance = 0 %}
        {% for record in records %}
            {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
            {% set totalCount = totalCount + 1 %}
            {% set totalAmt = totalAmt + record.amtDue %}
            {% set totalPaid = totalPaid + record.amtPaid %}
            {% set totalRefunded = totalRefunded + record.amtRefunded %}
            {% set totalBalance = totalBalance + record.amtBalance %}
            <tr class="{{ trC }}">
                <td><a href="{{ path('patient_detail', {patient_id : record.patientId|base64Encode, tab:'b', invoiceNo:record.invoiceNo}) }}" target="_blank">{{ record.invoiceNo }}</a></td>
                <td>{{ record.billingDate }}</td>
                <td><a href="{{ path('patient_detail', {patient_id : record.patientId|base64Encode, tab:'b'}) }}" target="_blank">{{ record.patient }}</a></td>
                <td>{{ record.companyDesc }}</td>
                <td>{{ record.healthFacility }}</td>
                <td>{{ record.healthFacilityAddress }}</td>
                <td class="text-right">{{ record.amtDue|number_format(2, '.', ',') }}</td>
                <td class="text-right">{{ record.amtPaid|number_format(2, '.', ',') }}</td>
                <td class="text-right">{{ record.amtRefunded|number_format(2, '.', ',') }}</td>
                <td class="text-right">{{ record.amtBalance|number_format(2, '.', ',') }}</td>
                <td>{{ record.billingStatus }}</td>
                <td>{{ record.note|raw }}</td>
            </tr>
        {% endfor %}
        <tr>
            <td><strong>Total:</strong></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td class="text-right"><strong>{{ totalAmt|number_format(2, '.', ',') }}</strong></td>
            <td class="text-right"><strong>{{ totalPaid|number_format(2, '.', ',') }}</strong></td>
            <td class="text-right"><strong>{{ totalRefunded|number_format(2, '.', ',') }}</strong></td>
            <td class="text-right"><strong>{{ totalBalance|number_format(2, '.', ',') }}</strong></td>
            <td></td>
            <td></td>
        </tr>
    </table>
    <div style="margin-bottom: 10px;">Total Record(s): {{ totalCount }}</div>
    <div style="width: 100%;">
        <div style="width: 49.5%; float: left;">
            <h5 class="report-header">Summary</h5>
            <table class="report-data" width="100%" style="margin-bottom: 10px;">
                <thead class="header">
                <tr>
                    <th width="33%">Branch Address</th>
                    <th style="text-align: right;" width="33%">Count</th>
                    <th style="text-align: right;" width="33%">Invoice</th>
                </tr>
                </thead>
                <tbody>
                {% set summaryTotal = 0 %}
                {% set summaryTotalAmtDue = 0 %}
                {% for record2 in records2.invoiceSummary %}
                    {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
                    {% set summaryTotal = summaryTotal + record2.totalCount %}
                    {% set summaryTotalAmtDue = summaryTotalAmtDue + record2.totalAmtDue %}
                    <tr class="{{ trC }}">
                        <td>{{ record2.healthFacility }}</td>
                        <td style="text-align: right;">{{ record2.totalCount }}</td>
                        <td style="text-align: right;">{{ record2.totalAmtDue|number_format(2, '.', ',') }}</td>
                    </tr>
                {% endfor %}
                <tr>
                    <td></td>
                    <td style="text-align: right;"><strong>Total: {{ summaryTotal }}</strong></td>
                    <td style="text-align: right;"><strong>Total: {{ summaryTotalAmtDue|number_format(2, '.', ',') }}</strong></td>
                </tr>
                </tbody>
            </table>
        </div>
        <div style="width: 49.5%; float: right;">
            <h5 class="report-header">Financing Company Summary</h5>
            <table class="report-data" width="100%" style="margin-bottom: 10px;">
                <thead class="header">
                <tr>
                    <th width="33%">Financing Company</th>
                    <th style="text-align: right;" width="33%">Count</th>
                    <th style="text-align: right;" width="33%">Invoice</th>
                </tr>
                </thead>
                <tbody>
                {% set summaryFinancingTotal = 0 %}
                {% set summaryFinancingTotalAmtDue = 0 %}
                {% for financingSummary in records2.financing %}
                    {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
                    {% set summaryFinancingTotal = summaryFinancingTotal + financingSummary.totalCount %}
                    {% set summaryFinancingTotalAmtDue = summaryFinancingTotalAmtDue + financingSummary.totalAmtDue %}
                    <tr class="{{ trC }}">
                        <td>{{ financingSummary.healthFacility }}</td>
                        <td style="text-align: right;">{{ financingSummary.totalCount }}</td>
                        <td style="text-align: right;">{{ financingSummary.totalAmtDue|number_format(2, '.', ',') }}</td>
                    </tr>
                {% endfor %}
                <tr>
                    <td></td>
                    <td style="text-align: right;"><strong>Total: {{ summaryFinancingTotal }}</strong></td>
                    <td style="text-align: right;"><strong>Total: {{ summaryFinancingTotalAmtDue|number_format(2, '.', ',') }}</strong></td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div style="width: 100%; float: left;">
        <h5 class="report-header">Long Term Summary</h5>
        <table class="report-data" width="100%">
            <thead class="header">
            <tr>
                <th width="33%">Branch Address</th>
                <th style="text-align: right;" width="33%">Count</th>
                <th style="text-align: right;" width="33%">Total Amount</th>
            </tr>
            </thead>
            <tbody>
            {% set longTermTotalCount = 0 %}
            {% set totalPrice = 0 %}
            {% for record3 in records3.longTerm %}
                {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
                {% set longTermTotalCount = longTermTotalCount + record3.longTermCount %}
                {% set totalPrice = totalPrice + record3.price %}
                <tr class="{{ trC }}">
                    <td>{{ record3.billingBranch }}</td>
                    <td style="text-align: right;"><a href="{{ path('report_invoice_itemised_preview', {date_from: dateFrom, date_to: dateTo, patient: 'null', company: 'null', branch: 'null', branchAddress: record3.billingBranchId, term: 'long_term',  item_type : 'null' }) }}" target="_blank">{{ record3.longTermCount }}</a></td>
                    <td style="text-align: right;">{{ record3.price|number_format(2, '.', ',') }}</td>
                </tr>
            {% endfor %}
            <tr>
                <td></td>
                <td style="text-align: right;"><strong>Total: {{ longTermTotalCount }}</strong></td>
                <td style="text-align: right;"><strong>Total: {{ totalPrice|number_format(2, '.', ',') }}</strong></td>
            </tr>
            </tbody>
        </table>
    </div>
    <div style="width: 100%; float: left;">
        <h5 class="report-header">Rent to Own Summary</h5>
        <table class="report-data" width="100%">
            <thead class="header">
            <tr>
                <th width="33%">Branch Address</th>
                <th style="text-align: right;" width="33%">Count</th>
                <th style="text-align: right;" width="33%">Total Amount</th>
            </tr>
            </thead>
            <tbody>
            {% set rentToTotalCount = 0 %}
            {% set totalPrice = 0 %}
            {% for record4 in records3.rentToOwn %}
                {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
                {% set rentToTotalCount = rentToTotalCount + record4.rentToCount %}
                {% set totalPrice = totalPrice + record4.price %}
                <tr class="{{ trC }}">
                    <td>{{ record4.billingBranch }}</td>
                    <td style="text-align: right;"><a href="{{ path('report_invoice_itemised_preview', {date_from: dateFrom, date_to: dateTo, patient: 'null', company: 'null', branch: 'null', branchAddress: record4.billingBranchId, term: 'rent_to_own',  item_type : 'null' }) }}" target="_blank">{{ record4.rentToCount }}</a></td>
                    <td style="text-align: right;">{{ record4.price|number_format(2, '.', ',') }}</td>
                </tr>
            {% endfor %}
            <tr>
                <td></td>
                <td style="text-align: right;"><strong>Total: {{ rentToTotalCount }}</strong></td>
                <td style="text-align: right;"><strong>Total: {{ totalPrice|number_format(2, '.', ',') }}</strong></td>
            </tr>
            </tbody>
        </table>
    </div>
    <div style="width: 100%; float: left;">
        <h5 class="report-header">Accessory Summary</h5>
        <table class="report-data" width="100%">
            <thead class="header">
            <tr>
                <th width="33%">Branch Address</th>
                <th style="text-align: right;" width="33%">Count</th>
                <th style="text-align: right;" width="33%">Total Amount</th>
            </tr>
            </thead>
            <tbody>
            {% set accessoryTotalCount = 0 %}
            {% set totalPrice = 0 %}
            {% for record5 in records3.accessory %}
                {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
                {% set accessoryTotalCount = accessoryTotalCount + record5.accessoryCount %}
                {% set totalPrice = totalPrice + record5.price %}
                <tr class="{{ trC }}">
                    <td>{{ record5.billingBranch }}</td>
                    <td style="text-align: right;"><a href="{{ path('report_invoice_itemised_preview', {date_from: dateFrom, date_to: dateTo, patient: 'null', company: 'null', branch: 'null', branchAddress: record5.billingBranchId, term: 'null',  item_type : 'accessory' }) }}" target="_blank">{{ record5.accessoryCount }}</a></td>
                    <td style="text-align: right;">{{ record5.price|number_format(2, '.', ',') }}</td>
                </tr>
            {% endfor %}
            <tr>
                <td></td>
                <td style="text-align: right;"><strong>Total: {{ accessoryTotalCount }}</strong></td>
                <td style="text-align: right;"><strong>Total: {{ totalPrice|number_format(2, '.', ',') }}</strong></td>
            </tr>
            </tbody>
        </table>
    </div>
    <div style="width: 100%; float: left;">
        <h5 class="report-header">CPAP Package Summary</h5>
        <table class="report-data" width="100%">
            <thead class="header">
            <tr>
                <th width="33%">Branch Address</th>
                <th style="text-align: right;" width="33%">Count</th>
                <th style="text-align: right;" width="33%">Total Amount</th>
            </tr>
            </thead>
            <tbody>
            {% set cpapPackageTotalCount = 0 %}
            {% set totalPrice = 0 %}
            {% for record6 in records3.cpapPackage %}
                {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
                {% set cpapPackageTotalCount = cpapPackageTotalCount + record6.cpapPackageCount %}
                {% set totalPrice = totalPrice + record6.price %}
                <tr class="{{ trC }}">
                    <td>{{ record6.billingBranch }}</td>
                    <td style="text-align: right;"><a href="{{ path('report_invoice_itemised_preview', {date_from: dateFrom, date_to: dateTo, patient: 'null', company: 'null', branch: 'null', branchAddress: record6.billingBranchId, term: 'null',  item_type : 'cpap_package' }) }}" target="_blank">{{ record6.cpapPackageCount }}</a></td>
                    <td style="text-align: right;">{{ record6.price|number_format(2, '.', ',') }}</td>
                </tr>
            {% endfor %}
            <tr>
                <td></td>
                <td style="text-align: right;"><strong>Total: {{ cpapPackageTotalCount }}</strong></td>
                <td style="text-align: right;"><strong>Total: {{ totalPrice|number_format(2, '.', ',') }}</strong></td>
            </tr>
            </tbody>
        </table>
    </div>
    <div style="width: 100%; float: left;">
        <h5 class="report-header">Medical Supplies Summary</h5>
        <table class="report-data" width="100%">
            <thead class="header">
            <tr>
                <th width="33%">Branch Address</th>
                <th style="text-align: right;" width="33%">Count</th>
                <th style="text-align: right;" width="33%">Total Amount</th>
            </tr>
            </thead>
            <tbody>
            {% set medicalSuppliesTotalCount = 0 %}
            {% set totalPrice = 0 %}
            {% for record7 in records3.medicalSupplies %}
                {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
                {% set medicalSuppliesTotalCount = medicalSuppliesTotalCount + record7.medicalSuppliesCount %}
                {% set totalPrice = totalPrice + record7.price %}
                <tr class="{{ trC }}">
                    <td>{{ record7.billingBranch }}</td>
                    <td style="text-align: right;"><a href="{{ path('report_invoice_itemised_preview', {date_from: dateFrom, date_to: dateTo, patient: 'null', company: 'null', branch: 'null', branchAddress: record7.billingBranchId, term: 'null',  item_type : 'medical_supplies' }) }}" target="_blank">{{ record7.medicalSuppliesCount }}</a></td>
                    <td style="text-align: right;">{{ record7.price|number_format(2, '.', ',') }}</td>
                </tr>
            {% endfor %}
            <tr>
                <td></td>
                <td style="text-align: right;"><strong>Total: {{ medicalSuppliesTotalCount }}</strong></td>
                <td style="text-align: right;"><strong>Total: {{ totalPrice|number_format(2, '.', ',') }}</strong></td>
            </tr>
            </tbody>
        </table>
    </div>
    <div></div>
    <div></div>

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