Passed
Push — master ( 17a5f2...fd79a2 )
by Brian
05:56
created

WPInv_Invoice::set_taxes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Contains the invoice class.
4
 *
5
 * @since 1.0.19
6
 * @package Invoicing
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Invoice class.
13
 */
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|array
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 = 0 ) {
139
140
        parent::__construct( $invoice );
141
142
		if ( ! empty( $invoice ) && is_numeric( $invoice ) && getpaid_is_invoice_post_type( get_post_type( (int) $invoice ) ) ) {
143
			$this->set_id( (int) $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_string( $invoice ) && $invoice_id = self::get_invoice_id_by_field( $invoice, 'key' ) ) {
156
			$this->set_id( $invoice_id );
157
		} elseif ( is_string( $invoice ) && $invoice_id = self::get_invoice_id_by_field( $invoice, 'number' ) ) {
158
			$this->set_id( $invoice_id );
159
		} elseif ( is_string( $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();
0 ignored issues
show
Bug Best Practice introduced by
The property ID does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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
	 * Returns the post type name.
596
	 *
597
	 * @since 1.0.19
598
	 * @return string
599
	 */
600
	public function get_invoice_quote_type() {
601
        return getpaid_get_post_type_label( $this->get_post_type(), false );
602
    }
603
604
    /**
605
	 * Get the invoice post type label.
606
	 *
607
	 * @since 1.0.19
608
	 * @param  string $context View or edit context.
609
	 * @return string
610
	 */
611
	public function get_label( $context = 'view' ) {
612
        return getpaid_get_post_type_label( $this->get_post_type( $context ), false );
613
	}
614
615
	/**
616
	 * Get the invoice post type.
617
	 *
618
	 * @since 1.0.19
619
	 * @param  string $context View or edit context.
620
	 * @return string
621
	 */
622
	public function get_post_type( $context = 'view' ) {
623
        return $this->get_prop( 'post_type', $context );
624
    }
625
626
    /**
627
	 * Get the invoice mode.
628
	 *
629
	 * @since 1.0.19
630
	 * @param  string $context View or edit context.
631
	 * @return string
632
	 */
633
	public function get_mode( $context = 'view' ) {
634
        return $this->get_prop( 'mode', $context );
635
    }
636
637
    /**
638
	 * Get the invoice path.
639
	 *
640
	 * @since 1.0.19
641
	 * @param  string $context View or edit context.
642
	 * @return string
643
	 */
644
	public function get_path( $context = 'view' ) {
645
        $path   = $this->get_prop( 'path', $context );
646
		$prefix = $this->get_type();
647
648
		if ( 0 !== strpos( $path, $prefix ) ) {
649
			$path = sanitize_title(  $prefix . '-' . $this->get_id()  );
650
			$this->set_path( $path );
651
		}
652
653
		return $path;
654
    }
655
656
    /**
657
	 * Get the invoice name/title.
658
	 *
659
	 * @since 1.0.19
660
	 * @param  string $context View or edit context.
661
	 * @return string
662
	 */
663
	public function get_name( $context = 'view' ) {
664
        return $this->get_prop( 'title', $context );
665
    }
666
667
    /**
668
	 * Alias of self::get_name().
669
	 *
670
	 * @since 1.0.19
671
	 * @param  string $context View or edit context.
672
	 * @return string
673
	 */
674
	public function get_title( $context = 'view' ) {
675
		return $this->get_name( $context );
676
    }
677
678
    /**
679
	 * Get the invoice description.
680
	 *
681
	 * @since 1.0.19
682
	 * @param  string $context View or edit context.
683
	 * @return string
684
	 */
685
	public function get_description( $context = 'view' ) {
686
		return $this->get_prop( 'description', $context );
687
    }
688
689
    /**
690
	 * Alias of self::get_description().
691
	 *
692
	 * @since 1.0.19
693
	 * @param  string $context View or edit context.
694
	 * @return string
695
	 */
696
	public function get_excerpt( $context = 'view' ) {
697
		return $this->get_description( $context );
698
    }
699
700
    /**
701
	 * Alias of self::get_description().
702
	 *
703
	 * @since 1.0.19
704
	 * @param  string $context View or edit context.
705
	 * @return string
706
	 */
707
	public function get_summary( $context = 'view' ) {
708
		return $this->get_description( $context );
709
    }
710
711
    /**
712
	 * Returns the user info.
713
	 *
714
	 * @since 1.0.19
715
     * @param  string $context View or edit context.
716
	 * @return array
717
	 */
718
    public function get_user_info( $context = 'view' ) {
719
720
        $user_info = array(
721
            'user_id'    => $this->get_user_id( $context ),
722
            'email'      => $this->get_email( $context ),
723
            'first_name' => $this->get_first_name( $context ),
724
            'last_name'  => $this->get_last_name( $context ),
725
            'address'    => $this->get_address( $context ),
726
            'phone'      => $this->get_phone( $context ),
727
            'city'       => $this->get_city( $context ),
728
            'country'    => $this->get_country( $context ),
729
            'state'      => $this->get_state( $context ),
730
            'zip'        => $this->get_zip( $context ),
731
            'company'    => $this->get_company( $context ),
732
            'vat_number' => $this->get_vat_number( $context ),
733
            'discount'   => $this->get_discount_code( $context ),
734
		);
735
736
		return apply_filters( 'wpinv_user_info', $user_info, $this->get_id(), $this );
737
738
    }
739
740
    /**
741
	 * Get the customer id.
742
	 *
743
	 * @since 1.0.19
744
	 * @param  string $context View or edit context.
745
	 * @return int
746
	 */
747
	public function get_author( $context = 'view' ) {
748
		return (int) $this->get_prop( 'author', $context );
749
    }
750
751
    /**
752
	 * Alias of self::get_author().
753
	 *
754
	 * @since 1.0.19
755
	 * @param  string $context View or edit context.
756
	 * @return int
757
	 */
758
	public function get_user_id( $context = 'view' ) {
759
		return $this->get_author( $context );
760
    }
761
762
     /**
763
	 * Alias of self::get_author().
764
	 *
765
	 * @since 1.0.19
766
	 * @param  string $context View or edit context.
767
	 * @return int
768
	 */
769
	public function get_customer_id( $context = 'view' ) {
770
		return $this->get_author( $context );
771
    }
772
773
    /**
774
	 * Get the customer's ip.
775
	 *
776
	 * @since 1.0.19
777
	 * @param  string $context View or edit context.
778
	 * @return string
779
	 */
780
	public function get_ip( $context = 'view' ) {
781
		return $this->get_prop( 'user_ip', $context );
782
    }
783
784
    /**
785
	 * Alias of self::get_ip().
786
	 *
787
	 * @since 1.0.19
788
	 * @param  string $context View or edit context.
789
	 * @return string
790
	 */
791
	public function get_user_ip( $context = 'view' ) {
792
		return $this->get_ip( $context );
793
    }
794
795
     /**
796
	 * Alias of self::get_ip().
797
	 *
798
	 * @since 1.0.19
799
	 * @param  string $context View or edit context.
800
	 * @return string
801
	 */
802
	public function get_customer_ip( $context = 'view' ) {
803
		return $this->get_ip( $context );
804
    }
805
806
    /**
807
	 * Get the customer's first name.
808
	 *
809
	 * @since 1.0.19
810
	 * @param  string $context View or edit context.
811
	 * @return string
812
	 */
813
	public function get_first_name( $context = 'view' ) {
814
		return $this->get_prop( 'first_name', $context );
815
    }
816
817
    /**
818
	 * Alias of self::get_first_name().
819
	 *
820
	 * @since 1.0.19
821
	 * @param  string $context View or edit context.
822
	 * @return string
823
	 */
824
	public function get_user_first_name( $context = 'view' ) {
825
		return $this->get_first_name( $context );
826
    }
827
828
     /**
829
	 * Alias of self::get_first_name().
830
	 *
831
	 * @since 1.0.19
832
	 * @param  string $context View or edit context.
833
	 * @return string
834
	 */
835
	public function get_customer_first_name( $context = 'view' ) {
836
		return $this->get_first_name( $context );
837
    }
838
839
    /**
840
	 * Get the customer's last name.
841
	 *
842
	 * @since 1.0.19
843
	 * @param  string $context View or edit context.
844
	 * @return string
845
	 */
846
	public function get_last_name( $context = 'view' ) {
847
		return $this->get_prop( 'last_name', $context );
848
    }
849
850
    /**
851
	 * Alias of self::get_last_name().
852
	 *
853
	 * @since 1.0.19
854
	 * @param  string $context View or edit context.
855
	 * @return string
856
	 */
857
	public function get_user_last_name( $context = 'view' ) {
858
		return $this->get_last_name( $context );
859
    }
860
861
    /**
862
	 * Alias of self::get_last_name().
863
	 *
864
	 * @since 1.0.19
865
	 * @param  string $context View or edit context.
866
	 * @return string
867
	 */
868
	public function get_customer_last_name( $context = 'view' ) {
869
		return $this->get_last_name( $context );
870
    }
871
872
    /**
873
	 * Get the customer's full name.
874
	 *
875
	 * @since 1.0.19
876
	 * @param  string $context View or edit context.
877
	 * @return string
878
	 */
879
	public function get_full_name( $context = 'view' ) {
880
		return trim( $this->get_first_name( $context ) . ' ' . $this->get_last_name( $context ) );
881
    }
882
883
    /**
884
	 * Alias of self::get_full_name().
885
	 *
886
	 * @since 1.0.19
887
	 * @param  string $context View or edit context.
888
	 * @return string
889
	 */
890
	public function get_user_full_name( $context = 'view' ) {
891
		return $this->get_full_name( $context );
892
    }
893
894
    /**
895
	 * Alias of self::get_full_name().
896
	 *
897
	 * @since 1.0.19
898
	 * @param  string $context View or edit context.
899
	 * @return string
900
	 */
901
	public function get_customer_full_name( $context = 'view' ) {
902
		return $this->get_full_name( $context );
903
    }
904
905
    /**
906
	 * Get the customer's phone number.
907
	 *
908
	 * @since 1.0.19
909
	 * @param  string $context View or edit context.
910
	 * @return string
911
	 */
912
	public function get_phone( $context = 'view' ) {
913
		return $this->get_prop( 'phone', $context );
914
    }
915
916
    /**
917
	 * Alias of self::get_phone().
918
	 *
919
	 * @since 1.0.19
920
	 * @param  string $context View or edit context.
921
	 * @return string
922
	 */
923
	public function get_phone_number( $context = 'view' ) {
924
		return $this->get_phone( $context );
925
    }
926
927
    /**
928
	 * Alias of self::get_phone().
929
	 *
930
	 * @since 1.0.19
931
	 * @param  string $context View or edit context.
932
	 * @return string
933
	 */
934
	public function get_user_phone( $context = 'view' ) {
935
		return $this->get_phone( $context );
936
    }
937
938
    /**
939
	 * Alias of self::get_phone().
940
	 *
941
	 * @since 1.0.19
942
	 * @param  string $context View or edit context.
943
	 * @return string
944
	 */
945
	public function get_customer_phone( $context = 'view' ) {
946
		return $this->get_phone( $context );
947
    }
948
949
    /**
950
	 * Get the customer's email address.
951
	 *
952
	 * @since 1.0.19
953
	 * @param  string $context View or edit context.
954
	 * @return string
955
	 */
956
	public function get_email( $context = 'view' ) {
957
		return $this->get_prop( 'email', $context );
958
    }
959
960
    /**
961
	 * Alias of self::get_email().
962
	 *
963
	 * @since 1.0.19
964
	 * @param  string $context View or edit context.
965
	 * @return string
966
	 */
967
	public function get_email_address( $context = 'view' ) {
968
		return $this->get_email( $context );
969
    }
970
971
    /**
972
	 * Alias of self::get_email().
973
	 *
974
	 * @since 1.0.19
975
	 * @param  string $context View or edit context.
976
	 * @return string
977
	 */
978
	public function get_user_email( $context = 'view' ) {
979
		return $this->get_email( $context );
980
    }
981
982
    /**
983
	 * Alias of self::get_email().
984
	 *
985
	 * @since 1.0.19
986
	 * @param  string $context View or edit context.
987
	 * @return string
988
	 */
989
	public function get_customer_email( $context = 'view' ) {
990
		return $this->get_email( $context );
991
    }
992
993
    /**
994
	 * Get the customer's country.
995
	 *
996
	 * @since 1.0.19
997
	 * @param  string $context View or edit context.
998
	 * @return string
999
	 */
1000
	public function get_country( $context = 'view' ) {
1001
		$country = $this->get_prop( 'country', $context );
1002
		return empty( $country ) ? wpinv_get_default_country() : $country;
1003
    }
1004
1005
    /**
1006
	 * Alias of self::get_country().
1007
	 *
1008
	 * @since 1.0.19
1009
	 * @param  string $context View or edit context.
1010
	 * @return string
1011
	 */
1012
	public function get_user_country( $context = 'view' ) {
1013
		return $this->get_country( $context );
1014
    }
1015
1016
    /**
1017
	 * Alias of self::get_country().
1018
	 *
1019
	 * @since 1.0.19
1020
	 * @param  string $context View or edit context.
1021
	 * @return string
1022
	 */
1023
	public function get_customer_country( $context = 'view' ) {
1024
		return $this->get_country( $context );
1025
    }
1026
1027
    /**
1028
	 * Get the customer's state.
1029
	 *
1030
	 * @since 1.0.19
1031
	 * @param  string $context View or edit context.
1032
	 * @return string
1033
	 */
1034
	public function get_state( $context = 'view' ) {
1035
		$state = $this->get_prop( 'state', $context );
1036
		return empty( $state ) ? wpinv_get_default_state() : $state;
1037
    }
1038
1039
    /**
1040
	 * Alias of self::get_state().
1041
	 *
1042
	 * @since 1.0.19
1043
	 * @param  string $context View or edit context.
1044
	 * @return string
1045
	 */
1046
	public function get_user_state( $context = 'view' ) {
1047
		return $this->get_state( $context );
1048
    }
1049
1050
    /**
1051
	 * Alias of self::get_state().
1052
	 *
1053
	 * @since 1.0.19
1054
	 * @param  string $context View or edit context.
1055
	 * @return string
1056
	 */
1057
	public function get_customer_state( $context = 'view' ) {
1058
		return $this->get_state( $context );
1059
    }
1060
1061
    /**
1062
	 * Get the customer's city.
1063
	 *
1064
	 * @since 1.0.19
1065
	 * @param  string $context View or edit context.
1066
	 * @return string
1067
	 */
1068
	public function get_city( $context = 'view' ) {
1069
		return $this->get_prop( 'city', $context );
1070
    }
1071
1072
    /**
1073
	 * Alias of self::get_city().
1074
	 *
1075
	 * @since 1.0.19
1076
	 * @param  string $context View or edit context.
1077
	 * @return string
1078
	 */
1079
	public function get_user_city( $context = 'view' ) {
1080
		return $this->get_city( $context );
1081
    }
1082
1083
    /**
1084
	 * Alias of self::get_city().
1085
	 *
1086
	 * @since 1.0.19
1087
	 * @param  string $context View or edit context.
1088
	 * @return string
1089
	 */
1090
	public function get_customer_city( $context = 'view' ) {
1091
		return $this->get_city( $context );
1092
    }
1093
1094
    /**
1095
	 * Get the customer's zip.
1096
	 *
1097
	 * @since 1.0.19
1098
	 * @param  string $context View or edit context.
1099
	 * @return string
1100
	 */
1101
	public function get_zip( $context = 'view' ) {
1102
		return $this->get_prop( 'zip', $context );
1103
    }
1104
1105
    /**
1106
	 * Alias of self::get_zip().
1107
	 *
1108
	 * @since 1.0.19
1109
	 * @param  string $context View or edit context.
1110
	 * @return string
1111
	 */
1112
	public function get_user_zip( $context = 'view' ) {
1113
		return $this->get_zip( $context );
1114
    }
1115
1116
    /**
1117
	 * Alias of self::get_zip().
1118
	 *
1119
	 * @since 1.0.19
1120
	 * @param  string $context View or edit context.
1121
	 * @return string
1122
	 */
1123
	public function get_customer_zip( $context = 'view' ) {
1124
		return $this->get_zip( $context );
1125
    }
1126
1127
    /**
1128
	 * Get the customer's company.
1129
	 *
1130
	 * @since 1.0.19
1131
	 * @param  string $context View or edit context.
1132
	 * @return string
1133
	 */
1134
	public function get_company( $context = 'view' ) {
1135
		return $this->get_prop( 'company', $context );
1136
    }
1137
1138
    /**
1139
	 * Alias of self::get_company().
1140
	 *
1141
	 * @since 1.0.19
1142
	 * @param  string $context View or edit context.
1143
	 * @return string
1144
	 */
1145
	public function get_user_company( $context = 'view' ) {
1146
		return $this->get_company( $context );
1147
    }
1148
1149
    /**
1150
	 * Alias of self::get_company().
1151
	 *
1152
	 * @since 1.0.19
1153
	 * @param  string $context View or edit context.
1154
	 * @return string
1155
	 */
1156
	public function get_customer_company( $context = 'view' ) {
1157
		return $this->get_company( $context );
1158
    }
1159
1160
    /**
1161
	 * Get the customer's vat number.
1162
	 *
1163
	 * @since 1.0.19
1164
	 * @param  string $context View or edit context.
1165
	 * @return string
1166
	 */
1167
	public function get_vat_number( $context = 'view' ) {
1168
		return $this->get_prop( 'vat_number', $context );
1169
    }
1170
1171
    /**
1172
	 * Alias of self::get_vat_number().
1173
	 *
1174
	 * @since 1.0.19
1175
	 * @param  string $context View or edit context.
1176
	 * @return string
1177
	 */
1178
	public function get_user_vat_number( $context = 'view' ) {
1179
		return $this->get_vat_number( $context );
1180
    }
1181
1182
    /**
1183
	 * Alias of self::get_vat_number().
1184
	 *
1185
	 * @since 1.0.19
1186
	 * @param  string $context View or edit context.
1187
	 * @return string
1188
	 */
1189
	public function get_customer_vat_number( $context = 'view' ) {
1190
		return $this->get_vat_number( $context );
1191
    }
1192
1193
    /**
1194
	 * Get the customer's vat rate.
1195
	 *
1196
	 * @since 1.0.19
1197
	 * @param  string $context View or edit context.
1198
	 * @return string
1199
	 */
1200
	public function get_vat_rate( $context = 'view' ) {
1201
		return $this->get_prop( 'vat_rate', $context );
1202
    }
1203
1204
    /**
1205
	 * Alias of self::get_vat_rate().
1206
	 *
1207
	 * @since 1.0.19
1208
	 * @param  string $context View or edit context.
1209
	 * @return string
1210
	 */
1211
	public function get_user_vat_rate( $context = 'view' ) {
1212
		return $this->get_vat_rate( $context );
1213
    }
1214
1215
    /**
1216
	 * Alias of self::get_vat_rate().
1217
	 *
1218
	 * @since 1.0.19
1219
	 * @param  string $context View or edit context.
1220
	 * @return string
1221
	 */
1222
	public function get_customer_vat_rate( $context = 'view' ) {
1223
		return $this->get_vat_rate( $context );
1224
    }
1225
1226
    /**
1227
	 * Get the customer's address.
1228
	 *
1229
	 * @since 1.0.19
1230
	 * @param  string $context View or edit context.
1231
	 * @return string
1232
	 */
1233
	public function get_address( $context = 'view' ) {
1234
		return $this->get_prop( 'address', $context );
1235
    }
1236
1237
    /**
1238
	 * Alias of self::get_address().
1239
	 *
1240
	 * @since 1.0.19
1241
	 * @param  string $context View or edit context.
1242
	 * @return string
1243
	 */
1244
	public function get_user_address( $context = 'view' ) {
1245
		return $this->get_address( $context );
1246
    }
1247
1248
    /**
1249
	 * Alias of self::get_address().
1250
	 *
1251
	 * @since 1.0.19
1252
	 * @param  string $context View or edit context.
1253
	 * @return string
1254
	 */
1255
	public function get_customer_address( $context = 'view' ) {
1256
		return $this->get_address( $context );
1257
    }
1258
1259
    /**
1260
	 * Get whether the customer has viewed the invoice or not.
1261
	 *
1262
	 * @since 1.0.19
1263
	 * @param  string $context View or edit context.
1264
	 * @return bool
1265
	 */
1266
	public function get_is_viewed( $context = 'view' ) {
1267
		return (bool) $this->get_prop( 'is_viewed', $context );
1268
	}
1269
1270
	/**
1271
	 * Get other recipients for invoice communications.
1272
	 *
1273
	 * @since 1.0.19
1274
	 * @param  string $context View or edit context.
1275
	 * @return bool
1276
	 */
1277
	public function get_email_cc( $context = 'view' ) {
1278
		return $this->get_prop( 'email_cc', $context );
1279
	}
1280
1281
	/**
1282
	 * Get invoice template.
1283
	 *
1284
	 * @since 1.0.19
1285
	 * @param  string $context View or edit context.
1286
	 * @return bool
1287
	 */
1288
	public function get_template( $context = 'view' ) {
1289
		return $this->get_prop( 'template', $context );
1290
	}
1291
1292
	/**
1293
	 * Get invoice source.
1294
	 *
1295
	 * @since 1.0.19
1296
	 * @param  string $context View or edit context.
1297
	 * @return bool
1298
	 */
1299
	public function get_created_via( $context = 'view' ) {
1300
		return $this->get_prop( 'created_via', $context );
1301
	}
1302
1303
	/**
1304
	 * Get whether the customer has confirmed their address.
1305
	 *
1306
	 * @since 1.0.19
1307
	 * @param  string $context View or edit context.
1308
	 * @return bool
1309
	 */
1310
	public function get_address_confirmed( $context = 'view' ) {
1311
		return (bool) $this->get_prop( 'address_confirmed', $context );
1312
    }
1313
1314
    /**
1315
	 * Alias of self::get_address_confirmed().
1316
	 *
1317
	 * @since 1.0.19
1318
	 * @param  string $context View or edit context.
1319
	 * @return bool
1320
	 */
1321
	public function get_user_address_confirmed( $context = 'view' ) {
1322
		return $this->get_address_confirmed( $context );
1323
    }
1324
1325
    /**
1326
	 * Alias of self::get_address().
1327
	 *
1328
	 * @since 1.0.19
1329
	 * @param  string $context View or edit context.
1330
	 * @return bool
1331
	 */
1332
	public function get_customer_address_confirmed( $context = 'view' ) {
1333
		return $this->get_address_confirmed( $context );
1334
    }
1335
1336
    /**
1337
	 * Get the invoice subtotal.
1338
	 *
1339
	 * @since 1.0.19
1340
	 * @param  string $context View or edit context.
1341
	 * @return float
1342
	 */
1343
	public function get_subtotal( $context = 'view' ) {
1344
        $subtotal = (float) $this->get_prop( 'subtotal', $context );
1345
1346
        // Backwards compatibility.
1347
        if ( is_bool( $context ) && $context ) {
0 ignored issues
show
introduced by
The condition is_bool($context) is always false.
Loading history...
1348
            return wpinv_price( wpinv_format_amount( $subtotal ), $this->get_currency() );
1349
        }
1350
1351
        return $subtotal;
1352
    }
1353
1354
    /**
1355
	 * Get the invoice discount total.
1356
	 *
1357
	 * @since 1.0.19
1358
	 * @param  string $context View or edit context.
1359
	 * @return float
1360
	 */
1361
	public function get_total_discount( $context = 'view' ) {
1362
		return wpinv_round_amount( wpinv_sanitize_amount( $this->get_prop( 'total_discount', $context ) ) );
1363
    }
1364
1365
    /**
1366
	 * Get the invoice tax total.
1367
	 *
1368
	 * @since 1.0.19
1369
	 * @param  string $context View or edit context.
1370
	 * @return float
1371
	 */
1372
	public function get_total_tax( $context = 'view' ) {
1373
		return wpinv_round_amount( wpinv_sanitize_amount( $this->get_prop( 'total_tax', $context ) ) );
1374
	}
1375
1376
	/**
1377
	 * @deprecated
1378
	 */
1379
	public function get_final_tax( $currency = false ) {
1380
		$tax = $this->get_total_tax();
1381
1382
        if ( $currency ) {
1383
			return wpinv_price( $tax, $this->get_currency() );
1384
        }
1385
1386
        return $tax;
1387
    }
1388
1389
    /**
1390
	 * Get the invoice fees total.
1391
	 *
1392
	 * @since 1.0.19
1393
	 * @param  string $context View or edit context.
1394
	 * @return float
1395
	 */
1396
	public function get_total_fees( $context = 'view' ) {
1397
		return wpinv_round_amount( wpinv_sanitize_amount( $this->get_prop( 'total_fees', $context ) ) );
1398
    }
1399
1400
    /**
1401
	 * Alias for self::get_total_fees().
1402
	 *
1403
	 * @since 1.0.19
1404
	 * @param  string $context View or edit context.
1405
	 * @return float
1406
	 */
1407
	public function get_fees_total( $context = 'view' ) {
1408
		return $this->get_total_fees( $context );
1409
    }
1410
1411
    /**
1412
	 * Get the invoice total.
1413
	 *
1414
	 * @since 1.0.19
1415
     * @return float
1416
	 */
1417
	public function get_total() {
1418
		$total = $this->is_renewal() ? $this->get_recurring_total() : $this->get_initial_total();
1419
		return apply_filters( 'getpaid_get_invoice_total_amount', $total, $this  );
1420
	}
1421
	
1422
	/**
1423
	 * Get the invoice totals.
1424
	 *
1425
	 * @since 1.0.19
1426
     * @return array
1427
	 */
1428
	public function get_totals() {
1429
		return $this->totals;
1430
    }
1431
1432
    /**
1433
	 * Get the initial invoice total.
1434
	 *
1435
	 * @since 1.0.19
1436
     * @param  string $context View or edit context.
1437
     * @return float
1438
	 */
1439
    public function get_initial_total() {
1440
1441
		if ( empty( $this->totals ) ) {
1442
			$this->recalculate_total();
1443
		}
1444
1445
		$tax      = $this->totals['tax']['initial'];
1446
		$fee      = $this->totals['fee']['initial'];
1447
		$discount = $this->totals['discount']['initial'];
1448
		$subtotal = $this->totals['subtotal']['initial'];
1449
		$total    = $tax + $fee - $discount + $subtotal;
1450
1451
		if ( 0 > $total ) {
1452
			$total = 0;
1453
		}
1454
1455
		$total = wpinv_round_amount( wpinv_sanitize_amount( $total ) );
1456
        return apply_filters( 'wpinv_get_initial_invoice_total', $total, $this );
1457
	}
1458
1459
	/**
1460
	 * Get the recurring invoice total.
1461
	 *
1462
	 * @since 1.0.19
1463
     * @param  string $context View or edit context.
1464
     * @return float
1465
	 */
1466
    public function get_recurring_total() {
1467
1468
		if ( empty( $this->totals ) ) {
1469
			$this->recalculate_total();
1470
		}
1471
1472
		$tax      = $this->totals['tax']['recurring'];
1473
		$fee      = $this->totals['fee']['recurring'];
1474
		$discount = $this->totals['discount']['recurring'];
1475
		$subtotal = $this->totals['subtotal']['recurring'];
1476
		$total    = $tax + $fee - $discount + $subtotal;
1477
1478
		if ( 0 > $total ) {
1479
			$total = 0;
1480
		}
1481
1482
		$total = wpinv_round_amount( wpinv_sanitize_amount( $total ) );
1483
        return apply_filters( 'wpinv_get_recurring_invoice_total', $total, $this );
1484
	}
1485
1486
	/**
1487
	 * Returns recurring payment details.
1488
	 *
1489
	 * @since 1.0.19
1490
     * @param  string $field Optionally provide a field to return.
1491
	 * @param string $currency Whether to include the currency.
1492
     * @return float|string
1493
	 */
1494
    public function get_recurring_details( $field = '', $currency = false ) {
1495
1496
		// Maybe recalculate totals.
1497
		if ( empty( $this->totals ) ) {
1498
			$this->recalculate_total();
1499
		}
1500
1501
		// Prepare recurring totals.
1502
        $data = apply_filters(
1503
			'wpinv_get_invoice_recurring_details',
1504
			array(
1505
				'cart_details' => $this->get_cart_details(),
1506
				'subtotal'     => $this->totals['subtotal']['recurring'],
1507
				'discount'     => $this->totals['discount']['recurring'],
1508
				'tax'          => $this->totals['tax']['recurring'],
1509
				'fee'          => $this->totals['fee']['recurring'],
1510
				'total'        => $this->get_recurring_total(),
1511
			),
1512
			$this,
1513
			$field,
1514
			$currency
1515
		);
1516
1517
        if ( isset( $data[$field] ) ) {
1518
            return ( $currency ? wpinv_price( $data[$field], $this->get_currency() ) : $data[$field] );
1519
        }
1520
1521
        return $data;
1522
    }
1523
1524
    /**
1525
	 * Get the invoice fees.
1526
	 *
1527
	 * @since 1.0.19
1528
	 * @param  string $context View or edit context.
1529
	 * @return array
1530
	 */
1531
	public function get_fees( $context = 'view' ) {
1532
		return wpinv_parse_list( $this->get_prop( 'fees', $context ) );
1533
    }
1534
1535
    /**
1536
	 * Get the invoice discounts.
1537
	 *
1538
	 * @since 1.0.19
1539
	 * @param  string $context View or edit context.
1540
	 * @return array
1541
	 */
1542
	public function get_discounts( $context = 'view' ) {
1543
		return wpinv_parse_list( $this->get_prop( 'discounts', $context ) );
1544
    }
1545
1546
    /**
1547
	 * Get the invoice taxes.
1548
	 *
1549
	 * @since 1.0.19
1550
	 * @param  string $context View or edit context.
1551
	 * @return array
1552
	 */
1553
	public function get_taxes( $context = 'view' ) {
1554
		return wpinv_parse_list( $this->get_prop( 'taxes', $context ) );
1555
    }
1556
1557
    /**
1558
	 * Get the invoice items.
1559
	 *
1560
	 * @since 1.0.19
1561
	 * @param  string $context View or edit context.
1562
	 * @return GetPaid_Form_Item[]
1563
	 */
1564
	public function get_items( $context = 'view' ) {
1565
        return $this->get_prop( 'items', $context );
1566
	}
1567
	
1568
	/**
1569
	 * Get the invoice item ids.
1570
	 *
1571
	 * @since 1.0.19
1572
	 * @return string
1573
	 */
1574
	public function get_item_ids() {
1575
		return implode( ', ', wp_list_pluck( $this->get_cart_details(), 'item_id' ) );
1576
    }
1577
1578
    /**
1579
	 * Get the invoice's payment form.
1580
	 *
1581
	 * @since 1.0.19
1582
	 * @param  string $context View or edit context.
1583
	 * @return int
1584
	 */
1585
	public function get_payment_form( $context = 'view' ) {
1586
		return intval( $this->get_prop( 'payment_form', $context ) );
1587
    }
1588
1589
    /**
1590
	 * Get the invoice's submission id.
1591
	 *
1592
	 * @since 1.0.19
1593
	 * @param  string $context View or edit context.
1594
	 * @return string
1595
	 */
1596
	public function get_submission_id( $context = 'view' ) {
1597
		return $this->get_prop( 'submission_id', $context );
1598
    }
1599
1600
    /**
1601
	 * Get the invoice's discount code.
1602
	 *
1603
	 * @since 1.0.19
1604
	 * @param  string $context View or edit context.
1605
	 * @return string
1606
	 */
1607
	public function get_discount_code( $context = 'view' ) {
1608
		return $this->get_prop( 'discount_code', $context );
1609
    }
1610
1611
    /**
1612
	 * Get the invoice's gateway.
1613
	 *
1614
	 * @since 1.0.19
1615
	 * @param  string $context View or edit context.
1616
	 * @return string
1617
	 */
1618
	public function get_gateway( $context = 'view' ) {
1619
		return $this->get_prop( 'gateway', $context );
1620
    }
1621
1622
    /**
1623
	 * Get the invoice's gateway display title.
1624
	 *
1625
	 * @since 1.0.19
1626
	 * @return string
1627
	 */
1628
    public function get_gateway_title() {
1629
        $title =  wpinv_get_gateway_checkout_label( $this->get_gateway() );
1630
        return apply_filters( 'wpinv_gateway_title', $title, $this->get_id(), $this );
1631
    }
1632
1633
    /**
1634
	 * Get the invoice's transaction id.
1635
	 *
1636
	 * @since 1.0.19
1637
	 * @param  string $context View or edit context.
1638
	 * @return string
1639
	 */
1640
	public function get_transaction_id( $context = 'view' ) {
1641
		return $this->get_prop( 'transaction_id', $context );
1642
    }
1643
1644
    /**
1645
	 * Get the invoice's currency.
1646
	 *
1647
	 * @since 1.0.19
1648
	 * @param  string $context View or edit context.
1649
	 * @return string
1650
	 */
1651
	public function get_currency( $context = 'view' ) {
1652
        $currency = $this->get_prop( 'currency', $context );
1653
        return empty( $currency ) ? wpinv_get_currency() : $currency;
1654
    }
1655
1656
    /**
1657
	 * Checks if we are charging taxes for this invoice.
1658
	 *
1659
	 * @since 1.0.19
1660
	 * @param  string $context View or edit context.
1661
	 * @return bool
1662
	 */
1663
	public function get_disable_taxes( $context = 'view' ) {
1664
        return (bool) $this->get_prop( 'disable_taxes', $context );
1665
    }
1666
1667
    /**
1668
	 * Retrieves the subscription id for an invoice.
1669
	 *
1670
	 * @since 1.0.19
1671
	 * @param  string $context View or edit context.
1672
	 * @return int
1673
	 */
1674
    public function get_subscription_id( $context = 'view' ) {
1675
		return $this->is_renewal() ? $this->get_parent()->get_subscription_id( $context ) : $this->get_prop( 'subscription_id', $context );
1676
	}
1677
1678
	/**
1679
	 * Retrieves the remote subscription id for an invoice.
1680
	 *
1681
	 * @since 1.0.19
1682
	 * @param  string $context View or edit context.
1683
	 * @return int
1684
	 */
1685
    public function get_remote_subscription_id( $context = 'view' ) {
1686
        $subscription_id = $this->get_prop( 'remote_subscription_id', $context );
1687
1688
        if ( empty( $subscription_id ) && $this->is_renewal() ) {
1689
            $parent = $this->get_parent();
1690
            return $parent->get_subscription_id( $context );
1691
        }
1692
1693
        return $subscription_id;
1694
    }
1695
1696
    /**
1697
	 * Retrieves the payment meta for an invoice.
1698
	 *
1699
	 * @since 1.0.19
1700
	 * @param  string $context View or edit context.
1701
	 * @return array
1702
	 */
1703
    public function get_payment_meta( $context = 'view' ) {
1704
1705
        return array(
1706
            'price'        => $this->get_total( $context ),
0 ignored issues
show
Unused Code introduced by
The call to WPInv_Invoice::get_total() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1706
            'price'        => $this->/** @scrutinizer ignore-call */ get_total( $context ),

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1707
            'date'         => $this->get_date_created( $context ),
1708
            'user_email'   => $this->get_email( $context ),
1709
            'invoice_key'  => $this->get_key( $context ),
1710
            'currency'     => $this->get_currency( $context ),
1711
            'items'        => $this->get_items( $context ),
1712
            'user_info'    => $this->get_user_info( $context ),
1713
            'cart_details' => $this->get_cart_details(),
1714
            'status'       => $this->get_status( $context ),
1715
            'fees'         => $this->get_fees( $context ),
1716
            'taxes'        => $this->get_taxes( $context ),
1717
        );
1718
1719
    }
1720
1721
    /**
1722
	 * Retrieves the cart details for an invoice.
1723
	 *
1724
	 * @since 1.0.19
1725
	 * @return array
1726
	 */
1727
    public function get_cart_details() {
1728
        $items        = $this->get_items();
1729
        $cart_details = array();
1730
1731
        foreach ( $items as $item ) {
1732
			$item->invoice_id = $this->get_id();
1733
            $cart_details[]   = $item->prepare_data_for_saving();
1734
        }
1735
1736
        return $cart_details;
1737
	}
1738
1739
	/**
1740
	 * Retrieves the recurring item.
1741
	 *
1742
	 * @return null|GetPaid_Form_Item|int
1743
	 */
1744
	public function get_recurring( $object = false ) {
1745
1746
		// Are we returning an object?
1747
        if ( $object ) {
1748
            return $this->get_item( $this->recurring_item );
1749
        }
1750
1751
        return $this->recurring_item;
1752
    }
1753
1754
	/**
1755
	 * Retrieves the subscription name.
1756
	 *
1757
	 * @since 1.0.19
1758
	 * @return string
1759
	 */
1760
	public function get_subscription_name() {
1761
1762
		// Retrieve the recurring name
1763
        $item = $this->get_recurring( true );
1764
1765
		// Abort if it does not exist.
1766
        if ( empty( $item ) ) {
1767
            return '';
1768
        }
1769
1770
		// Return the item name.
1771
        return apply_filters( 'wpinv_invoice_get_subscription_name', $item->get_name(), $this );
1772
	}
1773
1774
	/**
1775
	 * Retrieves the view url.
1776
	 *
1777
	 * @since 1.0.19
1778
	 * @return string
1779
	 */
1780
	public function get_view_url() {
1781
        $invoice_url = get_permalink( $this->get_id() );
1782
		$invoice_url = add_query_arg( 'invoice_key', $this->get_key(), $invoice_url );
1783
        return apply_filters( 'wpinv_get_view_url', $invoice_url, $this );
1784
	}
1785
1786
	/**
1787
	 * Retrieves the payment url.
1788
	 *
1789
	 * @since 1.0.19
1790
	 * @return string
1791
	 */
1792
	public function get_checkout_payment_url( $deprecated = false, $secret = false ) {
1793
1794
		// Retrieve the checkout url.
1795
        $pay_url = wpinv_get_checkout_uri();
1796
1797
		// Maybe force ssl.
1798
        if ( is_ssl() ) {
1799
            $pay_url = str_replace( 'http:', 'https:', $pay_url );
1800
        }
1801
1802
		// Add the invoice key.
1803
		$pay_url = add_query_arg( 'invoice_key', $this->get_key(), $pay_url );
1804
1805
		// (Maybe?) add a secret
1806
        if ( $secret ) {
1807
            $pay_url = add_query_arg( array( '_wpipay' => md5( $this->get_user_id() . '::' . $this->get_email() . '::' . $this->get_key() ) ), $pay_url );
1808
        }
1809
1810
        return apply_filters( 'wpinv_get_checkout_payment_url', $pay_url, $this, $deprecated, $secret );
1811
	}
1812
	
1813
	/**
1814
	 * Retrieves the receipt url.
1815
	 *
1816
	 * @since 1.0.19
1817
	 * @return string
1818
	 */
1819
	public function get_receipt_url() {
1820
1821
		// Retrieve the checkout url.
1822
        $receipt_url = wpinv_get_success_page_uri();
1823
1824
		// Maybe force ssl.
1825
        if ( is_ssl() ) {
1826
            $receipt_url = str_replace( 'http:', 'https:', $receipt_url );
1827
        }
1828
1829
		// Add the invoice key.
1830
		$receipt_url = add_query_arg( 'invoice_key', $this->get_key(), $receipt_url );
1831
1832
        return apply_filters( 'getpaid_get_invoice_receipt_url', $receipt_url, $this );
1833
	}
1834
	
1835
	/**
1836
	 * Retrieves the default status.
1837
	 *
1838
	 * @since 1.0.19
1839
	 * @return string
1840
	 */
1841
	public function get_default_status() {
1842
1843
		$type   = $this->get_type();
1844
		$status = "wpi-$type-pending";
1845
		return str_replace( '-invoice', '', $status );
1846
1847
	}
1848
1849
    /**
1850
	 * Magic method for accessing invoice properties.
1851
	 *
1852
	 * @since 1.0.15
1853
	 * @access public
1854
	 *
1855
	 * @param string $key Discount data to retrieve
1856
	 * @param  string $context View or edit context.
1857
	 * @return mixed Value of the given invoice property (if set).
1858
	 */
1859
	public function get( $key, $context = 'view' ) {
1860
        return $this->get_prop( $key, $context );
1861
	}
1862
1863
    /*
1864
	|--------------------------------------------------------------------------
1865
	| Setters
1866
	|--------------------------------------------------------------------------
1867
	|
1868
	| Functions for setting item data. These should not update anything in the
1869
	| database itself and should only change what is stored in the class
1870
	| object.
1871
    */
1872
1873
    /**
1874
	 * Magic method for setting invoice properties.
1875
	 *
1876
	 * @since 1.0.19
1877
	 * @access public
1878
	 *
1879
	 * @param string $key Discount data to retrieve
1880
	 * @param  mixed $value new value.
1881
	 * @return mixed Value of the given invoice property (if set).
1882
	 */
1883
	public function set( $key, $value ) {
1884
1885
        $setter = "set_$key";
1886
        if ( is_callable( array( $this, $setter ) ) ) {
1887
            $this->{$setter}( $value );
1888
        }
1889
1890
	}
1891
1892
	/**
1893
	 * Sets item status.
1894
	 *
1895
	 * @since 1.0.19
1896
	 * @param string $new_status    New status.
1897
	 * @param string $note          Optional note to add.
1898
	 * @param bool   $manual_update Is this a manual status change?.
1899
	 * @return array details of change.
1900
	 */
1901
	public function set_status( $new_status, $note = '', $manual_update = false ) {
1902
		$old_status = $this->get_status();
1903
1904
		$statuses = $this->get_all_statuses();
1905
1906
		if ( isset( $statuses[ 'draft' ] ) ) {
1907
			unset( $statuses[ 'draft' ] );
1908
		}
1909
1910
		$this->set_prop( 'status', $new_status );
1911
1912
		// If setting the status, ensure it's set to a valid status.
1913
		if ( true === $this->object_read ) {
1914
1915
			// Only allow valid new status.
1916
			if ( ! array_key_exists( $new_status, $statuses ) ) {
1917
				$new_status = $this->get_default_status();
1918
			}
1919
1920
			// If the old status is set but unknown (e.g. draft) assume its pending for action usage.
1921
			if ( $old_status && ! array_key_exists( $new_status, $statuses ) ) {
1922
				$old_status = $this->get_default_status();
1923
			}
1924
1925
			// Paid - Renewal (i.e when duplicating a parent invoice )
1926
			if ( $new_status == 'wpi-pending' && $old_status == 'publish' && ! $this->get_id() ) {
1927
				$old_status = 'wpi-pending';
1928
			}
1929
1930
		}
1931
1932
		if ( true === $this->object_read && $old_status !== $new_status ) {
1933
			$this->status_transition = array(
1934
				'from'   => ! empty( $this->status_transition['from'] ) ? $this->status_transition['from'] : $old_status,
1935
				'to'     => $new_status,
1936
				'note'   => $note,
1937
				'manual' => (bool) $manual_update,
1938
			);
1939
1940
			if ( $manual_update ) {
1941
				do_action( 'getpaid_' . $this->object_type .'_edit_status', $this->get_id(), $new_status );
1942
			}
1943
1944
			$this->maybe_set_date_paid();
1945
1946
		}
1947
1948
		return array(
1949
			'from' => $old_status,
1950
			'to'   => $new_status,
1951
		);
1952
	}
1953
1954
	/**
1955
	 * Maybe set date paid.
1956
	 *
1957
	 * Sets the date paid variable when transitioning to the payment complete
1958
	 * order status.
1959
	 *
1960
	 * @since 1.0.19
1961
	 */
1962
	public function maybe_set_date_paid() {
1963
1964
		if ( ! $this->get_date_completed( 'edit' ) && $this->is_paid() ) {
1965
			$this->set_date_completed( current_time( 'mysql' ) );
1966
		}
1967
	}
1968
1969
    /**
1970
	 * Set parent invoice ID.
1971
	 *
1972
	 * @since 1.0.19
1973
	 */
1974
	public function set_parent_id( $value ) {
1975
		if ( $value && ( $value === $this->get_id() ) ) {
1976
			return;
1977
		}
1978
		$this->set_prop( 'parent_id', absint( $value ) );
1979
    }
1980
1981
    /**
1982
	 * Set plugin version when the invoice was created.
1983
	 *
1984
	 * @since 1.0.19
1985
	 */
1986
	public function set_version( $value ) {
1987
		$this->set_prop( 'version', $value );
1988
    }
1989
1990
    /**
1991
	 * Set date when the invoice was created.
1992
	 *
1993
	 * @since 1.0.19
1994
	 * @param string $value Value to set.
1995
     * @return bool Whether or not the date was set.
1996
	 */
1997
	public function set_date_created( $value ) {
1998
        $date = strtotime( $value );
1999
2000
        if ( $date && $value !== '0000-00-00 00:00:00' ) {
2001
            $this->set_prop( 'date_created', date( 'Y-m-d H:i:s', $date ) );
2002
            return true;
2003
        }
2004
2005
		$this->set_prop( 'date_created', '' );
2006
		return false;
2007
2008
    }
2009
2010
    /**
2011
	 * Set date invoice due date.
2012
	 *
2013
	 * @since 1.0.19
2014
	 * @param string $value Value to set.
2015
     * @return bool Whether or not the date was set.
2016
	 */
2017
	public function set_due_date( $value ) {
2018
        $date = strtotime( $value );
2019
2020
        if ( $date && $value !== '0000-00-00 00:00:00' ) {
2021
            $this->set_prop( 'due_date', date( 'Y-m-d H:i:s', $date ) );
2022
            return true;
2023
        }
2024
2025
		$this->set_prop( 'due_date', '' );
2026
        return false;
2027
2028
    }
2029
2030
    /**
2031
	 * Alias of self::set_due_date().
2032
	 *
2033
	 * @since 1.0.19
2034
	 * @param  string $value New name.
2035
	 */
2036
	public function set_date_due( $value ) {
2037
		$this->set_due_date( $value );
2038
    }
2039
2040
    /**
2041
	 * Set date invoice was completed.
2042
	 *
2043
	 * @since 1.0.19
2044
	 * @param string $value Value to set.
2045
     * @return bool Whether or not the date was set.
2046
	 */
2047
	public function set_completed_date( $value ) {
2048
        $date = strtotime( $value );
2049
2050
        if ( $date && $value !== '0000-00-00 00:00:00'  ) {
2051
            $this->set_prop( 'completed_date', date( 'Y-m-d H:i:s', $date ) );
2052
            return true;
2053
        }
2054
2055
		$this->set_prop( 'completed_date', '' );
2056
        return false;
2057
2058
    }
2059
2060
    /**
2061
	 * Alias of self::set_completed_date().
2062
	 *
2063
	 * @since 1.0.19
2064
	 * @param  string $value New name.
2065
	 */
2066
	public function set_date_completed( $value ) {
2067
		$this->set_completed_date( $value );
2068
    }
2069
2070
    /**
2071
	 * Set date when the invoice was last modified.
2072
	 *
2073
	 * @since 1.0.19
2074
	 * @param string $value Value to set.
2075
     * @return bool Whether or not the date was set.
2076
	 */
2077
	public function set_date_modified( $value ) {
2078
        $date = strtotime( $value );
2079
2080
        if ( $date && $value !== '0000-00-00 00:00:00' ) {
2081
            $this->set_prop( 'date_modified', date( 'Y-m-d H:i:s', $date ) );
2082
            return true;
2083
        }
2084
2085
		$this->set_prop( 'date_modified', '' );
2086
        return false;
2087
2088
    }
2089
2090
    /**
2091
	 * Set the invoice number.
2092
	 *
2093
	 * @since 1.0.19
2094
	 * @param  string $value New number.
2095
	 */
2096
	public function set_number( $value ) {
2097
        $number = sanitize_text_field( $value );
2098
		$this->set_prop( 'number', $number );
2099
    }
2100
2101
    /**
2102
	 * Set the invoice type.
2103
	 *
2104
	 * @since 1.0.19
2105
	 * @param  string $value Type.
2106
	 */
2107
	public function set_type( $value ) {
2108
        $type = sanitize_text_field( str_replace( 'wpi_', '', $value ) );
2109
		$this->set_prop( 'type', $type );
2110
	}
2111
2112
    /**
2113
	 * Set the invoice post type.
2114
	 *
2115
	 * @since 1.0.19
2116
	 * @param  string $value Post type.
2117
	 */
2118
	public function set_post_type( $value ) {
2119
        if ( getpaid_is_invoice_post_type( $value ) ) {
2120
			$this->set_type( $value );
2121
            $this->set_prop( 'post_type', $value );
2122
        }
2123
    }
2124
2125
    /**
2126
	 * Set the invoice key.
2127
	 *
2128
	 * @since 1.0.19
2129
	 * @param  string $value New key.
2130
	 */
2131
	public function set_key( $value ) {
2132
        $key = sanitize_text_field( $value );
2133
		$this->set_prop( 'key', $key );
2134
    }
2135
2136
    /**
2137
	 * Set the invoice mode.
2138
	 *
2139
	 * @since 1.0.19
2140
	 * @param  string $value mode.
2141
	 */
2142
	public function set_mode( $value ) {
2143
        if ( in_array( $value, array( 'live', 'test' ) ) ) {
2144
            $this->set_prop( 'mode', $value );
2145
        }
2146
    }
2147
2148
    /**
2149
	 * Set the invoice path.
2150
	 *
2151
	 * @since 1.0.19
2152
	 * @param  string $value path.
2153
	 */
2154
	public function set_path( $value ) {
2155
        $this->set_prop( 'path', $value );
2156
    }
2157
2158
    /**
2159
	 * Set the invoice name.
2160
	 *
2161
	 * @since 1.0.19
2162
	 * @param  string $value New name.
2163
	 */
2164
	public function set_name( $value ) {
2165
        $name = sanitize_text_field( $value );
2166
		$this->set_prop( 'name', $name );
2167
    }
2168
2169
    /**
2170
	 * Alias of self::set_name().
2171
	 *
2172
	 * @since 1.0.19
2173
	 * @param  string $value New name.
2174
	 */
2175
	public function set_title( $value ) {
2176
		$this->set_name( $value );
2177
    }
2178
2179
    /**
2180
	 * Set the invoice description.
2181
	 *
2182
	 * @since 1.0.19
2183
	 * @param  string $value New description.
2184
	 */
2185
	public function set_description( $value ) {
2186
        $description = wp_kses_post( $value );
2187
		$this->set_prop( 'description', $description );
2188
    }
2189
2190
    /**
2191
	 * Alias of self::set_description().
2192
	 *
2193
	 * @since 1.0.19
2194
	 * @param  string $value New description.
2195
	 */
2196
	public function set_excerpt( $value ) {
2197
		$this->set_description( $value );
2198
    }
2199
2200
    /**
2201
	 * Alias of self::set_description().
2202
	 *
2203
	 * @since 1.0.19
2204
	 * @param  string $value New description.
2205
	 */
2206
	public function set_summary( $value ) {
2207
		$this->set_description( $value );
2208
    }
2209
2210
    /**
2211
	 * Set the receiver of the invoice.
2212
	 *
2213
	 * @since 1.0.19
2214
	 * @param  int $value New author.
2215
	 */
2216
	public function set_author( $value ) {
2217
		$user = get_user_by( 'id', (int) $value );
2218
2219
		if ( $user && $user->ID ) {
2220
			$this->set_prop( 'author', $user->ID );
2221
			$this->set_prop( 'email', $user->user_email );
2222
		}
2223
2224
    }
2225
2226
    /**
2227
	 * Alias of self::set_author().
2228
	 *
2229
	 * @since 1.0.19
2230
	 * @param  int $value New user id.
2231
	 */
2232
	public function set_user_id( $value ) {
2233
		$this->set_author( $value );
2234
    }
2235
2236
    /**
2237
	 * Alias of self::set_author().
2238
	 *
2239
	 * @since 1.0.19
2240
	 * @param  int $value New user id.
2241
	 */
2242
	public function set_customer_id( $value ) {
2243
		$this->set_author( $value );
2244
    }
2245
2246
    /**
2247
	 * Set the customer's ip.
2248
	 *
2249
	 * @since 1.0.19
2250
	 * @param  string $value ip address.
2251
	 */
2252
	public function set_ip( $value ) {
2253
		$this->set_prop( 'ip', $value );
2254
    }
2255
2256
    /**
2257
	 * Alias of self::set_ip().
2258
	 *
2259
	 * @since 1.0.19
2260
	 * @param  string $value ip address.
2261
	 */
2262
	public function set_user_ip( $value ) {
2263
		$this->set_ip( $value );
2264
    }
2265
2266
    /**
2267
	 * Set the customer's first name.
2268
	 *
2269
	 * @since 1.0.19
2270
	 * @param  string $value first name.
2271
	 */
2272
	public function set_first_name( $value ) {
2273
		$this->set_prop( 'first_name', $value );
2274
    }
2275
2276
    /**
2277
	 * Alias of self::set_first_name().
2278
	 *
2279
	 * @since 1.0.19
2280
	 * @param  string $value first name.
2281
	 */
2282
	public function set_user_first_name( $value ) {
2283
		$this->set_first_name( $value );
2284
    }
2285
2286
    /**
2287
	 * Alias of self::set_first_name().
2288
	 *
2289
	 * @since 1.0.19
2290
	 * @param  string $value first name.
2291
	 */
2292
	public function set_customer_first_name( $value ) {
2293
		$this->set_first_name( $value );
2294
    }
2295
2296
    /**
2297
	 * Set the customer's last name.
2298
	 *
2299
	 * @since 1.0.19
2300
	 * @param  string $value last name.
2301
	 */
2302
	public function set_last_name( $value ) {
2303
		$this->set_prop( 'last_name', $value );
2304
    }
2305
2306
    /**
2307
	 * Alias of self::set_last_name().
2308
	 *
2309
	 * @since 1.0.19
2310
	 * @param  string $value last name.
2311
	 */
2312
	public function set_user_last_name( $value ) {
2313
		$this->set_last_name( $value );
2314
    }
2315
2316
    /**
2317
	 * Alias of self::set_last_name().
2318
	 *
2319
	 * @since 1.0.19
2320
	 * @param  string $value last name.
2321
	 */
2322
	public function set_customer_last_name( $value ) {
2323
		$this->set_last_name( $value );
2324
    }
2325
2326
    /**
2327
	 * Set the customer's phone number.
2328
	 *
2329
	 * @since 1.0.19
2330
	 * @param  string $value phone.
2331
	 */
2332
	public function set_phone( $value ) {
2333
		$this->set_prop( 'phone', $value );
2334
    }
2335
2336
    /**
2337
	 * Alias of self::set_phone().
2338
	 *
2339
	 * @since 1.0.19
2340
	 * @param  string $value phone.
2341
	 */
2342
	public function set_user_phone( $value ) {
2343
		$this->set_phone( $value );
2344
    }
2345
2346
    /**
2347
	 * Alias of self::set_phone().
2348
	 *
2349
	 * @since 1.0.19
2350
	 * @param  string $value phone.
2351
	 */
2352
	public function set_customer_phone( $value ) {
2353
		$this->set_phone( $value );
2354
    }
2355
2356
    /**
2357
	 * Alias of self::set_phone().
2358
	 *
2359
	 * @since 1.0.19
2360
	 * @param  string $value phone.
2361
	 */
2362
	public function set_phone_number( $value ) {
2363
		$this->set_phone( $value );
2364
    }
2365
2366
    /**
2367
	 * Set the customer's email address.
2368
	 *
2369
	 * @since 1.0.19
2370
	 * @param  string $value email address.
2371
	 */
2372
	public function set_email( $value ) {
2373
		$this->set_prop( 'email', $value );
2374
    }
2375
2376
    /**
2377
	 * Alias of self::set_email().
2378
	 *
2379
	 * @since 1.0.19
2380
	 * @param  string $value email address.
2381
	 */
2382
	public function set_user_email( $value ) {
2383
		$this->set_email( $value );
2384
    }
2385
2386
    /**
2387
	 * Alias of self::set_email().
2388
	 *
2389
	 * @since 1.0.19
2390
	 * @param  string $value email address.
2391
	 */
2392
	public function set_email_address( $value ) {
2393
		$this->set_email( $value );
2394
    }
2395
2396
    /**
2397
	 * Alias of self::set_email().
2398
	 *
2399
	 * @since 1.0.19
2400
	 * @param  string $value email address.
2401
	 */
2402
	public function set_customer_email( $value ) {
2403
		$this->set_email( $value );
2404
    }
2405
2406
    /**
2407
	 * Set the customer's country.
2408
	 *
2409
	 * @since 1.0.19
2410
	 * @param  string $value country.
2411
	 */
2412
	public function set_country( $value ) {
2413
		$this->set_prop( 'country', $value );
2414
    }
2415
2416
    /**
2417
	 * Alias of self::set_country().
2418
	 *
2419
	 * @since 1.0.19
2420
	 * @param  string $value country.
2421
	 */
2422
	public function set_user_country( $value ) {
2423
		$this->set_country( $value );
2424
    }
2425
2426
    /**
2427
	 * Alias of self::set_country().
2428
	 *
2429
	 * @since 1.0.19
2430
	 * @param  string $value country.
2431
	 */
2432
	public function set_customer_country( $value ) {
2433
		$this->set_country( $value );
2434
    }
2435
2436
    /**
2437
	 * Set the customer's state.
2438
	 *
2439
	 * @since 1.0.19
2440
	 * @param  string $value state.
2441
	 */
2442
	public function set_state( $value ) {
2443
		$this->set_prop( 'state', $value );
2444
    }
2445
2446
    /**
2447
	 * Alias of self::set_state().
2448
	 *
2449
	 * @since 1.0.19
2450
	 * @param  string $value state.
2451
	 */
2452
	public function set_user_state( $value ) {
2453
		$this->set_state( $value );
2454
    }
2455
2456
    /**
2457
	 * Alias of self::set_state().
2458
	 *
2459
	 * @since 1.0.19
2460
	 * @param  string $value state.
2461
	 */
2462
	public function set_customer_state( $value ) {
2463
		$this->set_state( $value );
2464
    }
2465
2466
    /**
2467
	 * Set the customer's city.
2468
	 *
2469
	 * @since 1.0.19
2470
	 * @param  string $value city.
2471
	 */
2472
	public function set_city( $value ) {
2473
		$this->set_prop( 'city', $value );
2474
    }
2475
2476
    /**
2477
	 * Alias of self::set_city().
2478
	 *
2479
	 * @since 1.0.19
2480
	 * @param  string $value city.
2481
	 */
2482
	public function set_user_city( $value ) {
2483
		$this->set_city( $value );
2484
    }
2485
2486
    /**
2487
	 * Alias of self::set_city().
2488
	 *
2489
	 * @since 1.0.19
2490
	 * @param  string $value city.
2491
	 */
2492
	public function set_customer_city( $value ) {
2493
		$this->set_city( $value );
2494
    }
2495
2496
    /**
2497
	 * Set the customer's zip code.
2498
	 *
2499
	 * @since 1.0.19
2500
	 * @param  string $value zip.
2501
	 */
2502
	public function set_zip( $value ) {
2503
		$this->set_prop( 'zip', $value );
2504
    }
2505
2506
    /**
2507
	 * Alias of self::set_zip().
2508
	 *
2509
	 * @since 1.0.19
2510
	 * @param  string $value zip.
2511
	 */
2512
	public function set_user_zip( $value ) {
2513
		$this->set_zip( $value );
2514
    }
2515
2516
    /**
2517
	 * Alias of self::set_zip().
2518
	 *
2519
	 * @since 1.0.19
2520
	 * @param  string $value zip.
2521
	 */
2522
	public function set_customer_zip( $value ) {
2523
		$this->set_zip( $value );
2524
    }
2525
2526
    /**
2527
	 * Set the customer's company.
2528
	 *
2529
	 * @since 1.0.19
2530
	 * @param  string $value company.
2531
	 */
2532
	public function set_company( $value ) {
2533
		$this->set_prop( 'company', $value );
2534
    }
2535
2536
    /**
2537
	 * Alias of self::set_company().
2538
	 *
2539
	 * @since 1.0.19
2540
	 * @param  string $value company.
2541
	 */
2542
	public function set_user_company( $value ) {
2543
		$this->set_company( $value );
2544
    }
2545
2546
    /**
2547
	 * Alias of self::set_company().
2548
	 *
2549
	 * @since 1.0.19
2550
	 * @param  string $value company.
2551
	 */
2552
	public function set_customer_company( $value ) {
2553
		$this->set_company( $value );
2554
    }
2555
2556
    /**
2557
	 * Set the customer's var number.
2558
	 *
2559
	 * @since 1.0.19
2560
	 * @param  string $value var number.
2561
	 */
2562
	public function set_vat_number( $value ) {
2563
		$this->set_prop( 'vat_number', $value );
2564
    }
2565
2566
    /**
2567
	 * Alias of self::set_vat_number().
2568
	 *
2569
	 * @since 1.0.19
2570
	 * @param  string $value var number.
2571
	 */
2572
	public function set_user_vat_number( $value ) {
2573
		$this->set_vat_number( $value );
2574
    }
2575
2576
    /**
2577
	 * Alias of self::set_vat_number().
2578
	 *
2579
	 * @since 1.0.19
2580
	 * @param  string $value var number.
2581
	 */
2582
	public function set_customer_vat_number( $value ) {
2583
		$this->set_vat_number( $value );
2584
    }
2585
2586
    /**
2587
	 * Set the customer's vat rate.
2588
	 *
2589
	 * @since 1.0.19
2590
	 * @param  string $value var rate.
2591
	 */
2592
	public function set_vat_rate( $value ) {
2593
		$this->set_prop( 'vat_rate', $value );
2594
    }
2595
2596
    /**
2597
	 * Alias of self::set_vat_rate().
2598
	 *
2599
	 * @since 1.0.19
2600
	 * @param  string $value var number.
2601
	 */
2602
	public function set_user_vat_rate( $value ) {
2603
		$this->set_vat_rate( $value );
2604
    }
2605
2606
    /**
2607
	 * Alias of self::set_vat_rate().
2608
	 *
2609
	 * @since 1.0.19
2610
	 * @param  string $value var number.
2611
	 */
2612
	public function set_customer_vat_rate( $value ) {
2613
		$this->set_vat_rate( $value );
2614
    }
2615
2616
    /**
2617
	 * Set the customer's address.
2618
	 *
2619
	 * @since 1.0.19
2620
	 * @param  string $value address.
2621
	 */
2622
	public function set_address( $value ) {
2623
		$this->set_prop( 'address', $value );
2624
    }
2625
2626
    /**
2627
	 * Alias of self::set_address().
2628
	 *
2629
	 * @since 1.0.19
2630
	 * @param  string $value address.
2631
	 */
2632
	public function set_user_address( $value ) {
2633
		$this->set_address( $value );
2634
    }
2635
2636
    /**
2637
	 * Alias of self::set_address().
2638
	 *
2639
	 * @since 1.0.19
2640
	 * @param  string $value address.
2641
	 */
2642
	public function set_customer_address( $value ) {
2643
		$this->set_address( $value );
2644
    }
2645
2646
    /**
2647
	 * Set whether the customer has viewed the invoice or not.
2648
	 *
2649
	 * @since 1.0.19
2650
	 * @param  int|bool $value confirmed.
2651
	 */
2652
	public function set_is_viewed( $value ) {
2653
		$this->set_prop( 'is_viewed', $value );
2654
	}
2655
2656
	/**
2657
	 * Set extra email recipients.
2658
	 *
2659
	 * @since 1.0.19
2660
	 * @param  string $value email recipients.
2661
	 */
2662
	public function set_email_cc( $value ) {
2663
		$this->set_prop( 'email_cc', $value );
2664
	}
2665
2666
	/**
2667
	 * Set the invoice template.
2668
	 *
2669
	 * @since 1.0.19
2670
	 * @param  string $value template.
2671
	 */
2672
	public function set_template( $value ) {
2673
		if ( in_array( $value, array( 'quantity', 'hours', 'amount' ) ) ) {
2674
			$this->set_prop( 'template', $value );
2675
		}
2676
	}
2677
2678
	/**
2679
	 * Set the invoice source.
2680
	 *
2681
	 * @since 1.0.19
2682
	 * @param  string $value email recipients.
2683
	 */
2684
	public function created_via( $value ) {
2685
		$this->set_prop( 'created_via', sanitize_text_field( $value ) );
2686
	}
2687
2688
	/**
2689
	 * Set the customer's address confirmed status.
2690
	 *
2691
	 * @since 1.0.19
2692
	 * @param  int|bool $value confirmed.
2693
	 */
2694
	public function set_address_confirmed( $value ) {
2695
		$this->set_prop( 'address_confirmed', $value );
2696
    }
2697
2698
    /**
2699
	 * Alias of self::set_address_confirmed().
2700
	 *
2701
	 * @since 1.0.19
2702
	 * @param  int|bool $value confirmed.
2703
	 */
2704
	public function set_user_address_confirmed( $value ) {
2705
		$this->set_address_confirmed( $value );
2706
    }
2707
2708
    /**
2709
	 * Alias of self::set_address_confirmed().
2710
	 *
2711
	 * @since 1.0.19
2712
	 * @param  int|bool $value confirmed.
2713
	 */
2714
	public function set_customer_address_confirmed( $value ) {
2715
		$this->set_address_confirmed( $value );
2716
    }
2717
2718
    /**
2719
	 * Set the invoice sub total.
2720
	 *
2721
	 * @since 1.0.19
2722
	 * @param  float $value sub total.
2723
	 */
2724
	public function set_subtotal( $value ) {
2725
		$this->set_prop( 'subtotal', $value );
2726
    }
2727
2728
    /**
2729
	 * Set the invoice discount amount.
2730
	 *
2731
	 * @since 1.0.19
2732
	 * @param  float $value discount total.
2733
	 */
2734
	public function set_total_discount( $value ) {
2735
		$this->set_prop( 'total_discount', $value );
2736
    }
2737
2738
    /**
2739
	 * Alias of self::set_total_discount().
2740
	 *
2741
	 * @since 1.0.19
2742
	 * @param  float $value discount total.
2743
	 */
2744
	public function set_discount( $value ) {
2745
		$this->set_total_discount( $value );
2746
    }
2747
2748
    /**
2749
	 * Set the invoice tax amount.
2750
	 *
2751
	 * @since 1.0.19
2752
	 * @param  float $value tax total.
2753
	 */
2754
	public function set_total_tax( $value ) {
2755
		$this->set_prop( 'total_tax', $value );
2756
    }
2757
2758
    /**
2759
	 * Alias of self::set_total_tax().
2760
	 *
2761
	 * @since 1.0.19
2762
	 * @param  float $value tax total.
2763
	 */
2764
	public function set_tax_total( $value ) {
2765
		$this->set_total_tax( $value );
2766
    }
2767
2768
    /**
2769
	 * Set the invoice fees amount.
2770
	 *
2771
	 * @since 1.0.19
2772
	 * @param  float $value fees total.
2773
	 */
2774
	public function set_total_fees( $value ) {
2775
		$this->set_prop( 'total_fees', $value );
2776
    }
2777
2778
    /**
2779
	 * Alias of self::set_total_fees().
2780
	 *
2781
	 * @since 1.0.19
2782
	 * @param  float $value fees total.
2783
	 */
2784
	public function set_fees_total( $value ) {
2785
		$this->set_total_fees( $value );
2786
    }
2787
2788
    /**
2789
	 * Set the invoice fees.
2790
	 *
2791
	 * @since 1.0.19
2792
	 * @param  array $value fees.
2793
	 */
2794
	public function set_fees( $value ) {
2795
2796
		if ( ! is_array( $value ) ) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
2797
			$value = array();
2798
		}
2799
2800
		$this->set_prop( 'fees', $value );
2801
2802
    }
2803
2804
    /**
2805
	 * Set the invoice taxes.
2806
	 *
2807
	 * @since 1.0.19
2808
	 * @param  array $value taxes.
2809
	 */
2810
	public function set_taxes( $value ) {
2811
2812
		if ( ! is_array( $value ) ) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
2813
			$value = array();
2814
		}
2815
2816
		$this->set_prop( 'taxes', $value );
2817
2818
    }
2819
2820
    /**
2821
	 * Set the invoice discounts.
2822
	 *
2823
	 * @since 1.0.19
2824
	 * @param  array $value discounts.
2825
	 */
2826
	public function set_discounts( $value ) {
2827
2828
		if ( ! is_array( $value ) ) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
2829
			$value = array();
2830
		}
2831
2832
		$this->set_prop( 'discounts', $value );
2833
    }
2834
2835
    /**
2836
	 * Set the invoice items.
2837
	 *
2838
	 * @since 1.0.19
2839
	 * @param  GetPaid_Form_Item[] $value items.
2840
	 */
2841
	public function set_items( $value ) {
2842
2843
        // Remove existing items.
2844
        $this->set_prop( 'items', array() );
2845
2846
        // Ensure that we have an array.
2847
        if ( ! is_array( $value ) ) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
2848
            return;
2849
        }
2850
2851
        foreach ( $value as $item ) {
2852
            $this->add_item( $item );
2853
        }
2854
2855
    }
