<?php

function dashboard_view_approvals($user_id, $trans_type, $from_date, $to_date) {
    global $systypes_array, $document_approval_statuses;
    
    $user = get_user($user_id);
    display_heading("Approver : ".$user['real_name']);    br();
    
    start_table(TABLESTYLE, "width=70%");

        $th = array("Type", "Tran. No", "Approval Status", "Previous Approver", "This Approval", "Previous Approval", "Time Difference"); 
        table_header($th);

            $k = 9;
            $count = $avg_t = 0; 
            $result = dashboard_get_user_approvals_period_details($from_date, $to_date, $user_id, $trans_type);
                while($row = db_fetch($result)) {
                    alt_table_row_color($k);

                        label_cell($systypes_array[$trans_type]);

                        $link = get_trans_view_str($trans_type, $row['tran_no']);
                        label_cell($link, "align=center");
                        label_cell($document_approval_statuses[$row['approval_status']]);

                        $pre_user = get_user($row['pre_approved_by']);
                        label_cell($pre_user['real_name']);
                        label_cell($row['approved_on'], "align=center");
                        label_cell($row['pre_approved_on'], "align=center");
                        
                        $delay = dashboard_format_approval_delay_time($row['diff']);
                        label_cell($delay, "align=right");

                    end_row();
                    
                    $avg_t += $row['diff'];
                    $count++;
                }
                
                start_row();
                    $avg_t /= $count;
                    $total_avg_delay = dashboard_format_approval_delay_time($avg_t);
                    label_cell(text_bold('AVG Time Difference'), "align=right colspan=6");
                    label_cell(text_bold($total_avg_delay), "align=right");
                end_row();
                
    end_table(2);
}

function dashboard_get_user_approvals_period_details($from_date, $to_date, $user_id, $trans_type) {
    $sql_2 = dashboard_get_sql_for_approvals($from_date, $to_date, $user_id, $trans_type);
    
    $sql = "SELECT p.*, ( SELECT pl.approved_by  
                            FROM ".TB_PREF."approval_log pl 
                            WHERE pl.tran_type = p.tran_type 
                                AND pl.tran_no = p.tran_no 
                                AND pl.id < p.id 
                            ORDER BY pl.id 
                            DESC LIMIT 1) pre_approved_by 
            FROM ($sql_2) p 
            WHERE 1 = 1";
    
    return db_query($sql, "dashboard_get_user_approvals_period_details");  
}

?>