Passed
Push — master ( 7c4f48...a0ff4f )
by Brian
05:01
created

WPInv_Invoice::get_shipping_address()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
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
        'shipping'             => null,
69
		'subtotal'             => 0,
70
        'total_discount'       => 0,
71
        'total_tax'            => 0,
72
		'total_fees'           => 0,
73
		'total'                => 0,
74
        'fees'                 => array(),
75
        'discounts'            => array(),
76
        'taxes'                => array(),
77
        'items'                => array(),
78
        'payment_form'         => 1,
79
        'submission_id'        => null,
80
        'discount_code'        => null,
81
        'gateway'              => 'none',
82
        'transaction_id'       => '',
83
        'currency'             => '',
84
        'disable_taxes'        => false,
85
		'subscription_id'      => null,
86
		'remote_subscription_id' => null,
87
		'is_viewed'            => false,
88
		'email_cc'             => '',
89
		'template'             => 'quantity', // hours, amount only
90
		'created_via'          => null,
91
    );
92
93
    /**
94
	 * Stores meta in cache for future reads.
95
	 *
96
	 * A group must be set to to enable caching.
97
	 *
98
	 * @var string
99
	 */
100
	protected $cache_group = 'getpaid_invoices';
101
102
    /**
103
     * Stores a reference to the original WP_Post object
104
     *
105
     * @var WP_Post
106
     */
107
    protected $post = null;
108
109
    /**
110
     * Stores a reference to the recurring item id instead of looping through the items.
111
     *
112
     * @var int
113
     */
114
	protected $recurring_item = null;
115
116
	/**
117
     * Stores an array of item totals.
118
	 *
119
	 * e.g $totals['discount'] = array(
120
	 * 		'initial'   => 10,
121
	 * 		'recurring' => 10,
122
	 * )
123
     *
124
     * @var array
125
     */
126
	protected $totals = array();
127
128
	/**
129
     * Tax rate.
130
	 *
131
     * @var float
132
     */
133
	protected $tax_rate = 0;
134
135
	/**
136
	 * Stores the status transition information.
137
	 *
138
	 * @since 1.0.19
139
	 * @var bool|array
140
	 */
141
	protected $status_transition = false;
142
143
    /**
144
	 * Get the invoice if ID is passed, otherwise the invoice is new and empty.
145
	 *
146
	 * @param  int|string|object|WPInv_Invoice|WPInv_Legacy_Invoice|WP_Post $invoice Invoice id, key, transaction id, number or object to read.
147
	 */
148
    public function __construct( $invoice = 0 ) {
149
150
        parent::__construct( $invoice );
151
152
		if ( ! empty( $invoice ) && is_numeric( $invoice ) && getpaid_is_invoice_post_type( get_post_type( (int) $invoice ) ) ) {
0 ignored issues
show
Bug introduced by
It seems like get_post_type((int)$invoice) can also be of type false; however, parameter $post_type of getpaid_is_invoice_post_type() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

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

3645
				$taxes    = getpaid_calculate_item_taxes( /** @scrutinizer ignore-type */ getpaid_get_taxable_amount( $item, false ), $rates );
Loading history...
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

3645
				$taxes    = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, /** @scrutinizer ignore-type */ false ), $rates );
Loading history...
3646
				$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

3646
				$r_taxes  = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, /** @scrutinizer ignore-type */ true ), $rates );
Loading history...
3647
				foreach ( $taxes as $name => $amount ) {
3648
					$recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0;
3649
					$tax       = getpaid_prepare_item_tax( $item, $name, $amount, $recurring );
3650
3651
					if ( ! isset( $item_taxes[ $name ] ) ) {
3652
						$item_taxes[ $name ] = $tax;
3653
						continue;
3654
					}
3655
3656
					$item_taxes[ $name ]['initial_tax']   += $tax['initial_tax'];
3657
					$item_taxes[ $name ]['recurring_tax'] += $tax['recurring_tax'];
3658
3659
				}
3660
3661
			}
3662
3663
			$item_taxes = array_replace( $this->get_taxes(), $item_taxes );
3664
			$this->set_taxes( $item_taxes );
3665
3666
			$initial_tax   = array_sum( wp_list_pluck( $item_taxes, 'initial_tax' ) );
