<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_SHIFTS';
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/payroll_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 = "Shifts"));

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

function check_data($selected_id)
{	
    if (strlen($_POST['shift_name']) < 1)
    {
        display_error( _("Shift name can not be empty."));
        set_focus('shift_name');
        return false;
    }
    
    if (!is_time($_POST['start_time1'])) 
    {
        display_error( _('Start time1 is not a valid time value'));
        set_focus('start_time');
        return false;
    }

    if (!is_time($_POST['duration1'])) 
    {
        display_error( _('Duration1 is not a valid time duration'));
        set_focus('duration');
        return false;
    }
    
//    if($_POST['split_shift'] == 0){
//        $_POST['start_time2'] = "00:00:00";
//        $_POST['duration2'] = "00:00:00";
//    }
    
//    if (!is_time($_POST['start_time2'])) 
//    {
//        display_error( _('Start time2 is not a valid time value'));
//        set_focus('start_time');
//        return false;
//    }
//
//    if (!is_time($_POST['duration2'])) 
//    {
//        display_error( _('Duration2 is not a valid time duration'));
//        set_focus('duration');
//        return false;
//    }
    
//    if($_POST['split_shift'] == 1){
//        if($_POST['start_time1'] >= $_POST['start_time2']){
//            display_error( _('Start time 1 should be earlier than Stat time 2 for split shifts'));
//            set_focus('start_time1');
//            return false;
//        }
//    }
    
    if (!is_time($_POST['ot_start_gap'])) 
    {
        display_error( _('OT start gap is not a valid time duration'));
        set_focus('ot_start_gap');
        return false;
    }

    if (!is_time($_POST['qualifying_hrs_for_ot'])) 
    {
        display_error( _('Qualifying hrs for OT is not a valid time duration'));
        set_focus('qualifying_hrs_for_ot');
        return false;
    }
    
    
    return true;
    
}
//--------------------------------------------------------------------------------------------------

