<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_EST_INITIATE_PROJECTS';
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");
include_once ($path_to_root . "/trace.php");
$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 = "Initiate Estimation Project"), @$_REQUEST['popup'], false, "", $js);

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/estimation/includes/db/est_initiate_project_db.inc");
include_once($path_to_root . "/estimation/includes/db/est_projects_db.inc");
//include_once($path_to_root . "/estimation/includes/db/project_approval_path_db.inc");
//include_once($path_to_root . "/estimation/includes/db/project_serial_categories_db.inc");
//include_once($path_to_root . "/estimation/includes/db/tasks_db.inc");
//include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/estimation/includes/ui/est_approve_project_ui.inc");
include_once($path_to_root . "/includes/ui/ui_lists_estimation.inc");
include_once($path_to_root . "/includes/approval_functions.inc");

$user_comp = user_company();

$new_item = get_post('est_project_id')== '' || get_post('est_project_id')== -1 || get_post('cancel') || get_post('clone'); 


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

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

$est_project_id = get_post('est_project_id');

if (list_updated('est_project_id')) {
	$_POST['NewProjectID'] = $est_project_id = get_post('est_project_id');
    clear_data();
	$Ajax->activate('details');
	$Ajax->activate('controls');
}

if (get_post('cancel')) {
	$_POST['NewProjectID'] = $est_project_id = $_POST['est_project_id'] = '';
    clear_data();
	set_focus('est_project_id');
	$Ajax->activate('_page_body');
}

//if (isset($_GET['Approved_id'])) 
//{
//    $trans_no = $_GET['Approved_id'];
//    
////    $project = get_project_record($trans_no);
//    $message = get_session_variable('approve_project_messages');
//    clear_session_variable('approve_project_messages');
//
//    display_notification_centered($message);
//    
//    hyperlink_no_params("$path_to_root/projects/inquiry/unapproved_projects_inquiry.php", _("Approve another project."));
//
//    display_footer_exit();
//}

function clear_data()
{
    unset($_POST['code']);
    unset($_POST['description']);
    unset($_POST['department_id']);
    unset($_POST['approval_project_id']);
    unset($_POST['data_access_group_id']);
    unset($_POST['project_start_date']);
    unset($_POST['project_end_date']);
    unset($_POST['NewProjectID']);
	
}

function can_commit($new_item){
    
    if(!$new_item){
        $est_project = get_est_project_record($_POST['est_project_id']);
        if(!($est_project['approval_status'] == AS_NEW || $est_project['approval_status'] == AS_WITH_THE_APPROVER || 
                $est_project['approval_status'] == AS_AWAITING_APPROVAL || $est_project['approval_status'] == AS_BACK_TO_ORIGINATOR ||
                $est_project['approval_status'] == AS_REOPENED)){
            $message = ("This estimation project status does not allow further updating.");
            display_error($message);
            return false;
        }
    }    
    
    $input_error = 0;
    
    if (!(strlen($_POST['code']) > 0)){

        $input_error = 1;
        display_error( _("Enter a project code."));
        set_focus('code');
        return false;
    }
 
    if (!(strlen($_POST['description']) > 0)){

        $input_error = 1;
        display_error( _("Enter description."));
        set_focus('description');
        return false;
    }
    
    if (!(is_date($_POST['bid_date']) == TRUE)){

        $input_error = 1;
        display_error( _("Incorrect bid opening date."));
        set_focus('bid_date');
        return false;
    }
    
    if (!(is_date($_POST['project_start_date']) == TRUE)){

        $input_error = 1;
        display_error( _("Incorrect start date."));
        set_focus('project_start_date');
        return false;
    }
    
    if (!(is_date($_POST['project_end_date']) == TRUE)){

            $input_error = 1;
            display_error( _("Incorrect end date."));
            set_focus('project_end_date');
            return false;
    }
        
//    if (date1_greater_date2($_POST['project_start_date'], $_POST['project_end_date']))
//	{
//		$input_error = 1;
//		display_error( _("Project end date can not be greater than task start date"));
//		set_focus('project_end_date');
//                return false;
//	}

    if (!($_POST['department_id']> 0 )) 
    {
            $input_error = 1;
            display_error( _('Please select a department.'));
            set_focus('department_id');
            return false;
    }   
    
    if($new_item){
        if (!($_POST['approval_project_id']> 0 )) 
        {
                $input_error = 1;
                display_error( _('Please select a approval project.'));
                set_focus('approval_project_id');
                return false;
        }   
    }

    if(!($_POST['data_access_group_id'] > 0)) 
    {
        $input_error = 1;
        display_error( _('Please select a Data Access Group.'));
        set_focus('data_access_group_id');
        return false;
    } 
    
    if(!($_POST['created_by'] > 0)) 
    {
        $input_error = 1;
        display_error( _('Please select an originator.'));
        set_focus('created_by');
        return false;
    }
    
    
    return true;
}