3667
			$recurring_tax = array_sum( wp_list_pluck( $item_taxes, 'recurring_tax' ) );
3668
3669
			$current = $this->is_renewal() ? $recurring_tax : $initial_tax;
3670
3671
			$this->totals['tax'] = array(
3672
				'initial'   => $initial_tax,
3673
				'recurring' => $recurring_tax,
3674
			);
3675
3676
		}
3677
3678
		$this->set_total_tax( $current );
3679
3680
		return $current;
3681
3682
    }
3683
3684
    /**
3685
	 * Recalculates the invoice fees total.
3686
	 *
3687
	 * @since 1.0.19
3688
	 * @return float The recalculated fee
3689
	 */
3690
	public function recalculate_total_fees() {
3691
		$fees      = $this->get_fees();
3692
		$fee       = 0;
3693
		$recurring = 0;
3694
3695
        foreach ( $fees as $data ) {
3696
			$fee       += wpinv_sanitize_amount( $data['initial_fee'] );
3697
			$recurring += wpinv_sanitize_amount( $data['recurring_fee'] );
3698
		}
3699
3700
		$current = $this->is_renewal() ? $recurring : $fee;
3701
		$this->set_total_fees( $current );
3702
3703
		$this->totals['fee'] = array(
3704
			'initial'   => $fee,
3705
			'recurring' => $recurring,
3706
		);
3707
3708
        $this->set_total_fees( $fee );
3709
        return $current;
3710
    }
3711
3712
    /**
3713
	 * Recalculates the invoice total.
3714
	 *
3715
	 * @since 1.0.19
3716
     * @return float The invoice total
3717
	 */
3718
	public function recalculate_total() {
3719
        $this->recalculate_total_fees();
3720
        $this->recalculate_total_discount();
3721
		$this->recalculate_total_tax();
3722
		$this->recalculate_subtotal();
3723
		$this->set_total( $this->get_total_tax( 'edit' ) + $this->get_total_fees( 'edit' ) + $this->get_subtotal( 'edit' ) - $this->get_total_discount( 'edit' ) );
3724
		return $this->get_total();
3725
	}
3726
3727
	/**
3728
	 * @deprecated
3729
	 */
3730
    public function recalculate_totals() {
3731
        $this->recalculate_total();
3732
        $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

3732
        $this->/** @scrutinizer ignore-call */ 
3733
               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...
3733
        return $this;
3734
    }
3735
3736
    /**
3737
     * Convert this to an array.
3738
     */
3739
    public function array_convert() {
3740
        return $this->get_data();
3741
    }
3742
3743
	/**
3744
     * Adds a system note to an invoice.
3745
     *
3746
     * @param string $note The note being added.
3747
	 * @return int|false The new note's ID on success, false on failure.
3748
     *
3749
     */
3750
    public function add_system_note( $note ) {
3751
		return $this->add_note( $note, false, false, true );
3752
	}
3753
3754
    /**
3755
     * Adds a note to an invoice.
3756
     *
3757
     * @param string $note The note being added.
3758
	 * @return int|false The new note's ID on success, false on failure.
3759
     *
3760
     */
3761
    public function add_note( $note = '', $customer_type = false, $added_by_user = false, $system = false ) {
3762
3763
        // Bail if no note specified or this invoice is not yet saved.
3764
        if ( ! $note || $this->get_id() == 0 || ( ! is_user_logged_in() && ! $system ) ) {
3765
            return false;
3766
        }
3767
3768
		$author       = 'System';
3769
		$author_email = '[email protected]';
3770
3771
		// If this is an admin comment or it has been added by the user.
3772
		if ( is_user_logged_in() && ( ! $system || $added_by_user ) ) {
3773
			$user         = get_user_by( 'id', get_current_user_id() );
3774
            $author       = $user->display_name;
3775
            $author_email = $user->user_email;
3776
		}
3777
3778
		return getpaid_notes()->add_invoice_note( $this, $note, $author, $author_email, $customer_type );
3779
3780
	}
3781
3782
	/**
3783
     * Generates a unique key for the invoice.
3784
     */
3785
    public function generate_key( $string = '' ) {
3786
        $auth_key  = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
3787
        return strtolower(
3788
            $string . md5( $this->get_id() . date( 'Y-m-d H:i:s' ) . $auth_key . uniqid( 'wpinv', true ) )
3789
        );
3790
    }
3791
3792
    /**
3793
     * Generates a new number for the invoice.
3794
     */
3795
    public function generate_number() {
3796
        $number = $this->get_id();
3797
3798
        if ( wpinv_sequential_number_active( $this->get_post_type() ) ) {
3799
            $number = wpinv_get_next_invoice_number( $this->get_post_type() );
3800
        }
3801
3802
		return wpinv_format_invoice_number( $number, $this->get_post_type() );
3803
3804
	}
3805
3806
	/**
3807
	 * Handle the status transition.
3808
	 */
3809
	protected function status_transition() {
3810
		$status_transition = $this->status_transition;
3811
3812
		// Reset status transition variable.
3813
		$this->status_transition = false;
3814
3815
		if ( $status_transition ) {
3816
			try {
3817
3818
				// Fire a hook for the status change.
3819
				do_action( 'getpaid_invoice_status_' . $status_transition['to'], $this, $status_transition );
3820
3821
				// @deprecated this is deprecated and will be removed in the future.
3822
				do_action( 'wpinv_status_' . $status_transition['to'], $this->get_id(), $status_transition['from'] );
3823
3824
				if ( ! empty( $status_transition['from'] ) ) {
3825
3826
					/* translators: 1: old invoice status 2: new invoice status */
3827
					$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  ) );
3828
3829
					// Fire another hook.
3830
					do_action( 'getpaid_invoice_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this );
3831
					do_action( 'getpaid_invoice_status_changed', $this, $status_transition['from'], $status_transition['to'] );
3832
3833
					// @deprecated this is deprecated and will be removed in the future.
3834
					do_action( 'wpinv_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $status_transition['from'] );
3835
3836
					// Note the transition occurred.
3837
					$this->add_note( trim( $status_transition['note'] . ' ' . $transition_note ), false, $status_transition['manual'] );
3838
3839
					// Work out if this was for a payment, and trigger a payment_status hook instead.
3840
					if (
3841
						in_array( $status_transition['from'], array( 'wpi-cancelled', 'wpi-pending', 'wpi-failed', 'wpi-refunded', 'wpi-onhold' ), true )
3842
						&& in_array( $status_transition['to'], array( 'publish', 'wpi-processing', 'wpi-renewal' ), true )
3843
					) {
3844
						do_action( 'getpaid_invoice_payment_status_changed', $this, $status_transition );
3845
					}
3846
3847
					// Work out if this was for a payment reversal, and trigger a payment_status_reversed hook instead.
3848
					if (
3849
						in_array( $status_transition['from'], array( 'publish', 'wpi-processing', 'wpi-renewal' ), true )
3850
						&& in_array( $status_transition['to'], array( 'wpi-cancelled', 'wpi-pending', 'wpi-failed', 'wpi-refunded', 'wpi-onhold' ), true )
3851
					) {
3852
						do_action( 'getpaid_invoice_payment_status_reversed', $this, $status_transition );
3853
					}
3854
				} else {
3855
					/* translators: %s: new invoice status */
3856
					$transition_note = sprintf( __( 'Status set to %s.', 'invoicing' ), wpinv_status_nicename( $status_transition['to'], $this  ) );
3857
3858
					// Note the transition occurred.
3859
					$this->add_note( trim( $status_transition['note'] . ' ' . $transition_note ), 0, $status_transition['manual'] );
3860
3861
				}
3862
			} catch ( Exception $e ) {
3863
				$this->add_note( __( 'Error during status transition.', 'invoicing' ) . ' ' . $e->getMessage() );
3864
			}
3865
		}
3866
	}
3867
3868
	/**
3869
	 * Updates an invoice status.
3870
	 */
3871
	public function update_status( $new_status = false, $note = '', $manual = false ) {
3872
3873
		// Fires before updating a status.
3874
		do_action( 'wpinv_before_invoice_status_change', $this->get_id(), $new_status, $this->get_status( 'edit' ) );
3875
3876
		// Update the status.
3877
		$this->set_status( $new_status, $note, $manual );
3878
3879
		// Save the order.
3880
		return $this->save();
3881
3882
	}
3883
3884
	/**
3885
	 * @deprecated
3886
	 */
