Jump to content

WHMCS 5.0 Style Bar Widgets for Monthly Completed Orders and Monthly Income


MikeDVB

Recommended Posts

Here is the version tested and working with 5.2.14. I did clean up the code a little but if anybody has any suggestions/modifications feel free to post them up.

 

http://www.mddhosting.com/files/bar_widgets_5214.tar.gz

 

My Hero, Thank you. :-)

 

Still a mystery to me why WHMCS removed this style from the main release

 

- - - Updated - - -

 

Here is the version tested and working with 5.2.14. I did clean up the code a little but if anybody has any suggestions/modifications feel free to post them up.

 

http://www.mddhosting.com/files/bar_widgets_5214.tar.gz

 

My hero! :-)

Thank you working here again.

 

Still a mystery why this style was removed from the main release.

Link to comment
Share on other sites

Same question i think this one more better then the one they have
I use the two new ones and the two old ones. They show different sets of data. The one lets me see daily orders vs daily accepted orders - the other shows daily income and expenses - the two that are in this thread show your monthly values.

 

For us it's good to see the monthly without having to delve into the reports deeply. At a glance we can see what is what.

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 year later...
Still using this in v5.3.12. Working great! Tks for a great share!

 

Question: how can I add one more year to the chart, so it will show current and the past 2 years?

 

TIA

 

Something like

<?php

if (!defined("WHMCS"))

die("This file cannot be accessed directly");

 

function annual_orders_bars($vars) {

global $_ADMINLANG,$chart;

 

$title = 'Completed Order Overview for '.date("Y");

 

$args = array();

$args['colors'] = '#888888,#FF9900,#666666';

$args['chartarea'] = '80,40,85%,70%';

$args['xlabel'] = 'Month of Year';

$args['ylabel'] = 'Number of Orders';

 

$content = $chart->drawChart('Column',chartdata_annual_orders_bars(),$args,'300px');

 

return array( 'title' => $title, 'content' => $content );

}

 

function chartdata_annual_orders_bars() {

$thisyear = date("Y");

$lastyear = date("Y",strtotime("-1 year"));

$lastyear2 = date("Y",strtotime("-2 year"));

 

$chartdata = array();

$chartdata['cols'][] = array('label'=>'Month','type'=>'string');

$chartdata['cols'][] = array('label'=>$lastyear2,'type'=>'number');

$chartdata['cols'][] = array('label'=>$lastyear,'type'=>'number');

$chartdata['cols'][] = array('label'=>$thisyear,'type'=>'number');

 

$months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

if (!$year) $year = date("Y");

 

for ( $rawmonth = 1; $rawmonth <= 12; $rawmonth++ ) {

$month = str_pad($rawmonth, 2, 0, STR_PAD_LEFT);

 

$completedorders = get_query_val("tblorders","COUNT(*)","date LIKE '$thisyear-$month-%' AND status='Active'");

$completedorders2 = get_query_val("tblorders","COUNT(*)","date LIKE '$lastyear-$month-%' AND status='Active'");

$completedorders3 = get_query_val("tblorders","COUNT(*)","date LIKE '$lastyear2-$month-%' AND status='Active'");

$chartdata['rows'][] = array('c'=>array(array('v'=>$months[$rawmonth-1]),array('v'=>(int)$completedorders3),array('v'=>(int)$completedorders2),array('v'=>(int)$completedorders)));

}

return $chartdata;

}

 

add_hook("AdminHomeWidgets",1,"annual_orders_bars");

 

?>

 

 

EDIT: Posted wrong file

Edited by ethix
Link to comment
Share on other sites

Question: how can I add one more year to the chart, so it will show current and the past 2 years?

i've had a quick play and I think the code below should work - I only starting using WHMCS in April 2013, so I have no income data before then, but it looks right to me. :idea:

 

<?php
if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

