You are here

function commerce_reports_geckoboard_revenue_monthly in Commerce Reporting 7.3

Same name and namespace in other branches
  1. 7.4 modules/geckoboard/commerce_reports_geckoboard.module \commerce_reports_geckoboard_revenue_monthly()

Sales summary

1 string reference to 'commerce_reports_geckoboard_revenue_monthly'
commerce_reports_geckoboard_geckoboardapi in modules/geckoboard/commerce_reports_geckoboard.module
Implements hook_geckoboard().

File

modules/geckoboard/commerce_reports_geckoboard.module, line 193

Code

function commerce_reports_geckoboard_revenue_monthly() {
  $res = db_query("\n    SELECT SUM(amount) AS sum,\n           AVG(amount) AS average,\n           COUNT(*) AS count,\n           DATE_FORMAT(FROM_UNIXTIME(created), '%Y-%m') AS yyyymm\n      FROM commerce_payment_transaction cpt\n     WHERE currency_code='AUD'\n       AND status='success'\n       AND created > :created\n     GROUP BY yyyymm", array(
    ':created' => REQUEST_TIME - 3600 * 24 * 28 * 2,
  ));
  $thismonth = date('Y-m');
  $lastmonth = date('Y-m', REQUEST_TIME - 86400 * 28);
  $data = $res
    ->fetchAllAssoc('yyyymm');

  // Check for slow selling stores
  if (!isset($data[$thismonth])) {
    $data[$thismonth] = (object) array(
      'sum' => 0,
      'average' => 0,
      'count' => 0,
    );
  }
  if (!isset($data[$lastmonth])) {
    $data[$lastmonth] = (object) array(
      'sum' => 0,
      'average' => 0,
      'count' => 0,
    );
  }

  // Quick and dirty projection
  $hoursthismonth = (time() - mktime(0, 0, 0, date('n'), 1)) / 60 / 60;

  // Number of hours that have passed this month
  $hoursremaining = (mktime(0, 0, 0, date('n') + 1, 1) - time()) / 60 / 60;
  $projected_raw = $data[$thismonth]->sum + $data[$thismonth]->sum / $hoursthismonth * $hoursremaining;

  // Change our units from $.01 to $1000
  $current = (int) ($data[$thismonth]->sum / 100000);
  $previous = (int) ($data[$lastmonth]->sum / 100000);
  $projected = (int) ($projected_raw / 100000);
  $upper_bound = (int) (max($projected, $previous) * 1.2);

  // Set our goals
  $red = $previous * 0.7;

  // 30% decline on last month
  $amber = $previous * 0.9;

  // 10% growth on last month
  // Generate evenly spaced points
  $ticks = range(0, $upper_bound, $upper_bound / 6);
  $ticks = array_map('intval', $ticks);

  // Generate marks corresponding to our sales data so we don't have to guess the numbers

  //$ticks = array(0, $current, $previous, $projected, $upper_bound);

  //sort($ticks);
  return array(
    'orientation' => 'horizontal',
    'item' => array(
      'label' => t('Month to date revenue'),
      'sublabel' => t('(@currency in thousands)', array(
        '@currency' => commerce_default_currency(),
      )),
      'axis' => array(
        'point' => $ticks,
      ),
      'range' => array(
        array(
          'color' => 'red',
          'start' => 0,
          'end' => $red,
        ),
        // Should use last month to define range always
        array(
          'color' => 'amber',
          'start' => $red,
          'end' => $amber,
        ),
        array(
          'color' => 'green',
          'start' => $amber,
          'end' => $upper_bound,
        ),
      ),
      'measure' => array(
        'current' => array(
          'start' => 0,
          'end' => $current,
        ),
        'projected' => array(
          'start' => 0,
          'end' => $projected,
        ),
      ),
      'comparative' => array(
        'point' => $previous,
      ),
    ),
  );
}