3887
	public function refresh_item_ids() {
3888
        $item_ids = implode( ',', array_unique( wp_list_pluck( $this->get_cart_details(), 'item_id' ) ) );
3889
        update_post_meta( $this->get_id(), '_wpinv_item_ids', $item_ids );
3890
	}
3891
3892
	/**
3893
	 * @deprecated
3894
	 */
3895
	public function update_items( $temp = false ) {
3896
3897
		$this->set_items( $this->get_items() );
3898
3899
		if ( ! $temp ) {
3900
			$this->save();
3901
		}
3902
3903
        return $this;
3904
	}
3905
3906
	/**
3907
	 * @deprecated
3908
	 */
3909
    public function validate_discount() {
3910
3911
        $discount_code = $this->get_discount_code();
3912
3913
        if ( empty( $discount_code ) ) {
3914
            return false;
3915
        }
3916
3917
        $discount = wpinv_get_discount_obj( $discount_code );
3918
3919
        // Ensure it is active.
3920
        return $discount->exists();
3921
3922
    }
3923
3924
	/**
3925
	 * Refunds an invoice.
3926
	 */
3927
    public function refund() {
3928
		$this->set_status( 'wpi-refunded' );
3929
        $this->save();
3930
	}
3931
3932
	/**
3933
	 * Marks an invoice as paid.
3934
	 * 
3935
	 * @param string $transaction_id
3936
	 */
3937
    public function mark_paid( $transaction_id = null, $note = '' ) {
3938
3939
		// Set the transaction id.
3940
		if ( empty( $transaction_id ) ) {
3941
			$transaction_id = $this->generate_key('trans_');
3942
		}
3943
3944
		if ( ! $this->get_transaction_id() ) {
3945
			$this->set_transaction_id( $transaction_id );
3946
		}
3947
3948
		if ( $this->is_paid() && 'wpi-processing' != $this->get_status() ) {
3949
			return $this->save();
3950
		}
3951
3952
		// Set the completed date.
3953
		$this->set_date_completed( current_time( 'mysql' ) );
3954
3955
		// Set the new status.
3956
		$gateway = sanitize_text_field( $this->get_gateway_title() );
3957
		if ( $this->is_renewal() || ! $this->is_parent() ) {
3958
3959
			$_note = wp_sprintf( __( 'Renewed via %s', 'invoicing' ), $gateway );
3960
			$_note = $_note . empty( $note ) ? '' : " ($note)";
3961
3962
			if ( 'none' == $this->get_gateway() ) {
3963
				$_note = $note;
3964
			}
3965
3966
			$this->set_status( 'wpi-renewal', $_note );
3967
3968
		} else {
3969
3970
			$_note = wp_sprintf( __( 'Paid via %s', 'invoicing' ), $gateway );
3971
			$_note = $_note . empty( $note ) ? '' : " ($note)";
3972
3973
			if ( 'none' == $this->get_gateway() ) {
3974
				$_note = $note;
3975
			}
3976
3977
			$this->set_status( 'publish', $_note );
3978
3979
		}
3980
3981
		// Set checkout mode.
3982
		$mode = wpinv_is_test_mode( $this->get_gateway() ) ? 'test' : 'live';
3983
		$this->set_mode( $mode );
3984
3985
		// Save the invoice.
3986
        $this->save();
3987
	}
3988
3989
	/**
3990
	 * Save data to the database.
3991
	 *
3992
	 * @since 1.0.19
3993
	 * @return int invoice ID
3994
	 */
3995
	public function save() {
3996
		$this->maybe_set_date_paid();
3997
		$this->maybe_set_key();
3998
		parent::save();
3999
		$this->clear_cache();
4000
		$this->status_transition();
4001
		return $this->get_id();
4002
	}
4003
4004
	/**
4005
     * Clears the subscription's cache.
4006
     */
4007
    public function clear_cache() {
4008
		wp_cache_delete( $this->get_key(), 'getpaid_invoice_keys_to_invoice_ids' );
4009
		wp_cache_delete( $this->get_number(), 'getpaid_invoice_numbers_to_invoice_ids' );
4010
		wp_cache_delete( $this->get_transaction_id(), 'getpaid_invoice_transaction_ids_to_invoice_ids' );
4011
	}
4012
4013
}
4014