Passed
Push — master ( 25dd78...14f7c3 )
by Brian
15:22 queued 10:50
created

WPInv_Taxes_Reports_Table   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 168
rs 10
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A column_default() 0 2 1
A get_paged() 0 2 2
A bulk_actions() 0 2 1
A taxes_reports_data() 0 19 2
A reports_data() 0 34 3
A __construct() 0 7 1
A get_columns() 0 5 1
A get_primary_column_name() 0 2 1
A prepare_items() 0 6 1
1
<?php
2
/**
3
 * Gateways Reports Table Class
4
 *
5
 */
6
7
// Exit if accessed directly
8
if ( ! defined( 'ABSPATH' ) ) exit;
9
10
// Load WP_List_Table if not loaded
11
if ( ! class_exists( 'WP_List_Table' ) ) {
12
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
13
}
14
15
/**
16
 * WPInv_Taxes_Reports_Table Class
17
 *
18
 * Renders the Gateway Reports table
19
 *
20
 * @since 1.0.19
21
 */
22
class WPInv_Taxes_Reports_Table extends WP_List_Table {
23
24
	/**
25
	 * @var int Number of items per page
26
	 * @since 1.0.19
27
	 */
28
	public $per_page = 300;
29
30
31
	/**
32
	 * Get things started
33
	 *
34
	 * @since 1.0.19
35
	 * @see WP_List_Table::__construct()
36
	 */
37
	public function __construct() {
38
39
		// Set parent defaults
40
		parent::__construct( array(
41
			'singular' => 'id',
42
			'plural'   => 'ids',
43
			'ajax'     => false,
44
		) );
45
46
	}
47
48
	/**
49
	 * Gets the name of the primary column.
50
	 *
51
	 * @since 1.0.19
52
	 * @access protected
53
	 *
54
	 * @return string Name of the primary column.
55
	 */
56
	protected function get_primary_column_name() {
57
		return 'month';
58
	}
59
60
	/**
61
	 * This function renders most of the columns in the list table.
62
	 *
63
	 * @since 1.0.19
64
	 *
65
	 * @param array $item Contains all the data of the gateways
66
	 * @param string $column_name The name of the column
67
	 *
68
	 * @return string Column Name
69
	 */
70
	public function column_default( $item, $column_name ) {
71
		return esc_html( $item[ $column_name ] );
72
	}
73
74
	/**
75
	 * Retrieve the table columns
76
	 *
77
	 * @since 1.0.19
78
	 * @return array $columns Array of all the list table columns
79
	 */
80
	public function get_columns() {
81
82
		return array(
83
			'month'    => __( 'Month', 'invoicing' ),
84
			'tax'      => __( 'Total Taxes', 'invoicing' ),
85
		);
86
87
	}
88
89
	/**
90
	 * Retrieve the current page number
91
	 *
92
	 * @since 1.0.19
93
	 * @return int Current page number
94
	 */
95
	public function get_paged() {
96
		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
97
	}
98
99
	/**
100
	 * Outputs the reporting views
101
	 *
102
	 * @since 1.0.19
103
	 * @return void
104
	 */
105
	public function bulk_actions( $which = '' ) {
106
		return array();
0 ignored issues
show
Bug Best Practice introduced by
The expression return array() returns the type array which is incompatible with the documented return type void.
Loading history...
107
	}
108
109
	/**
110
	 * Build all the reports data
111
	 *
112
	 * @since 1.0.19
113
	 * @return array $reports_data All the data for taxes reports
114
	 */
115
	public function reports_data() {
116
117
		$reports_data = $this->taxes_reports_data();
118
		$months       = array(
119
			'1' => __( 'January', 'invoicing' ),
120
			'2' => __( 'February', 'invoicing' ),
121
			'3' => __( 'March', 'invoicing' ),
122
			'4' => __( 'April', 'invoicing' ),
123
			'5' => __( 'May', 'invoicing' ),
124
			'6' => __( 'June', 'invoicing' ),
125
			'7' => __( 'July', 'invoicing' ),
126
			'8' => __( 'August', 'invoicing' ),
127
			'9' => __( 'September', 'invoicing' ),
128
			'10' => __( 'October', 'invoicing' ),
129
			'11' => __( 'November', 'invoicing' ),
130
			'12' => __( 'December', 'invoicing' ),
131
		);
132
133
		$prepared = array();
134
		foreach ( $months as $month => $label ) {
135
136
			$tax = wpinv_price( 0 );
137
			if ( ! empty( $reports_data[ $month ] ) ) {
138
				$tax = wpinv_price( $reports_data[ $month ] );
139
			}
140
141
			$prepared[] = array(
142
				'month'    => $label,
143
				'tax'      => $tax,
144
			);
145
146
		}
147
148
		return $prepared;
149
	}
150
151
	/**
152
	 * Retrieves taxes data.
153
	 *
154
	 * @since 1.0.19
155
	 */
156
	public function taxes_reports_data() {
157
		global $wpdb;
158
159
		$table =  $wpdb->prefix . 'getpaid_invoices';
160
		$year  = isset( $_GET['year'] ) ? absint( $_GET['year'] ) : date( 'Y' );
161
		$data  = $wpdb->get_results(
162
			"SELECT
163
				MONTH(meta.completed_date) as _month,
164
				SUM(meta.tax) as tax
165
			FROM $wpdb->posts as posts
166
			LEFT JOIN $table as meta ON meta.post_id = posts.ID
167
			WHERE
168
				meta.post_id IS NOT NULL
169
				AND posts.post_type = 'wpi_invoice'
170
                AND ( posts.post_status = 'publish' OR posts.post_status = 'renewal' )
171
				AND ( YEAR(meta.completed_date) = '$year' )
172
			GROUP BY MONTH(meta.completed_date)");
173
174
		return wp_list_pluck( $data, 'tax', '_month' );
175
176
	}
177
178
	/**
179
	 * Setup the final data for the table
180
	 *
181
	 * @since 1.0.19
182
	 * @return void
183
	 */
184
	public function prepare_items() {
185
		$columns               = $this->get_columns();
186
		$hidden                = array(); // No hidden columns
187
		$sortable              = $this->get_sortable_columns();
188
		$this->_column_headers = array( $columns, $hidden, $sortable );
189
		$this->items           = $this->reports_data();
190
	}
191
}
192