<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_ATTENDANCE_VIEW';
$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/attendance_calculation.inc");
include_once($path_to_root . "/payroll/includes/db/attendance_db.inc");
include_once($path_to_root . "/payroll/includes/db/employees_db.inc");
include_once($path_to_root . "/payroll/includes/db/shifts_db.inc");
include_once($path_to_root . "/payroll/includes/leave_calculation.inc");
include_once($path_to_root . "/payroll/includes/db/leaves_db.inc");
include_once($path_to_root . "/includes/db/module_settings_db.inc");

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
page(_($help_context = "View Attendance Entry"), true, false, "", $js);

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

if ($_GET['trans_no'] != "")
{
	$tran_no = $_GET['trans_no'];
}

function display_row($name, $value)
{
    start_row();        
    label_cells($name, $value, "width=200px class='tableheader2'", "width=200px");
    end_row();
    
}

function fill_initial_values($cal, &$cal, &$summary)
{
        $summary = "";
        $summary['last_leave_date'] = "";
        $summary['rec_count'] = 0;
        $summary['hour_count'] = 0;
        
        $summary['total_entered_hour_count'] = 0;
        $summary['total_counted_duration_s'] = 0;
        
        $summary['total_ot_hours_calculated_s'] = 0;
        $summary['total_ot_hours_granted_s'] = 0;
        
        $summary['total_deficit_hours_calculated_s'] = 0;
        $summary['total_deficit_hours_deducted_s'] = 0;

//        $cal['id'] = $myrow["id"];
//        $cal['date'] = sql2date($myrow["date"]);
//        $cal['work_time_name'] = $myrow["work_time_name"];
//        
//        $cal['ot_rounddown_value'] = get_module_parameter_value("payroll", "ot_rounddown_value");
//        $cal['deficit_roundup_value'] = get_module_parameter_value("payroll", "deficit_roundup_value");
}

function get_values($tran_no, &$date, &$att_id, &$employee_code)
{
    $separater   = '~';
    
    $pos = strpos($tran_no, $separater);
    $date_s = substr($tran_no, 0, $pos);
    $date = date('Y-m-d', $date_s);
    
    $tran_no = substr($tran_no, $pos + 1);
    $pos = strpos($tran_no, $separater);
    $att_id = substr($tran_no, 0, $pos);
    
    $employee_code = substr($tran_no, $pos + 1);
    
}

