<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_EST_COPY_TO_SYS_PROJECT';
if (!@$_GET['popup'])
	$path_to_root = "../..";
else	
	$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/admin/db/list_values_db.inc"); 

//include_once($path_to_root . "/includes/custom_scripts/custom_script_copy_budgets_to_boq_items.inc");
include_once($path_to_root . "/estimation/includes/est_copy_est_project_to_sys_project_ui.inc");
include_once($path_to_root . "/estimation/includes/db/est_copy_est_project_to_sys_project_db.inc");
include_once($path_to_root . "/includes/ui/ui_lists_estimation.inc");
include_once($path_to_root . "/estimation/includes/db/est_project_boq_item_groups_db.inc");
include_once($path_to_root . "/estimation/includes/db/est_project_boq_items_db.inc");
include_once($path_to_root . "/estimation/includes/db/est_projects_db.inc");

include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_groups_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc");
include_once($path_to_root . "/projects/includes/ui/project_boq_item_groups_ui.inc");
include_once($path_to_root . "/projects/includes/ui/project_boq_items_ui.inc");

include_once($path_to_root . "/projects/includes/db/project_boq_item_detail_edit_db.inc");

include_once($path_to_root . "/estimation/includes/db/est_project_boq_item_detail_edit_db.inc");

include_once($path_to_root . "/estimation/includes/db/est_project_boq_item_costs_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_costs_db.inc");

include_once($path_to_root . "/estimation/includes/db/est_project_boq_item_material_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_material_db.inc");

include_once($path_to_root . "/estimation/includes/db/est_project_boq_item_labour_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_labour_db.inc");

include_once($path_to_root . "/estimation/includes/db/est_project_boq_item_assets_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_assets_db.inc");

include_once($path_to_root . "/estimation/includes/db/est_project_boq_item_transport_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_transport_db.inc");


$js = '';

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

if (!@$_GET['popup'])
        page(_($help_context = "Copy Estimation Project to a System Project"),false,false,"",$js);

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

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

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

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

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

function can_process()
{

    $est_project = get_est_project_record($_POST['est_project_id']);
    if($est_project['approval_status'] != AS_APPROVED){
        $message = ("This estimation project is not approved. It can not be copied to a system project.");
        display_error($message);
        return false;
    }
    
    $sys_project = get_project_record($_POST['project_id']);
    if(!($sys_project['approval_status'] == AS_NEW || $sys_project['approval_status'] == AS_WITH_THE_APPROVER || 
            $sys_project['approval_status'] == AS_AWAITING_APPROVAL || $sys_project['approval_status'] == AS_BACK_TO_ORIGINATOR ||
            $sys_project['approval_status'] == AS_REOPENED)){
        $message = ("This system project status does not allow further updating.");
        display_error($message);
        return false;
    }
    
    
    
    return true;
    
}
//----------------------------------------------------------------------------------------------------

//$est_project_id = get_post('est_project_id');
//
//if ($Mode=='ADD_ITEM') 
//{             
//    if(can_process($Mode)){
//        add_competitor($_POST['est_project_id'], $_POST['competitor'], input_num('value'),$_POST['bid_winner'],$_POST['comment']);            
//        $msg = _("The new competitor has been added.");
//             
//        clear_competitor_data();
//        $msg = _("Competitor has been added.");
//
//        display_notification($msg);
//        $Mode = 'RESET';  
//    }
//}
//
if (isset($_POST['COPY_PROJECT'])) 
{
    if(can_process($Mode)){
        $msg = copy_est_project_to_sys_project($_POST['est_project_id'], $_POST['filtering_est_group_id'], $_POST['project_id']);

        if($msg["success"] !== true){
            display_error($msg['message']);
        }
        else{
            display_notification($msg['message']);
        }        
    }
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}

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

start_form();

br(1);   
    
start_table(TABLESTYLE_NOBORDER);

est_project_list_row(_("Estimation Project:"), 'est_project_id', null, _('Select an Estimated Project'), true);


$est_project_id = $_POST['est_project_id'];

est_project_boq_item_groups_list_row("BOQ Item Group:", 'filtering_est_group_id', $est_project_id, null,  _('No BOQ Item Group'), true);

vertical_space(spaces(2));
project_list_row(_("System Project:"), 'project_id', null, _('Select a System Project'), true);
end_row();
end_table();

br();


submit_center("COPY_PROJECT", "Copy Project");

br(1);

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