<div class="content-wrapper">
   
    <div class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col">
                    <div class="card card-primary card-outline">
                        <div class="card-header">

                            <h5 class="m-0">Viewing Trial Balance <a onClick="exportTableToExcel('exportableTable','trialbalance')" class="btn btn-success">Export</a><a href="" onclick="printDiv('printableDiv');" class="btn btn-success btn-sm float-right">Print</a></h5>

                        </div>

                        <div class="card-body" id="printableDiv">
                            <h3 class="text-center"><?php echo $this->session->userdata("CompanyName"); ?></h3>
                            <h5 class="text-center">Trial Balance for FY <?php echo showFiscalYear(); ?></h5>
                            <div id="dataTable_wrapper">





                                <table class="table table-bordered table-hover" id="exportableTable">
                                    <thead>
                                        <tr>
                                            <th class="col-8">Account</th>
                                            <th class="col-2">Dr</th>
                                            <th class="col-2">Cr</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $showZeroBalance = 0;
                                        $r = 0;
                                        $dr = 0;
                                        $cr = 0;
                                        foreach ($AccountGroups as $AccountGroup) :
                                            $drAccountGroupTotal = 0;
                                            $crAccountGroupTotal = 0;
                                            showRow('table-group-heading', $AccountGroup->acgroup_name);

                                            foreach ($AccountGroup->AccountCategories as $AccountCategory) :
                                                $drAccountCategoryTotal = 0;
                                                $crAccountCategoryTotal = 0;
                                                showRow('table-category-heading', "<span class='lp-30'>" . $AccountCategory->accategory_name . "</span>");
                                                foreach ($AccountCategory->Accounts as $Account) :
                                                    $drAccountBalance = 0;
                                                    $crAccountBalance = 0;
                                                    $accountBalance = getBalance($Account->account_id);
                                                    $drAccountBalance = ($accountBalance > 0) ? $accountBalance : 0;
                                                    $crAccountBalance = ($accountBalance < 0) ? abs($accountBalance) : 0;
                                                    $drAccountCategoryTotal += $drAccountBalance;
                                                    $dr += $drAccountBalance;
                                                    $cr += $crAccountBalance;
                                                    $crAccountCategoryTotal += $crAccountBalance;
                                                    if ($accountBalance == 0) {
                                                        if ($showZeroBalance)
                                                            showRow($r, "<span class='lp-60'>" . $Account->account_name . "</span>", myCurrency($drAccountBalance), myCurrency($crAccountBalance));
                                                    } else {
                                                        showRow($r, "<span class='lp-60'>" . $Account->account_name . "</span>", myCurrency($drAccountBalance), myCurrency($crAccountBalance));
                                                    }
                                                endforeach;

                                                showRow('table-category-total', "<span class='text-right table-cell'>Total " . $AccountCategory->accategory_name . "</span>", myCurrency($drAccountCategoryTotal), myCurrency($crAccountCategoryTotal));
                                                $drAccountGroupTotal += $drAccountCategoryTotal;
                                                $crAccountGroupTotal += $crAccountCategoryTotal;
                                            endforeach;

                                            showRowHeader('table-group-total', "<span class='text-right table-cell'>Total " . $AccountGroup->acgroup_name . "</span>", myCurrency($drAccountGroupTotal), myCurrency($crAccountGroupTotal));

                                        endforeach;

                                        ?>

                                    </tbody>
                                    <tfoot>
                                        <?php showRowHeader("", "Trial Balance Total", myCurrency($dr), myCurrency($cr)); ?>
                                    </tfoot>

                                </table>
                            </div>


                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php
function showRow($r = "table-default", $c1 = '&nbsp;', $c2 = '', $c3 = '')
{
    if ($c2 == "" && $c3 == "") {
        //$html = "<tr class='$r'><td colspan='3'>$c1</td></tr>\n";
        $html = "<tr class='$r'><td>$c1</td><td class='col-2'>$c2</td><td class='col-2'>$c3</td></tr>\n";
    } else {
        $html = "<tr class='$r'><td>$c1</td><td class='col-2'>$c2</td><td class='col-2'>$c3</td></tr>\n";
    }
    echo $html;
}
function showRowHeader($r = "table-default", $c1 = '&nbsp;', $c2 = '', $c3 = '')
{
    $html = "<tr class='$r'><th>$c1</th><th class='col-2'>$c2</th><th class='col-2'>$c3</th></tr>\n";
    echo $html;
}
function footerFunctions()
{ ?>
    <script>
        function exportTableToExcel(tableID, filename = '') {
            var downloadLink;
            var dataType = 'application/vnd.ms-excel';
            var tableSelect = document.getElementById(tableID);
            var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');

            // Specify file name
            filename = filename ? filename + '.xls' : 'excel_data.xls';

            // Create download link element
            downloadLink = document.createElement("a");

            document.body.appendChild(downloadLink);

            if (navigator.msSaveOrOpenBlob) {
                var blob = new Blob(['\ufeff', tableHTML], {
                    type: dataType
                });
                navigator.msSaveOrOpenBlob(blob, filename);
            } else {
                // Create a link to the file
                downloadLink.href = 'data:' + dataType + ', ' + tableHTML;

                // Setting the file name
                downloadLink.download = filename;

                //triggering the function
                downloadLink.click();
            }
        }
    </script>
<?php
}
?>