<?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/data_checks.inc");
include_once($path_to_root . "/payroll/includes/payroll_db.inc");

$js = '';
$js .= get_js_date_picker();
$js .= get_js_open_window(900, 500);

page(_($help_context = "Shifts"));

simple_page_mode(true);


if (list_updated('shift_list_value_id')) {
    $selected_id = -1;
}

function clear_shits_data() {
    unset($_POST['id']);
    unset($_POST['from_date']);
    unset($_POST['day_start']); 
    unset($_POST['start_time1']); 
    unset($_POST['duration1']);
    unset($_POST['split_shift']);
    unset($_POST['start_time2']);
    unset($_POST['duration2']);
    unset($_POST['ot_start_gap']);
    unset($_POST['qualifying_hrs_for_ot']);
    unset($_POST['table1_id']);
    unset($_POST['table2_id']);
    unset($_POST["inactive"]);
    unset($_POST['no_ot']); 
}

function check_data($selected_id) {	
    if (! ($_POST['shift_list_value_id'] > 0)) {
        display_error( _("Please select a shift."));
        set_focus('shift_list_value_id');
        return false;
    }
    
    if (! (is_date($_POST['from_date']))) {
        display_error( _("From date is not a valid date."));
        set_focus('from_date');
        return false;
    }
    
    if(!($selected_id > 0)){
        $id = check_has_employee_attendance_after_date(date2sql($_POST['from_date']));
        if ($id > 0) {
            $old_attendance_msg = _("There are employee attendances entered on or after the selected from date. From date should be after the last employee attendance date.");
//            display_error($old_attendance_msg);
            display_warning($old_attendance_msg);
            set_focus('from_date');
//            return false;
        }
    }
    
    $row = get_shifts_by_shift_list_value_id_date($_POST['shift_list_value_id'], $_POST['from_date'], $selected_id);
    if (is_array($row)) {
        display_error( _("There is another shift starting on the same date. Record #".$row['id']));
        set_focus('from_date');
        return false;
    }
        
    $n = check_shift_in_employee_attendance($selected_id, date2sql($_POST['from_date']));
    if ($n > 0) {
//        display_error( _("Selected record has been used in employee attendance. It can not be edited."));
//        set_focus('from_date');
//        return false;
    }
    
    if (! is_time($_POST['start_time1'])) {
        display_error( _('Start time is not a valid time value'));
        set_focus('start_time');
        return false;
    }

    if (! is_time($_POST['duration1'])) {
        display_error( _('Duration is not a valid time duration'));
        set_focus('duration');
        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;
}

function can_delete_shift($selected_id) {	

    $shift = get_shift_details($selected_id);    
    $n = check_shift_in_employee_attendance($selected_id, $shift['from_date']);
    if ($n > 0) {
        display_error( _("Selected record has been used in employee attendance. It can not be edited."));
        set_focus('from_date');
        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_list_value_id'], $_POST['from_date'], $_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['from_date'], $_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') {
    if(can_delete_shift($selected_id)){
        delete_shift($selected_id);
        display_notification(_("Selected record has been deleted."));
        $Mode = 'RESET';
    }
}

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

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

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

if (!@$_GET['popup'])
    start_form();
    
    start_table(TABLESTYLE_NOBORDER);
        value_list_row('Shift:', 'shift_list_value_id', 'EMPLOYEE_SHIFTS', null, 'Select a shift', true);
    end_table(1);

        div_start('price_table');
            start_table(TABLESTYLE, "width=95%");
                $th = array(_("Rec Id"), _("From Date"), _("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);
                
                $result = get_shift_records($_POST['shift_list_value_id']);
                    if (db_num_rows($result) > 0) {

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

                        while ($myrow = db_fetch($result)) {
                            alt_table_row_color($k);
                                label_cell($myrow["id"], "align=center");
                                label_cell(sql2date($myrow["from_date"]), "align=center");
                                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(1);
        div_end();
            
            
        } else {
            end_table(1);
                display_note(_("There are no shifts defined."));
                div_end();
        }
    
    $Ajax->activate('price_details');

//-----------------------------------------------------------------------------------------------
    if(! isset($_POST['id'])){$_POST['id'] = 0;}
    if(! isset($_POST['shift_name'])){$_POST['shift_name'] = "";}
    hidden('shift_name', '');
    
    if(! isset($_POST['day_start'])){$_POST['day_start'] = '00:00';}
    if(! isset($_POST['start_time1'])){$_POST['start_time1'] = '08:00';}
    if(! isset($_POST['duration1'])){$_POST['duration1'] = '00:00';}
    
    if(! isset($_POST['split_shift'])){$_POST['split_shift'] = 0;}
    
    if(! isset($_POST['start_time2'])){$_POST['start_time2'] = '00:00';}
    if(! isset($_POST['duration2'])){$_POST['duration2'] = '00:00';}
    if(! isset($_POST['inactive'])){$_POST["inactive"] = 0;}
    if(! isset($_POST['ot_start_gap'])){$_POST['ot_start_gap'] = '00:00';}
    if(! isset($_POST['qualifying_hrs_for_ot'])){$_POST['qualifying_hrs_for_ot'] = '00:00';}
    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'] = '00:00';}
    
$dec2 = 6;
if ($Mode =='Edit') {
    $myrow = get_shift_details($selected_id);

    $_POST['id'] = $myrow["id"];
    $_POST['from_date'] = sql2date($myrow["from_date"]);
    $_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(2);

hidden('selected_id', $selected_id);

div_start('price_details');
    start_table(TABLESTYLE2);
        if ($Mode == 'Edit' && $selected_id > 0) {
            label_row('Id:', $selected_id);
        }
        
        if ($selected_id > 0) {
            label_row('From Date:', $_POST['from_date']);
            hidden('from_date', $_POST['from_date']);
        }else{
            date_row('From Date:', 'from_date');        
        }
        
        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);
        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');
br(2);

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

?>