<?php

function display_balance_sheet_view_selecting_parameters() {  
    start_table(TABLESTYLE_NOBORDER);
	$date = today();
	if (! isset($_POST['to_date'])) {
            $_POST['to_date'] = end_month($date);
        }
	if (! isset($_POST['from_date'])) {
            $_POST['from_date'] = add_days(end_month($date), -15);
        }        
        date_cells(_("From:"), 'from_date');
	date_cells(_("To:"), 'to_date');
	
	submit_cells('Show',_("Display"),'','', 'default');
    end_table(1);
}


function calculated_achieved($d1, $d2)
{
	if ($d1 == 0 && $d2 == 0)
		return 0;
	elseif ($d2 == 0)
		return 999;
	$ret = ($d1 / $d2 * 100.0);
	if ($ret > 9999)
		$ret = 9999;
	return $ret;
}



function display_balance_sheet_view($from_date_, $to_date_, $fina_yer_begin_, $fina_yer_end_)
{
    global $Ajax;
    
    $pre_from_date_ = add_months($from_date_, -12);
    $pre_to_date_ = add_months($to_date_, -12);
    $pre_fina_yer_begin_ = add_months($fina_yer_begin_, -12);
    $pre_fina_yer_end_ = add_months($fina_yer_end_, -12);
    
    $begin_ = sql2date(INFINITE_START_DATE);
    
    $Ajax->activate('details');
    div_start('details');
    start_table(TABLESTYLE, "width=70%");
    
        $type_totals_array = $parents_children_array = $class_totals_array = array();

        $clz_id = 0;
        $classes = get_account_classes(false, 1);
            while ($class = db_fetch($classes)) {
                $clz_id = $class['cid'];
                $class_name = $class["class_name"];
                
                $class_totals_array[$class_name] = array('clz_open_bal_total' => 0, 'clz_acc_bal_total' => 0, 'clz_close_bal_total' => 0);
                
                $clz_row = get_account_class($clz_id);
                $convert = get_class_type_convert($clz_row["ctype"]); 
                
                table_section_title($class_name, "10 style=background-color:#0ec9ba");
                
                    $th = array(_("Account Name/ Group"), 
                                _("Open Balance<br>(Before $from_date_)"), 
                                _("Period<br>($from_date_ -<br> $to_date_)"), 
                                _("Closing Balance<br>($to_date_)"));
                    table_header($th);

                    $tmp_parents_array = array();
                    $previous_depth = $k = 0;
                    
                    $result = get_account_class_account_types($clz_id, false);
                        while ($row = db_fetch($result)) {
                            $parent = $row["parent"];
                            $depth = $row["depth"];
                            $position = $row["position"];
                            
//                            display_balance_sheet_group_totals($type_totals_array, $parents_children_array, $depth, $previous_depth, $tmp_parents_array);
                            
                            alt_table_row_color($k);
                                label_cell(spaces(($depth) * 10).text_bold($row["name"]), 'colspan=10');
                            end_row();

                            $ar = display_balance_sheet_acc_group_accounts($row["id"], $from_date_, $to_date_, $convert, $position, $depth);
                            
                            if ($parent > 0) {
                                $tmp_parents_array[$parent] = $parent;
                                $parents_children_array[$parent][$row["id"]] = $row["id"];
                            }
                            
                            $type_totals_array[$row["id"]] = array('period_open_bal_total' => $ar['period_open_bal_total'], 
                                                                    'period_acc_bal_total' => $ar['period_acc_bal_total'], 
                                                                    'period_close_bal_total' => $ar['period_close_bal_total'], 
                                                                    'depth' => $depth, 
                                                                    'position' => $position, 
                                                                    'group_name' => $row["name"]);
                            
                            $class_totals_array[$class_name]['clz_open_bal_total'] += $ar['period_open_bal_total'];
                            $class_totals_array[$class_name]['clz_acc_bal_total'] += $ar['period_acc_bal_total'];
                            $class_totals_array[$class_name]['clz_close_bal_total'] += $ar['period_close_bal_total']; 
                            
                            $previous_depth = $depth;
                        }
                        
//                        if (count($tmp_parents_array) > 0) {
//                            display_profit_loss_group_totals($type_totals_array, $parents_children_array, 0, $previous_depth, $tmp_parents_array);
//                        }
                        
                        display_balance_sheet_class_total($class_name, $class_totals_array);
            }
            

            $cal_return_open_bal = $class_totals_array["Assets"]["clz_open_bal_total"] - $class_totals_array["Liabilities"]["clz_open_bal_total"];
            $cal_return_acc_bal = $class_totals_array["Assets"]["clz_acc_bal_total"] - $class_totals_array["Liabilities"]["clz_acc_bal_total"];
            $cal_return_close_bal = $class_totals_array["Assets"]["clz_close_bal_total"] - $class_totals_array["Liabilities"]["clz_close_bal_total"];
            
            start_row();
                label_cell(("Calculated return"), 'align=right colspan=1 style=color:blue');
                amount_cell($cal_return_open_bal, false, 'style=color:blue');
                amount_cell($cal_return_acc_bal, false, 'style=color:blue'); 
                amount_cell($cal_return_close_bal, false, 'style=color:blue'); 
            end_row();
            
            $liability_equity_open_bal = $class_totals_array["Liabilities"]["clz_open_bal_total"] + $class_totals_array["Equity"]["clz_open_bal_total"] + $cal_return_open_bal;
            $liability_equity_acc_bal = $class_totals_array["Liabilities"]["clz_acc_bal_total"] + $class_totals_array["Equity"]["clz_acc_bal_total"] + $cal_return_acc_bal;
            $liability_equity_close_bal = $class_totals_array["Liabilities"]["clz_close_bal_total"] + $class_totals_array["Equity"]["clz_close_bal_total"] + $cal_return_close_bal;
            
            start_row();
                label_cell(("Total Liabilities and Equities"), 'align=right colspan=1 style=color:blue');
                amount_cell($liability_equity_open_bal, false, 'style=color:blue');
                amount_cell($liability_equity_acc_bal, false, 'style=color:blue'); 
                amount_cell($liability_equity_close_bal, false, 'style=color:blue'); 
            end_row();
            

    end_table(2);
    div_end();
}