2856
2857
    /**
2858
	 * Set the payment form.
2859
	 *
2860
	 * @since 1.0.19
2861
	 * @param  int $value payment form.
2862
	 */
2863
	public function set_payment_form( $value ) {
2864
		$this->set_prop( 'payment_form', $value );
2865
    }
2866
2867
    /**
2868
	 * Set the submission id.
2869
	 *
2870
	 * @since 1.0.19
2871
	 * @param  string $value submission id.
2872
	 */
2873
	public function set_submission_id( $value ) {
2874
		$this->set_prop( 'submission_id', $value );
2875
    }
2876
2877
    /**
2878
	 * Set the discount code.
2879
	 *
2880
	 * @since 1.0.19
2881
	 * @param  string $value discount code.
2882
	 */
2883
	public function set_discount_code( $value ) {
2884
		$this->set_prop( 'discount_code', $value );
2885
    }
2886
2887
    /**
2888
	 * Set the gateway.
2889
	 *
2890
	 * @since 1.0.19
2891
	 * @param  string $value gateway.
2892
	 */
2893
	public function set_gateway( $value ) {
2894
		$this->set_prop( 'gateway', $value );
2895
    }
2896
2897
    /**
2898
	 * Set the transaction id.
2899
	 *
2900
	 * @since 1.0.19
2901
	 * @param  string $value transaction id.
2902
	 */
