<?php

function dashboard_petty_cash_data_entry($from_month_display_order, $to_month_display_order) {
    
    global $systypes_array;
//    display_heading2('(Based on Goods Received Notes and Tax Free Item Costs)');    
    br();
    
    $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("Transaction Type");
            foreach ($month_ranges_array as $month) {
                $th[] = $month['month_name'];
            }
            $th[] = "Average";

            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'];
            
            $users_res = dashboard_get_petty_cash_data_entry_users($from_date, $to_date);
            
            $k = $previous_user_id = 0;
            
            
            while ($user = db_fetch($users_res)) {
                
                $user_id = $user['created_by'];
                $tran_type = $user['tran_type'];
                
                if($previous_user_id != $user_id){
                    start_row();
                    label_cell(spaces(10).text_bold($user['real_name']), 'colspan=14');
                    end_row();
                }
                
                alt_table_row_color($k);
                
                

                
                label_cell($systypes_array[$user['tran_type']]);
                
                
                
                $total_delay = $count = 0;
                foreach ($month_ranges_array as $month) {
                    
                    $from_date = $month['month_begin'];
                    $to_date = $month['month_end'];
                    
//                    $user_id = 180;
//                    $tran_type = 232;
                    $row = dashboard_get_petty_cash_user_data_entry_period_summary($from_date, $to_date, $user_id, $tran_type);
                    if($row['tran_count'] == 0){
                        label_cell("");
                        continue;
                    }
                    
                    $delay = $row['delay'];
//                    if($delay != 0){                                            
                        $parameters = "uid=".$user_id."&type=".$tran_type."&from_date=$from_date&to_date=$to_date";
                        $str_delay = get_dashboard_view_str(DASH_SC_PETTY_CASH_DATA_ENTRY, $parameters, number_format($delay,2));
                        label_cell($str_delay, 'align=center');
                        $total_delay += $delay;
                        $count++;
//                    }else{
//                        label_cell("");
//                    }

//                    }else{
////                        $str_count = number_format($total,2);
//                        $str_count = "";
//                    }
//                    label_cell($str_count, 'align=right');
                }   
                
                if($count > 0){
                    $total_ave_delay = $total_delay/$count;
                }else{
                    $total_ave_delay = 0;
                }
                
                if($total_ave_delay != 0){
                    label_cell(number_format($total_delay/$count,2), 'align=right');
                }else{
                    label_cell("");
                }
                end_row();
                
                $previous_user_id = $user_id;
            }
        
                
            
    end_table(2);
}
