<?php

function dispaly_employee_attendance_details_controller() {
    div_start('emp_att_control');
        start_table(TABLESTYLE_NOBORDER);
            start_row();
                date_cells(_("From:"), 'from_date', '', null, -30);
                date_cells(_("To:"), 'to_date');
                emoplyees_list_cells(_("Employee:"), 'employee_code', null, "", false, check_value('show_inactive'));
                yesno_list_cells("OT Only", "ot_only", 0, "Yes", "No");
                value_list_code_cells("Status Code:", "status_code", "ATTENDANCE_STATUS", false, "");

                submit_cells('SearchEmpAtt', _("Search"),'',_('Search'), 'default');
            end_row();
        end_table(1);
    div_end();
}

function dispaly_employee_attendance_details_inquiry($from_date, $to_date, $employee_code, $ot_only, $status_code) {   
    global $Ajax;
    
    $Ajax->activate('emp_att_details');
    div_start('emp_att_details');
        start_table(TABLESTYLE, "width=80%");

            $th = array(_("Date"), _("Shift"), _("Shift Start Time (24h)"), _("Shift End Time (24h)"), _("In Time (24h)"), _("Out Time (24h)"), _("Deficit Time"));

            table_header($th);

                $k = $count = $previous_employee_id = $days = $present = $absent = 0;
                
//                $header_recs = get_employee_attendances_header(0, date2sql($from_date), date2sql($to_date));
//                    $days = db_num_rows($header_recs);

                $result = get_employee_attendance_details_inquiry($from_date, $to_date, $employee_code, $ot_only, $status_code);
                    while ($row = db_fetch($result)) {
                        $employee_code = $row["employee_code"];
                        
                        if ($previous_employee_id === 0) {
                            start_row('style=background-color:#abd5de');
                                label_cell(text_bold(get_employee_formatted_long_name($employee_code)), "align=center colspan=9");
                            end_row();
                        }
                        
                        if (! ($previous_employee_id === 0) && ! ($previous_employee_id == $employee_code)) {
                            $present = get_employee_attendances_count($previous_employee_id, $from_date, $to_date);
                            
                                start_row();
//                                    $m = "Working Days: ".$days.spaces(10)."Presents: ".$present.spaces(10)."Absents: ".($days - $present).spaces(10);
                                        label_cell(text_bold("Presents: ".$present), "align=center colspan=9");
                                end_row();
                                start_row();
                                    vertical_space("height=20px align=center colspan=9");
                                end_row();
                            
                            table_header($th);
                            start_row('style=background-color:#abd5de');
                                label_cell(text_bold(get_employee_formatted_long_name($employee_code)), "align=center colspan=9");
                            end_row();
                        } 
                        
                        if ($count <= 1000) {
                            alt_table_row_color($k); 
                                $date_s = strtotime($row["date"]);
                                $att_id = $row['id'];

                                $link = $date_s."~".$att_id."~".$employee_code;

                                $link_code = $date_s."~".$att_id."~".$employee_code;
                                $link = get_payroll_trans_view_str(ST_ATTENDANCE, $link_code, $row['date']);
                                    label_cell($link, 'align=center');

                                    label_cell($row['shift_name']);

                                    $selected_shift_start_time = substr($row['start_time1'], 0, 5);
                                    $selected_shift_duration = $row['duration1'];

                                        $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($row['in1'], 'align=center');
                                    label_cell($row['out1'], 'align=center');
                                    if ($row['calculated_deficit_time'] == "00:00:00") {
                                        label_cell('', 'align=center');
                                    } else {
                                        label_cell(substr($row['calculated_deficit_time'], 0, 5), 'align=center');
                                    }

                                $count++;
                                
                                $previous_employee_id = $employee_code;
                            end_row();
                        } else {
                            break;
                        }
                    }
                    
                    $present = get_employee_attendances_count($previous_employee_id, $from_date, $to_date);
                        start_row();
//                            $m = "Working Days: ".$days.spaces(10)."Presents: ".$present.spaces(10)."Absents: ".($days - $present).spaces(10);
                                label_cell(text_bold("Presents: ".$present), "align=center colspan=9");
                        end_row();

        end_table();


        if ($count > 1000) {
            display_warning("Maximum limt of 1000 records reached. Please change the filtering criteria to see other records.");
        }
    
    div_end();
}