function income_overview_bars($vars) {
   global $_ADMINLANG,$chart;

   $title = 'Income Overview for '.date("Y");

   $args = array();
   $args['colors'] = '#c0c0c0,#F9D88C,#3070CF';
   $args['chartarea'] = '80,40,85%,70%';
   $args['xlabel'] = 'Month of Year';
   $args['ylabel'] = 'Default Currency';

   $content = $chart->drawChart('Column',chartdata_annual_overview_bars(),$args,'300px');

   return array( 'title' => $title, 'content' => $content );
}

function chartdata_annual_overview_bars() {
       $thisyear = date("Y");
       $lastyear = date("Y",strtotime("-1 year"));
       $yearb4 = date("Y",strtotime("-2 year"));

   $chartdata = array();
   $chartdata['cols'][] = array('label'=>'Days Range','type'=>'string');
   $chartdata['cols'][] = array('label'=>$yearb4,'type'=>'number');
   $chartdata['cols'][] = array('label'=>$lastyear,'type'=>'number');
   $chartdata['cols'][] = array('label'=>$thisyear,'type'=>'number');


   $months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

   for ( $counter = 1; $counter <= 12; $counter += 1) {
       $month = $months[$counter-1];
       $counter = str_pad($counter, 2, "0", STR_PAD_LEFT);
       $data = get_query_vals("tblaccounts","SUM(amountin/rate),SUM(fees/rate),SUM(amountout/rate)","date LIKE '$thisyear-$counter-%'");
       $amountin = $data[0];
       $fees = $data[1];
       $amountout = $data[2];
       $monthlybalance = $amountin-$fees-$amountout;
       $overallbalance += $monthlybalance;
       $prevyearbal = get_query_val("tblaccounts","SUM((amountin-fees-amountout)/rate)","date LIKE '".$lastyear."-$counter-%'");
       $prevyearbal = round($prevyearbal,2);
       $yearb4bal = get_query_val("tblaccounts","SUM((amountin-fees-amountout)/rate)","date LIKE '".$yearb4."-$counter-%'");
       $yearb4bal = round($yearb4bal,2);
       $chartdata['rows'][] = array('c'=>array(array('v'=>$month),array('v'=>$yearb4bal,'f'=>formatCurrency($yearb4bal)),array('v'=>$prevyearbal,'f'=>formatCurrency($prevyearbal)),array('v'=>$monthlybalance,'f'=>formatCurrency($monthlybalance))));
       $amountin = formatCurrency($amountin);
       $fees = formatCurrency($fees);
       $amountout = formatCurrency($amountout);
       $monthlybalance = formatCurrency($monthlybalance);
   }
   return $chartdata;
}

add_hook("AdminHomeWidgets",1,"income_overview_bars");

?>

I think between ethix and I, we have both files covered!

Edited by brian!
Link to comment
Share on other sites

just replaced annual_overview_bars.php with your code and it is working perfectly for me :)

I just tried your code - it works fine too! :)

 

however, when I copy&pasted the above code into the existing widget file, it didn't work at first. :?:

 

turns out that there's nothing wrong with your code, but when pasting code in this forum, it's best not to use the QUOTE tag as it can weirdly add spaces to the text - it did that with your code (put a space in the middle of a variable) and that's why it kept failing for me... so, it's preferabler to use PHP or CODE tags.

 

i've pasted your code below with the spaces removed for others to copy.

 

<?php
if (!defined("WHMCS"))
die("This file cannot be accessed directly");

function annual_orders_bars($vars) {
global $_ADMINLANG,$chart;

$title = 'Completed Order Overview for '.date("Y");

$args = array();
$args['colors'] = '#888888,#FF9900,#666666';
$args['chartarea'] = '80,40,85%,70%';
$args['xlabel'] = 'Month of Year';
$args['ylabel'] = 'Number of Orders';

$content = $chart->drawChart('Column',chartdata_annual_orders_bars(),$args,'300px');

return array( 'title' => $title, 'content' => $content );
}

