1 <?php
  2 /**
  3  * WC_Report_Sales_By_Date
  4  *
  5  * @author      WooThemes
  6  * @category    Admin
  7  * @package     WooCommerce/Admin/Reports
  8  * @version     2.1.0
  9  */
 10 class WC_Report_Sales_By_Date extends WC_Admin_Report {
 11 
 12     public $chart_colours = array();
 13     
 14     /**
 15      * Get the legend for the main chart sidebar
 16      * @return array
 17      */
 18     public function get_chart_legend() {
 19         $legend   = array();
 20 
 21         $order_totals = $this->get_order_report_data( array(
 22             'data' => array(
 23                 '_order_total' => array(
 24                     'type'     => 'meta',
 25                     'function' => 'SUM',
 26                     'name'     => 'total_sales'
 27                 ),
 28                 '_order_shipping' => array(
 29                     'type'     => 'meta',
 30                     'function' => 'SUM',
 31                     'name'     => 'total_shipping'
 32                 ),
 33                 'ID' => array(
 34                     'type'     => 'post_data',
 35                     'function' => 'COUNT',
 36                     'name'     => 'total_orders'
 37                 )
 38             ),
 39             'filter_range' => true
 40         ) );
 41         $total_sales    = $order_totals->total_sales;
 42         $total_shipping = $order_totals->total_shipping;
 43         $total_orders   = absint( $order_totals->total_orders );
 44         $total_items    = absint( $this->get_order_report_data( array(
 45             'data' => array(
 46                 '_qty' => array(
 47                     'type'            => 'order_item_meta',
 48                     'order_item_type' => 'line_item',
 49                     'function'        => 'SUM',
 50                     'name'            => 'order_item_qty'
 51                 )
 52             ),
 53             'query_type' => 'get_var',
 54             'filter_range' => true
 55         ) ) );
 56         // Get discount amounts in range
 57         $total_coupons = $this->get_order_report_data( array(
 58             'data' => array(
 59                 'discount_amount' => array(
 60                     'type'            => 'order_item_meta',
 61                     'order_item_type' => 'coupon',
 62                     'function'        => 'SUM',
 63                     'name'            => 'discount_amount'
 64                 )
 65             ),
 66             'where' => array(
 67                 array(
 68                     'key'      => 'order_items.order_item_type',
 69                     'value'    => 'coupon',
 70                     'operator' => '='
 71                 )
 72             ),
 73             'query_type' => 'get_var',
 74             'filter_range' => true
 75         ) );
 76 
 77         $this->average_sales = $total_sales / ( $this->chart_interval + 1 );
 78 
 79         switch ( $this->chart_groupby ) {
 80             case 'day' :
 81                 $average_sales_title = sprintf( __( '%s average daily sales', 'woocommerce' ), '<strong>' . wc_price( $this->average_sales ) . '</strong>' );
 82             break;
 83             case 'month' :
 84                 $average_sales_title = sprintf( __( '%s average monthly sales', 'woocommerce' ), '<strong>' . wc_price( $this->average_sales ) . '</strong>' );
 85             break;
 86         }
 87 
 88         $legend[] = array(
 89             'title' => sprintf( __( '%s sales in this period', 'woocommerce' ), '<strong>' . wc_price( $total_sales ) . '</strong>' ),
 90             'color' => $this->chart_colours['sales_amount'],
 91             'highlight_series' => 5
 92         );
 93         $legend[] = array(
 94             'title' => $average_sales_title,
 95             'color' => $this->chart_colours['average'],
 96             'highlight_series' => 2
 97         );
 98         $legend[] = array(
 99             'title' => sprintf( __( '%s orders placed', 'woocommerce' ), '<strong>' . $total_orders . '</strong>' ),
100             'color' => $this->chart_colours['order_count'],
101             'highlight_series' => 1
102         );
103         $legend[] = array(
104             'title' => sprintf( __( '%s items purchased', 'woocommerce' ), '<strong>' . $total_items . '</strong>' ),
105             'color' => $this->chart_colours['item_count'],
106             'highlight_series' => 0
107         );
108         $legend[] = array(
109             'title' => sprintf( __( '%s charged for shipping', 'woocommerce' ), '<strong>' . wc_price( $total_shipping ) . '</strong>' ),
110             'color' => $this->chart_colours['shipping_amount'],
111             'highlight_series' => 4
112         );
113         $legend[] = array(
114             'title' => sprintf( __( '%s worth of coupons used', 'woocommerce' ), '<strong>' . wc_price( $total_coupons ) . '</strong>' ),
115             'color' => $this->chart_colours['coupon_amount'],
116             'highlight_series' => 3
117         );
118 
119         return $legend;
120     }
121 
122     /**
123      * Output the report
124      */
125     public function output_report() {
126         global $woocommerce, $wpdb, $wp_locale;
127 
128         $ranges = array(
129             'year'         => __( 'Year', 'woocommerce' ),
130             'last_month'   => __( 'Last Month', 'woocommerce' ),
131             'month'        => __( 'This Month', 'woocommerce' ),
132             '7day'         => __( 'Last 7 Days', 'woocommerce' )
133         );
134 
135         $this->chart_colours = array(
136             'sales_amount' => '#3498db',
137             'average'      => '#75b9e7',
138             'order_count'  => '#b8c0c5',
139             'item_count'   => '#d4d9dc',
140             'coupon_amount' => '#e67e22',
141             'shipping_amount' => '#1abc9c'
142         );
143 
144         $current_range = ! empty( $_GET['range'] ) ? $_GET['range'] : '7day';
145 
146         if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', 'month', '7day' ) ) )
147             $current_range = '7day';
148 
149         $this->calculate_current_range( $current_range );
150 
151         include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php');
152     }
153 
154     /**
155      * Output an export link
156      */
157     public function get_export_button() {
158         $current_range = ! empty( $_GET['range'] ) ? $_GET['range'] : '7day';
159         ?>
160         <a
161             href="#"
162             download="report-<?php echo $current_range; ?>-<?php echo date_i18n( 'Y-m-d', current_time('timestamp') ); ?>.csv"
163             class="export_csv"
164             data-export="chart"
165             data-xaxes="<?php _e( 'Date', 'woocommerce' ); ?>"
166             data-exclude_series="2"
167             data-groupby="<?php echo $this->chart_groupby; ?>"
168         >
169             <?php _e( 'Export CSV', 'woocommerce' ); ?>
170         </a>
171         <?php
172     }
173 
174     /**
175      * Get the main chart
176      * @return string
177      */
178     public function get_main_chart() {
179         global $wp_locale;
180 
181         // Get orders and dates in range - we want the SUM of order totals, COUNT of order items, COUNT of orders, and the date
182         $orders = $this->get_order_report_data( array(
183             'data' => array(
184                 '_order_total' => array(
185                     'type'     => 'meta',
186                     'function' => 'SUM',
187                     'name'     => 'total_sales'
188                 ),
189                 '_order_shipping' => array(
190                     'type'     => 'meta',
191                     'function' => 'SUM',
192                     'name'     => 'total_shipping'
193                 ),
194                 'ID' => array(
195                     'type'     => 'post_data',
196                     'function' => 'COUNT',
197                     'name'     => 'total_orders',
198                     'distinct' => true,
199                 ),
200                 'post_date' => array(
201                     'type'     => 'post_data',
202                     'function' => '',
203                     'name'     => 'post_date'
204                 ),
205             ),
206             'group_by'     => $this->group_by_query,
207             'order_by'     => 'post_date ASC',
208             'query_type'   => 'get_results',
209             'filter_range' => true
210         ) );
211 
212         // Order items
213         $order_items = $this->get_order_report_data( array(
214             'data' => array(
215                 '_qty' => array(
216                     'type'            => 'order_item_meta',
217                     'order_item_type' => 'line_item',
218                     'function'        => 'SUM',
219                     'name'            => 'order_item_count'
220                 ),
221                 'post_date' => array(
222                     'type'     => 'post_data',
223                     'function' => '',
224                     'name'     => 'post_date'
225                 ),
226             ),
227             'where' => array(
228                 array(
229                     'key'      => 'order_items.order_item_type',
230                     'value'    => 'line_item',
231                     'operator' => '='
232                 )
233             ),
234             'group_by'     => $this->group_by_query,
235             'order_by'     => 'post_date ASC',
236             'query_type'   => 'get_results',
237             'filter_range' => true
238         ) );
239 
240         // Get discount amounts in range
241         $coupons = $this->get_order_report_data( array(
242             'data' => array(
243                 'order_item_name' => array(
244                     'type'     => 'order_item',
245                     'function' => '',
246                     'name'     => 'order_item_name'
247                 ),
248                 'discount_amount' => array(
249                     'type'            => 'order_item_meta',
250                     'order_item_type' => 'coupon',
251                     'function'        => 'SUM',
252                     'name'            => 'discount_amount'
253                 ),
254                 'post_date' => array(
255                     'type'     => 'post_data',
256                     'function' => '',
257                     'name'     => 'post_date'
258                 ),
259             ),
260             'where' => array(
261                 array(
262                     'key'      => 'order_items.order_item_type',
263                     'value'    => 'coupon',
264                     'operator' => '='
265                 )
266             ),
267             'group_by'     => $this->group_by_query . ', order_item_name',
268             'order_by'     => 'post_date ASC',
269             'query_type'   => 'get_results',
270             'filter_range' => true
271         ) );
272 
273         // Prepare data for report
274         $order_counts      = $this->prepare_chart_data( $orders, 'post_date', 'total_orders', $this->chart_interval, $this->start_date, $this->chart_groupby );
275         $order_item_counts = $this->prepare_chart_data( $order_items, 'post_date', 'order_item_count', $this->chart_interval, $this->start_date, $this->chart_groupby );
276         $order_amounts     = $this->prepare_chart_data( $orders, 'post_date', 'total_sales', $this->chart_interval, $this->start_date, $this->chart_groupby );
277         $coupon_amounts    = $this->prepare_chart_data( $coupons, 'post_date', 'discount_amount', $this->chart_interval, $this->start_date, $this->chart_groupby );
278         $shipping_amounts    = $this->prepare_chart_data( $orders, 'post_date', 'total_shipping', $this->chart_interval, $this->start_date, $this->chart_groupby );
279 
280         // Encode in json format
281         $chart_data = json_encode( array(
282             'order_counts'      => array_values( $order_counts ),
283             'order_item_counts' => array_values( $order_item_counts ),
284             'order_amounts'     => array_values( $order_amounts ),
285             'coupon_amounts'    => array_values( $coupon_amounts ),
286             'shipping_amounts'  => array_values( $shipping_amounts )
287         ) );
288         ?>
289         <div class="chart-container">
290             <div class="chart-placeholder main"></div>
291         </div>
292         <script type="text/javascript">
293 
294             var main_chart;
295 
296             jQuery(function(){
297                 var order_data = jQuery.parseJSON( '<?php echo $chart_data; ?>' );
298                 var drawGraph = function( highlight ) {
299                     var series = [
300                         {
301                             label: "<?php echo esc_js( __( 'Number of items sold', 'woocommerce' ) ) ?>",
302                             data: order_data.order_item_counts,
303                             color: '<?php echo $this->chart_colours['item_count']; ?>',
304                             bars: { fillColor: '<?php echo $this->chart_colours['item_count']; ?>', fill: true, show: true, lineWidth: 0, barWidth: <?php echo $this->barwidth; ?> * 0.5, align: 'center' },
305                             shadowSize: 0,
306                             hoverable: false
307                         },
308                         {
309                             label: "<?php echo esc_js( __( 'Number of orders', 'woocommerce' ) ) ?>",
310                             data: order_data.order_counts,
311                             color: '<?php echo $this->chart_colours['order_count']; ?>',
312                             bars: { fillColor: '<?php echo $this->chart_colours['order_count']; ?>', fill: true, show: true, lineWidth: 0, barWidth: <?php echo $this->barwidth; ?> * 0.5, align: 'center' },
313                             shadowSize: 0,
314                             hoverable: false
315                         },
316                         {
317                             label: "<?php echo esc_js( __( 'Average sales amount', 'woocommerce' ) ) ?>",
318                             data: [ [ <?php echo min( array_keys( $order_amounts ) ); ?>, <?php echo $this->average_sales; ?> ], [ <?php echo max( array_keys( $order_amounts ) ); ?>, <?php echo $this->average_sales; ?> ] ],
319                             yaxis: 2,
320                             color: '<?php echo $this->chart_colours['average']; ?>',
321                             points: { show: false },
322                             lines: { show: true, lineWidth: 2, fill: false },
323                             shadowSize: 0,
324                             hoverable: false
325                         },
326                         {
327                             label: "<?php echo esc_js( __( 'Coupon amount', 'woocommerce' ) ) ?>",
328                             data: order_data.coupon_amounts,
329                             yaxis: 2,
330                             color: '<?php echo $this->chart_colours['coupon_amount']; ?>',
331                             points: { show: true, radius: 5, lineWidth: 3, fillColor: '#fff', fill: true },
332                             lines: { show: true, lineWidth: 4, fill: false },
333                             shadowSize: 0,
334                             prepend_tooltip: "<?php echo get_woocommerce_currency_symbol(); ?>"
335                         },
336                         {
337                             label: "<?php echo esc_js( __( 'Shipping amount', 'woocommerce' ) ) ?>",
338                             data: order_data.shipping_amounts,
339                             yaxis: 2,
340                             color: '<?php echo $this->chart_colours['shipping_amount']; ?>',
341                             points: { show: true, radius: 5, lineWidth: 3, fillColor: '#fff', fill: true },
342                             lines: { show: true, lineWidth: 4, fill: false },
343                             shadowSize: 0,
344                             prepend_tooltip: "<?php echo get_woocommerce_currency_symbol(); ?>"
345                         },
346                         {
347                             label: "<?php echo esc_js( __( 'Sales amount', 'woocommerce' ) ) ?>",
348                             data: order_data.order_amounts,
349                             yaxis: 2,
350                             color: '<?php echo $this->chart_colours['sales_amount']; ?>',
351                             points: { show: true, radius: 5, lineWidth: 3, fillColor: '#fff', fill: true },
352                             lines: { show: true, lineWidth: 4, fill: false },
353                             shadowSize: 0,
354                             prepend_tooltip: "<?php echo get_woocommerce_currency_symbol(); ?>"
355                         }
356                     ];
357 
358                     if ( highlight !== 'undefined' && series[ highlight ] ) {
359                         highlight_series = series[ highlight ];
360 
361                         highlight_series.color = '#9c5d90';
362 
363                         if ( highlight_series.bars )
364                             highlight_series.bars.fillColor = '#9c5d90';
365 
366                         if ( highlight_series.lines ) {
367                             highlight_series.lines.lineWidth = 5;
368                         }
369                     }
370 
371                     main_chart = jQuery.plot(
372                         jQuery('.chart-placeholder.main'),
373                         series,
374                         {
375                             legend: {
376                                 show: false
377                             },
378                             grid: {
379                                 color: '#aaa',
380                                 borderColor: 'transparent',
381                                 borderWidth: 0,
382                                 hoverable: true
383                             },
384                             xaxes: [ {
385                                 color: '#aaa',
386                                 position: "bottom",
387                                 tickColor: 'transparent',
388                                 mode: "time",
389                                 timeformat: "<?php if ( $this->chart_groupby == 'day' ) echo '%d %b'; else echo '%b'; ?>",
390                                 monthNames: <?php echo json_encode( array_values( $wp_locale->month_abbrev ) ) ?>,
391                                 tickLength: 1,
392                                 minTickSize: [1, "<?php echo $this->chart_groupby; ?>"],
393                                 font: {
394                                     color: "#aaa"
395                                 }
396                             } ],
397                             yaxes: [
398                                 {
399                                     min: 0,
400                                     minTickSize: 1,
401                                     tickDecimals: 0,
402                                     color: '#d4d9dc',
403                                     font: { color: "#aaa" }
404                                 },
405                                 {
406                                     position: "right",
407                                     min: 0,
408                                     tickDecimals: 2,
409                                     alignTicksWithAxis: 1,
410                                     color: 'transparent',
411                                     font: { color: "#aaa" }
412                                 }
413                             ],
414                         }
415                     );
416 
417                     jQuery('.chart-placeholder').resize();
418                 }
419 
420                 drawGraph();
421 
422                 jQuery('.highlight_series').hover(
423                     function() {
424                         drawGraph( jQuery(this).data('series') );
425                     },
426                     function() {
427                         drawGraph();
428                     }
429                 );
430             });
431         </script>
432         <?php
433     }
434 }
WooCommerce API documentation generated by ApiGen 2.8.0