View source
<?php
function commerce_reports_geckoboard_geckoboardapi() {
$services = array();
$services['commerce_sales_daily'] = array(
'callback' => 'commerce_reports_geckoboard_sales_daily',
'type' => 'text',
);
$services['commerce_sales_weekly'] = array(
'callback' => 'commerce_reports_geckoboard_sales_weekly',
'type' => 'text',
);
$services['commerce_sales_monthly'] = array(
'callback' => 'commerce_reports_geckoboard_sales_monthly',
'type' => 'text',
);
$services['commerce_sales_yearly'] = array(
'callback' => 'commerce_reports_geckoboard_sales_yearly',
'type' => 'text',
);
$services['commerce_revenue_monthly'] = array(
'callback' => 'commerce_reports_geckoboard_revenue_monthly',
'type' => 'text',
);
$services['commerce_order_processing_rag'] = array(
'callback' => 'commerce_reports_geckoboard_order_processing_rag',
'type' => 'rag',
);
$services['commerce_order_status_rag'] = array(
'callback' => 'commerce_reports_geckoboard_order_status_rag',
'type' => 'rag',
);
return $services;
}
function commerce_reports_geckoboard_sales_daily() {
$basequery = db_select('commerce_line_item', 'li')
->fields('li', array(
'line_item_id',
))
->condition('li.type', 'product')
->condition('o.status', array(
'pending',
'processing',
'completed',
));
$basequery
->addExpression('SUM(quantity)', 'quantity');
$basequery
->join('commerce_order', 'o', 'o.order_id = li.order_id');
$today = mktime(0, 0, 0, date('n'), date('j'));
$query = clone $basequery;
$query
->condition('o.created', $today, '>=');
$result = $query
->execute()
->fetchField(1);
$todayunits = (int) $result;
$yesterday = $today - 86400;
$query = clone $basequery;
$query
->condition('o.created', $yesterday, '>=')
->condition('o.created', $today, '<');
$result = $query
->execute()
->fetchField(1);
$yesterdayunits = (int) $result;
$yesterday_text = t('@units yesterday', array(
'@units' => $yesterdayunits,
));
return _commerce_reports_geckoboard_texthelper($todayunits, $yesterday_text);
}
function commerce_reports_geckoboard_sales_weekly() {
$basequery = db_select('commerce_line_item', 'li')
->fields('li', array(
'line_item_id',
))
->condition('li.type', 'product')
->condition('o.status', array(
'pending',
'processing',
'completed',
));
$basequery
->addExpression('SUM(quantity)', 'quantity');
$basequery
->join('commerce_order', 'o', 'o.order_id = li.order_id');
$weekstart = strtotime(date("o-\\WW"));
$weekend = $weekstart + 604800;
$query = clone $basequery;
$query
->condition('o.created', $weekstart, '>=')
->condition('o.created', $weekend, '<');
$result = $query
->execute()
->fetchField(1);
$thisweek = (int) $result;
$lastweekend = strtotime(date("o-\\WW"));
$lastweekstart = $lastweekend - 604800;
$query = clone $basequery;
$query
->condition('o.created', $lastweekstart, '>=')
->condition('o.created', $lastweekend, '<');
$result = $query
->execute()
->fetchField(1);
$lastweek = (int) $result;
$lastweek_text = t('@units last week', array(
'@units' => $lastweek,
));
return _commerce_reports_geckoboard_texthelper($thisweek, $lastweek_text);
}
function commerce_reports_geckoboard_sales_monthly() {
$basequery = db_select('commerce_line_item', 'li')
->fields('li', array(
'line_item_id',
))
->condition('li.type', 'product')
->condition('o.status', array(
'pending',
'processing',
'completed',
));
$basequery
->addExpression('SUM(quantity)', 'quantity');
$basequery
->join('commerce_order', 'o', 'o.order_id = li.order_id');
$thismonth = mktime(0, 0, 0, date('n'), 1);
$thismonthend = mktime(0, 0, 0, date('n') + 1, 1);
$query = clone $basequery;
$query
->condition('o.created', $thismonth, '>=')
->condition('o.created', $thismonthend, '<');
$result = $query
->execute()
->fetchField(1);
$thismonth = (int) $result;
$lastmonth = mktime(0, 0, 0, date('n') - 1, 1);
$lastmonthend = mktime(0, 0, 0, date('n'), 1);
$query = clone $basequery;
$query
->condition('o.created', $lastmonth, '>=')
->condition('o.created', $lastmonthend, '<');
$result = $query
->execute()
->fetchField(1);
$lastmonth = (int) $result;
$lastmonth_text = t('@units last month', array(
'@units' => $lastmonth,
));
return _commerce_reports_geckoboard_texthelper($thismonth, $lastmonth_text);
}
function commerce_reports_geckoboard_sales_yearly() {
$basequery = db_select('commerce_line_item', 'li')
->fields('li', array(
'line_item_id',
))
->condition('li.type', 'product')
->condition('o.status', array(
'pending',
'processing',
'completed',
));
$basequery
->addExpression('SUM(quantity)', 'quantity');
$basequery
->join('commerce_order', 'o', 'o.order_id = li.order_id');
$thisyear = mktime(0, 0, 0, 1, 1, date('Y'));
$thisyearend = mktime(0, 0, 0, 1, 1, date('Y') + 1);
$query = clone $basequery;
$query
->condition('o.created', $thisyear, '>=')
->condition('o.created', $thisyearend, '<');
$result = $query
->execute()
->fetchField(1);
$thisyear = (int) $result;
$lastyear = mktime(0, 0, 0, 1, 1, date('Y') - 1);
$lastyearend = mktime(0, 0, 0, 1, 1, date('Y'));
$query = clone $basequery;
$query
->condition('o.created', $lastyear, '>=')
->condition('o.created', $lastyearend, '<');
$result = $query
->execute()
->fetchField(1);
$lastyear = (int) $result;
$lastyear_text = t('@units last year', array(
'@units' => $lastyear,
));
return _commerce_reports_geckoboard_texthelper($thisyear, $lastyear_text);
}
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');
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,
);
}
$hoursthismonth = (time() - mktime(0, 0, 0, date('n'), 1)) / 60 / 60;
$hoursremaining = (mktime(0, 0, 0, date('n') + 1, 1) - time()) / 60 / 60;
$projected_raw = $data[$thismonth]->sum + $data[$thismonth]->sum / $hoursthismonth * $hoursremaining;
$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);
$red = $previous * 0.7;
$amber = $previous * 0.9;
$ticks = range(0, $upper_bound, $upper_bound / 6);
$ticks = array_map('intval', $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,
),
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,
),
),
);
}
function commerce_reports_geckoboard_order_processing_rag() {
$periods = array(
REQUEST_TIME - 86400 => REQUEST_TIME,
REQUEST_TIME - 86400 * 2 => REQUEST_TIME - 86400,
0 => REQUEST_TIME - 86400 * 2,
);
$stats = array();
foreach ($periods as $start => $end) {
$result = db_select('commerce_order', 'o')
->condition('o.status', 'processing')
->condition('o.created', $start, '>')
->condition('o.created', $end, '<=')
->countQuery()
->execute()
->fetchField();
$stats[] = (int) $result;
}
return array(
'item' => array(
array(
'value' => $stats[2],
'text' => t('processing orders older than 48h'),
),
array(
'value' => $stats[1],
'text' => t('processing orders older than 24h'),
),
array(
'value' => $stats[0],
'text' => t('processing orders within the last 24h'),
),
),
);
}
function commerce_reports_geckoboard_order_status_rag() {
$statuses = array(
'review',
'pending',
'processing',
);
$stats = array();
foreach ($statuses as $status) {
$result = db_select('commerce_order', 'o')
->condition('o.status', $status)
->countQuery()
->execute()
->fetchField();
$stats[$status] = (int) $result;
}
return array(
'item' => array(
array(
'value' => $stats['review'],
'text' => t('orders requiring review'),
),
array(
'value' => $stats['pending'],
'text' => t('pending orders'),
),
array(
'value' => $stats['processing'],
'text' => t('processing orders'),
),
),
);
}
function _commerce_reports_geckoboard_texthelper($primary, $secondary) {
$markup = sprintf('<div class="t-size-x60">%s</div><div class="t-secondary">%s</div>', $primary, $secondary);
return array(
'item' => array(
array(
"text" => $markup,
"type" => 0,
),
),
);
}