<?php

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

    switch ($type) {
        case DASH_VW_INVENTORY_MATERIAL_REQUISITIONS_CREATED:
        case DASH_VW_INVENTORY_MATERIAL_REQUISITIONS_UNAPPROVED:
            dashboard_print_material_requisitions_view($type, $from_date, $to_date); 
            break;
        
        case DASH_VW_INVENTORY_MATERIAL_REQUISITIONS_NOT_ISSUED:
            $result = view_dashboard_get_material_requisitions_not_issued_details($from_date, $to_date); 
                dashboard_print_material_requisitions_type_details($type, $result);
            break;
        
        case DASH_VW_INVENTORY_MATERIAL_REQUISITIONS_PARTIALLY_ISSUED:
            $result = view_dashboard_get_material_requisitions_partially_issued_details($from_date, $to_date); 
                dashboard_print_material_requisitions_type_details($type, $result);
            break;
        
        case DASH_VW_INVENTORY_MATERIAL_REQUISITIONS_FULLY_ISSUED:
            $result = view_dashboard_get_material_requisitions_fully_issued_details($from_date, $to_date); 
                dashboard_print_material_requisitions_type_details($type, $result);
            break;
        
        case DASH_VW_INVENTORY_MATERIAL_REQUISITIONS_PARTIALLY_ISSUED_AND_CLOSED:
            $result = view_dashboard_get_material_requisitions_partially_issued_and_closed_details($from_date, $to_date); 
                dashboard_print_material_requisitions_type_details($type, $result);
            break;
        
        case DASH_VW_INVENTORY_MATERIAL_REQUISITIONS_CLOSED_WITHOUT_ISSUING:
            $result = view_dashboard_get_material_requisitions_closed_without_issuing_details($from_date, $to_date); 
                dashboard_print_material_requisitions_type_details($type, $result);
            break;
    }
}

function dashboard_print_material_requisitions_view($type, $from_date, $to_date) {
    global $dashboard_view_types_array, $document_approval_statuses;
    $ap_st_array = null;
     
        if ($type == DASH_VW_INVENTORY_MATERIAL_REQUISITIONS_UNAPPROVED) {
            $ap_st_array = array(AS_NEW, AS_BACK_TO_ORIGINATOR, AS_AWAITING_APPROVAL, AS_WITH_THE_APPROVER);
        }
    
        display_heading('Material Requisitions - '.$dashboard_view_types_array[$type]);    br();
        
        start_table(TABLESTYLE, "width=60%");

            $th = array("Requisted Date", "MR #", "Reference", "Project", "Approval Status", "Total"); 
                table_header($th);
                
                    $k = $total_amount = $count = 0;
                    $result = view_dashboard_get_material_requisitions_details($from_date, $to_date, $ap_st_array);
                        while($row = db_fetch($result)) {
                            alt_table_row_color($k);
                                label_cell(sql2date($row['mr_date']),"align=center");
                                $link = get_trans_view_str(ST_MATERIAL_REQ, $row['mr_no']);
                                label_cell($link, "align=center");
                                label_cell($row['reference']);
                                label_cell($row['project_name']);
                                label_cell($document_approval_statuses[$row['approval_status']]);
                                $total = $row["total"];
                                label_cell(number_format($total, 2),"align=right");
                            end_row();

                            $total_amount += $total;
                            $count++;
                        }
                        
                        start_row();
                            label_cell(text_bold('Count'), "align=right");
                            label_cell(text_bold($count),"align=center");
                            label_cell(text_bold('Total'), "align=right colspan=3");
                            label_cell(text_bold(number_format($total_amount, 2)),"align=right");
                        end_row();
                
        end_table(2);
}

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

            $th = array("Requisted Date", "MR #", "Reference", "Project", "Total");
                table_header($th);
                
                    $k = $total_amount = $count = 0;
                        while($row = db_fetch($result)) {
                            alt_table_row_color($k);
                                label_cell(sql2date($row['mr_date']),"align=center");
                                $link = get_trans_view_str(ST_MATERIAL_REQ, $row['mr_no']);
                                label_cell($link, "align=center");
                                label_cell($row['reference']);
                                label_cell($row['project_name']);
                                $total = $row["total"];
                                label_cell(number_format($total, 2),"align=right");
                            end_row();

                            $total_amount += $total;
                            $count++;
                        }
                        
                        start_row();
                            label_cell(text_bold('Count'), "align=right");
                            label_cell(text_bold($count),"align=center");
                            label_cell(text_bold('Total'), "align=right colspan=2");
                            label_cell(text_bold(number_format($total_amount, 2)),"align=right");
                        end_row();
                
        end_table(2);
}

?>