
<style type="text/css">
::-webkit-scrollbar {
    display: none;
}
#dynamic-content-container{background: #fff;}
    .navbar-default.navbar-static-side{display: none !important;}
    #page-wrapper{margin: 0px !important; padding: 0 !important;}
    .row.border-bottom{display: none !important;}
    .footer.fixed{display: none;}
    .ibox-content{padding: 0 !important;}
    body {
    -webkit-print-color-adjust:exact;
    background: #fff;
    }

</style>

<table style="width: 100%; background: #fff;">
    <tr>
        <td><img style="width: 100%; height: 200px;" src="<?= get_branch_image('report'); ?>"></td>
    </tr>
</table>

<?php
$currency_symbol = array_key_exists($search_currency, $currencies) ? $currencies[$search_currency]['symbol'] : NULL;
if(!empty($search_from)){$from = display_true_date($search_from);}else{$from = '';}
if(!empty($search_to)){$to = display_true_date($search_to);}else{$to = '';}

?>

<table style="width: 100%; margin: 15px 0px; background: #fff;">
    <tr style="margin: 10px 0px; text-align: center;">
        <td style="width: 33.33%;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important; font-size: 15px;"><?= strtoupper(print_lang($app_lang,'type')); ?>:</strong></span> <strong style="font-size: 14px;"><?= print_lang($app_lang,'balancesheet'); ?></strong></td>
        <td style="width: 33.33%;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important; font-size: 15px;"><?= strtoupper(print_lang($app_lang,'currency')); ?>:</strong></span> <strong style="font-size: 14px;"><?php if(!empty($search_currency)){echo ucfirst($currency_symbol);} ?></strong></td>
        <td style="width: 33.33%;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important; font-size: 15px;"><?= strtoupper(print_lang($app_lang,'date')); ?>:</strong></span> <strong style="font-size: 14px;"><?php if(!empty($from)){echo $from."  -  ".$to;}else{echo print_lang($app_lang,'overall');} ?></strong></td>
    </tr>
</table>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    
                    <div class="table-responsive">
                        <table class="table table-striped pointer-none">
                            <thead>
                                <tr>
                                    <th style="width: 28%;"><?= print_lang($app_lang,'balance_sheet_1st_head'); ?></th>
                                    <th style="width: 18%; text-align: center;"><?= print_lang($app_lang,'balance_sheet_old_head'); ?></th>
                                    <th style="width: 18%; text-align: center;"><?= print_lang($app_lang,'balance_sheet_2nd_head'); ?></th>
                                    <th style="width: 18%; text-align: center;"><?= print_lang($app_lang,'balance_sheet_3rd_head'); ?></th>
                                    <th style="width: 18%; text-align: center;"><?= print_lang($app_lang,'balance_sheet_4th_head'); ?></th>
                                </tr>
                            </thead>
                            <tbody>

                                <?php 
                                $child_accounts = array(); 
                                $parent_count = 1; 
                                $branch_id = active_branch_id();
                                $parent_credit = 0;
                                $parent_debit = 0;
                                $parent_balance = 0;
                                $parent_balance_previous = 0;
                                foreach($root_accounts as $parent_row){
                                    if($parent_row['uid'] == 'cashbook')
                                    {
                                        $child_accounts = fetch_all_cashbooks_branch();
                                    }
                                    else if($parent_row['uid'] == 'client')
                                    {
                                        $child_accounts = fetch_all_clients_branchs();
                                    }
                                    else
                                    {
                                        $child_accounts = std_obj_to_array(DB::table('accounts')->where('branch',$branch_id)->where('type',$parent_row['uid'])->get());
                                    }
                                ?>

                                    <?php
                                    if($parent_row['branch'] == 'general')
                                    {
                                        $parent_name = print_lang($app_lang,'builtin_account_'.$parent_row['uid']);
                                    }
                                    else
                                    {
                                        $parent_name = $parent_row['name'];
                                    }
                                    // $parent_balance = _calculate_system_account_type_balance_details(['currency' => $search_currency, 'type' => $parent_row['uid'], 'from' => $search_from, 'to' => $search_to]);
                                    $parent_credit = isset($balance_summary_data[$parent_row['uid']]['credit']) ? array_sum($balance_summary_data[$parent_row['uid']]['credit']) : 0;
                                    $parent_debit = isset($balance_summary_data[$parent_row['uid']]['debit']) ? array_sum($balance_summary_data[$parent_row['uid']]['debit']) : 0;
                                    $parent_balance = isset($balance_summary_data[$parent_row['uid']]['balance']) ? array_sum($balance_summary_data[$parent_row['uid']]['balance']) : 0;
                                    $parent_balance_previous = isset($balance_summary_data_previous[$parent_row['uid']]['balance']) ? array_sum($balance_summary_data_previous[$parent_row['uid']]['balance']) : 0;
                                    $parent_balance_previous_color = ($parent_balance_previous >= 0) ? '#2B9C1B' : '#ec4758';
                                    ?>

                                    <tr>
                                        <td colspan="5" style="padding-left: 0; padding-right: 0;">
                                            <div class="panel-group" id="Parent<?= $parent_count; ?>">
                                                <div class="panel" style="border-color:  #1c3b30 !important;">
                                                    <div class="panel-heading" style="min-height: 43px; background-color: #1c3b30 !important; border-color: #1c3b30 !important; color: #fff !important;">
                                                        <h5 class="panel-title" style="color: #fff !important; width: 28%; <?php if($app_direction == 'rtl'){echo 'float: right;';}else{echo 'float: left;';}?> display: block;">
                                                            <?= $parent_name; ?>
                                                        </h5>
                                                        <h5 class="panel-title" style="width: 18%; color: <?= $parent_balance_previous_color; ?>; <?php if($app_direction == 'rtl'){echo 'float: right;';}else{echo 'float: left;';}?> display: block; text-align: center;"><?= amount_number_format($parent_balance_previous); ?></h5>
                                                        <h5 class="panel-title" style="color: #fff !important; width: 18%; <?php if($app_direction == 'rtl'){echo 'float: right;';}else{echo 'float: left;';}?> display: block; text-align: center;"><?= amount_number_format($parent_debit); ?></h5>
                                                        <h5 class="panel-title" style="color: #fff !important; width: 18%; <?php if($app_direction == 'rtl'){echo 'float: right;';}else{echo 'float: left;';}?> display: block; text-align: center;"><?= amount_number_format($parent_debit); ?></h5>

                                                        <?php
                                                        // set colors
                                                        $parent_balance_color = ($parent_balance >= 0) ? '#2B9C1B' : '#ec4758';
                                                        ?>

                                                        <h5 class="panel-title" style="width: 18%; color: <?= $parent_balance_color; ?> !important; <?php if($app_direction == 'rtl'){echo 'float: right;';}else{echo 'float: left;';}?> display: block; text-align: center;"><?= amount_number_format($parent_balance); ?></h5>
                                                    </div>
                                                    <div id="account-parent-<?= $parent_count; ?>" class="panel-collapse collapse in">
                                                        <div class="panel-body">
                                                            <table class="table table-striped" style="width: 100%;">
                                                                <?php 
                                                                $child_credit = 0;
                                                                $child_debit = 0;
                                                                $child_balance = 0;
                                                                $child_count = 1; 
                                                                foreach($child_accounts as $child_row){ ?>

                                                                    <?php

                                                                    // if($child_row['shared'] == 'yes')
                                                                    // {
                                                                    //     $balance_child = fetch_shared_account_balance_details($child_row['uid'],$search_currency,$child_row['type'],$search_from,$search_to);
                                                                    // }
                                                                    // else
                                                                    // {
                                                                    //     $balance_child = fetch_account_balance_details($child_row['uid'],$search_currency,$child_row['type'],$search_from,$search_to);
                                                                    // }

                                                                    $child_debit = isset($balance_summary_data[$parent_row['uid']][$child_row['uid']]['debit']) ? $balance_summary_data[$parent_row['uid']][$child_row['uid']]['debit'] : 0;
                                                                    $child_credit = isset($balance_summary_data[$parent_row['uid']][$child_row['uid']]['credit']) ? $balance_summary_data[$parent_row['uid']][$child_row['uid']]['credit'] : 0;
                                                                    $child_balance = isset($balance_summary_data[$parent_row['uid']][$child_row['uid']]['balance']) ? $balance_summary_data[$parent_row['uid']][$child_row['uid']]['balance'] : 0;
                                                                    $child_balance_previous = isset($balance_summary_data_previous[$parent_row['uid']][$child_row['uid']]['balance']) ? $balance_summary_data_previous[$parent_row['uid']][$child_row['uid']]['balance'] : 0;
                                                                    
                                                                    // value limit
                                                                    if($child_balance >= 0)
                                                                    {
                                                                        // positive value limit
                                                                        if($child_balance < $search_limit_positive){ continue; }
                                                                    }
                                                                    else
                                                                    {
                                                                        // negative value limit
                                                                        // -9000 < -8000
                                                                        if($child_balance > $search_limit_negative){ continue; }
                                                                    }
                                                                    
                                                                    // set colors
                                                                    $child_balance_color = ($child_balance >= 0) ? '#2B9C1B' : '#ec4758';
                                                                    $child_balance_previous_color = ($child_balance_previous >= 0) ? '#2B9C1B' : '#ec4758';
                                                                    ?>

                                                                    <tr style="line-height: 35px;">
                                                                        <td style="width: 28%; text-indent: 20px;"><?= $child_row['name']; ?></td>
                                                                        <td style="width: 18%; text-align: center; color: <?= $child_balance_previous_color; ?>;"><?= amount_number_format($child_balance_previous); ?></td>
                                                                        <td style="width: 18%; text-align: center;"><?= amount_number_format($child_debit); ?></td>
                                                                        <td style="width: 18%; text-align: center;"><?= amount_number_format($child_credit); ?></td>
                                                                        <td style="width: 18%; text-align: center; color: <?= $child_balance_color; ?> !important;"><?= amount_number_format($child_balance); ?></td>
                                                                    </tr>
                                                                    <?php $child_count++; } ?>
                                                            </table>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                <?php $parent_count++; } ?>

                            </tbody>
                        </table>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>