Passed
Push — master ( 2bb08e...e53b80 )
by Brian
04:30
created

GetPaid_Reports_Report_Discounts::get_labels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
/**
3
 * Contains the class that displays the discounts report.
4
 *
5
 *
6
 */
7
8
defined( 'ABSPATH' ) || exit;
9
10
/**
11
 * GetPaid_Reports_Report_Discounts Class.
12
 */
13
class GetPaid_Reports_Report_Discounts extends GetPaid_Reports_Abstract_Report {
14
15
	/**
16
	 * Retrieves the discounts sql.
17
	 *
18
	 */
19
	public function get_sql( $range ) {
20
		global $wpdb;
21
22
		$table      = $wpdb->prefix . 'getpaid_invoices';
23
		$clauses    = $this->get_range_sql( $range );
24
25
		$sql        = "SELECT
26
				meta.discount_code AS discount_code,
27
				SUM(total) as total
28
            FROM $wpdb->posts
29
            LEFT JOIN $table as meta ON meta.post_id = $wpdb->posts.ID
30
            WHERE meta.post_id IS NOT NULL
31
				AND meta.discount_code != ''
32
                AND $wpdb->posts.post_type = 'wpi_invoice'
33
                AND ( $wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'wpi-renewal' )
34
                AND {$clauses[1]}
35
            GROUP BY discount_code
36
			ORDER BY total DESC
37
        ";
38
39
		return apply_filters( 'getpaid_discounts_graphs_get_sql', $sql, $range );
40
41
	}
42
43
	/**
44
	 * Prepares the report stats.
45
	 *
46
	 */
47
	public function prepare_stats() {
48
		global $wpdb;
49
		$this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) );
50
		$this->stats = $this->normalize_stats( $this->stats );
51
	}
52
53
	/**
54
	 * Normalizes the report stats.
55
	 *
56
	 */
57
	public function normalize_stats( $stats ) {
58
		$normalized = array();
59
		$others     = 0;
60
		$did        = 0;
61
62
		foreach ( $stats as $stat ) {
63
64
			if ( $did > 4 ) {
65
66
				$others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) );
67
68
			} else {
69
70
				$normalized[] = array(
71
					'total'         => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ),
72
					'discount_code' => strip_tags( $stat->discount_code ),
73
				);
74
75
			}
76
77
			$did++;
78
		}
79
80
		if ( $others > 0 ) {
81
82
			$normalized[] = array(
83
				'total'         => wpinv_round_amount( wpinv_sanitize_amount( $others ) ),
84
				'discount_code' => esc_html__( 'Others', 'invoicing' ),
85
			);
86
87
		}
88
89
		return $normalized;
90
	}
91
92
	/**
93
	 * Retrieves report data.
94
	 *
95
	 */
96
	public function get_data() {
97
98
		$data     = wp_list_pluck( $this->stats, 'total' );
99
		$colors   = array( '#009688','#4caf50','#8bc34a','#00bcd4','#03a9f4','#2196f3' );
100
101
		shuffle( $colors );
102
103
		return array(
104
			'data'            => $data,
105
			'backgroundColor' => $colors,
106
		);
107
108
	}
109
110
	/**
111
	 * Retrieves report labels.
112
	 *
113
	 */
114
	public function get_labels() {
115
		return wp_list_pluck( $this->stats, 'discount_code' );
116
	}
117
118
	/**
119
	 * Displays the actual report.
120
	 *
121
	 */
122
	public function display_stats() {
123
		?>
124
125
			<canvas id="getpaid-chartjs-earnings-discount_code"></canvas>
126
127
			<script>
128
				window.addEventListener( 'DOMContentLoaded', function() {
129
130
					var ctx = document.getElementById( 'getpaid-chartjs-earnings-discount_code' ).getContext('2d');
131
					new Chart(
132
						ctx,
133
						{
134
							type: 'doughnut',
135
							data: {
136
								'labels': <?php echo wp_json_encode( $this->get_labels() ); ?>,
0 ignored issues
show
Bug introduced by
Are you sure wp_json_encode($this->get_labels()) of type false|string can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

136
								'labels': <?php echo /** @scrutinizer ignore-type */ wp_json_encode( $this->get_labels() ); ?>,
Loading history...
137
								'datasets': [ <?php echo wp_json_encode( $this->get_data() ); ?> ]
0 ignored issues
show
Bug introduced by
Are you sure wp_json_encode($this->get_data()) of type false|string can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
								'datasets': [ <?php echo /** @scrutinizer ignore-type */ wp_json_encode( $this->get_data() ); ?> ]
Loading history...
138
							},
139
							options: {
140
								legend: {
141
									display: false
142
								}
143
							}
144
						}
145
					);
146
147
				})
148
149
			</script>
150
151
		<?php
152
	}
153
154
}
155