<?php

function display_job_order_status_summary_filtering_parameters() {
    start_table(TABLESTYLE_NOBORDER);
        start_row();
            date_cells(_("From Date:"), 'from_date', null, null, 0, -2);
            date_cells(_("To Date:"), 'to_date', null, null, 0, 0);
            value_list_cells('Department:', 'department_id', 'DEPARTMENTS', null, '');
            
            submit_cells('SearchOrders', _("Search"), '', _('Select documents'), false, ICON_SUBMIT);
        end_row();
    end_table(1);
}

function display_job_order_status_summary($from_date_, $to_date_, $department_id) {
    $from_date = date2sql($from_date_); $to_date = date2sql($to_date_); $dep_totals_array = $comp_totals_array = array();
        
        $th = array(_("Section"));
        
        $header_array = job_order_approval_status_array();
            foreach ($header_array as $approval_status => $ap_st_name) {
                array_push($th, $ap_st_name);
                
                $dep_totals_array[$approval_status] = 0;
            }
            
            $colspan = count($header_array) + 1;
            
            $comp_totals_array = $dep_totals_array;
        
            start_table(TABLESTYLE, "width=80%");
                table_header($th);

                $section_list_codes_array = create_job_order_department_section_list_codes_array($department_id);

                $k = $previous_department_id = 0;  $section_list_code = $previous_department_code = '';
                    
                    $result = get_job_order_all_sections($section_list_codes_array);
                        while ($row = db_fetch($result)) {
                            $section_id = $row['id'];
                            $section_list_code = $row['list_code'];
                            $department_code = array_search("'$section_list_code'", $section_list_codes_array);
                            $dep_row = get_listvalue_by_code('DEPARTMENTS', $department_code);
                            
                            if ($previous_department_code != $department_code) {
                                if ($previous_department_code != '') {
                                    display_job_order_status_summary_department_total($header_array, $dep_totals_array, $previous_department_id, $comp_totals_array, 
                                                                                      $from_date, $to_date);
                                    vertical_space("height=18px colspan=$colspan");
                                }
                                
                                start_row("style=background-color:#86d660");
                                    label_cell(text_bold($dep_row['value_name']), "align=center colspan=$colspan");
                                end_row();
                            }

                            alt_table_row_color($k);
                                label_cell($row['value_name']);

                                foreach ($header_array as $approval_status => $ap_st_name) {
                                    $count = get_job_orders_count_by_filters($from_date, $to_date, $department_id, $section_id, $approval_status);
                                    $params = "&from_date=$from_date&to_date=$to_date&section_id=$section_id&approval_status=$approval_status";
                                    
                                    if ($count > 0) {
                                        label_cell(get_dashboard_view_str(DASH_VW_SERVICE_JOB_ORDERS, $params, $count), 'align=center');
                                        
                                        $dep_totals_array[$approval_status] += $count;
                                    } else {
                                        label_cell('');
                                    }
                                }
                            end_row();
                            
                            $previous_department_code = $department_code;
                            $previous_department_id = $dep_row['id'];
                        }
                        
                        if ($previous_department_code != '') {
                            display_job_order_status_summary_department_total($header_array, $dep_totals_array, $previous_department_id, $comp_totals_array, $from_date, 
                                                                              $to_date);
                        }
                        
                        if (! ($department_id > 0)) {
                            display_job_order_status_summary_company_total($header_array, $comp_totals_array, $from_date, $to_date);
                        }

            end_table(2);
}

function display_job_order_status_summary_department_total($header_array, &$dep_totals_array, $previous_department_id, &$comp_totals_array, $from_date, $to_date) {
    start_row("style=background-color:#edda8e");
        label_cell('', 'align=right');
        
        foreach ($header_array as $approval_status => $ap_st_name) {
            $params = "&from_date=$from_date&to_date=$to_date&department_id=$previous_department_id&approval_status=$approval_status";
            
            $dep_total = $dep_totals_array[$approval_status];
            
                if ($dep_total > 0) {
                    label_cell(get_dashboard_view_str(DASH_VW_SERVICE_JOB_ORDERS, $params, text_bold($dep_total)), 'align=center');

                    $dep_totals_array[$approval_status] = 0;
                } else {
                    label_cell('0', 'align=center style=color:silver');
                }

                $comp_totals_array[$approval_status] += $dep_total;
        }
    end_row();
}

function display_job_order_status_summary_company_total($header_array, $comp_totals_array, $from_date, $to_date) {
    start_row("style=background-color:#f58142");
        label_cell('', 'align=right');
        
        foreach ($header_array as $approval_status => $ap_st_name) {
            $params = "&from_date=$from_date&to_date=$to_date&approval_status=$approval_status";
            
            $comp_total = $comp_totals_array[$approval_status];
            
                if ($comp_total > 0) {
                    label_cell(get_dashboard_view_str(DASH_VW_SERVICE_JOB_ORDERS, $params, text_bold($comp_total)), 'align=center');
                } else {
                    label_cell('0', 'align=center style=color:silver');
                }
        }
    end_row();
}

?>