@@ -281,7 +281,16 @@ public function get_amounts_range( $from_day, $from_month, $from_year, $to_day,
281281 $ cache_key = 'amounts_range_ ' . $ first_date . '_ ' . $ last_date ;
282282 $ result = $ this ->check_cache ( $ cache_key , $ query );
283283
284- $ variance = array ();
284+ $ variance = array (
285+ '0-100 ' => 0 ,
286+ '100-300 ' => 0 ,
287+ '300-500 ' => 0 ,
288+ '500-1000 ' => 0 ,
289+ '1000-3000 ' => 0 ,
290+ '3000-5000 ' => 0 ,
291+ '5000-10000 ' => 0 ,
292+ '10000-20000 ' => 0
293+ );
285294 $ milestones = array ( 0 , 100 , 300 , 500 , 1000 , 3000 , 5000 , 10000 , 20000 );
286295
287296 foreach ( $ result as $ amount ) {
@@ -413,6 +422,167 @@ public function get_preferred_count( $from_day, $from_month, $from_year, $to_day
413422 return $ out ;
414423
415424 }
425+
426+
427+ // returns an array with all stats
428+ public function get_all_stats ($ from_day , $ from_month , $ from_year , $ to_day , $ to_month , $ to_year , $ chart_display_method = 'months ' ) {
429+
430+ $ stats = array ();
431+
432+ $ averages = $ this ->get_months_average ( $ from_month , $ from_year , $ to_month , $ to_year );
433+ $ preferred_count = $ this ->get_preferred_count ( $ from_day , $ from_month , $ from_year , $ to_day , $ to_month , $ to_year );
434+ $ get_amounts_range = $ this ->get_amounts_range ( $ from_day , $ from_month , $ from_year , $ to_day , $ to_month , $ to_year );
435+
436+ $ chart_amounts_range = array ();
437+ $ get_available_ranges = array ();
438+ foreach ( $ get_amounts_range as $ range => $ num_of_tasks ) {
439+ $ chart_amounts_range [] = '[" ' . $ range . '", ' . $ num_of_tasks . '] ' ;
440+ $ get_available_ranges [] = '" ' .$ range .'" ' ;
441+ }
442+
443+ $ get_tasks_type = $ this ->get_tasks_type ( $ from_day , $ from_month , $ from_year , $ to_day , $ to_month , $ to_year );
444+
445+ $ type_categories = array ();
446+ $ type_contractor_fee = array ();
447+ $ type_revenue = array ();
448+ $ type_tasks_count = array ();
449+
450+ foreach ($ get_tasks_type as $ type => $ type_data ) {
451+
452+ $ type_categories [ $ type ] = "' " . $ type . "' " ;
453+ $ type_contractor_fee [ $ type ] = floatval ( $ type_data ['fee ' ] );
454+ $ type_revenue [ $ type ] = floatval ( $ type_data ['revenue ' ] );
455+ $ type_tasks_count [ $ type ] = intval ( $ type_data ['count ' ] );
456+ }
457+
458+ $ type_tasks_count_json = json_encode ( $ type_tasks_count );
459+
460+ if ( $ chart_display_method == 'months ' ) {
461+
462+ $ month_totals = $ this ->get_month_range_totals ( $ from_month , $ from_year , $ to_month , $ to_year );
463+
464+ $ max_month_totals = max ( $ month_totals );
465+ $ max_month_totals_key = array_keys ( $ month_totals , max ( $ month_totals ) );
466+
467+ $ all_month_totals = array ();
468+ $ all_month_totals ['revenue ' ] = $ all_month_totals ['total_cost ' ] = '' ;
469+ foreach ( $ month_totals as $ mt ) {
470+ $ all_month_totals ['revenue ' ] = $ all_month_totals ['revenue ' ] + $ mt ['revenue ' ];
471+ $ all_month_totals ['total_cost ' ] = $ all_month_totals ['total_cost ' ] + $ mt ['total_cost ' ];
472+ }
473+
474+ $ chart_categories = array ();
475+ $ chart_dates = array ();
476+ $ chart_contractor_fee = array ();
477+ $ chart_revenue = array ();
478+ $ chart_revenue_avg = array ();
479+ $ chart_total_cost = array ();
480+ $ chart_tasks_count = array ();
481+ $ chart_tasks_count_avg = array ();
482+
483+ foreach ( $ month_totals as $ yearmonth => $ amounts ) {
484+
485+ $ chart_categories [ $ yearmonth ] = "' " . wordwrap ( $ yearmonth , 4 , '- ' , true ) . "' " ;
486+ $ chart_dates [] = wordwrap ( $ yearmonth , 4 , '- ' , true );
487+ $ chart_contractor_fee [ $ yearmonth ] = floatval ( $ amounts ['fee_amount ' ] );
488+ $ chart_revenue [ $ yearmonth ] = floatval ( $ amounts ['revenue ' ] );
489+ $ chart_total_cost [ $ yearmonth ] = floatval ( $ amounts ['total_cost ' ] );
490+ $ chart_tasks_count [ $ yearmonth ] = intval ( $ amounts ['tasks ' ] );
491+
492+ }
493+
494+ $ chart_tasks_count_json = json_encode ( $ chart_tasks_count );
495+ $ chart_revenue_json = json_encode ( $ chart_revenue );
496+
497+ } else {
498+
499+ $ days_totals = $ this ->get_days ( $ from_day , $ from_month , $ from_year , $ to_day , $ to_month , $ to_year );
500+
501+
502+ $ max_month_totals = max ( $ days_totals );
503+ $ max_month_totals_key = array_keys ( $ days_totals , max ( $ days_totals ) );
504+ $ max_month_totals_key [0 ] = wordwrap ( $ max_month_totals_key [0 ], 6 , '- ' , true );
505+
506+ $ all_month_totals = array ();
507+ foreach ( $ days_totals as $ mt ) {
508+ if (!isset ($ all_month_totals ['revenue ' ])) { $ all_month_totals ['revenue ' ] = 0 ; }
509+ if (!isset ($ all_month_totals ['total_cost ' ])) { $ all_month_totals ['total_cost ' ] = 0 ; }
510+
511+ $ all_month_totals ['revenue ' ] = $ all_month_totals ['revenue ' ] + $ mt ['revenue ' ];
512+ $ all_month_totals ['total_cost ' ] = $ all_month_totals ['total_cost ' ] + $ mt ['total_cost ' ];
513+ }
514+
515+ $ chart_categories = array ();
516+ $ chart_dates = array ();
517+ $ chart_contractor_fee = array ();
518+ $ chart_revenue = array ();
519+ $ chart_revenue_avg = array ();
520+ $ chart_total_cost = array ();
521+ $ chart_tasks_count = array ();
522+ $ chart_tasks_count_avg = array ();
523+
524+ foreach ( $ days_totals as $ yearmonthday => $ amounts ) {
525+
526+ $ date_array = array ();
527+ $ date_array = date_parse_from_format ( 'Ymd ' , $ yearmonthday );
528+
529+ $ chart_categories [ $ yearmonthday ] = "' " . $ date_array ['year ' ] . '- ' . sprintf ( "%02d " , $ date_array ['month ' ] ) . '- ' . sprintf ( "%02d " , $ date_array ['day ' ] ) . "' " ;
530+ $ chart_dates [] = $ date_array ['year ' ] . '- ' . sprintf ( "%02d " , $ date_array ['month ' ] ) . '- ' . sprintf ( "%02d " , $ date_array ['day ' ] );
531+ $ chart_contractor_fee [ $ yearmonthday ] = floatval ( $ amounts ['fee_amount ' ] );
532+ $ chart_revenue [ $ yearmonthday ] = floatval ( $ amounts ['revenue ' ] );
533+ $ chart_total_cost [ $ yearmonthday ] = floatval ( $ amounts ['total_cost ' ] );
534+ $ chart_tasks_count [ $ yearmonthday ] = intval ( $ amounts ['tasks ' ] );
535+ }
536+
537+
538+ $ chart_tasks_count_json = json_encode ( $ chart_tasks_count );
539+ $ chart_revenue_json = json_encode ( $ chart_revenue );
540+
541+ }
542+
543+ $ chart_dates_json = json_encode ($ chart_dates );
544+
545+ $ fromDT = new DateTime ($ from_year .'- ' .$ from_month .'- ' .$ from_day );
546+ $ toDT = new DateTime ($ to_year .'- ' .$ to_month .'- ' .$ to_day );
547+
548+ $ datediff = date_diff ($ fromDT , $ toDT );
549+
550+ if ($ chart_display_method == 'months ' ) {
551+ $ datediffcount = $ datediff ->format ('%m ' ) + ($ datediff ->format ('%y ' ) * 12 ) + 1 ;
552+ }
553+ if ($ chart_display_method == 'days ' ) {
554+ $ datediffcount = $ datediff ->format ('%a ' );
555+ }
556+
557+ $ chart_revenue_avg = array_fill (0 , count ($ chart_revenue ), round (array_sum ($ chart_revenue ) / $ datediffcount , 2 ));
558+ $ chart_tasks_count_avg = array_fill (0 , count ($ chart_tasks_count ), round (array_sum ($ chart_tasks_count ) / $ datediffcount , 2 ));
559+
560+ $ stats ['averages ' ] = $ averages ;
561+ $ stats ['preferred_count ' ] = $ preferred_count ;
562+ $ stats ['chart_amounts_range ' ] = $ chart_amounts_range ;
563+ $ stats ['get_available_ranges ' ] = $ get_available_ranges ;
564+ $ stats ['type_categories ' ] = $ type_categories ;
565+ $ stats ['type_contractor_fee ' ] = $ type_contractor_fee ;
566+ $ stats ['type_revenue ' ] = $ type_revenue ;
567+ $ stats ['type_tasks_count ' ] = $ type_tasks_count ;
568+ $ stats ['type_tasks_count_json ' ] = $ type_tasks_count_json ;
569+ $ stats ['max_month_totals ' ] = $ max_month_totals ;
570+ $ stats ['max_month_totals_key ' ] = $ max_month_totals_key ;
571+ $ stats ['all_month_totals ' ] = $ all_month_totals ;
572+ $ stats ['chart_categories ' ] = $ chart_categories ;
573+ $ stats ['chart_dates ' ] = $ chart_dates ;
574+ $ stats ['chart_dates_json ' ] = $ chart_dates_json ;
575+ $ stats ['chart_contractor_fee ' ] = $ chart_contractor_fee ;
576+ $ stats ['chart_revenue ' ] = $ chart_revenue ;
577+ $ stats ['chart_revenue_avg ' ] = $ chart_revenue_avg ;
578+ $ stats ['chart_total_cost ' ] = $ chart_total_cost ;
579+ $ stats ['chart_tasks_count ' ] = $ chart_tasks_count ;
580+ $ stats ['chart_tasks_count_avg ' ] = $ chart_tasks_count_avg ;
581+ $ stats ['chart_tasks_count_json ' ] = $ chart_tasks_count_json ;
582+ $ stats ['chart_revenue_json ' ] = $ chart_revenue_json ;
583+
584+ return $ stats ;
585+ }
416586
417587 /**
418588 * Checks and sets cached data
0 commit comments