<?php

function dashboard_gl_budgets1111($from_month_display_order, $to_month_display_order) {
    
//    display_heading2('(Based on Goods Received Notes and Tax Free Item Costs)');    
    br();
    
    
    $month_ranges_array = dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order);
    $table_width = dashboard_calculate_table_width($month_ranges_array);
    
    start_table(TABLESTYLE, "width$table_width%");
        
        $th = array("Expense Type");
        foreach ($month_ranges_array as $month) {
            $th[] = $month['month_name'];
        }
        $th[] = "Total";

        table_header($th);

        $from_month = get_month_by_display_order($from_month_display_order);
        $to_month = get_month_by_display_order($to_month_display_order);

        $from_date = $from_month['month_begin_date'];
        $to_date = $to_month['month_end_date'];

        $expense_types_res = dashboard_get_gl_budget_account_classes($from_date, $to_date);

        $k = 0;
            
            
            while ($exp = db_fetch($expense_types_res)) {
                alt_table_row_color($k);

                label_cell($exp['expense_type_name']);
                
                foreach ($month_ranges_array as $month) {
                    
                    $from_date = $month['month_begin'];
                    $to_date = $month['month_end'];
                    $expense_type_id = $exp['expense_type_id'];
                    $parameters = "expt=".$expense_type_id."&from_date=$from_date&to_date=$to_date";
                    $total = dashboard_get_petty_cash_period_expense_type_total($expense_type_id, $from_date, $to_date);

                    if($total > 0){
                        $str_count = get_dashboard_view_str(DASH_SC_PETTY_CASH_EXPENSES, $parameters, number_format($total,2));
                        $month['month_value'] += $total;      
                    }else{
//                        $str_count = number_format($total,2);
                        $str_count = "";
                    }
                    label_cell($str_count, 'align=right');
                }   
                
                label_cell(number_format($exp['approved_total'],2), 'align=right');
                end_row();
            }
        
        dashboard_display_simple_month_totals($month_ranges_array);            
            
    end_table(2);
}

function dashboard_display_gl_budgets($from_month_display_order, $to_month_display_order)
{
    br();    
    
    $month_ranges_array = dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order);
    $table_width = dashboard_calculate_table_width($month_ranges_array);
    
    start_table(TABLESTYLE, "width$table_width%");
        
    $th = array("Account", "Account Name");
    foreach ($month_ranges_array as $month) {
        $th[] = $month['month_name'];
    }
    $th[] = "Total";

    table_header($th);

    $from_month = get_month_by_display_order($from_month_display_order);
    $to_month = get_month_by_display_order($to_month_display_order);

    $range_from_date = $from_month['month_begin_date'];
    $range_to_date = $to_month['month_end_date'];

    $class_result = dashboard_get_gl_budget_account_classes($range_from_date, $range_to_date);

    $k = 0;

    while ($class = db_fetch($class_result)){  
        $class_year_total = 0;
        $class_total = array();
        
        $convert = get_class_type_convert($class['ctype']);
        
//        foreach ($month_ranges_array as $month) {
//            $month['month_value'] = 0;
//        }    
        
//        $class_total = $month_ranges_array;
                
        $class_month_values = array("class_total_budgets"=>0, "class_total_trans"=>0);
        foreach ($month_ranges_array as &$class_month) {
            $class_total[$class_month['month_begin']] = $class_month_values; 
        }
        
//        start_row();
        $class_name = $class['class_name'];

        table_section_title("Account Class: ".$class_name, "15 style=background-color:#0ec9ba");
        
        dashboard_display_gl_budgets_account_class($month_ranges_array, $range_from_date, $range_to_date, $class_total, $class['cid'], $convert);
        
        start_row();
        label_cell(text_bold("Account Class: ".$class_name." (Budgets)"), 'colspan=2 align=right');
//        foreach ($class_total as $key => $class_month_total) { 
//            qty_cell($class_month_total, true, 0);
//            $class_year_total += $class_month_total;
//            $grand_total[$key] += $class_month_total;
//        }       
//        qty_cell($class_year_total, true, 0);
//        end_row();
        
        
        $account_class_range_total_budgets = 0;
        $account_class_range_total_trans = 0;
        
        foreach ($class_total as $key => $account_type_month_total) { 
            $account_class_month_total_budgets = $account_type_month_total['class_total_budgets'];
            qty_cell($account_class_month_total_budgets, true, 0);
//            $account_type_year_total += $account_class_month_total_budgets;
            $account_class_range_total_budgets += $account_class_month_total_budgets; 
        }      
        qty_cell($account_class_range_total_budgets, true, 0);
        end_row();
        
        start_row();
        label_cell(text_bold("Transactions"), 'colspan=2 align=right');
        foreach ($class_total as $key => $account_type_month_total) { 
            $account_class_month_total_trans = $account_type_month_total['class_total_trans'];
            qty_cell($account_class_month_total_trans, true, 0);
            $account_class_range_total_trans += $account_class_month_total_trans;
//            $account_type_year_total += $account_class_month_total_trans; 
        }      
        qty_cell($account_class_range_total_trans, true, 0);
        end_row();
        
        start_row();
        label_cell(text_bold("Achievement(%)"), 'colspan=2 align=right');
        foreach ($class_total as $key => $account_type_month_total) { 
            $account_class_month_total_budgets = $account_type_month_total['class_total_budgets'];
            $account_class_month_total_trans = $account_type_month_total['class_total_trans'];
            
            $class_month_achievement = 0; 
            if($account_class_month_total_budgets != 0){
                $class_month_achievement = $account_class_month_total_trans/$account_class_month_total_budgets * 100;
            }
//            qty_cell($class_month_achievement, true, 2);
            dashboard_display_coloured_achievement_cell(abs($class_month_achievement), true);
//            $account_type_year_total += $account_class_month_total_budgets; 
        }      
        
        $class_range_achievement = 0; 
        if($account_class_range_total_budgets != 0){
            $class_range_achievement = $account_class_range_total_trans/$account_class_range_total_budgets * 100;
        }
//        qty_cell($class_range_achievement, true, 2);
        dashboard_display_coloured_achievement_cell(abs($class_range_achievement), true);
        end_row();
    }
    
    