function chartdata_annual_orders_bars() {
$thisyear = date("Y");
$lastyear = date("Y",strtotime("-1 year"));
$lastyear2 = date("Y",strtotime("-2 year"));

$chartdata = array();
$chartdata['cols'][] = array('label'=>'Month','type'=>'string');
$chartdata['cols'][] = array('label'=>$lastyear2,'type'=>'number');
$chartdata['cols'][] = array('label'=>$lastyear,'type'=>'number');
$chartdata['cols'][] = array('label'=>$thisyear,'type'=>'number');

$months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
if (!$year) $year = date("Y");

for ( $rawmonth = 1; $rawmonth <= 12; $rawmonth++ ) {
$month = str_pad($rawmonth, 2, 0, STR_PAD_LEFT);

$completedorders = get_query_val("tblorders","COUNT(*)","date LIKE '$thisyear-$month-%' AND status='Active'");
$completedorders2 = get_query_val("tblorders","COUNT(*)","date LIKE '$lastyear-$month-%' AND status='Active'");
$completedorders3 = get_query_val("tblorders","COUNT(*)","date LIKE '$lastyear2-$month-%' AND status='Active'");
$chartdata['rows'][] = array('c'=>array(array('v'=>$months[$rawmonth-1]),array('v'=>(int)$completedorders3),array('v'=>(int)$completedorders2),array('v'=>(int)$completedorders)));
}
return $chartdata;
}

add_hook("AdminHomeWidgets",1,"annual_orders_bars" );

?>

Link to comment
Share on other sites

I just tried your code - it works fine too! :)

 

however, when I copy&pasted the above code into the existing widget file, it didn't work at first. :?:

 

turns out that there's nothing wrong with your code, but when pasting code in this forum, it's best not to use the QUOTE tag as it can weirdly add spaces to the text - it did that with your code (put a space in the middle of a variable) and that's why it kept failing for me... so, it's preferabler to use PHP or CODE tags.

 

i've pasted your code below with the spaces removed for others to copy.

 

<?php
if (!defined("WHMCS"))
die("This file cannot be accessed directly");

function annual_orders_bars($vars) {
global $_ADMINLANG,$chart;

$title = 'Completed Order Overview for '.date("Y");

$args = array();
$args['colors'] = '#888888,#FF9900,#666666';
$args['chartarea'] = '80,40,85%,70%';
$args['xlabel'] = 'Month of Year';
$args['ylabel'] = 'Number of Orders';

$content = $chart->drawChart('Column',chartdata_annual_orders_bars(),$args,'300px');

return array( 'title' => $title, 'content' => $content );
}

function chartdata_annual_orders_bars() {
$thisyear = date("Y");
$lastyear = date("Y",strtotime("-1 year"));
$lastyear2 = date("Y",strtotime("-2 year"));

$chartdata = array();
$chartdata['cols'][] = array('label'=>'Month','type'=>'string');
$chartdata['cols'][] = array('label'=>$lastyear2,'type'=>'number');
$chartdata['cols'][] = array('label'=>$lastyear,'type'=>'number');
$chartdata['cols'][] = array('label'=>$thisyear,'type'=>'number');

$months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
if (!$year) $year = date("Y");

for ( $rawmonth = 1; $rawmonth <= 12; $rawmonth++ ) {
$month = str_pad($rawmonth, 2, 0, STR_PAD_LEFT);

$completedorders = get_query_val("tblorders","COUNT(*)","date LIKE '$thisyear-$month-%' AND status='Active'");
$completedorders2 = get_query_val("tblorders","COUNT(*)","date LIKE '$lastyear-$month-%' AND status='Active'");
$completedorders3 = get_query_val("tblorders","COUNT(*)","date LIKE '$lastyear2-$month-%' AND status='Active'");
$chartdata['rows'][] = array('c'=>array(array('v'=>$months[$rawmonth-1]),array('v'=>(int)$completedorders3),array('v'=>(int)$completedorders2),array('v'=>(int)$completedorders)));
}
return $chartdata;
}

