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

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/payroll/includes/db/employee_shifts_db.inc");
include_once($path_to_root . "/payroll/includes/db/employee_departments_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 Shifts Schedule"));

//----------------------------------------------------------------------------------------
simple_page_mode(true);

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


function check_data($selected_id)
{	
        if (!isset($_POST['employee_code']) || strlen($_POST['employee_code']) < 1)
   	{
            display_error( _("Please select an employee."));
            set_focus('employee_code');
            return false;
   	}
        
        if (!is_date($_POST['start_date']))
   	{
            display_error( _("Start date entered is not a valid date."));
            set_focus('start_date');
            return false;
   	}
        
        if (!isset($_POST['week_start_day']) || strlen($_POST['week_start_day']) < 1)
   	{
            display_error( _("Please select week start day."));
            set_focus('employee_code');
            return false;
   	}    
        return true;
    
}


function get_weekday_name($code)
{
    switch ($code)
    {
        case "M":
            return "Monday";
        case "T":
            return "Tuesday";
        case "W":
            return "Wednesday";
        case "H":
            return "Thursday";
        case "F":
            return "Friday";
        case "S":
            return "Saturday";
        case "N":
            return "Sunday";
        default:
            return "Unknown day";
            

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

function get_weekend_work_time_name($code)
{
    switch ($code)
    {
        case "0":
            return "Shift hours";
        case "1":
            return "4 hours";
        case "2":
            return "Half of shift hours";
        case "3":
            return "8 hours";
        case "4":
            return "Zero hours";
        case "5":
            return "5 hours";
        default:
            return "Unknown weekend work time";
            

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


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

    if ($Mode == 'ADD_ITEM') 
    {
            add_employee_shift($_POST['employee_code'], date2sql($_POST['start_date']), 
                                        $_POST['shift_id'], $_POST['week_start_day'],
                                        $_POST['saturday_work_time_id'], $_POST['sunday_work_time_id'],
                                        $_POST['continuous_work'], $_POST['task_id']);

            display_notification(_("Employee shift has been added."));
    } 
    if ($Mode == 'UPDATE_ITEM') 
    {
            update_employee_shift($selected_id, $_POST['employee_code'], date2sql($_POST['start_date']), 
                                        $_POST['shift_id'], $_POST['week_start_day'],
                                        $_POST['saturday_work_time_id'], $_POST['sunday_work_time_id'],
                                        $_POST['continuous_work'], $_POST['task_id']);

            $selected_id = -1;
            display_notification(_("Employee shift has been updated."));
    }
    $Mode = 'RESET';

}

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

if ($Mode == 'Delete')
{
	return;
        
    //delete_loan($selected_id);
	display_notification(_("The leave entitlement has been sucessfully deleted."));
	$Mode = 'RESET';
}

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

if (isset($_POST['_selected_id_update']) )
{
	$selected_id = $_POST['selected_id'];
	$Ajax->activate('_page_body');
}

if (list_updated('employee_code')) 
	$Ajax->activate('price_table');
//--------------------------------------------------------------------------------------------------

if (!@$_GET['popup'])
	start_form();


    emoplyees_list_cells(_("Select an Employee:"), 'employee_code', null,
	  false, true, check_value('show_inactive'));

br(2);	

    $dep_result = get_all_employee_departments($_POST['employee_code']);
    
    div_start('price_table');
    
    display_heading("Emplyee Departments");
    if (db_num_rows($dep_result) == 0)
    {
    	display_note(_("This employee is not attached to any department"));
    }
    else
    {
        start_table(TABLESTYLE, "width=60%");

		$dep_th = array(_('Department'),_('Rate Category'), _("Start Date"), _("End date"));

        table_header($dep_th);

        $k = $j = 0; //row colour counter

        while ($myrow = db_fetch($dep_result))
        {
            alt_table_row_color($k);
            label_cell($myrow["department_name"]);
            label_cell($myrow["rate_category_name"]);
            label_cell($myrow["start_date"]);
            if($myrow["end_date"] == INFINITE_END_DATE){
                label_cell("");
            }
            else{
                label_cell(sql2date($myrow["end_date"]),"align=center");
            }
            end_row();
        }
        
        end_table();
    }
     
    br();
    
    display_heading("Emplyee Shift Schedule");
    
    $result = get_employee_shifts($_POST['employee_code']);
    
   
    
    if (db_num_rows($result) == 0)
    {
    	display_note(_("There are no shifts defined for selected employee"));
    }
    else
    {
        start_table(TABLESTYLE, "width=60%");

		$th = array(_("Start Date"), _("Shift"),_("Week Start Day"),
                            _("Continuous Work"), _("6th Day Work Time"),_("7th Day Work Time"),
                            _("Task"),"", "");

        table_header($th);

        $k = $j = 0; //row colour counter

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

            //label_cell($myrow["id"]);
            label_cell(sql2date($myrow["start_date"]));
            
            label_cell($myrow["shift_name"]);
            label_cell(get_weekday_name($myrow["week_start_day"]));
            
            if($myrow["continuous_work"] == 1)
                label_cell("Yes", "align=center");
            else{
                label_cell("No", "align=center");
            }
            
            label_cell(get_weekend_work_time_name($myrow["saturday_work_time_id"]));
            label_cell(get_weekend_work_time_name($myrow["sunday_work_time_id"]));
            
            label_cell($myrow["description"]);
            
		 	edit_button_cell("Edit".$myrow['id'], _("Edit"));
		 	delete_button_cell("Delete".$myrow['id'], _("Delete"));
            end_row();

            $j++;
            If ($j == 12)
            {
            	$j = 1;
        		table_header($th);
            } //end of page full new headings
        } //end of while loop

        end_table();
    }
    div_end();

//-----------------------------------------------------------------------------------------------
    
if(!isset($_POST['id'])){
    $_POST['id'] = 0;
    $_POST['shift_id'] = "";
    $_POST['start_date'] = "";
    $_POST['week_start_day'] = "";
    $_POST['saturday_work_time_id'] = 0;
    $_POST['sunday_work_time_id'] = 0;
}

if(!isset($_POST['continuous_work'])){
        $_POST['continuous_work'] = 0;
}
    
$dec2 = 6;
if ($Mode =='Edit')
{
    $myrow = get_employee_shift_details($selected_id);

    $_POST['id'] = $myrow["id"];
    $_POST['shift_id'] = $myrow["shift_id"];
    $_POST['start_date'] = sql2date($myrow["start_date"]);
    $_POST['week_start_day'] = $myrow["week_start_day"];
    $_POST['saturday_work_time_id'] = $myrow["saturday_work_time_id"];
    $_POST['sunday_work_time_id'] = $myrow["sunday_work_time_id"];
    $_POST['continuous_work'] = $myrow["continuous_work"];
    $_POST['task_id'] = $myrow["task_id"];
    
    hidden('id', $_POST['id']); 
}

br();

hidden('selected_id', $selected_id);

start_table(TABLESTYLE2);

date_row("Start Date:", "start_date", $_POST['start_date']);
shift_list_row("Shift:", 'shift_id', $_POST['shift_id']);
weekdays_list_row("Week Start Day:", "week_start_day", $_POST['week_start_day']);
yesno_list_row("Continuous Work:", 'continuous_work', $_POST['continuous_work'], null, null, true);
if($_POST['continuous_work'] == 0){
    weekend_work_time_list_row("6th day (Saturday) Work Time:", "saturday_work_time_id", $_POST['saturday_work_time_id']);
    weekend_work_time_list_row("7th day (Sunday) Work Time:", "sunday_work_time_id", $_POST['sunday_work_time_id']);
}
else{
    hidden('saturday_work_time_id', 0);
    hidden('sunday_work_time_id', 0);
}
tasks_all_list_row("Task:", "task_id");
end_table(1);

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

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