Passed
Push — master ( 10624e...03e582 )
by Brian
04:27
created

WPInv_Invoice::add_system_note()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
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
		'company_id'           => null,
64
        'vat_number'           => null,
65
        'vat_rate'             => null,
66
        'address'              => null,
67
        'address_confirmed'    => false,
68
        'subtotal'             => 0,
69
        'total_discount'       => 0,
70
        'total_tax'            => 0,
71
		'total_fees'           => 0,
72
		'total'                => 0,
73
        'fees'                 => array(),
74
        'discounts'            => array(),
75
        'taxes'                => array(),
76
        'items'                => array(),
77
        'payment_form'         => 1,
78
        'submission_id'        => null,
79
        'discount_code'        => null,
80
        'gateway'              => 'none',
81
        'transaction_id'       => '',
82
        'currency'             => '',
83
        'disable_taxes'        => false,
84
		'subscription_id'      => null,
85
		'remote_subscription_id' => null,
86
		'is_viewed'            => false,
87
		'email_cc'             => '',
88
		'template'             => 'quantity', // hours, amount only
89
		'created_via'          => null,
90
    );
91
92
    /**
93
	 * Stores meta in cache for future reads.
94
	 *
95
	 * A group must be set to to enable caching.
96
	 *
97
	 * @var string
98
	 */
99
	protected $cache_group = 'getpaid_invoices';
100
101
    /**
102
     * Stores a reference to the original WP_Post object
103
     *
104
     * @var WP_Post
105
     */
106
    protected $post = null;
107
108
    /**
109
     * Stores a reference to the recurring item id instead of looping through the items.
110
     *
111
     * @var int
112
     */
113
	protected $recurring_item = null;
114
115
	/**
116
     * Stores an array of item totals.
117
	 *
118
	 * e.g $totals['discount'] = array(
119
	 * 		'initial'   => 10,
120
	 * 		'recurring' => 10,
121
	 * )
122
     *
123
     * @var array
124
     */
125
	protected $totals = array();
126
127
	/**
128
     * Tax rate.
129
	 *
130
     * @var float
131
     */
132
	protected $tax_rate = 0;
133
134
	/**
135
	 * Stores the status transition information.
136
	 *
137
	 * @since 1.0.19
138
	 * @var bool|array
139
	 */
140
	protected $status_transition = false;
141
142
    /**
143
	 * Get the invoice if ID is passed, otherwise the invoice is new and empty.
144
	 *
145
	 * @param  int|string|object|WPInv_Invoice|WPInv_Legacy_Invoice|WP_Post $invoice Invoice id, key, transaction id, number or object to read.
146
	 */
147
    public function __construct( $invoice = 0 ) {
148
149
        parent::__construct( $invoice );
150
151
		if ( ! empty( $invoice ) && is_numeric( $invoice ) && getpaid_is_invoice_post_type( get_post_type( (int) $invoice ) ) ) {
152
			$this->set_id( (int) $invoice );
153
		} elseif ( $invoice instanceof self ) {
154
			$this->set_id( $invoice->get_id() );
155
		} elseif ( ! empty( $invoice->ID ) ) {
156
			$this->set_id( $invoice->ID );
157
		} elseif ( is_array( $invoice ) ) {
158
			$this->set_props( $invoice );
159
160
			if ( isset( $invoice['ID'] ) ) {
161
				$this->set_id( $invoice['ID'] );
162
			}
163
164
		} elseif ( is_string( $invoice ) && $invoice_id = self::get_invoice_id_by_field( $invoice, 'key' ) ) {
165
			$this->set_id( $invoice_id );
166
		} elseif ( is_string( $invoice ) && $invoice_id = self::get_invoice_id_by_field( $invoice, 'number' ) ) {
167
			$this->set_id( $invoice_id );
168
		} elseif ( is_string( $invoice ) && $invoice_id = self::get_invoice_id_by_field( $invoice, 'transaction_id' ) ) {
169
			$this->set_id( $invoice_id );
170
		} else {
171
			$this->set_object_read( true );
172
		}
173
174
        // Load the datastore.
175
		$this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
176
177
		if ( $this->get_id() > 0 ) {
178
            $this->post = get_post( $this->get_id() );
0 ignored issues
show
Documentation Bug introduced by
It seems like get_post($this->get_id()) can also be of type array. However, the property $post is declared as type WP_Post. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

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

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

3580
				$taxes    = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, /** @scrutinizer ignore-type */ false ), $rates );
Loading history...
Bug introduced by
getpaid_get_taxable_amount($item, false) of type string is incompatible with the type double expected by parameter $amount of getpaid_calculate_item_taxes(). ( Ignorable by Annotation )

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

3580
				$taxes    = getpaid_calculate_item_taxes( /** @scrutinizer ignore-type */ getpaid_get_taxable_amount( $item, false ), $rates );
Loading history...
3581
				$r_taxes  = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, true ), $rates );
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $recurring of getpaid_get_taxable_amount(). ( Ignorable by Annotation )

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

3581
				$r_taxes  = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, /** @scrutinizer ignore-type */ true ), $rates );
Loading history...
3582
				foreach ( $taxes as $name => $amount ) {
3583
					$recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0;
3584
					$tax       = getpaid_prepare_item_tax( $item, $name, $amount, $recurring );
3585
3586
					if ( ! isset( $item_taxes[ $name ] ) ) {
3587
						$item_taxes[ $name ] = $tax;
3588
						continue;
3589
					}
3590
3591
					$item_taxes[ $name ]['initial_tax']   += $tax['initial_tax'];
3592
					$item_taxes[ $name ]['recurring_tax'] += $tax['recurring_tax'];
3593
3594
				}
3595
3596
			}
3597
3598
			$item_taxes = array_replace( $this->get_taxes(), $item_taxes );
3599
			$this->set_taxes( $item_taxes );
3600
3601
			$initial_tax   = array_sum( wp_list_pluck( $item_taxes, 'initial_tax' ) );
3602
			$recurring_tax = array_sum( wp_list_pluck( $item_taxes, 'recurring_tax' ) );
3603
3604
			$current = $this->is_renewal() ? $recurring_tax : $initial_tax;
3605
3606
			$this->totals['tax'] = array(
3607
				'initial'   => $initial_tax,
3608
				'recurring' => $recurring_tax,
3609
			);
3610
3611
		}
3612
3613
		$this->set_total_tax( $current );
3614
3615
		return $current;
3616
3617
    }
3618
3619
    /**
3620
	 * Recalculates the invoice fees total.
3621
	 *
3622
	 * @since 1.0.19
3623
	 * @return float The recalculated fee
3624
	 */
3625
	public function recalculate_total_fees() {
3626
		$fees      = $this->get_fees();
3627
		$fee       = 0;
3628
		$recurring = 0;
3629
3630
        foreach ( $fees as $data ) {
3631
			$fee       += wpinv_sanitize_amount( $data['initial_fee'] );
3632
			$recurring += wpinv_sanitize_amount( $data['recurring_fee'] );
3633
		}
3634
3635
		$current = $this->is_renewal() ? $recurring : $fee;
3636
		$this->set_total_fees( $current );
3637
3638
		$this->totals['fee'] = array(
3639
			'initial'   => $fee,
3640
			'recurring' => $recurring,
3641
		);
3642
3643
        $this->set_total_fees( $fee );
3644
        return $current;
3645
    }
3646
3647
    /**
3648
	 * Recalculates the invoice total.
3649
	 *
3650
	 * @since 1.0.19
3651
     * @return float The invoice total
3652
	 */
3653
	public function recalculate_total() {
3654
        $this->recalculate_total_fees();
3655
        $this->recalculate_total_discount();
3656
		$this->recalculate_total_tax();
3657
		$this->recalculate_subtotal();
3658
		$this->set_total( $this->get_total_tax() + $this->get_total_fees() + $this->get_subtotal() - $this->get_total_discount() );
3659
		return $this->get_total();
3660
	}
3661
3662
	/**
3663
	 * @deprecated
3664
	 */
3665
    public function recalculate_totals() {
3666
        $this->recalculate_total();
3667
        $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

3667
        $this->/** @scrutinizer ignore-call */ 
3668
               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...
3668
        return $this;
3669
    }
3670
3671
    /**
3672
     * Convert this to an array.
3673
     */
3674
    public function array_convert() {
3675
        return $this->get_data();
3676
    }
3677
3678
	/**
3679
     * Adds a system note to an invoice.
3680
     *
3681
     * @param string $note The note being added.
3682
	 * @return int|false The new note's ID on success, false on failure.
3683
     *
3684
     */
3685
    public function add_system_note( $note ) {
3686
		return $this->add_note( $note, false, false, true );
3687
	}
3688
3689
    /**
3690
     * Adds a note to an invoice.
3691
     *
3692
     * @param string $note The note being added.
3693
	 * @return int|false The new note's ID on success, false on failure.
3694
     *
3695
     */
3696
    public function add_note( $note = '', $customer_type = false, $added_by_user = false, $system = false ) {
3697
3698
        // Bail if no note specified or this invoice is not yet saved.
3699
        if ( ! $note || $this->get_id() == 0 || ( ! is_user_logged_in() && ! $system ) ) {
3700
            return false;
3701
        }
3702
3703
		$author       = 'System';
3704
		$author_email = '[email protected]';
3705
3706
		// If this is an admin comment or it has been added by the user.
3707
		if ( is_user_logged_in() && ( ! $system || $added_by_user ) ) {
3708
			$user         = get_user_by( 'id', get_current_user_id() );
3709
            $author       = $user->display_name;
3710
            $author_email = $user->user_email;
3711
		}
3712
3713
		return getpaid_notes()->add_invoice_note( $this, $note, $author, $author_email, $customer_type );
3714
3715
	}
3716
3717
	/**
3718
     * Generates a unique key for the invoice.
3719
     */
3720
    public function generate_key( $string = '' ) {
3721
        $auth_key  = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
3722
        return strtolower(
3723
            $string . md5( $this->get_id() . date( 'Y-m-d H:i:s' ) . $auth_key . uniqid( 'wpinv', true ) )
3724
        );
3725
    }
3726
3727
    /**
3728
     * Generates a new number for the invoice.
3729
     */
3730
    public function generate_number() {
3731
        $number = $this->get_id();
3732
3733
        if ( wpinv_sequential_number_active( $this->get_post_type() ) ) {
3734
            $number = wpinv_get_next_invoice_number( $this->get_post_type() );
3735
        }
3736
3737
		return wpinv_format_invoice_number( $number, $this->get_post_type() );
3738
3739
	}
3740
3741
	/**
3742
	 * Handle the status transition.
3743
	 */
3744
	protected function status_transition() {
3745
		$status_transition = $this->status_transition;
3746
3747
		// Reset status transition variable.
3748
		$this->status_transition = false;
3749
3750
		if ( $status_transition ) {
3751
			try {
3752
3753
				// Fire a hook for the status change.
3754
				do_action( 'getpaid_invoice_status_' . $status_transition['to'], $this, $status_transition );
3755
3756
				// @deprecated this is deprecated and will be removed in the future.
3757
				do_action( 'wpinv_status_' . $status_transition['to'], $this->get_id(), $status_transition['from'] );
3758
3759
				if ( ! empty( $status_transition['from'] ) ) {
3760
3761
					/* translators: 1: old invoice status 2: new invoice status */
3762
					$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  ) );
3763
3764
					// Fire another hook.
3765
					do_action( 'getpaid_invoice_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this );
3766
					do_action( 'getpaid_invoice_status_changed', $this, $status_transition['from'], $status_transition['to'] );
3767
3768
					// @deprecated this is deprecated and will be removed in the future.
3769
					do_action( 'wpinv_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $status_transition['from'] );
3770
3771
					// Note the transition occurred.
3772
					$this->add_note( trim( $status_transition['note'] . ' ' . $transition_note ), false, $status_transition['manual'] );
3773
3774
					// Work out if this was for a payment, and trigger a payment_status hook instead.
3775
					if (
3776
						in_array( $status_transition['from'], array( 'wpi-cancelled', 'wpi-pending', 'wpi-failed', 'wpi-refunded', 'wpi-onhold' ), true )
3777
						&& in_array( $status_transition['to'], array( 'publish', 'wpi-processing', 'wpi-renewal' ), true )
3778
					) {
3779
						do_action( 'getpaid_invoice_payment_status_changed', $this, $status_transition );
3780
					}
3781
3782
					// Work out if this was for a payment reversal, and trigger a payment_status_reversed hook instead.
3783
					if (
3784
						in_array( $status_transition['from'], array( 'publish', 'wpi-processing', 'wpi-renewal' ), true )
3785
						&& in_array( $status_transition['to'], array( 'wpi-cancelled', 'wpi-pending', 'wpi-failed', 'wpi-refunded', 'wpi-onhold' ), true )
3786
					) {
3787
						do_action( 'getpaid_invoice_payment_status_reversed', $this, $status_transition );
3788
					}
3789
				} else {
3790
					/* translators: %s: new invoice status */
3791
					$transition_note = sprintf( __( 'Status set to %s.', 'invoicing' ), wpinv_status_nicename( $status_transition['to'], $this  ) );
3792
3793
					// Note the transition occurred.
3794
					$this->add_note( trim( $status_transition['note'] . ' ' . $transition_note ), 0, $status_transition['manual'] );
3795
3796
				}
3797
			} catch ( Exception $e ) {
3798
				$this->add_note( __( 'Error during status transition.', 'invoicing' ) . ' ' . $e->getMessage() );
3799
			}
3800
		}
3801
	}
3802
3803
	/**
3804
	 * Updates an invoice status.
3805
	 */
3806
	public function update_status( $new_status = false, $note = '', $manual = false ) {
3807
3808
		// Fires before updating a status.
3809
		do_action( 'wpinv_before_invoice_status_change', $this->get_id(), $new_status, $this->get_status( 'edit' ) );
3810
3811
		// Update the status.
3812
		$this->set_status( $new_status, $note, $manual );
3813
3814
		// Save the order.
3815
		return $this->save();
3816
3817
	}
3818
3819
	/**
3820
	 * @deprecated
3821
	 */
3822
	public function refresh_item_ids() {
3823
        $item_ids = implode( ',', array_unique( wp_list_pluck( $this->get_cart_details(), 'item_id' ) ) );
3824
        update_post_meta( $this->get_id(), '_wpinv_item_ids', $item_ids );
3825
	}
3826
3827
	/**
3828
	 * @deprecated
3829
	 */
3830
	public function update_items( $temp = false ) {
3831
3832
		$this->set_items( $this->get_items() );
3833
3834
		if ( ! $temp ) {
3835
			$this->save();
3836
		}
3837
3838
        return $this;
3839
	}
3840
3841
	/**
3842
	 * @deprecated
3843
	 */
3844
    public function validate_discount() {
3845
3846
        $discount_code = $this->get_discount_code();
3847
3848
        if ( empty( $discount_code ) ) {
3849
            return false;
3850
        }
3851
3852
        $discount = wpinv_get_discount_obj( $discount_code );
3853
3854
        // Ensure it is active.
3855
        return $discount->exists();
3856
3857
    }
3858
3859
	/**
3860
	 * Refunds an invoice.
3861
	 */
3862
    public function refund() {
3863
		$this->set_status( 'wpi-refunded' );
3864
        $this->save();
3865
	}
3866
3867
	/**
3868
	 * Marks an invoice as paid.
3869
	 * 
3870
	 * @param string $transaction_id
3871
	 */
3872
    public function mark_paid( $transaction_id = null, $note = '' ) {
3873
3874
		// Set the transaction id.
3875
		if ( empty( $transaction_id ) ) {
3876
			$transaction_id = $this->generate_key('trans_');
3877
		}
3878
3879
		if ( ! $this->get_transaction_id() ) {
3880
			$this->set_transaction_id( $transaction_id );
3881
		}
3882
3883
		if ( $this->is_paid() && 'wpi-processing' != $this->get_status() ) {
3884
			return $this->save();
3885
		}
3886
3887
		// Set the completed date.
3888
		$this->set_date_completed( current_time( 'mysql' ) );
3889
3890
		// Set the new status.
3891
		$gateway = sanitize_text_field( $this->get_gateway_title() );
3892
		if ( $this->is_renewal() || ! $this->is_parent() ) {
3893
3894
			$_note = wp_sprintf( __( 'Renewed via %s', 'invoicing' ), $gateway );
3895
			$_note = $_note . empty( $note ) ? '' : " ($note)";
3896
3897
			if ( 'none' == $this->get_gateway() ) {
3898
				$_note = $note;
3899
			}
3900
3901
			$this->set_status( 'wpi-renewal', $_note );
3902
3903
		} else {
3904
3905
			$_note = wp_sprintf( __( 'Paid via %s', 'invoicing' ), $gateway );
3906
			$_note = $_note . empty( $note ) ? '' : " ($note)";
3907
3908
			if ( 'none' == $this->get_gateway() ) {
3909
				$_note = $note;
3910
			}
3911
3912
			$this->set_status( 'publish', $_note );
3913
3914
		}
3915
3916
		// Set checkout mode.
3917
		$mode = wpinv_is_test_mode( $this->get_gateway() ) ? 'test' : 'live';
3918
		$this->set_mode( $mode );
3919
3920
		// Save the invoice.
3921
        $this->save();
3922
	}
3923
3924
	/**
3925
	 * Save data to the database.
3926
	 *
3927
	 * @since 1.0.19
3928
	 * @return int invoice ID
3929
	 */
3930
	public function save() {
3931
		$this->maybe_set_date_paid();
3932
		$this->maybe_set_key();
3933
		parent::save();
3934
		$this->clear_cache();
3935
		$this->status_transition();
3936
		return $this->get_id();
3937
	}
3938
3939
	/**
3940
     * Clears the subscription's cache.
3941
     */
3942
    public function clear_cache() {
3943
		wp_cache_delete( $this->get_key(), 'getpaid_invoice_keys_to_invoice_ids' );
3944
		wp_cache_delete( $this->get_number(), 'getpaid_invoice_numbers_to_invoice_ids' );
3945
		wp_cache_delete( $this->get_transaction_id(), 'getpaid_invoice_transaction_ids_to_invoice_ids' );
3946
	}
3947
3948
}
3949