<?php $currency_symbol = $this->customlib->getSchoolCurrencyFormat(); ?>
<style type="text/css">
    @media print {
        .col-sm-1, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-10, .col-sm-11, .col-sm-12 {
            float: left;
        }
        .col-sm-12 {
            width: 100%;
        }
        .col-sm-11 {
            width: 91.66666667%;
        }
        .col-sm-10 {
            width: 83.33333333%;
        }
        .col-sm-9 {
            width: 75%;
        }
        .col-sm-8 {
            width: 66.66666667%;
        }
        .col-sm-7 {
            width: 58.33333333%;
        }
        .col-sm-6 {
            width: 50%;
        }
        .col-sm-5 {
            width: 41.66666667%;
        }
        .col-sm-4 {
            width: 33.33333333%;
        }
        .col-sm-3 {
            width: 25%;
        }
        .col-sm-2 {
            width: 16.66666667%;
        }
        .col-sm-1 {
            width: 8.33333333%;
        }
        .col-sm-pull-12 {
            right: 100%;
        }
        .col-sm-pull-11 {
            right: 91.66666667%;
        }
        .col-sm-pull-10 {
            right: 83.33333333%;
        }
        .col-sm-pull-9 {
            right: 75%;
        }
        .col-sm-pull-8 {
            right: 66.66666667%;
        }
        .col-sm-pull-7 {
            right: 58.33333333%;
        }
        .col-sm-pull-6 {
            right: 50%;
        }
        .col-sm-pull-5 {
            right: 41.66666667%;
        }
        .col-sm-pull-4 {
            right: 33.33333333%;
        }
        .col-sm-pull-3 {
            right: 25%;
        }
        .col-sm-pull-2 {
            right: 16.66666667%;
        }
        .col-sm-pull-1 {
            right: 8.33333333%;
        }
        .col-sm-pull-0 {
            right: auto;
        }
        .col-sm-push-12 {
            left: 100%;
        }
        .col-sm-push-11 {
            left: 91.66666667%;
        }
        .col-sm-push-10 {
            left: 83.33333333%;
        }
        .col-sm-push-9 {
            left: 75%;
        }
        .col-sm-push-8 {
            left: 66.66666667%;
        }
        .col-sm-push-7 {
            left: 58.33333333%;
        }
        .col-sm-push-6 {
            left: 50%;
        }
        .col-sm-push-5 {
            left: 41.66666667%;
        }
        .col-sm-push-4 {
            left: 33.33333333%;
        }
        .col-sm-push-3 {
            left: 25%;
        }
        .col-sm-push-2 {
            left: 16.66666667%;
        }
        .col-sm-push-1 {
            left: 8.33333333%;
        }
        .col-sm-push-0 {
            left: auto;
        }
        .col-sm-offset-12 {
            margin-left: 100%;
        }
        .col-sm-offset-11 {
            margin-left: 91.66666667%;
        }
        .col-sm-offset-10 {
            margin-left: 83.33333333%;
        }
        .col-sm-offset-9 {
            margin-left: 75%;
        }
        .col-sm-offset-8 {
            margin-left: 66.66666667%;
        }
        .col-sm-offset-7 {
            margin-left: 58.33333333%;
        }
        .col-sm-offset-6 {
            margin-left: 50%;
        }
        .col-sm-offset-5 {
            margin-left: 41.66666667%;
        }
        .col-sm-offset-4 {
            margin-left: 33.33333333%;
        }
        .col-sm-offset-3 {
            margin-left: 25%;
        }
        .col-sm-offset-2 {
            margin-left: 16.66666667%;
        }
        .col-sm-offset-1 {
            margin-left: 8.33333333%;
        }
        .col-sm-offset-0 {
            margin-left: 0%;
        }
        .visible-xs {
            display: none !important;
        }
        .hidden-xs {
            display: block !important;
        }
        table.hidden-xs {
            display: table;
        }
        tr.hidden-xs {
            display: table-row !important;
        }
        th.hidden-xs,
        td.hidden-xs {
            display: table-cell !important;
        }
        .hidden-xs.hidden-print {
            display: none !important;
        }
        .hidden-sm {
            display: none !important;
        }
        .visible-sm {
            display: block !important;
        }
        table.visible-sm {
            display: table;
        }
        tr.visible-sm {
            display: table-row !important;
        }
        th.visible-sm,
        td.visible-sm {
            display: table-cell !important;
        }
    }
