Last active
December 16, 2015 07:58
-
-
Save amphibian/5402076 to your computer and use it in GitHub Desktop.
devot:ee developer sales widget for Panic's StatusBoard iPad app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
devot:ee developer sales widget for Panic's StatusBoard iPad app | |
http://panic.com/statusboard | |
Author: Derek Hogue (http://amphibian.info) | |
Usage: Fill-in your devot:ee API credentials, then upload to your server. | |
You can use this script to feed either the Graph widget or Table widget. | |
Examples: | |
http://yourserver.com/devot-ee-statusboard.php | |
http://yourserver.com/devot-ee-statusboard.php?type=table | |
http://yourserver.com/devot-ee-statusboard.php?time=daily&metric=sales | |
http://yourserver.com/devot-ee-statusboard.php?graph=bar&num=6 | |
Configuration Below: | |
*/ | |
$config = array( | |
'api_key' => '', | |
'secret_key' => '', | |
'graph' => 'line', // 'line' or 'bar' - type of graph | |
'metric' => 'profit', // 'sales' or 'profit' | |
'num' => 12, // integer - number of days or months | |
'time' => 'months', // 'days', 'months', 'current_month', or 'current_year' | |
'title' => 'devot:ee Sales', | |
'totals' => false, // boolean - whether to show grand totals for each add-on | |
'type' => 'graph' // 'graph' or 'table' | |
); | |
/* | |
That's it, you're done. | |
The following config variables can be altered via $_GET: | |
- graph | |
- metric | |
- num | |
- time | |
- title | |
- totals | |
- type | |
*/ | |
if(!empty($_GET['graph']) && in_array($_GET['graph'], array('line','bar')) ) | |
{ | |
$config['graph'] = $_GET['graph']; | |
} | |
if(!empty($_GET['metric']) && in_array($_GET['metric'], array('sales','profit')) ) | |
{ | |
$config['metric'] = $_GET['metric']; | |
} | |
if(!empty($_GET['num'])) | |
{ | |
$config['num'] = intval($_GET['num']); | |
} | |
if(!empty($_GET['time']) && in_array($_GET['time'], array('days','months','current_month','current_year')) ) | |
{ | |
$config['time'] = $_GET['time']; | |
} | |
if(!empty($_GET['title'])) | |
{ | |
$config['title'] = urldecode($_GET['title']); | |
} | |
if(!empty($_GET['totals']) && in_array($_GET['totals'], array('true','false')) ) | |
{ | |
$config['totals'] = ($_GET['totals'] == 'true') ? true : false; | |
} | |
if(!empty($_GET['type']) && in_array($_GET['type'], array('graph','table')) ) | |
{ | |
$config['type'] = $_GET['type']; | |
} | |
if($sales = get_sales($config)) | |
{ | |
$data = array( | |
'graph' => array( | |
'title' => $config['title'], | |
'type' => $config['graph'], | |
'total' => $config['totals'], | |
'datasequences' => array() | |
) | |
); | |
if($config['metric'] == 'profit') | |
{ | |
$data['graph']['yAxis']['units']['prefix'] = '$'; | |
} | |
$dates = array(); | |
$addons = array(); | |
$table_data = array(); | |
$sales = array_reverse($sales['items'][0]); | |
foreach($sales as $item) | |
{ | |
$date = (in_array($config['time'], array('days','current_month'))) ? date('Y-m-d', $item['purchase_date']) : date('Y-m', $item['purchase_date']); | |
if( ! array_key_exists($item['title'], $addons) ) | |
{ | |
$addons[$item['title']] = array(); | |
} | |
if( ! array_key_exists($item['title'], $table_data) ) | |
{ | |
$addons[$item['title']] = array(); | |
} | |
if( ! in_array($date, $dates) ) | |
{ | |
$dates[] = $date; | |
} | |
if(empty($addons[$item['title']][$date])) | |
{ | |
$addons[$item['title']][$date] = sales_value($item, $config); | |
} | |
else | |
{ | |
$addons[$item['title']][$date] += sales_value($item, $config); | |
} | |
if(empty($table_data[$item['title']])) | |
{ | |
$table_data[$item['title']] = sales_value($item, $config); | |
} | |
else | |
{ | |
$table_data[$item['title']] += sales_value($item, $config); | |
} | |
} | |
ksort($addons); | |
ksort($table_data); | |
if($config['type'] == 'table') | |
{ | |
$totals = array_values($table_data); | |
rsort($totals); | |
$split = $totals[0] / 8; | |
$grand_total = ($config['metric'] == 'sales') ? array_sum($totals) : '$'.number_format(array_sum($totals), 2); | |
$r = '<table>'; | |
foreach($table_data as $addon => $total) | |
{ | |
$formatted_total = ($config['metric'] == 'sales') ? $total : '$'.number_format($total, 2); | |
$r .= "<tr><td>$addon</td><td style='width: 25%;'>$formatted_total</td><td class='projectsBars'>".get_bars($total, $split)."</td></tr>"; | |
} | |
if($config['totals'] == true) | |
{ | |
$r .= "<tr><td style='color: rgb(174,183,188)';>Total</td><td style='color: rgb(174,183,188); width: 25%;'>$grand_total</td><td class='projectsBars'><div class='barSegment value8'></div><div class='barSegment value8'></div><div class='barSegment value8'></div><div class='barSegment value8'></div><div class='barSegment value8'></div><div class='barSegment value8'></div><div class='barSegment value8'></div><div class='barSegment value8'></div></td></tr>"; | |
} | |
$r .= '</table>'; | |
echo $r; | |
exit(); | |
} | |
else | |
{ | |
$i = 0; | |
foreach($addons as $addon => $addon_sales) | |
{ | |
$data['graph']['datasequences'][$i] = array( | |
'title' => $addon, | |
'datapoints' => array() | |
); | |
foreach($dates as $date) | |
{ | |
if( ! array_key_exists($date, $addon_sales) ) | |
{ | |
$addon_sales[$date] = 0; | |
} | |
} | |
ksort($addon_sales); | |
foreach($addon_sales as $date => $total) | |
{ | |
$data['graph']['datasequences'][$i]['datapoints'][] = array( | |
'title' => (in_array($config['time'], array('days','current_month'))) ? date('n/j', strtotime($date)) : date('M', strtotime($date.'-01')), | |
'value' => ($config['metric'] == 'sales') ? $total : number_format($total, 2) | |
); | |
} | |
$i++; | |
} | |
} | |
header('Content-Type: application/json'); | |
echo(json_encode($data)); | |
exit(); | |
} | |
function get_sales($config) | |
{ | |
switch($config['time']) | |
{ | |
case 'days': | |
case 'months': | |
$start_date = date('Y-m-d', strtotime($config['num'].' '.$config['time'].' ago')); | |
break; | |
case 'current_month': | |
$start_date = date('Y-m').'-01'; | |
break; | |
case 'current_year': | |
$start_date = date('Y').'-01-01'; | |
break; | |
} | |
$end_date = date('Y-m-d'); | |
$post_string = 'api_key='.$config['api_key'].'&secret_key='.$config['secret_key'].'&start_dt='.$start_date.'&end_dt='.$end_date; | |
$ch = curl_init('https://devot-ee.com/api/orders'); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); | |
$response = urldecode(curl_exec($ch)); | |
return json_decode($response, TRUE); | |
} | |
function sales_value($item, $config) | |
{ | |
return ($config['metric'] == 'sales') ? $item['quantity'] : $item['price'] * 0.8; | |
} | |
function get_bars($total, $split) | |
{ | |
$r = ''; | |
$bars = round($total/$split); | |
if($bars == 0) $bars = 1; | |
for($bar = 1; $bar <= $bars; $bar++) | |
{ | |
$r .= '<div class="barSegment value'.$bar.'"></div>'; | |
} | |
return $r; | |
} | |
/* | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment