pmpro_report_sales_page

The Paid Memberships Pro pmpro report sales page function.

Description

pmpro_report_sales_page(); 

Usage

  1. if ( !function_exists( 'pmpro_report_sales_page' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/adminpages/reports/sales.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = pmpro_report_sales_page(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/adminpages/reports/sales.php  
  1. function pmpro_report_sales_page() 
  2. global $wpdb, $pmpro_currency_symbol, $pmpro_currency, $pmpro_currencies; 
  3.  
  4. //get values from form 
  5. if(isset($_REQUEST['type'])) 
  6. $type = sanitize_text_field($_REQUEST['type']); 
  7. else 
  8. $type = "revenue"; 
  9.  
  10. if($type == "sales") 
  11. $type_function = "COUNT"; 
  12. else 
  13. $type_function = "SUM"; 
  14.  
  15. if(isset($_REQUEST['period'])) 
  16. $period = sanitize_text_field($_REQUEST['period']); 
  17. else 
  18. $period = "daily"; 
  19.  
  20. if(isset($_REQUEST['month'])) 
  21. $month = intval($_REQUEST['month']); 
  22. else 
  23. $month = date_i18n("n", current_time('timestamp')); 
  24.  
  25. $thisyear = date_i18n("Y", current_time('timestamp')); 
  26. if(isset($_REQUEST['year'])) 
  27. $year = intval($_REQUEST['year']); 
  28. else 
  29. $year = $thisyear; 
  30.  
  31. if(isset($_REQUEST['level'])) 
  32. $l = intval($_REQUEST['level']); 
  33. else 
  34. $l = ""; 
  35.  
  36. //calculate start date and how to group dates returned from DB 
  37. if($period == "daily") 
  38. $startdate = $year . '-' . substr("0" . $month, strlen($month) - 1, 2) . '-01';  
  39. $enddate = $year . '-' . substr("0" . $month, strlen($month) - 1, 2) . '-32';  
  40. $date_function = 'DAY'; 
  41. elseif($period == "monthly") 
  42. $startdate = $year . '-01-01'; 
  43. $enddate = strval(intval($year)+1) . '-01-01'; 
  44. $date_function = 'MONTH'; 
  45. else 
  46. $startdate = '1960-01-01'; //all time 
  47. $date_function = 'YEAR'; 
  48.  
  49. //testing or live data 
  50. $gateway_environment = pmpro_getOption("gateway_environment"); 
  51.  
  52. //get data 
  53. $sqlQuery = "SELECT $date_function(timestamp) as date, $type_function(total) as value FROM $wpdb->pmpro_membership_orders WHERE total > 0 AND timestamp >= '" . $startdate . "' AND status NOT IN('refunded', 'review', token, error) AND gateway_environment = '" . esc_sql($gateway_environment) . "' "; 
  54.  
  55. if(!empty($enddate)) 
  56. $sqlQuery .= "AND timestamp < '" . $enddate . "' "; 
  57.  
  58. if(!empty($l)) 
  59. $sqlQuery .= "AND membership_id IN(" . $l . ") "; 
  60.  
  61. $sqlQuery .= " GROUP BY dateORDERBY date "; 
  62.  
  63. $dates = $wpdb->get_results($sqlQuery);  
  64.  
  65. //fill in blanks in dates 
  66. $cols = array();  
  67. if($period == "daily") 
  68. $lastday = date_i18n("t", strtotime($startdate, current_time("timestamp"))); 
  69.  
  70. for($i = 1; $i <= $lastday; $i++) 
  71. $cols[$i] = 0; 
  72. foreach($dates as $date) 
  73. if($date->date == $i) 
  74. $cols[$i] = $date->value; 
  75. elseif($period == "monthly") 
  76. {  
  77. for($i = 1; $i < 13; $i++) 
  78. $cols[$i] = 0; 
  79. foreach($dates as $date) 
  80. if($date->date == $i) 
  81. $cols[$i] = $date->value; 
  82. else //annual 
  83. //get min and max years 
  84. $min = 9999; 
  85. $max = 0; 
  86. foreach($dates as $date) 
  87. $min = min($min, $date->date); 
  88. $max = max($max, $date->date); 
  89.  
  90. for($i = $min; $i <= $max; $i++) 
  91. foreach($dates as $date) 
  92. if($date->date == $i) 
  93. $cols[$i] = $date->value; 
  94. }  
  95. ?> 
  96. <form id="posts-filter" method="get" action="">  
  97. <h1> 
  98. <?php _e('Sales and Revenue', 'paid-memberships-pro' );?> 
  99. </h1> 
  100.  
  101. <div class="tablenav top"> 
  102. <?php _e('Show', 'paid-memberships-pro' )?> 
  103. <select id="period" name="period"> 
  104. <option value="daily" <?php selected($period, "daily");?>><?php _e('Daily', 'paid-memberships-pro' );?></option> 
  105. <option value="monthly" <?php selected($period, "monthly");?>><?php _e('Monthly', 'paid-memberships-pro' );?></option> 
  106. <option value="annual" <?php selected($period, "annual");?>><?php _e('Annual', 'paid-memberships-pro' );?></option> 
  107. </select> 
  108. <select name="type"> 
  109. <option value="revenue" <?php selected($type, "revenue");?>><?php _e('Revenue', 'paid-memberships-pro' );?></option> 
  110. <option value="sales" <?php selected($type, "sales");?>><?php _e('Sales', 'paid-memberships-pro' );?></option> 
  111. </select> 
  112. <span id="for"><?php _e('for', 'paid-memberships-pro' )?></span> 
  113. <select id="month" name="month"> 
  114. <?php for($i = 1; $i < 13; $i++) { ?> 
  115. <option value="<?php echo $i;?>" <?php selected($month, $i);?>><?php echo date_i18n("F", mktime(0, 0, 0, $i, 2));?></option> 
  116. <?php } ?> 
  117. </select> 
  118. <select id="year" name="year"> 
  119. <?php for($i = $thisyear; $i > 2007; $i--) { ?> 
  120. <option value="<?php echo $i;?>" <?php selected($year, $i);?>><?php echo $i;?></option> 
  121. <?php } ?> 
  122. </select> 
  123. <span id="for"><?php _e('for', 'paid-memberships-pro' )?></span> 
  124. <select name="level"> 
  125. <option value="" <?php if(!$l) { ?>selected="selected"<?php } ?>><?php _e('All Levels', 'paid-memberships-pro' );?></option> 
  126. <?php 
  127. $levels = $wpdb->get_results("SELECT id, name FROM $wpdb->pmpro_membership_levelsORDERBY name"); 
  128. foreach($levels as $level) 
  129. ?> 
  130. <option value="<?php echo $level->id?>" <?php if($l == $level->id) { ?>selected="selected"<?php } ?>><?php echo $level->name?></option> 
  131. <?php 
  132. ?> 
  133. </select> 
  134.  
  135. <input type="hidden" name="page" value="pmpro-reports" />  
  136. <input type="hidden" name="report" value="sales" />  
  137. <input type="submit" class="button action" value="<?php _e('Generate Report', 'paid-memberships-pro' );?>" /> 
  138. </div> 
  139.  
  140. <div id="chart_div" style="clear: both; width: 100%; height: 500px;"></div>  
  141.  
  142. <script> 
  143. //update month/year when period dropdown is changed 
  144. jQuery(document).ready(function() { 
  145. jQuery('#period').change(function() { 
  146. pmpro_ShowMonthOrYear(); 
  147. }); 
  148. }); 
  149.  
  150. function pmpro_ShowMonthOrYear() 
  151. var period = jQuery('#period').val(); 
  152. if(period == 'daily') 
  153. jQuery('#for').show(); 
  154. jQuery('#month').show(); 
  155. jQuery('#year').show(); 
  156. else if(period == 'monthly') 
  157. jQuery('#for').show(); 
  158. jQuery('#month').hide(); 
  159. jQuery('#year').show(); 
  160. else 
  161. jQuery('#for').hide(); 
  162. jQuery('#month').hide(); 
  163. jQuery('#year').hide(); 
  164.  
  165. pmpro_ShowMonthOrYear(); 
  166.  
  167. //draw the chart 
  168. google.load("visualization", "1", {packages:["corechart"]}); 
  169. google.setOnLoadCallback(drawChart); 
  170. function drawChart() {  
  171.  
  172. var data = google.visualization.arrayToDataTable([ 
  173. ['<?php echo $date_function;?>', '<?php echo ucwords($type);?>'],  
  174. <?php foreach($cols as $date => $value) { ?> 
  175. ['<?php if($period == "monthly") echo date_i18n("M", mktime(0, 0, 0, $date, 2)); else echo $date;?>', <?php echo $value;?>],  
  176. <?php } ?> 
  177. ]); 
  178.  
  179. var options = {  
  180. colors: ['#51a351', '#387038'],  
  181. hAxis: {title: '<?php echo $date_function;?>', titleTextStyle: {color: 'black'}, maxAlternation: 1},  
  182. vAxis: {color: 'green', titleTextStyle: {color: '#51a351'}},  
  183. }; 
  184.  
  185. <?php  
  186. if($type != "sales")  
  187. {  
  188. if(pmpro_getCurrencyPosition() == "right") 
  189. $position = "suffix"; 
  190. else 
  191. $position = "prefix";  
  192. ?> 
  193. var formatter = new google.visualization.NumberFormat({<?php echo $position;?>: '<?php echo html_entity_decode($pmpro_currency_symbol);?>'}); 
  194. formatter.format(data, 1); 
  195. <?php 
  196. ?> 
  197.  
  198. var chart = new google.visualization.ColumnChart(document.getElementById('chart_div')); 
  199. chart.draw(data, options); 
  200. </script> 
  201.  
  202. </form> 
  203. <?php