add_hook("AdminHomeWidgets",1,"annual_orders_bars" );

?>

 

Somebody post a screen shot of the new widget with additional years please? I know I built the separate report to keep the front page clean but if the 'pagination' of the widget isn't intrusive I'll include it in the original version as well - perhaps allowing the user to choose if pagination is enabled or not.

Link to comment
Share on other sites

can someone test the code below for me? :)

 

it works fine for me upto 3 years, but would be good to know if it works for those with more than 3 years of whmcs income data...

 

<?php
if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

function income_overview_bars($vars) {
   global $_ADMINLANG,$chart;

   $title = 'Income Overview for '.date("Y");

   $args = array();
   $args['colors'] = '#3070CF,#F9D88C,#779500,#E8C2D4,#CBEAAD';
   $args['chartarea'] = '80,40,85%,70%';
   $args['xlabel'] = 'Month of Year';
   $args['ylabel'] = 'Default Currency';

   $content = $chart->drawChart('Column',chartdata_annual_overview_bars(),$args,'300px');

   return array( 'title' => $title, 'content' => $content );
}

function chartdata_annual_overview_bars() {

   $totalyears = 3;

   for($i = 0; $i <= $totalyears-1; $i++) {
   $chartyear[$i] = date("Y",strtotime("-$i year"));
   }

   $chartdata = array();
   $chartdata['cols'][] = array('label'=>'Days Range','type'=>'string');

   for($i = 0; $i <= $totalyears-1; $i++) {
   $chartdata['cols'][] = array('label'=>$chartyear[$i],'type'=>'number');
   }

   $months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

   for ( $counter = 1; $counter <= 12; $counter += 1) {
       $month = $months[$counter-1];
       $counter = str_pad($counter, 2, "0", STR_PAD_LEFT);
       $data = get_query_vals("tblaccounts","SUM(amountin/rate),SUM(fees/rate),SUM(amountout/rate)","date LIKE '$chartyear[0]-$counter-%'");
       $amountin = $data[0];
       $fees = $data[1];
       $amountout = $data[2];
       $monthlybalance = $amountin-$fees-$amountout;
       $overallbalance += $monthlybalance;

           for($i = 1; $i <= $totalyears-1; $i++) {
           $prevyearbal[$i] = get_query_val("tblaccounts","SUM((amountin-fees-amountout)/rate)","date LIKE '".$chartyear[$i]."-$counter-%'");
           $prevyearbal[$i] = round($prevyearbal[$i],2);
           }

       $chartdata['rows'][] = array('c'=>array(array('v'=>$month),array('v'=>$monthlybalance,'f'=>formatCurrency($monthlybalance)),array('v'=>$prevyearbal[1],'f'=>formatCurrency($prevyearbal[1])),array('v'=>$prevyearbal[2],'f'=>formatCurrency($prevyearbal[2])),array('v'=>$prevyearbal[3],'f'=>formatCurrency($prevyearbal[3])),array('v'=>$prevyearbal[4],'f'=>formatCurrency($prevyearbal[4])),array('v'=>$prevyearbal[5],'f'=>formatCurrency($prevyearbal[5]))));
       $amountin = formatCurrency($amountin);
       $fees = formatCurrency($fees);
       $amountout = formatCurrency($amountout);
       $monthlybalance = formatCurrency($monthlybalance);
   }
   return $chartdata;
}

add_hook("AdminHomeWidgets",1,"income_overview_bars");

?>

basically, i've just tweaked the code to make it more flexible for showing additional years - as its written now, it can show between 1-5 years of income... in theory, you could take it upto 10 years, but you'd then need to expand the chartdata output to show years 6-10.

 

to define the number of years to show, you only need to change the value of the $totalyears variable in the code.

 

$totalyears = 3;

you could also define it for more years than you have income data for, and that would remove the need to adjust the number annually.

Link to comment
Share on other sites

I tested up to 5 years and it works fine.

The bars end up to small though IMO also I prefer oldest to newest dates, from left to right.

thanks for testing it... five years looked slightly small to me too, though when you set it to ten years, the size of the bars are insanely narrow!

 

the only reason I changed the date sort order was because it made the programming easier - you could switch the year order, but you'd have to change the order of other things too.

 

i've trimmed some more redundant code from the widget, so it's slightly shorter now...

 

<?php
if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

function income_overview_bars($vars) {
   global $_ADMINLANG,$chart;

   $title = 'Income Overview for '.date("Y");
   $args = array();
   $args['colors'] = '#3070CF,#F9D88C,#779500,#E8C2D4,#CBEAAD';
   $args['chartarea'] = '80,40,85%,70%';
   $args['xlabel'] = 'Month of Year';
   $args['ylabel'] = 'Default Currency';
   $content = $chart->drawChart('Column',chartdata_annual_overview_bars(),$args,'300px');

   return array( 'title' => $title, 'content' => $content );
}

function chartdata_annual_overview_bars() {

   $totalyears = 3;

   for($i = 0; $i <= $totalyears-1; $i++) {
   $chartyear[$i] = date("Y",strtotime("-$i year"));
   }

   $chartdata = array();
   $chartdata['cols'][] = array('label'=>'Days Range','type'=>'string');

   for($i = 0; $i <= $totalyears-1; $i++) {
   $chartdata['cols'][] = array('label'=>$chartyear[$i],'type'=>'number');
   }

   $months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

   for ( $counter = 1; $counter <= 12; $counter += 1) {
       $month = $months[$counter-1];
       $counter = str_pad($counter, 2, "0", STR_PAD_LEFT);
           for($i = 0; $i <= $totalyears-1; $i++) {
           $monthbal[$i] = get_query_val("tblaccounts","SUM((amountin-fees-amountout)/rate)","date LIKE '".$chartyear[$i]."-$counter-%'");
           }
       $chartdata['rows'][] = array('c'=>array(array('v'=>$month),array('v'=>$monthbal[0],'f'=>$monthbal[0]),array('v'=>$monthbal[1],'f'=>$monthbal[1]),array('v'=>$monthbal[2],'f'=>$monthbal[2]),array('v'=>$monthbal[3],'f'=>$monthbal[3]),array('v'=>$monthbal[4],'f'=>$monthbal[4]),array('v'=>$monthbal[5],'f'=>$monthbal[5])));
   }
   return $chartdata;
}

add_hook("AdminHomeWidgets",1,"income_overview_bars");

?>

Link to comment
Share on other sites

Good idea! But the total amount is not correct. For instance, it's showing 5277.077059 for Dec/2011 using this last code.

The previous one works fine for 5 years.

i'm an idiot - I never noticed the amounts were displayed when you run the mouse over a bar... :roll:

 

try the following...

 

<?php
if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

function income_overview_bars($vars) {
   global $_ADMINLANG,$chart;

   $title = 'Income Overview for '.date("Y");
   $args = array();
   $args['colors'] = '#3070CF,#F9D88C,#779500,#E8C2D4,#CBEAAD';
   $args['chartarea'] = '80,40,85%,70%';
   $args['xlabel'] = 'Month of Year';
   $args['ylabel'] = 'Default Currency';
   $content = $chart->drawChart('Column',chartdata_annual_overview_bars(),$args,'300px');

   return array( 'title' => $title, 'content' => $content );
}