//    start_row();
//    label_cell(text_bold("Grand Total"), 'colspan=2 align=right');
//    foreach ($grand_total as $month_full_total) { 
//        qty_cell($month_full_total, true, 0); 
//    }        
//    end_row();
    
    end_table();

}


function dashboard_display_gl_budgets_account_class($months, $range_from_date, $range_to_date, &$class_total, $class_id, $convert){

    $account_type_res = dashboard_get_gl_budget_account_types($range_from_date, $range_to_date, $class_id);
    while ($account_type = db_fetch($account_type_res)){ 
        $account_type_year_total = 0;
        $account_type_total = array();
        $month_values = array("type_total_budgets"=>0, "type_total_trans"=>0);
        foreach ($months as $month) {
            $account_type_total[$month['month_begin']] = $month_values; 
        }
        start_row();
        $account_type_name = $account_type['id']." - ".$account_type['name'];
        label_cell(text_bold($account_type_name), 'colspan=15');
        end_row();
        
        dashboard_display_gl_budgets_account_types($months, $range_from_date, $range_to_date, $account_type_total, $account_type['id'], $convert);
        
        start_row();
        label_cell(text_bold($account_type_name." (Budgets)"), 'colspan=2 align=right');
        
        $account_type_range_total_budgets = 0;
        $account_type_range_total_trans = 0;
        
        foreach ($account_type_total as $key => $account_type_month_total) { 
            $account_type_month_total_budgets = $account_type_month_total['type_total_budgets'];
            qty_cell($account_type_month_total_budgets, true, 0);
            $account_type_year_total += $account_type_month_total_budgets;
            $account_type_range_total_budgets += $account_type_month_total_budgets;
            
//            $class_month_values_array = $class_total[$key];
            $class_total[$key]['class_total_budgets'] += $account_type_month_total_budgets;
        }      
        qty_cell($account_type_range_total_budgets, true, 0);
        end_row();
        
        start_row();
        label_cell(text_bold("Transactions"), 'colspan=2 align=right');
        foreach ($account_type_total as $key => $account_type_month_total) { 
            $account_type_month_total_trans = $account_type_month_total['type_total_trans'];
            qty_cell($account_type_month_total_trans, true, 0);
            $account_type_range_total_trans += $account_type_month_total_trans;
            $account_type_year_total += $account_type_month_total_trans;
            
            $class_total[$key]['class_total_trans'] += $account_type_month_total_trans;
        }      
        qty_cell($account_type_range_total_trans, true, 0);
        end_row();
        
        start_row();
        label_cell(text_bold("Achievement(%)"), 'colspan=2 align=right');
        foreach ($account_type_total as $key => $account_type_month_total) { 
            $account_type_month_total_budgets = $account_type_month_total['type_total_budgets'];
            $account_type_month_total_trans = $account_type_month_total['type_total_trans'];
            
            $type_month_achievement = 0; 
            if($account_type_month_total_budgets != 0){
                $type_month_achievement = $account_type_month_total_trans/$account_type_month_total_budgets * 100;
            }
//            qty_cell($type_month_achievement, true, 2);
            dashboard_display_coloured_achievement_cell(abs($type_month_achievement), true);
            $account_type_year_total += $account_type_month_total_budgets; 
        }      
        
        $type_range_achievement = 0; 
        if($account_type_range_total_budgets != 0){
            $type_range_achievement = $account_type_range_total_trans/$account_type_range_total_budgets * 100;
        }
//        qty_cell($type_range_achievement, true, 2);
        dashboard_display_coloured_achievement_cell(abs($type_range_achievement), true);
        end_row();
    }
    
}

