<?php

$page_security = 'SA_CALCULATE_TOOL_USAGE_CHARGES';
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 . "/assets/includes/db/calculate_tool_usage_costs_db.inc");
include_once($path_to_root . "/assets/includes/ui/calculate_tool_usage_costs_ui.inc");
include_once($path_to_root . "/assets/includes/db/tool_requests_db.inc");
include_once($path_to_root . "/assets/includes/db/tool_issues_db.inc");
include_once($path_to_root . "/assets/includes/db/tool_returns_db.inc");
include_once($path_to_root . "/includes/common_functions.inc");
include_once($path_to_root . "/includes/project_cost_functions.inc");


$js = '';

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


page(_($help_context = "Calculate Tool Usage Charges"),false,false,"",$js);

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

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

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





if(isset($_POST['CalculateAssetUsageCosts'])){
    
    $cutoff_date_ = $_POST['cutoff_date'];

    $cptuc_msg = can_process_tool_usage_costs(); 
    if($cptuc_msg['success'] !== true){ 
        display_error($cptuc_msg['message']);
    }else{
        begin_transaction();
        $cauc_msg = calculate_tool_usage_costs($cutoff_date_); 
        if($cauc_msg['success'] !== true){
            cancel_transaction();
            display_error($cauc_msg['message']);
        }else{
            commit_transaction();
            display_notification("Asset usage charges up to $cutoff_date_ have been created.");
        }
    }

}



start_form();


start_table(TABLESTYLE);

date_row("Cutoff Date: :", 'cutoff_date');

end_table(1);


start_table(TABLESTYLE_NOBORDER);
start_row();

submit_cells('CalculateAssetUsageCosts', _("Calculate Asset Usage Costs"));

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);
}	


?>
