|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Contains the main API class |
|
4
|
|
|
* |
|
5
|
|
|
* @since 1.0.0 |
|
6
|
|
|
* @package Invoicing |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
// MUST have WordPress. |
|
10
|
|
|
if ( !defined( 'WPINC' ) ) { |
|
11
|
|
|
exit; |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The main API class |
|
16
|
|
|
*/ |
|
17
|
|
|
class WPInv_API { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param string A prefix for our REST routes |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $api_namespace = ''; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @since 1.0.13 |
|
28
|
|
|
* Sets the API namespace and inits hooks |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct( $api_namespace = 'invoicing/v1' ) { |
|
31
|
|
|
$this->api_namespace = apply_filters( 'invoicing_api_namespace', $api_namespace ); |
|
32
|
|
|
|
|
33
|
|
|
//Register REST routes |
|
34
|
|
|
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Registers routes |
|
40
|
|
|
* |
|
41
|
|
|
* @since 1.0.13 |
|
42
|
|
|
*/ |
|
43
|
|
|
public function register_rest_routes() { |
|
44
|
|
|
|
|
45
|
|
|
//Invoices |
|
46
|
|
|
register_rest_route( |
|
47
|
|
|
$this->api_namespace, |
|
48
|
|
|
'/invoices', |
|
49
|
|
|
array( |
|
50
|
|
|
|
|
51
|
|
|
//Create a single invoice |
|
52
|
|
|
array( |
|
53
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
54
|
|
|
'callback' => array( $this, 'insert_invoice' ), |
|
55
|
|
|
'permission_callback' => array( $this, 'can_manage_options' ), |
|
56
|
|
|
), |
|
57
|
|
|
|
|
58
|
|
|
) |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Checks if the current user can manage options |
|
65
|
|
|
* |
|
66
|
|
|
* @since 1.0.13 |
|
67
|
|
|
* @param WP_REST_Request $request |
|
68
|
|
|
*/ |
|
69
|
|
|
public function can_manage_options( $request ) { |
|
|
|
|
|
|
70
|
|
|
return current_user_can( 'manage_options' ); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Creates a new invoice |
|
75
|
|
|
* |
|
76
|
|
|
* @param WP_REST_Request $request |
|
77
|
|
|
* @return mixed WP_Error or invoice data |
|
78
|
|
|
*/ |
|
79
|
|
|
public function insert_invoice( $request ) { |
|
80
|
|
|
|
|
81
|
|
|
// Fetch invoice data from the request |
|
82
|
|
|
$invoice_data = wp_unslash( $request->get_params() ); |
|
83
|
|
|
|
|
84
|
|
|
// Abort if no invoice data is provided |
|
85
|
|
|
if( empty( $invoice_data ) ) { |
|
86
|
|
|
return new WP_Error( 'missing_data', __( 'Invoice data not provided', 'invoicing' ) ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// Try creating the invoice |
|
90
|
|
|
$invoice = wpinv_insert_invoice( $invoice_data, true ); |
|
91
|
|
|
|
|
92
|
|
|
if ( is_wp_error( $invoice ) ) { |
|
93
|
|
|
return $invoice; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Fetch invoice data ... |
|
97
|
|
|
$invoice_data = get_object_vars( $invoice ); |
|
98
|
|
|
|
|
99
|
|
|
// ... and formart some of it |
|
100
|
|
|
foreach( $invoice_data as $key => $value ) { |
|
101
|
|
|
$invoice_data[ $key ] = $invoice->get( $key ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
//Return the invoice data |
|
105
|
|
|
return rest_ensure_response( $invoice_data ); |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function send_status( $code ) { |
|
110
|
|
|
status_header( $code ); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function set_billing_details( $invoice, $data ) { |
|
114
|
|
|
$address_fields = array( |
|
115
|
|
|
'user_id', |
|
116
|
|
|
'first_name', |
|
117
|
|
|
'last_name', |
|
118
|
|
|
'company', |
|
119
|
|
|
'vat_number', |
|
120
|
|
|
'email', |
|
121
|
|
|
'phone', |
|
122
|
|
|
'address', |
|
123
|
|
|
'city', |
|
124
|
|
|
'state', |
|
125
|
|
|
'country', |
|
126
|
|
|
'zip', |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
$billing_details = array(); |
|
|
|
|
|
|
130
|
|
|
$user_id = $invoice->get_user_id(); |
|
131
|
|
|
|
|
132
|
|
|
foreach ( $address_fields as $field ) { |
|
133
|
|
|
if ( isset( $data['billing_details'][ $field ] ) ) { |
|
134
|
|
|
$value = sanitize_text_field( $data['billing_details'][ $field ] ); |
|
135
|
|
|
|
|
136
|
|
View Code Duplication |
if ( $field == 'country' && empty( $value ) ) { |
|
137
|
|
|
if ( !empty( $invoice->country ) ) { |
|
138
|
|
|
$value = $invoice->country; |
|
139
|
|
|
} else { |
|
140
|
|
|
$value = wpinv_default_billing_country( '', $user_id ); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
View Code Duplication |
if ( $field == 'state' && empty( $value ) ) { |
|
145
|
|
|
if ( !empty( $invoice->state ) ) { |
|
146
|
|
|
$value = $invoice->state; |
|
147
|
|
|
} else { |
|
148
|
|
|
$value = wpinv_get_default_state(); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$invoice->set( $field, $value ); |
|
153
|
|
|
|
|
154
|
|
|
update_post_meta( $invoice->ID, '_wpinv_' . $field, $value ); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $invoice; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
protected function set_discount( $invoice, $data ) { |
|
162
|
|
|
if ( isset( $data['discount'] ) ) { |
|
163
|
|
|
$invoice->set( 'discount', wpinv_round_amount( $data['discount'] ) ); |
|
164
|
|
|
|
|
165
|
|
|
update_post_meta( $invoice->ID, '_wpinv_discount', wpinv_round_amount( $data['discount'] ) ); |
|
166
|
|
|
|
|
167
|
|
|
if ( isset( $data['discount_code'] ) ) { |
|
168
|
|
|
$invoice->set( 'discount_code', $data['discount_code'] ); |
|
169
|
|
|
|
|
170
|
|
|
update_post_meta( $invoice->ID, '_wpinv_discount_code', $data['discount_code'] ); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $invoice; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
protected function set_items( $invoice, $data ) { |
|
178
|
|
|
if ( !empty( $data['items'] ) && is_array( $data['items'] ) ) { |
|
179
|
|
|
$items_array = array(); |
|
180
|
|
|
|
|
181
|
|
|
if ( !empty( $invoice->country ) ) { |
|
182
|
|
|
$country = $invoice->country; |
|
183
|
|
|
} else if ( !empty( $data['billing_details']['country'] ) ) { |
|
184
|
|
|
$country = $data['billing_details']['country']; |
|
185
|
|
|
} else { |
|
186
|
|
|
$country = wpinv_default_billing_country( '', $invoice->get_user_id() ); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
if ( !empty( $invoice->state ) ) { |
|
190
|
|
|
$state = $invoice->state; |
|
191
|
|
|
} else if ( !empty( $data['billing_details']['state'] ) ) { |
|
192
|
|
|
$state = $data['billing_details']['state']; |
|
193
|
|
|
} else { |
|
194
|
|
|
$state = wpinv_get_default_state(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$_POST['country'] = $country; |
|
198
|
|
|
$_POST['state'] = $state; |
|
199
|
|
|
|
|
200
|
|
|
$rate = wpinv_get_tax_rate( $country, $state, 'global' ); |
|
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
$total_tax = 0; |
|
203
|
|
|
foreach ( $data['items'] as $item ) { |
|
204
|
|
|
$id = isset( $item['id'] ) ? sanitize_text_field( $item['id'] ) : ''; |
|
205
|
|
|
$title = isset( $item['title'] ) ? sanitize_text_field( $item['title'] ) : ''; |
|
206
|
|
|
$desc = isset( $item['description'] ) ? sanitize_text_field( $item['description'] ) : ''; |
|
207
|
|
|
$amount = isset( $item['amount'] ) ? wpinv_round_amount( $item['amount'] ) : 0; |
|
208
|
|
|
|
|
209
|
|
|
if ( !empty( $item['vat_rates_class'] ) ) { |
|
210
|
|
|
$vat_rates_class = $item['vat_rates_class']; |
|
211
|
|
|
} else { |
|
212
|
|
|
$vat_rates_class = '_standard'; |
|
213
|
|
|
} |
|
214
|
|
|
$vat_rate = wpinv_get_tax_rate( $country, $state, $id ); |
|
215
|
|
|
|
|
216
|
|
|
$tax = $amount > 0 ? ( $amount * 0.01 * (float)$vat_rate ) : 0; |
|
217
|
|
|
$total_tax += $tax; |
|
218
|
|
|
|
|
219
|
|
|
$items_array[] = array( |
|
220
|
|
|
'id' => $id, |
|
221
|
|
|
'title' => esc_html( $title ), |
|
222
|
|
|
'description' => esc_html( $desc ), |
|
223
|
|
|
'amount' => $amount > 0 ? wpinv_round_amount( $amount ) : 0, |
|
224
|
|
|
'subtotal' => $amount > 0 ? wpinv_round_amount( $amount ) : 0, |
|
225
|
|
|
'vat_rates_class' => $vat_rates_class, |
|
226
|
|
|
'vat_rate' => $vat_rate, |
|
227
|
|
|
'tax' => $tax > 0 ? wpinv_round_amount( $tax ) : 0, |
|
228
|
|
|
); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
update_post_meta( $invoice->ID, '_wpinv_tax', wpinv_round_amount( $total_tax ) ); |
|
232
|
|
|
$invoice->set( 'tax', wpinv_round_amount( $total_tax ) ); |
|
233
|
|
|
|
|
234
|
|
|
$items_array = apply_filters( 'wpinv_save_invoice_items', $items_array, $data['items'], $invoice ); |
|
235
|
|
|
|
|
236
|
|
|
$invoice->set( 'items', $items_array ); |
|
237
|
|
|
update_post_meta( $invoice->ID, '_wpinv_items', $items_array ); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return $invoice; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
protected function set_invoice_meta( $invoice_id, $invoice_meta ) { |
|
244
|
|
|
foreach ( $invoice_meta as $meta_key => $meta_value ) { |
|
245
|
|
|
|
|
246
|
|
|
if ( is_string( $meta_key) && ! is_protected_meta( $meta_key ) && is_scalar( $meta_value ) ) { |
|
247
|
|
|
update_post_meta( $invoice_id, $meta_key, $meta_value ); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
class WPInv_API_Exception extends Exception { |
|
255
|
|
|
protected $error_code; |
|
256
|
|
|
|
|
257
|
|
|
public function __construct( $error_code, $error_message, $http_status_code ) { |
|
258
|
|
|
$this->error_code = $error_code; |
|
259
|
|
|
parent::__construct( $error_message, $http_status_code ); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
public function getErrorCode() { |
|
263
|
|
|
return $this->error_code; |
|
264
|
|
|
} |
|
265
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.