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


$page_security = 'SA_ITEM';
$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 = "Jobs"), @$_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 . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/projects/includes/db/project_serial_categories_db.inc");
include_once($path_to_root . "/projects/includes/db/tasks_db.inc");

$user_comp = user_company();

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


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

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

$project_id = get_post('project_id');

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

if (get_post('cancel')) {
	$_POST['NewProjectID'] = $project_id = $_POST['project_id'] = '';
    clear_data();
	set_focus('project_id');
	$Ajax->activate('_page_body');
}
if (list_updated('category_id') || list_updated('mb_flag')) {
	$Ajax->activate('details');
}


function clear_data()
{
	unset($_POST['long_description']);
	unset($_POST['description']);
	unset($_POST['project_manager_id']);
	unset($_POST['company_category_id']);
	unset($_POST['group_serial_no']);
	unset($_POST['company_serial_no']);
	unset($_POST['NewProjectID']);
	unset($_POST['year']);
	unset($_POST['customer_category_id']);
	unset($_POST['inactive']);       
    unset($_POST['project_category_id']);
    unset($_POST['category_serial_no']);
    unset($_POST['job_value']);
    unset($_POST['project_name']);
    unset($_POST['project_code']);
}

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

if (isset($_POST['addupdate'])) 
{ 
    $input_error = 0;
 
        if (is_date($_POST['start_date']) == FALSE){

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

                $input_error = 1;
                display_error( _("Incorrect end date"));
                set_focus('end_date');
                return;
        }
        
       if (date1_greater_date2($_POST['start_date'], $_POST['end_date']))
	{
		$input_error = 1;
		display_error( _("Job end date can not be greater than task start date"));
		set_focus('end_date');
                return;
	}

        
       if (check_num('project_value', 0) == false) 
	{
		$input_error = 1;
		display_error( _('Job value must be numeric and positive.'));
		set_focus('project_value');
                return;
	}  
    
    if (strlen($_POST['project_code']) == 0) 
	{
		$input_error = 1;
		display_error( _('Job code can not be empty.'));
		set_focus('project_code');
        return;
	} 
    
    if (strlen($_POST['project_name']) == 0) 
	{
		$input_error = 1;
		display_error( _('Job name can not be empty.'));
		set_focus('project_name');
        return;
	}  
        
	if($new_item)
        {
            if ($_POST['company_category_id'] == -1) 
            {
                    $input_error = 1;
                    display_error( _('Select a company.'));
                    set_focus('company_category_id');
                    return;
            } 
            if (strlen($_POST['group_serial_no']) == 0) 
            {
                    $input_error = 1;
                    display_error( _('The group serial number must be entered.'));
                    set_focus('group_serial_no');
                    return;
            } 
            if (check_num('group_serial_no', 0) == FALSE)
            {
                    $input_error = 1;
                    display_error( _("Group serial number must be numeric."));
                    set_focus('group_serial_no');
                    return;
            }
             if ($_POST['customer_category_id'] == -1) 
            {
                    $input_error = 1;
                    display_error( _('Select a customer category.'));
                    set_focus('customer_category_id');
                    return;
            } 
            if ($_POST['project_category_id'] == -1) 
            {
                    $input_error = 1;
                    display_error( _('Select a project.'));
                    set_focus('project_category_id');
                    return;
            }
        }
	if ($input_error != 1)
	{
		
		if (!$new_item) 
		{ /*so its an existing one */
                    update_project($project_id, $_POST['long_description'], $_POST['project_manager_id'],  
                                $_POST['inactive'],
				$_POST['start_date'], $_POST['end_date'], input_num('project_value'), $_POST['project_code'], $_POST['project_name']) ;
                    
			set_focus('description');
			$Ajax->activate('description'); // in case of status change
			display_notification(_("Job has been updated."));
		} 
		else 
		{ //it is a NEW part                  
                    $myrow = get_next_number($_POST['company_category_id']);
                    $company_serial_no = $myrow["next_number"];
                    $result = get_next_number($_POST['project_category_id']);
                    $category_serial_no = $result["next_number"];
                    $description = $_POST['company_category_id']. '/' .$_POST['group_serial_no']. '/' .$company_serial_no. '/' .$_POST['year']. '/' .$_POST['customer_category_id']. '/' .$_POST['project_category_id']
                            . '/' .$category_serial_no;
		add_project($description,
				$_POST['long_description'], $_POST['inactive'],
                                $_POST['project_manager_id'], $_POST['company_category_id'],
                                $_POST['group_serial_no'], $company_serial_no,
                                $_POST['year'], $_POST['customer_category_id'],
                                $_POST['project_category_id'], $category_serial_no,
                                $_POST['start_date'], $_POST['end_date'], input_num('project_value'), 
                                $_POST['project_code'], $_POST['project_name']) ;
                      
                        $group_serial_no = $_POST['group_serial_no']+1;
                        save_next_reference(103, $group_serial_no);    
                        
                        $compny_next_number = $company_serial_no+1;
                        save_next_number($_POST['company_category_id'], $compny_next_number);
                        
                        $project_next_number = $category_serial_no+1;
                        save_next_number($_POST['project_category_id'], $project_next_number);
                        
                        clear_data();
			display_notification(_("A new Job has been added."));
			set_focus('description');
		}
		$Ajax->activate('_page_body');
	}
}

if (get_post('clone')) {
	unset($_POST['project_id']);
	$project_id = '';
	unset($_POST['inactive']);
	set_focus('NewProjectID');
	$Ajax->activate('_page_body');
        //l('224 inside get_post ');
}

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

//function check_usage($project_id, $dispmsg=true)
//{
//	$msg = item_in_foreign_codes($project_id);
//
//	if ($msg != '')	{
//		if($dispmsg) display_error($msg);
//		return false;
//	}
//	return true;
//}
//
if (isset($_POST['delete']) && strlen($_POST['delete']) > 1) 
{
    $tasks_list = get_tasks($_POST['project_id']);
    $myrow = db_fetch($tasks_list);
    if($myrow != '')
    {
        display_error(_("Selected job can not be deleted.It has tasks"));
    }
    else 
    {
        $project_id = $_POST['project_id'];
        delete_project($project_id);

        display_notification(_("Selected job has been deleted."));

        clear_data();
        $Ajax->activate('_page_body'); 
    }
	
}
function project_settings(&$project_id) 
{
	global $SysPrefs, $path_to_root, $new_item, $pic_height, $Refs;;

	start_outer_table(TABLESTYLE2);

	table_section(1);

	table_section_title(_("Jobs"));
	//------------------------------------------------------------------------------------
	if ($new_item) 
	{
        clear_data();
        
        text_row(_("Job Code:"), 'project_code', null, 20, 20);
        
        text_row(_("Job Name:"), 'project_name', null, 20, 50);
        
        project_serial_category_list_row(_("Company Category:"), 'company_category_id', null,
                    _('Select a company category'), true, 3,check_value('show_inactive')); 
        
        ref_row(_("Group Serial No:"), 'group_serial_no', '', $Refs->get_next(103));
        
        if($_POST['company_category_id'] != -1){
            $myrow = get_next_number($_POST['company_category_id']);
            $company_serial_no = $myrow["next_number"];
            label_row(_("Company Serial No:"), $company_serial_no);
            //text_row(_("Company Serial No:"), 'company_serial_no', $_POST['company_serial_no'], 20, 20);
        }
        else{
            label_row(_("Company Serial No:"), '');
        }
        
        value_list_code_row(_("Year:"), 'year', 'YEAR');
       
        project_serial_category_list_row(_("Customer Category:"), 'customer_category_id', null,
                    _('Select a customer category'), true, 2,check_value('show_inactive'));
        
        project_serial_category_list_row(_("Job Category:"), 'project_category_id', null,
                    _('Select a job category'), true, 1,check_value('show_inactive'));

        if($_POST['project_category_id'] != -1){
            $result = get_next_number($_POST['project_category_id']);
            $category_serial_no = $result["next_number"];
        
            label_row(_("Category Serial No:"), $category_serial_no);
        }
        else{
            label_row(_("Category Serial No:"), '');
        }
         //ref_row(_("Category Serial No:"), 'category_serial_no', '', $Refs->get_next(101));
     
        date_row(_('Start date:'), 'start_date');
        
        date_row(_('End date:'), 'end_date');
        amount_row(_('Job Value:'), 'project_value');
        user_list_row(_("Job Manager:"), 'project_manager_id', null, false, true);
        
        textarea_row(_('Description:'), 'long_description', null, 31, 3);

        $fresh_item = !isset($_POST['NewProjectID']) || $new_item 
            || check_usage($_POST['project_id'],false);


        record_status_list_row(_("status:"), 'inactive');
        
        end_outer_table(1);
		$_POST['inactive'] = 0;
	} 
	else 
	{ // Must be modifying an existing item

        if (get_post('NewProjectID') != get_post('project_id') || get_post('addupdate')) { // first item display
            
            $_POST['NewProjectID'] = $_POST['project_id'];
			$myrow = get_project($_POST['NewProjectID']);
                        
//			$_POST['long_description'] = $myrow["long_description"];
			$_POST['description'] = $myrow["description"];
			$_POST['project_manager_id']  = $myrow["project_manager_id"];
//			$_POST['created_on']  = sql2date($myrow["created_on"]);
//			$_POST['updated_on']  = sql2date($myrow["updated_on"]);
            $_POST['group_serial_no'] =  $myrow['group_serial_no'];
			$_POST['company_category_name']  = $myrow["company_category_name"];
            $_POST['customer_category_name'] =  $myrow['customer_category_name'];
            $_POST['project_category_name'] =  $myrow['project_category_name'];
			$_POST['year'] =  $myrow['year_value'];
//			$_POST['project_category_id'] = $myrow['project_category_id'];
			$_POST['category_serial_no']	= $myrow['category_serial_no'];
			$_POST['company_serial_no']	= $myrow['company_serial_no'];	
//			$_POST['inactive'] = $myrow['inactive'];
            $_POST['start_date'] = sql2date($myrow['start_date']);
            $_POST['end_date'] = sql2date($myrow['end_date']);
            $_POST['project_value'] = $myrow['project_value'];
            $_POST['project_code'] = $myrow['project_code'];
            $_POST['project_name'] = $myrow['project_name'];            
		}
        
             text_row(_("Job Code:"), 'project_code', $_POST['project_code'], 20, 20);
        
            text_row(_("Job Name:"), 'project_name', $_POST['project_name'], 20, 50);
            
            label_row(_("Name:"),$_POST['description']);
            //hidden('NewProjectID', $_POST['NewProjectID']);
            label_row(_("Company Code:"),$_POST['company_category_name']);
            label_row(_("Group Serial No:"),$_POST['group_serial_no']);
            label_row(_("Company Serial No:"),$_POST['company_serial_no']);
            label_row(_("Year:"),$_POST['year']);
            label_row(_("Customer Category:"),$_POST['customer_category_name']);
            label_row(_("Job Category:"),$_POST['project_category_name']);
            label_row(_("Category Serial No:"),$_POST['category_serial_no']);
        
            date_row(_('Start date:'), 'start_date');

            date_row(_('End date:'), 'end_date');
            amount_row(_("Job Value:"), 'project_value');
            user_list_row(_("Job Manager:"), 'project_manager_id', null, false, true);

            textarea_row(_('Description:'), 'long_description', null, 31, 3);

//            $fresh_item = !isset($_POST['NewProjectID']) || $new_item 
//                    || check_usage($_POST['project_id'],false);


            record_status_list_row(_("Item status:"), 'inactive');
            end_outer_table(1);
                    set_focus('description');

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

            submit_return('select', get_post('project_id'), 
                    _("Select this items and return to document entry."), 'default');
            //submit('clone', _("Clone This Item"), true, '', true);
            submit('delete', _("Delete This Project"), true, '', true);
            submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
    }

    div_end();
}

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