function show_attendance_record($employee_code, $date, &$cal, &$summary)
{

        //$myrow = get_view_attendance_record($att_id);
    
        calculate_viewing_record($employee_code, $date, $cal);

        if(!isset($cal['aid']) || $cal['aid'] == 0)
        {
            return;
        }

//        $cal = "";
//        $summary = "";
//        $summary = "";
//        $last_header_date ="";
//
//        $k = $j = 0; //row colour counter
//        $summary['last_leave_date'] = "";
//        $summary['rec_count'] = 0;
//        $summary['total_entered_hour_count'] = 0;
//        $summary['total_counted_duration_s'] = 0;
//        $summary['total_ot_hours_granted_s'] = 0;

        
        
        fill_initial_values($cal, $cal, $summary);
        //calculate_common_record($employee_code, $myrow, $cal, $summary);

        display_heading2("Attendance Entry for ".$cal['date']);

        br(1);
        
        start_outer_table(TABLESTYLE2,"width=80%");
        table_section(1,'50%');
        table_section_title("Work Details");
        display_row("Date:",$cal['date'].", ".date('l',strtotime($cal['date'])));
        display_row("Work Time:",$cal['work_time_name']);
        display_row("From Time:",substr($cal['from_date_time1'],0,16));
        display_row("To Time:",substr($cal['to_date_time1'],0,16));
        display_row("Out Of Office Time:",substr($cal['out_of_office_time1'],0,5));

        display_row("Total Entered Duration:",$cal['total_entered_duration']);

        //table_section(2,'30%');
        table_section_title("Shift Details");
        display_row("Shift Name:",$cal['shift_name']);
        display_row("Shift Start Time1:",substr($cal['shift_start_time1'],0,5));
        display_row("Shift Duration1:",substr($cal['shift_duration1'],0,5));

        if($cal['split_shift'] == 1)
            $split_shift = "Yes";
        else{
            $split_shift = "No";
        }

        $shift_start_date_time1 = date_time_add($date, $cal["shift_start_time1"]);
        $shift_start_date_time1_s = strtotime($shift_start_date_time1);
        
        $shift_start_time1_s = strtotime("1970-01-01 ".$cal['shift_start_time1']." UTC");
        $shift_duration1_s = strtotime("1970-01-01 ".$cal['shift_duration1']." UTC");
        $shift_end_time1_s = $shift_start_time1_s + $shift_duration1_s;

        $shift_end_time1 = gmdate("H:i:s", $shift_end_time1_s);
        $cal['shift_end_time'] = substr($shift_end_time1, -8, 5);
        
        display_row("Shift End Time:",$cal['shift_end_time']);
        display_row("OT Start Gap:",$cal['ot_start_gap']);
        display_row("Qualifying Hours for OT:",$cal['qualifying_hrs_for_ot']);
        
        $no_ot = "No";
        if($cal['no_ot'] == 1){
            $no_ot = "Yes";
        }
        display_row("No OT:",$no_ot);

        table_section(2,'50%');
        table_section_title("Calculation Details");
        display_row("Late Time:",$cal['late_time1']);

        //display_row("Total Late Time:",$cal['day_late_time']);
        display_row("Short Leave:",$cal['short_leave1']);
        display_row("Total Nonworking Time:", $cal['non_working_time']);
        //display_row("Total Short Leave:",$cal['day_short_leave']);
        display_row("Worked Time Within Shift:",$cal['worked_time_within_shift']);
        
        $leave_hours = "00:00";
        if(strlen($cal['leave_hours']) > 2){
            $leave_hours = $cal['leave_hours'];
        }
        display_row("Leave Hours:", $leave_hours);
        display_row("Counted Duration:",$cal['counted_duration1']);
        //display_row("Total Counted Duration:",$cal['day_counted_duration']);
        display_row("Applicable OT Start Gap:",$cal['applicable_ot_start_gap']);
        display_row("Applicable Shift Duration:",$cal['applicable_shift_duration']);
        display_row("Calculated Deficit Time:",$cal['calculated_deficit_time']);
        display_row("Deducted Deficit Time:",$cal['deducted_deficit_time']);
        display_row("Deficit Status:",$cal['deficit_status_name']);
        display_row("Calculated OT Hours:",$cal['calculated_ot_time']);
        display_row("Granted OT Hours:",substr($cal['granted_ot_time'],0,5));
        display_row("Payroll OT Hours:",substr($cal['payroll_ot_time'],0,5));
        display_row("OT Status:",$cal['ot_status_name']);


        end_outer_table();
}

function show_leave_record($date, $employee_code)
{
    br(2);
    
    $result = get_leave_details($employee_code,$date);
    $myrow = db_fetch($result);
    
    if($myrow){
        
        display_heading2("Leave Entry for ".sql2date($date));
        
        br(1);
        
        start_table(TABLESTYLE2,"width=40%");
        display_row("Leave Type:",$myrow['leave_type_name']);
        display_row("Leave time:",$myrow['leave_time_name']);
        display_row("Leave Status:",$myrow['leave_status_name']);
        end_outer_table();
    }
    else{
        display_heading2("No leave record found for ".sql2date($date));
    }


}




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


$date = "";
$employee_code = "";
$att_id = 0;
$cal = "";
$summary = "";

get_values($tran_no, $date, $att_id, $employee_code);

$emp = get_employee($employee_code);
if($emp){
    $emp_name = $emp['employee_code']." - ".$emp['first_name']." ".$emp['middle_name']." ".$emp['last_name'];
}
else{
    $emp_name = "Could not find employee [".$employee_code."]";
}

br(2);

display_heading($emp_name);

br(1);

if($att_id > 0){
    show_attendance_record($employee_code, $date, $cal, $summary);
}
else{
    display_heading2(_("No attendance record found for ".sql2date($date)));
}

br(1);

show_leave_record($date, $employee_code);

br(1);


end_page(true, false, false, ST_POINTSUPDATE, $tran_no);

?>
