<?php 

function display_unapproved_attendances_summary($period_start_date, $period_end_date) {   
    global $Ajax, $payroll_approval_statuses;
    
    $Ajax->activate('unapprove_att_sum');
    div_start('unapprove_att_sum');
        start_table(TABLESTYLE, "width=50%");

            $th = array(_("Date"), _("Total Employees"), _("Marked"), _("Not Marked"), _("Approval Status"), _(""));
            table_header($th);

                $k = 0;
                $result = get_attendance_summary_records_by_filters(0, null, array(), false, $period_start_date, $period_end_date);
                    while ($row = db_fetch($result)) {
                        alt_table_row_color($k); 
                            label_cell(get_payroll_trans_view_str(ST_EMPLOYEE_ATTENDANCE, $row['date'], sql2date($row['date'])), 'align=center');
                            label_cell($row['total_employees'], 'align=center');
                            label_cell(get_payroll_trans_view_str(ST_EMPLOYEE_ATTENDANCE, $row['date'], $row['presents']), 'align=center');                            
                            $absents = $row['total_employees'] - $row['presents'];
                            label_cell($absents, 'align=center');
                            label_cell($payroll_approval_statuses[$row['attendance_status_code']], 'align=center');
                            
                            if (in_array($row['attendance_status_code'], array('N', 'U'))) {
                                check_cells('', 'selected_'.$row["id"], 0, false, false, 'align=center');
                            } else {
                                label_cell('', 'align=center');
                            }
                        end_row();
                    }
                    
        end_table(2);
    div_end();
}

function can_approve_attendances_summary($payroll_run_id) {
    
    if (! ($payroll_run_id > 0)) {
        return array('success' => false, 'message' => "Please select a payroll run.");
    }
    
    $selected_recs = 0;
    $status_array = array("'S'", "'P'", "'R'", "'T'", "'Y'", "'D'");
    
    $result = get_attendance_summary_records_by_filters(0, null, array("'N'", "'U'"));
        while ($row = db_fetch($result)) {
            $id = $row["id"];
            $date = $row["date"];
            
            if ((isset($_POST["selected_$id"])) && ($_POST["selected_$id"] == true)) {
                if (! in_array($row['attendance_status_code'], array('N', 'U'))) {
                    return array('success' => false, 'message' => "Approval status of Attendances Summary on $date is changed by another user.");
                }
    
                $pay_row = get_payroll_runs_records_by_filters(0, 0, $date, $status_array, true);
                    if (is_array($pay_row)) {
                        $payroll_name = get_listvalue_name_by_id($pay_row['payroll_id']);
                        $des = $pay_row['description'];
                        $start = $pay_row['period_start_date'];
                        $end = $pay_row['period_end_date'];
                        $payname = "$payroll_name ($start - $end - $des)";
                        
                        return array('success' => false, 
                                     'message' => "Could not approve Attendances Summary on $date since payroll $payname is already run.");
                    }

                $selected_recs++;
            }
        }
        
        if ($selected_recs == 0) {
            return array('success' => false, 'message' => 'Please select at least one record before approving.');
        }
    
        return array('success' => true, 'message' => '');
}

function approve_unapproved_attendances_summary($payroll_run_id) {
    $msg = can_approve_attendances_summary($payroll_run_id);
        if ($msg['success'] === false) {
            return $msg;
        }
        
        $pay_run_row = get_payroll_run_details($_POST['payroll_run_id']);
        $period_start_date = $pay_run_row['period_start_date'];
        $period_end_date = $pay_run_row['period_end_date'];
        
        $approved_recs = 0;
    
        $result = get_attendance_summary_records_by_filters(0, null, array("'N'", "'U'"), false, $period_start_date, $period_end_date);
            while ($row = db_fetch($result)) {
                $id = $row["id"];
                $date = $row["date"];
                
                if ((isset($_POST["selected_$id"])) && ($_POST["selected_$id"] == true)) {
                    
                    begin_transaction();
                    
                    $af_row = update_employee_attendances_header_approvel_status($id, 'A', array("'N'", "'U'"));
                        if ($af_row != 1) {
                            cancel_transaction();
                            
                            return array('success' => false, 'message' => "Could not update Attendances Summary approval status on $date.");
                        }
                        
                    commit_transaction();
                    
                    $approved_recs++;
                }
            }

            if ($approved_recs > 0) {
                return array('success' => true, 'message' => "$approved_recs Attendance Summary records have been approved.");
            } else {
                return array('success' => false, 'message' => "Could not approve any Attendances Summary record.");
            }
}

function can_reopen_attendances_summary($date) {
    
    // check already approved work time deficit records belong any employee for selected payroll from to dates
    $def_row = get_payroll_worktime_deficit_summary_records_with_payroll_run($date, 0, array("'N'", "'U'"), true, true);
        if (is_array($def_row)) {
            $emp_name = get_employee_formatted_long_name($def_row['employee_code']);
            return array('success' => false, 
                         'message' => "$emp_name - has Approved Employee Work Time Deficit Summary records. Plase reopen those records first.");
        }
    
    // check already approved OT records belong any employee for selected date
    $ot_row = get_ot_records_by_filters(0, $date, $date, null, array("'N'", "'U'"), true, true);
        if (is_array($ot_row)) {
            $emp_name = get_employee_formatted_long_name($ot_row['employee_code']);
            return array('success' => false, 
                         'message' =>"$emp_name - has Approved OT on ".sql2date($date)." Plase reopen those records first.");
        }
    
    $status_array = array("'S'", "'P'", "'R'", "'T'", "'Y'", "'D'");
    
    $rec = get_attendance_summary_records_by_filters(0, $date, array(), true);
        if (! is_array($rec)) {
            return array('success' => false, 'message' => "Attendances Summary on $date is deleted by another user.");
        } else {
            if (! in_array($rec['attendance_status_code'], array('A'))) {
                return array('success' => false, 'message' => "Approval status of Attendances Summary on $date is changed by another user.");
            }
        }
            
    $pay_row = get_payroll_runs_records_by_filters(0, 0, $date, $status_array, true);
        if (is_array($pay_row)) {
            $payroll_name = get_listvalue_name_by_id($pay_row['payroll_id']);
            $des = $pay_row['description'];
            $start = $pay_row['period_start_date'];
            $end = $pay_row['period_end_date'];
            $payname = "$payroll_name ($start - $end - $des)";
            
            return array('success' => false, 'message' => "Could not reopen Attendances Summary on $date since payroll $payname is already run.");
        }

        return array('success' => true, 'message' => '');
}

function reopen_approved_attendances_summary($date) {
    $msg = can_reopen_attendances_summary($date);
        if ($msg['success'] === false) {
            return $msg;
        }
        
        $rec = get_attendance_summary_records_by_filters(0, $date, array(), true);
        
        begin_transaction();

            $result = get_payroll_runs_records_by_filters(0, 0, $date);
                while ($row = db_fetch($result)) {
                    $msg = check_and_delete_payroll_run_details($row['id']);
                        if ($msg['success'] === false) {
                            cancel_transaction();
                            
                            return $msg;
                        }
                }
                
            $af_row_2 = update_employee_attendances_header_approvel_status($rec['id'], 'U', array("'A'"));
                if ($af_row_2 != 1) {
                    cancel_transaction();

                    return array('success' => false, 'message' => "Could not reopen Attendances Summary on $date.");
                }

        commit_transaction();

            return array('success' => true, 'message' => "Attendance Summary on $date has been reopened.");
}

?>