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

WPInv_Items_Report_Table::get_columns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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_Items_Reports_Table Class
17
 *
18
 * Renders the Gateway Reports table
19
 *
20
 * @since 1.0.19
21
 */
22
class WPInv_Items_Report_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 'item';
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
			'item'     => __( 'Item', 'invoicing' ),
84
			'sales'    => __( 'Quantity Sold', 'invoicing' ),
85
			'total'    => __( 'Total Earnings', 'invoicing' ),
86
			'discount' => __( 'Total Discounts', 'invoicing' ),
87
			'tax'      => __( 'Total Taxes', 'invoicing' ),
88
		);
89
90
	}
91
92
	/**
93
	 * Retrieve the current page number
94
	 *
95
	 * @since 1.0.19
96
	 * @return int Current page number
97
	 */
98
	public function get_paged() {
99
		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
100
	}
101
102
	/**
103
	 * Outputs the reporting views
104
	 *
105
	 * @since 1.0.19
106
	 * @return void
107
	 */
108
	public function bulk_actions( $which = '' ) {
109
		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...
110
	}
111
112
	/**
113
	 * Build all the reports data
114
	 *
115
	 * @since 1.0.19
116
	 * @return array $reports_data All the data for gateway reports
117
	 */
118
	public function reports_data() {
119
120
		$reports_data = $this->revenue_reports_data();
121
122
		$prepared = array();
123
		foreach ( $reports_data as $report_data ) {
124
			$prepared[] = array(
125
				'item'     => $report_data['item_name'],
126
				'sales'    => $report_data['sales'],
127
				'total'    => wpinv_price( $report_data['total'] ),
128
				'discount' => wpinv_price( $report_data['discount'] ),
129
				'tax'      => wpinv_price( $report_data['tax'] ),
130
			);
131
		}
132
133
		return $prepared;
134
	}
135
136
	/**
137
	 * Retrieves report data.
138
	 *
139
	 * @since 1.0.19
140
	 */
141
	public function revenue_reports_data() {
142
		global $wpdb;
143
144
		$table =  $wpdb->prefix . 'getpaid_invoice_items';
145
		return $wpdb->get_results(
146
			"SELECT
147
				SUM(quantity) as sales,
148
				item_name,
149
				SUM(tax) as tax,
150
				SUM(discount) as discount,
151
				SUM(price) as total
152
			FROM $table
153
			LEFT JOIN $wpdb->posts ON $table.post_id = $wpdb->posts.ID
154
			WHERE
155
				$wpdb->posts.post_type = 'wpi_invoice'
156
                AND ( $wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'renewal' )
157
			GROUP BY item_id
158
			ORDER BY item_name ASC", ARRAY_A);
159
160
	}
161
162
	/**
163
	 * Setup the final data for the table
164
	 *
165
	 * @since 1.0.19
166
	 * @return void
167
	 */
168
	public function prepare_items() {
169
		$columns               = $this->get_columns();
170
		$hidden                = array(); // No hidden columns
171
		$sortable              = $this->get_sortable_columns();
172
		$this->_column_headers = array( $columns, $hidden, $sortable );
173
		$this->items           = $this->reports_data();
174
	}
175
}
176