class-wc-report-sales-by-date.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. /**
  6. * WC_Report_Sales_By_Date
  7. *
  8. * @author WooThemes
  9. * @category Admin
  10. * @package WooCommerce/Admin/Reports
  11. * @version 2.1.0
  12. */
  13. class WC_Report_Sales_By_Date extends WC_Admin_Report {
  14. /**
  15. * Chart colors.
  16. *
  17. * @var array
  18. */
  19. public $chart_colours = array();
  20. /**
  21. * The report data.
  22. *
  23. * @var stdClass
  24. */
  25. private $report_data;
  26. /**
  27. * Get report data.
  28. *
  29. * @return stdClass
  30. */
  31. public function get_report_data() {
  32. if ( empty( $this->report_data ) ) {
  33. $this->query_report_data();
  34. }
  35. return $this->report_data;
  36. }
  37. /**
  38. * Get all data needed for this report and store in the class.
  39. */
  40. private function query_report_data() {
  41. $this->report_data = new stdClass();
  42. $this->report_data->order_counts = (array) $this->get_order_report_data(
  43. array(
  44. 'data' => array(
  45. 'ID' => array(
  46. 'type' => 'post_data',
  47. 'function' => 'COUNT',
  48. 'name' => 'count',
  49. 'distinct' => true,
  50. ),
  51. 'post_date' => array(
  52. 'type' => 'post_data',
  53. 'function' => '',
  54. 'name' => 'post_date',
  55. ),
  56. ),
  57. 'group_by' => $this->group_by_query,
  58. 'order_by' => 'post_date ASC',
  59. 'query_type' => 'get_results',
  60. 'filter_range' => true,
  61. 'order_types' => wc_get_order_types( 'order-count' ),
  62. 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
  63. )
  64. );
  65. $this->report_data->coupons = (array) $this->get_order_report_data(
  66. array(
  67. 'data' => array(
  68. 'order_item_name' => array(
  69. 'type' => 'order_item',
  70. 'function' => '',
  71. 'name' => 'order_item_name',
  72. ),
  73. 'discount_amount' => array(
  74. 'type' => 'order_item_meta',
  75. 'order_item_type' => 'coupon',
  76. 'function' => 'SUM',
  77. 'name' => 'discount_amount',
  78. ),
  79. 'post_date' => array(
  80. 'type' => 'post_data',
  81. 'function' => '',
  82. 'name' => 'post_date',
  83. ),
  84. ),
  85. 'where' => array(
  86. array(
  87. 'key' => 'order_items.order_item_type',
  88. 'value' => 'coupon',
  89. 'operator' => '=',
  90. ),
  91. ),
  92. 'group_by' => $this->group_by_query . ', order_item_name',
  93. 'order_by' => 'post_date ASC',
  94. 'query_type' => 'get_results',
  95. 'filter_range' => true,
  96. 'order_types' => wc_get_order_types( 'order-count' ),
  97. 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
  98. )
  99. );
  100. // All items from orders - even those refunded
  101. $this->report_data->order_items = (array) $this->get_order_report_data(
  102. array(
  103. 'data' => array(
  104. '_qty' => array(
  105. 'type' => 'order_item_meta',
  106. 'order_item_type' => 'line_item',
  107. 'function' => 'SUM',
  108. 'name' => 'order_item_count',
  109. ),
  110. 'post_date' => array(
  111. 'type' => 'post_data',
  112. 'function' => '',
  113. 'name' => 'post_date',
  114. ),
  115. ),
  116. 'where' => array(
  117. array(
  118. 'key' => 'order_items.order_item_type',
  119. 'value' => 'line_item',
  120. 'operator' => '=',
  121. ),
  122. ),
  123. 'group_by' => $this->group_by_query,
  124. 'order_by' => 'post_date ASC',
  125. 'query_type' => 'get_results',
  126. 'filter_range' => true,
  127. 'order_types' => wc_get_order_types( 'order-count' ),
  128. 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
  129. )
  130. );
  131. /**
  132. * Get total of fully refunded items.
  133. */
  134. $this->report_data->refunded_order_items = absint(
  135. $this->get_order_report_data(
  136. array(
  137. 'data' => array(
  138. '_qty' => array(
  139. 'type' => 'order_item_meta',
  140. 'order_item_type' => 'line_item',
  141. 'function' => 'SUM',
  142. 'name' => 'order_item_count',
  143. ),
  144. ),
  145. 'where' => array(
  146. array(
  147. 'key' => 'order_items.order_item_type',
  148. 'value' => 'line_item',
  149. 'operator' => '=',
  150. ),
  151. ),
  152. 'query_type' => 'get_var',
  153. 'filter_range' => true,
  154. 'order_types' => wc_get_order_types( 'order-count' ),
  155. 'order_status' => array( 'refunded' ),
  156. )
  157. )
  158. );
  159. /**
  160. * Order totals by date. Charts should show GROSS amounts to avoid going -ve.
  161. */
  162. $this->report_data->orders = (array) $this->get_order_report_data(
  163. array(
  164. 'data' => array(
  165. '_order_total' => array(
  166. 'type' => 'meta',
  167. 'function' => 'SUM',
  168. 'name' => 'total_sales',
  169. ),
  170. '_order_shipping' => array(
  171. 'type' => 'meta',
  172. 'function' => 'SUM',
  173. 'name' => 'total_shipping',
  174. ),
  175. '_order_tax' => array(
  176. 'type' => 'meta',
  177. 'function' => 'SUM',
  178. 'name' => 'total_tax',
  179. ),
  180. '_order_shipping_tax' => array(
  181. 'type' => 'meta',
  182. 'function' => 'SUM',
  183. 'name' => 'total_shipping_tax',
  184. ),
  185. 'post_date' => array(
  186. 'type' => 'post_data',
  187. 'function' => '',
  188. 'name' => 'post_date',
  189. ),
  190. ),
  191. 'group_by' => $this->group_by_query,
  192. 'order_by' => 'post_date ASC',
  193. 'query_type' => 'get_results',
  194. 'filter_range' => true,
  195. 'order_types' => wc_get_order_types( 'sales-reports' ),
  196. 'order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
  197. )
  198. );
  199. /**
  200. * If an order is 100% refunded we should look at the parent's totals, but the refunds dates.
  201. * We also need to ensure each parent order's values are only counted/summed once.
  202. */
  203. $this->report_data->full_refunds = (array) $this->get_order_report_data(
  204. array(
  205. 'data' => array(
  206. '_order_total' => array(
  207. 'type' => 'parent_meta',
  208. 'function' => '',
  209. 'name' => 'total_refund',
  210. ),
  211. '_order_shipping' => array(
  212. 'type' => 'parent_meta',
  213. 'function' => '',
  214. 'name' => 'total_shipping',
  215. ),
  216. '_order_tax' => array(
  217. 'type' => 'parent_meta',
  218. 'function' => '',
  219. 'name' => 'total_tax',
  220. ),
  221. '_order_shipping_tax' => array(
  222. 'type' => 'parent_meta',
  223. 'function' => '',
  224. 'name' => 'total_shipping_tax',
  225. ),
  226. 'post_date' => array(
  227. 'type' => 'post_data',
  228. 'function' => '',
  229. 'name' => 'post_date',
  230. ),
  231. ),
  232. 'group_by' => 'posts.post_parent',
  233. 'query_type' => 'get_results',
  234. 'filter_range' => true,
  235. 'order_status' => false,
  236. 'parent_order_status' => array( 'refunded' ),
  237. )
  238. );
  239. /**
  240. * Partial refunds. This includes line items, shipping and taxes. Not grouped by date.
  241. */
  242. $this->report_data->partial_refunds = (array) $this->get_order_report_data(
  243. array(
  244. 'data' => array(
  245. 'ID' => array(
  246. 'type' => 'post_data',
  247. 'function' => '',
  248. 'name' => 'refund_id',
  249. ),
  250. '_refund_amount' => array(
  251. 'type' => 'meta',
  252. 'function' => '',
  253. 'name' => 'total_refund',
  254. ),
  255. 'post_date' => array(
  256. 'type' => 'post_data',
  257. 'function' => '',
  258. 'name' => 'post_date',
  259. ),
  260. 'order_item_type' => array(
  261. 'type' => 'order_item',
  262. 'function' => '',
  263. 'name' => 'item_type',
  264. 'join_type' => 'LEFT',
  265. ),
  266. '_order_total' => array(
  267. 'type' => 'meta',
  268. 'function' => '',
  269. 'name' => 'total_sales',
  270. ),
  271. '_order_shipping' => array(
  272. 'type' => 'meta',
  273. 'function' => '',
  274. 'name' => 'total_shipping',
  275. 'join_type' => 'LEFT',
  276. ),
  277. '_order_tax' => array(
  278. 'type' => 'meta',
  279. 'function' => '',
  280. 'name' => 'total_tax',
  281. 'join_type' => 'LEFT',
  282. ),
  283. '_order_shipping_tax' => array(
  284. 'type' => 'meta',
  285. 'function' => '',
  286. 'name' => 'total_shipping_tax',
  287. 'join_type' => 'LEFT',
  288. ),
  289. '_qty' => array(
  290. 'type' => 'order_item_meta',
  291. 'function' => 'SUM',
  292. 'name' => 'order_item_count',
  293. 'join_type' => 'LEFT',
  294. ),
  295. ),
  296. 'group_by' => 'refund_id',
  297. 'order_by' => 'post_date ASC',
  298. 'query_type' => 'get_results',
  299. 'filter_range' => true,
  300. 'order_status' => false,
  301. 'parent_order_status' => array( 'completed', 'processing', 'on-hold' ),
  302. )
  303. );
  304. /**
  305. * Refund lines - all partial refunds on all order types so we can plot full AND partial refunds on the chart.
  306. */
  307. $this->report_data->refund_lines = (array) $this->get_order_report_data(
  308. array(
  309. 'data' => array(
  310. 'ID' => array(
  311. 'type' => 'post_data',
  312. 'function' => '',
  313. 'name' => 'refund_id',
  314. ),
  315. '_refund_amount' => array(
  316. 'type' => 'meta',
  317. 'function' => '',
  318. 'name' => 'total_refund',
  319. ),
  320. 'post_date' => array(
  321. 'type' => 'post_data',
  322. 'function' => '',
  323. 'name' => 'post_date',
  324. ),
  325. 'order_item_type' => array(
  326. 'type' => 'order_item',
  327. 'function' => '',
  328. 'name' => 'item_type',
  329. 'join_type' => 'LEFT',
  330. ),
  331. '_order_total' => array(
  332. 'type' => 'meta',
  333. 'function' => '',
  334. 'name' => 'total_sales',
  335. ),
  336. '_order_shipping' => array(
  337. 'type' => 'meta',
  338. 'function' => '',
  339. 'name' => 'total_shipping',
  340. 'join_type' => 'LEFT',
  341. ),
  342. '_order_tax' => array(
  343. 'type' => 'meta',
  344. 'function' => '',
  345. 'name' => 'total_tax',
  346. 'join_type' => 'LEFT',
  347. ),
  348. '_order_shipping_tax' => array(
  349. 'type' => 'meta',
  350. 'function' => '',
  351. 'name' => 'total_shipping_tax',
  352. 'join_type' => 'LEFT',
  353. ),
  354. '_qty' => array(
  355. 'type' => 'order_item_meta',
  356. 'function' => 'SUM',
  357. 'name' => 'order_item_count',
  358. 'join_type' => 'LEFT',
  359. ),
  360. ),
  361. 'group_by' => 'refund_id',
  362. 'order_by' => 'post_date ASC',
  363. 'query_type' => 'get_results',
  364. 'filter_range' => true,
  365. 'order_status' => false,
  366. 'parent_order_status' => array( 'completed', 'processing', 'on-hold', 'refunded' ),
  367. )
  368. );
  369. /**
  370. * Total up refunds. Note: when an order is fully refunded, a refund line will be added.
  371. */
  372. $this->report_data->total_tax_refunded = 0;
  373. $this->report_data->total_shipping_refunded = 0;
  374. $this->report_data->total_shipping_tax_refunded = 0;
  375. $this->report_data->total_refunds = 0;
  376. $refunded_orders = array_merge( $this->report_data->partial_refunds, $this->report_data->full_refunds );
  377. foreach ( $refunded_orders as $key => $value ) {
  378. $this->report_data->total_tax_refunded += floatval( $value->total_tax < 0 ? $value->total_tax * -1 : $value->total_tax );
  379. $this->report_data->total_refunds += floatval( $value->total_refund );
  380. $this->report_data->total_shipping_tax_refunded += floatval( $value->total_shipping_tax < 0 ? $value->total_shipping_tax * -1 : $value->total_shipping_tax );
  381. $this->report_data->total_shipping_refunded += floatval( $value->total_shipping < 0 ? $value->total_shipping * -1 : $value->total_shipping );
  382. // Only applies to parial.
  383. if ( isset( $value->order_item_count ) ) {
  384. $this->report_data->refunded_order_items += floatval( $value->order_item_count < 0 ? $value->order_item_count * -1 : $value->order_item_count );
  385. }
  386. }
  387. // Totals from all orders - including those refunded. Subtract refunded amounts.
  388. $this->report_data->total_tax = wc_format_decimal( array_sum( wp_list_pluck( $this->report_data->orders, 'total_tax' ) ) - $this->report_data->total_tax_refunded, 2 );
  389. $this->report_data->total_shipping = wc_format_decimal( array_sum( wp_list_pluck( $this->report_data->orders, 'total_shipping' ) ) - $this->report_data->total_shipping_refunded, 2 );
  390. $this->report_data->total_shipping_tax = wc_format_decimal( array_sum( wp_list_pluck( $this->report_data->orders, 'total_shipping_tax' ) ) - $this->report_data->total_shipping_tax_refunded, 2 );
  391. // Total the refunds and sales amounts. Sales subract refunds. Note - total_sales also includes shipping costs.
  392. $this->report_data->total_sales = wc_format_decimal( array_sum( wp_list_pluck( $this->report_data->orders, 'total_sales' ) ) - $this->report_data->total_refunds, 2 );
  393. $this->report_data->net_sales = wc_format_decimal( $this->report_data->total_sales - $this->report_data->total_shipping - max( 0, $this->report_data->total_tax ) - max( 0, $this->report_data->total_shipping_tax ), 2 );
  394. // Calculate average based on net
  395. $this->report_data->average_sales = wc_format_decimal( $this->report_data->net_sales / ( $this->chart_interval + 1 ), 2 );
  396. $this->report_data->average_total_sales = wc_format_decimal( $this->report_data->total_sales / ( $this->chart_interval + 1 ), 2 );
  397. // Total orders and discounts also includes those which have been refunded at some point
  398. $this->report_data->total_coupons = number_format( array_sum( wp_list_pluck( $this->report_data->coupons, 'discount_amount' ) ), 2, '.', '' );
  399. $this->report_data->total_refunded_orders = absint( count( $this->report_data->full_refunds ) );
  400. // Total orders in this period, even if refunded.
  401. $this->report_data->total_orders = absint( array_sum( wp_list_pluck( $this->report_data->order_counts, 'count' ) ) );
  402. // Item items ordered in this period, even if refunded.
  403. $this->report_data->total_items = absint( array_sum( wp_list_pluck( $this->report_data->order_items, 'order_item_count' ) ) );
  404. // 3rd party filtering of report data
  405. $this->report_data = apply_filters( 'woocommerce_admin_report_data', $this->report_data );
  406. }
  407. /**
  408. * Get the legend for the main chart sidebar.
  409. *
  410. * @return array
  411. */
  412. public function get_chart_legend() {
  413. $legend = array();
  414. $data = $this->get_report_data();
  415. switch ( $this->chart_groupby ) {
  416. case 'day':
  417. /* translators: %s: average total sales */
  418. $average_total_sales_title = sprintf(
  419. __( '%s average gross daily sales', 'woocommerce' ),
  420. '<strong>' . wc_price( $data->average_total_sales ) . '</strong>'
  421. );
  422. /* translators: %s: average sales */
  423. $average_sales_title = sprintf(
  424. __( '%s average net daily sales', 'woocommerce' ),
  425. '<strong>' . wc_price( $data->average_sales ) . '</strong>'
  426. );
  427. break;
  428. case 'month':
  429. default:
  430. /* translators: %s: average total sales */
  431. $average_total_sales_title = sprintf(
  432. __( '%s average gross monthly sales', 'woocommerce' ),
  433. '<strong>' . wc_price( $data->average_total_sales ) . '</strong>'
  434. );
  435. /* translators: %s: average sales */
  436. $average_sales_title = sprintf(
  437. __( '%s average net monthly sales', 'woocommerce' ),
  438. '<strong>' . wc_price( $data->average_sales ) . '</strong>'
  439. );
  440. break;
  441. }
  442. $legend[] = array(
  443. /* translators: %s: total sales */
  444. 'title' => sprintf(
  445. __( '%s gross sales in this period', 'woocommerce' ),
  446. '<strong>' . wc_price( $data->total_sales ) . '</strong>'
  447. ),
  448. 'placeholder' => __( 'This is the sum of the order totals after any refunds and including shipping and taxes.', 'woocommerce' ),
  449. 'color' => $this->chart_colours['sales_amount'],
  450. 'highlight_series' => 6,
  451. );
  452. if ( $data->average_total_sales > 0 ) {
  453. $legend[] = array(
  454. 'title' => $average_total_sales_title,
  455. 'color' => $this->chart_colours['average'],
  456. 'highlight_series' => 2,
  457. );
  458. }
  459. $legend[] = array(
  460. /* translators: %s: net sales */
  461. 'title' => sprintf(
  462. __( '%s net sales in this period', 'woocommerce' ),
  463. '<strong>' . wc_price( $data->net_sales ) . '</strong>'
  464. ),
  465. 'placeholder' => __( 'This is the sum of the order totals after any refunds and excluding shipping and taxes.', 'woocommerce' ),
  466. 'color' => $this->chart_colours['net_sales_amount'],
  467. 'highlight_series' => 7,
  468. );
  469. if ( $data->average_sales > 0 ) {
  470. $legend[] = array(
  471. 'title' => $average_sales_title,
  472. 'color' => $this->chart_colours['net_average'],
  473. 'highlight_series' => 3,
  474. );
  475. }
  476. $legend[] = array(
  477. /* translators: %s: total orders */
  478. 'title' => sprintf(
  479. __( '%s orders placed', 'woocommerce' ),
  480. '<strong>' . $data->total_orders . '</strong>'
  481. ),
  482. 'color' => $this->chart_colours['order_count'],
  483. 'highlight_series' => 1,
  484. );
  485. $legend[] = array(
  486. /* translators: %s: total items */
  487. 'title' => sprintf(
  488. __( '%s items purchased', 'woocommerce' ),
  489. '<strong>' . $data->total_items . '</strong>'
  490. ),
  491. 'color' => $this->chart_colours['item_count'],
  492. 'highlight_series' => 0,
  493. );
  494. $legend[] = array(
  495. /* translators: 1: total refunds 2: total refunded orders 3: refunded items */
  496. 'title' => sprintf(
  497. _n( '%1$s refunded %2$d order (%3$d item)', '%1$s refunded %2$d orders (%3$d items)', $this->report_data->total_refunded_orders, 'woocommerce' ),
  498. '<strong>' . wc_price( $data->total_refunds ) . '</strong>',
  499. $this->report_data->total_refunded_orders,
  500. $this->report_data->refunded_order_items
  501. ),
  502. 'color' => $this->chart_colours['refund_amount'],
  503. 'highlight_series' => 8,
  504. );
  505. $legend[] = array(
  506. /* translators: %s: total shipping */
  507. 'title' => sprintf(
  508. __( '%s charged for shipping', 'woocommerce' ),
  509. '<strong>' . wc_price( $data->total_shipping ) . '</strong>'
  510. ),
  511. 'color' => $this->chart_colours['shipping_amount'],
  512. 'highlight_series' => 5,
  513. );
  514. $legend[] = array(
  515. /* translators: %s: total coupons */
  516. 'title' => sprintf(
  517. __( '%s worth of coupons used', 'woocommerce' ),
  518. '<strong>' . wc_price( $data->total_coupons ) . '</strong>'
  519. ),
  520. 'color' => $this->chart_colours['coupon_amount'],
  521. 'highlight_series' => 4,
  522. );
  523. return $legend;
  524. }
  525. /**
  526. * Output the report.
  527. */
  528. public function output_report() {
  529. $ranges = array(
  530. 'year' => __( 'Year', 'woocommerce' ),
  531. 'last_month' => __( 'Last month', 'woocommerce' ),
  532. 'month' => __( 'This month', 'woocommerce' ),
  533. '7day' => __( 'Last 7 days', 'woocommerce' ),
  534. );
  535. $this->chart_colours = array(
  536. 'sales_amount' => '#b1d4ea',
  537. 'net_sales_amount' => '#3498db',
  538. 'average' => '#b1d4ea',
  539. 'net_average' => '#3498db',
  540. 'order_count' => '#dbe1e3',
  541. 'item_count' => '#ecf0f1',
  542. 'shipping_amount' => '#5cc488',
  543. 'coupon_amount' => '#f1c40f',
  544. 'refund_amount' => '#e74c3c',
  545. );
  546. $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day';
  547. if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', 'month', '7day' ) ) ) {
  548. $current_range = '7day';
  549. }
  550. $this->check_current_range_nonce( $current_range );
  551. $this->calculate_current_range( $current_range );
  552. include WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php';
  553. }
  554. /**
  555. * Output an export link.
  556. */
  557. public function get_export_button() {
  558. $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day';
  559. ?>
  560. <a
  561. href="#"
  562. download="report-<?php echo esc_attr( $current_range ); ?>-<?php echo date_i18n( 'Y-m-d', current_time( 'timestamp' ) ); ?>.csv"
  563. class="export_csv"
  564. data-export="chart"
  565. data-xaxes="<?php esc_attr_e( 'Date', 'woocommerce' ); ?>"
  566. data-exclude_series="2"
  567. data-groupby="<?php echo $this->chart_groupby; ?>"
  568. >
  569. <?php _e( 'Export CSV', 'woocommerce' ); ?>
  570. </a>
  571. <?php
  572. }
  573. /**
  574. * Round our totals correctly.
  575. *
  576. * @param array|string $amount
  577. *
  578. * @return array|string
  579. */
  580. private function round_chart_totals( $amount ) {
  581. if ( is_array( $amount ) ) {
  582. return array( $amount[0], wc_format_decimal( $amount[1], wc_get_price_decimals() ) );
  583. } else {
  584. return wc_format_decimal( $amount, wc_get_price_decimals() );
  585. }
  586. }
  587. /**
  588. * Get the main chart.
  589. */
  590. public function get_main_chart() {
  591. global $wp_locale;
  592. // Prepare data for report
  593. $data = array(
  594. 'order_counts' => $this->prepare_chart_data( $this->report_data->order_counts, 'post_date', 'count', $this->chart_interval, $this->start_date, $this->chart_groupby ),
  595. 'order_item_counts' => $this->prepare_chart_data( $this->report_data->order_items, 'post_date', 'order_item_count', $this->chart_interval, $this->start_date, $this->chart_groupby ),
  596. 'order_amounts' => $this->prepare_chart_data( $this->report_data->orders, 'post_date', 'total_sales', $this->chart_interval, $this->start_date, $this->chart_groupby ),
  597. 'coupon_amounts' => $this->prepare_chart_data( $this->report_data->coupons, 'post_date', 'discount_amount', $this->chart_interval, $this->start_date, $this->chart_groupby ),
  598. 'shipping_amounts' => $this->prepare_chart_data( $this->report_data->orders, 'post_date', 'total_shipping', $this->chart_interval, $this->start_date, $this->chart_groupby ),
  599. 'refund_amounts' => $this->prepare_chart_data( $this->report_data->refund_lines, 'post_date', 'total_refund', $this->chart_interval, $this->start_date, $this->chart_groupby ),
  600. 'shipping_tax_amounts' => $this->prepare_chart_data( $this->report_data->orders, 'post_date', 'total_shipping_tax', $this->chart_interval, $this->start_date, $this->chart_groupby ),
  601. 'tax_amounts' => $this->prepare_chart_data( $this->report_data->orders, 'post_date', 'total_tax', $this->chart_interval, $this->start_date, $this->chart_groupby ),
  602. 'net_order_amounts' => array(),
  603. 'gross_order_amounts' => array(),
  604. );
  605. foreach ( $data['order_amounts'] as $order_amount_key => $order_amount_value ) {
  606. $data['gross_order_amounts'][ $order_amount_key ] = $order_amount_value;
  607. $data['gross_order_amounts'][ $order_amount_key ][1] -= $data['refund_amounts'][ $order_amount_key ][1];
  608. $data['net_order_amounts'][ $order_amount_key ] = $order_amount_value;
  609. // subtract the sum of the values from net order amounts
  610. $data['net_order_amounts'][ $order_amount_key ][1] -=
  611. $data['refund_amounts'][ $order_amount_key ][1] +
  612. $data['shipping_amounts'][ $order_amount_key ][1] +
  613. $data['shipping_tax_amounts'][ $order_amount_key ][1] +
  614. $data['tax_amounts'][ $order_amount_key ][1];
  615. }
  616. // 3rd party filtering of report data
  617. $data = apply_filters( 'woocommerce_admin_report_chart_data', $data );
  618. // Encode in json format
  619. $chart_data = json_encode(
  620. array(
  621. 'order_counts' => array_values( $data['order_counts'] ),
  622. 'order_item_counts' => array_values( $data['order_item_counts'] ),
  623. 'order_amounts' => array_map( array( $this, 'round_chart_totals' ), array_values( $data['order_amounts'] ) ),
  624. 'gross_order_amounts' => array_map( array( $this, 'round_chart_totals' ), array_values( $data['gross_order_amounts'] ) ),
  625. 'net_order_amounts' => array_map( array( $this, 'round_chart_totals' ), array_values( $data['net_order_amounts'] ) ),
  626. 'shipping_amounts' => array_map( array( $this, 'round_chart_totals' ), array_values( $data['shipping_amounts'] ) ),
  627. 'coupon_amounts' => array_map( array( $this, 'round_chart_totals' ), array_values( $data['coupon_amounts'] ) ),
  628. 'refund_amounts' => array_map( array( $this, 'round_chart_totals' ), array_values( $data['refund_amounts'] ) ),
  629. )
  630. );
  631. ?>
  632. <div class="chart-container">
  633. <div class="chart-placeholder main"></div>
  634. </div>
  635. <script type="text/javascript">
  636. var main_chart;
  637. jQuery(function(){
  638. var order_data = jQuery.parseJSON( '<?php echo $chart_data; ?>' );
  639. var drawGraph = function( highlight ) {
  640. var series = [
  641. {
  642. label: "<?php echo esc_js( __( 'Number of items sold', 'woocommerce' ) ); ?>",
  643. data: order_data.order_item_counts,
  644. color: '<?php echo $this->chart_colours['item_count']; ?>',
  645. bars: { fillColor: '<?php echo $this->chart_colours['item_count']; ?>', fill: true, show: true, lineWidth: 0, barWidth: <?php echo $this->barwidth; ?> * 0.5, align: 'center' },
  646. shadowSize: 0,
  647. hoverable: false
  648. },
  649. {
  650. label: "<?php echo esc_js( __( 'Number of orders', 'woocommerce' ) ); ?>",
  651. data: order_data.order_counts,
  652. color: '<?php echo $this->chart_colours['order_count']; ?>',
  653. bars: { fillColor: '<?php echo $this->chart_colours['order_count']; ?>', fill: true, show: true, lineWidth: 0, barWidth: <?php echo $this->barwidth; ?> * 0.5, align: 'center' },
  654. shadowSize: 0,
  655. hoverable: false
  656. },
  657. {
  658. label: "<?php echo esc_js( __( 'Average gross sales amount', 'woocommerce' ) ); ?>",
  659. data: [ [ <?php echo min( array_keys( $data['order_amounts'] ) ); ?>, <?php echo $this->report_data->average_total_sales; ?> ], [ <?php echo max( array_keys( $data['order_amounts'] ) ); ?>, <?php echo $this->report_data->average_total_sales; ?> ] ],
  660. yaxis: 2,
  661. color: '<?php echo $this->chart_colours['average']; ?>',
  662. points: { show: false },
  663. lines: { show: true, lineWidth: 2, fill: false },
  664. shadowSize: 0,
  665. hoverable: false
  666. },
  667. {
  668. label: "<?php echo esc_js( __( 'Average net sales amount', 'woocommerce' ) ); ?>",
  669. data: [ [ <?php echo min( array_keys( $data['order_amounts'] ) ); ?>, <?php echo $this->report_data->average_sales; ?> ], [ <?php echo max( array_keys( $data['order_amounts'] ) ); ?>, <?php echo $this->report_data->average_sales; ?> ] ],
  670. yaxis: 2,
  671. color: '<?php echo $this->chart_colours['net_average']; ?>',
  672. points: { show: false },
  673. lines: { show: true, lineWidth: 2, fill: false },
  674. shadowSize: 0,
  675. hoverable: false
  676. },
  677. {
  678. label: "<?php echo esc_js( __( 'Coupon amount', 'woocommerce' ) ); ?>",
  679. data: order_data.coupon_amounts,
  680. yaxis: 2,
  681. color: '<?php echo $this->chart_colours['coupon_amount']; ?>',
  682. points: { show: true, radius: 5, lineWidth: 2, fillColor: '#fff', fill: true },
  683. lines: { show: true, lineWidth: 2, fill: false },
  684. shadowSize: 0,
  685. <?php echo $this->get_currency_tooltip(); ?>
  686. },
  687. {
  688. label: "<?php echo esc_js( __( 'Shipping amount', 'woocommerce' ) ); ?>",
  689. data: order_data.shipping_amounts,
  690. yaxis: 2,
  691. color: '<?php echo $this->chart_colours['shipping_amount']; ?>',
  692. points: { show: true, radius: 5, lineWidth: 2, fillColor: '#fff', fill: true },
  693. lines: { show: true, lineWidth: 2, fill: false },
  694. shadowSize: 0,
  695. prepend_tooltip: "<?php echo get_woocommerce_currency_symbol(); ?>"
  696. },
  697. {
  698. label: "<?php echo esc_js( __( 'Gross sales amount', 'woocommerce' ) ); ?>",
  699. data: order_data.gross_order_amounts,
  700. yaxis: 2,
  701. color: '<?php echo $this->chart_colours['sales_amount']; ?>',
  702. points: { show: true, radius: 5, lineWidth: 2, fillColor: '#fff', fill: true },
  703. lines: { show: true, lineWidth: 2, fill: false },
  704. shadowSize: 0,
  705. <?php echo $this->get_currency_tooltip(); ?>
  706. },
  707. {
  708. label: "<?php echo esc_js( __( 'Net sales amount', 'woocommerce' ) ); ?>",
  709. data: order_data.net_order_amounts,
  710. yaxis: 2,
  711. color: '<?php echo $this->chart_colours['net_sales_amount']; ?>',
  712. points: { show: true, radius: 6, lineWidth: 4, fillColor: '#fff', fill: true },
  713. lines: { show: true, lineWidth: 5, fill: false },
  714. shadowSize: 0,
  715. <?php echo $this->get_currency_tooltip(); ?>
  716. },
  717. {
  718. label: "<?php echo esc_js( __( 'Refund amount', 'woocommerce' ) ); ?>",
  719. data: order_data.refund_amounts,
  720. yaxis: 2,
  721. color: '<?php echo $this->chart_colours['refund_amount']; ?>',
  722. points: { show: true, radius: 5, lineWidth: 2, fillColor: '#fff', fill: true },
  723. lines: { show: true, lineWidth: 2, fill: false },
  724. shadowSize: 0,
  725. prepend_tooltip: "<?php echo get_woocommerce_currency_symbol(); ?>"
  726. },
  727. ];
  728. if ( highlight !== 'undefined' && series[ highlight ] ) {
  729. highlight_series = series[ highlight ];
  730. highlight_series.color = '#9c5d90';
  731. if ( highlight_series.bars ) {
  732. highlight_series.bars.fillColor = '#9c5d90';
  733. }
  734. if ( highlight_series.lines ) {
  735. highlight_series.lines.lineWidth = 5;
  736. }
  737. }
  738. main_chart = jQuery.plot(
  739. jQuery('.chart-placeholder.main'),
  740. series,
  741. {
  742. legend: {
  743. show: false
  744. },
  745. grid: {
  746. color: '#aaa',
  747. borderColor: 'transparent',
  748. borderWidth: 0,
  749. hoverable: true
  750. },
  751. xaxes: [ {
  752. color: '#aaa',
  753. position: "bottom",
  754. tickColor: 'transparent',
  755. mode: "time",
  756. timeformat: "<?php echo ( 'day' === $this->chart_groupby ) ? '%d %b' : '%b'; ?>",
  757. monthNames: <?php echo json_encode( array_values( $wp_locale->month_abbrev ) ); ?>,
  758. tickLength: 1,
  759. minTickSize: [1, "<?php echo $this->chart_groupby; ?>"],
  760. font: {
  761. color: "#aaa"
  762. }
  763. } ],
  764. yaxes: [
  765. {
  766. min: 0,
  767. minTickSize: 1,
  768. tickDecimals: 0,
  769. color: '#d4d9dc',
  770. font: { color: "#aaa" }
  771. },
  772. {
  773. position: "right",
  774. min: 0,
  775. tickDecimals: 2,
  776. alignTicksWithAxis: 1,
  777. color: 'transparent',
  778. font: { color: "#aaa" }
  779. }
  780. ],
  781. }
  782. );
  783. jQuery('.chart-placeholder').resize();
  784. }
  785. drawGraph();
  786. jQuery('.highlight_series').hover(
  787. function() {
  788. drawGraph( jQuery(this).data('series') );
  789. },
  790. function() {
  791. drawGraph();
  792. }
  793. );
  794. });
  795. </script>
  796. <?php
  797. }
  798. }