<?php 

function display_employees_attendance_details($date, $status_array=array(), $check_result_msg=null) {   
    global $Ajax, $payroll_approval_statuses;
    
    $date_ = sql2date($date);

    $result = view_employee_attendances_details($date, $status_array);
        if (! (db_num_rows($result) > 0) && $check_result_msg !== null) {
            display_note($check_result_msg, 2, 2, "style=color:red");
            return false;
        }
        
    br(1);
    
    display_heading("Employee Attendance On ".date("l, d/m/Y", strtotime($date)));
    
    br(1);
        
    $Ajax->activate('emp_attendances');
    div_start('emp_attendances');
        start_table(TABLESTYLE, "width=99%");
        
            if (count($status_array) > 0) {
                $th = array(_("Rec Id"), _("Employee"), _("Shift"), _("Shift Start Time (24h)"), _("Shift End Time (24h)"), _("In Time (24h)"), 
                            _("Out Time (24h)"), _('Deficit Time'), _('Comment'), _('Deficit Status'), _('OT Status'));
            } else {
                $th = array(_("Rec Id"), _("Employee"), _("Shift"), _("Shift Start Time (24h)"), _("Shift End Time (24h)"), _("In Time (24h)"), 
                            _("Out Time (24h)"), _('Deficit Time'), _('Comment'));
            }

                table_header($th);
                
                $def_in_time = get_module_parameter_value('payroll', 'from_time1');
                $def_out_time = get_module_parameter_value('payroll', 'to_time1');
                

                    $k = $j = 0; //row colour counter
                    while ($row = db_fetch($result)) {
                        $emp_code = $row['employee_code'];
                        
                        alt_table_row_color($k);

                            if ($row['id'] > 0) {
                                $date_s = strtotime($date);
                                $att_id = $row['id'];
                                $link_code = $date_s."~".$att_id."~".$emp_code;
                                $link = get_payroll_trans_view_str(ST_ATTENDANCE, $link_code, $row['id']);
                            } else {
                                $link = '';
                            }

                            label_cell($link, 'align=center');
                            label_cell(get_employee_formatted_long_name($emp_code));

                            label_cell($row['shift_name'], 'align=center');

                            $shift_res = get_defined_shifts_by_shift_id_and_date($row['shift_id'], sql2date($date));
                                $selected_shift = db_fetch($shift_res);

                                    if (is_array($selected_shift)) {
                                        $selected_shift_start_time = substr($selected_shift['start_time1'], 0, 5);
                                        $selected_shift_duration = $selected_shift['duration1'];
                                    } else {
                                        $time_dura = time_diff_seconds($date_, $def_in_time, $date_, $def_out_time);
                                        $selected_shift_start_time = $def_in_time;
                                        $selected_shift_duration = gmdate("H:i:s", $time_dura);
                                    }

                                $selected_shift_start_time_dt = "1970-01-01 ".$selected_shift_start_time.":00";
                                $selected_shift_start_time_dt_t = strtotime($selected_shift_start_time_dt);
                                $selected_shift_end_time_dt_t = strtotime('+'.substr($selected_shift_duration,0,2).' hours', $selected_shift_start_time_dt_t);
                                $selected_shift_end_time_dt_t = strtotime('+'.substr($selected_shift_duration,3,2).' minutes', $selected_shift_end_time_dt_t);

                                $selected_shift_end_time = date("H:i", $selected_shift_end_time_dt_t);

                            label_cell($selected_shift_start_time, 'align=center');
                            label_cell($selected_shift_end_time, 'align=center');
                            label_cell(substr($row['from_date_time1'], 11, 5), 'align=center');
                            label_cell(substr($row['to_date_time1'], 11, 5), 'align=center');

                            if (substr($row['calculated_deficit_time'], 0, 5) == "00:00") {
                                $deficit_time_text = "";
                            } else {
                                $deficit_time_text = substr($row['calculated_deficit_time'], 0, 5);
                            }
                            label_cell($deficit_time_text, 'align=center');
                            label_cell(nl2br($row['comment']));
                            
                            if (count($status_array) > 0) {
                                $def_row = get_work_time_deficit_records_by_filters(0, $date, 0, $emp_code, true);
                                if (is_array($def_row)) {
                                    if (in_array($def_row['status_code'], array('A', 'L', 'C'))) {
                                        label_cell($payroll_approval_statuses[$def_row['status_code']], 'align=center style=color:red');
                                    } else{
                                        label_cell($payroll_approval_statuses[$def_row['status_code']], 'align=center');
                                    }
                                } else {
                                    label_cell('');
                                }
                                if ($row['ot_status_code'] !== null && ! in_array($row['ot_status_code'], array('N', 'U'))) {
                                    label_cell($payroll_approval_statuses[$row['ot_status_code']], 'align=center style=color:red');
                                } else {
                                    label_cell('');
                                }
                            }

                        end_row();
                    }
        end_table(2);
    div_end();
    
    return true;
}

?>