<?php


function display_gl_account_budget_filtering_controls()
{
    start_table(TABLESTYLE_NOBORDER);
    start_row();
    fiscalyears_list_cells(_("Fiscal Year:"), 'fyear', null);
//    gl_all_accounts_list_row(_("Account Code:"), 'account', null);
    
    $classresult = get_account_classes(false, 0);
    while ($class = db_fetch($classresult)){
        $account_classes_array[$class['cid']] = $class['class_name'];
    }

    custom_values_list_cells("Account Class:", 'aclass_id', $account_classes_array, null, 'All P&L Account Classes', true);
    
    submit_cells('display_gl_budgets',_("Display"),'','', 'default');
    
    end_row();
    
    end_table(1);
    
//    submit_row('submit', _("Get"), true, '', '', true);
    
//    submit_center_first('display_gl_budgets', _("Display"), '', null);
}

function build_gl_account_budgets_months_array($fiscal_year_id)
{
    $fiscal_year = get_fiscalyear($fiscal_year_id);
    $begin_date = $fiscal_year['begin'];
    $end_date = $fiscal_year['end'];
    
    $begin_date_ = sql2date($begin_date);
    $end_date_ = sql2date($end_date);
    
    $months = array();
    
    for ($i = 0, $date_ = $begin_date_; date1_greater_date2($end_date_, $date_); $i++){
        $months[] = date2sql($date_); 
        $date_ = add_months($date_, 1); 
    }
    
    return $months;
}


function display_gl_account_budget_edit_controls()
{
    $fiscal_year_id = $_POST['fyear'];
    
    $months = build_gl_account_budgets_months_array($fiscal_year_id);
    
    start_table(TABLESTYLE, "width=98%");
    $th = array(_('Code'),_('Name'));   
    
    foreach ($months as $m) {
        $month_name = date('F y', strtotime($m));
        $th[] = $month_name;
    }
    $th[] = "Total";    

    table_header($th);
    
    if($_POST['aclass_id'] > 0){
        $filtering_aclass_id = $_POST['aclass_id'];
    }else{
        $filtering_aclass_id = -1;
    }
    
    $grand_total = array();
    foreach ($months as $month_date) {
        $grand_total[$month_date] = 0;
    }
        
    $classresult = get_account_classes(false, 0, $filtering_aclass_id);
    while ($class = db_fetch($classresult)){  
        $class_year_total = 0;
        $class_total = array();
        foreach ($months as $month_date) {
            $class_total[$month_date] = 0;
        }
//        start_row();
        $class_name = $class['class_name'];
//        label_cell(text_bold($class_name), 'colspan=15  align=center');
//        end_row();
        table_section_title("Account Class: ".$class_name, "15 style=background-color:#0ec9ba");
        
        display_gl_account_class_edit_controls($months, $class_total, $class['cid']);
        
        start_row();
        label_cell(text_bold("Account Class: ".$class_name." Total"), '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();
    }
    
//    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 display_gl_account_class_edit_controls($months, &$class_total, $class_id){

    $account_type_res = get_account_class_account_types($class_id);
    while ($account_type = db_fetch($account_type_res)){ 
        $account_type_year_total = 0;
        $account_type_total = array();
        foreach ($months as $month_date) {
            $account_type_total[$month_date] = 0;
        }
        start_row();
        $account_type_name = $account_type['id']." - ".$account_type['name'];
        label_cell(text_bold($account_type_name), 'colspan=15');
        end_row();
        
        display_gl_account_types_edit_controls($months, $account_type_total, $account_type['id']);
        
        start_row();
        label_cell(text_bold($account_type_name." Total"), 'colspan=2 align=right');
        foreach ($account_type_total as $key => $account_type_month_total) { 
            qty_cell($account_type_month_total, true, 0);
            $account_type_year_total += $account_type_month_total;
            $class_total[$key] += $account_type_month_total;
        }      
        qty_cell($account_type_year_total, true, 0);
        end_row();
    }
    
}

function display_gl_account_types_edit_controls($months, &$class_total, $account_type_id){
    
    $result = get_gl_accounts(null, null, $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']);
        display_gl_account_months_edit_controls($months, $class_total, $account['account_code']);
        end_row();
    }
    
}

function display_gl_account_months_edit_controls($months, &$class_total, $account_code){
    
    $total = 0;
    
    foreach ($months as $month_date) {
        $month_date_ = sql2date($month_date);
        $acc_balance = get_budget_trans_from_to($month_date_, $month_date_, $account_code);
        
        if($acc_balance > 0){
            $month_budget = number_format($acc_balance,0);            
        }else{
            $month_budget = '';
        }
        
        $control_name = $account_code."_".$month_date;
        
//        $_POST[$control_name] = $acc_balance;
        set_post_not_set_numeric($control_name, $month_budget, $_POST['aclass_id']);
        
        amount_cells(null, $control_name, null, null, null, 0);
        $value = input_num($control_name);
        
        $total += $value;
        
        $class_total[$month_date] += $value;
    }
    
    label_cell(number_format($total), 'align=right');
    
}



function update_gl_account_budgets()
{
    $fiscal_year_id = $_POST['fyear'];
    
    $months = build_gl_account_budgets_months_array($fiscal_year_id);
    
    if($_POST['aclass_id'] > 0){
        $filtering_aclass_id = $_POST['aclass_id'];
    }else{
        $filtering_aclass_id = -1;
    }
    
    $classresult = get_account_classes(false, 0, $filtering_aclass_id);
    while ($class = db_fetch($classresult)){  
        update_gl_account_class_budgets($months, $class['cid']);
    }
}

function update_gl_account_class_budgets($months, $class_id){
    
    $account_type_res = get_account_class_account_types($class_id);
    while ($account_type = db_fetch($account_type_res)){ 
        update_gl_account_types_budgets($months, $account_type['id']);
    }
    
}

function update_gl_account_types_budgets($months, $account_type_id){
    
    $result = get_gl_accounts(null, null, $account_type_id); 
    while ($account = db_fetch($result)){
        update_gl_account_month_budgets($months, $account['account_code']);
    }
    
}


function update_gl_account_month_budgets($months, $account_code){
    
    foreach ($months as $month_date) {
        $month_date_ = sql2date($month_date);

        delete_gl_budget_trans($month_date_, $account_code, 0, 0);
        
        $control_name = $account_code."_".$month_date;
        $amount = input_num($control_name);
        if($amount > 0){
            add_gl_budget($month_date_, $account_code, 0, 0, $amount);
        }
    }
}
    