Total Complexity | 441 |
Total Lines | 3754 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like WPInv_Invoice often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WPInv_Invoice, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class WPInv_Invoice extends GetPaid_Data { |
||
15 | |||
16 | /** |
||
17 | * Which data store to load. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $data_store_name = 'invoice'; |
||
22 | |||
23 | /** |
||
24 | * This is the name of this object type. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $object_type = 'invoice'; |
||
29 | |||
30 | /** |
||
31 | * Item Data array. This is the core item data exposed in APIs. |
||
32 | * |
||
33 | * @since 1.0.19 |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $data = array( |
||
37 | 'parent_id' => 0, |
||
38 | 'status' => 'wpi-pending', |
||
39 | 'version' => '', |
||
40 | 'date_created' => null, |
||
41 | 'date_modified' => null, |
||
42 | 'due_date' => null, |
||
43 | 'completed_date' => null, |
||
44 | 'number' => '', |
||
45 | 'title' => '', |
||
46 | 'path' => '', |
||
47 | 'key' => '', |
||
48 | 'description' => '', |
||
49 | 'author' => 1, |
||
50 | 'type' => 'invoice', |
||
51 | 'post_type' => 'wpi_invoice', |
||
52 | 'mode' => 'live', |
||
53 | 'user_ip' => null, |
||
54 | 'first_name' => null, |
||
55 | 'last_name' => null, |
||
56 | 'phone' => null, |
||
57 | 'email' => null, |
||
58 | 'country' => null, |
||
59 | 'city' => null, |
||
60 | 'state' => null, |
||
61 | 'zip' => null, |
||
62 | 'company' => null, |
||
63 | 'vat_number' => null, |
||
64 | 'vat_rate' => null, |
||
65 | 'address' => null, |
||
66 | 'address_confirmed' => false, |
||
67 | 'subtotal' => 0, |
||
68 | 'total_discount' => 0, |
||
69 | 'total_tax' => 0, |
||
70 | 'total_fees' => 0, |
||
71 | 'fees' => array(), |
||
72 | 'discounts' => array(), |
||
73 | 'taxes' => array(), |
||
74 | 'items' => array(), |
||
75 | 'payment_form' => 1, |
||
76 | 'submission_id' => null, |
||
77 | 'discount_code' => null, |
||
78 | 'gateway' => 'none', |
||
79 | 'transaction_id' => '', |
||
80 | 'currency' => '', |
||
81 | 'disable_taxes' => false, |
||
82 | 'subscription_id' => null, |
||
83 | 'remote_subscription_id' => null, |
||
84 | 'is_viewed' => false, |
||
85 | 'email_cc' => '', |
||
86 | 'template' => 'quantity', // hours, amount only |
||
87 | 'created_via' => null, |
||
88 | ); |
||
89 | |||
90 | /** |
||
91 | * Stores meta in cache for future reads. |
||
92 | * |
||
93 | * A group must be set to to enable caching. |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $cache_group = 'getpaid_invoices'; |
||
98 | |||
99 | /** |
||
100 | * Stores a reference to the original WP_Post object |
||
101 | * |
||
102 | * @var WP_Post |
||
103 | */ |
||
104 | protected $post = null; |
||
105 | |||
106 | /** |
||
107 | * Stores a reference to the recurring item id instead of looping through the items. |
||
108 | * |
||
109 | * @var int |
||
110 | */ |
||
111 | protected $recurring_item = null; |
||
112 | |||
113 | /** |
||
114 | * Stores an array of item totals. |
||
115 | * |
||
116 | * e.g $totals['discount'] = array( |
||
117 | * 'initial' => 10, |
||
118 | * 'recurring' => 10, |
||
119 | * ) |
||
120 | * |
||
121 | * @var array |
||
122 | */ |
||
123 | protected $totals = array(); |
||
124 | |||
125 | /** |
||
126 | * Stores the status transition information. |
||
127 | * |
||
128 | * @since 1.0.19 |
||
129 | * @var bool |
||
130 | */ |
||
131 | protected $status_transition = false; |
||
132 | |||
133 | /** |
||
134 | * Get the invoice if ID is passed, otherwise the invoice is new and empty. |
||
135 | * |
||
136 | * @param int|string|object|WPInv_Invoice|WPInv_Legacy_Invoice|WP_Post $invoice Invoice id, key, transaction id, number or object to read. |
||
137 | */ |
||
138 | public function __construct( $invoice = false ) { |
||
139 | |||
140 | parent::__construct( $invoice ); |
||
|
|||
141 | |||
142 | if ( ! empty( $invoice ) && is_numeric( $invoice ) && getpaid_is_invoice_post_type( get_post_type( $invoice ) ) ) { |
||
143 | $this->set_id( $invoice ); |
||
144 | } elseif ( $invoice instanceof self ) { |
||
145 | $this->set_id( $invoice->get_id() ); |
||
146 | } elseif ( ! empty( $invoice->ID ) ) { |
||
147 | $this->set_id( $invoice->ID ); |
||
148 | } elseif ( is_array( $invoice ) ) { |
||
149 | $this->set_props( $invoice ); |
||
150 | |||
151 | if ( isset( $invoice['ID'] ) ) { |
||
152 | $this->set_id( $invoice['ID'] ); |
||
153 | } |
||
154 | |||
155 | } elseif ( is_scalar( $invoice ) && $invoice_id = self::get_invoice_id_by_field( $invoice, 'key' ) ) { |
||
156 | $this->set_id( $invoice_id ); |
||
157 | } elseif ( is_scalar( $invoice ) && $invoice_id = self::get_invoice_id_by_field( $invoice, 'number' ) ) { |
||
158 | $this->set_id( $invoice_id ); |
||
159 | } elseif ( is_scalar( $invoice ) && $invoice_id = self::get_invoice_id_by_field( $invoice, 'transaction_id' ) ) { |
||
160 | $this->set_id( $invoice_id ); |
||
161 | }else { |
||
162 | $this->set_object_read( true ); |
||
163 | } |
||
164 | |||
165 | // Load the datastore. |
||
166 | $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
||
167 | |||
168 | if ( $this->get_id() > 0 ) { |
||
169 | $this->post = get_post( $this->get_id() ); |
||
170 | $this->ID = $this->get_id(); |
||
171 | $this->data_store->read( $this ); |
||
172 | } |
||
173 | |||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Given an invoice key/number, it returns its id. |
||
178 | * |
||
179 | * |
||
180 | * @static |
||
181 | * @param string $value The invoice key or number |
||
182 | * @param string $field Either key, transaction_id or number. |
||
183 | * @since 1.0.15 |
||
184 | * @return int |
||
185 | */ |
||
186 | public static function get_invoice_id_by_field( $value, $field = 'key' ) { |
||
187 | global $wpdb; |
||
188 | |||
189 | // Trim the value. |
||
190 | $value = trim( $value ); |
||
191 | |||
192 | if ( empty( $value ) ) { |
||
193 | return 0; |
||
194 | } |
||
195 | |||
196 | // Valid fields. |
||
197 | $fields = array( 'key', 'number', 'transaction_id' ); |
||
198 | |||
199 | // Ensure a field has been passed. |
||
200 | if ( empty( $field ) || ! in_array( $field, $fields ) ) { |
||
201 | return 0; |
||
202 | } |
||
203 | |||
204 | // Maybe retrieve from the cache. |
||
205 | $invoice_id = wp_cache_get( $value, "getpaid_invoice_{$field}s_to_invoice_ids" ); |
||
206 | if ( false !== $invoice_id ) { |
||
207 | return $invoice_id; |
||
208 | } |
||
209 | |||
210 | // Fetch from the db. |
||
211 | $table = $wpdb->prefix . 'getpaid_invoices'; |
||
212 | $invoice_id = (int) $wpdb->get_var( |
||
213 | $wpdb->prepare( "SELECT `post_id` FROM $table WHERE `$field`=%s LIMIT 1", $value ) |
||
214 | ); |
||
215 | |||
216 | // Update the cache with our data |
||
217 | wp_cache_set( $value, $invoice_id, "getpaid_invoice_{$field}s_to_invoice_ids" ); |
||
218 | |||
219 | return $invoice_id; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Checks if an invoice key is set. |
||
224 | */ |
||
225 | public function _isset( $key ) { |
||
226 | return isset( $this->data[$key] ) || method_exists( $this, "get_$key" ); |
||
227 | } |
||
228 | |||
229 | /* |
||
230 | |-------------------------------------------------------------------------- |
||
231 | | CRUD methods |
||
232 | |-------------------------------------------------------------------------- |
||
233 | | |
||
234 | | Methods which create, read, update and delete items from the database. |
||
235 | | |
||
236 | */ |
||
237 | |||
238 | /* |
||
239 | |-------------------------------------------------------------------------- |
||
240 | | Getters |
||
241 | |-------------------------------------------------------------------------- |
||
242 | */ |
||
243 | |||
244 | /** |
||
245 | * Get parent invoice ID. |
||
246 | * |
||
247 | * @since 1.0.19 |
||
248 | * @param string $context View or edit context. |
||
249 | * @return int |
||
250 | */ |
||
251 | public function get_parent_id( $context = 'view' ) { |
||
252 | return (int) $this->get_prop( 'parent_id', $context ); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Get parent invoice. |
||
257 | * |
||
258 | * @since 1.0.19 |
||
259 | * @return WPInv_Invoice |
||
260 | */ |
||
261 | public function get_parent_payment() { |
||
262 | return new WPInv_Invoice( $this->get_parent_id() ); |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Alias for self::get_parent_payment(). |
||
267 | * |
||
268 | * @since 1.0.19 |
||
269 | * @return WPInv_Invoice |
||
270 | */ |
||
271 | public function get_parent() { |
||
272 | return $this->get_parent_payment(); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Get invoice status. |
||
277 | * |
||
278 | * @since 1.0.19 |
||
279 | * @param string $context View or edit context. |
||
280 | * @return string |
||
281 | */ |
||
282 | public function get_status( $context = 'view' ) { |
||
283 | return $this->get_prop( 'status', $context ); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Retrieves an array of possible invoice statuses. |
||
288 | * |
||
289 | * @since 1.0.19 |
||
290 | * @return array |
||
291 | */ |
||
292 | public function get_all_statuses() { |
||
293 | return wpinv_get_invoice_statuses( true, true, $this ); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Get invoice status nice name. |
||
298 | * |
||
299 | * @since 1.0.19 |
||
300 | * @return string |
||
301 | */ |
||
302 | public function get_status_nicename() { |
||
303 | $statuses = $this->get_all_statuses(); |
||
304 | |||
305 | $status = isset( $statuses[ $this->get_status() ] ) ? $statuses[ $this->get_status() ] : $this->get_status(); |
||
306 | |||
307 | return apply_filters( 'wpinv_get_invoice_status_nicename', $status, $this ); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Retrieves the invoice status label html |
||
312 | * |
||
313 | * @since 1.0.0 |
||
314 | * @return string |
||
315 | */ |
||
316 | public function get_status_label_html() { |
||
317 | |||
318 | $status_label = sanitize_text_field( $this->get_status_nicename() ); |
||
319 | $status = sanitize_html_class( $this->get_status() ); |
||
320 | |||
321 | return "<span class='bsui'><span class='d-inline-block py-2 px-3 rounded getpaid-invoice-status-$status'>$status_label</span></span>"; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Get plugin version when the invoice was created. |
||
326 | * |
||
327 | * @since 1.0.19 |
||
328 | * @param string $context View or edit context. |
||
329 | * @return string |
||
330 | */ |
||
331 | public function get_version( $context = 'view' ) { |
||
332 | return $this->get_prop( 'version', $context ); |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @deprecated |
||
337 | */ |
||
338 | public function get_invoice_date( $format = true ) { |
||
339 | $date = getpaid_format_date( $this->get_date_completed() ); |
||
340 | $date = empty( $date ) ? $this->get_date_created() : $this->get_date_completed(); |
||
341 | $formatted = getpaid_format_date( $date ); |
||
342 | |||
343 | if ( $format ) { |
||
344 | return $formatted; |
||
345 | } |
||
346 | |||
347 | return empty( $formatted ) ? '' : $date; |
||
348 | |||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Get date when the invoice was created. |
||
353 | * |
||
354 | * @since 1.0.19 |
||
355 | * @param string $context View or edit context. |
||
356 | * @return string |
||
357 | */ |
||
358 | public function get_date_created( $context = 'view' ) { |
||
359 | return $this->get_prop( 'date_created', $context ); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Alias for self::get_date_created(). |
||
364 | * |
||
365 | * @since 1.0.19 |
||
366 | * @param string $context View or edit context. |
||
367 | * @return string |
||
368 | */ |
||
369 | public function get_created_date( $context = 'view' ) { |
||
370 | return $this->get_date_created( $context ); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Get GMT date when the invoice was created. |
||
375 | * |
||
376 | * @since 1.0.19 |
||
377 | * @param string $context View or edit context. |
||
378 | * @return string |
||
379 | */ |
||
380 | public function get_date_created_gmt( $context = 'view' ) { |
||
381 | $date = $this->get_date_created( $context ); |
||
382 | |||
383 | if ( $date ) { |
||
384 | $date = get_gmt_from_date( $date ); |
||
385 | } |
||
386 | return $date; |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * Get date when the invoice was last modified. |
||
391 | * |
||
392 | * @since 1.0.19 |
||
393 | * @param string $context View or edit context. |
||
394 | * @return string |
||
395 | */ |
||
396 | public function get_date_modified( $context = 'view' ) { |
||
397 | return $this->get_prop( 'date_modified', $context ); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Alias for self::get_date_modified(). |
||
402 | * |
||
403 | * @since 1.0.19 |
||
404 | * @param string $context View or edit context. |
||
405 | * @return string |
||
406 | */ |
||
407 | public function get_modified_date( $context = 'view' ) { |
||
408 | return $this->get_date_modified( $context ); |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Get GMT date when the invoice was last modified. |
||
413 | * |
||
414 | * @since 1.0.19 |
||
415 | * @param string $context View or edit context. |
||
416 | * @return string |
||
417 | */ |
||
418 | public function get_date_modified_gmt( $context = 'view' ) { |
||
419 | $date = $this->get_date_modified( $context ); |
||
420 | |||
421 | if ( $date ) { |
||
422 | $date = get_gmt_from_date( $date ); |
||
423 | } |
||
424 | return $date; |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * Get the invoice due date. |
||
429 | * |
||
430 | * @since 1.0.19 |
||
431 | * @param string $context View or edit context. |
||
432 | * @return string |
||
433 | */ |
||
434 | public function get_due_date( $context = 'view' ) { |
||
435 | return $this->get_prop( 'due_date', $context ); |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Alias for self::get_due_date(). |
||
440 | * |
||
441 | * @since 1.0.19 |
||
442 | * @param string $context View or edit context. |
||
443 | * @return string |
||
444 | */ |
||
445 | public function get_date_due( $context = 'view' ) { |
||
446 | return $this->get_due_date( $context ); |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Get the invoice GMT due date. |
||
451 | * |
||
452 | * @since 1.0.19 |
||
453 | * @param string $context View or edit context. |
||
454 | * @return string |
||
455 | */ |
||
456 | public function get_due_date_gmt( $context = 'view' ) { |
||
457 | $date = $this->get_due_date( $context ); |
||
458 | |||
459 | if ( $date ) { |
||
460 | $date = get_gmt_from_date( $date ); |
||
461 | } |
||
462 | return $date; |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Alias for self::get_due_date_gmt(). |
||
467 | * |
||
468 | * @since 1.0.19 |
||
469 | * @param string $context View or edit context. |
||
470 | * @return string |
||
471 | */ |
||
472 | public function get_gmt_date_due( $context = 'view' ) { |
||
473 | return $this->get_due_date_gmt( $context ); |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Get date when the invoice was completed. |
||
478 | * |
||
479 | * @since 1.0.19 |
||
480 | * @param string $context View or edit context. |
||
481 | * @return string |
||
482 | */ |
||
483 | public function get_completed_date( $context = 'view' ) { |
||
484 | return $this->get_prop( 'completed_date', $context ); |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Alias for self::get_completed_date(). |
||
489 | * |
||
490 | * @since 1.0.19 |
||
491 | * @param string $context View or edit context. |
||
492 | * @return string |
||
493 | */ |
||
494 | public function get_date_completed( $context = 'view' ) { |
||
495 | return $this->get_completed_date( $context ); |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * Get GMT date when the invoice was was completed. |
||
500 | * |
||
501 | * @since 1.0.19 |
||
502 | * @param string $context View or edit context. |
||
503 | * @return string |
||
504 | */ |
||
505 | public function get_completed_date_gmt( $context = 'view' ) { |
||
506 | $date = $this->get_completed_date( $context ); |
||
507 | |||
508 | if ( $date ) { |
||
509 | $date = get_gmt_from_date( $date ); |
||
510 | } |
||
511 | return $date; |
||
512 | } |
||
513 | |||
514 | /** |
||
515 | * Alias for self::get_completed_date_gmt(). |
||
516 | * |
||
517 | * @since 1.0.19 |
||
518 | * @param string $context View or edit context. |
||
519 | * @return string |
||
520 | */ |
||
521 | public function get_gmt_completed_date( $context = 'view' ) { |
||
522 | return $this->get_completed_date_gmt( $context ); |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * Get the invoice number. |
||
527 | * |
||
528 | * @since 1.0.19 |
||
529 | * @param string $context View or edit context. |
||
530 | * @return string |
||
531 | */ |
||
532 | public function get_number( $context = 'view' ) { |
||
533 | $number = $this->get_prop( 'number', $context ); |
||
534 | |||
535 | if ( empty( $number ) ) { |
||
536 | $number = $this->generate_number(); |
||
537 | $this->set_number( $this->generate_number() ); |
||
538 | } |
||
539 | |||
540 | return $number; |
||
541 | } |
||
542 | |||
543 | /** |
||
544 | * Set the invoice number. |
||
545 | * |
||
546 | * @since 1.0.19 |
||
547 | */ |
||
548 | public function maybe_set_number() { |
||
549 | $number = $this->get_number(); |
||
550 | |||
551 | if ( empty( $number ) || $this->get_id() == $number ) { |
||
552 | $this->set_number( $this->generate_number() ); |
||
553 | } |
||
554 | |||
555 | } |
||
556 | |||
557 | /** |
||
558 | * Get the invoice key. |
||
559 | * |
||
560 | * @since 1.0.19 |
||
561 | * @param string $context View or edit context. |
||
562 | * @return string |
||
563 | */ |
||
564 | public function get_key( $context = 'view' ) { |
||
565 | return $this->get_prop( 'key', $context ); |
||
566 | } |
||
567 | |||
568 | /** |
||
569 | * Set the invoice key. |
||
570 | * |
||
571 | * @since 1.0.19 |
||
572 | */ |
||
573 | public function maybe_set_key() { |
||
574 | $key = $this->get_key(); |
||
575 | |||
576 | if ( empty( $key ) ) { |
||
577 | $key = $this->generate_key( $this->get_type() . '_' ); |
||
578 | $this->set_key( $key ); |
||
579 | } |
||
580 | |||
581 | } |
||
582 | |||
583 | /** |
||
584 | * Get the invoice type. |
||
585 | * |
||
586 | * @since 1.0.19 |
||
587 | * @param string $context View or edit context. |
||
588 | * @return string |
||
589 | */ |
||
590 | public function get_type( $context = 'view' ) { |
||
591 | return $this->get_prop( 'type', $context ); |
||
592 | } |
||
593 | |||
594 | /** |
||
595 | * @deprecated |
||
596 | */ |
||
597 | public function get_invoice_quote_type() { |
||
598 | ucfirst( $this->get_type() ); |
||
599 | } |
||
600 | |||
601 | /** |
||
602 | * Get the invoice post type. |
||
603 | * |
||
604 | * @since 1.0.19 |
||
605 | * @param string $context View or edit context. |
||
606 | * @return string |
||
607 | */ |
||
608 | public function get_post_type( $context = 'view' ) { |
||
609 | return $this->get_prop( 'post_type', $context ); |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * Get the invoice mode. |
||
614 | * |
||
615 | * @since 1.0.19 |
||
616 | * @param string $context View or edit context. |
||
617 | * @return string |
||
618 | */ |
||
619 | public function get_mode( $context = 'view' ) { |
||
620 | return $this->get_prop( 'mode', $context ); |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * Get the invoice path. |
||
625 | * |
||
626 | * @since 1.0.19 |
||
627 | * @param string $context View or edit context. |
||
628 | * @return string |
||
629 | */ |
||
630 | public function get_path( $context = 'view' ) { |
||
631 | $path = $this->get_prop( 'path', $context ); |
||
632 | $prefix = $this->get_type(); |
||
633 | |||
634 | if ( 0 !== strpos( $path, $prefix ) ) { |
||
635 | $path = sanitize_title( $prefix . '-' . $this->get_id() ); |
||
636 | $this->set_path( $path ); |
||
637 | } |
||
638 | |||
639 | return $path; |
||
640 | } |
||
641 | |||
642 | /** |
||
643 | * Get the invoice name/title. |
||
644 | * |
||
645 | * @since 1.0.19 |
||
646 | * @param string $context View or edit context. |
||
647 | * @return string |
||
648 | */ |
||
649 | public function get_name( $context = 'view' ) { |
||
650 | return $this->get_prop( 'title', $context ); |
||
651 | } |
||
652 | |||
653 | /** |
||
654 | * Alias of self::get_name(). |
||
655 | * |
||
656 | * @since 1.0.19 |
||
657 | * @param string $context View or edit context. |
||
658 | * @return string |
||
659 | */ |
||
660 | public function get_title( $context = 'view' ) { |
||
661 | return $this->get_name( $context ); |
||
662 | } |
||
663 | |||
664 | /** |
||
665 | * Get the invoice description. |
||
666 | * |
||
667 | * @since 1.0.19 |
||
668 | * @param string $context View or edit context. |
||
669 | * @return string |
||
670 | */ |
||
671 | public function get_description( $context = 'view' ) { |
||
672 | return $this->get_prop( 'description', $context ); |
||
673 | } |
||
674 | |||
675 | /** |
||
676 | * Alias of self::get_description(). |
||
677 | * |
||
678 | * @since 1.0.19 |
||
679 | * @param string $context View or edit context. |
||
680 | * @return string |
||
681 | */ |
||
682 | public function get_excerpt( $context = 'view' ) { |
||
683 | return $this->get_description( $context ); |
||
684 | } |
||
685 | |||
686 | /** |
||
687 | * Alias of self::get_description(). |
||
688 | * |
||
689 | * @since 1.0.19 |
||
690 | * @param string $context View or edit context. |
||
691 | * @return string |
||
692 | */ |
||
693 | public function get_summary( $context = 'view' ) { |
||
694 | return $this->get_description( $context ); |
||
695 | } |
||
696 | |||
697 | /** |
||
698 | * Returns the user info. |
||
699 | * |
||
700 | * @since 1.0.19 |
||
701 | * @param string $context View or edit context. |
||
702 | * @return array |
||
703 | */ |
||
704 | public function get_user_info( $context = 'view' ) { |
||
705 | |||
706 | $user_info = array( |
||
707 | 'user_id' => $this->get_user_id( $context ), |
||
708 | 'email' => $this->get_email( $context ), |
||
709 | 'first_name' => $this->get_first_name( $context ), |
||
710 | 'last_name' => $this->get_last_name( $context ), |
||
711 | 'address' => $this->get_address( $context ), |
||
712 | 'phone' => $this->get_phone( $context ), |
||
713 | 'city' => $this->get_city( $context ), |
||
714 | 'country' => $this->get_country( $context ), |
||
715 | 'state' => $this->get_state( $context ), |
||
716 | 'zip' => $this->get_zip( $context ), |
||
717 | 'company' => $this->get_company( $context ), |
||
718 | 'vat_number' => $this->get_vat_number( $context ), |
||
719 | 'discount' => $this->get_discount_code( $context ), |
||
720 | ); |
||
721 | |||
722 | return apply_filters( 'wpinv_user_info', $user_info, $this->get_id(), $this ); |
||
723 | |||
724 | } |
||
725 | |||
726 | /** |
||
727 | * Get the customer id. |
||
728 | * |
||
729 | * @since 1.0.19 |
||
730 | * @param string $context View or edit context. |
||
731 | * @return int |
||
732 | */ |
||
733 | public function get_author( $context = 'view' ) { |
||
734 | return (int) $this->get_prop( 'author', $context ); |
||
735 | } |
||
736 | |||
737 | /** |
||
738 | * Alias of self::get_author(). |
||
739 | * |
||
740 | * @since 1.0.19 |
||
741 | * @param string $context View or edit context. |
||
742 | * @return int |
||
743 | */ |
||
744 | public function get_user_id( $context = 'view' ) { |
||
745 | return $this->get_author( $context ); |
||
746 | } |
||
747 | |||
748 | /** |
||
749 | * Alias of self::get_author(). |
||
750 | * |
||
751 | * @since 1.0.19 |
||
752 | * @param string $context View or edit context. |
||
753 | * @return int |
||
754 | */ |
||
755 | public function get_customer_id( $context = 'view' ) { |
||
756 | return $this->get_author( $context ); |
||
757 | } |
||
758 | |||
759 | /** |
||
760 | * Get the customer's ip. |
||
761 | * |
||
762 | * @since 1.0.19 |
||
763 | * @param string $context View or edit context. |
||
764 | * @return string |
||
765 | */ |
||
766 | public function get_ip( $context = 'view' ) { |
||
767 | return $this->get_prop( 'user_ip', $context ); |
||
768 | } |
||
769 | |||
770 | /** |
||
771 | * Alias of self::get_ip(). |
||
772 | * |
||
773 | * @since 1.0.19 |
||
774 | * @param string $context View or edit context. |
||
775 | * @return string |
||
776 | */ |
||
777 | public function get_user_ip( $context = 'view' ) { |
||
778 | return $this->get_ip( $context ); |
||
779 | } |
||
780 | |||
781 | /** |
||
782 | * Alias of self::get_ip(). |
||
783 | * |
||
784 | * @since 1.0.19 |
||
785 | * @param string $context View or edit context. |
||
786 | * @return string |
||
787 | */ |
||
788 | public function get_customer_ip( $context = 'view' ) { |
||
789 | return $this->get_ip( $context ); |
||
790 | } |
||
791 | |||
792 | /** |
||
793 | * Get the customer's first name. |
||
794 | * |
||
795 | * @since 1.0.19 |
||
796 | * @param string $context View or edit context. |
||
797 | * @return string |
||
798 | */ |
||
799 | public function get_first_name( $context = 'view' ) { |
||
800 | return $this->get_prop( 'first_name', $context ); |
||
801 | } |
||
802 | |||
803 | /** |
||
804 | * Alias of self::get_first_name(). |
||
805 | * |
||
806 | * @since 1.0.19 |
||
807 | * @param string $context View or edit context. |
||
808 | * @return int |
||
809 | */ |
||
810 | public function get_user_first_name( $context = 'view' ) { |
||
811 | return $this->get_first_name( $context ); |
||
812 | } |
||
813 | |||
814 | /** |
||
815 | * Alias of self::get_first_name(). |
||
816 | * |
||
817 | * @since 1.0.19 |
||
818 | * @param string $context View or edit context. |
||
819 | * @return int |
||
820 | */ |
||
821 | public function get_customer_first_name( $context = 'view' ) { |
||
822 | return $this->get_first_name( $context ); |
||
823 | } |
||
824 | |||
825 | /** |
||
826 | * Get the customer's last name. |
||
827 | * |
||
828 | * @since 1.0.19 |
||
829 | * @param string $context View or edit context. |
||
830 | * @return string |
||
831 | */ |
||
832 | public function get_last_name( $context = 'view' ) { |
||
833 | return $this->get_prop( 'last_name', $context ); |
||
834 | } |
||
835 | |||
836 | /** |
||
837 | * Alias of self::get_last_name(). |
||
838 | * |
||
839 | * @since 1.0.19 |
||
840 | * @param string $context View or edit context. |
||
841 | * @return int |
||
842 | */ |
||
843 | public function get_user_last_name( $context = 'view' ) { |
||
844 | return $this->get_last_name( $context ); |
||
845 | } |
||
846 | |||
847 | /** |
||
848 | * Alias of self::get_last_name(). |
||
849 | * |
||
850 | * @since 1.0.19 |
||
851 | * @param string $context View or edit context. |
||
852 | * @return int |
||
853 | */ |
||
854 | public function get_customer_last_name( $context = 'view' ) { |
||
855 | return $this->get_last_name( $context ); |
||
856 | } |
||
857 | |||
858 | /** |
||
859 | * Get the customer's full name. |
||
860 | * |
||
861 | * @since 1.0.19 |
||
862 | * @param string $context View or edit context. |
||
863 | * @return string |
||
864 | */ |
||
865 | public function get_full_name( $context = 'view' ) { |
||
866 | return trim( $this->get_first_name( $context ) . ' ' . $this->get_last_name( $context ) ); |
||
867 | } |
||
868 | |||
869 | /** |
||
870 | * Alias of self::get_full_name(). |
||
871 | * |
||
872 | * @since 1.0.19 |
||
873 | * @param string $context View or edit context. |
||
874 | * @return int |
||
875 | */ |
||
876 | public function get_user_full_name( $context = 'view' ) { |
||
877 | return $this->get_full_name( $context ); |
||
878 | } |
||
879 | |||
880 | /** |
||
881 | * Alias of self::get_full_name(). |
||
882 | * |
||
883 | * @since 1.0.19 |
||
884 | * @param string $context View or edit context. |
||
885 | * @return int |
||
886 | */ |
||
887 | public function get_customer_full_name( $context = 'view' ) { |
||
888 | return $this->get_full_name( $context ); |
||
889 | } |
||
890 | |||
891 | /** |
||
892 | * Get the customer's phone number. |
||
893 | * |
||
894 | * @since 1.0.19 |
||
895 | * @param string $context View or edit context. |
||
896 | * @return string |
||
897 | */ |
||
898 | public function get_phone( $context = 'view' ) { |
||
899 | return $this->get_prop( 'phone', $context ); |
||
900 | } |
||
901 | |||
902 | /** |
||
903 | * Alias of self::get_phone(). |
||
904 | * |
||
905 | * @since 1.0.19 |
||
906 | * @param string $context View or edit context. |
||
907 | * @return int |
||
908 | */ |
||
909 | public function get_phone_number( $context = 'view' ) { |
||
910 | return $this->get_phone( $context ); |
||
911 | } |
||
912 | |||
913 | /** |
||
914 | * Alias of self::get_phone(). |
||
915 | * |
||
916 | * @since 1.0.19 |
||
917 | * @param string $context View or edit context. |
||
918 | * @return int |
||
919 | */ |
||
920 | public function get_user_phone( $context = 'view' ) { |
||
921 | return $this->get_phone( $context ); |
||
922 | } |
||
923 | |||
924 | /** |
||
925 | * Alias of self::get_phone(). |
||
926 | * |
||
927 | * @since 1.0.19 |
||
928 | * @param string $context View or edit context. |
||
929 | * @return int |
||
930 | */ |
||
931 | public function get_customer_phone( $context = 'view' ) { |
||
932 | return $this->get_phone( $context ); |
||
933 | } |
||
934 | |||
935 | /** |
||
936 | * Get the customer's email address. |
||
937 | * |
||
938 | * @since 1.0.19 |
||
939 | * @param string $context View or edit context. |
||
940 | * @return string |
||
941 | */ |
||
942 | public function get_email( $context = 'view' ) { |
||
943 | return $this->get_prop( 'email', $context ); |
||
944 | } |
||
945 | |||
946 | /** |
||
947 | * Alias of self::get_email(). |
||
948 | * |
||
949 | * @since 1.0.19 |
||
950 | * @param string $context View or edit context. |
||
951 | * @return string |
||
952 | */ |
||
953 | public function get_email_address( $context = 'view' ) { |
||
954 | return $this->get_email( $context ); |
||
955 | } |
||
956 | |||
957 | /** |
||
958 | * Alias of self::get_email(). |
||
959 | * |
||
960 | * @since 1.0.19 |
||
961 | * @param string $context View or edit context. |
||
962 | * @return int |
||
963 | */ |
||
964 | public function get_user_email( $context = 'view' ) { |
||
965 | return $this->get_email( $context ); |
||
966 | } |
||
967 | |||
968 | /** |
||
969 | * Alias of self::get_email(). |
||
970 | * |
||
971 | * @since 1.0.19 |
||
972 | * @param string $context View or edit context. |
||
973 | * @return int |
||
974 | */ |
||
975 | public function get_customer_email( $context = 'view' ) { |
||
976 | return $this->get_email( $context ); |
||
977 | } |
||
978 | |||
979 | /** |
||
980 | * Get the customer's country. |
||
981 | * |
||
982 | * @since 1.0.19 |
||
983 | * @param string $context View or edit context. |
||
984 | * @return string |
||
985 | */ |
||
986 | public function get_country( $context = 'view' ) { |
||
987 | $country = $this->get_prop( 'country', $context ); |
||
988 | return empty( $country ) ? wpinv_get_default_country() : $country; |
||
989 | } |
||
990 | |||
991 | /** |
||
992 | * Alias of self::get_country(). |
||
993 | * |
||
994 | * @since 1.0.19 |
||
995 | * @param string $context View or edit context. |
||
996 | * @return int |
||
997 | */ |
||
998 | public function get_user_country( $context = 'view' ) { |
||
999 | return $this->get_country( $context ); |
||
1000 | } |
||
1001 | |||
1002 | /** |
||
1003 | * Alias of self::get_country(). |
||
1004 | * |
||
1005 | * @since 1.0.19 |
||
1006 | * @param string $context View or edit context. |
||
1007 | * @return int |
||
1008 | */ |
||
1009 | public function get_customer_country( $context = 'view' ) { |
||
1010 | return $this->get_country( $context ); |
||
1011 | } |
||
1012 | |||
1013 | /** |
||
1014 | * Get the customer's state. |
||
1015 | * |
||
1016 | * @since 1.0.19 |
||
1017 | * @param string $context View or edit context. |
||
1018 | * @return string |
||
1019 | */ |
||
1020 | public function get_state( $context = 'view' ) { |
||
1021 | $state = $this->get_prop( 'state', $context ); |
||
1022 | return empty( $state ) ? wpinv_get_default_state() : $state; |
||
1023 | } |
||
1024 | |||
1025 | /** |
||
1026 | * Alias of self::get_state(). |
||
1027 | * |
||
1028 | * @since 1.0.19 |
||
1029 | * @param string $context View or edit context. |
||
1030 | * @return int |
||
1031 | */ |
||
1032 | public function get_user_state( $context = 'view' ) { |
||
1033 | return $this->get_state( $context ); |
||
1034 | } |
||
1035 | |||
1036 | /** |
||
1037 | * Alias of self::get_state(). |
||
1038 | * |
||
1039 | * @since 1.0.19 |
||
1040 | * @param string $context View or edit context. |
||
1041 | * @return int |
||
1042 | */ |
||
1043 | public function get_customer_state( $context = 'view' ) { |
||
1044 | return $this->get_state( $context ); |
||
1045 | } |
||
1046 | |||
1047 | /** |
||
1048 | * Get the customer's city. |
||
1049 | * |
||
1050 | * @since 1.0.19 |
||
1051 | * @param string $context View or edit context. |
||
1052 | * @return string |
||
1053 | */ |
||
1054 | public function get_city( $context = 'view' ) { |
||
1055 | return $this->get_prop( 'city', $context ); |
||
1056 | } |
||
1057 | |||
1058 | /** |
||
1059 | * Alias of self::get_city(). |
||
1060 | * |
||
1061 | * @since 1.0.19 |
||
1062 | * @param string $context View or edit context. |
||
1063 | * @return string |
||
1064 | */ |
||
1065 | public function get_user_city( $context = 'view' ) { |
||
1066 | return $this->get_city( $context ); |
||
1067 | } |
||
1068 | |||
1069 | /** |
||
1070 | * Alias of self::get_city(). |
||
1071 | * |
||
1072 | * @since 1.0.19 |
||
1073 | * @param string $context View or edit context. |
||
1074 | * @return string |
||
1075 | */ |
||
1076 | public function get_customer_city( $context = 'view' ) { |
||
1077 | return $this->get_city( $context ); |
||
1078 | } |
||
1079 | |||
1080 | /** |
||
1081 | * Get the customer's zip. |
||
1082 | * |
||
1083 | * @since 1.0.19 |
||
1084 | * @param string $context View or edit context. |
||
1085 | * @return string |
||
1086 | */ |
||
1087 | public function get_zip( $context = 'view' ) { |
||
1088 | return $this->get_prop( 'zip', $context ); |
||
1089 | } |
||
1090 | |||
1091 | /** |
||
1092 | * Alias of self::get_zip(). |
||
1093 | * |
||
1094 | * @since 1.0.19 |
||
1095 | * @param string $context View or edit context. |
||
1096 | * @return string |
||
1097 | */ |
||
1098 | public function get_user_zip( $context = 'view' ) { |
||
1099 | return $this->get_zip( $context ); |
||
1100 | } |
||
1101 | |||
1102 | /** |
||
1103 | * Alias of self::get_zip(). |
||
1104 | * |
||
1105 | * @since 1.0.19 |
||
1106 | * @param string $context View or edit context. |
||
1107 | * @return string |
||
1108 | */ |
||
1109 | public function get_customer_zip( $context = 'view' ) { |
||
1110 | return $this->get_zip( $context ); |
||
1111 | } |
||
1112 | |||
1113 | /** |
||
1114 | * Get the customer's company. |
||
1115 | * |
||
1116 | * @since 1.0.19 |
||
1117 | * @param string $context View or edit context. |
||
1118 | * @return string |
||
1119 | */ |
||
1120 | public function get_company( $context = 'view' ) { |
||
1121 | return $this->get_prop( 'company', $context ); |
||
1122 | } |
||
1123 | |||
1124 | /** |
||
1125 | * Alias of self::get_company(). |
||
1126 | * |
||
1127 | * @since 1.0.19 |
||
1128 | * @param string $context View or edit context. |
||
1129 | * @return string |
||
1130 | */ |
||
1131 | public function get_user_company( $context = 'view' ) { |
||
1132 | return $this->get_company( $context ); |
||
1133 | } |
||
1134 | |||
1135 | /** |
||
1136 | * Alias of self::get_company(). |
||
1137 | * |
||
1138 | * @since 1.0.19 |
||
1139 | * @param string $context View or edit context. |
||
1140 | * @return string |
||
1141 | */ |
||
1142 | public function get_customer_company( $context = 'view' ) { |
||
1143 | return $this->get_company( $context ); |
||
1144 | } |
||
1145 | |||
1146 | /** |
||
1147 | * Get the customer's vat number. |
||
1148 | * |
||
1149 | * @since 1.0.19 |
||
1150 | * @param string $context View or edit context. |
||
1151 | * @return string |
||
1152 | */ |
||
1153 | public function get_vat_number( $context = 'view' ) { |
||
1154 | return $this->get_prop( 'vat_number', $context ); |
||
1155 | } |
||
1156 | |||
1157 | /** |
||
1158 | * Alias of self::get_vat_number(). |
||
1159 | * |
||
1160 | * @since 1.0.19 |
||
1161 | * @param string $context View or edit context. |
||
1162 | * @return string |
||
1163 | */ |
||
1164 | public function get_user_vat_number( $context = 'view' ) { |
||
1165 | return $this->get_vat_number( $context ); |
||
1166 | } |
||
1167 | |||
1168 | /** |
||
1169 | * Alias of self::get_vat_number(). |
||
1170 | * |
||
1171 | * @since 1.0.19 |
||
1172 | * @param string $context View or edit context. |
||
1173 | * @return string |
||
1174 | */ |
||
1175 | public function get_customer_vat_number( $context = 'view' ) { |
||
1176 | return $this->get_vat_number( $context ); |
||
1177 | } |
||
1178 | |||
1179 | /** |
||
1180 | * Get the customer's vat rate. |
||
1181 | * |
||
1182 | * @since 1.0.19 |
||
1183 | * @param string $context View or edit context. |
||
1184 | * @return string |
||
1185 | */ |
||
1186 | public function get_vat_rate( $context = 'view' ) { |
||
1187 | return $this->get_prop( 'vat_rate', $context ); |
||
1188 | } |
||
1189 | |||
1190 | /** |
||
1191 | * Alias of self::get_vat_rate(). |
||
1192 | * |
||
1193 | * @since 1.0.19 |
||
1194 | * @param string $context View or edit context. |
||
1195 | * @return string |
||
1196 | */ |
||
1197 | public function get_user_vat_rate( $context = 'view' ) { |
||
1198 | return $this->get_vat_rate( $context ); |
||
1199 | } |
||
1200 | |||
1201 | /** |
||
1202 | * Alias of self::get_vat_rate(). |
||
1203 | * |
||
1204 | * @since 1.0.19 |
||
1205 | * @param string $context View or edit context. |
||
1206 | * @return string |
||
1207 | */ |
||
1208 | public function get_customer_vat_rate( $context = 'view' ) { |
||
1209 | return $this->get_vat_rate( $context ); |
||
1210 | } |
||
1211 | |||
1212 | /** |
||
1213 | * Get the customer's address. |
||
1214 | * |
||
1215 | * @since 1.0.19 |
||
1216 | * @param string $context View or edit context. |
||
1217 | * @return string |
||
1218 | */ |
||
1219 | public function get_address( $context = 'view' ) { |
||
1220 | return $this->get_prop( 'address', $context ); |
||
1221 | } |
||
1222 | |||
1223 | /** |
||
1224 | * Alias of self::get_address(). |
||
1225 | * |
||
1226 | * @since 1.0.19 |
||
1227 | * @param string $context View or edit context. |
||
1228 | * @return string |
||
1229 | */ |
||
1230 | public function get_user_address( $context = 'view' ) { |
||
1231 | return $this->get_address( $context ); |
||
1232 | } |
||
1233 | |||
1234 | /** |
||
1235 | * Alias of self::get_address(). |
||
1236 | * |
||
1237 | * @since 1.0.19 |
||
1238 | * @param string $context View or edit context. |
||
1239 | * @return string |
||
1240 | */ |
||
1241 | public function get_customer_address( $context = 'view' ) { |
||
1242 | return $this->get_address( $context ); |
||
1243 | } |
||
1244 | |||
1245 | /** |
||
1246 | * Get whether the customer has viewed the invoice or not. |
||
1247 | * |
||
1248 | * @since 1.0.19 |
||
1249 | * @param string $context View or edit context. |
||
1250 | * @return bool |
||
1251 | */ |
||
1252 | public function get_is_viewed( $context = 'view' ) { |
||
1253 | return (bool) $this->get_prop( 'is_viewed', $context ); |
||
1254 | } |
||
1255 | |||
1256 | /** |
||
1257 | * Get other recipients for invoice communications. |
||
1258 | * |
||
1259 | * @since 1.0.19 |
||
1260 | * @param string $context View or edit context. |
||
1261 | * @return bool |
||
1262 | */ |
||
1263 | public function get_email_cc( $context = 'view' ) { |
||
1264 | return $this->get_prop( 'email_cc', $context ); |
||
1265 | } |
||
1266 | |||
1267 | /** |
||
1268 | * Get invoice template. |
||
1269 | * |
||
1270 | * @since 1.0.19 |
||
1271 | * @param string $context View or edit context. |
||
1272 | * @return bool |
||
1273 | */ |
||
1274 | public function get_template( $context = 'view' ) { |
||
1275 | return $this->get_prop( 'template', $context ); |
||
1276 | } |
||
1277 | |||
1278 | /** |
||
1279 | * Get invoice source. |
||
1280 | * |
||
1281 | * @since 1.0.19 |
||
1282 | * @param string $context View or edit context. |
||
1283 | * @return bool |
||
1284 | */ |
||
1285 | public function get_created_via( $context = 'view' ) { |
||
1286 | return $this->get_prop( 'created_via', $context ); |
||
1287 | } |
||
1288 | |||
1289 | /** |
||
1290 | * Get whether the customer has confirmed their address. |
||
1291 | * |
||
1292 | * @since 1.0.19 |
||
1293 | * @param string $context View or edit context. |
||
1294 | * @return bool |
||
1295 | */ |
||
1296 | public function get_address_confirmed( $context = 'view' ) { |
||
1297 | return (bool) $this->get_prop( 'address_confirmed', $context ); |
||
1298 | } |
||
1299 | |||
1300 | /** |
||
1301 | * Alias of self::get_address_confirmed(). |
||
1302 | * |
||
1303 | * @since 1.0.19 |
||
1304 | * @param string $context View or edit context. |
||
1305 | * @return bool |
||
1306 | */ |
||
1307 | public function get_user_address_confirmed( $context = 'view' ) { |
||
1308 | return $this->get_address_confirmed( $context ); |
||
1309 | } |
||
1310 | |||
1311 | /** |
||
1312 | * Alias of self::get_address(). |
||
1313 | * |
||
1314 | * @since 1.0.19 |
||
1315 | * @param string $context View or edit context. |
||
1316 | * @return bool |
||
1317 | */ |
||
1318 | public function get_customer_address_confirmed( $context = 'view' ) { |
||
1319 | return $this->get_address_confirmed( $context ); |
||
1320 | } |
||
1321 | |||
1322 | /** |
||
1323 | * Get the invoice subtotal. |
||
1324 | * |
||
1325 | * @since 1.0.19 |
||
1326 | * @param string $context View or edit context. |
||
1327 | * @return float |
||
1328 | */ |
||
1329 | public function get_subtotal( $context = 'view' ) { |
||
1330 | $subtotal = (float) $this->get_prop( 'subtotal', $context ); |
||
1331 | |||
1332 | // Backwards compatibility. |
||
1333 | if ( is_bool( $context ) && $context ) { |
||
1334 | return wpinv_price( wpinv_format_amount( $subtotal ), $this->get_currency() ); |
||
1335 | } |
||
1336 | |||
1337 | return $subtotal; |
||
1338 | } |
||
1339 | |||
1340 | /** |
||
1341 | * Get the invoice discount total. |
||
1342 | * |
||
1343 | * @since 1.0.19 |
||
1344 | * @param string $context View or edit context. |
||
1345 | * @return float |
||
1346 | */ |
||
1347 | public function get_total_discount( $context = 'view' ) { |
||
1348 | return (float) $this->get_prop( 'total_discount', $context ); |
||
1349 | } |
||
1350 | |||
1351 | /** |
||
1352 | * Get the invoice tax total. |
||
1353 | * |
||
1354 | * @since 1.0.19 |
||
1355 | * @param string $context View or edit context. |
||
1356 | * @return float |
||
1357 | */ |
||
1358 | public function get_total_tax( $context = 'view' ) { |
||
1359 | return (float) $this->get_prop( 'total_tax', $context ); |
||
1360 | } |
||
1361 | |||
1362 | /** |
||
1363 | * @deprecated |
||
1364 | */ |
||
1365 | public function get_final_tax( $currency = false ) { |
||
1366 | $tax = $this->get_total_tax(); |
||
1367 | |||
1368 | if ( $currency ) { |
||
1369 | return wpinv_price( wpinv_format_amount( $tax, NULL, false ), $this->get_currency() ); |
||
1370 | } |
||
1371 | |||
1372 | return $tax; |
||
1373 | } |
||
1374 | |||
1375 | /** |
||
1376 | * Get the invoice fees total. |
||
1377 | * |
||
1378 | * @since 1.0.19 |
||
1379 | * @param string $context View or edit context. |
||
1380 | * @return float |
||
1381 | */ |
||
1382 | public function get_total_fees( $context = 'view' ) { |
||
1383 | return (float) $this->get_prop( 'total_fees', $context ); |
||
1384 | } |
||
1385 | |||
1386 | /** |
||
1387 | * Alias for self::get_total_fees(). |
||
1388 | * |
||
1389 | * @since 1.0.19 |
||
1390 | * @param string $context View or edit context. |
||
1391 | * @return float |
||
1392 | */ |
||
1393 | public function get_fees_total( $context = 'view' ) { |
||
1394 | return $this->get_total_fees( $context ); |
||
1395 | } |
||
1396 | |||
1397 | /** |
||
1398 | * Get the invoice total. |
||
1399 | * |
||
1400 | * @since 1.0.19 |
||
1401 | * @return float |
||
1402 | */ |
||
1403 | public function get_total() { |
||
1404 | $total = $this->is_renewal() ? $this->get_recurring_total() : $this->get_initial_total(); |
||
1405 | return apply_filters( 'getpaid_get_invoice_total_amount', $total, $this ); |
||
1406 | } |
||
1407 | |||
1408 | /** |
||
1409 | * Get the invoice totals. |
||
1410 | * |
||
1411 | * @since 1.0.19 |
||
1412 | * @return float |
||
1413 | */ |
||
1414 | public function get_totals() { |
||
1415 | return $this->totals; |
||
1416 | } |
||
1417 | |||
1418 | /** |
||
1419 | * Get the initial invoice total. |
||
1420 | * |
||
1421 | * @since 1.0.19 |
||
1422 | * @param string $context View or edit context. |
||
1423 | * @return float |
||
1424 | */ |
||
1425 | public function get_initial_total() { |
||
1426 | |||
1427 | if ( empty( $this->totals ) ) { |
||
1428 | $this->recalculate_total(); |
||
1429 | } |
||
1430 | |||
1431 | $tax = $this->totals['tax']['initial']; |
||
1432 | $fee = $this->totals['fee']['initial']; |
||
1433 | $discount = $this->totals['discount']['initial']; |
||
1434 | $subtotal = $this->totals['subtotal']['initial']; |
||
1435 | $total = $tax + $fee - $discount + $subtotal; |
||
1436 | |||
1437 | if ( 0 > $total ) { |
||
1438 | $total = 0; |
||
1439 | } |
||
1440 | |||
1441 | return apply_filters( 'wpinv_get_initial_invoice_total', $total, $this ); |
||
1442 | } |
||
1443 | |||
1444 | /** |
||
1445 | * Get the recurring invoice total. |
||
1446 | * |
||
1447 | * @since 1.0.19 |
||
1448 | * @param string $context View or edit context. |
||
1449 | * @return float |
||
1450 | */ |
||
1451 | public function get_recurring_total() { |
||
1452 | |||
1453 | if ( empty( $this->totals ) ) { |
||
1454 | $this->recalculate_total(); |
||
1455 | } |
||
1456 | |||
1457 | $tax = $this->totals['tax']['recurring']; |
||
1458 | $fee = $this->totals['fee']['recurring']; |
||
1459 | $discount = $this->totals['discount']['recurring']; |
||
1460 | $subtotal = $this->totals['subtotal']['recurring']; |
||
1461 | $total = $tax + $fee - $discount + $subtotal; |
||
1462 | |||
1463 | if ( 0 > $total ) { |
||
1464 | $total = 0; |
||
1465 | } |
||
1466 | |||
1467 | return apply_filters( 'wpinv_get_recurring_invoice_total', $total, $this ); |
||
1468 | } |
||
1469 | |||
1470 | /** |
||
1471 | * Returns recurring payment details. |
||
1472 | * |
||
1473 | * @since 1.0.19 |
||
1474 | * @param string $field Optionally provide a field to return. |
||
1475 | * @param string $currency Whether to include the currency. |
||
1476 | * @return float |
||
1477 | */ |
||
1478 | public function get_recurring_details( $field = '', $currency = false ) { |
||
1479 | |||
1480 | // Maybe recalculate totals. |
||
1481 | if ( empty( $this->totals ) ) { |
||
1482 | $this->recalculate_total(); |
||
1483 | } |
||
1484 | |||
1485 | // Prepare recurring totals. |
||
1486 | $data = apply_filters( |
||
1487 | 'wpinv_get_invoice_recurring_details', |
||
1488 | array( |
||
1489 | 'cart_details' => $this->get_cart_details(), |
||
1490 | 'subtotal' => $this->totals['subtotal']['recurring'], |
||
1491 | 'discount' => $this->totals['discount']['recurring'], |
||
1492 | 'tax' => $this->totals['tax']['recurring'], |
||
1493 | 'fee' => $this->totals['fee']['recurring'], |
||
1494 | 'total' => $this->get_recurring_total(), |
||
1495 | ), |
||
1496 | $this, |
||
1497 | $field, |
||
1498 | $currency |
||
1499 | ); |
||
1500 | |||
1501 | if ( isset( $data[$field] ) ) { |
||
1502 | return ( $currency ? wpinv_price( $data[$field], $this->get_currency() ) : $data[$field] ); |
||
1503 | } |
||
1504 | |||
1505 | return $data; |
||
1506 | } |
||
1507 | |||
1508 | /** |
||
1509 | * Get the invoice fees. |
||
1510 | * |
||
1511 | * @since 1.0.19 |
||
1512 | * @param string $context View or edit context. |
||
1513 | * @return array |
||
1514 | */ |
||
1515 | public function get_fees( $context = 'view' ) { |
||
1516 | return wpinv_parse_list( $this->get_prop( 'fees', $context ) ); |
||
1517 | } |
||
1518 | |||
1519 | /** |
||
1520 | * Get the invoice discounts. |
||
1521 | * |
||
1522 | * @since 1.0.19 |
||
1523 | * @param string $context View or edit context. |
||
1524 | * @return array |
||
1525 | */ |
||
1526 | public function get_discounts( $context = 'view' ) { |
||
1527 | return wpinv_parse_list( $this->get_prop( 'discounts', $context ) ); |
||
1528 | } |
||
1529 | |||
1530 | /** |
||
1531 | * Get the invoice taxes. |
||
1532 | * |
||
1533 | * @since 1.0.19 |
||
1534 | * @param string $context View or edit context. |
||
1535 | * @return array |
||
1536 | */ |
||
1537 | public function get_taxes( $context = 'view' ) { |
||
1538 | return wpinv_parse_list( $this->get_prop( 'taxes', $context ) ); |
||
1539 | } |
||
1540 | |||
1541 | /** |
||
1542 | * Get the invoice items. |
||
1543 | * |
||
1544 | * @since 1.0.19 |
||
1545 | * @param string $context View or edit context. |
||
1546 | * @return GetPaid_Form_Item[] |
||
1547 | */ |
||
1548 | public function get_items( $context = 'view' ) { |
||
1549 | return $this->get_prop( 'items', $context ); |
||
1550 | } |
||
1551 | |||
1552 | /** |
||
1553 | * Get the invoice's payment form. |
||
1554 | * |
||
1555 | * @since 1.0.19 |
||
1556 | * @param string $context View or edit context. |
||
1557 | * @return int |
||
1558 | */ |
||
1559 | public function get_payment_form( $context = 'view' ) { |
||
1560 | return intval( $this->get_prop( 'payment_form', $context ) ); |
||
1561 | } |
||
1562 | |||
1563 | /** |
||
1564 | * Get the invoice's submission id. |
||
1565 | * |
||
1566 | * @since 1.0.19 |
||
1567 | * @param string $context View or edit context. |
||
1568 | * @return string |
||
1569 | */ |
||
1570 | public function get_submission_id( $context = 'view' ) { |
||
1571 | return $this->get_prop( 'submission_id', $context ); |
||
1572 | } |
||
1573 | |||
1574 | /** |
||
1575 | * Get the invoice's discount code. |
||
1576 | * |
||
1577 | * @since 1.0.19 |
||
1578 | * @param string $context View or edit context. |
||
1579 | * @return string |
||
1580 | */ |
||
1581 | public function get_discount_code( $context = 'view' ) { |
||
1582 | return $this->get_prop( 'discount_code', $context ); |
||
1583 | } |
||
1584 | |||
1585 | /** |
||
1586 | * Get the invoice's gateway. |
||
1587 | * |
||
1588 | * @since 1.0.19 |
||
1589 | * @param string $context View or edit context. |
||
1590 | * @return string |
||
1591 | */ |
||
1592 | public function get_gateway( $context = 'view' ) { |
||
1593 | return $this->get_prop( 'gateway', $context ); |
||
1594 | } |
||
1595 | |||
1596 | /** |
||
1597 | * Get the invoice's gateway display title. |
||
1598 | * |
||
1599 | * @since 1.0.19 |
||
1600 | * @return string |
||
1601 | */ |
||
1602 | public function get_gateway_title() { |
||
1603 | $title = wpinv_get_gateway_checkout_label( $this->get_gateway() ); |
||
1604 | return apply_filters( 'wpinv_gateway_title', $title, $this->get_id(), $this ); |
||
1605 | } |
||
1606 | |||
1607 | /** |
||
1608 | * Get the invoice's transaction id. |
||
1609 | * |
||
1610 | * @since 1.0.19 |
||
1611 | * @param string $context View or edit context. |
||
1612 | * @return string |
||
1613 | */ |
||
1614 | public function get_transaction_id( $context = 'view' ) { |
||
1615 | return $this->get_prop( 'transaction_id', $context ); |
||
1616 | } |
||
1617 | |||
1618 | /** |
||
1619 | * Get the invoice's currency. |
||
1620 | * |
||
1621 | * @since 1.0.19 |
||
1622 | * @param string $context View or edit context. |
||
1623 | * @return string |
||
1624 | */ |
||
1625 | public function get_currency( $context = 'view' ) { |
||
1626 | $currency = $this->get_prop( 'currency', $context ); |
||
1627 | return empty( $currency ) ? wpinv_get_currency() : $currency; |
||
1628 | } |
||
1629 | |||
1630 | /** |
||
1631 | * Checks if we are charging taxes for this invoice. |
||
1632 | * |
||
1633 | * @since 1.0.19 |
||
1634 | * @param string $context View or edit context. |
||
1635 | * @return bool |
||
1636 | */ |
||
1637 | public function get_disable_taxes( $context = 'view' ) { |
||
1638 | return (bool) $this->get_prop( 'disable_taxes', $context ); |
||
1639 | } |
||
1640 | |||
1641 | /** |
||
1642 | * Retrieves the subscription id for an invoice. |
||
1643 | * |
||
1644 | * @since 1.0.19 |
||
1645 | * @param string $context View or edit context. |
||
1646 | * @return int |
||
1647 | */ |
||
1648 | public function get_subscription_id( $context = 'view' ) { |
||
1649 | return $this->is_renewal() ? $this->get_parent()->get_subscription_id( $context ) : $this->get_prop( 'subscription_id', $context ); |
||
1650 | } |
||
1651 | |||
1652 | /** |
||
1653 | * Retrieves the remote subscription id for an invoice. |
||
1654 | * |
||
1655 | * @since 1.0.19 |
||
1656 | * @param string $context View or edit context. |
||
1657 | * @return int |
||
1658 | */ |
||
1659 | public function get_remote_subscription_id( $context = 'view' ) { |
||
1660 | $subscription_id = $this->get_prop( 'remote_subscription_id', $context ); |
||
1661 | |||
1662 | if ( empty( $subscription_id ) && $this->is_renewal() ) { |
||
1663 | $parent = $this->get_parent(); |
||
1664 | return $parent->get_subscription_id( $context ); |
||
1665 | } |
||
1666 | |||
1667 | return $subscription_id; |
||
1668 | } |
||
1669 | |||
1670 | /** |
||
1671 | * Retrieves the payment meta for an invoice. |
||
1672 | * |
||
1673 | * @since 1.0.19 |
||
1674 | * @param string $context View or edit context. |
||
1675 | * @return array |
||
1676 | */ |
||
1677 | public function get_payment_meta( $context = 'view' ) { |
||
1678 | |||
1679 | return array( |
||
1680 | 'price' => $this->get_total( $context ), |
||
1681 | 'date' => $this->get_date_created( $context ), |
||
1682 | 'user_email' => $this->get_email( $context ), |
||
1683 | 'invoice_key' => $this->get_key( $context ), |
||
1684 | 'currency' => $this->get_currency( $context ), |
||
1685 | 'items' => $this->get_items( $context ), |
||
1686 | 'user_info' => $this->get_user_info( $context ), |
||
1687 | 'cart_details' => $this->get_cart_details(), |
||
1688 | 'status' => $this->get_status( $context ), |
||
1689 | 'fees' => $this->get_fees( $context ), |
||
1690 | 'taxes' => $this->get_taxes( $context ), |
||
1691 | ); |
||
1692 | |||
1693 | } |
||
1694 | |||
1695 | /** |
||
1696 | * Retrieves the cart details for an invoice. |
||
1697 | * |
||
1698 | * @since 1.0.19 |
||
1699 | * @return array |
||
1700 | */ |
||
1701 | public function get_cart_details() { |
||
1702 | $items = $this->get_items(); |
||
1703 | $cart_details = array(); |
||
1704 | |||
1705 | foreach ( $items as $item_id => $item ) { |
||
1706 | $item->invoice_id = $this->get_id(); |
||
1707 | $cart_details[] = $item->prepare_data_for_saving(); |
||
1708 | } |
||
1709 | |||
1710 | return $cart_details; |
||
1711 | } |
||
1712 | |||
1713 | /** |
||
1714 | * Retrieves the recurring item. |
||
1715 | * |
||
1716 | * @return null|GetPaid_Form_Item|int |
||
1717 | */ |
||
1718 | public function get_recurring( $object = false ) { |
||
1719 | |||
1720 | // Are we returning an object? |
||
1721 | if ( $object ) { |
||
1722 | return $this->get_item( $this->recurring_item ); |
||
1723 | } |
||
1724 | |||
1725 | return $this->recurring_item; |
||
1726 | } |
||
1727 | |||
1728 | /** |
||
1729 | * Retrieves the subscription name. |
||
1730 | * |
||
1731 | * @since 1.0.19 |
||
1732 | * @return string |
||
1733 | */ |
||
1734 | public function get_subscription_name() { |
||
1735 | |||
1736 | // Retrieve the recurring name |
||
1737 | $item = $this->get_recurring( true ); |
||
1738 | |||
1739 | // Abort if it does not exist. |
||
1740 | if ( empty( $item ) ) { |
||
1741 | return ''; |
||
1742 | } |
||
1743 | |||
1744 | // Return the item name. |
||
1745 | return apply_filters( 'wpinv_invoice_get_subscription_name', $item->get_name(), $this ); |
||
1746 | } |
||
1747 | |||
1748 | /** |
||
1749 | * Retrieves the view url. |
||
1750 | * |
||
1751 | * @since 1.0.19 |
||
1752 | * @return string |
||
1753 | */ |
||
1754 | public function get_view_url() { |
||
1755 | $invoice_url = get_permalink( $this->get_id() ); |
||
1756 | $invoice_url = add_query_arg( 'invoice_key', $this->get_key(), $invoice_url ); |
||
1757 | return apply_filters( 'wpinv_get_view_url', $invoice_url, $this ); |
||
1758 | } |
||
1759 | |||
1760 | /** |
||
1761 | * Retrieves the payment url. |
||
1762 | * |
||
1763 | * @since 1.0.19 |
||
1764 | * @return string |
||
1765 | */ |
||
1766 | public function get_checkout_payment_url( $deprecated = false, $secret = false ) { |
||
1767 | |||
1768 | // Retrieve the checkout url. |
||
1769 | $pay_url = wpinv_get_checkout_uri(); |
||
1770 | |||
1771 | // Maybe force ssl. |
||
1772 | if ( is_ssl() ) { |
||
1773 | $pay_url = str_replace( 'http:', 'https:', $pay_url ); |
||
1774 | } |
||
1775 | |||
1776 | // Add the invoice key. |
||
1777 | $pay_url = add_query_arg( 'invoice_key', $this->get_key(), $pay_url ); |
||
1778 | |||
1779 | // (Maybe?) add a secret |
||
1780 | if ( $secret ) { |
||
1781 | $pay_url = add_query_arg( array( '_wpipay' => md5( $this->get_user_id() . '::' . $this->get_email() . '::' . $this->get_key() ) ), $pay_url ); |
||
1782 | } |
||
1783 | |||
1784 | return apply_filters( 'wpinv_get_checkout_payment_url', $pay_url, $this, $deprecated, $secret ); |
||
1785 | } |
||
1786 | |||
1787 | /** |
||
1788 | * Retrieves the receipt url. |
||
1789 | * |
||
1790 | * @since 1.0.19 |
||
1791 | * @return string |
||
1792 | */ |
||
1793 | public function get_receipt_url() { |
||
1794 | |||
1795 | // Retrieve the checkout url. |
||
1796 | $receipt_url = wpinv_get_success_page_uri(); |
||
1797 | |||
1798 | // Maybe force ssl. |
||
1799 | if ( is_ssl() ) { |
||
1800 | $receipt_url = str_replace( 'http:', 'https:', $receipt_url ); |
||
1801 | } |
||
1802 | |||
1803 | // Add the invoice key. |
||
1804 | $receipt_url = add_query_arg( 'invoice_key', $this->get_key(), $receipt_url ); |
||
1805 | |||
1806 | return apply_filters( 'getpaid_get_invoice_receipt_url', $receipt_url, $this ); |
||
1807 | } |
||
1808 | |||
1809 | /** |
||
1810 | * Retrieves the default status. |
||
1811 | * |
||
1812 | * @since 1.0.19 |
||
1813 | * @return string |
||
1814 | */ |
||
1815 | public function get_default_status() { |
||
1816 | |||
1817 | $type = $this->get_type(); |
||
1818 | $status = "wpi-$type-pending"; |
||
1819 | return str_replace( '-invoice', '', $status ); |
||
1820 | |||
1821 | } |
||
1822 | |||
1823 | /** |
||
1824 | * Magic method for accessing invoice properties. |
||
1825 | * |
||
1826 | * @since 1.0.15 |
||
1827 | * @access public |
||
1828 | * |
||
1829 | * @param string $key Discount data to retrieve |
||
1830 | * @param string $context View or edit context. |
||
1831 | * @return mixed Value of the given invoice property (if set). |
||
1832 | */ |
||
1833 | public function get( $key, $context = 'view' ) { |
||
1834 | return $this->get_prop( $key, $context ); |
||
1835 | } |
||
1836 | |||
1837 | /* |
||
1838 | |-------------------------------------------------------------------------- |
||
1839 | | Setters |
||
1840 | |-------------------------------------------------------------------------- |
||
1841 | | |
||
1842 | | Functions for setting item data. These should not update anything in the |
||
1843 | | database itself and should only change what is stored in the class |
||
1844 | | object. |
||
1845 | */ |
||
1846 | |||
1847 | /** |
||
1848 | * Magic method for setting invoice properties. |
||
1849 | * |
||
1850 | * @since 1.0.19 |
||
1851 | * @access public |
||
1852 | * |
||
1853 | * @param string $key Discount data to retrieve |
||
1854 | * @param mixed $value new value. |
||
1855 | * @return mixed Value of the given invoice property (if set). |
||
1856 | */ |
||
1857 | public function set( $key, $value ) { |
||
1858 | |||
1859 | $setter = "set_$key"; |
||
1860 | if ( is_callable( array( $this, $setter ) ) ) { |
||
1861 | $this->{$setter}( $value ); |
||
1862 | } |
||
1863 | |||
1864 | } |
||
1865 | |||
1866 | /** |
||
1867 | * Sets item status. |
||
1868 | * |
||
1869 | * @since 1.0.19 |
||
1870 | * @param string $new_status New status. |
||
1871 | * @param string $note Optional note to add. |
||
1872 | * @param bool $manual_update Is this a manual status change?. |
||
1873 | * @return array details of change. |
||
1874 | */ |
||
1875 | public function set_status( $new_status, $note = '', $manual_update = false ) { |
||
1876 | $old_status = $this->get_status(); |
||
1877 | |||
1878 | $statuses = $this->get_all_statuses(); |
||
1879 | |||
1880 | if ( isset( $statuses[ 'draft' ] ) ) { |
||
1881 | unset( $statuses[ 'draft' ] ); |
||
1882 | } |
||
1883 | |||
1884 | $this->set_prop( 'status', $new_status ); |
||
1885 | |||
1886 | // If setting the status, ensure it's set to a valid status. |
||
1887 | if ( true === $this->object_read ) { |
||
1888 | |||
1889 | // Only allow valid new status. |
||
1890 | if ( ! array_key_exists( $new_status, $statuses ) ) { |
||
1891 | $new_status = $this->get_default_status(); |
||
1892 | } |
||
1893 | |||
1894 | // If the old status is set but unknown (e.g. draft) assume its pending for action usage. |
||
1895 | if ( $old_status && ! array_key_exists( $new_status, $statuses ) ) { |
||
1896 | $old_status = $this->get_default_status(); |
||
1897 | } |
||
1898 | |||
1899 | // Paid - Renewal (i.e when duplicating a parent invoice ) |
||
1900 | if ( $new_status == 'wpi-pending' && $old_status == 'publish' && ! $this->get_id() ) { |
||
1901 | $old_status = 'wpi-pending'; |
||
1902 | } |
||
1903 | |||
1904 | } |
||
1905 | |||
1906 | if ( true === $this->object_read && $old_status !== $new_status ) { |
||
1907 | $this->status_transition = array( |
||
1908 | 'from' => ! empty( $this->status_transition['from'] ) ? $this->status_transition['from'] : $old_status, |
||
1909 | 'to' => $new_status, |
||
1910 | 'note' => $note, |
||
1911 | 'manual' => (bool) $manual_update, |
||
1912 | ); |
||
1913 | |||
1914 | if ( $manual_update ) { |
||
1915 | do_action( 'getpaid_' . $this->object_type .'_edit_status', $this->get_id(), $new_status ); |
||
1916 | } |
||
1917 | |||
1918 | $this->maybe_set_date_paid(); |
||
1919 | |||
1920 | } |
||
1921 | |||
1922 | return array( |
||
1923 | 'from' => $old_status, |
||
1924 | 'to' => $new_status, |
||
1925 | ); |
||
1926 | } |
||
1927 | |||
1928 | /** |
||
1929 | * Maybe set date paid. |
||
1930 | * |
||
1931 | * Sets the date paid variable when transitioning to the payment complete |
||
1932 | * order status. |
||
1933 | * |
||
1934 | * @since 1.0.19 |
||
1935 | */ |
||
1936 | public function maybe_set_date_paid() { |
||
1937 | |||
1938 | if ( ! $this->get_date_completed( 'edit' ) && $this->is_paid() ) { |
||
1939 | $this->set_date_completed( current_time( 'mysql' ) ); |
||
1940 | } |
||
1941 | } |
||
1942 | |||
1943 | /** |
||
1944 | * Set parent invoice ID. |
||
1945 | * |
||
1946 | * @since 1.0.19 |
||
1947 | */ |
||
1948 | public function set_parent_id( $value ) { |
||
1949 | if ( $value && ( $value === $this->get_id() ) ) { |
||
1950 | return; |
||
1951 | } |
||
1952 | $this->set_prop( 'parent_id', absint( $value ) ); |
||
1953 | } |
||
1954 | |||
1955 | /** |
||
1956 | * Set plugin version when the invoice was created. |
||
1957 | * |
||
1958 | * @since 1.0.19 |
||
1959 | */ |
||
1960 | public function set_version( $value ) { |
||
1961 | $this->set_prop( 'version', $value ); |
||
1962 | } |
||
1963 | |||
1964 | /** |
||
1965 | * Set date when the invoice was created. |
||
1966 | * |
||
1967 | * @since 1.0.19 |
||
1968 | * @param string $value Value to set. |
||
1969 | * @return bool Whether or not the date was set. |
||
1970 | */ |
||
1971 | public function set_date_created( $value ) { |
||
1972 | $date = strtotime( $value ); |
||
1973 | |||
1974 | if ( $date && $value !== '0000-00-00 00:00:00' ) { |
||
1975 | $this->set_prop( 'date_created', date( 'Y-m-d H:i:s', $date ) ); |
||
1976 | return true; |
||
1977 | } |
||
1978 | |||
1979 | return $this->set_prop( 'date_created', '' ); |
||
1980 | |||
1981 | } |
||
1982 | |||
1983 | /** |
||
1984 | * Set date invoice due date. |
||
1985 | * |
||
1986 | * @since 1.0.19 |
||
1987 | * @param string $value Value to set. |
||
1988 | * @return bool Whether or not the date was set. |
||
1989 | */ |
||
1990 | public function set_due_date( $value ) { |
||
1991 | $date = strtotime( $value ); |
||
1992 | |||
1993 | if ( $date && $value !== '0000-00-00 00:00:00' ) { |
||
1994 | $this->set_prop( 'due_date', date( 'Y-m-d H:i:s', $date ) ); |
||
1995 | return true; |
||
1996 | } |
||
1997 | |||
1998 | $this->set_prop( 'due_date', '' ); |
||
1999 | return false; |
||
2000 | |||
2001 | } |
||
2002 | |||
2003 | /** |
||
2004 | * Alias of self::set_due_date(). |
||
2005 | * |
||
2006 | * @since 1.0.19 |
||
2007 | * @param string $value New name. |
||
2008 | */ |
||
2009 | public function set_date_due( $value ) { |
||
2010 | $this->set_due_date( $value ); |
||
2011 | } |
||
2012 | |||
2013 | /** |
||
2014 | * Set date invoice was completed. |
||
2015 | * |
||
2016 | * @since 1.0.19 |
||
2017 | * @param string $value Value to set. |
||
2018 | * @return bool Whether or not the date was set. |
||
2019 | */ |
||
2020 | public function set_completed_date( $value ) { |
||
2021 | $date = strtotime( $value ); |
||
2022 | |||
2023 | if ( $date && $value !== '0000-00-00 00:00:00' ) { |
||
2024 | $this->set_prop( 'completed_date', date( 'Y-m-d H:i:s', $date ) ); |
||
2025 | return true; |
||
2026 | } |
||
2027 | |||
2028 | $this->set_prop( 'completed_date', '' ); |
||
2029 | return false; |
||
2030 | |||
2031 | } |
||
2032 | |||
2033 | /** |
||
2034 | * Alias of self::set_completed_date(). |
||
2035 | * |
||
2036 | * @since 1.0.19 |
||
2037 | * @param string $value New name. |
||
2038 | */ |
||
2039 | public function set_date_completed( $value ) { |
||
2040 | $this->set_completed_date( $value ); |
||
2041 | } |
||
2042 | |||
2043 | /** |
||
2044 | * Set date when the invoice was last modified. |
||
2045 | * |
||
2046 | * @since 1.0.19 |
||
2047 | * @param string $value Value to set. |
||
2048 | * @return bool Whether or not the date was set. |
||
2049 | */ |
||
2050 | public function set_date_modified( $value ) { |
||
2051 | $date = strtotime( $value ); |
||
2052 | |||
2053 | if ( $date && $value !== '0000-00-00 00:00:00' ) { |
||
2054 | $this->set_prop( 'date_modified', date( 'Y-m-d H:i:s', $date ) ); |
||
2055 | return true; |
||
2056 | } |
||
2057 | |||
2058 | $this->set_prop( 'date_modified', '' ); |
||
2059 | return false; |
||
2060 | |||
2061 | } |
||
2062 | |||
2063 | /** |
||
2064 | * Set the invoice number. |
||
2065 | * |
||
2066 | * @since 1.0.19 |
||
2067 | * @param string $value New number. |
||
2068 | */ |
||
2069 | public function set_number( $value ) { |
||
2070 | $number = sanitize_text_field( $value ); |
||
2071 | $this->set_prop( 'number', $number ); |
||
2072 | } |
||
2073 | |||
2074 | /** |
||
2075 | * Set the invoice type. |
||
2076 | * |
||
2077 | * @since 1.0.19 |
||
2078 | * @param string $value Type. |
||
2079 | */ |
||
2080 | public function set_type( $value ) { |
||
2081 | $type = sanitize_text_field( str_replace( 'wpi_', '', $value ) ); |
||
2082 | $this->set_prop( 'type', $type ); |
||
2083 | } |
||
2084 | |||
2085 | /** |
||
2086 | * Set the invoice post type. |
||
2087 | * |
||
2088 | * @since 1.0.19 |
||
2089 | * @param string $value Post type. |
||
2090 | */ |
||
2091 | public function set_post_type( $value ) { |
||
2092 | if ( getpaid_is_invoice_post_type( $value ) ) { |
||
2093 | $this->set_type( $value ); |
||
2094 | $this->set_prop( 'post_type', $value ); |
||
2095 | } |
||
2096 | } |
||
2097 | |||
2098 | /** |
||
2099 | * Set the invoice key. |
||
2100 | * |
||
2101 | * @since 1.0.19 |
||
2102 | * @param string $value New key. |
||
2103 | */ |
||
2104 | public function set_key( $value ) { |
||
2105 | $key = sanitize_text_field( $value ); |
||
2106 | $this->set_prop( 'key', $key ); |
||
2107 | } |
||
2108 | |||
2109 | /** |
||
2110 | * Set the invoice mode. |
||
2111 | * |
||
2112 | * @since 1.0.19 |
||
2113 | * @param string $value mode. |
||
2114 | */ |
||
2115 | public function set_mode( $value ) { |
||
2116 | if ( ! in_array( $value, array( 'live', 'test' ) ) ) { |
||
2117 | $this->set_prop( 'value', $value ); |
||
2118 | } |
||
2119 | } |
||
2120 | |||
2121 | /** |
||
2122 | * Set the invoice path. |
||
2123 | * |
||
2124 | * @since 1.0.19 |
||
2125 | * @param string $value path. |
||
2126 | */ |
||
2127 | public function set_path( $value ) { |
||
2128 | $this->set_prop( 'path', $value ); |
||
2129 | } |
||
2130 | |||
2131 | /** |
||
2132 | * Set the invoice name. |
||
2133 | * |
||
2134 | * @since 1.0.19 |
||
2135 | * @param string $value New name. |
||
2136 | */ |
||
2137 | public function set_name( $value ) { |
||
2138 | $name = sanitize_text_field( $value ); |
||
2139 | $this->set_prop( 'name', $name ); |
||
2140 | } |
||
2141 | |||
2142 | /** |
||
2143 | * Alias of self::set_name(). |
||
2144 | * |
||
2145 | * @since 1.0.19 |
||
2146 | * @param string $value New name. |
||
2147 | */ |
||
2148 | public function set_title( $value ) { |
||
2149 | $this->set_name( $value ); |
||
2150 | } |
||
2151 | |||
2152 | /** |
||
2153 | * Set the invoice description. |
||
2154 | * |
||
2155 | * @since 1.0.19 |
||
2156 | * @param string $value New description. |
||
2157 | */ |
||
2158 | public function set_description( $value ) { |
||
2159 | $description = wp_kses_post( $value ); |
||
2160 | return $this->set_prop( 'description', $description ); |
||
2161 | } |
||
2162 | |||
2163 | /** |
||
2164 | * Alias of self::set_description(). |
||
2165 | * |
||
2166 | * @since 1.0.19 |
||
2167 | * @param string $value New description. |
||
2168 | */ |
||
2169 | public function set_excerpt( $value ) { |
||
2170 | $this->set_description( $value ); |
||
2171 | } |
||
2172 | |||
2173 | /** |
||
2174 | * Alias of self::set_description(). |
||
2175 | * |
||
2176 | * @since 1.0.19 |
||
2177 | * @param string $value New description. |
||
2178 | */ |
||
2179 | public function set_summary( $value ) { |
||
2180 | $this->set_description( $value ); |
||
2181 | } |
||
2182 | |||
2183 | /** |
||
2184 | * Set the receiver of the invoice. |
||
2185 | * |
||
2186 | * @since 1.0.19 |
||
2187 | * @param int $value New author. |
||
2188 | */ |
||
2189 | public function set_author( $value ) { |
||
2190 | $user = get_user_by( 'id', (int) $value ); |
||
2191 | |||
2192 | if ( $user && $user->ID ) { |
||
2193 | $this->set_prop( 'author', $user->ID ); |
||
2194 | $this->set_prop( 'email', $user->user_email ); |
||
2195 | } |
||
2196 | |||
2197 | } |
||
2198 | |||
2199 | /** |
||
2200 | * Alias of self::set_author(). |
||
2201 | * |
||
2202 | * @since 1.0.19 |
||
2203 | * @param int $value New user id. |
||
2204 | */ |
||
2205 | public function set_user_id( $value ) { |
||
2206 | $this->set_author( $value ); |
||
2207 | } |
||
2208 | |||
2209 | /** |
||
2210 | * Alias of self::set_author(). |
||
2211 | * |
||
2212 | * @since 1.0.19 |
||
2213 | * @param int $value New user id. |
||
2214 | */ |
||
2215 | public function set_customer_id( $value ) { |
||
2216 | $this->set_author( $value ); |
||
2217 | } |
||
2218 | |||
2219 | /** |
||
2220 | * Set the customer's ip. |
||
2221 | * |
||
2222 | * @since 1.0.19 |
||
2223 | * @param string $value ip address. |
||
2224 | */ |
||
2225 | public function set_ip( $value ) { |
||
2226 | $this->set_prop( 'ip', $value ); |
||
2227 | } |
||
2228 | |||
2229 | /** |
||
2230 | * Alias of self::set_ip(). |
||
2231 | * |
||
2232 | * @since 1.0.19 |
||
2233 | * @param string $value ip address. |
||
2234 | */ |
||
2235 | public function set_user_ip( $value ) { |
||
2236 | $this->set_ip( $value ); |
||
2237 | } |
||
2238 | |||
2239 | /** |
||
2240 | * Set the customer's first name. |
||
2241 | * |
||
2242 | * @since 1.0.19 |
||
2243 | * @param string $value first name. |
||
2244 | */ |
||
2245 | public function set_first_name( $value ) { |
||
2246 | $this->set_prop( 'first_name', $value ); |
||
2247 | } |
||
2248 | |||
2249 | /** |
||
2250 | * Alias of self::set_first_name(). |
||
2251 | * |
||
2252 | * @since 1.0.19 |
||
2253 | * @param string $value first name. |
||
2254 | */ |
||
2255 | public function set_user_first_name( $value ) { |
||
2256 | $this->set_first_name( $value ); |
||
2257 | } |
||
2258 | |||
2259 | /** |
||
2260 | * Alias of self::set_first_name(). |
||
2261 | * |
||
2262 | * @since 1.0.19 |
||
2263 | * @param string $value first name. |
||
2264 | */ |
||
2265 | public function set_customer_first_name( $value ) { |
||
2266 | $this->set_first_name( $value ); |
||
2267 | } |
||
2268 | |||
2269 | /** |
||
2270 | * Set the customer's last name. |
||
2271 | * |
||
2272 | * @since 1.0.19 |
||
2273 | * @param string $value last name. |
||
2274 | */ |
||
2275 | public function set_last_name( $value ) { |
||
2276 | $this->set_prop( 'last_name', $value ); |
||
2277 | } |
||
2278 | |||
2279 | /** |
||
2280 | * Alias of self::set_last_name(). |
||
2281 | * |
||
2282 | * @since 1.0.19 |
||
2283 | * @param string $value last name. |
||
2284 | */ |
||
2285 | public function set_user_last_name( $value ) { |
||
2286 | $this->set_last_name( $value ); |
||
2287 | } |
||
2288 | |||
2289 | /** |
||
2290 | * Alias of self::set_last_name(). |
||
2291 | * |
||
2292 | * @since 1.0.19 |
||
2293 | * @param string $value last name. |
||
2294 | */ |
||
2295 | public function set_customer_last_name( $value ) { |
||
2296 | $this->set_last_name( $value ); |
||
2297 | } |
||
2298 | |||
2299 | /** |
||
2300 | * Set the customer's phone number. |
||
2301 | * |
||
2302 | * @since 1.0.19 |
||
2303 | * @param string $value phone. |
||
2304 | */ |
||
2305 | public function set_phone( $value ) { |
||
2306 | $this->set_prop( 'phone', $value ); |
||
2307 | } |
||
2308 | |||
2309 | /** |
||
2310 | * Alias of self::set_phone(). |
||
2311 | * |
||
2312 | * @since 1.0.19 |
||
2313 | * @param string $value phone. |
||
2314 | */ |
||
2315 | public function set_user_phone( $value ) { |
||
2316 | $this->set_phone( $value ); |
||
2317 | } |
||
2318 | |||
2319 | /** |
||
2320 | * Alias of self::set_phone(). |
||
2321 | * |
||
2322 | * @since 1.0.19 |
||
2323 | * @param string $value phone. |
||
2324 | */ |
||
2325 | public function set_customer_phone( $value ) { |
||
2326 | $this->set_phone( $value ); |
||
2327 | } |
||
2328 | |||
2329 | /** |
||
2330 | * Alias of self::set_phone(). |
||
2331 | * |
||
2332 | * @since 1.0.19 |
||
2333 | * @param string $value phone. |
||
2334 | */ |
||
2335 | public function set_phone_number( $value ) { |
||
2336 | $this->set_phone( $value ); |
||
2337 | } |
||
2338 | |||
2339 | /** |
||
2340 | * Set the customer's email address. |
||
2341 | * |
||
2342 | * @since 1.0.19 |
||
2343 | * @param string $value email address. |
||
2344 | */ |
||
2345 | public function set_email( $value ) { |
||
2346 | $this->set_prop( 'email', $value ); |
||
2347 | } |
||
2348 | |||
2349 | /** |
||
2350 | * Alias of self::set_email(). |
||
2351 | * |
||
2352 | * @since 1.0.19 |
||
2353 | * @param string $value email address. |
||
2354 | */ |
||
2355 | public function set_user_email( $value ) { |
||
2356 | $this->set_email( $value ); |
||
2357 | } |
||
2358 | |||
2359 | /** |
||
2360 | * Alias of self::set_email(). |
||
2361 | * |
||
2362 | * @since 1.0.19 |
||
2363 | * @param string $value email address. |
||
2364 | */ |
||
2365 | public function set_email_address( $value ) { |
||
2366 | $this->set_email( $value ); |
||
2367 | } |
||
2368 | |||
2369 | /** |
||
2370 | * Alias of self::set_email(). |
||
2371 | * |
||
2372 | * @since 1.0.19 |
||
2373 | * @param string $value email address. |
||
2374 | */ |
||
2375 | public function set_customer_email( $value ) { |
||
2376 | $this->set_email( $value ); |
||
2377 | } |
||
2378 | |||
2379 | /** |
||
2380 | * Set the customer's country. |
||
2381 | * |
||
2382 | * @since 1.0.19 |
||
2383 | * @param string $value country. |
||
2384 | */ |
||
2385 | public function set_country( $value ) { |
||
2386 | $this->set_prop( 'country', $value ); |
||
2387 | } |
||
2388 | |||
2389 | /** |
||
2390 | * Alias of self::set_country(). |
||
2391 | * |
||
2392 | * @since 1.0.19 |
||
2393 | * @param string $value country. |
||
2394 | */ |
||
2395 | public function set_user_country( $value ) { |
||
2396 | $this->set_country( $value ); |
||
2397 | } |
||
2398 | |||
2399 | /** |
||
2400 | * Alias of self::set_country(). |
||
2401 | * |
||
2402 | * @since 1.0.19 |
||
2403 | * @param string $value country. |
||
2404 | */ |
||
2405 | public function set_customer_country( $value ) { |
||
2406 | $this->set_country( $value ); |
||
2407 | } |
||
2408 | |||
2409 | /** |
||
2410 | * Set the customer's state. |
||
2411 | * |
||
2412 | * @since 1.0.19 |
||
2413 | * @param string $value state. |
||
2414 | */ |
||
2415 | public function set_state( $value ) { |
||
2416 | $this->set_prop( 'state', $value ); |
||
2417 | } |
||
2418 | |||
2419 | /** |
||
2420 | * Alias of self::set_state(). |
||
2421 | * |
||
2422 | * @since 1.0.19 |
||
2423 | * @param string $value state. |
||
2424 | */ |
||
2425 | public function set_user_state( $value ) { |
||
2426 | $this->set_state( $value ); |
||
2427 | } |
||
2428 | |||
2429 | /** |
||
2430 | * Alias of self::set_state(). |
||
2431 | * |
||
2432 | * @since 1.0.19 |
||
2433 | * @param string $value state. |
||
2434 | */ |
||
2435 | public function set_customer_state( $value ) { |
||
2436 | $this->set_state( $value ); |
||
2437 | } |
||
2438 | |||
2439 | /** |
||
2440 | * Set the customer's city. |
||
2441 | * |
||
2442 | * @since 1.0.19 |
||
2443 | * @param string $value city. |
||
2444 | */ |
||
2445 | public function set_city( $value ) { |
||
2446 | $this->set_prop( 'city', $value ); |
||
2447 | } |
||
2448 | |||
2449 | /** |
||
2450 | * Alias of self::set_city(). |
||
2451 | * |
||
2452 | * @since 1.0.19 |
||
2453 | * @param string $value city. |
||
2454 | */ |
||
2455 | public function set_user_city( $value ) { |
||
2456 | $this->set_city( $value ); |
||
2457 | } |
||
2458 | |||
2459 | /** |
||
2460 | * Alias of self::set_city(). |
||
2461 | * |
||
2462 | * @since 1.0.19 |
||
2463 | * @param string $value city. |
||
2464 | */ |
||
2465 | public function set_customer_city( $value ) { |
||
2466 | $this->set_city( $value ); |
||
2467 | } |
||
2468 | |||
2469 | /** |
||
2470 | * Set the customer's zip code. |
||
2471 | * |
||
2472 | * @since 1.0.19 |
||
2473 | * @param string $value zip. |
||
2474 | */ |
||
2475 | public function set_zip( $value ) { |
||
2476 | $this->set_prop( 'zip', $value ); |
||
2477 | } |
||
2478 | |||
2479 | /** |
||
2480 | * Alias of self::set_zip(). |
||
2481 | * |
||
2482 | * @since 1.0.19 |
||
2483 | * @param string $value zip. |
||
2484 | */ |
||
2485 | public function set_user_zip( $value ) { |
||
2486 | $this->set_zip( $value ); |
||
2487 | } |
||
2488 | |||
2489 | /** |
||
2490 | * Alias of self::set_zip(). |
||
2491 | * |
||
2492 | * @since 1.0.19 |
||
2493 | * @param string $value zip. |
||
2494 | */ |
||
2495 | public function set_customer_zip( $value ) { |
||
2496 | $this->set_zip( $value ); |
||
2497 | } |
||
2498 | |||
2499 | /** |
||
2500 | * Set the customer's company. |
||
2501 | * |
||
2502 | * @since 1.0.19 |
||
2503 | * @param string $value company. |
||
2504 | */ |
||
2505 | public function set_company( $value ) { |
||
2506 | $this->set_prop( 'company', $value ); |
||
2507 | } |
||
2508 | |||
2509 | /** |
||
2510 | * Alias of self::set_company(). |
||
2511 | * |
||
2512 | * @since 1.0.19 |
||
2513 | * @param string $value company. |
||
2514 | */ |
||
2515 | public function set_user_company( $value ) { |
||
2516 | $this->set_company( $value ); |
||
2517 | } |
||
2518 | |||
2519 | /** |
||
2520 | * Alias of self::set_company(). |
||
2521 | * |
||
2522 | * @since 1.0.19 |
||
2523 | * @param string $value company. |
||
2524 | */ |
||
2525 | public function set_customer_company( $value ) { |
||
2526 | $this->set_company( $value ); |
||
2527 | } |
||
2528 | |||
2529 | /** |
||
2530 | * Set the customer's var number. |
||
2531 | * |
||
2532 | * @since 1.0.19 |
||
2533 | * @param string $value var number. |
||
2534 | */ |
||
2535 | public function set_vat_number( $value ) { |
||
2536 | $this->set_prop( 'vat_number', $value ); |
||
2537 | } |
||
2538 | |||
2539 | /** |
||
2540 | * Alias of self::set_vat_number(). |
||
2541 | * |
||
2542 | * @since 1.0.19 |
||
2543 | * @param string $value var number. |
||
2544 | */ |
||
2545 | public function set_user_vat_number( $value ) { |
||
2546 | $this->set_vat_number( $value ); |
||
2547 | } |
||
2548 | |||
2549 | /** |
||
2550 | * Alias of self::set_vat_number(). |
||
2551 | * |
||
2552 | * @since 1.0.19 |
||
2553 | * @param string $value var number. |
||
2554 | */ |
||
2555 | public function set_customer_vat_number( $value ) { |
||
2556 | $this->set_vat_number( $value ); |
||
2557 | } |
||
2558 | |||
2559 | /** |
||
2560 | * Set the customer's vat rate. |
||
2561 | * |
||
2562 | * @since 1.0.19 |
||
2563 | * @param string $value var rate. |
||
2564 | */ |
||
2565 | public function set_vat_rate( $value ) { |
||
2566 | $this->set_prop( 'vat_rate', $value ); |
||
2567 | } |
||
2568 | |||
2569 | /** |
||
2570 | * Alias of self::set_vat_rate(). |
||
2571 | * |
||
2572 | * @since 1.0.19 |
||
2573 | * @param string $value var number. |
||
2574 | */ |
||
2575 | public function set_user_vat_rate( $value ) { |
||
2576 | $this->set_vat_rate( $value ); |
||
2577 | } |
||
2578 | |||
2579 | /** |
||
2580 | * Alias of self::set_vat_rate(). |
||
2581 | * |
||
2582 | * @since 1.0.19 |
||
2583 | * @param string $value var number. |
||
2584 | */ |
||
2585 | public function set_customer_vat_rate( $value ) { |
||
2586 | $this->set_vat_rate( $value ); |
||
2587 | } |
||
2588 | |||
2589 | /** |
||
2590 | * Set the customer's address. |
||
2591 | * |
||
2592 | * @since 1.0.19 |
||
2593 | * @param string $value address. |
||
2594 | */ |
||
2595 | public function set_address( $value ) { |
||
2596 | $this->set_prop( 'address', $value ); |
||
2597 | } |
||
2598 | |||
2599 | /** |
||
2600 | * Alias of self::set_address(). |
||
2601 | * |
||
2602 | * @since 1.0.19 |
||
2603 | * @param string $value address. |
||
2604 | */ |
||
2605 | public function set_user_address( $value ) { |
||
2606 | $this->set_address( $value ); |
||
2607 | } |
||
2608 | |||
2609 | /** |
||
2610 | * Alias of self::set_address(). |
||
2611 | * |
||
2612 | * @since 1.0.19 |
||
2613 | * @param string $value address. |
||
2614 | */ |
||
2615 | public function set_customer_address( $value ) { |
||
2616 | $this->set_address( $value ); |
||
2617 | } |
||
2618 | |||
2619 | /** |
||
2620 | * Set whether the customer has viewed the invoice or not. |
||
2621 | * |
||
2622 | * @since 1.0.19 |
||
2623 | * @param int|bool $value confirmed. |
||
2624 | */ |
||
2625 | public function set_is_viewed( $value ) { |
||
2626 | $this->set_prop( 'is_viewed', $value ); |
||
2627 | } |
||
2628 | |||
2629 | /** |
||
2630 | * Set extra email recipients. |
||
2631 | * |
||
2632 | * @since 1.0.19 |
||
2633 | * @param string $value email recipients. |
||
2634 | */ |
||
2635 | public function set_email_cc( $value ) { |
||
2636 | $this->set_prop( 'email_cc', $value ); |
||
2637 | } |
||
2638 | |||
2639 | /** |
||
2640 | * Set the invoice template. |
||
2641 | * |
||
2642 | * @since 1.0.19 |
||
2643 | * @param string $value template. |
||
2644 | */ |
||
2645 | public function set_template( $value ) { |
||
2646 | if ( in_array( $value, array( 'quantity', 'hours', 'amount' ) ) ) { |
||
2647 | $this->set_prop( 'template', $value ); |
||
2648 | } |
||
2649 | } |
||
2650 | |||
2651 | /** |
||
2652 | * Set the invoice source. |
||
2653 | * |
||
2654 | * @since 1.0.19 |
||
2655 | * @param string $value email recipients. |
||
2656 | */ |
||
2657 | public function created_via( $value ) { |
||
2658 | $this->set_prop( 'created_via', sanitize_text_field( $value ) ); |
||
2659 | } |
||
2660 | |||
2661 | /** |
||
2662 | * Set the customer's address confirmed status. |
||
2663 | * |
||
2664 | * @since 1.0.19 |
||
2665 | * @param int|bool $value confirmed. |
||
2666 | */ |
||
2667 | public function set_address_confirmed( $value ) { |
||
2668 | $this->set_prop( 'address_confirmed', $value ); |
||
2669 | } |
||
2670 | |||
2671 | /** |
||
2672 | * Alias of self::set_address_confirmed(). |
||
2673 | * |
||
2674 | * @since 1.0.19 |
||
2675 | * @param int|bool $value confirmed. |
||
2676 | */ |
||
2677 | public function set_user_address_confirmed( $value ) { |
||
2678 | $this->set_address_confirmed( $value ); |
||
2679 | } |
||
2680 | |||
2681 | /** |
||
2682 | * Alias of self::set_address_confirmed(). |
||
2683 | * |
||
2684 | * @since 1.0.19 |
||
2685 | * @param int|bool $value confirmed. |
||
2686 | */ |
||
2687 | public function set_customer_address_confirmed( $value ) { |
||
2688 | $this->set_address_confirmed( $value ); |
||
2689 | } |
||
2690 | |||
2691 | /** |
||
2692 | * Set the invoice sub total. |
||
2693 | * |
||
2694 | * @since 1.0.19 |
||
2695 | * @param float $value sub total. |
||
2696 | */ |
||
2697 | public function set_subtotal( $value ) { |
||
2698 | $this->set_prop( 'subtotal', $value ); |
||
2699 | } |
||
2700 | |||
2701 | /** |
||
2702 | * Set the invoice discount amount. |
||
2703 | * |
||
2704 | * @since 1.0.19 |
||
2705 | * @param float $value discount total. |
||
2706 | */ |
||
2707 | public function set_total_discount( $value ) { |
||
2708 | $this->set_prop( 'total_discount', $value ); |
||
2709 | } |
||
2710 | |||
2711 | /** |
||
2712 | * Alias of self::set_total_discount(). |
||
2713 | * |
||
2714 | * @since 1.0.19 |
||
2715 | * @param float $value discount total. |
||
2716 | */ |
||
2717 | public function set_discount( $value ) { |
||
2718 | $this->set_total_discount( $value ); |
||
2719 | } |
||
2720 | |||
2721 | /** |
||
2722 | * Set the invoice tax amount. |
||
2723 | * |
||
2724 | * @since 1.0.19 |
||
2725 | * @param float $value tax total. |
||
2726 | */ |
||
2727 | public function set_total_tax( $value ) { |
||
2728 | $this->set_prop( 'total_tax', $value ); |
||
2729 | } |
||
2730 | |||
2731 | /** |
||
2732 | * Alias of self::set_total_tax(). |
||
2733 | * |
||
2734 | * @since 1.0.19 |
||
2735 | * @param float $value tax total. |
||
2736 | */ |
||
2737 | public function set_tax_total( $value ) { |
||
2738 | $this->set_total_tax( $value ); |
||
2739 | } |
||
2740 | |||
2741 | /** |
||
2742 | * Set the invoice fees amount. |
||
2743 | * |
||
2744 | * @since 1.0.19 |
||
2745 | * @param float $value fees total. |
||
2746 | */ |
||
2747 | public function set_total_fees( $value ) { |
||
2748 | $this->set_prop( 'total_fees', $value ); |
||
2749 | } |
||
2750 | |||
2751 | /** |
||
2752 | * Alias of self::set_total_fees(). |
||
2753 | * |
||
2754 | * @since 1.0.19 |
||
2755 | * @param float $value fees total. |
||
2756 | */ |
||
2757 | public function set_fees_total( $value ) { |
||
2758 | $this->set_total_fees( $value ); |
||
2759 | } |
||
2760 | |||
2761 | /** |
||
2762 | * Set the invoice fees. |
||
2763 | * |
||
2764 | * @since 1.0.19 |
||
2765 | * @param array $value fees. |
||
2766 | */ |
||
2767 | public function set_fees( $value ) { |
||
2768 | |||
2769 | if ( ! is_array( $value ) ) { |
||
2770 | $value = array(); |
||
2771 | } |
||
2772 | |||
2773 | $this->set_prop( 'fees', $value ); |
||
2774 | |||
2775 | } |
||
2776 | |||
2777 | /** |
||
2778 | * Set the invoice taxes. |
||
2779 | * |
||
2780 | * @since 1.0.19 |
||
2781 | * @param array $value taxes. |
||
2782 | */ |
||
2783 | public function set_taxes( $value ) { |
||
2784 | |||
2785 | if ( ! is_array( $value ) ) { |
||
2786 | $value = array(); |
||
2787 | } |
||
2788 | |||
2789 | $this->set_prop( 'taxes', $value ); |
||
2790 | |||
2791 | } |
||
2792 | |||
2793 | /** |
||
2794 | * Set the invoice discounts. |
||
2795 | * |
||
2796 | * @since 1.0.19 |
||
2797 | * @param array $value discounts. |
||
2798 | */ |
||
2799 | public function set_discounts( $value ) { |
||
2800 | |||
2801 | if ( ! is_array( $value ) ) { |
||
2802 | $value = array(); |
||
2803 | } |
||
2804 | |||
2805 | $this->set_prop( 'discounts', $value ); |
||
2806 | } |
||
2807 | |||
2808 | /** |
||
2809 | * Set the invoice items. |
||
2810 | * |
||
2811 | * @since 1.0.19 |
||
2812 | * @param GetPaid_Form_Item[] $value items. |
||
2813 | */ |
||
2814 | public function set_items( $value ) { |
||
2815 | |||
2816 | // Remove existing items. |
||
2817 | $this->set_prop( 'items', array() ); |
||
2818 | |||
2819 | // Ensure that we have an array. |
||
2820 | if ( ! is_array( $value ) ) { |
||
2821 | return; |
||
2822 | } |
||
2823 | |||
2824 | foreach ( $value as $item ) { |
||
2825 | $this->add_item( $item ); |
||
2826 | } |
||
2827 | |||
2828 | } |
||
2829 | |||
2830 | /** |
||
2831 | * Set the payment form. |
||
2832 | * |
||
2833 | * @since 1.0.19 |
||
2834 | * @param int $value payment form. |
||
2835 | */ |
||
2836 | public function set_payment_form( $value ) { |
||
2837 | $this->set_prop( 'payment_form', $value ); |
||
2838 | } |
||
2839 | |||
2840 | /** |
||
2841 | * Set the submission id. |
||
2842 | * |
||
2843 | * @since 1.0.19 |
||
2844 | * @param string $value submission id. |
||
2845 | */ |
||
2846 | public function set_submission_id( $value ) { |
||
2847 | $this->set_prop( 'submission_id', $value ); |
||
2848 | } |
||
2849 | |||
2850 | /** |
||
2851 | * Set the discount code. |
||
2852 | * |
||
2853 | * @since 1.0.19 |
||
2854 | * @param string $value discount code. |
||
2855 | */ |
||
2856 | public function set_discount_code( $value ) { |
||
2857 | $this->set_prop( 'discount_code', $value ); |
||
2858 | } |
||
2859 | |||
2860 | /** |
||
2861 | * Set the gateway. |
||
2862 | * |
||
2863 | * @since 1.0.19 |
||
2864 | * @param string $value gateway. |
||
2865 | */ |
||
2866 | public function set_gateway( $value ) { |
||
2867 | $this->set_prop( 'gateway', $value ); |
||
2868 | } |
||
2869 | |||
2870 | /** |
||
2871 | * Set the transaction id. |
||
2872 | * |
||
2873 | * @since 1.0.19 |
||
2874 | * @param string $value transaction id. |
||
2875 | */ |
||
2876 | public function set_transaction_id( $value ) { |
||
2877 | if ( ! empty( $value ) ) { |
||
2878 | $this->set_prop( 'transaction_id', $value ); |
||
2879 | } |
||
2880 | } |
||
2881 | |||
2882 | /** |
||
2883 | * Set the currency id. |
||
2884 | * |
||
2885 | * @since 1.0.19 |
||
2886 | * @param string $value currency id. |
||
2887 | */ |
||
2888 | public function set_currency( $value ) { |
||
2889 | $this->set_prop( 'currency', $value ); |
||
2890 | } |
||
2891 | |||
2892 | /** |
||
2893 | * Set whether to disable taxes. |
||
2894 | * |
||
2895 | * @since 1.0.19 |
||
2896 | * @param bool $value value. |
||
2897 | */ |
||
2898 | public function set_disable_taxes( $value ) { |
||
2899 | $this->set_prop( 'disable_taxes', (bool) $value ); |
||
2900 | } |
||
2901 | |||
2902 | /** |
||
2903 | * Set the subscription id. |
||
2904 | * |
||
2905 | * @since 1.0.19 |
||
2906 | * @param string $value subscription id. |
||
2907 | */ |
||
2908 | public function set_subscription_id( $value ) { |
||
2909 | $this->set_prop( 'subscription_id', $value ); |
||
2910 | } |
||
2911 | |||
2912 | /** |
||
2913 | * Set the remote subscription id. |
||
2914 | * |
||
2915 | * @since 1.0.19 |
||
2916 | * @param string $value subscription id. |
||
2917 | */ |
||
2918 | public function set_remote_subscription_id( $value ) { |
||
2919 | $this->set_prop( 'remote_subscription_id', $value ); |
||
2920 | } |
||
2921 | |||
2922 | /* |
||
2923 | |-------------------------------------------------------------------------- |
||
2924 | | Boolean methods |
||
2925 | |-------------------------------------------------------------------------- |
||
2926 | | |
||
2927 | | Return true or false. |
||
2928 | | |
||
2929 | */ |
||
2930 | |||
2931 | /** |
||
2932 | * Checks if this is a parent invoice. |
||
2933 | */ |
||
2934 | public function is_parent() { |
||
2935 | $parent = $this->get_parent_id(); |
||
2936 | return apply_filters( 'wpinv_invoice_is_parent', empty( $parent ), $this ); |
||
2937 | } |
||
2938 | |||
2939 | /** |
||
2940 | * Checks if this is a renewal invoice. |
||
2941 | */ |
||
2942 | public function is_renewal() { |
||
2943 | return ! $this->is_parent(); |
||
2944 | } |
||
2945 | |||
2946 | /** |
||
2947 | * Checks if this is a recurring invoice. |
||
2948 | */ |
||
2949 | public function is_recurring() { |
||
2950 | return $this->is_renewal() || ! empty( $this->recurring_item ); |
||
2951 | } |
||
2952 | |||
2953 | /** |
||
2954 | * Checks if this is a taxable invoice. |
||
2955 | */ |
||
2956 | public function is_taxable() { |
||
2957 | return ! $this->get_disable_taxes(); |
||
2958 | } |
||
2959 | |||
2960 | /** |
||
2961 | * @deprecated |
||
2962 | */ |
||
2963 | public function has_vat() { |
||
2964 | global $wpinv_euvat, $wpi_country; |
||
2965 | |||
2966 | $requires_vat = false; |
||
2967 | |||
2968 | if ( $this->country ) { |
||
2969 | $wpi_country = $this->country; |
||
2970 | $requires_vat = $wpinv_euvat->requires_vat( $requires_vat, $this->get_user_id(), $wpinv_euvat->invoice_has_digital_rule( $this ) ); |
||
2971 | } |
||
2972 | |||
2973 | return apply_filters( 'wpinv_invoice_has_vat', $requires_vat, $this ); |
||
2974 | } |
||
2975 | |||
2976 | /** |
||
2977 | * Checks to see if the invoice requires payment. |
||
2978 | */ |
||
2979 | public function is_free() { |
||
2980 | $is_free = ( (float) wpinv_round_amount( $this->get_initial_total() ) == 0 ); |
||
2981 | |||
2982 | if ( $this->is_recurring() && $this->get_recurring_total() > 0 ) { |
||
2983 | $is_free = false; |
||
2984 | } |
||
2985 | |||
2986 | return apply_filters( 'wpinv_invoice_is_free', $is_free, $this ); |
||
2987 | } |
||
2988 | |||
2989 | /** |
||
2990 | * Checks if the invoice is paid. |
||
2991 | */ |
||
2992 | public function is_paid() { |
||
2993 | $is_paid = $this->has_status( array( 'publish', 'wpi-processing', 'wpi-renewal' ) ); |
||
2994 | return apply_filters( 'wpinv_invoice_is_paid', $is_paid, $this ); |
||
2995 | } |
||
2996 | |||
2997 | /** |
||
2998 | * Checks if the invoice needs payment. |
||
2999 | */ |
||
3000 | public function needs_payment() { |
||
3001 | $needs_payment = ! $this->is_paid() && ! $this->is_refunded() && ! $this->is_free(); |
||
3002 | return apply_filters( 'wpinv_needs_payment', $needs_payment, $this ); |
||
3003 | } |
||
3004 | |||
3005 | /** |
||
3006 | * Checks if the invoice is refunded. |
||
3007 | */ |
||
3008 | public function is_refunded() { |
||
3009 | $is_refunded = $this->has_status( 'wpi-refunded' ); |
||
3010 | return apply_filters( 'wpinv_invoice_is_refunded', $is_refunded, $this ); |
||
3011 | } |
||
3012 | |||
3013 | /** |
||
3014 | * Checks if the invoice is held. |
||
3015 | */ |
||
3016 | public function is_held() { |
||
3017 | $is_held = $this->has_status( 'wpi-onhold' ); |
||
3018 | return apply_filters( 'wpinv_invoice_is_held', $is_held, $this ); |
||
3019 | } |
||
3020 | |||
3021 | /** |
||
3022 | * Checks if the invoice is due. |
||
3023 | */ |
||
3024 | public function is_due() { |
||
3025 | $due_date = $this->get_due_date(); |
||
3026 | return empty( $due_date ) ? false : current_time( 'timestamp' ) > strtotime( $due_date ); |
||
3027 | } |
||
3028 | |||
3029 | /** |
||
3030 | * Checks if the invoice is draft. |
||
3031 | */ |
||
3032 | public function is_draft() { |
||
3033 | return $this->has_status( 'draft, auto-draft' ); |
||
3034 | } |
||
3035 | |||
3036 | /** |
||
3037 | * Checks if the invoice has a given status. |
||
3038 | */ |
||
3039 | public function has_status( $status ) { |
||
3040 | $status = wpinv_parse_list( $status ); |
||
3041 | return apply_filters( 'wpinv_has_status', in_array( $this->get_status(), $status ), $status ); |
||
3042 | } |
||
3043 | |||
3044 | /** |
||
3045 | * Checks if the invoice is of a given type. |
||
3046 | */ |
||
3047 | public function is_type( $type ) { |
||
3048 | $type = wpinv_parse_list( $type ); |
||
3049 | return in_array( $this->get_type(), $type ); |
||
3050 | } |
||
3051 | |||
3052 | /** |
||
3053 | * Checks if this is a quote object. |
||
3054 | * |
||
3055 | * @since 1.0.15 |
||
3056 | */ |
||
3057 | public function is_quote() { |
||
3058 | return 'wpi_quote' == $this->get_post_type(); |
||
3059 | } |
||
3060 | |||
3061 | /** |
||
3062 | * Check if the invoice (or it's parent has a free trial). |
||
3063 | * |
||
3064 | */ |
||
3065 | public function has_free_trial() { |
||
3066 | return $this->is_recurring() && 0 == $this->get_initial_total(); |
||
3067 | } |
||
3068 | |||
3069 | /** |
||
3070 | * @deprecated |
||
3071 | */ |
||
3072 | public function is_free_trial() { |
||
3073 | $this->has_free_trial(); |
||
3074 | } |
||
3075 | |||
3076 | /** |
||
3077 | * Check if the initial payment if 0. |
||
3078 | * |
||
3079 | */ |
||
3080 | public function is_initial_free() { |
||
3081 | $is_initial_free = ! ( (float) wpinv_round_amount( $this->get_initial_total() ) > 0 ); |
||
3082 | return apply_filters( 'wpinv_invoice_is_initial_free', $is_initial_free, $this->get_cart_details(), $this ); |
||
3083 | } |
||
3084 | |||
3085 | /** |
||
3086 | * Check if the recurring item has a free trial. |
||
3087 | * |
||
3088 | */ |
||
3089 | public function item_has_free_trial() { |
||
3090 | |||
3091 | // Ensure we have a recurring item. |
||
3092 | if ( ! $this->is_recurring() ) { |
||
3093 | return false; |
||
3094 | } |
||
3095 | |||
3096 | $item = $this->get_recurring( true ); |
||
3097 | return $item->has_free_trial(); |
||
3098 | } |
||
3099 | |||
3100 | /** |
||
3101 | * Check if the free trial is a result of a discount. |
||
3102 | */ |
||
3103 | public function is_free_trial_from_discount() { |
||
3104 | return $this->has_free_trial() && ! $this->item_has_free_trial(); |
||
3105 | } |
||
3106 | |||
3107 | /** |
||
3108 | * @deprecated |
||
3109 | */ |
||
3110 | public function discount_first_payment_only() { |
||
3111 | |||
3112 | $discount_code = $this->get_discount_code(); |
||
3113 | if ( empty( $this->discount_code ) || ! $this->is_recurring() ) { |
||
3114 | return true; |
||
3115 | } |
||
3116 | |||
3117 | $discount = wpinv_get_discount_obj( $discount_code ); |
||
3118 | |||
3119 | if ( ! $discount || ! $discount->exists() ) { |
||
3120 | return true; |
||
3121 | } |
||
3122 | |||
3123 | return ! $discount->get_is_recurring(); |
||
3124 | } |
||
3125 | |||
3126 | /* |
||
3127 | |-------------------------------------------------------------------------- |
||
3128 | | Cart related methods |
||
3129 | |-------------------------------------------------------------------------- |
||
3130 | | |
||
3131 | | Do not forget to recalculate totals after calling the following methods. |
||
3132 | | |
||
3133 | */ |
||
3134 | |||
3135 | /** |
||
3136 | * Adds an item to the invoice. |
||
3137 | * |
||
3138 | * @param GetPaid_Form_Item|array $item |
||
3139 | * @return WP_Error|Bool |
||
3140 | */ |
||
3141 | public function add_item( $item ) { |
||
3142 | |||
3143 | if ( is_array( $item ) ) { |
||
3144 | $item = $this->process_array_item( $item ); |
||
3145 | } |
||
3146 | |||
3147 | if ( is_numeric( $item ) ) { |
||
3148 | $item = new GetPaid_Form_Item( $item ); |
||
3149 | } |
||
3150 | |||
3151 | // Make sure that it is available for purchase. |
||
3152 | if ( $item->get_id() > 0 && ! $item->can_purchase() ) { |
||
3153 | return new WP_Error( 'invalid_item', __( 'This item is not available for purchase', 'invoicing' ) ); |
||
3154 | } |
||
3155 | |||
3156 | // Do we have a recurring item? |
||
3157 | if ( $item->is_recurring() ) { |
||
3158 | |||
3159 | // An invoice can only contain one recurring item. |
||
3160 | if ( ! empty( $this->recurring_item && $this->recurring_item != (int) $item->get_id() ) ) { |
||
3161 | return new WP_Error( 'recurring_item', __( 'An invoice can only contain one recurring item', 'invoicing' ) ); |
||
3162 | } |
||
3163 | |||
3164 | $this->recurring_item = $item->get_id(); |
||
3165 | } |
||
3166 | |||
3167 | // Invoice id. |
||
3168 | $item->invoice_id = (int) $this->get_id(); |
||
3169 | |||
3170 | // Retrieve all items. |
||
3171 | $items = $this->get_items(); |
||
3172 | $items[ (int) $item->get_id() ] = $item; |
||
3173 | |||
3174 | $this->set_prop( 'items', $items ); |
||
3175 | return true; |
||
3176 | } |
||
3177 | |||
3178 | /** |
||
3179 | * Converts an array to an item. |
||
3180 | * |
||
3181 | * @since 1.0.19 |
||
3182 | * @return GetPaid_Form_Item |
||
3183 | */ |
||
3184 | protected function process_array_item( $array ) { |
||
3185 | |||
3186 | $item_id = isset( $array['item_id'] ) ? $array['item_id'] : 0; |
||
3187 | $item = new GetPaid_Form_Item( $item_id ); |
||
3188 | |||
3189 | // Set item data. |
||
3190 | foreach ( array( 'name', 'price', 'description' ) as $key ) { |
||
3191 | if ( isset( $array[ "item_$key" ] ) ) { |
||
3192 | $method = "set_$key"; |
||
3193 | $item->$method( $array[ "item_$key" ] ); |
||
3194 | } |
||
3195 | } |
||
3196 | |||
3197 | if ( isset( $array['quantity'] ) ) { |
||
3198 | $item->set_quantity( $array['quantity'] ); |
||
3199 | } |
||
3200 | |||
3201 | // Set item meta. |
||
3202 | if ( isset( $array['meta'] ) && is_array( $array['meta'] ) ) { |
||
3203 | $item->set_item_meta( $array['meta'] ); |
||
3204 | } |
||
3205 | |||
3206 | return $item; |
||
3207 | |||
3208 | } |
||
3209 | |||
3210 | /** |
||
3211 | * Retrieves a specific item. |
||
3212 | * |
||
3213 | * @since 1.0.19 |
||
3214 | */ |
||
3215 | public function get_item( $item_id ) { |
||
3216 | $items = $this->get_items(); |
||
3217 | $item_id = (int) $item_id; |
||
3218 | return ( ! empty( $item_id ) && isset( $items[ $item_id ] ) ) ? $items[ $item_id ] : null; |
||
3219 | } |
||
3220 | |||
3221 | /** |
||
3222 | * Removes a specific item. |
||
3223 | * |
||
3224 | * @since 1.0.19 |
||
3225 | */ |
||
3226 | public function remove_item( $item_id ) { |
||
3227 | $items = $this->get_items(); |
||
3228 | $item_id = (int) $item_id; |
||
3229 | |||
3230 | if ( $item_id == $this->recurring_item ) { |
||
3231 | $this->recurring_item = null; |
||
3232 | } |
||
3233 | |||
3234 | if ( isset( $items[ $item_id ] ) ) { |
||
3235 | unset( $items[ $item_id ] ); |
||
3236 | $this->set_prop( 'items', $items ); |
||
3237 | } |
||
3238 | } |
||
3239 | |||
3240 | /** |
||
3241 | * Adds a fee to the invoice. |
||
3242 | * |
||
3243 | * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required. |
||
3244 | * @since 1.0.19 |
||
3245 | */ |
||
3246 | public function add_fee( $fee ) { |
||
3247 | |||
3248 | $fees = $this->get_fees(); |
||
3249 | $fees[ $fee['name'] ] = $fee; |
||
3250 | $this->set_prop( 'fees', $fees ); |
||
3251 | |||
3252 | } |
||
3253 | |||
3254 | /** |
||
3255 | * Retrieves a specific fee. |
||
3256 | * |
||
3257 | * @since 1.0.19 |
||
3258 | */ |
||
3259 | public function get_fee( $fee ) { |
||
3260 | $fees = $this->get_fees(); |
||
3261 | return isset( $fees[ $fee ] ) ? $fees[ $fee ] : null; |
||
3262 | } |
||
3263 | |||
3264 | /** |
||
3265 | * Removes a specific fee. |
||
3266 | * |
||
3267 | * @since 1.0.19 |
||
3268 | */ |
||
3269 | public function remove_fee( $fee ) { |
||
3270 | $fees = $this->get_fees(); |
||
3271 | if ( isset( $fees[ $fee ] ) ) { |
||
3272 | unset( $fees[ $fee ] ); |
||
3273 | $this->set_prop( 'fees', $fees ); |
||
3274 | } |
||
3275 | } |
||
3276 | |||
3277 | /** |
||
3278 | * Adds a discount to the invoice. |
||
3279 | * |
||
3280 | * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code. |
||
3281 | * @since 1.0.19 |
||
3282 | */ |
||
3283 | public function add_discount( $discount ) { |
||
3284 | |||
3285 | $discounts = $this->get_discounts(); |
||
3286 | $discounts[ $discount['name'] ] = $discount; |
||
3287 | $this->set_prop( 'discounts', $discounts ); |
||
3288 | |||
3289 | } |
||
3290 | |||
3291 | /** |
||
3292 | * Retrieves a specific discount. |
||
3293 | * |
||
3294 | * @since 1.0.19 |
||
3295 | * @return float |
||
3296 | */ |
||
3297 | public function get_discount( $discount = false ) { |
||
3298 | |||
3299 | // Backwards compatibilty. |
||
3300 | if ( empty( $discount ) ) { |
||
3301 | return $this->get_total_discount(); |
||
3302 | } |
||
3303 | |||
3304 | $discounts = $this->get_discounts(); |
||
3305 | return isset( $discounts[ $discount ] ) ? $discounts[ $discount ] : null; |
||
3306 | } |
||
3307 | |||
3308 | /** |
||
3309 | * Removes a specific discount. |
||
3310 | * |
||
3311 | * @since 1.0.19 |
||
3312 | */ |
||
3313 | public function remove_discount( $discount ) { |
||
3314 | $discounts = $this->get_discounts(); |
||
3315 | if ( isset( $discounts[ $discount ] ) ) { |
||
3316 | unset( $discounts[ $discount ] ); |
||
3317 | $this->set_prop( 'discounts', $discounts ); |
||
3318 | } |
||
3319 | } |
||
3320 | |||
3321 | /** |
||
3322 | * Adds a tax to the invoice. |
||
3323 | * |
||
3324 | * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required. |
||
3325 | */ |
||
3326 | public function add_tax( $tax ) { |
||
3327 | if ( $this->is_taxable() ) { |
||
3328 | |||
3329 | $taxes = $this->get_taxes(); |
||
3330 | $taxes[ $tax['name'] ] = $tax; |
||
3331 | $this->set_prop( 'taxes', $tax ); |
||
3332 | |||
3333 | } |
||
3334 | } |
||
3335 | |||
3336 | /** |
||
3337 | * Retrieves a specific tax. |
||
3338 | * |
||
3339 | * @since 1.0.19 |
||
3340 | */ |
||
3341 | public function get_tax( $tax = null ) { |
||
3342 | |||
3343 | // Backwards compatility. |
||
3344 | if ( empty( $tax ) ) { |
||
3345 | return $this->get_total_tax(); |
||
3346 | } |
||
3347 | |||
3348 | $taxes = $this->get_taxes(); |
||
3349 | return isset( $taxes[ $tax ] ) ? $taxes[ $tax ] : null; |
||
3350 | } |
||
3351 | |||
3352 | /** |
||
3353 | * Removes a specific tax. |
||
3354 | * |
||
3355 | * @since 1.0.19 |
||
3356 | */ |
||
3357 | public function remove_tax( $tax ) { |
||
3362 | } |
||
3363 | } |
||
3364 | |||
3365 | /** |
||
3366 | * Recalculates the invoice subtotal. |
||
3367 | * |
||
3368 | * @since 1.0.19 |
||
3369 | * @return float The recalculated subtotal |
||
3370 | */ |
||
3371 | public function recalculate_subtotal() { |
||
3372 | $items = $this->get_items(); |
||
3373 | $subtotal = 0; |
||
3374 | $recurring = 0; |
||
3375 | |||
3376 | foreach ( $items as $item ) { |
||
3377 | $subtotal += $item->get_sub_total(); |
||
3378 | $recurring += $item->get_recurring_sub_total(); |
||
3379 | } |
||
3380 | |||
3381 | $current = $this->is_renewal() ? $recurring : $subtotal; |
||
3382 | $this->set_subtotal( $current ); |
||
3383 | |||
3384 | $this->totals['subtotal'] = array( |
||
3385 | 'initial' => $subtotal, |
||
3386 | 'recurring' => $recurring, |
||
3387 | ); |
||
3388 | |||
3389 | return $current; |
||
3390 | } |
||
3391 | |||
3392 | /** |
||
3393 | * Recalculates the invoice discount total. |
||
3394 | * |
||
3395 | * @since 1.0.19 |
||
3396 | * @return float The recalculated discount |
||
3397 | */ |
||
3398 | public function recalculate_total_discount() { |
||
3399 | $discounts = $this->get_discounts(); |
||
3400 | $discount = 0; |
||
3401 | $recurring = 0; |
||
3402 | |||
3403 | foreach ( $discounts as $data ) { |
||
3404 | $discount += wpinv_sanitize_amount( $data['initial_discount'] ); |
||
3405 | $recurring += wpinv_sanitize_amount( $data['recurring_discount'] ); |
||
3406 | } |
||
3407 | |||
3408 | $current = $this->is_renewal() ? $recurring : $discount; |
||
3409 | |||
3410 | $this->set_total_discount( $current ); |
||
3411 | |||
3412 | $this->totals['discount'] = array( |
||
3413 | 'initial' => $discount, |
||
3414 | 'recurring' => $recurring, |
||
3415 | ); |
||
3416 | |||
3417 | return $current; |
||
3418 | |||
3419 | } |
||
3420 | |||
3421 | /** |
||
3422 | * Recalculates the invoice tax total. |
||
3423 | * |
||
3424 | * @since 1.0.19 |
||
3425 | * @return float The recalculated tax |
||
3426 | */ |
||
3427 | public function recalculate_total_tax() { |
||
3428 | $taxes = $this->get_taxes(); |
||
3429 | $tax = 0; |
||
3430 | $recurring = 0; |
||
3431 | |||
3432 | foreach ( $taxes as $data ) { |
||
3433 | $tax += wpinv_sanitize_amount( $data['initial_tax'] ); |
||
3434 | $recurring += wpinv_sanitize_amount( $data['recurring_tax'] ); |
||
3435 | } |
||
3436 | |||
3437 | $current = $this->is_renewal() ? $recurring : $tax; |
||
3438 | $this->set_total_tax( $current ); |
||
3439 | |||
3440 | $this->totals['tax'] = array( |
||
3441 | 'initial' => $tax, |
||
3442 | 'recurring' => $recurring, |
||
3443 | ); |
||
3444 | |||
3445 | return $current; |
||
3446 | |||
3447 | } |
||
3448 | |||
3449 | /** |
||
3450 | * Recalculates the invoice fees total. |
||
3451 | * |
||
3452 | * @since 1.0.19 |
||
3453 | * @return float The recalculated fee |
||
3454 | */ |
||
3455 | public function recalculate_total_fees() { |
||
3456 | $fees = $this->get_fees(); |
||
3457 | $fee = 0; |
||
3458 | $recurring = 0; |
||
3459 | |||
3460 | foreach ( $fees as $data ) { |
||
3461 | $fee += wpinv_sanitize_amount( $data['initial_fee'] ); |
||
3462 | $recurring += wpinv_sanitize_amount( $data['recurring_fee'] ); |
||
3463 | } |
||
3464 | |||
3465 | $current = $this->is_renewal() ? $recurring : $fee; |
||
3466 | $this->set_total_fees( $current ); |
||
3467 | |||
3468 | $this->totals['fee'] = array( |
||
3469 | 'initial' => $fee, |
||
3470 | 'recurring' => $recurring, |
||
3471 | ); |
||
3472 | |||
3473 | $this->set_total_fees( $fee ); |
||
3474 | return $current; |
||
3475 | } |
||
3476 | |||
3477 | /** |
||
3478 | * Recalculates the invoice total. |
||
3479 | * |
||
3480 | * @since 1.0.19 |
||
3481 | * @return float The invoice total |
||
3482 | */ |
||
3483 | public function recalculate_total() { |
||
3484 | $this->recalculate_subtotal(); |
||
3485 | $this->recalculate_total_fees(); |
||
3486 | $this->recalculate_total_discount(); |
||
3487 | $this->recalculate_total_tax(); |
||
3488 | return $this->get_total(); |
||
3489 | } |
||
3490 | |||
3491 | /** |
||
3492 | * @deprecated |
||
3493 | */ |
||
3494 | public function recalculate_totals( $temp = false ) { |
||
3495 | $this->update_items( $temp ); |
||
3496 | $this->save( true ); |
||
3497 | return $this; |
||
3498 | } |
||
3499 | |||
3500 | /** |
||
3501 | * Convert this to an array. |
||
3502 | */ |
||
3503 | public function array_convert() { |
||
3504 | return $this->get_data(); |
||
3505 | } |
||
3506 | |||
3507 | /** |
||
3508 | * Adds a note to an invoice. |
||
3509 | * |
||
3510 | * @param string $note The note being added. |
||
3511 | * @return int|false The new note's ID on success, false on failure. |
||
3512 | * |
||
3513 | */ |
||
3514 | public function add_note( $note = '', $customer_type = false, $added_by_user = false, $system = false ) { |
||
3515 | |||
3516 | // Bail if no note specified or this invoice is not yet saved. |
||
3517 | if ( ! $note || $this->get_id() == 0 || ( ! is_user_logged_in() && ! $system ) ) { |
||
3518 | return false; |
||
3519 | } |
||
3520 | |||
3521 | $author = 'System'; |
||
3522 | $author_email = '[email protected]'; |
||
3523 | |||
3524 | // If this is an admin comment or it has been added by the user. |
||
3525 | if ( is_user_logged_in() && ( ! $system || $added_by_user ) ) { |
||
3526 | $user = get_user_by( 'id', get_current_user_id() ); |
||
3527 | $author = $user->display_name; |
||
3528 | $author_email = $user->user_email; |
||
3529 | } |
||
3530 | |||
3531 | return getpaid_notes()->add_invoice_note( $this, $note, $author, $author_email, $customer_type ); |
||
3532 | |||
3533 | } |
||
3534 | |||
3535 | /** |
||
3536 | * Generates a unique key for the invoice. |
||
3537 | */ |
||
3538 | public function generate_key( $string = '' ) { |
||
3539 | $auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : ''; |
||
3540 | return strtolower( |
||
3541 | $string . md5( $this->get_id() . date( 'Y-m-d H:i:s' ) . $auth_key . uniqid( 'wpinv', true ) ) |
||
3542 | ); |
||
3543 | } |
||
3544 | |||
3545 | /** |
||
3546 | * Generates a new number for the invoice. |
||
3547 | */ |
||
3548 | public function generate_number() { |
||
3549 | $number = $this->get_id(); |
||
3550 | |||
3551 | if ( wpinv_sequential_number_active( $this->get_post_type() ) ) { |
||
3552 | $number = wpinv_get_next_invoice_number( $this->get_post_type() ); |
||
3553 | } |
||
3554 | |||
3555 | return wpinv_format_invoice_number( $number, $this->get_post_type() ); |
||
3556 | |||
3557 | } |
||
3558 | |||
3559 | /** |
||
3560 | * Handle the status transition. |
||
3561 | */ |
||
3562 | protected function status_transition() { |
||
3617 | } |
||
3618 | } |
||
3619 | } |
||
3620 | |||
3621 | /** |
||
3622 | * Updates an invoice status. |
||
3623 | */ |
||
3624 | public function update_status( $new_status = false, $note = '', $manual = false ) { |
||
3625 | |||
3626 | // Fires before updating a status. |
||
3627 | do_action( 'wpinv_before_invoice_status_change', $this->get_id(), $new_status, $this->get_status( 'edit' ) ); |
||
3628 | |||
3629 | // Update the status. |
||
3630 | $this->set_status( $new_status, $note, $manual ); |
||
3631 | |||
3632 | // Save the order. |
||
3633 | return $this->save(); |
||
3634 | |||
3635 | } |
||
3636 | |||
3637 | /** |
||
3638 | * @deprecated |
||
3639 | */ |
||
3640 | public function refresh_item_ids() { |
||
3641 | $item_ids = implode( ',', array_unique( array_keys( $this->get_items() ) ) ); |
||
3642 | update_post_meta( $this->get_id(), '_wpinv_item_ids', $item_ids ); |
||
3643 | } |
||
3644 | |||
3645 | /** |
||
3646 | * @deprecated |
||
3647 | */ |
||
3648 | public function update_items( $temp = false ) { |
||
3657 | } |
||
3658 | |||
3659 | /** |
||
3660 | * @deprecated |
||
3661 | */ |
||
3662 | public function validate_discount() { |
||
3674 | |||
3675 | } |
||
3676 | |||
3677 | /** |
||
3678 | * Refunds an invoice. |
||
3679 | */ |
||
3680 | public function refund() { |
||
3681 | $this->set_status( 'wpi-refunded' ); |
||
3682 | $this->save(); |
||
3683 | } |
||
3684 | |||
3685 | /** |
||
3686 | * Marks an invoice as paid. |
||
3687 | * |
||
3688 | * @param string $transaction_id |
||
3689 | */ |
||
3690 | public function mark_paid( $transaction_id = null, $note = '' ) { |
||
3691 | |||
3692 | // Set the transaction id. |
||
3693 | if ( empty( $transaction_id ) ) { |
||
3694 | $transaction_id = $this->generate_key('trans_'); |
||
3695 | } |
||
3696 | |||
3697 | if ( ! $this->get_transaction_id() ) { |
||
3698 | $this->set_transaction_id( $transaction_id ); |
||
3699 | } |
||
3700 | |||
3701 | if ( $this->is_paid() && 'wpi-processing' != $this->get_status() ) { |
||
3702 | return $this->save(); |
||
3703 | } |
||
3704 | |||
3705 | // Set the completed date. |
||
3706 | $this->set_date_completed( current_time( 'mysql' ) ); |
||
3707 | |||
3708 | // Set the new status. |
||
3709 | if ( $this->is_renewal() ) { |
||
3710 | |||
3711 | $_note = sprintf( |
||
3712 | __( 'Renewed via %s', 'invoicing' ), |
||
3713 | $this->get_gateway_title() . empty( $note ) ? '' : " ($note)" |
||
3714 | ); |
||
3715 | |||
3716 | if ( 'none' == $this->get_gateway() ) { |
||
3717 | $_note = $note; |
||
3718 | } |
||
3719 | |||
3720 | $this->set_status( 'wpi-renewal', $_note ); |
||
3721 | |||
3722 | } else { |
||
3723 | |||
3724 | $_note = sprintf( |
||
3725 | __( 'Paid via %s', 'invoicing' ), |
||
3726 | $this->get_gateway_title() . empty( $note ) ? '' : " ($note)" |
||
3727 | ); |
||
3728 | |||
3729 | if ( 'none' == $this->get_gateway() ) { |
||
3730 | $_note = $note; |
||
3731 | } |
||
3732 | |||
3733 | $this->set_status( 'publish',$_note ); |
||
3734 | |||
3735 | } |
||
3736 | |||
3737 | // Set checkout mode. |
||
3738 | $mode = wpinv_is_test_mode( $this->get_gateway() ) ? 'test' : 'live'; |
||
3739 | $this->set_mode( $mode ); |
||
3740 | |||
3741 | // Save the invoice. |
||
3742 | $this->save(); |
||
3743 | } |
||
3744 | |||
3745 | |||
3746 | /** |
||
3747 | * Save data to the database. |
||
3748 | * |
||
3749 | * @since 1.0.19 |
||
3750 | * @return int invoice ID |
||
3751 | */ |
||
3752 | public function save() { |
||
3759 | } |
||
3760 | |||
3761 | /** |
||
3762 | * Clears the subscription's cache. |
||
3763 | */ |
||
3764 | public function clear_cache() { |
||
3765 | wp_cache_delete( $this->get_key(), 'getpaid_invoice_keys_to_invoice_ids' ); |
||
3766 | wp_cache_delete( $this->get_number(), 'getpaid_invoice_numbers_to_invoice_ids' ); |
||
3767 | wp_cache_delete( $this->get_transaction_id(), 'getpaid_invoice_transaction_ids_to_invoice_ids' ); |
||
3768 | } |
||
3769 | |||
3770 | } |
||
3771 |