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

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/sales/includes/db/sales_types_db.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 = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}

include_once($path_to_root . "/includes/date_functions.inc");


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

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

simple_page_mode(true);
//---------------------------------------------------------------------------------------------------
$input_error = 0;

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

	br(2);
//----------------------------------------------------------------------------------------------------

function check_data()
{
    if (!is_date($_POST['holiday_date'])) 
    {
            display_error(_("The entered date is invalid."));
            set_focus('holiday_date');
            return false;
    }
    
    return true;
}


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

    if ($selected_id != -1) 
    {
            //editing an existing price
            update_holiday($selected_id, date2sql($_POST['holiday_date']),
                        $_POST['holiday_name'], $_POST['holiday_time_code'], $_POST['ot_type']);

            $msg = _("Holiday has been updated.");
    }
    else
    {

            add_holiday(date2sql($_POST['holiday_date']),
                        $_POST['holiday_name'], $_POST['holiday_time_code'], $_POST['ot_type']);

            $msg = _("New holiday has been added.");
    }
    display_notification($msg);
    $Mode = 'RESET';

}

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

if ($Mode == 'Delete')
{
	//the link to delete a holiday record was clicked
	delete_holiday($selected_id);
	display_notification(_("The selected holiday has been deleted."));
	$Mode = 'RESET';
}


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

$holidays_list = get_holidays();

div_start('price_table');
start_table(TABLESTYLE, "width=50%");

$th = array(_("Date"), _("Name"), _("Holiday Time"), _("Holiday Type"), "", "");
table_header($th);
$k = 0; //row colour counter
$calculated = false;
while ($myrow = db_fetch($holidays_list))
{

    alt_table_row_color($k);

    label_cell($myrow["holiday_date"]);
    label_cell($myrow["holiday_name"]);
    label_cell($myrow["holiday_time_name"]);

    $ot_type_name = "No OT type selected";
    $ot_type = $myrow["ot_type"];
    if($ot_type == 1)
    {
        $ot_type_name = "OT Type1";
    }
    elseif($ot_type == 2){
        $ot_type_name = "OT Type2";
    }
    
    label_cell($ot_type_name);
    
 	edit_button_cell("Edit".$myrow['id'], _("Edit"));
 	delete_button_cell("Delete".$myrow['id'], _("Delete"));
    end_row();

}
end_table();

div_end();
//------------------------------------------------------------------------------------------------

echo "<br>";

$_POST['additional_ot_percenatge'] = 0;

if ($Mode == 'Edit')
{
	$myrow = get_holiday_details($selected_id);
	$_POST['holiday_date'] = sql2date($myrow["holiday_date"]);
	$_POST['holiday_name'] = $myrow["holiday_name"];
    $_POST['holiday_time_code'] = $myrow["holiday_time_code"];
    $_POST['holiday_time_name'] = $myrow["holiday_time_name"];
    $_POST['ot_type'] = $myrow["ot_type"];
}
else{
    $_POST['ot_type'] = 1;
}

hidden('selected_id', $selected_id);

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




date_row('Holiday:', ('holiday_date'));

text_row(_("Holiday Name:"), 'holiday_name', null, 50, 100);
if (isset($_POST['holiday_time_code'])){
    value_list_code_row("Holiday Time:", 'holiday_time_code', "HOLIDAY_TIME", $_POST['holiday_time_code']);
}
else{
    value_list_code_row("Holiday Time:", 'holiday_time_code', "HOLIDAY_TIME");
}

two_value_list_row("Holiday Type:", 'ot_type', $_POST['ot_type'] , "OT Type2", "OT Type1", 2, 1);

end_table(1);


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

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