start_form(true);

if (db_has_projects()) 
{
	start_table(TABLESTYLE_NOBORDER);
	start_row();
    project_list_cells(_("Select a job:"), 'project_id', null,
	  _('New job'), true, check_value('show_inactive'));
    
	$new_item = get_post('project_id')=='';
        
        if(get_post('project_id') == -1 || get_post('project_id') == ''){
            $new_item = TRUE;
        }


//	check_cells(_("Show inactive:"), 'show_inactive', null, true);
	end_row();
	end_table();

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



div_start('details');

$project_id = get_post('project_id');


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

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

tabbed_content_start('tabs', array(
		'settings' => array(_('&General settings'), $project_id),
		'budgets' => array(_('&Budgets'), $project_id),
        'billing_schedule' => array(_('&Job Billing Schedule'), $project_id),
        'users' => array(_('&Users'), $project_id),
	));
	
	switch (get_post('_tabs_sel')) 
        {
		default:
		case 'settings':
                        project_settings($project_id); 
			break;
		case 'budgets':
			$_GET['project_id'] = $project_id;
			$_GET['popup'] = 1;
			include_once($path_to_root."/projects/manage/tasks.php");
			break;
                case 'billing_schedule':
			$_GET['project_id'] = $project_id;
			$_GET['popup'] = 1;
			include_once($path_to_root."/projects/manage/projects_billing_schedule.php");
			break;
                case 'users':
			$_GET['project_id'] = $project_id;
			$_GET['popup'] = 1;
			include_once($path_to_root."/projects/manage/users.php");
			break;
	};
br();
tabbed_content_end();

div_end();


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

end_form();

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

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