<?php
/*=======================================================\
|                 Point of Sales Module                  |
|--------------------------------------------------------|
|   Creator: Phương                                      |
|   Date :   18-Dec-2017                                 |
|   Description: ERP Point of Sales Extension            |
|   Website: http://notrinos.com                         |
|                                                        |
\=======================================================*/

$page_security = 'SA_SALESMANAGER';
$path_to_root  = '../../..';

include_once($path_to_root . '/includes/session.inc');
add_access_extensions();

include_once($path_to_root . '/includes/ui.inc');
include_once($path_to_root . '/modules/pos/includes/pos_ui.inc');
include_once($path_to_root . '/modules/pos/includes/pos_db.inc');

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

page(_($help_context = 'Shifts Management'));
simple_page_mode(true);

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

	$start = $_POST['start_hour'].':'.$_POST['start_minute'];
	$end = $_POST['end_hour'].':'.$_POST['end_minute'];

	if(empty(trim($_POST['shift_name']))) {
		display_error( _('Name field cannot be empty.'));
		set_focus('shift_name');
	}
	elseif ((strtotime($start) - strtotime($end)) >= 0) {
		display_error(_('End time must be after start time.'));
		set_focus('start_hour');
	}
	elseif(check_duplicated_shift($selected_id, $start, $end)) {
		display_error(_('Duplicated time found.'));
		set_focus('shift_name');
	}
	else {
		$id = $selected_id == -1 ? false : $selected_id;

		write_pos_shift($id, $_POST['shift_name'], $_POST['start_hour'].':'.$_POST['start_minute'], $_POST['end_hour'].':'.$_POST['end_minute']);
		
    	if ($selected_id != -1)
			display_notification(_('Selected shift has been updated'));
    	else
			display_notification(_('New shift has been added'));
		
		$Mode = 'RESET';
	}
}

if ($Mode == 'Delete') {

	if(pos_shift_used($selected_id))
		display_error( _('This shift cannot be deleted.'));
	else {
		delete_pos_shift($selected_id);
		display_notification(_('Selected shift has been deleted'));
	}
	$Mode = 'RESET';
}

if($Mode == 'RESET') {
	$selected_id = -1;
	$_POST['shift_name'] = '';
	$_POST['start_hour'] = '';
	$_POST['end_hour'] = '';
	$_POST['start_minute'] = '';
	$_POST['end_minute'] = '';
}

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

start_form();

start_table(TABLESTYLE);
$th = array(_('Id'), _('Name'), _('From'), _('To'), '', '');
inactive_control_column($th);
table_header($th);

$result = get_pos_shifts(false, check_value('show_inactive'));
$k = 0;
while ($myrow = db_fetch($result)) {
	alt_table_row_color($k);
	label_cell($myrow['id']);
	label_cell($myrow['shift_name']);
	label_cell($myrow['start_hour'].':'.$myrow['start_minute']);
	label_cell($myrow['end_hour'].':'.$myrow['end_minute']);
	inactive_control_cell($myrow['id'], $myrow['inactive'], 'pos_shift', 'id');
	edit_button_cell('Edit'.$myrow['id'], _('Edit'));
	delete_button_cell('Delete'.$myrow['id'], _('Delete'));
	end_row();
}
inactive_control_row($th);
end_table(1);

start_table(TABLESTYLE2);

if($selected_id != -1) {
	
 	if ($Mode == 'Edit') {
		
		$myrow = get_pos_shifts($selected_id);

		$_POST['shift_name']  = $myrow['shift_name'];
		$_POST['start_hour']  = (int)$myrow['start_hour'];
		$_POST['start_minute']  = (int)$myrow['start_minute'];
		$_POST['end_hour']  = (int)$myrow['end_hour'];
		$_POST['end_minute']  = (int)$myrow['end_minute'];
		hidden('selected_id', $selected_id);
		label_row(_('ID:'), $myrow['id']);
 	}
}

text_row_ex(_('Shift Name:'), 'shift_name', 37, 50);
number_list_row(_('Start Hour:'), 'start_hour', null, 0, 23);
number_list_row(_('Start Minute:'), 'start_minute', 0, 0, 59);
number_list_row(_('End Hour:'), 'end_hour', null, 0, 23);
number_list_row(_('End Minute:'), 'end_minute', 0, 0, 59);

end_table(1);

submit_add_or_update_center($selected_id == -1, '', 'both');
// var_dump($_SESSION['wa_current_user']);
end_form();
end_page();