<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_REBUILD_PROJECT_BUDGETS';
if (!@$_GET['popup'])
	$path_to_root = "../..";
else	
	$path_to_root = "..";
//include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
//include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");

include_once($path_to_root . "/projects/includes/db/update_rebuilt_project_task_costs_db.inc");
include_once($path_to_root . "/includes/ui/ui_view.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");
include_once($path_to_root . "/projects/includes/db/rebuild_project_budget_costs_db.inc");
include_once($path_to_root . "/projects/includes/ui/update_rebuilt_project_task_costs_ui.inc");




$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

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

page($help_context = "Update Rebuilt Project Budget Costs", false, false, "", $js);
//---------------------------------------------------------------------------------------------
simple_page_mode(true);


function update_recalculated_costs_of_selected_tasks($project_id)
{
    $result = get_task_records($project_id);
    while ($row = db_fetch($result)) {
        if(isset($_POST['recalculated_'.$row['task_id']]) && $_POST['recalculated_'.$row['task_id']] > 0){
            $msg = update_task_recalculated_costs($row['task_id']);  
            if($msg['success'] !== true){
                return $msg;
            }
            
        }
    }
    return $msg;
}




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

    if($_POST['project_id'] > 0){
        $msg = update_recalculated_costs_of_selected_tasks($_POST['project_id']);   
        if($msg['success'] !== true){
            display_error($msg['message']);
        }
        else{
            display_notification("Costs updated for selected budgets.");
        }
    }
    else{
        display_error("Please select a project");
    }
}



if(isset($_POST['UpdateForEntireProject']))
{
    $project_id = $_POST['project_id'];

    if($project_id > 0){
        $msg = update_recalculated_costs_of_all_tasks($project_id);
        if($msg['success'] !== true){
            display_error($msg['message']);
        }
        else{
            display_notification("Costs updated for selected BOQ item group.");
        }
        display_notification("Costs updated for the selected project.");
    }
    else{
        display_error("Please a project");
    }
   
}
//
//if(isset($_POST['UpdateNewCosts']))
//{
//  $task_id = $_POST['task_id'];
//
//    update_new_costs($task_id); 
//  
//}


$Ajax->activate('_page_body');
//--------------------------------------------------------------------------------------------------

start_form();
start_table();
start_row();
    project_list_cells(_("Project:"), 'project_id', null, _('Select a project'), true, check_value('show_inactive'));
end_row();
$project_id = get_post('project_id');

end_table(1);

//submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');
submit_center('SearchOrders', _("Search"), true, 'Search',  'default');

br(2);

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


$project_id = 0;
if(isset($_POST['project_id'])){
  $project_id = $_POST['project_id'];
}


display_all_task_recalculations($project_id);

//display_rebuilt_project_task_allocation_details($task_id);
//
//br(2);
//
//display_rebuilt_project_task_cost_details($task_id);

//if(isset($_POST['SearchOrders'])){
  
 
   

//------------------------------------------------------------------------------------------------      
//if (get_post('StockLocation') != $all_items) {
//	$cols[_("Location")] = 'skip';
//}
//---------------------------------------------------------------------------------------------------



br(2);


//start_row();
    




if(isset($_POST['task_id']) && $_POST['task_id'] > 0){
    submit_center('UpdateForTask', _("Update for Budget"), true, '',  'default');
}    


br(1);



if(isset($_POST['project_id']) && $_POST['project_id'] > 0){
    submit_center('UpdateSelectedTasks', _("Update Selected Budgets"), true, '',  'default');
}

br(1);

if(isset($_POST['project_id']) && $_POST['project_id'] > 0){
  submit_center('UpdateForEntireProject', _("Update All Budgets"), true, '',  'default');
}


br(2);


//end_row();

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



?>
