| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPPIZZA_REPORTS Class |
| 4 |
* |
| 5 |
* @package WPPIZZA |
| 6 |
* @subpackage Submenu Pages / Classes / Reports |
| 7 |
* @copyright Copyright (c) 2015, Oliver Bach |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 3.0 |
| 10 |
* |
| 11 |
* |
| 12 |
*/ |
| 13 |
if ( ! defined( 'ABSPATH' ) ) exit;/*Exit if accessed directly*/ |
| 14 |
|
| 15 |
|
| 16 |
/************************************************************************************************************************ |
| 17 |
* |
| 18 |
* |
| 19 |
* WPPIZZA_REPORTS |
| 20 |
* |
| 21 |
* |
| 22 |
************************************************************************************************************************/ |
| 23 |
class WPPIZZA_REPORTS{ |
| 24 |
/* |
| 25 |
* class ident |
| 26 |
* @var str |
| 27 |
* @since 3.0 |
| 28 |
*/ |
| 29 |
private $class_key='reports';/*to help consistency throughout class in various places*/ |
| 30 |
/* |
| 31 |
* saved wppizza option array key - not required here |
| 32 |
* @var str |
| 33 |
* @since 3.0 |
| 34 |
*/ |
| 35 |
//private $option_key='reports'; |
| 36 |
/* |
| 37 |
* titles/lables |
| 38 |
* @var str |
| 39 |
* @since 3.0 |
| 40 |
*/ |
| 41 |
private $submenu_page_header; |
| 42 |
private $submenu_page_title; |
| 43 |
private $submenu_caps_title; |
| 44 |
private $submenu_link_label; |
| 45 |
private $submenu_priority = 110; |
| 46 |
/* |
| 47 |
define for php8.2+ happiness |
| 48 |
@since 3.17.2 |
| 49 |
*/ |
| 50 |
private $report_data_set; |
| 51 |
/****************************************************************************************************************** |
| 52 |
* |
| 53 |
* [CONSTRUCTOR] |
| 54 |
* |
| 55 |
* Setup wppizza_meal_sizes subpage |
| 56 |
* @since 3.0 |
| 57 |
* |
| 58 |
******************************************************************************************************************/ |
| 59 |
|
| 60 |
function __construct() { |
| 61 |
|
| 62 |
/*titles/labels throughout class*/ |
| 63 |
add_action('init', array( $this, 'init_admin_lables') ); |
| 64 |
|
| 65 |
/** registering submenu page -> priority 110 **/ |
| 66 |
add_action( 'admin_menu', array( $this, 'wppizza_register_submenu_page'), $this->submenu_priority ); |
| 67 |
|
| 68 |
/*register capabilities for this page*/ |
| 69 |
add_filter('wppizza_filter_define_caps', array( $this, 'wppizza_filter_define_caps'), $this->submenu_priority); |
| 70 |
/**enqueue css/js**/ |
| 71 |
add_action('admin_enqueue_scripts', array( $this, 'wppizza_enqueue_admin_scripts_and_styles')); |
| 72 |
|
| 73 |
/* redirect to 30d report on load */ |
| 74 |
add_action('current_screen', array( $this, 'wppizza_30d_report_onload'), 9 ); |
| 75 |
|
| 76 |
/*execute some helper functions once to use their return multiple times */ |
| 77 |
add_action('current_screen', array( $this, 'wppizza_add_helpers') ); |
| 78 |
|
| 79 |
/**admin ajax**/ |
| 80 |
add_action('wp_ajax_wppizza_admin_'.$this->class_key.'_ajax', array($this, 'set_admin_ajax') ); |
| 81 |
} |
| 82 |
|
| 83 |
/****************** |
| 84 |
* @since 3.0.26 |
| 85 |
* [admin ajax include file] |
| 86 |
*******************/ |
| 87 |
public function init_admin_lables(){ |
| 88 |
/*titles/labels throughout class*/ |
| 89 |
$this->submenu_page_header = apply_filters('wppizza_filter_admin_label_page_header_'.$this->class_key.'', __('Reports','wppizza-admin')); |
| 90 |
$this->submenu_page_title = apply_filters('wppizza_filter_admin_label_page_title_'.$this->class_key.'', __('Manage Reports','wppizza-admin')); |
| 91 |
$this->submenu_caps_title = apply_filters('wppizza_filter_admin_label_caps_title_'.$this->class_key.'', __('Reports','wppizza-admin')); |
| 92 |
$this->submenu_link_label = apply_filters('wppizza_filter_admin_label_link_label_'.$this->class_key.'', __('· Reports','wppizza-admin')); |
| 93 |
} |
| 94 |
|
| 95 |
/****************** |
| 96 |
* @since 3.0 |
| 97 |
* [admin ajax include file] |
| 98 |
*******************/ |
| 99 |
public function set_admin_ajax(){ |
| 100 |
require(WPPIZZA_PATH.'ajax/admin.ajax.wppizza.php'); |
| 101 |
die(); |
| 102 |
} |
| 103 |
|
| 104 |
/********************************************************* |
| 105 |
* |
| 106 |
* [default (redirect) to the last 30 days report here as opposed to |
| 107 |
* the full overview on initial load when coming to the reports page from elsewhere] |
| 108 |
* @since 3.12.14 |
| 109 |
* |
| 110 |
*********************************************************/ |
| 111 |
public function wppizza_30d_report_onload($current_screen){ |
| 112 |
|
| 113 |
if($current_screen->id == ''.WPPIZZA_POST_TYPE.'_page_'.$this->class_key.'' && $current_screen->post_type == WPPIZZA_POST_TYPE){ |
| 114 |
|
| 115 |
|
| 116 |
$referrer_query = array(); |
| 117 |
if(!empty($_SERVER['HTTP_REFERER'])){ |
| 118 |
parse_str($_SERVER['HTTP_REFERER'], $referrer_query); |
| 119 |
} |
| 120 |
|
| 121 |
$request_query = array(); |
| 122 |
if(!empty($_SERVER['REQUEST_URI'])){ |
| 123 |
parse_str($_SERVER['REQUEST_URI'], $request_query); |
| 124 |
} |
| 125 |
|
| 126 |
if( ( empty($referrer_query['page']) || $referrer_query['page'] != $this->class_key ) && empty($request_query['report']) ) { |
| 127 |
wp_safe_redirect(admin_url('/edit.php?post_type=' . WPPIZZA_SLUG .'&page='.$this->class_key.'&report=30d')); |
| 128 |
exit(); |
| 129 |
} |
| 130 |
|
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/********************************************************* |
| 135 |
* |
| 136 |
* [add global helpers and enque js] |
| 137 |
* @since 3.0 |
| 138 |
* |
| 139 |
*********************************************************/ |
| 140 |
public function wppizza_add_helpers($current_screen){ |
| 141 |
if($current_screen->id == ''.WPPIZZA_POST_TYPE.'_page_'.$this->class_key.'' && $current_screen->post_type == WPPIZZA_POST_TYPE){ |
| 142 |
/** |
| 143 |
[get data set] |
| 144 |
**/ |
| 145 |
$export = !empty($_GET['export']) ? true : false; |
| 146 |
$this->report_data_set = WPPIZZA() -> sales_data ->wppizza_report_dataset($export); |
| 147 |
/** |
| 148 |
[export] |
| 149 |
**/ |
| 150 |
if($export){ |
| 151 |
$this->wppizza_report_export($this->report_data_set); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
/********************************************************* |
| 158 |
* |
| 159 |
* [add scripts and styles for reports screen] |
| 160 |
* |
| 161 |
*********************************************************/ |
| 162 |
function wppizza_enqueue_admin_scripts_and_styles(){ |
| 163 |
global $current_screen, $wp_styles, $wp_scripts, $wppizza_options; |
| 164 |
/**include reporting js**/ |
| 165 |
if($current_screen->id == ''.WPPIZZA_POST_TYPE.'_page_'.$this->class_key.'' && $current_screen->post_type == WPPIZZA_POST_TYPE){ |
| 166 |
|
| 167 |
/* Get the WP built-in jquery-ui-core version to use for jquery ui*/ |
| 168 |
$jquery_ui_core_version = $wp_scripts->registered['jquery-ui-core']->ver; |
| 169 |
|
| 170 |
/************ |
| 171 |
css |
| 172 |
***********/ |
| 173 |
/* |
| 174 |
datepicker css |
| 175 |
load a localised (gdpr) version (currently only smoothness supported) here too if we load it in the frontend |
| 176 |
*/ |
| 177 |
$ui_style = $wppizza_options['order_settings']['order_page_quantity_change_style']; |
| 178 |
$isLocal = substr($ui_style, -5) == '.gdpr' ? true : false ; |
| 179 |
$stylePath = $isLocal ? ''. WPPIZZA_URL . 'css/ui/smoothness.css' : '//code.jquery.com/ui/'.$jquery_ui_core_version .'/themes/smoothness/jquery-ui.min.css'; |
| 180 |
wp_enqueue_style('jquery-style', $stylePath, array(), $jquery_ui_core_version); |
| 181 |
|
| 182 |
/** chosen **/ |
| 183 |
wp_register_style(WPPIZZA_SLUG.'-chosen', plugins_url( 'css/wppizza-chosen.min.css', WPPIZZA_PLUGIN_PATH ), array(), WPPIZZA_VERSION); |
| 184 |
wp_enqueue_style(WPPIZZA_SLUG.'-chosen'); |
| 185 |
|
| 186 |
/************ |
| 187 |
js |
| 188 |
***********/ |
| 189 |
wp_enqueue_script('jquery-ui-datepicker'); |
| 190 |
|
| 191 |
/** chosen **/ |
| 192 |
wp_register_script(WPPIZZA_SLUG.'-chosen', plugins_url( 'js/chosen.jquery.min.js', WPPIZZA_PLUGIN_PATH ), array('jquery'), WPPIZZA_VERSION ,true); |
| 193 |
wp_enqueue_script(WPPIZZA_SLUG.'-chosen'); |
| 194 |
|
| 195 |
wp_register_script(WPPIZZA_SLUG.'_'.$this->class_key.'', plugins_url( 'js/scripts.admin.'.$this->class_key.'.js', WPPIZZA_PLUGIN_PATH ), array('jquery'), WPPIZZA_VERSION ,true); |
| 196 |
wp_enqueue_script(WPPIZZA_SLUG.'_'.$this->class_key.''); |
| 197 |
|
| 198 |
wp_register_script(WPPIZZA_SLUG.'-flot', plugins_url( 'js/jquery.flot.min.js', WPPIZZA_PLUGIN_PATH ), array('jquery'), WPPIZZA_VERSION ,true); |
| 199 |
wp_enqueue_script(WPPIZZA_SLUG.'-flot'); |
| 200 |
|
| 201 |
wp_register_script(WPPIZZA_SLUG.'-flotcats', plugins_url( 'js/jquery.flot.categories.min.js', WPPIZZA_PLUGIN_PATH ), array('jquery'), WPPIZZA_VERSION ,true); |
| 202 |
wp_enqueue_script(WPPIZZA_SLUG.'-flotcats'); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/********************************************************* |
| 207 |
* |
| 208 |
* [register submenu page] |
| 209 |
* @since 3.0 |
| 210 |
* |
| 211 |
*********************************************************/ |
| 212 |
public function wppizza_register_submenu_page(){ |
| 213 |
$submenu_page= array( |
| 214 |
'url' => 'edit.php?post_type='.WPPIZZA_SLUG.'', |
| 215 |
'title' => ''.WPPIZZA_NAME.' '.$this->submenu_page_title, |
| 216 |
'link_label' => $this->submenu_link_label, |
| 217 |
'caps' => 'wppizza_cap_'.$this->class_key.'', |
| 218 |
'key' => $this->class_key, |
| 219 |
'callback' => array($this, 'wppizza_admin_manage_sections') |
| 220 |
); |
| 221 |
/**add submenu page**/ |
| 222 |
$wppizza_submenu_page=add_submenu_page($submenu_page['url'], $submenu_page['title'], $submenu_page['link_label'], $submenu_page['caps'], $submenu_page['key'], $submenu_page['callback']); |
| 223 |
} |
| 224 |
/********************************************************* |
| 225 |
* |
| 226 |
* [echo manage settings] |
| 227 |
* |
| 228 |
* wrap settings sections into div->form |
| 229 |
* add uniquely identifiable id's / classes |
| 230 |
* add h2 text |
| 231 |
* add uniquely identifiable hidden input |
| 232 |
* add submit button |
| 233 |
* |
| 234 |
* @since 3.0 |
| 235 |
* @return str |
| 236 |
* |
| 237 |
*********************************************************/ |
| 238 |
public function wppizza_admin_manage_sections(){ |
| 239 |
|
| 240 |
/* |
| 241 |
wppizza post type only |
| 242 |
*/ |
| 243 |
$screen = get_current_screen(); |
| 244 |
if($screen->post_type != WPPIZZA_POST_TYPE){return;} |
| 245 |
|
| 246 |
|
| 247 |
/**wrap settings sections into div->form */ |
| 248 |
echo'<div id="'.WPPIZZA_SLUG.'-'.$this->class_key.'" class="'.WPPIZZA_SLUG.'-wrap '.WPPIZZA_SLUG.'-'.$this->class_key.'-wrap">'; |
| 249 |
|
| 250 |
echo"<div class='".WPPIZZA_SLUG."-admin-pageheader'>"; |
| 251 |
echo"<h2><span id='".WPPIZZA_SLUG."-header'>".WPPIZZA_NAME." ".$this->submenu_page_header."</span></h2>"; |
| 252 |
echo"</div>"; |
| 253 |
|
| 254 |
echo $this->wppizza_report_range_select($this->report_data_set); |
| 255 |
|
| 256 |
echo $this->wppizza_report_markup($this->report_data_set); |
| 257 |
$this->wppizza_report_js($this->report_data_set); |
| 258 |
|
| 259 |
echo'</div>'; |
| 260 |
} |
| 261 |
|
| 262 |
/********************************************************* |
| 263 |
* |
| 264 |
* [range and export selection header ] |
| 265 |
* |
| 266 |
* @since unknown |
| 267 |
* @param array |
| 268 |
* @return str |
| 269 |
* |
| 270 |
*********************************************************/ |
| 271 |
private function wppizza_report_range_select($data_set){ |
| 272 |
/* |
| 273 |
sanitise some get vars |
| 274 |
*/ |
| 275 |
$sanitised = wppizza_sanitize_get_vars( |
| 276 |
array( |
| 277 |
'report' => 'wppizza_validate_alpha_only', |
| 278 |
'from' => 'wppizza_validate_alpha_only', |
| 279 |
'to' => 'wppizza_validate_alpha_only', |
| 280 |
) |
| 281 |
); |
| 282 |
//sanitise from/to dates even more for it to be something useful in case someone really wants to mess around with get vars for some reason |
| 283 |
$getFrom = !empty($_GET['from']) && $_GET['from'] == wppizza_validate_date(''.$_GET['from'].'', 'Y-m-d') ? $_GET['from'] : ''; |
| 284 |
$getTo = !empty($_GET['to']) && $_GET['to'] == wppizza_validate_date(''.$_GET['to'].'', 'Y-m-d') ? $_GET['to'] : ''; |
| 285 |
|
| 286 |
/** |
| 287 |
make some vars to use |
| 288 |
**/ |
| 289 |
$selectedReport = !empty($_GET['report']) ? $_GET['report'] : ''; |
| 290 |
|
| 291 |
$exportLabel=($data_set['view']=='ini') ? __('Export All','wppizza-admin') : __('Export Range','wppizza-admin'); |
| 292 |
|
| 293 |
$output=''; |
| 294 |
$output.='<div id="wppizza-reports-range">'; |
| 295 |
|
| 296 |
/* |
| 297 |
range selection |
| 298 |
*/ |
| 299 |
$output.='<span id="wppizza-reports-range-select">'; |
| 300 |
$output.='<select id="wppizza-reports-set-range">'; |
| 301 |
$output.='<option value="" >'.__('Overview','wppizza-admin').'</option>'; |
| 302 |
foreach($data_set['reportTypes'] as $rkey => $rArr){ |
| 303 |
$sel = ($selectedReport==$rkey) ? 'selected="selected"' : '' ; |
| 304 |
$output.='<option value="'.$rkey.'" '.$sel.'>'.$rArr['lbl'].'</option>'; |
| 305 |
} |
| 306 |
if(!empty($getFrom) && !empty($getTo)){ |
| 307 |
$output.='<option selected="selected">'.__('Custom Range','wppizza-admin').'</option>'; |
| 308 |
} |
| 309 |
$output.='</select>'; |
| 310 |
$output.='</span>'; |
| 311 |
|
| 312 |
/* |
| 313 |
date selection |
| 314 |
*/ |
| 315 |
$output.='<span id="wppizza-reports-range-set">'; |
| 316 |
$output.=''.__('Custom Range','wppizza-admin').' : '; |
| 317 |
$output.='<input type="text" size="9" placeholder="yyyy-mm-dd" value="'.$getFrom.'" name="wppizza_reports_start_date" id="wppizza_reports_start_date" readonly="readonly" />'; |
| 318 |
$output.='<input type="text" size="9" placeholder="yyyy-mm-dd" value="'.$getTo.'" name="wppizza_reports_end_date" id="wppizza_reports_end_date" readonly="readonly" />'; |
| 319 |
$output.='<input type="button" class="button" value="'.__('Go','wppizza-admin').'" id="wppizza_reports_custom_range" />'; |
| 320 |
$output.='</span>'; |
| 321 |
|
| 322 |
|
| 323 |
/* |
| 324 |
export selection and button |
| 325 |
*/ |
| 326 |
$output.='<span id="wppizza-reports-range-export">'; |
| 327 |
|
| 328 |
/* button */ |
| 329 |
$output.='<input type="button" class="button" value="'.$exportLabel.'" id="wppizza_reports_export" />'; |
| 330 |
|
| 331 |
/* |
| 332 |
allow filterable export reports selection |
| 333 |
*/ |
| 334 |
$export_type = apply_filters('wppizza_filter_csv_export_select' , array('default' => __('Summary','wppizza-admin'))); |
| 335 |
if(!empty($export_type)){ |
| 336 |
$output.='<select id="wppizza_reports_export_type" name="wppizza_reports_export_type">'; |
| 337 |
foreach($export_type as $select_value => $select_label){ |
| 338 |
$output.='<option value="'.wppizza_latin_lowercase($select_value).'">'.wp_strip_all_tags($select_label, true).'</option>'; |
| 339 |
} |
| 340 |
$output.='</select>'; |
| 341 |
} |
| 342 |
|
| 343 |
$output.='</span>'; |
| 344 |
|
| 345 |
|
| 346 |
$output.='</div>'; |
| 347 |
|
| 348 |
return $output; |
| 349 |
} |
| 350 |
|
| 351 |
private function wppizza_report_markup($data_set){ |
| 352 |
|
| 353 |
$output=array(); |
| 354 |
$output[]='<!-- boxes and graphs -->'; |
| 355 |
$output[]='<div id="wppizza-reports-details">'; |
| 356 |
|
| 357 |
$output[]='<!-- sidebar boxes -->'; |
| 358 |
$output[]='<div id="wppizza-sidebar-reports" class="wppizza-sidebar">'; |
| 359 |
foreach($data_set['boxes'] as $vals){ |
| 360 |
$output[]='<div id="'.$vals['id'].'" class="postbox wppizza-reports-postbox">'; |
| 361 |
$output[]='<h3 class="button">'.$vals['lbl'].'</h3>'; |
| 362 |
$output[]=''.$vals['val'].''; |
| 363 |
$output[]='</div>'; |
| 364 |
} |
| 365 |
$output[]='</div>'; |
| 366 |
|
| 367 |
$output[]='<!-- flot graphs -->'; |
| 368 |
$output[]='<div id="wppizza-reports-canvas-wrap">'; |
| 369 |
$output[]='<h4>'.$data_set['graphs']['label'].'</h4>'; |
| 370 |
$output[]='<div style="min-height:150px" id="wppizza-reports-canvas"></div>'; |
| 371 |
$output[]='<ul id="wppizza-report-choices"></ul>'; |
| 372 |
$output[]='</div>'; |
| 373 |
|
| 374 |
|
| 375 |
$output[]='<div id="wppizza-sidebar-reports-right" class="wppizza-sidebar-right">'; |
| 376 |
foreach($data_set['boxesrt'] as $vals){ |
| 377 |
$output[]='<div id="'.$vals['id'].'" class="postbox wppizza-reports-postbox-right '.$vals['class'].'">'; |
| 378 |
$output[]='<h3 class="button">'.$vals['lbl'].'</h3>'; |
| 379 |
$output[]=''.$vals['val'].''; |
| 380 |
$output[]='</div>'; |
| 381 |
} |
| 382 |
$output[]='</div>'; |
| 383 |
|
| 384 |
|
| 385 |
$output[]='</div>'; |
| 386 |
/*implode*/ |
| 387 |
$output = implode(PHP_EOL, $output); |
| 388 |
|
| 389 |
return $output; |
| 390 |
} |
| 391 |
|
| 392 |
private function wppizza_report_js($data_set){ |
| 393 |
?> |
| 394 |
<script> |
| 395 |
jQuery(document).ready(function($){ |
| 396 |
$(function() { |
| 397 |
var datasets = { |
| 398 |
<?php |
| 399 |
$i=0; |
| 400 |
foreach($data_set['graphs']['data'] as $gk=>$gv){ |
| 401 |
if($i>0){print",";}; |
| 402 |
print'"'.$gk.'":{'.$gv.'}'; |
| 403 |
$i++; |
| 404 |
} |
| 405 |
?> |
| 406 |
}; |
| 407 |
/*********tooltip hover*****/ |
| 408 |
$("<div id='wppizza-reports-tooltip'></div>").appendTo("body"); |
| 409 |
$("#wppizza-reports-canvas").bind("plothover", function (event, pos, item) { |
| 410 |
if (item) { |
| 411 |
var x = item.datapoint[0], |
| 412 |
y = item.datapoint[1].toFixed(2); |
| 413 |
|
| 414 |
$("#wppizza-reports-tooltip").html(y) |
| 415 |
.css({top: item.pageY-<?php echo $data_set['graphs']['hoverOffsetTop'] ?>, left: item.pageX+<?php echo $data_set['graphs']['hoverOffsetLeft'] ?>}) |
| 416 |
.fadeIn(200); |
| 417 |
} else { |
| 418 |
$("#wppizza-reports-tooltip").hide(); |
| 419 |
} |
| 420 |
}); |
| 421 |
/************colours***************/ |
| 422 |
var i = 1; |
| 423 |
$.each(datasets, function(key, val) { |
| 424 |
val.color = i; |
| 425 |
++i; |
| 426 |
}); |
| 427 |
/************radios***************/ |
| 428 |
var choiceContainer = $("#wppizza-report-choices"); |
| 429 |
$.each(datasets, function(key, val) { |
| 430 |
if(key=='sales_value'){var valchkd='checked="checked"';}else{var valchkd='';} |
| 431 |
choiceContainer.append("<li><label for='" + key + "'><input type='radio' name='wppizza-graph-select' "+valchkd+" id='" + key + "' />"+ val.label + "</label></li>"); |
| 432 |
}); |
| 433 |
choiceContainer.find("input").click(plotAccordingToChoices); |
| 434 |
/************format legend***************/ |
| 435 |
function legendFormatter(v, axis) { |
| 436 |
if(axis.n==1){ |
| 437 |
return "<?php echo $data_set['currency'] ?> "+v.toFixed(2); |
| 438 |
}else{ |
| 439 |
return v.toFixed(0); |
| 440 |
} |
| 441 |
} |
| 442 |
/************plot***************/ |
| 443 |
function plotAccordingToChoices() { |
| 444 |
var data = []; |
| 445 |
choiceContainer.find("input:checked").each(function () { |
| 446 |
var key = $(this).attr("id"); |
| 447 |
if (key && datasets[key]) { |
| 448 |
data.push(datasets[key]); |
| 449 |
} |
| 450 |
}); |
| 451 |
if (data.length > 0) { |
| 452 |
$.plot("#wppizza-reports-canvas", data,{ |
| 453 |
series: { |
| 454 |
lines: { |
| 455 |
show: <?php echo $data_set['graphs']['series']['lines'] ?> |
| 456 |
}, |
| 457 |
bars: { |
| 458 |
show: <?php echo $data_set['graphs']['series']['bars'] ?>, |
| 459 |
barWidth: 0.6, |
| 460 |
align: "center" |
| 461 |
}, |
| 462 |
points: { |
| 463 |
show: <?php echo $data_set['graphs']['series']['points'] ?> |
| 464 |
} |
| 465 |
}, |
| 466 |
grid: { |
| 467 |
hoverable: true |
| 468 |
}, |
| 469 |
xaxis: { |
| 470 |
mode: "categories" |
| 471 |
}, |
| 472 |
yaxis: { |
| 473 |
min:0, |
| 474 |
tickDecimals: 0, |
| 475 |
tickFormatter: legendFormatter |
| 476 |
} |
| 477 |
}); |
| 478 |
} |
| 479 |
} |
| 480 |
plotAccordingToChoices(); |
| 481 |
}); |
| 482 |
}); |
| 483 |
</script> |
| 484 |
<?php |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
/********************************************************* |
| 489 |
* |
| 490 |
* [define caps] |
| 491 |
* @since 3.0 |
| 492 |
* |
| 493 |
*********************************************************/ |
| 494 |
function wppizza_filter_define_caps($caps){ |
| 495 |
/**add editing capability for this page**/ |
| 496 |
$caps[$this->class_key]=array('name'=>$this->submenu_caps_title ,'cap'=>'wppizza_cap_'.$this->class_key.''); |
| 497 |
return $caps; |
| 498 |
} |
| 499 |
|
| 500 |
/********************* |
| 501 |
export |
| 502 |
********************/ |
| 503 |
function wppizza_report_export($report_data, $delimiter = ','){ |
| 504 |
|
| 505 |
/************************************** |
| 506 |
generate custom reports by filter |
| 507 |
@since 3.9 |
| 508 |
filter: |
| 509 |
@param empty str |
| 510 |
@param report data array |
| 511 |
@return str (csv format) |
| 512 |
**************************************/ |
| 513 |
if(!empty($_GET['type']) && $_GET['type'] != 'default'){ |
| 514 |
|
| 515 |
/* |
| 516 |
sanitise $_GET['type'] var |
| 517 |
*/ |
| 518 |
$type = wppizza_latin_lowercase($_GET['type']); |
| 519 |
|
| 520 |
/* |
| 521 |
start with an empty string here |
| 522 |
*/ |
| 523 |
$csv = apply_filters('wppizza_filter_csv_export_'.$type.'', '', $report_data, $type ); |
| 524 |
|
| 525 |
/* |
| 526 |
write to file setting headers and |
| 527 |
triggering save file dialogue |
| 528 |
*/ |
| 529 |
$this->do_report_export($csv); |
| 530 |
/* |
| 531 |
make sure to exit |
| 532 |
*/ |
| 533 |
exit(); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
/* |
| 538 |
export your own report if you want |
| 539 |
*/ |
| 540 |
do_action('wppizza_custom_report', $report_data); |
| 541 |
|
| 542 |
$currency = $report_data['currency']; |
| 543 |
$dataset = $report_data['dataset']; |
| 544 |
|
| 545 |
/** |
| 546 |
get first and last date |
| 547 |
or make upi a range label from get vars |
| 548 |
**/ |
| 549 |
$d=0; |
| 550 |
if(!empty($dataset['sales'])){ |
| 551 |
foreach($dataset['sales'] as $date => $order){ |
| 552 |
if($d==0){ |
| 553 |
$startdate=$date; |
| 554 |
}else{ |
| 555 |
$enddate=$date; |
| 556 |
} |
| 557 |
$d++; |
| 558 |
} |
| 559 |
/**in case start and end are the same**/ |
| 560 |
$enddate=empty($enddate) ? $startdate : $enddate; |
| 561 |
/** range label **/ |
| 562 |
$range_label = ''.$startdate.' - '.$enddate.''; |
| 563 |
|
| 564 |
}else{ |
| 565 |
$range_label = ''.sanitize_text_field($_GET['name']).''; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
/************************************************************************** |
| 570 |
sales by date |
| 571 |
**************************************************************************/ |
| 572 |
$result['sales_by_date']='"Range: '.$range_label.'"'.PHP_EOL.PHP_EOL; |
| 573 |
$result['sales_by_date'].='"'.__('sales by dates','wppizza-admin').'"'.PHP_EOL; |
| 574 |
/*sales*/ |
| 575 |
$result['sales_by_date'].='"'.__('date','wppizza-admin').'", "'.__('sales value(incl. taxes, charges, discounts, tips)','wppizza-admin').'", "'.__('items order value','wppizza-admin').'", "'.__('number of sales','wppizza-admin').'", "'.__('number of items sold','wppizza-admin').'" , "'.__('tax on order','wppizza-admin').'" , "'.__('tips','wppizza-admin').'" '.PHP_EOL; |
| 576 |
$d=0; |
| 577 |
/**sum it up*/ |
| 578 |
$sales_value_total=0; |
| 579 |
$items_value_total=0; |
| 580 |
$sales_count_total=0; |
| 581 |
$items_count_total=0; |
| 582 |
$sales_order_tax=0; |
| 583 |
$tips_value_total=0; |
| 584 |
foreach($dataset['sales'] as $date=>$order){ |
| 585 |
$result['sales_by_date'].=$date . $delimiter . $order['sales_value_total'] . $delimiter . $order['items_value_total'] . $delimiter . $order['sales_count_total'] . $delimiter . $order['items_count_total'] . $delimiter . $order['sales_order_tax']. $delimiter . $order['tips_value_total']; |
| 586 |
$result['sales_by_date'].=PHP_EOL; |
| 587 |
|
| 588 |
/**add it up**/ |
| 589 |
$sales_value_total+=$order['sales_value_total']; |
| 590 |
$items_value_total+=$order['items_value_total']; |
| 591 |
$sales_count_total+=$order['sales_count_total']; |
| 592 |
$items_count_total+=$order['items_count_total']; |
| 593 |
$sales_order_tax+=$order['sales_order_tax']; |
| 594 |
$tips_value_total+=$order['tips_value_total']; |
| 595 |
|
| 596 |
$d++; |
| 597 |
} |
| 598 |
/**sums of it all*/ |
| 599 |
$result['sales_by_date'].='"", "'.__('total','wppizza-admin').'", "'.__('total','wppizza-admin').'", "'.__('total','wppizza-admin').'", "'.__('total','wppizza-admin').'", "'.__('total','wppizza-admin').'", "'.__('total','wppizza-admin').'" '.PHP_EOL; |
| 600 |
$result['sales_by_date'].=''. $delimiter . $sales_value_total . $delimiter . $items_value_total . $delimiter . $sales_count_total . $delimiter . $items_count_total . $delimiter . $sales_order_tax . $delimiter . $tips_value_total; |
| 601 |
|
| 602 |
|
| 603 |
/************************************************************************** |
| 604 |
sales by item |
| 605 |
**************************************************************************/ |
| 606 |
if(is_array($dataset['items_summary']) && count($dataset['items_summary'])>0){ |
| 607 |
|
| 608 |
/*add some empty lines first*/ |
| 609 |
$result['sales_by_item']=PHP_EOL.PHP_EOL.PHP_EOL.PHP_EOL.PHP_EOL; |
| 610 |
$result['sales_by_item'].='"'.__('sales by item','wppizza-admin').'"'.PHP_EOL; |
| 611 |
/*items*/ |
| 612 |
$result['sales_by_item'].='"'.__('quantity','wppizza-admin').'", "'.__('item','wppizza-admin').'", "'.__('total value','wppizza-admin').'"'.PHP_EOL; |
| 613 |
$totalNumberItems=0; |
| 614 |
$totalSalesItems=0; |
| 615 |
foreach($dataset['items_summary'] as $uniqueItem=>$itemDetails){ |
| 616 |
$result['sales_by_item'].=$itemDetails['quantity'] . $delimiter . wppizza_decode_entities($itemDetails['title']) . $delimiter . $itemDetails['pricetotal']; |
| 617 |
$result['sales_by_item'].=PHP_EOL; |
| 618 |
/*add it up*/ |
| 619 |
$totalNumberItems+=$itemDetails['quantity']; |
| 620 |
$totalSalesItems+=$itemDetails['pricetotal']; |
| 621 |
} |
| 622 |
|
| 623 |
/*add some empty lines*/ |
| 624 |
$result['sales_by_item'].=PHP_EOL; |
| 625 |
//irrelevant as already displayed |
| 626 |
//$result.='"total quantity all items", "", "total value all items"'.PHP_EOL; |
| 627 |
//$result.=$totalNumberItems . $delimiter . '' . $delimiter . $totalSalesItems; |
| 628 |
} |
| 629 |
|
| 630 |
/************************************************************************** |
| 631 |
sales value by gateway |
| 632 |
**************************************************************************/ |
| 633 |
if(is_array($dataset['gateways_summary']) && count($dataset['gateways_summary'])>0 && !defined('WPPIZZA_OMIT_REPORT_GATEWAYS_SUMMARY')){ |
| 634 |
$result['sales_by_gateway']=PHP_EOL.'"'.__('payment type','wppizza-admin').'"'.PHP_EOL; |
| 635 |
/*items*/ |
| 636 |
$result['sales_by_gateway'].='"'.__('type','wppizza-admin').'", "'.__('total value','wppizza-admin').'"'.PHP_EOL; |
| 637 |
foreach($dataset['gateways_summary'] as $uniqueGateway=>$gatewayValue){ |
| 638 |
$result['sales_by_gateway'].=$uniqueGateway . $delimiter . $gatewayValue; |
| 639 |
$result['sales_by_gateway'].=PHP_EOL; |
| 640 |
} |
| 641 |
/*add some empty lines */ |
| 642 |
$result['sales_by_gateway'].=PHP_EOL; |
| 643 |
} |
| 644 |
|
| 645 |
/************************************************************************** |
| 646 |
sales value by order status |
| 647 |
**************************************************************************/ |
| 648 |
if(is_array($dataset['order_status_summary']) && count($dataset['order_status_summary'])>0 && !defined('WPPIZZA_OMIT_REPORT_ORDER_STATUS_SUMMARY')){ |
| 649 |
$result['sales_by_status']=PHP_EOL.'"'.__('order status','wppizza-admin').'"'.PHP_EOL; |
| 650 |
/*items*/ |
| 651 |
$result['sales_by_status'].='"'.__('status','wppizza-admin').'", "'.__('count','wppizza-admin').'", "'.__('total value','wppizza-admin').'"'.PHP_EOL; |
| 652 |
foreach($dataset['order_status_summary'] as $uniqueKey=>$statusValue){ |
| 653 |
$result['sales_by_status'].=$uniqueKey . $delimiter . $statusValue['count'] . $delimiter . $statusValue['value']; |
| 654 |
$result['sales_by_status'].=PHP_EOL; |
| 655 |
} |
| 656 |
/*add some empty lines */ |
| 657 |
$result['sales_by_status'].=PHP_EOL; |
| 658 |
} |
| 659 |
|
| 660 |
/************************************************************************** |
| 661 |
sales value by custom order status |
| 662 |
**************************************************************************/ |
| 663 |
if(is_array($dataset['order_custom_status_summary']) && count($dataset['order_custom_status_summary'])>0 && !defined('WPPIZZA_OMIT_REPORT_CUSTOM_ORDER_STATUS_SUMMARY')){ |
| 664 |
$result['sales_by_custom_status']=PHP_EOL.'"'.__('custom options','wppizza-admin').'"'.PHP_EOL; |
| 665 |
/*items*/ |
| 666 |
$result['sales_by_custom_status'].='"'.__('option','wppizza-admin').'", "'.__('count','wppizza-admin').'", "'.__('total value','wppizza-admin').'"'.PHP_EOL; |
| 667 |
foreach($dataset['order_custom_status_summary'] as $uniqueKey=>$statusValue){ |
| 668 |
$result['sales_by_custom_status'].=$uniqueKey . $delimiter . $statusValue['count'] . $delimiter . $statusValue['value']; |
| 669 |
$result['sales_by_custom_status'].=PHP_EOL; |
| 670 |
} |
| 671 |
/*add some empty lines */ |
| 672 |
$result['sales_by_custom_status'].=PHP_EOL; |
| 673 |
} |
| 674 |
|
| 675 |
/* filter array to be able to delete / add things to the output before imploding if required */ |
| 676 |
$result = apply_filters('wppizza_filter_reports_export_results', $result, $report_data); |
| 677 |
$result = implode('',$result); |
| 678 |
|
| 679 |
/************************************************************************** |
| 680 |
write to file setting headers and |
| 681 |
triggering save file dialogue |
| 682 |
**************************************************************************/ |
| 683 |
$this->do_report_export($result); |
| 684 |
|
| 685 |
exit(); |
| 686 |
} |
| 687 |
|
| 688 |
/********************************************************* |
| 689 |
* |
| 690 |
* [do the exporting, setting headers etc, trigger save csv dialogue] |
| 691 |
* @since 3.9 |
| 692 |
* @param str |
| 693 |
* @return void |
| 694 |
*********************************************************/ |
| 695 |
function do_report_export($result){ |
| 696 |
|
| 697 |
/* |
| 698 |
sanitise some get vars |
| 699 |
*/ |
| 700 |
wppizza_sanitize_get_vars( |
| 701 |
array( |
| 702 |
'from' => 'wppizza_validate_alpha_only', |
| 703 |
'to' => 'wppizza_validate_alpha_only', |
| 704 |
'name' => 'wppizza_validate_alpha_extend_ws', |
| 705 |
'type' => 'wppizza_validate_alpha_only_ws', |
| 706 |
) |
| 707 |
); |
| 708 |
|
| 709 |
/* |
| 710 |
set some header vars |
| 711 |
*/ |
| 712 |
$encoding='base64'; |
| 713 |
$mime='text/csv; charset='.WPPIZZA_CHARSET.''; |
| 714 |
$extension='.csv'; |
| 715 |
|
| 716 |
/* |
| 717 |
set filename - date |
| 718 |
*/ |
| 719 |
$filename = array(); |
| 720 |
$filename[]=date('Y.m.d', WPPIZZA_WP_TIME ); |
| 721 |
|
| 722 |
/* |
| 723 |
set filename - add range |
| 724 |
*/ |
| 725 |
if(isset($_GET['from']) && isset($_GET['to'])){ |
| 726 |
$filename[]='-['; |
| 727 |
$filename[]=esc_sql(str_replace("-",".",$_GET['from'])); |
| 728 |
$filename[]='-'; |
| 729 |
$filename[]=esc_sql(str_replace("-",".",$_GET['to'])); |
| 730 |
$filename[]=']'; |
| 731 |
}else{ |
| 732 |
if(isset($_GET['name'])){ |
| 733 |
$filename[]='-'.esc_sql(str_replace(" ","_",$_GET['name'])); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
/* |
| 738 |
set filename - type. if not default |
| 739 |
*/ |
| 740 |
if(isset($_GET['type']) && $_GET['type']!='default'){ |
| 741 |
$filename[]='-'.esc_sql(str_replace(" ","_",$_GET['type'])); |
| 742 |
} |
| 743 |
|
| 744 |
/*filter if you want*/ |
| 745 |
$filename = apply_filters('wppizza_filter_report_export_title', $filename); |
| 746 |
$filename=implode("",$filename);//implode to string |
| 747 |
$filename = ''.strtolower(wppizza_validate_alpha_only(WPPIZZA_NAME)).'_report_'.$filename.''.$extension.''; |
| 748 |
|
| 749 |
/* |
| 750 |
set headers and content |
| 751 |
*/ |
| 752 |
header("Content-Encoding: ".WPPIZZA_CHARSET.""); |
| 753 |
header("Content-Type: ".$mime.""); |
| 754 |
header('Content-Disposition: attachment; filename="'.$filename.'"'); |
| 755 |
header("Content-Length: " . strlen($result)); |
| 756 |
echo $result; |
| 757 |
|
| 758 |
exit(); |
| 759 |
} |
| 760 |
|
| 761 |
} |
| 762 |
|
| 763 |
/*************************************************************** |
| 764 |
* |
| 765 |
* [ini] |
| 766 |
* |
| 767 |
***************************************************************/ |
| 768 |
$WPPIZZA_REPORTS = new WPPIZZA_REPORTS(); |
| 769 |
?> |