function display_balance_sheet_acc_group_accounts($type, $from_date_, $to_date_, $convert, $position, $depth) {
    $path_to_root = "../..";
		
    $dimension = $dimension2 = $period_open_bal_total = $period_acc_bal_total = $period_close_bal_total = 0;
    
    $depth += 1;
    $begin_ = sql2date(INFINITE_START_DATE);
    $from_date_before_date_ = add_days($from_date_, -1);
	
        $display_acc_total = false;
	//Get Accounts directly under this group/type
	$result = get_gl_accounts_with_position_depth(null, null, $type);	

            while ($account = db_fetch($result)) {
                $acc_code = $account["account_code"];
                $open_bal = get_gl_trans_from_to($begin_, $from_date_before_date_, $acc_code);
                $period_acc_bal = get_gl_trans_from_to($from_date_, $to_date_, $acc_code);
                
		if ($open_bal  == 0 && $period_acc_bal == 0) {
                    continue;
                }
		
                    $url = "<a href='$path_to_root/gl/inquiry/gl_account_inquiry.php?from_date=" 
                            . $from_date_ . "&to_date=" . $to_date_ . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2 
                            . "&account=" . $account['account_code'] . "'>" . $account['account_code'] 
                            ." - ". $account['account_name'] ."</a>";
                    
                    start_row();
                        label_cell(spaces($depth * 10).$url);
                        $param = "account=$acc_code&from_date=$begin_&to_date=$from_date_before_date_";
                        $view_str = get_gl_account_trans_str($param, number_format($open_bal * $convert, 2));
                        label_cell($view_str, 'align=right');
                        
                        $param = "account=$acc_code&from_date=$from_date_&to_date=$to_date_";
                        $view_str = get_gl_account_trans_str($param, number_format($period_acc_bal * $convert, 2));
                        label_cell($view_str, 'align=right');
                        
                        $close_bal = $open_bal + $period_acc_bal;
                        
                        amount_cell($close_bal); 
                        
                    end_row();
			
		$period_open_bal_total += $open_bal * $convert;
		$period_acc_bal_total += $period_acc_bal * $convert;
		$period_close_bal_total += ($open_bal + $period_acc_bal )* $convert; 
                
                $display_acc_total = true;
            }
            
            if ($display_acc_total) {
                start_row();
                    label_cell(text_bold('Group Total'), 'align=right colspan=1');
                    amount_cell($period_open_bal_total, true);
                    amount_cell($period_acc_bal_total, true); 
                    amount_cell($period_close_bal_total, true); 
                end_row();
            }
            
            return array('period_open_bal_total' => $period_open_bal_total, 'period_acc_bal_total' => $period_acc_bal_total, 
                         'period_close_bal_total' => $period_close_bal_total, 
                         'depth' => $depth, 'position' => $position);
}


function display_balance_sheet_class_total($class_name, &$class_totals_array) {
    
    start_row();
        label_cell(("Total $class_name"), 'align=right colspan=1 style=color:blue');
        amount_cell($class_totals_array[$class_name]['clz_open_bal_total'], false, 'style=color:blue');
        amount_cell($class_totals_array[$class_name]['clz_acc_bal_total'], false, 'style=color:blue'); 
        amount_cell($class_totals_array[$class_name]['clz_close_bal_total'], false, 'style=color:blue'); 
    end_row();
   
}