function dashboard_display_gl_budgets_account_types($months, $range_from_date, $range_to_date, &$class_total, $account_type_id, $convert){
    
    $months_acc = $months;
    foreach ($months_acc as &$acc_month) {
        $acc_month['budget'] = 0;
        $acc_month['achieved'] = 0;
    }
    
    $result = dashboard_get_gl_budget_accounts($range_from_date, $range_to_date, $account_type_id);	
    $k = 0;
    while ($account = db_fetch($result)){
        alt_table_row_color($k);
        label_cell($account['account_code']);
        label_cell($account['account_name']);        
//        label_cell("Budget");
        dashboard_display_gl_budgets_account_budget($months_acc, $class_total, $account['account_code']);
        end_row();
        
        alt_table_row_color($k);
        label_cell("");
//        label_cell("");
        label_cell("Transactions", "align=right");
        dashboard_display_gl_budgets_account_achieved_value($months_acc, $class_total, $account['account_code'], $convert);
        end_row();
        
        alt_table_row_color($k);
        label_cell("");
//        label_cell("");
        label_cell(text_bold("Achievement(%)"), "align=right");
        dashboard_display_gl_budgets_account_achievement($months_acc, $class_total, $account['account_code']);
        end_row();
    }
    
}

function dashboard_display_gl_budgets_account_budget(&$months, &$class_total, $account_code){
    
    $total = 0;
    
    foreach ($months as &$month) {
        $month_begin = $month['month_begin'];
        $month_end = $month['month_end'];
        $budget_value = dashboard_get_gl_budget_account_month_value($month_begin, $month_end, $account_code);

        qty_cell($budget_value, false, 0);
        $month['budget'] = $budget_value;
        
        $total += $budget_value;        
//        $class_total['type_total_budgets'] += $budget_value;
        $class_total[$month_begin]["type_total_budgets"] += $budget_value;
    }
    
    label_cell(number_format($total), 'align=right');
    
}


function dashboard_display_gl_budgets_account_achieved_value(&$months, &$class_total, $account_code, $convert){
    
    $total = 0;
    
    foreach ($months as &$month) {
        $month_begin = $month['month_begin'];
        $month_end = $month['month_end'];
        
        $month_begin_ = sql2date($month_begin);
        $month_end_ = sql2date($month_end);
        
//        $budget_value = dashboard_get_gl_budget_account_month_value($month_begin, $month_end, $account_code);
        $period_acc_trans = get_gl_trans_from_to($month_begin_, $month_end_, $account_code);

//        qty_cell($period_acc_trans, false, 0);
        if($period_acc_trans != 0){
            $param = "account=$account_code&from_date=$month_begin_&to_date=$month_end_";
            $view_str = get_gl_account_trans_str($param, number_format($period_acc_trans * $convert, 0));
            label_cell($view_str, 'align=right');
        }else{
            label_cell("");
        }
                        
        $month['achieved'] = $period_acc_trans;
        
        $total += $period_acc_trans;        
        $class_total[$month_begin]["type_total_trans"] += $period_acc_trans;
    }
    
    label_cell(number_format($total), 'align=right');
    
}



function dashboard_display_gl_budgets_account_achievement(&$months, &$class_total, $account_code){
    
    $budget_total = 0;
    $achieved_total = 0;
    
    foreach ($months as $month) {
//        $month_begin = $month['month_begin'];
//        $month_end = $month['month_end'];
//        $budget_value = dashboard_get_gl_budget_account_month_value($month_begin, $month_end, $account_code);
        
        $budget = $month['budget'];
        $achieved = $month['achieved'];

        $percentage = 0;
        if($budget != 0){
            $percentage = abs($achieved/$budget * 100);
        }
        
//        $rgb = dashboard_get_achievement_background_colour($percentage);
//        if($rgb){
//            $colour = "style=".$rgb;
//            $percentage_text = text_bold(number_format($percentage,2));
//        }else{
//            $colour = "";
//            $percentage_text = number_format($percentage,2);
//        }
//        
//        if($percentage != 0){
////            amount_cell($percentage, true);
//            label_cell($percentage_text, 'align=right '.$colour); 
//        }else{
//            label_cell("");
//        }
        
        dashboard_display_coloured_achievement_cell($percentage, true);
        
        
        $budget_total += $budget;        
        $achieved_total += $achieved;     
    }
    
    $range_percentage = 0;
    if($budget_total != 0){
        $range_percentage = $achieved_total/$budget_total * 100;
    }
    
//    label_cell(number_format($range_percentage, 2), 'align=right');
    dashboard_display_coloured_achievement_cell(abs($range_percentage), true);
    
}

