<?php

function dashboard_display_purchase_orders($from_month_display_order, $to_month_display_order) {
    
   $month_ranges_array = dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order);
   $table_width = dashboard_calculate_table_width($month_ranges_array);
   
   start_table(TABLESTYLE, "width$table_width%");
        
        $th = array("Category");
            foreach ($month_ranges_array as $month) {
                $th[] = $month['month_name'];
            }

            table_header($th);
        
                dashboard_purchase_orders_created_and_unapproved_details($month_ranges_array, DASH_VW_PURCHASE_PURCHASE_ORDERS_CREATED, 0);

                dashboard_purchase_orders_created_and_unapproved_details($month_ranges_array, DASH_VW_PURCHASE_PURCHASE_ORDERS_UNAPPROVED, 1);

                dashboard_purchase_orders_types_detals($month_ranges_array, DASH_VW_PURCHASE_PURCHASE_ORDERS_NOT_GRNED, 0);

                dashboard_purchase_orders_types_detals($month_ranges_array, DASH_VW_PURCHASE_PURCHASE_ORDERS_PARTIALLY_GRNED, 1);

                dashboard_purchase_orders_types_detals($month_ranges_array, DASH_VW_PURCHASE_PURCHASE_ORDERS_FULLY_GRNED, 0);

                dashboard_purchase_orders_types_detals($month_ranges_array, DASH_VW_PURCHASE_PURCHASE_ORDERS_NOT_INVOICED, 1);

                dashboard_purchase_orders_types_detals($month_ranges_array, DASH_VW_PURCHASE_PURCHASE_ORDERS_PARTIALLY_INVOICED, 0);

                dashboard_purchase_orders_types_detals($month_ranges_array, DASH_VW_PURCHASE_PURCHASE_ORDERS_FULLY_INVOICED, 1);
            
    end_table(2);
}

function dashboard_purchase_orders_created_and_unapproved_details($month_ranges_array, $type, $k=0) {
    $ap_st_array = null;
    
        alt_table_row_color($k);

            if ($type === DASH_VW_PURCHASE_PURCHASE_ORDERS_CREATED) {
                label_cell('Created');
            } else {
                label_cell('Unapproved');
                $ap_st_array = array(AS_NEW, AS_BACK_TO_ORIGINATOR, AS_AWAITING_APPROVAL, AS_WITH_THE_APPROVER);
            }

            foreach ($month_ranges_array as $month) {
                $from_date = $month['month_begin'];
                $to_date = $month['month_end'];
                $parameters = "type=$type&from_date=$from_date&to_date=$to_date";
                $count = dashboard_get_purchase_orders_count($from_date, $to_date, $ap_st_array);
                
                if($count > 0){
                    $str_count = get_dashboard_view_str(DASH_SC_PURCHASE_PURCHASE_ORDERS, $parameters, $count);
                }else{   
                    $str_count = "";
                }
                
                label_cell($str_count, 'align=center');
                    
            }

        end_row();
}

function dashboard_purchase_orders_types_detals($month_ranges_array, $type, $k=0) {
    global $dashboard_view_types_array;
    
    alt_table_row_color($k);

        label_cell($dashboard_view_types_array[$type]);
        
        switch ($type) {
            case DASH_VW_PURCHASE_PURCHASE_ORDERS_NOT_GRNED :
                foreach ($month_ranges_array as $month) {
                    $from_date = $month['month_begin'];
                    $to_date = $month['month_end'];
                    $parameters = "type=$type&from_date=$from_date&to_date=$to_date";
                    $count = dashboard_get_purchase_orders_not_grned_count($from_date, $to_date);
                    
                    if($count > 0){
                        $str_count = get_dashboard_view_str(DASH_SC_PURCHASE_PURCHASE_ORDERS, $parameters, $count);
                    }else{   
                        $str_count = "";
                    }

                    label_cell($str_count, 'align=center');
                }

                break;
            case DASH_VW_PURCHASE_PURCHASE_ORDERS_PARTIALLY_GRNED :
                foreach ($month_ranges_array as $month) {
                    $from_date = $month['month_begin'];
                    $to_date = $month['month_end'];
                    $parameters = "type=$type&from_date=$from_date&to_date=$to_date";
                    $count = dashboard_get_purchase_orders_partially_grned_count($from_date, $to_date);
                    
                    if($count > 0){
                        $str_count = get_dashboard_view_str(DASH_SC_PURCHASE_PURCHASE_ORDERS, $parameters, $count);
                    }else{   
                        $str_count = "";
                    }

                    label_cell($str_count, 'align=center');
                }

                break;
            case DASH_VW_PURCHASE_PURCHASE_ORDERS_FULLY_GRNED :
                foreach ($month_ranges_array as $month) {
                    $from_date = $month['month_begin'];
                    $to_date = $month['month_end'];
                    $parameters = "type=$type&from_date=$from_date&to_date=$to_date";
                    $count = dashboard_get_purchase_orders_fully_grned_count($from_date, $to_date);
                    
                    if($count > 0){
                        $str_count = get_dashboard_view_str(DASH_SC_PURCHASE_PURCHASE_ORDERS, $parameters, $count);
                    }else{   
                        $str_count = "";
                    }

                    label_cell($str_count, 'align=center');
                }

                break;
            case DASH_VW_PURCHASE_PURCHASE_ORDERS_NOT_INVOICED :
                foreach ($month_ranges_array as $month) {
                    $from_date = $month['month_begin'];
                    $to_date = $month['month_end'];
                    $parameters = "type=$type&from_date=$from_date&to_date=$to_date";
                    $count = dashboard_get_purchase_orders_not_invoiced_count($from_date, $to_date);
                    
                    if($count > 0){
                        $str_count = get_dashboard_view_str(DASH_SC_PURCHASE_PURCHASE_ORDERS, $parameters, $count);
                    }else{   
                        $str_count = "";
                    }

                    label_cell($str_count, 'align=center');
                }

                break;
            case DASH_VW_PURCHASE_PURCHASE_ORDERS_PARTIALLY_INVOICED :
                foreach ($month_ranges_array as $month) {
                    $from_date = $month['month_begin'];
                    $to_date = $month['month_end'];
                    $parameters = "type=$type&from_date=$from_date&to_date=$to_date";
                    $count = dashboard_get_purchase_orders_partially_invoiced_count($from_date, $to_date);
                    
                    if($count > 0){
                        $str_count = get_dashboard_view_str(DASH_SC_PURCHASE_PURCHASE_ORDERS, $parameters, $count);
                    }else{   
                        $str_count = "";
                    }

                    label_cell($str_count, 'align=center');
                }

                break;
            case DASH_VW_PURCHASE_PURCHASE_ORDERS_FULLY_INVOICED :
                foreach ($month_ranges_array as $month) {
                    $from_date = $month['month_begin'];
                    $to_date = $month['month_end'];
                    $parameters = "type=$type&from_date=$from_date&to_date=$to_date";
                    $count = dashboard_get_purchase_orders_fully_invoiced_count($from_date, $to_date);
                    
                    if($count > 0){
                        $str_count = get_dashboard_view_str(DASH_SC_PURCHASE_PURCHASE_ORDERS, $parameters, $count);
                    }else{   
                        $str_count = "";
                    }

                    label_cell($str_count, 'align=center');
                }

                break;

            default:
                break;
        }

    end_row();
}

?>