<?php

$page_security = 'SA_REBUILD_PROJECT_BUDGETS';
if (!@$_GET['popup'])
	$path_to_root = "../..";
else	
	$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/projects/includes/ui/rebuild_project_budgets_ui.inc");
include_once($path_to_root . "/projects/includes/db/rebuild_project_budget_allocations_db.inc");
include_once($path_to_root . "/projects/includes/ui/rebuild_project_budget_allocations_ui.inc");
include_once($path_to_root . "/projects/includes/db/rebuild_project_budget_costs_db.inc");
include_once($path_to_root . "/projects/includes/ui/rebuild_project_budget_costs_ui.inc");
include_once($path_to_root . "/projects/includes/db/tasks_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_groups_db.inc");


$js = '';

if (user_use_date_picker()) {
        $js .= get_js_date_picker();
}


page(_($help_context = "Rebuild Project Budgets"),false,false,"",$js);

//---------------------------------------------------------------------------------------------------

check_db_has_projects(_("There are no projects defined in the system."));

simple_page_mode(true);
//---------------------------------------------------------------------------------------------------
$input_error = 0;



function can_commit_task($selected_id, $Mode)
{
        $project = get_project($_POST['project_id']);
        
        $budget_freeze_date = sql2date($project['budget_freeze_date']);
        if(date1_greater_date2(Today(), $budget_freeze_date)){
            display_error( _("All budgets of this project are frozen and can not be updated."));
            set_focus('group_code');
            return false;
        }

        if(!($_POST['type_id'] > 0)){
                display_error( _("Please select a type for the budget group."));
                set_focus('type_id');
                return false;
        }
        
        if(!(strlen($_POST['group_code']) > 0)){
                display_error( _("Code can not be empty."));
                set_focus('group_code');
                return false;
        }
        
        $row = get_existing_code_from_project_task_groups(get_post('project_id'), get_post('group_code'), $selected_id);
        if(isset($row['id']) && $row['id'] > 0){
            $m = "Entered code already used for a budget group in BOQ item group ".$row['boq_group_code']." - ".$row['boq_group_description'];
           display_error($m);
           set_focus('group_code');
           return false;        
        }

        if(!(strlen($_POST['group_description']) > 0)){
                display_error( _("Description can not be empty."));
                set_focus('group_description');
                return false;
        }        

        if (is_date($_POST['start_date']) == FALSE){
            display_error( _("Incorrect task start date"));
            set_focus('start_date');
            return false;
        }
        
        if (is_date($_POST['end_date']) == FALSE){
            display_error( _("Incorrect task end date"));
            set_focus('end_date');
            return false;
        }

        $project_start_date = sql2date($project['start_date']);
        $project_end_date = sql2date($project['end_date']);

        if (date1_greater_date2($project_start_date, $_POST['start_date']))
        {
            display_error( _("Budget group start date can not be ealier than project start date"));
            set_focus('start_date');
            return false;
        }
            
        if (date1_greater_date2($_POST['end_date'], $project_end_date))
        {
            display_error( _("Budget group end date can not be later than project end date"));
            set_focus('end_date');
            return false;
        }
            
        if (date1_greater_date2($_POST['start_date'], $_POST['end_date']))
        {
            display_error( _("Budget group end date can not be later than Budget group start date"));
            set_focus('end_date');
            return false;
        }
        
        $other_tasks_budget_total = get_other_task_groups_budget_total($project['project_id'], $selected_id);
        
        $count = $this_task_group_budget_total = 0;    
        $list_code = 'PROJECT_TASK_DETAIL_TYPES';
        $list_values_q = get_all_list_values($list_code);
        while ($list_value = db_fetch($list_values_q))
        {
            if(isset($_POST['selected_'.$list_value["value_code"]]) && $_POST['selected_'.$list_value["value_code"]] == 1){
                $budget = input_num('budget_'.$list_value["value_code"]);
                if(($budget < 0)){
                    display_error( _("Budget eneterd for detail type ".$list_value["value_name"]." has to be numeric and not negative."));
                    set_focus('budget_'.$list_value["value_code"]);
                    return false;
                }
                else{
                    $this_task_group_budget_total += $budget;
                    $count++;
                }
            }
        }
    
        $new_project_budget = $other_tasks_budget_total + $this_task_group_budget_total; 
        if($new_project_budget > $project['estimated_cost'])
        {
            display_error( _("With the budgets entered, the total of all the individual budgets will exceed the project total cost."));
            set_focus('budget');
            return false;
        }
        
        if($count == 0)
        {
            display_error( "No Budgets selected.");
            set_focus('budget');
            return false;
        }
        
        return true;
}




    

if(isset($_POST['CalculateForBudget'])){
    $task_id = $_POST['task_id'];
    
    if($_POST['task_id'] > 0){
        recalculate_project_task($task_id); 
        display_notification("Costs recalculated for selected budget.");
    }
    else{
        display_error("Please a budget");
    }
    
    
}


if(isset($_POST['CalculateForBOQItemGroup'])){

    $filtering_group_id = 0;
    if($_POST['filtering_group_id'] > 0){
        $filtering_group_id = $_POST['filtering_group_id'];
    }
    recalculate_project_boq_group_task_costs($_POST['project_id'], $filtering_group_id);
    
    display_notification("Costs recalculated for the selected BOQ Item Group.");
}
    
if(isset($_POST['CalculateForEntireProject']))
{
    $project_id = $_POST['project_id'];

    if($project_id > 0){
        recalculate_entire_project_task_costs($project_id);

        display_notification("Costs recalculated for the selected project.");
    }
    else{
         display_error("Please a project");
    }
   
}


start_form();


start_table(TABLESTYLE_NOBORDER);
start_row();
project_list_cells(_("Project:"), 'project_id', null, _('New project'), true, check_value('show_inactive'));
$project_id = get_post('project_id');
if($project_id > 0){
submit_cells('CalculateForEntireProject', _("Calculate For Entire Project"));
}
end_row();
//end_table();
//
//start_table(TABLESTYLE_NOBORDER);
start_row();
project_boq_item_groups_list_cells("BOQ Item Group:", 'filtering_group_id', $project_id, null, '', true);
if($project_id > 0){
submit_cells('CalculateForBOQItemGroup', _("Calculate For BOQ Item Group"));
}
end_row();


if(get_post('filtering_group_id') > 0){
    $filetering_group_id = get_post('filtering_group_id');
}
else{
    $filetering_group_id = 0;
}

start_row();
group_tasks_list_cells("Budget :", 'task_id', $_POST['project_id'], $_POST['filtering_group_id'], null, _('Select a budget'), true);  
if($_POST['task_id'] > 0){
submit_cells('CalculateForBudget', _("Calculate For Budget"));
}
end_row();


end_table();
	
br(2);
    
//display_task_groups_summary($filetering_group_id, $project_id);

//display_task_groups($_POST['project_id'], $_POST['filtering_group_id']);
//------------------------------------------------------------------------------------------------

if(get_post('task_group_id') > 0){
    $selected_id = get_post('task_group_id');
    $Mode = 'Edit';
}
else{
    $selected_id = -1;
}

//display_edit_controls($Mode, $selected_id, $project_id);

//br();
//
//
//submit_center('CalculateProjectBudgetCosts', _("Calculate"), true, '',  'default');
//
//br(2);
//
//submit_center('CalculateForEntireProject', _("Calculate For Entire Project"), true, '',  'default');

br(2);

if (!@$_GET['popup'])
{
	end_form();
	end_page(@$_GET['popup'], false, false);
}	


?>