function chartdata_annual_overview_bars() {

   $totalyears = 3;

   for($i = 0; $i <= $totalyears-1; $i++) {
   $chartyear[$i] = date("Y",strtotime("-$i year"));
   }

   $chartdata = array();
   $chartdata['cols'][] = array('label'=>'Days Range','type'=>'string');

   for($i = 0; $i <= $totalyears-1; $i++) {
   $chartdata['cols'][] = array('label'=>$chartyear[$i],'type'=>'number');
   }

   $months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

   for ( $counter = 1; $counter <= 12; $counter += 1) {
       $month = $months[$counter-1];
       $counter = str_pad($counter, 2, "0", STR_PAD_LEFT);
           for($i = 0; $i <= $totalyears-1; $i++) {
           $monthbal[$i] = get_query_val("tblaccounts","SUM((amountin-fees-amountout)/rate)","date LIKE '".$chartyear[$i]."-$counter-%'");
           }
       $chartdata['rows'][] = array('c'=>array(array('v'=>$month),array('v'=>$monthbal[0],'f'=>formatCurrency($monthbal[0])),array('v'=>$monthbal[1],'f'=>formatCurrency($monthbal[1])),array('v'=>$monthbal[2],'f'=>formatCurrency($monthbal[2])),array('v'=>$monthbal[3],'f'=>formatCurrency($monthbal[3])),array('v'=>$monthbal[4],'f'=>formatCurrency($monthbal[4]))));
   }
   return $chartdata;
}

add_hook("AdminHomeWidgets",1,"income_overview_bars");

?>

Edited by brian!
Link to comment
Share on other sites

Question: is there a way to invert the way years are displayed, ie, instead of 2015 | 2014 | 2013.... show 2013 | 2014 | 2015?

yes - but you need to reverse the loop, reverse the order of the bars (and add validation) - you might even need to reverse the color order too. :cry:

 

<?php
if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

function income_overview_bars($vars) {
   global $_ADMINLANG,$chart;

   $title = 'Income Overview for '.date("Y");
   $args = array();
   $args['colors'] = '#3070CF,#F9D88C,#779500,#E8C2D4,#CBEAAD';
   $args['chartarea'] = '80,40,85%,70%';
   $args['xlabel'] = 'Month of Year';
   $args['ylabel'] = 'Default Currency';
   $content = $chart->drawChart('Column',chartdata_annual_overview_bars(),$args,'300px');

   return array( 'title' => $title, 'content' => $content );
}

function chartdata_annual_overview_bars() {

   $totalyears = 3;

   for($i = 0; $i <= $totalyears-1; $i++) {
   $chartyear[$i] = date("Y",strtotime("-$i year"));
   }

   $chartdata = array();
   $chartdata['cols'][] = array('label'=>'Days Range','type'=>'string');

   for($i = $totalyears-1; $i >=0 ; $i--) {
   $chartdata['cols'][] = array('label'=>$chartyear[$i],'type'=>'number');
   }

   $months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

   for ( $counter = 1; $counter <= 12; $counter += 1) {
       $month = $months[$counter-1];
       $counter = str_pad($counter, 2, "0", STR_PAD_LEFT);
           for($i = 0; $i <= $totalyears-1; $i++) {
           $monthbal[$i] = get_query_val("tblaccounts","SUM((amountin-fees-amountout)/rate)","date LIKE '".$chartyear[$i]."-$counter-%'");
           }

       if ($totalyears == 5) {
           $chartdata['rows'][] = array('c'=>array(array('v'=>$month),array('v'=>$monthbal[4],'f'=>formatCurrency($monthbal[4])),array('v'=>$monthbal[3],'f'=>formatCurrency($monthbal[3])),array('v'=>$monthbal[2],'f'=>formatCurrency($monthbal[2])),array('v'=>$monthbal[1],'f'=>formatCurrency($monthbal[1])),array('v'=>$monthbal[0],'f'=>formatCurrency($monthbal[0]))));
           }
       elseif ($totalyears == 4) {
           $chartdata['rows'][] = array('c'=>array(array('v'=>$month),array('v'=>$monthbal[3],'f'=>formatCurrency($monthbal[3])),array('v'=>$monthbal[2],'f'=>formatCurrency($monthbal[2])),array('v'=>$monthbal[1],'f'=>formatCurrency($monthbal[1])),array('v'=>$monthbal[0],'f'=>formatCurrency($monthbal[0]))));
           }
       elseif ($totalyears == 3) {
           $chartdata['rows'][] = array('c'=>array(array('v'=>$month),array('v'=>$monthbal[2],'f'=>formatCurrency($monthbal[2])),array('v'=>$monthbal[1],'f'=>formatCurrency($monthbal[1])),array('v'=>$monthbal[0],'f'=>formatCurrency($monthbal[0]))));
           }
       elseif ($totalyears == 2) {
           $chartdata['rows'][] = array('c'=>array(array('v'=>$month),array('v'=>$monthbal[1],'f'=>formatCurrency($monthbal[1])),array('v'=>$monthbal[0],'f'=>formatCurrency($monthbal[0]))));
           }
       else {
           $chartdata['rows'][] = array('c'=>array(array('v'=>$month),array('v'=>$monthbal[0],'f'=>formatCurrency($monthbal[0]))));
           }
   }
   return $chartdata;
}

