<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_EMPLOYEE_PROJECTS';

	$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 . "/projects/includes/db/employee_projects_db.inc");
include_once($path_to_root . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.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 = "Employee Projects"), false, false, "", $js);

simple_page_mode(false);





function display_employee_projects($company_code, $employee_code)
{
    $result = get_employee_all_projects($company_code, $employee_code);

    start_table(TABLESTYLE, "width=60%");
    $th = array( _("Start Date"), _("End Date"), _('Project'),_("Days Per Month"), _('Rate Category'), "", "");
    //inactive_control_column($th);

    table_header($th);
    $k = 0; //row colour counter

    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

        label_cell(sql2date($myrow["start_date"]), "align=center");
        
        if(substr($myrow["end_date"], 0, 4) == '3000'){
            label_cell("");
        }
        else{
            label_cell(sql2date($myrow["end_date"]), "align=center");
        }
        label_cell($myrow["project_description"]);
        label_cell($myrow["days_per_month"], 'align=right');
        label_cell($myrow["rate_category_name"]);
        
        
        
        edit_button_cell("Edit".$myrow["id"], _("Edit"));

        delete_button_cell("Delete".$myrow["id"], _("Delete"));

        end_row();
    }

    //inactive_control_row($th);
    end_table(1);
}


function display_employee_projects_edit_controls($selected_id, $Mode)
{
    start_table(TABLESTYLE2);


    if ($Mode == 'Edit') {
        //editing an existing item category

        $myrow = get_employee_project($selected_id);
        
        $_POST['project_id'] = $myrow["project_id"];
        $_POST['rate_category_id']  = $myrow["rate_category_id"];
        $_POST['days_per_month']  = $myrow["days_per_month"];
        $_POST['start_date'] = sql2date($myrow["start_date"]);
        $_POST['end_date'] = sql2date($myrow["end_date"]);

    }
    else{
        $_POST['project_id'] = 0;
        $_POST['rate_category_id']  = 0;
        $_POST['end_date'] = sql2date(INFINITE_END_DATE);
        $_POST['days_per_month'] = 0;
        $selected_id = -1;
    }


    hidden('selected_id', $selected_id);

    date_row(_('Start Date:'), 'start_date'); 
    date_row(_('End Date:'), 'end_date'); 
    project_list_row("Project", 'project_id', null, 'Select a project');
    qty_row("Days Per Month", 'days_per_month', null, null, null, 0);
    rate_category_list_row(_("Rate Category:"), 'rate_category_id', null, "Select a Rate category");
      


    end_table(1);

    submit_add_or_update_center($selected_id == -1, '', 'both');

}

function can_commint_employee_project()
{
    if(!($_POST['project_id'] > 0))
    {
        display_error(_("Select a project."));
        set_focus('project');  
        return false;
    }
	
    if (!isset($_POST['rate_category_id']) || $_POST['rate_category_id'] == -1)
    {
        display_error(_("Select a rate category."));
        set_focus('rate_category_id');   
        return false;
    }
    
    if (!is_date($_POST['start_date']))
    {
        display_error( _("The start date entered is invalid."));
        set_focus('start_date');
        return false;
    }
    
    if (!is_date($_POST['end_date']))
    {
        display_error( _("The end date entered is invalid."));
        set_focus('start_date');
        return false;
    }
    
    if (date1_greater_date2($_POST['start_date'], $_POST['end_date']))
    {
        display_error( _("End date can not be before the end date."));
        set_focus('end_date');
        return false;
    }
    
    if(!check_num('days_per_month', 0.1))
    {
        display_error( _("Days per month should be grater than zero."));
        set_focus('days_per_month');
        return false;
    }
    
    return true;

}




if (($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') && can_commint_employee_project()) 
{

    if($Mode ==  'UPDATE_ITEM'){
        update_employee_project($selected_id, $_POST['project_id'], $_POST['rate_category_id'],
                        $_POST['start_date'], $_POST['end_date'], input_num('days_per_month'));
        display_notification(_('Selected employee project has been updated'));
    }
    else
    {
        add_employee_project($_POST['company_code'], $_POST['employee_code'], $_POST['project_id'], $_POST['rate_category_id'],
                        $_POST['start_date'], $_POST['end_date'], input_num('days_per_month'));

        display_notification(_('New employee project has been added'));
    }    
    
    $selected = -1;
    $Mode = 'RESET';
    
}

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

if ($Mode == 'Delete')
{
    delete_employee_project($selected_id);
    display_notification(_('Selected employee project has been deleted'));
    $Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
	$sav = get_post('show_inactive');
	//unset($_POST);
	$_POST['show_inactive'] = $sav;
}

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

global $Ajax;

start_form();

start_table(TABLESTYLE_NOBORDER);

    group_companies_list_row("Company:", 'company_code', null, false, true);
    

    group_company_employees_simple_list_row(_("Employee:"), 'employee_code', $_POST['company_code'], null, false, true);

end_table(1);

$Ajax->activate('emp_proj');
div_start('emp_proj');
display_employee_projects($_POST['company_code'], $_POST['employee_code']);
div_end();

display_employee_projects_edit_controls($selected_id, $Mode);


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

?>
