<?php

function dashboard_purchase_orders_views($type, $from_date, $to_date) {

    switch ($type) {
        case DASH_VW_PURCHASE_PURCHASE_ORDERS_CREATED:
        case DASH_VW_PURCHASE_PURCHASE_ORDERS_UNAPPROVED:
            dashboard_print_purchase_orders_created_and_unapproved($type, $from_date, $to_date); 
            break;
        
        case DASH_VW_PURCHASE_PURCHASE_ORDERS_NOT_GRNED:
            $result = view_dashboard_get_purchase_orders_not_grned_details($from_date, $to_date); 
                dashboard_print_purchase_orders_type_details($type, $result);
            break;
        
        case DASH_VW_PURCHASE_PURCHASE_ORDERS_PARTIALLY_GRNED:
            $result = view_dashboard_get_purchase_orders_partially_grned_details($from_date, $to_date); 
                dashboard_print_purchase_orders_type_details($type, $result);
            break;
        
        case DASH_VW_PURCHASE_PURCHASE_ORDERS_FULLY_GRNED:
            $result = view_dashboard_get_purchase_orders_fully_grned_details($from_date, $to_date); 
                dashboard_print_purchase_orders_type_details($type, $result);
            break;
        
        case DASH_VW_PURCHASE_PURCHASE_ORDERS_NOT_INVOICED:
            $result = view_dashboard_get_purchase_orders_not_invoiced_details($from_date, $to_date); 
                dashboard_print_purchase_orders_type_details($type, $result);
            break;
        
        case DASH_VW_PURCHASE_PURCHASE_ORDERS_PARTIALLY_INVOICED:
            $result = view_dashboard_get_purchase_orders_partially_invoiced_details($from_date, $to_date); 
                dashboard_print_purchase_orders_type_details($type, $result);
            break;
        
        case DASH_VW_PURCHASE_PURCHASE_ORDERS_FULLY_INVOICED:
            $result = view_dashboard_get_purchase_orders_fully_invoiced_details($from_date, $to_date); 
                dashboard_print_purchase_orders_type_details($type, $result);
            break;
    }
}

function dashboard_print_purchase_orders_created_and_unapproved($type, $from_date, $to_date) {
    global $dashboard_view_types_array;
    $ap_st_array = null;
     
        if ($type == DASH_VW_PURCHASE_PURCHASE_ORDERS_UNAPPROVED) {
            $ap_st_array = array(AS_NEW, AS_BACK_TO_ORIGINATOR, AS_AWAITING_APPROVAL, AS_WITH_THE_APPROVER);
        }
    
        display_heading('Purchase Orders - '.$dashboard_view_types_array[$type]);    br();
        
        start_table(TABLESTYLE, "width=60%");

            $th = array("Order Date", "Order #", "Reference", "Supplier", "Total");
                table_header($th);
                
                    $k = $total_amount = $count = 0;
                    $result = view_dashboard_get_purchase_orders_details($from_date, $to_date, $ap_st_array);
                        while($row = db_fetch($result)) {
                            alt_table_row_color($k);
                                label_cell(sql2date($row['ord_date']),"align=center");
                                $link = get_trans_view_str(ST_PURCHORDER, $row['order_no']);
                                label_cell($link, "align=center");
                                label_cell($row['reference']);
                                label_cell($row['supp_name']);
                                $total = $row["total"];
                                label_cell(number_format($total, 2),"align=right");
                            end_row();

                            $total_amount += $total;
                            $count++;
                        }
                        
                        start_row();
                            label_cell("Count","align=right");
                            label_cell(text_bold($count),"align=center");
                            label_cell("Total for ".'Purchase Orders '.$dashboard_view_types_array[$type], "align=right colspan=2");
                            label_cell(text_bold(number_format($total_amount, 2)),"align=right");
                        end_row();
                
        end_table(2);
}

function dashboard_print_purchase_orders_type_details($type, $result) {
    global $dashboard_view_types_array;
    
        display_heading('Purchase Orders - '.$dashboard_view_types_array[$type]);    br();
        
        start_table(TABLESTYLE, "width=60%");

            $th = array("Order Date", "Order #", "Reference", "Supplier", "Total");
                table_header($th);
                
                    $k = $total_amount = $count = 0;
                        while($row = db_fetch($result)) {
                            alt_table_row_color($k);
                                label_cell(sql2date($row['ord_date']),"align=center");
                                $link = get_trans_view_str(ST_PURCHORDER, $row['order_no']);
                                label_cell($link, "align=center");
                                label_cell($row['reference']);
                                label_cell($row['supp_name']);
                                $total = $row["total"];
                                amount_decimal_cell($total); 
                            end_row();

                            $total_amount += $total;
                            $count++;
                        }
                        
                        start_row();
                            label_cell("Count","align=right");
                            label_cell(text_bold($count),"align=center");
                            label_cell("Total for ".'Purchase Orders '.$dashboard_view_types_array[$type], "align=right colspan=2");
                            label_cell(text_bold(number_format($total_amount, 2)),"align=right");
                        end_row();
                
        end_table(2);
}

?>