|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Contains the class that displays the items report. |
|
4
|
|
|
* |
|
5
|
|
|
* |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
defined( 'ABSPATH' ) || exit; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* GetPaid_Reports_Report_Items Class. |
|
12
|
|
|
*/ |
|
13
|
|
|
class GetPaid_Reports_Report_Items extends GetPaid_Reports_Abstract_Report { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Retrieves the earning sql. |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
public function get_sql( $range ) { |
|
20
|
|
|
global $wpdb; |
|
21
|
|
|
|
|
22
|
|
|
$table = $wpdb->prefix . 'getpaid_invoices'; |
|
23
|
|
|
$table2 = $wpdb->prefix . 'getpaid_invoice_items'; |
|
24
|
|
|
$clauses = $this->get_range_sql( $range ); |
|
25
|
|
|
|
|
26
|
|
|
$sql = "SELECT |
|
27
|
|
|
item.item_name AS item_name, |
|
28
|
|
|
item.item_id AS item_id, |
|
29
|
|
|
SUM(price) as total |
|
30
|
|
|
FROM $wpdb->posts |
|
31
|
|
|
LEFT JOIN $table as meta ON meta.post_id = $wpdb->posts.ID |
|
32
|
|
|
INNER JOIN $table2 as item ON item.post_id = wp_posts.ID |
|
33
|
|
|
WHERE meta.post_id IS NOT NULL |
|
34
|
|
|
AND $wpdb->posts.post_type = 'wpi_invoice' |
|
35
|
|
|
AND ( $wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'wpi-renewal' ) |
|
36
|
|
|
AND {$clauses[1]} |
|
37
|
|
|
GROUP BY item_id |
|
38
|
|
|
ORDER BY total DESC |
|
39
|
|
|
"; |
|
40
|
|
|
|
|
41
|
|
|
return apply_filters( 'getpaid_items_graphs_get_sql', $sql, $range ); |
|
42
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Prepares the report stats. |
|
47
|
|
|
* |
|
48
|
|
|
*/ |
|
49
|
|
|
public function prepare_stats() { |
|
50
|
|
|
global $wpdb; |
|
51
|
|
|
$this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) ); |
|
52
|
|
|
$this->stats = $this->normalize_stats( $this->stats ); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Normalizes the report stats. |
|
57
|
|
|
* |
|
58
|
|
|
*/ |
|
59
|
|
|
public function normalize_stats( $stats ) { |
|
60
|
|
|
$normalized = array(); |
|
61
|
|
|
$others = 0; |
|
62
|
|
|
$did = 0; |
|
63
|
|
|
|
|
64
|
|
|
foreach ( $stats as $stat ) { |
|
65
|
|
|
|
|
66
|
|
|
if ( $did > 4 ) { |
|
67
|
|
|
|
|
68
|
|
|
$others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ); |
|
69
|
|
|
|
|
70
|
|
|
} else { |
|
71
|
|
|
|
|
72
|
|
|
$normalized[] = array( |
|
73
|
|
|
'total' => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ), |
|
74
|
|
|
'item_name' => strip_tags( $stat->item_name ), |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$did++; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ( $others > 0 ) { |
|
83
|
|
|
|
|
84
|
|
|
$normalized[] = array( |
|
85
|
|
|
'total' => wpinv_round_amount( wpinv_sanitize_amount( $others ) ), |
|
86
|
|
|
'item_name' => esc_html__( 'Others', 'invoicing' ), |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $normalized; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Retrieves report data. |
|
96
|
|
|
* |
|
97
|
|
|
*/ |
|
98
|
|
|
public function get_data() { |
|
99
|
|
|
|
|
100
|
|
|
$data = wp_list_pluck( $this->stats, 'total' ); |
|
101
|
|
|
$colors = array( '#007bff','#28a745','#333333','#c3e6cb','#dc3545','#6c757d', '#00bcd4', '#00695C', '#03a9f4', '#2196f3', '#3f51b5', '#673ab7', '#9c27b0', '#e91e63', '#f44336', '#8bc34a', '#4caf50' ); |
|
102
|
|
|
|
|
103
|
|
|
shuffle( $colors ); |
|
104
|
|
|
|
|
105
|
|
|
return array( |
|
106
|
|
|
'data' => $data, |
|
107
|
|
|
'backgroundColor' => $colors, |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Retrieves report labels. |
|
114
|
|
|
* |
|
115
|
|
|
*/ |
|
116
|
|
|
public function get_labels() { |
|
117
|
|
|
return wp_list_pluck( $this->stats, 'item_name' ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Displays the actual report. |
|
122
|
|
|
* |
|
123
|
|
|
*/ |
|
124
|
|
|
public function display_stats() { |
|
125
|
|
|
?> |
|
126
|
|
|
|
|
127
|
|
|
<canvas id="getpaid-chartjs-earnings-items"></canvas> |
|
128
|
|
|
|
|
129
|
|
|
<script> |
|
130
|
|
|
window.addEventListener( 'DOMContentLoaded', function() { |
|
131
|
|
|
|
|
132
|
|
|
var ctx = document.getElementById( 'getpaid-chartjs-earnings-items' ).getContext('2d'); |
|
133
|
|
|
new Chart( |
|
134
|
|
|
ctx, |
|
135
|
|
|
{ |
|
136
|
|
|
type: 'doughnut', |
|
137
|
|
|
data: { |
|
138
|
|
|
'labels': <?php echo wp_json_encode( $this->get_labels() ); ?>, |
|
|
|
|
|
|
139
|
|
|
'datasets': [ <?php echo wp_json_encode( $this->get_data() ); ?> ] |
|
|
|
|
|
|
140
|
|
|
}, |
|
141
|
|
|
options: { |
|
142
|
|
|
legend: { |
|
143
|
|
|
display: false |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
}) |
|
150
|
|
|
|
|
151
|
|
|
</script> |
|
152
|
|
|
|
|
153
|
|
|
<?php |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|