<?php

function dashboard_transport_vehicle_all_costs($from_month_display_order, $to_month_display_order, $vehicle_id) {
    
    if(!($vehicle_id > 0)){
        display_error("Please select a vehicle.");
        return;
    }
    $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'];
            }
            $th[] = "Total";

            table_header($th);
            
            $from_month = get_month_by_display_order($from_month_display_order);
            $to_month = get_month_by_display_order($to_month_display_order);
            
            $from_date = $from_month['month_begin_date'];
            $to_date = $to_month['month_end_date'];
            
            
            dashboard_transport_vehicle_all_costs_item_issues($from_date, $to_date, $month_ranges_array, $vehicle_id);
            dashboard_transport_vehicle_all_costs_petty_cash_expenses($from_date, $to_date, $month_ranges_array, $vehicle_id);
            dashboard_transport_vehicle_all_costs_utility_expenses($from_date, $to_date, $month_ranges_array, $vehicle_id);
            
            start_row();
            label_cell(text_bold("Totals"), 'align=right');
            
            $grand_total = 0;
            foreach ($month_ranges_array as &$month) {
                $month_totals = $month['month_totals'];
                $total = 0;
                foreach ($month_totals as $month_type_total) {
                    $total += $month_type_total;
                }
                $str_count = number_format($total,2);

                label_cell(text_bold($str_count), 'align=right');
                
                $grand_total += $total; 
            }   
            label_cell(text_bold(number_format($grand_total,2)), 'align=right'); 
            end_row();
        
    end_table(2);
}


function dashboard_transport_vehicle_all_costs_item_issues($from_date, $to_date, &$month_ranges_array, $vehicle_id) {
    
//    global $systypes_array;

            start_row();
            label_cell('');
            label_cell(text_bold("Item Issues"), 'colspan=13');
            end_row();
            
            $items_res = dashboard_get_transport_all_costs_item_consumption_items_value($from_date, $to_date, $vehicle_id);
            if(db_num_rows($items_res) == 0){
                start_row();
                label_cell('');
                label_cell("No Item Issues found", 'colspan=13 align=center');
                end_row();
            }
            $k = 0;
            
            $linked_field_type = IMLF_VEHICLE;
            
            while ($item = db_fetch($items_res)) {
                alt_table_row_color($k);

                $item_code = $item['item_code'];
                $item_name = get_stock_item_formatted_name($item_code, false, $item['item_description']);
                label_cell($item_name);
                
                foreach ($month_ranges_array as &$month) {
                    
                    $from_date = $month['month_begin'];
                    $to_date = $month['month_end'];

                    $parameters = "item=".$item_code."&from_date=$from_date&to_date=$to_date&lftid=$linked_field_type&lfid=$vehicle_id";
                    
                    $total = dashboard_get_period_transport_all_costs_item_consumption_total($item_code, $from_date, $to_date, $vehicle_id);

                    if($total != 0){
                        $str_count = get_dashboard_view_str(DASH_SC_INVENTORY_ITEM_CONSUMPTION, $parameters, number_format($total,2));
                        if(isset($month['month_totals'][DASH_SC_INVENTORY_ITEM_CONSUMPTION])){ 
                            $month['month_totals'][DASH_SC_INVENTORY_ITEM_CONSUMPTION] += $total; 
                        }else{
                            $month['month_totals'][DASH_SC_INVENTORY_ITEM_CONSUMPTION] = $total; 
                        }
                    }else{
//                        $str_count = number_format($total,2);
                        $str_count = "";
                    }
                    label_cell($str_count, 'align=right');
                }   
                
                label_cell(number_format($item['grand_total'],2), 'align=right');
                end_row();
            }
        
                 
}


function dashboard_transport_vehicle_all_costs_petty_cash_expenses($from_date, $to_date, &$month_ranges_array, $vehicle_id) {
    
            start_row();
            label_cell('');
            label_cell(text_bold("Petty Cash Expenses"), 'colspan=13');
            end_row();
            
            $expense_types_res = dashboard_get_petty_cash_expense_types($from_date, $to_date, $vehicle_id);
            if(db_num_rows($expense_types_res) == 0){
                start_row();
                label_cell('');
                label_cell("No Petty Cash Expenses found", 'colspan=13 align=center');
                end_row();
            }
            
            $k = 0;
            
            
            while ($exp = db_fetch($expense_types_res)) {
                alt_table_row_color($k);

                label_cell($exp['expense_type_name']);
                
                foreach ($month_ranges_array as &$month) {
                    
                    $from_date = $month['month_begin'];
                    $to_date = $month['month_end'];
                    $expense_type_id = $exp['expense_type_id'];
                    $parameters = "expt=".$expense_type_id."&from_date=$from_date&to_date=$to_date&vid=".$vehicle_id;
                    $total = dashboard_get_petty_cash_period_expense_type_total($expense_type_id, $from_date, $to_date, $vehicle_id);

                    if($total > 0){
                        $str_count = get_dashboard_view_str(DASH_SC_PETTY_CASH_EXPENSES, $parameters, number_format($total,2));
                        if(isset($month['month_totals'][DASH_SC_PETTY_CASH_EXPENSES])){ 
                            $month['month_totals'][DASH_SC_PETTY_CASH_EXPENSES] += $total; 
                        }else{
                            $month['month_totals'][DASH_SC_PETTY_CASH_EXPENSES] = $total; 
                        }
                    }else{
//                        $str_count = number_format($total,2);
                        $str_count = "";
                    }
                    label_cell($str_count, 'align=right');
                }   
                
                label_cell(number_format($exp['approved_total'],2), 'align=right');
                end_row();
            }

}


function dashboard_transport_vehicle_all_costs_utility_expenses($from_date, $to_date, &$month_ranges_array, $vehicle_id) {
    
            start_row();
            label_cell('');
            label_cell(text_bold("Utility Expenses"), 'colspan=13');
            end_row();
            
            $expense_types_res = dashboard_get_utility_expense_types($from_date, $to_date, $vehicle_id);
            if(db_num_rows($expense_types_res) == 0){
                start_row();
                label_cell('');
                label_cell("No Utility Expenses found", 'colspan=13 align=center');
                end_row();
            }
            
            $k = 0;
            while ($exp = db_fetch($expense_types_res)) {
                alt_table_row_color($k);
                $line_totals = 0;
                    label_cell($exp['expense_type_name']);

                    foreach ($month_ranges_array as &$month) {
                        $from_date = $month['month_begin'];
                        $to_date = $month['month_end'];
                        $expense_type_id = $exp['expense_type_id'];
                        $parameters = "expt=".$expense_type_id."&from_date=$from_date&to_date=$to_date&vid=$vehicle_id";

                        $total = dashboard_get_utility_period_expense_type_total($expense_type_id, $from_date, $to_date);
                            if ($total > 0) {
                                $str_count = get_dashboard_view_str(DASH_SC_TRANSPORT_UTILITY_EXPENSES, $parameters, number_format($total, 2));
                                if(isset($month['month_totals'][DASH_SC_TRANSPORT_UTILITY_EXPENSES])){
                                    $month['month_totals'][DASH_SC_TRANSPORT_UTILITY_EXPENSES] += $total; 
                                }else{
                                    $month['month_totals'][DASH_SC_TRANSPORT_UTILITY_EXPENSES] = $total; 
                                }
                            } else {
                                $str_count = "";
                            }

                        label_cell($str_count, 'align=right');
                        
                        $line_totals += $total;
                    }   

                    label_cell(number_format($exp['grand_total'], 2), 'align=right');
                end_row();
            }
}