Passed
Push — master ( bdf164...c6d931 )
by Brian
05:05
created

GetPaid_Invoice_Exporter::prepare_row()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 27
rs 9.5222
1
<?php
2
/**
3
 * Contains the class that exports invoices.
4
 *
5
 *
6
 */
7
8
defined( 'ABSPATH' ) || exit;
9
10
/**
11
 * GetPaid_Invoice_Exporter Class.
12
 */
13
class GetPaid_Invoice_Exporter extends GetPaid_Graph_Downloader {
14
15
	/**
16
	 * Retrieves invoices query args.
17
	 * 
18
	 * @param string $post_type post type to retrieve.
19
	 * @param array $args Args to search for.
20
	 * @return array
21
	 */
22
	public function get_invoice_query_args( $post_type, $args ) {
23
24
		$query_args = array(
25
			'post_type'              => $post_type,
26
			'post_status'            => array_keys( wpinv_get_invoice_statuses( true, false, $post_type ) ),
27
			'posts_per_page'         => -1,
28
			'no_found_rows'          => true,
29
			'update_post_term_cache' => false,
30
			'fields'                 => 'ids',
31
		);
32
33
		if ( ! empty( $args['status'] ) && in_array( $args['status'], $query_args['post_status'], true ) ) {
34
			$query_args['post_status'] = wpinv_clean( wpinv_parse_list( $args['status'] ) );
35
		}
36
37
		$date_query = array();
38
		if ( ! empty( $args['to_date'] ) ) {
39
			$date_query['before'] = wpinv_clean( $args['to_date'] );
40
		}
41
42
		if ( ! empty( $args['from_date'] ) ) {
43
			$date_query['after'] = wpinv_clean( $args['from_date'] );
44
		}
45
46
		if ( ! empty( $date_query ) ) {
47
			$date_query['inclusive']  = true;
48
			$query_args['date_query'] = array( $date_query );
49
		}
50
51
		return $query_args;
52
	}
53
54
	/**
55
	 * Retrieves invoices.
56
	 * 
57
	 * @param array $query_args WP_Query args.
58
	 * @return WPInv_Invoice[]
59
	 */
60
	public function get_invoices( $query_args ) {
61
62
		// Get invoices.
63
		$invoices = new WP_Query( $query_args );
64
65
		// Prepare the results.
66
		return array_map( 'wpinv_get_invoice', $invoices->posts );
67
68
	}
69
70
	/**
71
	 * Handles the actual download.
72
	 *
73
	 */
74
	public function export( $post_type, $args ) {
75
76
		$invoices  = $this->get_invoices( $this->get_invoice_query_args( $post_type, $args ) );
77
		$stream    = $this->prepare_output();
78
		$headers   = $this->get_export_fields( $post_type );
79
		$file_type = $this->prepare_file_type( strtolower( getpaid_get_post_type_label( $post_type ) ) );
80
81
		if ( 'csv' == $file_type ) {
82
			$this->download_csv( $invoices, $stream, $headers );
83
		} else if( 'xml' == $file_type ) {
84
			$this->download_xml( $invoices, $stream, $headers );
85
		} else {
86
			$this->download_json( $invoices, $stream, $headers );
87
		}
88
89
		fclose( $stream );
90
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
91
	}
92
93
	/**
94
	 * Prepares a single invoice for download.
95
	 *
96
	 * @param WPInv_Invoice $invoice The invoice to prepare..
97
	 * @param array $fields The fields to stream.
98
	 * @since       1.0.19
99
	 * @return array
100
	 */
101
	public function prepare_row( $invoice, $fields ) {
102
103
		$prepared      = array();
104
		$amount_fields = $this->get_amount_fields( $invoice->get_post_type() );
105
106
		foreach ( $fields as $field ) {
107
108
			$value  = '';
109
			$method = "get_$field";
110
111
			if ( method_exists( $invoice, $method ) ) {
112
				$value  = $invoice->$method();
113
			}
114
115
			if ( in_array( $field, $amount_fields ) ) {
116
				$value  = wpinv_round_amount( wpinv_sanitize_amount( $value ) );
117
			}
118
119
			if ( $field === 'items' ) {
120
				$value  = $invoice->get_cart_details();
121
			}
122
123
			$prepared[ $field ] = wpinv_clean( $value );
124
125
		}
126
127
		return $prepared;
128
	}
129
130
	/**
131
	 * Retrieves export fields.
132
	 *
133
	 * @param string $post_type
134
	 * @since       1.0.19
135
	 * @return array
136
	 */
137
	public function get_export_fields( $post_type ) {
138
139
		$fields = array(
140
			'id',
141
			'parent_id',
142
			'status',
143
			'date_created',
144
			'date_modified',
145
			'date_due',
146
			'date_completed',
147
			'number',
148
			'key',
149
			'description',
150
			'post_type',
151
			'mode',
152
			'customer_id',
153
			'customer_first_name',
154
			'customer_last_name',
155
			'customer_phone',
156
			'customer_email',
157
			'customer_country',
158
			'customer_city',
159
			'customer_state',
160
			'customer_zip',
161
			'customer_company',
162
			'customer_vat_number',
163
			'customer_address',
164
			'subtotal',
165
			'total_discount',
166
			'total_tax',
167
			'total_fees',
168
			'fees',
169
			'discounts',
170
			'taxes',
171
			'items',
172
			'payment_form',
173
			'discount_code',
174
			'gateway',
175
			'transaction_id',
176
			'currency',
177
			'disable_taxes',
178
			'subscription_id',
179
			'remote_subscription_id',
180
			'is_viewed',
181
			'email_cc',
182
			'template',
183
			'created_via'
184
    	);
185
186
		return apply_filters( 'getpaid_invoice_exporter_get_fields', $fields, $post_type );
187
	}
188
189
	/**
190
	 * Retrieves amount fields.
191
	 *
192
	 * @param string $post_type
193
	 * @since       1.0.19
194
	 * @return array
195
	 */
196
	public function get_amount_fields( $post_type ) {
197
198
		$fields = array(
199
			'subtotal',
200
			'total_discount',
201
			'total_tax',
202
			'total_fees'
203
    	);
204
205
		return apply_filters( 'getpaid_invoice_exporter_get_amount_fields', $fields, $post_type );
206
	}
207
208
}
209