<?php

function display_aged_service_complains_filtering_parameters() {
    start_table(TABLESTYLE_NOBORDER);
        start_row();
            date_cells(_("To Date:"), 'to_date', null, null, 0, 0);
            custom_values_list_cells("Number of Months Backwards:", "num_of_months", array(3=>3,6=>6,9=>9,12=>12), null, false);
            
            submit_cells('SearchOrders', _("Search"), '', _('Select documents'), false, ICON_SUBMIT);
        end_row();
    end_table(1);
}

function display_aged_service_complains_view($to_date_, $number_of_moths=3) {
    $width = 39; $comp_totals_array = array();
        
        $th = array(_("Request Department"));
        
        $header_array = create_aged_service_complains_header_array($to_date_, $number_of_moths);
            foreach ($header_array as $key => $value) {
                array_push($th, $value['column_name']);
                
                $width += 5;
                $comp_totals_array[$key] = 0;
            }
            
            start_table(TABLESTYLE, "width=$width%");
                table_header($th);

                    $k = $previous_department_id = 0;   
                    
                    $section_list_codes_array = create_request_departmens_array();
                        foreach ($section_list_codes_array as $req_ped_id => $req_dep_name) {
                            alt_table_row_color($k);
                                label_cell($req_dep_name);

                                foreach ($header_array as $key => $val) {
                                    $from_date = $val['from_date'];
                                    $to_date = $val['to_date'];
                                    
                                    $count = get_service_complains_count_by_filters($from_date, $to_date, $req_ped_id, 0, 0, 0);
                                    $params = "&from_date=$from_date&to_date=$to_date&req_department_id=$req_ped_id&closed=0&has_job=0";
                                    
                                    if ($count > 0) {
                                        label_cell(get_dashboard_view_str(DASH_VW_SERVICE_SERVICE_COMPLAIN, $params, $count), 'align=center');
                                        
                                        $comp_totals_array[$key] += $count;
                                    } else {
                                        label_cell('');
                                    }
                                }
                            end_row();
                        }
                        
                        display_aged_service_complains_company_total($header_array, $comp_totals_array);

            end_table(2);
}

function create_aged_service_complains_header_array($to_date_, $number_of_moths=3) {
    $header_array = array();
    
        $from_date_ = $from_dt_ = date('m/01/Y', strtotime($to_date_));
        $to_dt_ = date('m/t/Y', strtotime($to_date_));
    
            for ($i=1; $number_of_moths >= $i; $i++) {
                $header_array[$i] = array('column_name' => "$from_dt_ - $to_dt_", 'from_date' => date2sql($from_dt_), 'to_date' => date2sql($to_dt_));

                $from_dt_ = date('m/01/Y', strtotime(add_months($from_date_, -$i)));
                $to_dt_ = date('m/t/Y', strtotime($from_dt_));
            }
        
            krsort($header_array);

            return $header_array;
}

function display_aged_service_complains_company_total($header_array, $comp_totals_array) {
    start_row("style=background-color:#f58142");
        label_cell('', 'align=right');
        
        foreach ($header_array as $key => $val) {
            $from_date = $val['from_date'];
            $to_date = $val['to_date'];
            $params = "&from_date=$from_date&to_date=$to_date&closed=0&has_job=0";
            
            $dep_total = $comp_totals_array[$key];
            
                if ($dep_total > 0) {
                    label_cell(get_dashboard_view_str(DASH_VW_SERVICE_SERVICE_COMPLAIN, $params, text_bold($dep_total)), 'align=center');
                } else {
                    label_cell('0', 'align=center style=color:silver');
                }
        }
    end_row();
}

?>