function display_initiate_estimation_controls($Mode, $selected_id) 
{
    
    global $SysPrefs, $path_to_root, $new_item, $pic_height, $Refs;

    start_outer_table(TABLESTYLE2);

    table_section(1);

    table_section_title(_("Initiate Estimation"));
    //------------------------------------------------------------------------------------

    
    
        if ($Mode == "Edit") {

            $_POST['NewProjectID'] = $_POST['est_project_id'];
            $myrow = get_initiate_est_project_details($selected_id);
            $_POST['code'] = $myrow["code"];
            $_POST['description'] = $myrow["description"];
            $_POST['department_id'] = $myrow['department_id'];
            $_POST['bid_date'] = sql2date($myrow['bid_date']);
            $_POST['project_start_date'] = sql2date($myrow['start_date']);
            $_POST['project_end_date'] = sql2date($myrow['end_date']);
            $_POST['approval_project_id'] = $myrow['project_id'];
            $_POST['data_access_group_id'] = $myrow['dag_id'];
            $_POST['created_by'] = $myrow['created_by'];
            $_POST['approval_status'] = $myrow['approval_status'];
                        
        }
        else{
            $_POST['approval_status'] = 0;
        }
        
            if ($Mode == "Edit") {
                label_row("No:", $_POST['est_project_id']);
            }
            text_row("Code", "code", null, 25, 50);
            text_row(_("Description:"), 'description', null, 50, 200);
            date_row(_('Bid Opening date:'), 'bid_date');
            value_list_row(_("Department:"), 'department_id', 'DEPARTMENTS', null,"Select a Department");   
            
            date_row(_('Start date:'), 'project_start_date');
            date_row(_('End date:'), 'project_end_date');
            
            value_list_row("Data Access Group:", 'data_access_group_id', 'DATA_ACCESS_GROUPS', null, 'Select a Data Access Group', false);
            
            if ($Mode == "Edit") {
                $approval_project = get_project_record($_POST['approval_project_id']);
                label_row("Approval Project:", $approval_project['project_code']." - ".$approval_project['description']);
            }
            else{
                project_list_row(_("Approval Project:"), 'approval_project_id', null, _('Select an approval project'), false);
            }
            system_users_list_row("Originator:", 'created_by', null, 'Select an originator');
            
            global $approval_statuses;
            $s = get_project_trans_view_str(ST_APPROVAL_LOG, $_POST['est_project_id']."&source_type_id=".ST_APPROVE_EST_PROJECT, "Approval Status");
            label_row($s,$approval_statuses[$_POST['approval_status']]);
        
            end_outer_table(1);


        
            div_start('controls');
                if (!isset($_POST['NewProjectID']) || $new_item) 
                {
                    submit_center('addupdate', _("Insert New Project"), true, '', 'default');
                } 
                else 
                {
                    submit_center_first('addupdate', _("Update Project"), '', @$_REQUEST['popup'] ? true : 'default');
                }

            div_end();

	}
 
        
//------------------------------------------------------------------------------------

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

    if(can_commit($new_item)){

        if (!$new_item){ /*so its an existing one */
            
            $approval_required = 0;

            begin_transaction();
            
            update_initiate_est_project($est_project_id, $_POST['code'], $_POST['description'], 
                                        $_POST['bid_date'], $_POST['project_start_date'], $_POST['project_end_date'], 
                                        $_POST['department_id'], $_POST['data_access_group_id'], $_POST['created_by']);

            commit_transaction();
            set_focus('description');
            $Ajax->activate('description'); // in case of status change
            display_notification(_("Initiate Estimation Project has been updated."));
            
        } 
        else 
        { 
                    
            begin_transaction();

            $approved = 0;
            $add_budgets = 1;

            add_initiate_est_project( $_POST['code'], $_POST['description'], $_POST['department_id'], 
                                    $_POST['bid_date'], $_POST['project_start_date'], $_POST['project_end_date'], 
                                    $_POST['approval_project_id'], $_POST['data_access_group_id'], $_POST['created_by']);

            commit_transaction();

            clear_data();

            display_notification(_("A new Initiate Estimation has been added."));
            set_focus('description');
        }
        
    }
    
}

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

$Ajax->activate('est_project_id');

start_form(true);

if (db_has_projects()) 
{
	start_table(TABLESTYLE_NOBORDER);
	start_row();
            est_project_list_cells(_("Select an Estimation:"), 'est_project_id', null, _('New Estimation project'), true, check_value('show_inactive'));
        end_row();
        
            if(get_post('est_project_id') > 0){
                $selected_id = get_post('est_project_id');
                $Mode = 'Edit';
            }
            else{
                $selected_id = -1;
                $Mode ="";
            }
        
        start_row();
            display_initiate_estimation_controls($Mode, $selected_id);
        end_row();
        end_table();
        br(1);
        if(get_post('est_project_id') > 0){
            $selected_id = get_post('est_project_id');
            $Mode = 'Edit';
        }
        else{
            $selected_id = -1;
            $Mode ="";
        }


      

//	if (get_post('_show_inactive_update')) {
//		$Ajax->activate('est_project_id');
//		set_focus('est_project_id');
//	}
}
else
{
	hidden('est_project_id', get_post('est_project_id'));
}



div_start('details');

$est_project_id = get_post('est_project_id');


if ($est_project_id == -1){
    $est_project_id = '';
}

if (!$est_project_id){
	unset($_POST['_tabs_sel']); // force settings tab for new customer
}


div_end();


hidden('popup', @$_REQUEST['popup']);

end_form();

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

end_page(@$_REQUEST['popup']);


?>