if (($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') && check_data($selected_id))
{
    $_POST['split_shift'] = 0;
    $_POST['start_time2'] = "00:00:00";
    $_POST['duration2'] = "00:00:00";
    
    if ($Mode == 'ADD_ITEM') 
    {
            add_shift($_POST['shift_name'], time2sql($_POST['day_start']),
                                    time2sql($_POST['start_time1']),time2sql($_POST['duration1']), 
                                    $_POST['split_shift'],
                                    time2sql($_POST['start_time2']),time2sql($_POST['duration2']), 
                                    time2sql($_POST['ot_start_gap']),
                                    time2sql($_POST['qualifying_hrs_for_ot']),$_POST['table1_id'],
                                    $_POST['table2_id'], $_POST['no_ot'], $_POST['inactive']);
            
            display_notification(_("Shift has been added."));
    } 
    if ($Mode == 'UPDATE_ITEM') 
    {
            update_shift($selected_id, $_POST['shift_name'], time2sql($_POST['day_start']),
                                    time2sql($_POST['start_time1']),time2sql($_POST['duration1']),
                                    $_POST['split_shift'],
                                    time2sql($_POST['start_time2']),time2sql($_POST['duration2']), 
                                    time2sql($_POST['ot_start_gap']),
                                    time2sql($_POST['qualifying_hrs_for_ot']),$_POST['table1_id'],
                                    $_POST['table2_id'], $_POST['no_ot'], $_POST['inactive']);

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

}

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

if ($Mode == 'Delete')
{
	return;
        
        delete_shift($selected_id);
	display_notification(_("The shift 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 (!@$_GET['popup'])
	start_form();


br();	

    $result = get_shift_records();
    
    div_start('price_table');
    
    if (db_num_rows($result) == 0)
    {
    	display_note(_("There are no shifts defined."));
    }
    else
    {
        start_table(TABLESTYLE, "width=90%");

		$th = array(_("id"), _("Shift Name"), _("Day Start (24hr)"),
                        _("Start Time1 (24hr)"), _("Duration1 (hrs)"), 
                        _("OT Start Gap (hrs)"),
                        _("Qualifying Hrs For OT"), _("Normal OT"), _("Double OT"),
                        _("No OT"), _("Inactive"), "", "");

        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($myrow["shift_name"]);
            label_cell(sql2time($myrow["day_start"]), "align=center");
            label_cell(sql2time($myrow["start_time1"]), "align=center");
            label_cell(sql2time($myrow["duration1"]), "align=center");
            label_cell(sql2time($myrow["ot_start_gap"]), "align=center");
            label_cell(sql2time($myrow["qualifying_hrs_for_ot"]), "align=center");
            label_cell($myrow["table1_name"]);
            label_cell($myrow["table2_name"]);
            
            if($myrow["no_ot"] == 1)
                label_cell("Yes","align=center");
            else {
                label_cell("No", "align=center");
            }
            
            if($myrow["inactive"] == 1)
                label_cell("Yes","align=center");
            else {
                label_cell("No", "align=center");
            }
            
		 	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();
    

    $Ajax->activate('price_details');


//-----------------------------------------------------------------------------------------------
    if(!isset($_POST['id'])){$_POST['id'] = 0;}
    if(!isset($_POST['shift_name'])){$_POST['shift_name'] = "";}
    if(!isset($_POST['day_start'])){$_POST['day_start'] = 0;}
    if(!isset($_POST['start_time1'])){$_POST['start_time1'] = 0;}
    if(!isset($_POST['duration1'])){$_POST['duration1'] = 0;}
    
    if(!isset($_POST['split_shift'])){$_POST['split_shift'] = 0;}
    
    if(!isset($_POST['start_time2'])){$_POST['start_time2'] = 0;}
    if(!isset($_POST['duration2'])){$_POST['duration2'] = 0;}
    if(!isset($_POST['inactive'])){$_POST["inactive"] = 0;}
    if(!isset($_POST['ot_start_gap'])){$_POST['ot_start_gap'] = 0;}
    if(!isset($_POST['qualifying_hrs_for_ot'])){$_POST['qualifying_hrs_for_ot'] = 0;}
    if(!isset($_POST['table1_id'])){$_POST['table1_id'] = 0;}
    if(!isset($_POST['table2_id'])){$_POST['table2_id'] = 0;}
    if(!isset($_POST['no_ot'])){$_POST['no_ot'] = 0;}
    
$dec2 = 6;
if ($Mode =='Edit')
{
    $myrow = get_shift_details($selected_id);

    $_POST['id'] = $myrow["id"];
    $_POST['shift_name'] = $myrow["shift_name"];
    $_POST['day_start'] = sql2time($myrow["day_start"]);
    $_POST['start_time1'] = sql2time($myrow["start_time1"]);
    $_POST['duration1'] = sql2time($myrow["duration1"]);
    $_POST['split_shift'] = $myrow["split_shift"];;
    $_POST['start_time2'] = sql2time($myrow["start_time2"]);
    $_POST['duration2'] = sql2time($myrow["duration2"]);
    $_POST['ot_start_gap'] = sql2time($myrow["ot_start_gap"]);
    $_POST['qualifying_hrs_for_ot'] = sql2time($myrow["qualifying_hrs_for_ot"]);
    $_POST['table1_id'] = $myrow["table1_id"];
    $_POST['table2_id'] = $myrow["table2_id"];
    $_POST["inactive"] = $myrow["inactive"];
    $_POST['no_ot'] = $myrow["no_ot"];
    
    hidden('id', $_POST['id']); 
}

br();

hidden('selected_id', $selected_id);

div_start('price_details');
start_table(TABLESTYLE2);

text_row("Shift Name:", "shift_name", $_POST['shift_name'], 50,200);
time_row(_("Day Start (24hr):"), 'day_start', null, 10);
time_row(_("Start Time1 (24hr):"), 'start_time1', null, 10);
time_row(_("Duration1 (hrs):"),'duration1',  null, 10);

//yesno_list_row("Split Shift:", 'split_shift', $_POST['split_shift'], "", "", true );
if(get_post('split_shift') == 1){
    time_row(_("Start Time2 (24hr):"), 'start_time2', null, 10);
    time_row(_("Duration2 (hrs):"),'duration2',  null, 10);    
}

time_row(_("OT Start Gap (hrs):"),'ot_start_gap',  null, 10);
time_row(_("Qualifying Hours For OT (hrs):"),'qualifying_hrs_for_ot',  null, 10);
value_list_row("Normal OT Rate Table", "table1_id", "VALUE_TABLES");
value_list_row("Double OT Rate Table", "table2_id", "VALUE_TABLES");
yesno_list_row("No OT:", 'no_ot', $_POST['no_ot'], "Yes", "No" );
yesno_list_row("Inactive:", 'inactive', $_POST['inactive'] );

end_table(1);
div_end();

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

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