2903
	public function set_transaction_id( $value ) {
2904
		if ( ! empty( $value ) ) {
2905
			$this->set_prop( 'transaction_id', $value );
2906
		}
2907
    }
2908
2909
    /**
2910
	 * Set the currency id.
2911
	 *
2912
	 * @since 1.0.19
2913
	 * @param  string $value currency id.
2914
	 */
2915
	public function set_currency( $value ) {
2916
		$this->set_prop( 'currency', $value );
2917
    }
2918
2919
	/**
2920
	 * Set whether to disable taxes.
2921
	 *
2922
	 * @since 1.0.19
2923
	 * @param  bool $value value.
2924
	 */
2925
	public function set_disable_taxes( $value ) {
2926
		$this->set_prop( 'disable_taxes', (bool) $value );
2927
	}
2928
2929
    /**
2930
	 * Set the subscription id.
2931
	 *
2932
	 * @since 1.0.19
2933
	 * @param  string $value subscription id.
2934
	 */
2935
	public function set_subscription_id( $value ) {
2936
		$this->set_prop( 'subscription_id', $value );
2937
	}
2938
	
2939
	/**
2940
	 * Set the remote subscription id.
2941
	 *
2942
	 * @since 1.0.19
2943
	 * @param  string $value subscription id.
2944
	 */
2945
	public function set_remote_subscription_id( $value ) {
2946
		$this->set_prop( 'remote_subscription_id', $value );
2947
    }
2948
2949
    /*
2950
	|--------------------------------------------------------------------------
2951
	| Boolean methods
2952
	|--------------------------------------------------------------------------
2953
	|
2954
	| Return true or false.
2955
	|
2956
    */
2957
2958
    /**
2959
     * Checks if this is a parent invoice.
2960
     */
2961
    public function is_parent() {
2962
        $parent = $this->get_parent_id();
2963
        return apply_filters( 'wpinv_invoice_is_parent', empty( $parent ), $this );
2964
    }
2965
2966
    /**
2967
     * Checks if this is a renewal invoice.
2968
     */
2969
    public function is_renewal() {
2970
        return ! $this->is_parent();
2971
    }
2972
2973
    /**
2974
     * Checks if this is a recurring invoice.
2975
     */
2976
    public function is_recurring() {
2977
        return $this->is_renewal() || ! empty( $this->recurring_item );
2978
    }
2979
2980
    /**
2981
     * Checks if this is a taxable invoice.
2982
     */
2983
    public function is_taxable() {
2984
        return ! $this->get_disable_taxes();
2985
	}
2986
2987
	/**
2988
	 * @deprecated
2989
	 */
2990
	public function has_vat() {
2991
        return $this->is_taxable();
2992
	}
2993
2994
	/**
2995
	 * Checks to see if the invoice requires payment.
2996
	 */
2997
	public function is_free() {
2998
        $is_free = ( (float) wpinv_round_amount( $this->get_initial_total() ) == 0 );
2999
3000
		if ( $this->is_recurring() && $this->get_recurring_total() > 0 ) {
3001
			$is_free = false;
3002
		}
3003
3004
        return apply_filters( 'wpinv_invoice_is_free', $is_free, $this );
3005
    }
3006
3007
    /**
3008
     * Checks if the invoice is paid.
3009
     */
3010
    public function is_paid() {
3011
        $is_paid = $this->has_status( array( 'publish', 'wpi-processing', 'wpi-renewal' ) );
3012
        return apply_filters( 'wpinv_invoice_is_paid', $is_paid, $this );
3013
	}
3014
3015
	/**
3016
     * Checks if the invoice needs payment.
3017
     */
3018
	public function needs_payment() {
3019
		$needs_payment = ! $this->is_paid() && ! $this->is_refunded() && ! $this->is_free();
3020
        return apply_filters( 'wpinv_needs_payment', $needs_payment, $this );
3021
    }
3022
  
3023
	/**
3024
     * Checks if the invoice is refunded.
3025
     */
3026
	public function is_refunded() {
3027
        $is_refunded = $this->has_status( 'wpi-refunded' );
3028
        return apply_filters( 'wpinv_invoice_is_refunded', $is_refunded, $this );
3029
	}
3030
3031
	/**
3032
     * Checks if the invoice is held.
3033
     */
3034
	public function is_held() {
3035
        $is_held = $this->has_status( 'wpi-onhold' );
3036
        return apply_filters( 'wpinv_invoice_is_held', $is_held, $this );
3037
	}
3038
3039
	/**
3040
     * Checks if the invoice is due.
3041
     */
3042
	public function is_due() {
3043
		$due_date = $this->get_due_date();
3044
		return empty( $due_date ) ? false : current_time( 'timestamp' ) > strtotime( $due_date );
3045
	}
3046
3047
	/**
3048
     * Checks if the invoice is draft.
3049
     */
3050
	public function is_draft() {
3051
        return $this->has_status( 'draft, auto-draft' );
3052
	}
3053
3054
    /**
3055
     * Checks if the invoice has a given status.
3056
     */
3057
    public function has_status( $status ) {
3058
        $status = wpinv_parse_list( $status );
3059
        return apply_filters( 'wpinv_has_status', in_array( $this->get_status(), $status ), $status );
3060
	}
3061
3062
	/**
3063
     * Checks if the invoice is of a given type.
3064
     */
3065
    public function is_type( $type ) {
3066
        $type = wpinv_parse_list( $type );
3067
        return in_array( $this->get_type(), $type );
3068
    }
3069
3070
    /**
3071
     * Checks if this is a quote object.
3072
     *
3073
     * @since 1.0.15
3074
     */
3075
    public function is_quote() {
3076
        return 'wpi_quote' == $this->get_post_type();
3077
    }
3078
3079
    /**
3080
     * Check if the invoice (or it's parent has a free trial).
3081
     *
3082
     */
3083
    public function has_free_trial() {
3084
        return $this->is_recurring() && 0 == $this->get_initial_total();
3085
	}
3086
3087
	/**
3088
     * @deprecated
3089
     */
3090
    public function is_free_trial() {
3091
        $this->has_free_trial();
3092
    }
3093
3094
	/**
3095
     * Check if the initial payment if 0.
3096
     *
3097
     */
3098
	public function is_initial_free() {
3099
        $is_initial_free = ! ( (float) wpinv_round_amount( $this->get_initial_total() ) > 0 );
3100
        return apply_filters( 'wpinv_invoice_is_initial_free', $is_initial_free, $this->get_cart_details(), $this );
3101
    }
3102
	
3103
	/**
3104
     * Check if the recurring item has a free trial.
3105
     *
3106
     */
3107
    public function item_has_free_trial() {
3108
3109
        // Ensure we have a recurring item.
3110
        if ( ! $this->is_recurring() ) {
3111
            return false;
3112
        }
3113
3114
        $item = $this->get_recurring( true );
3115
        return $item->has_free_trial();
3116
	}
3117
3118
	/**
3119
     * Check if the free trial is a result of a discount.
3120
     */
3121
    public function is_free_trial_from_discount() {
3122
		return $this->has_free_trial() && ! $this->item_has_free_trial();
3123
	}
3124
	
3125
	/**
3126
     * @deprecated
3127
     */
3128
    public function discount_first_payment_only() {
3129
3130
		$discount = wpinv_get_discount_obj( $this->get_discount_code() );
3131
        if ( ! $discount->exists() || ! $this->is_recurring() ) {
3132
            return true;
3133
        }
3134
3135
        return ! $discount->get_is_recurring();
3136
    }
3137
3138
    /*
3139
	|--------------------------------------------------------------------------
3140
	| Cart related methods
3141
	|--------------------------------------------------------------------------
3142
	|
3143
	| Do not forget to recalculate totals after calling the following methods.
3144
	|
3145
    */
3146
3147
    /**
3148
     * Adds an item to the invoice.
3149
     *
3150
     * @param GetPaid_Form_Item|array $item
3151
     * @return WP_Error|Bool
3152
     */
3153
    public function add_item( $item ) {
3154
3155
		if ( is_array( $item ) ) {
3156
			$item = $this->process_array_item( $item );
3157
		}
3158
3159
		if ( is_numeric( $item ) ) {
0 ignored issues
show
introduced by
The condition is_numeric($item) is always false.
Loading history...
3160
			$item = new GetPaid_Form_Item( $item );
3161
		}
3162
3163
        // Make sure that it is available for purchase.
3164
		if ( $item->get_id() > 0 && ! $item->can_purchase() ) {
3165
			return new WP_Error( 'invalid_item', __( 'This item is not available for purchase', 'invoicing' ) );
3166
        }
3167
3168
        // Do we have a recurring item?
3169
		if ( $item->is_recurring() ) {
3170
3171
			// An invoice can only contain one recurring item.
3172
			if ( ! empty( $this->recurring_item  && $this->recurring_item != (int) $item->get_id() ) ) {
3173
				return new WP_Error( 'recurring_item', __( 'An invoice can only contain one recurring item', 'invoicing' ) );
3174
			}
3175
3176
			$this->recurring_item = $item->get_id();
3177
        }
3178
3179
        // Invoice id.
3180
        $item->invoice_id = (int) $this->get_id();
3181
3182
        // Retrieve all items.
3183
        $items = $this->get_items();
3184
        $items[ (int) $item->get_id() ] = $item;
3185
3186
        $this->set_prop( 'items', $items );
3187
		return true;
3188
	}
3189
3190
	/**
3191
	 * Converts an array to an item.
3192
	 *
3193
	 * @since 1.0.19
3194
	 * @return GetPaid_Form_Item
3195
	 */
3196
	protected function process_array_item( $array ) {
3197
3198
		$item_id = isset( $array['item_id'] ) ? $array['item_id'] : 0;
3199
		$item    = new GetPaid_Form_Item( $item_id );
3200
3201
		// Set item data.
3202
		foreach ( array( 'name', 'price', 'description' ) as $key ) {
3203
			if ( isset( $array[ "item_$key" ] ) ) {
3204
				$method = "set_$key";
3205
				$item->$method( $array[ "item_$key" ] );
3206
			}
3207
		}
3208
3209
		if ( isset( $array['quantity'] ) ) {
3210
			$item->set_quantity( $array['quantity'] );
3211
		}
3212
3213
		// Set item meta.
3214
		if ( isset( $array['meta'] ) && is_array( $array['meta'] ) ) {
3215
			$item->set_item_meta( $array['meta'] );
3216
		}
3217
3218
		return $item;
3219
3220
	}
3221
3222
    /**
3223
	 * Retrieves a specific item.
3224
	 *
3225
	 * @since 1.0.19
3226
	 */
3227
	public function get_item( $item_id ) {
3228
		$items   = $this->get_items();
3229
		$item_id = (int) $item_id;
3230
		return ( ! empty( $item_id ) && isset( $items[ $item_id ] ) ) ? $items[ $item_id ] : null;
3231
    }
3232
3233
    /**
3234
	 * Removes a specific item.
3235
	 *
3236
	 * @since 1.0.19
3237
	 */
3238
	public function remove_item( $item_id ) {
3239
		$items   = $this->get_items();
3240
		$item_id = (int) $item_id;
3241
3242
        if ( $item_id == $this->recurring_item ) {
3243
            $this->recurring_item = null;
3244
        }
3245
3246
        if ( isset( $items[ $item_id ] ) ) {
3247
            unset( $items[ $item_id ] );
3248
            $this->set_prop( 'items', $items );
3249
        }
3250
    }
3251
3252
    /**
3253
	 * Adds a fee to the invoice.
3254
	 *
3255
	 * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required.
3256
	 * @since 1.0.19
3257
	 */
3258
    public function add_fee( $fee ) {
3259
3260
		$fees                 = $this->get_fees();
3261
		$fees[ $fee['name'] ] = $fee;
3262
		$this->set_prop( 'fees', $fees );
3263
3264
    }
3265
3266
    /**
3267
	 * Retrieves a specific fee.
3268
	 *
3269
	 * @since 1.0.19
3270
	 */
3271
	public function get_fee( $fee ) {
3272
        $fees = $this->get_fees();
3273
		return isset( $fees[ $fee ] ) ? $fees[ $fee ] : null;
3274
    }
3275
3276
    /**
3277
	 * Removes a specific fee.
3278
	 *
3279
	 * @since 1.0.19
3280
	 */
3281
	public function remove_fee( $fee ) {
3282
        $fees = $this->get_fees();
3283
        if ( isset( $fees[ $fee ] ) ) {
3284
            unset( $fees[ $fee ] );
3285
            $this->set_prop( 'fees', $fees );
3286
        }
3287
    }
3288
3289
	/**
3290
	 * Adds a discount to the invoice.
3291
	 *
3292
	 * @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.
3293
	 * @since 1.0.19
3294
	 */
3295
	public function add_discount( $discount ) {
3296
3297
		$discounts = $this->get_discounts();
3298
		$discounts[ $discount['name'] ] = $discount;
3299
		$this->set_prop( 'discounts', $discounts );
3300
3301
	}
3302
3303
    /**
3304
	 * Retrieves a specific discount.
3305
	 *
3306
	 * @since 1.0.19
3307
	 * @return float
3308
	 */
3309
	public function get_discount( $discount = false ) {
3310
3311
		// Backwards compatibilty.
3312
		if ( empty( $discount ) ) {
3313
			return $this->get_total_discount();
3314
		}
3315
3316
        $discounts = $this->get_discounts();
3317
		return isset( $discounts[ $discount ] ) ? $discounts[ $discount ] : null;
3318
    }
3319
3320
    /**
3321
	 * Removes a specific discount.
3322
	 *
3323
	 * @since 1.0.19
3324
	 */
3325
	public function remove_discount( $discount ) {
3326
        $discounts = $this->get_discounts();
3327
        if ( isset( $discounts[ $discount ] ) ) {
3328
            unset( $discounts[ $discount ] );
3329
            $this->set_prop( 'discounts', $discounts );
3330
        }
3331
    }
3332
3333
    /**
3334
     * Adds a tax to the invoice.
3335
     *
3336
     * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required.
3337
     */
3338
    public function add_tax( $tax ) {
3339
        if ( $this->is_taxable() ) {
3340
3341
            $taxes                 = $this->get_taxes();
3342
			$taxes[ $tax['name'] ] = $tax;
3343
			$this->set_prop( 'taxes', $tax );
3344
3345
        }
3346
    }
3347
3348
    /**
3349
	 * Retrieves a specific tax.
3350
	 *
3351
	 * @since 1.0.19
3352
	 */
3353
	public function get_tax( $tax = null ) {
3354
3355
		// Backwards compatility.
3356
		if ( empty( $tax ) ) {
3357
			return $this->get_total_tax();
3358
		}
3359
3360
        $taxes = $this->get_taxes();
3361
		return isset( $taxes[ $tax ] ) ? $taxes[ $tax ] : null;
3362
    }
3363
3364
    /**
3365
	 * Removes a specific tax.
3366
	 *
3367
	 * @since 1.0.19
3368
	 */
3369
	public function remove_tax( $tax ) {
3370
        $taxes = $this->get_taxes();
3371
        if ( isset( $taxes[ $tax ] ) ) {
3372
            unset( $taxes[ $tax ] );
3373
            $this->set_prop( 'taxes', $taxes );
3374
        }
3375
    }
3376
3377
    /**
3378
	 * Recalculates the invoice subtotal.
3379
	 *
3380
	 * @since 1.0.19
3381
	 * @return float The recalculated subtotal
3382
	 */
3383
	public function recalculate_subtotal() {
3384
        $items     = $this->get_items();
3385
		$subtotal  = 0;
3386
		$recurring = 0;
3387
3388
        foreach ( $items as $item ) {
3389
			$subtotal  += $item->get_sub_total();
3390
			$recurring += $item->get_recurring_sub_total();
3391
        }
3392
3393
		$current = $this->is_renewal() ? $recurring : $subtotal;
3394
		$this->set_subtotal( $current );
3395
3396
		$this->totals['subtotal'] = array(
3397
			'initial'   => $subtotal,
3398
			'recurring' => $recurring,
3399
		);
3400
3401
        return $current;
3402
    }
3403
3404
    /**
3405
	 * Recalculates the invoice discount total.
3406
	 *
3407
	 * @since 1.0.19
3408
	 * @return float The recalculated discount
3409
	 */
3410
	public function recalculate_total_discount() {
3411
        $discounts = $this->get_discounts();
3412
		$discount  = 0;
3413
		$recurring = 0;
3414
3415
        foreach ( $discounts as $data ) {
3416
			$discount  += wpinv_sanitize_amount( $data['initial_discount'] );
3417
			$recurring += wpinv_sanitize_amount( $data['recurring_discount'] );
3418
		}
3419
3420
		$current = $this->is_renewal() ? $recurring : $discount;
3421
3422
		$this->set_total_discount( $current );
3423
3424
		$this->totals['discount'] = array(
3425
			'initial'   => $discount,
3426
			'recurring' => $recurring,
3427
		);
3428
3429
		return $current;
3430
3431
    }
3432
3433
    /**
3434
	 * Recalculates the invoice tax total.
3435
	 *
3436
	 * @since 1.0.19
3437
	 * @return float The recalculated tax
3438
	 */
3439
	public function recalculate_total_tax() {
3440
        $taxes     = $this->get_taxes();
3441
		$tax       = 0;
3442
		$recurring = 0;
3443
3444
        foreach ( $taxes as $data ) {
3445
			$tax       += wpinv_sanitize_amount( $data['initial_tax'] );
3446
			$recurring += wpinv_sanitize_amount( $data['recurring_tax'] );
3447
		}
3448
3449
		$current = $this->is_renewal() ? $recurring : $tax;
3450
		$this->set_total_tax( $current );
3451
3452
		$this->totals['tax'] = array(
3453
			'initial'   => $tax,
3454
			'recurring' => $recurring,
3455
		);
3456
3457
		return $current;
3458
3459
    }
3460
3461
    /**
3462
	 * Recalculates the invoice fees total.
3463
	 *
3464
	 * @since 1.0.19
3465
	 * @return float The recalculated fee
3466
	 */
3467
	public function recalculate_total_fees() {
3468
		$fees      = $this->get_fees();
3469
		$fee       = 0;
3470
		$recurring = 0;
3471
3472
        foreach ( $fees as $data ) {
3473
			$fee       += wpinv_sanitize_amount( $data['initial_fee'] );
3474
			$recurring += wpinv_sanitize_amount( $data['recurring_fee'] );
3475
		}
3476
3477
		$current = $this->is_renewal() ? $recurring : $fee;
3478
		$this->set_total_fees( $current );
3479
3480
		$this->totals['fee'] = array(
3481
			'initial'   => $fee,
3482
			'recurring' => $recurring,
3483
		);
3484
3485
        $this->set_total_fees( $fee );
3486
        return $current;
3487
    }
3488
3489
    /**
3490
	 * Recalculates the invoice total.
3491
	 *
3492
	 * @since 1.0.19
3493
     * @return float The invoice total
3494
	 */
3495
	public function recalculate_total() {
3496
        $this->recalculate_subtotal();
3497
        $this->recalculate_total_fees();
3498
        $this->recalculate_total_discount();
3499
        $this->recalculate_total_tax();
3500
		return $this->get_total();
3501
	}
3502
3503
	/**
3504
	 * @deprecated
3505
	 */
3506
    public function recalculate_totals() {
3507
        $this->recalculate_total();
3508
        $this->save( true );
0 ignored issues
show
Unused Code introduced by
The call to WPInv_Invoice::save() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

3508
        $this->/** @scrutinizer ignore-call */ 
3509
               save( true );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
3509
        return $this;
3510
    }
3511
3512
    /**
3513
     * Convert this to an array.
3514
     */
3515
    public function array_convert() {
3516
        return $this->get_data();
3517
    }
3518
3519
    /**
3520
     * Adds a note to an invoice.
3521
     *
3522
     * @param string $note The note being added.
3523
	 * @return int|false The new note's ID on success, false on failure.
3524
     *
3525
     */
3526
    public function add_note( $note = '', $customer_type = false, $added_by_user = false, $system = false ) {
3527
3528
        // Bail if no note specified or this invoice is not yet saved.
3529
        if ( ! $note || $this->get_id() == 0 || ( ! is_user_logged_in() && ! $system ) ) {
3530
            return false;
3531
        }
3532
3533
		$author       = 'System';
3534
		$author_email = '[email protected]';
3535
3536
		// If this is an admin comment or it has been added by the user.
3537
		if ( is_user_logged_in() && ( ! $system || $added_by_user ) ) {
3538
			$user         = get_user_by( 'id', get_current_user_id() );
3539
            $author       = $user->display_name;
3540
            $author_email = $user->user_email;
3541
		}
3542
3543
		return getpaid_notes()->add_invoice_note( $this, $note, $author, $author_email, $customer_type );
3544
3545
	}
3546
3547
	/**
3548
     * Generates a unique key for the invoice.
3549
     */
3550
    public function generate_key( $string = '' ) {
3551
        $auth_key  = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
3552
        return strtolower(
3553
            $string . md5( $this->get_id() . date( 'Y-m-d H:i:s' ) . $auth_key . uniqid( 'wpinv', true ) )
3554
        );
3555
    }
3556
3557
    /**
3558
     * Generates a new number for the invoice.
3559
     */
3560
    public function generate_number() {
3561
        $number = $this->get_id();
3562
3563
        if ( wpinv_sequential_number_active( $this->get_post_type() ) ) {
3564
            $number = wpinv_get_next_invoice_number( $this->get_post_type() );
3565
        }
3566
3567
		return wpinv_format_invoice_number( $number, $this->get_post_type() );
3568
3569
	}
3570
3571
	/**
3572
	 * Handle the status transition.
3573
	 */
3574
	protected function status_transition() {
3575
		$status_transition = $this->status_transition;
3576
3577
		// Reset status transition variable.
3578
		$this->status_transition = false;
3579
3580
		if ( $status_transition ) {
3581
			try {
3582
3583
				// Fire a hook for the status change.
3584
				do_action( 'getpaid_invoice_status_' . $status_transition['to'], $this, $status_transition );
3585
3586
				// @deprecated this is deprecated and will be removed in the future.
3587
				do_action( 'wpinv_status_' . $status_transition['to'], $this->get_id(), $status_transition['from'] );
3588
3589
				if ( ! empty( $status_transition['from'] ) ) {
3590
3591
					/* translators: 1: old invoice status 2: new invoice status */
3592
					$transition_note = sprintf( __( 'Status changed from %1$s to %2$s.', 'invoicing' ), wpinv_status_nicename( $status_transition['from'], $this ), wpinv_status_nicename( $status_transition['to'], $this  ) );
3593
3594
					// Fire another hook.
3595
					do_action( 'getpaid_invoice_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this );
3596
					do_action( 'getpaid_invoice_status_changed', $this, $status_transition['from'], $status_transition['to'] );
3597
3598
					// @deprecated this is deprecated and will be removed in the future.
3599
					do_action( 'wpinv_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $status_transition['from'] );
3600
3601
					// Note the transition occurred.
3602
					$this->add_note( trim( $status_transition['note'] . ' ' . $transition_note ), false, $status_transition['manual'] );
3603
3604
					// Work out if this was for a payment, and trigger a payment_status hook instead.
3605
					if (
3606
						in_array( $status_transition['from'], array( 'wpi-cancelled', 'wpi-pending', 'wpi-failed', 'wpi-refunded', 'wpi-onhold' ), true )
3607
						&& in_array( $status_transition['to'], array( 'publish', 'wpi-processing', 'wpi-renewal' ), true )
3608
					) {
3609
						do_action( 'getpaid_invoice_payment_status_changed', $this, $status_transition );
3610
					}
3611
3612
					// Work out if this was for a payment reversal, and trigger a payment_status_reversed hook instead.
3613
					if (
3614
						in_array( $status_transition['from'], array( 'publish', 'wpi-processing', 'wpi-renewal' ), true )
3615
						&& in_array( $status_transition['to'], array( 'wpi-cancelled', 'wpi-pending', 'wpi-failed', 'wpi-refunded', 'wpi-onhold' ), true )
3616
					) {
3617
						do_action( 'getpaid_invoice_payment_status_reversed', $this, $status_transition );
3618
					}
3619
				} else {
3620
					/* translators: %s: new invoice status */
3621
					$transition_note = sprintf( __( 'Status set to %s.', 'invoicing' ), wpinv_status_nicename( $status_transition['to'], $this  ) );
3622
3623
					// Note the transition occurred.
3624
					$this->add_note( trim( $status_transition['note'] . ' ' . $transition_note ), 0, $status_transition['manual'] );
3625
3626
				}
3627
			} catch ( Exception $e ) {
3628
				$this->add_note( __( 'Error during status transition.', 'invoicing' ) . ' ' . $e->getMessage() );
3629
			}
3630
		}
3631
	}
3632
3633
	/**
3634
	 * Updates an invoice status.
3635
	 */
3636
	public function update_status( $new_status = false, $note = '', $manual = false ) {
3637
3638
		// Fires before updating a status.
3639
		do_action( 'wpinv_before_invoice_status_change', $this->get_id(), $new_status, $this->get_status( 'edit' ) );
3640
3641
		// Update the status.
3642
		$this->set_status( $new_status, $note, $manual );
3643
3644
		// Save the order.
3645
		return $this->save();
3646
3647
	}
3648
3649
	/**
3650
	 * @deprecated
3651
	 */
3652
	public function refresh_item_ids() {
3653
        $item_ids = implode( ',', array_unique( array_keys( $this->get_items() ) ) );
3654
        update_post_meta( $this->get_id(), '_wpinv_item_ids', $item_ids );
3655
	}
3656
3657
	/**
3658
	 * @deprecated
3659
	 */
3660
	public function update_items( $temp = false ) {
3661
3662
		$this->set_items( $this->get_items() );
3663
3664
		if ( ! $temp ) {
3665
			$this->save();
3666
		}
3667
3668
        return $this;
3669
	}
3670
3671
	/**
3672
	 * @deprecated
3673
	 */
3674
    public function validate_discount() {
3675
3676
        $discount_code = $this->get_discount_code();
3677
3678
        if ( empty( $discount_code ) ) {
3679
            return false;
3680
        }
3681
3682
        $discount = wpinv_get_discount_obj( $discount_code );
3683
3684
        // Ensure it is active.
3685
        return $discount->exists();
3686
3687
    }
3688
3689
	/**
3690
	 * Refunds an invoice.
3691
	 */
3692
    public function refund() {
3693
		$this->set_status( 'wpi-refunded' );
3694
        $this->save();
3695
	}
3696
3697
	/**
3698
	 * Marks an invoice as paid.
3699
	 * 
3700
	 * @param string $transaction_id
3701
	 */
3702
    public function mark_paid( $transaction_id = null, $note = '' ) {
3703
3704
		// Set the transaction id.
3705
		if ( empty( $transaction_id ) ) {
3706
			$transaction_id = $this->generate_key('trans_');
3707
		}
3708
3709
		if ( ! $this->get_transaction_id() ) {
3710
			$this->set_transaction_id( $transaction_id );
3711
		}
3712
3713
		if ( $this->is_paid() && 'wpi-processing' != $this->get_status() ) {
3714
			return $this->save();
3715
		}
3716
3717
		// Set the completed date.
3718
		$this->set_date_completed( current_time( 'mysql' ) );
3719
3720
		// Set the new status.
3721
		if ( $this->is_renewal() ) {
3722
3723
			$_note = sprintf(
3724
				__( 'Renewed via %s', 'invoicing' ),
3725
				$this->get_gateway_title() . empty( $note ) ? '' : " ($note)"
3726
			);
3727
3728
			if ( 'none' == $this->get_gateway() ) {
3729
				$_note = $note;
3730
			}
3731
3732
			$this->set_status( 'wpi-renewal', $_note );
3733
3734
		} else {
3735
3736
			$_note = sprintf(
3737
				__( 'Paid via %s', 'invoicing' ),
3738
				$this->get_gateway_title() . empty( $note ) ? '' : " ($note)"
3739
			);
3740
3741
			if ( 'none' == $this->get_gateway() ) {
3742
				$_note = $note;
3743
			}
3744
3745
			$this->set_status( 'publish',$_note );
3746
3747
		}
3748
3749
		// Set checkout mode.
3750
		$mode = wpinv_is_test_mode( $this->get_gateway() ) ? 'test' : 'live';
3751
		$this->set_mode( $mode );
3752
3753
		// Save the invoice.
3754
        $this->save();
3755
	}
3756
3757
3758
	/**
3759
	 * Save data to the database.
3760
	 *
3761
	 * @since 1.0.19
3762
	 * @return int invoice ID
3763
	 */
3764
	public function save() {
3765
		$this->maybe_set_date_paid();
3766
		$this->maybe_set_key();
3767
		parent::save();
3768
		$this->clear_cache();
3769
		$this->status_transition();
3770
		return $this->get_id();
3771
	}
3772
3773
	/**
3774
     * Clears the subscription's cache.
3775
     */
3776
    public function clear_cache() {
3777
		wp_cache_delete( $this->get_key(), 'getpaid_invoice_keys_to_invoice_ids' );
3778
		wp_cache_delete( $this->get_number(), 'getpaid_invoice_numbers_to_invoice_ids' );
3779
		wp_cache_delete( $this->get_transaction_id(), 'getpaid_invoice_transaction_ids_to_invoice_ids' );
3780
	}
3781
3782
}
3783