<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_CALENDARS';
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");
include_once($path_to_root . "/admin/db/fiscalyears_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();
}


if (!@$_GET['popup'])
	page(_($help_context = "Work Time Calendar"));

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


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

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


if(isset($_POST['generate']))
{ 
    $error_no = 0;
    
    $fs_row = get_fiscalyear($_POST['fiscal_year']);

    $_POST['start_date'] = $fs_row['begin'];
    $_POST['end_date'] = $fs_row['end'];
    
    {
        $current_date = $_POST['start_date'];
        
        $result = check_processed_dates($_POST['start_date'],$_POST['end_date']);
        if ($result == true)
        {
            $error_no = 1;
            display_error( _("There are processed dates. Not possible to regenerate the calendar"));
            set_focus('fiscal_year');
        }
        
        if($error_no == 0)
        {
            $result = delete_work_time_calendar($_POST['start_date'],$_POST['end_date']);
            if ($result == false)
            {
                display_error( _("Could not delete calendar"));
                return;
            }
        

            while($current_date <= $_POST['end_date'])
            {
                $work_time_code = get_work_time_code($current_date);

                $rec_id = add_work_time_calendar_date($current_date,$work_time_code,0); 

                $current_date = sql2date($current_date);
                $current_date = add_days($current_date,+1);
                $current_date = date2sql($current_date);

                //return;
            }
        }
    }
    //$Mode = 'RESET';

}


if(isset($_POST['update_date']))
{ 
        $error_no = 0;
        
        $cd_row = get_calendar_date($selected_id);
        if ($cd_row['closed'] == 1)
        {
            $error_no = 1;
            display_error( _("selected date is already processed. Changes not allowed."));
            set_focus('work_time_code');
        }
        
        if($error_no == 0)
        {
            $result = update_work_time_calendar_date($selected_id,$_POST['work_time_code'],
                        $_POST['comment']);
            
             $Ajax->activate('controls');
            if($result)
                display_notification(_("Calendar date updated."));
        }
}

if (get_post('cancel')) 
{
    $selected_id = -1;
    $_POST['selected_id'];
    clear_data();
    set_focus('work_time_code');
    $Ajax->activate('controls');
}
//--------------------------------------------------------------------------------------------------


function clear_data()
{
    unset($_POST['date']);
    unset($_POST['work_time_code']);
    unset($_POST['comment']);
}

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

function get_work_time_code($date)
{
    $y = date("Y", strtotime($date));
    $m = date("m", strtotime($date));
    $d = date("d", strtotime($date));

    $day_name = strtolower(date("l", mktime(0,0,0,$m,$d,$y)));
    
    $working_time = "";
    $wt_row = get_working_time($day_name);
    
    if(!$wt_row)
    {
        display_error( _("working week is not defined."));
    }

    $work_time_code = $wt_row[$day_name];
    return $work_time_code;

}

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


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

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

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

if(isset($_POST['fiscal_year']))
    $fiscal_year = $_POST['fiscal_year'];
else {
$fiscal_year = null;    
}

start_table(TABLESTYLE);

fiscalyears_list_row("Select Fiscal Year:",'fiscal_year', $fiscal_year, true);
end_table(1); 


submit_center('generate', 'Generate Calendar', true, false);

br();



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

function display_calendar()
{
    $fs_row = get_fiscalyear($_POST['fiscal_year']);

    $_POST['start_date'] = $fs_row['begin'];
    $_POST['end_date'] = $fs_row['end'];

    $yd_result = get_year_dates($_POST['start_date'],$_POST['end_date']);
    
    div_start('Entitlements');
    
    if (db_num_rows($yd_result) == 0)
    {
    	display_note(_("No work calendar is defined for the selected fiscal year"));
    }
    else
    {
        display_heading("Work Time Calendar");

        start_table(TABLESTYLE, "width=65%");

            $th = array(_("Day No"), _("Week No"),
                    _("Date"), _("Work Time"),
                    _("Comment"), _("Closed"), "");

        table_header($th);

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

        while ($yd_row = db_fetch($yd_result))
        {
			alt_table_row_color($k);

            //label_cell($yd_row["id"]);
            
            $date = $yd_row["date"];
            $y = date("Y", strtotime($date));
            $m = date("m", strtotime($date));
            $d = date("d", strtotime($date));

            $day_no = strtolower(date("z", mktime(0,0,0,$m,$d,$y))+1);
            label_cell($day_no);
            
            $week_no = strtolower(date("W", mktime(0,0,0,$m,$d,$y)));
            
            label_cell($week_no);
            label_cell($date);
            //label_cell($yd_row["work_time_code"]);
            label_cell($yd_row["work_time_name"]);
            label_cell($yd_row["comment"]);
            
            if($yd_row["closed"] == 1)
            label_cell("Closed");
            else {
                label_cell("");
            }
            
            edit_button_cell("Edit".$yd_row['id'], _("Edit"));
            
            end_row();

            $day_name = strtolower(date("l", mktime(0,0,0,$m,$d,$y)));
            
            If ($day_name == "sunday")
            {
        		table_header($th);
            } //end of page full new headings
        } //end of while loop

        end_table();
    }
    div_end();
}

//echo "<br/>";
//-----------------------------------------------------------------------------------------------




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

hidden('selected_id', $selected_id);

div_start('controls');

$d_row = get_calendar_date($selected_id);
if(isset($d_row['id']) && $d_row['id'] > 0){
    $_POST['date'] = $d_row['date'];
    $_POST['work_time_code'] = $d_row['work_time_code'];
    $_POST['date'] = $d_row['date'];
    $_POST['comment'] = $d_row['comment'];
}

    if(!isset($_POST['date']))
        $_POST['date'] = "";

    if(!isset($_POST['work_time_code']))
        $_POST['work_time_code'] = "";

    if(!isset($_POST['date']))
        $_POST['date'] = "";

    if(!isset($_POST['comment']))
        $_POST['comment'] = "";


start_table(TABLESTYLE2, "width=40%");

label_row("Date:",$_POST['date']);
value_list_code_row("Leave Time:", 'work_time_code', "WORKING_TIME", $_POST['work_time_code']);
textarea_row(_("Comment:"), 'comment', null, 20, 3);

end_table(1);


//submit_add_or_update_center($selected_id == -1, '', 'both');
//submit_center('update_date', 'Update Date', true, false);
if($selected_id > 0)
{
    submit_center_first('update_date', _("Update Date"), '','default');
    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
}

br();

display_calendar();

div_end();
br();



br();

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