add_hook("AdminHomeWidgets",1,"income_overview_bars");

?>

i'm sure there must be a better/simpler way to define $chartdata utilising the value of $totalyears - but it hasn't occurred to me yet!

Link to comment
Share on other sites

I finally figured out a way to remove the validation and make use of the $totalyears variable - makes the code much neater too. :idea:

 

 <?php
if (!defined("WHMCS"))
   die("This file cannot be accessed directly");

function income_overview_bars($vars) {
   global $_ADMINLANG,$chart;

   $title = 'Income Overview for '.date("Y");
   $args = array();
   $args['colors'] = '#3070CF,#F9D88C,#779500,#E8C2D4,#CBEAAD';
   $args['chartarea'] = '80,40,85%,70%';
   $args['xlabel'] = 'Month of Year';
   $args['ylabel'] = 'Default Currency';
   $content = $chart->drawChart('Column',chartdata_annual_overview_bars(),$args,'300px');

   return array( 'title' => $title, 'content' => $content );
}

function chartdata_annual_overview_bars() {

   $totalyears = 4;

   for($i = 0; $i <= $totalyears-1; $i++) {
   $chartyear[$i] = date("Y",strtotime("-$i year"));
   }

   $chartdata = array();
   $chartdata['cols'][] = array('label'=>'Days Range','type'=>'string');

   for($i = $totalyears-1; $i >=0 ; $i--) {
   $chartdata['cols'][] = array('label'=>$chartyear[$i],'type'=>'number');
   }

   $months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

   for ( $counter = 1; $counter <= 12; $counter += 1) {
       $month = $months[$counter-1];
       $counter = str_pad($counter, 2, "0", STR_PAD_LEFT);
           for($i = $totalyears-1; $i >=0 ; $i--) {
           $monthbal[$i] = get_query_val("tblaccounts","SUM((amountin-fees-amountout)/rate)","date LIKE '".$chartyear[$i]."-$counter-%'");
           }
       $chartdata['rows'][] = array('c'=>array(array('v'=>$month),array('v'=>$monthbal[$totalyears-1],'f'=>formatCurrency($monthbal[$totalyears-1])),array('v'=>$monthbal[$totalyears-2],'f'=>formatCurrency($monthbal[$totalyears-2])),array('v'=>$monthbal[$totalyears-3],'f'=>formatCurrency($monthbal[$totalyears-3])),array('v'=>$monthbal[$totalyears-4],'f'=>formatCurrency($monthbal[$totalyears-4])),array('v'=>$monthbal[$totalyears-5],'f'=>formatCurrency($monthbal[$totalyears-5]))));
   }
   return $chartdata;
}

add_hook("AdminHomeWidgets",1,"income_overview_bars");

?> 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated