<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <div style="padding:0px;overflow:auto;width:auto;height:583px;border:0px solid grey" >   
        <input type="hidden" name="hdn_begin_date" id="hdn_begin_date"/>
        <input type="hidden" name="hdn_end_date" id="hdn_end_date"/>
        
        <div style="padding:2px">
            <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-print'" onClick="link_print('report/index_to_report/print_profit_loss_report/<?php $BeginDate ?>/<?php $EndDate ?>');">Print</a>
        </div><br />
		
        <table width="40%" border="0" cellspacing="0" cellpadding="0" style="margin-left:340px; margin-top:10px;margin-bottom:10px;" >
            <tr><td colspan="8" class="s2"><?= $branch->CompanyName ?></td></tr>
            <tr><td colspan="4" class="s2"><?= $branch->BranchName; ?></td></tr>
            <tr><td colspan="4" class="s1">Balance Sheet Report</td></tr>
            <tr><td colspan="4" class="s2">Period : <?= $month ?> <?= $year ?></td></tr>
            <tr><td>&nbsp;</td></tr>
        </table>

        <table width="45%" border="0" cellspacing="1" cellpadding="1" data-options="fit:true" style="margin-left:310px;margin-top:10px;" style="size:auto;width:auto;margin:auto;">
            <tr>
                <td width="150" align='center' class="s4">Description</td>
                <td width="10" align='center' class="s4">Saldo Amount</td>
                <td width="10" align='center' class="s4" hidden>Trans Number</td>
                <td width="10" align='center' class="s4" hidden>Account Number</td>
            </tr>

            <?php
            $last_account_parent    = '';
            
            $group_saldo[0]         = 0;
            $group_saldo[1]         = 0;
            $group_saldo[2]         = 0;
            $group_saldo[3]         = 0;
            $group_saldo[4]         = 0;

            $group_parent[0]        = 0;
            $group_parent[1]        = 0;
            $group_parent[2]        = 0;
            $group_parent[3]        = 0;
            $group_parent[4]        = 0;
            
            $balance_saldo          = 0;
            $balance_group          = 0;
            
            foreach($detail_saldo as $row_saldo){
                $account_number     = $row_saldo->AccountNumber;
                $account_parent     = $row_saldo->AccountParent;
                $account_level      = $row_saldo->AccountLevel;
                $account_child      = $row_saldo->AccountChild;
                $saldo_balance      = $row_saldo->Saldo_Balance;
                
                if($last_account_parent == ''){$last_account_parent = $account_parent;}

                if($last_account_parent == $account_parent){
                    $balance_saldo  = $balance_saldo + $saldo_balance;
                    
//                    if($group_parent[$account_level] == $last_account_parent){$balance_group = $balance_group + $saldo_balance;};
//                    if($last_account_level == $account_level){$balance_group = $balance_group + $saldo_balance;};
                    
                }
                else{
                    if($account_parent == NULL){
                        $balance_group  = $group_saldo[0] + $group_saldo[1] + $group_saldo[2] + $group_saldo[3] + $group_saldo[4];
                        $balance_amount[$account_number] = $balance_group;
                        
                        $balance_saldo      = 0;
                        
                        $group_saldo[0]     = 0;
                        $group_saldo[1]     = 0;
                        $group_saldo[2]     = 0;
                        $group_saldo[3]     = 0;
                        $group_saldo[4]     = 0;

                        $group_parent[0]    = 0;
                        $group_parent[1]    = 0;
                        $group_parent[2]    = 0;
                        $group_parent[3]    = 0;
                        $group_parent[4]    = 0;
                        
                        $last_account_parent = $account_number;
                    }
                    else{
                        if($account_child == 0){
//                            if($group_parent[$account_level] != $account_parent){$group_parent[$account_level] = $account_parent;}

//                            $group_parent[$account_level]       = $account_parent;
                            $group_saldo[$account_level]        = $group_saldo[$account_level] + $balance_saldo;
                            $balance_amount[$account_number]    = $balance_saldo;
                        }
                        else{
                            $balance_saldo = $saldo_balance;
                            
                            if($last_account_parent != $account_parent){ 
                                $group_parent[$account_level]   = $account_parent;
                                $group_saldo[$account_level]    = $balance_saldo;
                            }
                        }

                        $last_account_parent = $account_parent;
                    }
                }
            }

            foreach($detail as $row){ 
                $account_level = $row->AccountLevel;

                if($row->AccountChild == 0){
                    $saldo_amount   = empty($balance_amount[$row->AccountNumber]) ? 0 : $balance_amount[$row->AccountNumber];
                }
                else{
                    $saldo_amount   = $row->Saldo_Balance;
                }
                
//                switch (intval($row->AccountLevel)){
//                    case 0:
//                        $account_level = $row->AccountLevel + 0;
//                        break;
//                    case 1:
//                        $account_level = $row->AccountLevel + 1;
//                        break;
//                    case 2:
//                        $account_level = $row->AccountLevel + 2;
//                        break;
//                    case 3:
//                        $account_level = $row->AccountLevel + 3;
//                        break;
//                    case 4:
//                        $account_level = $row->AccountLevel + 4;
//                        break;
//                }                
            ?>
                <tr>
                    <td class="s5" align='Left' style="<?= 'padding-left:' . $account_level . 'em;'?>" ><?= $row->Branch_Account ?></td>
                    <td class="s5" align='right' style='padding-left:15px'>
                        <a <?= ($saldo_amount != 0  && $row->AccountChild == 1)? "href='#'" : '' ?> onClick="" style="<?= ($saldo_amount < 0)? 'color:#FF6666' : '' ?>"> <?= formatRp_acc($saldo_amount,"2",",",".")?></a>
                    </td>
                    <td class="s5" align='Left' style='padding-left:15px' hidden><?= $row->AccountNumber ?></td>
                </tr>
            <?php } ?>
            
            <tr height='50'><td></td></tr>            
        </table>
    </div>
    
    <script type="text/javascript">
//        function view_journal(journal_number, account_number){
//            journal_number  = journal_number.replace(/[/]/g, '~');
//            date_begin      = $('#hdn_begin_date').val();
//            date_end        = $('#hdn_end_date').val();
//
//            parent.tab_add('Journal Index Report', '<?= "report/index_financial_statement/v_journal_balance_sheet_statement/" ?>' + journal_number + '/' + account_number + '/' + date_begin + '/' + date_end,'icon-add');
//        }
        
    </script>
</html>