</style>

<html lang="en">
    <head>
        <title><?php echo $this->lang->line('fees_receipt'); ?></title>
        <link rel="stylesheet" href="<?php echo base_url(); ?>backend/bootstrap/css/bootstrap.min.css"> 
        <link rel="stylesheet" href="<?php echo base_url(); ?>backend/dist/css/AdminLTE.min.css">
    </head>
    <body> 
        <div class="container">

            <div class="row">

                <div id="content" class="col-lg-12 col-sm-12 ">
                    <div class="invoice">
                        <div class="row header text-center">
                            <div class="col-sm-12">
                                <?php
                                if ($settinglist[0]['image'] != "") {
                                    ?>

                                    <img style="height:70px; " src="<?php echo base_url(); ?>/uploads/school_content/logo/<?php echo $settinglist[0]['image']; ?>"> <br/>
                                    <?php
                                }
                                ?>

                                <strong style="font-size: 20px;"><?php echo $settinglist[0]['name']; ?></strong><br>
                                <?php echo $settinglist[0]['address']; ?><br>

                                <?php echo $this->lang->line('phone'); ?>: <?php echo $settinglist[0]['phone']; ?><br>

                            </div><!--/col-->
                        </div>
                        <div class="row" style="text-align:center;">
                            <div class="col-xs-12">
                                <br/>
                                <address>
                                    <strong><?php echo $exp_title; ?></strong><br/>                                    
                                    <h4 style="color:blue; font-weight:bold;">BALANCES SHEET REPORT</h4><br/>
                                </address> 
                            </div>
                        </div>

                        <hr style="margin-top: 0px;margin-bottom: 0px;" />
                        <div class="row">
                            
                                     <!-- Income Statement Table -->
                                   <table class="table table-bordered" style="width: 100%; font-size: 14px; border-collapse: collapse;">
                <!-- ASSETS -->
                <thead style="background-color:#f0f8ff; color:#fff;">
                    <tr><th colspan="3">Assets</th></tr>
                </thead>
                <tbody>
                    <!-- Current Assets -->
                    <tr style="background-color:#f0f8ff; font-weight:bold;">
                        <td colspan="3">Current Assets</td>
                    </tr>
                    <?php
                            // Calculate cash and grand total from gl_expenseList:
                            $cash = 0;
                            $grand_total = 0;

                            if (!empty($gl_expenseList) && is_array($gl_expenseList)) {
                                foreach ($gl_expenseList as $entry) {
                                    $debit = isset($entry['debit']) ? (float) $entry['debit'] : 0;
                                    $credit = isset($entry['credit']) ? (float) $entry['credit'] : 0;
                                    $total = $debit - $credit;

                                    // Optional: Still track "cash" value if you want to differentiate
                                    $cash += $total;

                                    $grand_total += $total;
                                }
                            }
                            ?>

                        <?php if ($grand_total > 0): ?>
                            <tr>
                                <td>Cash</td>
                                <td class="text-right"></td>
                                <td class="text-right">USD <?= number_format($grand_total, 2) ?></td>
                            </tr>
                        <?php endif; ?>

                         <?php 
                            // Calculate fees total from fees summary with currency conversion
                            $fees_total = 0;
                            $processed_invoices = [];
                            $first_date = null;
                            foreach ($feeList as $invoice) {
                                if (!isset($processed_invoices[$invoice['id']])) {
                                    $processed_invoices[$invoice['id']] = true;

                                    // original invoice amount & payment mode
                                    $original_amount = $invoice['invoice_amount'];
                                    $payment_mode    = $invoice['payment_mode'] ?? '';

                                    // lookup the foreign rate (default to 1)
                                    $query_rate = $this->db
                                        ->select('foreign_rate')
                                        ->from('bank_rates')
                                        ->where('foreign_currency', $payment_mode)
                                        ->get();
                                    $foreign_rate = $query_rate->row()->foreign_rate ?? 1;

                                    // convert any currency to USD
                                    $converted_amount = $original_amount / $foreign_rate;

                                    // accumulate converted amount
                                    $fees_total += $converted_amount;

                                    // capture first date
                                    if (!$first_date && !empty($invoice['date'])) {
                                        $first_date = $invoice['date'];
                                    }
                                }
                            }

                            // Sum up customer invoice totals and calculate discounts
                            $customer_total = 0;
                            $discount_total = 0;
                            if (isset($customerIncomeList) && is_array($customerIncomeList)) {
                                foreach ($customerIncomeList as $customer_invoice) {
                                    if (isset($customer_invoice['quantity'], $customer_invoice['price'])) {
                                        $invoice_total = $customer_invoice['quantity'] * $customer_invoice['price'];
                                        if (isset($customer_invoice['discount'])) {
                                            $discount_total += ($customer_invoice['discount'] / 100) * $invoice_total;
                                        }
                                        $customer_total += $invoice_total;
                                    }
                                }
                            }

                            // Calculate fee discounts and fines
                            $fees_discount_total = 0;
                            $fine_total          = 0;
                            if (isset($feediscount) && is_array($feediscount)) {
                                foreach ($feediscount as $fee_discount) {
                                    $fees_discount_total += $fee_discount['amount_discount'] ?? 0;
                                    $fine_total          += $fee_discount['amount_fine']     ?? 0;
                                }
                            }
                            $discount_total += $fees_discount_total;

                            // Calculate total Uniforms
                            $uniform_total = 0;
                            if (isset($uniformList) && is_array($uniformList)) {
                                foreach ($uniformList as $uniform) {
                                    if (isset($uniform['quantity'], $uniform['amount'])) {
                                        $uniform_total += $uniform['quantity'] * $uniform['amount'];
                                    }
                                }
                            }

                            // Calculate total sales returns and fees returns
                            $sales_returns_total = 0;
                            $fees_returns_total  = 0;
                            if (isset($journalList) && is_array($journalList)) {
                                foreach ($journalList as $journal) {
                                    if (isset($journal['credit'], $journal['cashbook'])) {
                                        $name = strtolower(trim($journal['cashbook']));
                                        if ($name === 'sales returns journal') {
                                            $sales_returns_total += $journal['credit'];
                                        }
                                        if ($name === 'learner returns') {
                                            $fees_returns_total += $journal['credit'];
                                        }
                                    }
                                }
                            }

                            // Final Total Revenue
                            $total_revenue = ($customer_total + $fees_total + $uniform_total + $fine_total)
                                        - ($sales_returns_total + $fees_returns_total + $discount_total);

                            // Assign to Accounts Receivable
                            $accounts_receivable = $total_revenue;
                        ?>

                        <!-- Output Accounts Receivable as one line, only if non‐zero -->
                        <?php if ($accounts_receivable != 0): ?>
                            <tr>
                                <td>Accounts receivable</td>
                                <td class="text-right"></td>
                                <td class="text-right">USD <?= number_format($accounts_receivable, 2) ?></td>
                            </tr>
                        <?php endif; ?>


                                                            <?php
                                    // Initialize variables for total sums
                                    $total_Materials = 0;
                                    $total_Labor = 0;

                                    // Query the supplier_invoice table for records where Module = 'ST'
                                    $this->db->where('Module', 'ST');
                                    $query = $this->db->get('supplier_invoice');
                                    $supplierLists = $query->result_array();

                                    if (!empty($supplierList)) {
                                        foreach ($supplierList as $supplier) {
                                            if (isset($supplier['price']) && isset($supplier['quantity'])) {
                                                $cogs_amount = $supplier['price'] * $supplier['quantity'];
                                                if ($cogs_amount > 0) {
                                                    if (isset($supplier['description']) && stripos($supplier['description'], 'labor') !== false) {
                                                        $total_Labor += $cogs_amount;
                                                    } else {
                                                        $total_Materials += $cogs_amount;
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    // Calculate inventory
                                    $inventory = $total_Materials + $total_Labor;
                                    ?>
                                    <?php if ($inventory > 0): ?>
                                    <tr>
                                        <td>Inventory</td>
                                        <td class="text-right"></td>
                                        <td class="text-right">USD <?= number_format($inventory, 2) ?></td>
                                    </tr>
                                    <?php endif; ?>
                                    <?php if ($prepaid_expenses > 0): ?>
                                        <tr>
                                            <td>Prepaid expenses</td>
                                            <td class="text-right"></td>
                                            <td class="text-right">USD <?= number_format($prepaid_expenses,2) ?></td>
                                </tr>
                                <?php endif; ?>

                                <?php if ($short_term_investments > 0): ?>
                                <tr>
                                    <td>Short‑term investments</td>
                                    <td class="text-right"></td>
                                    <td class="text-right">USD<?= number_format($short_term_investments,2) ?></td>
                                </tr>
                                <?php endif; ?>
                                <?php
                                    // Calculate total current assets by summing all non‐zero asset values
                                    $total_current_assets = 0;
                                    if ($grand_total > 0) {
                                        $total_current_assets += $grand_total;
                                    }
                                    if (!empty($accounts_receivable) && $accounts_receivable > 0) {
                                        $total_current_assets += $accounts_receivable;
                                    }
                                    if (!empty($inventory) && $inventory > 0) {
                                        $total_current_assets += $inventory;
                                    }
                                    if (!empty($prepaid_expenses) && $prepaid_expenses > 0) {
                                        $total_current_assets += $prepaid_expenses;
                                    }
                                    if (!empty($short_term_investments) && $short_term_investments > 0) {
                                        $total_current_assets += $short_term_investments;
                                    }
                                    ?>

                                    <tr style="font-weight:bold;">
                                        <td>Total current assets</td>
                                        <td class="text-right"></td>
                                        <td class="text-right">USD <?= number_format($total_current_assets, 2) ?></td>
                                    </tr>

                                    <!-- Fixed Assets -->
                        
                                    <tr style="background-color:#f0f8ff; font-weight:bold;">
                                        <td colspan="3">Fixed (Long-Term) Assets</td>
                                    </tr>
                                    <?php
                                    // Fixed Assets Section
                                    $netBookValues = [];
                                    $groups = [];

                                    if (!empty($assetList) && is_array($assetList)) {
                                        // Group entries by code
                                        foreach ($assetList as $entry) {
                                            if (strtolower($entry['module']) !== 'gl') continue;
                                            $debit  = floatval($entry['debit']);
                                            $credit = floatval($entry['credit']);
                                            $label  = null;
                                            $code   = null;

                                            if (!empty($entry['glid']) && strtolower($entry['gl_account_type']) === 'fixed assets') {
                                                $label = $entry['gl_name'];
                                                $code  = $entry['gl_code'];
                                            } elseif (!empty($entry['sub_name']) && strtolower($entry['parent_account_type']) === 'fixed assets') {
                                                $label = $entry['sub_name'];
                                                $code  = $entry['sub_account_type'];
                                            }
                                            if ($label === null || $code === null) continue;

                                            $groups[$code][] = ['label' => $label, 'debit' => $debit, 'credit' => $credit];
                                        }

                                        // Process each asset code group
                                        foreach ($groups as $code => $rows) {
                                            // Aggregate by label
                                            $debitByLabel  = [];
                                            $creditByLabel = [];
                                            foreach ($rows as $r) {
                                                if ($r['debit'] > 0) {
                                                    $debitByLabel[$r['label']] = ($debitByLabel[$r['label']] ?? 0) + $r['debit'];
                                                }
                                                if ($r['credit'] > 0) {
                                                    $creditByLabel[$r['label']] = ($creditByLabel[$r['label']] ?? 0) + $r['credit'];
                                                }
                                            }

                                            // Display combined debits
                                            foreach ($debitByLabel as $label => $amt) {
                                                echo '<tr>';
                                                echo '  <td>' . htmlspecialchars($label) . '</td>';
                                                echo '  <td></td>';
                                                echo '  <td class="text-right">USD ' . number_format($amt, 2) . '</td>';
                                                echo '</tr>';
                                            }

                                            // Display combined credits
                                            foreach ($creditByLabel as $label => $amt) {
                                                echo '<tr>';
                                                echo '  <td>&nbsp;&nbsp;' . htmlspecialchars($label) . '</td>';
                                                echo '  <td></td>';
                                                echo '  <td class="text-right">(USD ' . number_format($amt, 2) . ')</td>';
                                                echo '</tr>';
                                            }

                                            // Net Book Value for this code
                                            $sumDebit  = array_sum($debitByLabel);
                                            $sumCredit = array_sum($creditByLabel);
                                            $net       = $sumDebit - $sumCredit;
                                            $netBookValues[] = $net;
                                            $netFmt    = $net < 0
                                                ? '(USD ' . number_format(abs($net), 2) . ')'
                                                : 'USD ' . number_format($net, 2);

                                            echo '<tr>';
                                            echo '  <td><strong>&nbsp;&nbsp;Net Book Value</strong></td>';
                                            echo '  <td></td>';
                                            echo '  <td class="text-right"><strong>' . $netFmt . '</strong></td>';
                                            echo '</tr>';
                                        }
                                    }

                                    // Total Fixed Assets = sum of all group net book values
                                    $total_fixed_assets = array_sum($netBookValues);

                                    if ($total_fixed_assets != 0) {
                                        $totalFmt = $total_fixed_assets < 0
                                            ? '(USD ' . number_format(abs($total_fixed_assets), 2) . ')'
                                            : 'USD ' . number_format($total_fixed_assets, 2);

                                        echo '<tr class="font-weight-bold">';
                                        echo '  <td><strong>Total Fixed Assets</strong></td>';
                                        echo '  <td></td>';
                                        echo '  <td class="text-right"><strong>' . $totalFmt . '</strong></td>';
                                        echo '</tr>';
                                    }
                                    ?>


                    
                                                <!-- Other Assets Header -->
                            <tr style="background-color:#f0f8ff; font-weight:bold;">
                                <td colspan="3">Other Assets</td>
                            </tr>

                            <?php if (!empty($otherasset)): ?>
                                <?php foreach ($otherasset as $asset): ?>
                                    <tr>
                                        <td><?= htmlspecialchars($asset['description']) ?></td>
                                        <td class="text-right"></td>
                                        <td class="text-right"><?= number_format($asset['purchase_price'], 2) ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="3" class="text-center"></td>
                                </tr>
                            <?php endif; ?>

                            <!-- Total Other Assets -->
                             <?php if ($total_other_assets > 0): ?>
                            <tr style="font-weight:bold;">
                                <td>Total Other Assets</td>
                                <td class="text-right"></td>
                                <td class="text-right"><?= number_format($total_other_assets, 2) ?></td>
                            </tr>
                            <?php endif; ?>

                            <?php
                            // Calculate Total Assets as sum of current, fixed & other assets
                            $total_assets = floatval($total_current_assets  ?? 0)
                                        + floatval($total_fixed_assets    ?? 0)
                                        + floatval($total_other_assets    ?? 0);
                            ?>
                            <tr style="background-color:#d9e1f2; color:black; font-weight:bold;">
                                <td>Total Assets</td>
                                <td class="text-right"></td>
                                <td class="text-right">USD <?= number_format($total_assets, 2) ?></td>
                            </tr>


                <!-- LIABILITIES & OWNER’S EQUITY -->
                <thead style="background-color:#f0f8ff; color:#fff;">
                    <tr><th colspan="3">Liabilities and Owner’s Equity</th></tr>
                </thead>
                <tbody>
                    <!-- Current Liabilities -->
                    <tr style="background-color:#f0f8ff; font-weight:bold;">
                        <td colspan="3">Current Liabilities</td>
                    </tr>
                    <?php
                    // Compute COGS → Accounts Payable
                    $accounts_payable = 0;
                    if (!empty($supplierList) && is_array($supplierList)) {
                        foreach ($supplierList as $supplier) {
                            $price    = isset($supplier['price'])    ? (float)$supplier['price']    : 0;
                            $quantity = isset($supplier['quantity']) ? (float)$supplier['quantity'] : 0;
                            $accounts_payable += $price * $quantity;
                        }
                    }
                    ?>

                    <?php if ($accounts_payable > 0): ?>
                        <tr>
                            <td>Accounts payable</td>
                            <td class="text-right"></td>
                            <td class="text-right">USD <?= number_format($accounts_payable, 2) ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php if ($short_term_loans > 0): ?>
                    <tr>
                        <td>Short‑term loans</td>
                        <td class="text-right"></td>
                        <td class="text-right"><?= number_format($short_term_loans,2) ?></td>
                    </tr>
                    <?php endif; ?>
                    <?php if ($income_taxes_payable > 0): ?>
                    <tr>
                        <td>Income taxes payable</td>
                        <td class="text-right"></td>
                        <td class="text-right"><?= number_format($income_taxes_payable,2) ?></td>
                    </tr>
                    <?php endif; ?>
                    <?php if ($accrued_salaries > 0): ?>
                    <tr>
                        <td>Accrued salaries and wages</td>
                        <td class="text-right"></td>
                        <td class="text-right"><?= number_format($accrued_salaries,2) ?></td>
                    </tr>
                    <?php endif; ?>
                    <?php if ($unearned_revenue > 0): ?>
                    <tr>
                        <td>Unearned revenue</td>
                        <td class="text-right"></td>
                        <td class="text-right"><?= number_format($unearned_revenue,2) ?></td>
                    </tr>
                    <?php endif; ?>
                    <?php if ($current_portion_debt > 0): ?>
                    <tr>
                        <td>Current portion of long‑term debt</td>
                        <td class="text-right"></td>
                        <td class="text-right"><?= number_format($current_portion_debt,2) ?></td>
                    </tr>
                    <?php endif; ?>
                    <?php
                    // Sum up all current‐liability balances
                    $total_current_liabilities =
                        $accounts_payable
                        + (float)($short_term_loans       ?? 0)
                        + (float)($income_taxes_payable   ?? 0)
                        + (float)($accrued_salaries       ?? 0)
                        + (float)($unearned_revenue       ?? 0)
                        + (float)($current_portion_debt   ?? 0);
                    ?>

                    <?php if ($total_current_liabilities > 0): ?>
                    <tr style="font-weight:bold;">
                        <td>Total current liabilities</td>
                        <td class="text-right"></td>
                        <td class="text-right">USD <?= number_format($total_current_liabilities, 2) ?></td>
                    </tr>
                    <?php endif; ?>


                    <!-- Long‑Term Liabilities -->
                    <tr style="background-color:#f0f8ff; font-weight:bold;">
                        <td colspan="3">Long‑Term Liabilities</td>
                    </tr>
                    <?php if ($long_term_debt > 0): ?>
                    <tr>
                        <td>Long‑term debt</td>
                        <td class="text-right"></td>
                        <td class="text-right"><?= number_format($long_term_debt,2) ?></td>
                    </tr>
                    <?php endif; ?>
                    <?php if ($deferred_liabilities > 0): ?>
                    <tr>
                        <td>Deferred income tax</td>
                        <td class="text-right"></td>
                        <td class="text-right"><?= number_format($deferred_liabilities,2) ?></td>
                    </tr>
                    <?php endif; ?>
                    <?php if ($total_long_term_liabilities > 0): ?>
                    <tr style="font-weight:bold;">
                        <td>Total long-term liabilities</td>
                        <td class="text-right"></td>
                        <td class="text-right"><?= number_format($total_long_term_liabilities,2) ?></td>
                    </tr>
                    <?php endif; ?>

                    <!-- Owner’s Equity -->
                    <tr style="background-color:#f0f8ff; font-weight:bold;">
                        <td colspan="3">Owner’s Equity</td>
                    </tr>

                    <?php if (!empty($owners_investment) && $owners_investment > 0): ?>
                    <tr>
                        <td>Owner’s investment</td>
                        <td class="text-right"></td>
                        <td class="text-right"><?= number_format($owners_investment,2) ?></td>
                    </tr>
                    <?php endif; ?>
                    






<!--------------------------------Retained Earnings------------------------------------------>

                     
                   <?php if (isset($net_profit) && $net_profit != 0): ?>
                    <tr>
                        <td>Net Profit</td>
                        <td class="text-right"></td>
                        <td class="text-right"><?= number_format($net_profit, 2) ?></td>
                    </tr>
                    <?php endif; ?>

                    <?php if (isset($retained_earnings) && $retained_earnings != 0): ?>
                    <tr>
                        <td>Retained Earnings</td>
                        <td class="text-right"></td>
                        <td class="text-right"><?= number_format($retained_earnings, 2) ?></td>
                    </tr>
                    <?php endif; ?>

                    <?php if (!empty($other_equity) && $other_equity > 0): ?>
                    <tr>
                        <td>Other</td>
                        <td class="text-right"></td>
                        <td class="text-right"><?= number_format($other_equity,2) ?></td>
                    </tr>
                    <?php endif; ?>
                    <?php
                        // Calculate Total Owner’s Equity
                        $total_equity = 
                            (float)($other_equity   ?? 0)
                            + (float)($net_profit               ?? 0)
                             + (float)($retained_earnings               ?? 0);
                        ?>

                    <?php if ($total_equity > 0): ?>
                        <tr style="font-weight:bold;">
                            <td>Total Owner’s Equity</td>
                            <td class="text-right"></td>
                            <td class="text-right"><?= number_format($total_equity, 2) ?></td>
                        </tr>
                        <?php endif; ?>

                        <?php
                        // Calculate Total Liabilities & Owner’s Equity
                        $total_liabilities_and_equity = 
                            (float)($total_current_liabilities   ?? 0)
                            + (float)($total_long_term_liabilities ?? 0)
                            + (float)($total_equity               ?? 0);
                        ?>

                    <?php if ($total_liabilities_and_equity > 0): ?>
                    <tr style="background-color:#d9e1f2; color:black; font-weight:bold;">
                        <td>Total Liabilities and Owner’s Equity</td>
                        <td class="text-right"></td>
                        <td class="text-right"><?= number_format($total_liabilities_and_equity, 2) ?></td>
                    </tr>
                    <?php endif; ?>


                </tbody>
            </table>                    
                                <?php
                        
                            ?>

                        </div>
                    </div>
                </div>  
            </div>
        </div>
        <div class="clearfix"></div>
        <footer>           
        </footer>
    </body>
</html>