Passed
Push — master ( 44aa87...37ef0b )
by Brian
04:04
created

WPInv_Invoice::recalculate_total_tax()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 65
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 31
nc 3
nop 0
dl 0
loc 65
rs 8.1795
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Contains the invoice class.
4
 *
5
 * @since 1.0.19
6
 * @package Invoicing
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Invoice class.
13
 */
14
class WPInv_Invoice extends GetPaid_Data {
15
16
    /**
17
	 * Which data store to load.
18
	 *
19
	 * @var string
20
	 */
21
    protected $data_store_name = 'invoice';
22
23
    /**
24
	 * This is the name of this object type.
25
	 *
26
	 * @var string
27
	 */
28
    protected $object_type = 'invoice';
29
30
    /**
31
	 * Item Data array. This is the core item data exposed in APIs.
32
	 *
33
	 * @since 1.0.19
34
	 * @var array
35
	 */
36
	protected $data = array(
37
		'parent_id'            => 0,
38
		'status'               => 'wpi-pending',
39
		'version'              => '',
40
		'date_created'         => null,
41
        'date_modified'        => null,
42
        'due_date'             => null,
43
        'completed_date'       => null,
44
        'number'               => '',
45
        'title'                => '',
46
        'path'                 => '',
47
        'key'                  => '',
48
        'description'          => '',
49
        'author'               => 1,
50
        'type'                 => 'invoice',
51
        'post_type'            => 'wpi_invoice',
52
        'mode'                 => 'live',
53
        'user_ip'              => null,
54
        'first_name'           => null,
55
        'last_name'            => null,
56
        'phone'                => null,
57
        'email'                => null,
58
        'country'              => null,
59
        'city'                 => null,
60
        'state'                => null,
61
        'zip'                  => null,
62
        'company'              => null,
63
        'vat_number'           => null,
64
        'vat_rate'             => null,
65
        'address'              => null,
66
        'address_confirmed'    => false,
67
        'subtotal'             => 0,
68
        'total_discount'       => 0,
69
        'total_tax'            => 0,
70
        'total_fees'           => 0,
71
        'fees'                 => array(),
72
        'discounts'            => array(),
73
        'taxes'                => array(),
74
        'items'                => array(),
75
        'payment_form'         => 1,
76
        'submission_id'        => null,
77
        'discount_code'        => null,
78
        'gateway'              => 'none',
79
        'transaction_id'       => '',
80
        'currency'             => '',
81
        'disable_taxes'        => false,
82
		'subscription_id'      => null,
83
		'remote_subscription_id' => null,
84
		'is_viewed'            => false,
85
		'email_cc'             => '',
86
		'template'             => 'quantity', // hours, amount only
87
		'created_via'          => null,
88
    );
89
90
    /**
91
	 * Stores meta in cache for future reads.
92
	 *
93
	 * A group must be set to to enable caching.
94
	 *
95
	 * @var string
96
	 */
97
	protected $cache_group = 'getpaid_invoices';
98
99
    /**
100
     * Stores a reference to the original WP_Post object
101
     *
102
     * @var WP_Post
103
     */
104
    protected $post = null;
105
106
    /**
107
     * Stores a reference to the recurring item id instead of looping through the items.
108
     *
109
     * @var int
110
     */
111
	protected $recurring_item = null;
112
113
	/**
114
     * Stores an array of item totals.
115
	 *
116
	 * e.g $totals['discount'] = array(
117
	 * 		'initial'   => 10,
118
	 * 		'recurring' => 10,
119
	 * )
120
     *
121
     * @var array
122
     */
123
	protected $totals = array();
124
125
	/**
126
	 * Stores the status transition information.
127
	 *
128
	 * @since 1.0.19
129
	 * @var bool|array
130
	 */
131
	protected $status_transition = false;
132
133
    /**
134
	 * Get the invoice if ID is passed, otherwise the invoice is new and empty.
135
	 *
136
	 * @param  int|string|object|WPInv_Invoice|WPInv_Legacy_Invoice|WP_Post $invoice Invoice id, key, transaction id, number or object to read.
137
	 */
138
    public function __construct( $invoice = 0 ) {
139
140
        parent::__construct( $invoice );
141
142
		if ( ! empty( $invoice ) && is_numeric( $invoice ) && getpaid_is_invoice_post_type( get_post_type( (int) $invoice ) ) ) {
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

142
		if ( ! empty( $invoice ) && is_numeric( $invoice ) && getpaid_is_invoice_post_type( /** @scrutinizer ignore-type */ get_post_type( (int) $invoice ) ) ) {
Loading history...
143
			$this->set_id( (int) $invoice );
144
		} elseif ( $invoice instanceof self ) {
145
			$this->set_id( $invoice->get_id() );
146
		} elseif ( ! empty( $invoice->ID ) ) {
147
			$this->set_id( $invoice->ID );
148
		} elseif ( is_array( $invoice ) ) {
149
			$this->set_props( $invoice );
150
151
			if ( isset( $invoice['ID'] ) ) {
152
				$this->set_id( $invoice['ID'] );
153
			}
154
155
		} elseif ( is_string( $invoice ) && $invoice_id = self::get_invoice_id_by_field( $invoice, 'key' ) ) {
156
			$this->set_id( $invoice_id );
157
		} elseif ( is_string( $invoice ) && $invoice_id = self::get_invoice_id_by_field( $invoice, 'number' ) ) {
158
			$this->set_id( $invoice_id );
159
		} elseif ( is_string( $invoice ) && $invoice_id = self::get_invoice_id_by_field( $invoice, 'transaction_id' ) ) {
160
			$this->set_id( $invoice_id );
161
		}else {
162
			$this->set_object_read( true );
163
		}
164
165
        // Load the datastore.
166
		$this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
167
168
		if ( $this->get_id() > 0 ) {
169
            $this->post = get_post( $this->get_id() );
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...
170
            $this->ID   = $this->get_id();
0 ignored issues
show
Bug Best Practice introduced by
The property ID does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
171
			$this->data_store->read( $this );
172
        }
173
174
    }
175
176
    /**
177
	 * Given an invoice key/number, it returns its id.
178
	 *
179
	 *
180
	 * @static
181
	 * @param string $value The invoice key or number
182
	 * @param string $field Either key, transaction_id or number.
183
	 * @since 1.0.15
184
	 * @return int
185
	 */
186
	public static function get_invoice_id_by_field( $value, $field = 'key' ) {
187
        global $wpdb;
188
189
		// Trim the value.
190
		$value = trim( $value );
191
192
		if ( empty( $value ) ) {
193
			return 0;
194
		}
195
196
        // Valid fields.
197
        $fields = array( 'key', 'number', 'transaction_id' );
198
199
		// Ensure a field has been passed.
200
		if ( empty( $field ) || ! in_array( $field, $fields ) ) {
201
			return 0;
202
		}
203
204
		// Maybe retrieve from the cache.
205
		$invoice_id   = wp_cache_get( $value, "getpaid_invoice_{$field}s_to_invoice_ids" );
206
		if ( false !== $invoice_id ) {
207
			return $invoice_id;
208
		}
209
210
        // Fetch from the db.
211
        $table       = $wpdb->prefix . 'getpaid_invoices';
212
        $invoice_id  = (int) $wpdb->get_var(
213
            $wpdb->prepare( "SELECT `post_id` FROM $table WHERE `$field`=%s LIMIT 1", $value )
214
        );
215
216
		// Update the cache with our data
217
		wp_cache_set( $value, $invoice_id, "getpaid_invoice_{$field}s_to_invoice_ids" );
218
219
		return $invoice_id;
220
    }
221
222
    /**
223
     * Checks if an invoice key is set.
224
     */
225
    public function _isset( $key ) {
226
        return isset( $this->data[$key] ) || method_exists( $this, "get_$key" );
227
    }
228
229
    /*
230
	|--------------------------------------------------------------------------
231
	| CRUD methods
232
	|--------------------------------------------------------------------------
233
	|
234
	| Methods which create, read, update and delete items from the database.
235
	|
236
    */
237
238
    /*
239
	|--------------------------------------------------------------------------
240
	| Getters
241
	|--------------------------------------------------------------------------
242
    */
243
244
    /**
245
	 * Get parent invoice ID.
246
	 *
247
	 * @since 1.0.19
248
	 * @param  string $context View or edit context.
249
	 * @return int
250
	 */
251
	public function get_parent_id( $context = 'view' ) {
252
		return (int) $this->get_prop( 'parent_id', $context );
253
    }
254
255
    /**
256
	 * Get parent invoice.
257
	 *
258
	 * @since 1.0.19
259
	 * @return WPInv_Invoice
260
	 */
261
    public function get_parent_payment() {
262
        return new WPInv_Invoice( $this->get_parent_id() );
263
    }
264
265
    /**
266
	 * Alias for self::get_parent_payment().
267
	 *
268
	 * @since 1.0.19
269
	 * @return WPInv_Invoice
270
	 */
271
    public function get_parent() {
272
        return $this->get_parent_payment();
273
    }
274
275
    /**
276
	 * Get invoice status.
277
	 *
278
	 * @since 1.0.19
279
	 * @param  string $context View or edit context.
280
	 * @return string
281
	 */
282
	public function get_status( $context = 'view' ) {
283
		return $this->get_prop( 'status', $context );
284
	}
285
	
286
	/**
287
	 * Retrieves an array of possible invoice statuses.
288
	 *
289
	 * @since 1.0.19
290
	 * @return array
291
	 */
292
	public function get_all_statuses() {
293
		return wpinv_get_invoice_statuses( true, true, $this );
294
    }
295
296
    /**
297
	 * Get invoice status nice name.
298
	 *
299
	 * @since 1.0.19
300
	 * @return string
301
	 */
302
    public function get_status_nicename() {
303
		$statuses = $this->get_all_statuses();
304
305
        $status = isset( $statuses[ $this->get_status() ] ) ? $statuses[ $this->get_status() ] : $this->get_status();
306
307
        return apply_filters( 'wpinv_get_invoice_status_nicename', $status, $this );
308
    }
309
310
	/**
311
	 * Retrieves the invoice status class
312
	 *
313
	 * @since  1.0.19
314
	 * @return string
315
	 */
316
	public function get_status_class() {
317
		$statuses = getpaid_get_invoice_status_classes();
318
		return isset( $statuses[ $this->get_status() ] ) ? $statuses[ $this->get_status() ] : 'badge-dark';
319
	}
320
321
	/**
322
     * Retrieves the invoice status label html
323
     *
324
     * @since  1.0.0
325
     * @return string
326
     */
327
    public function get_status_label_html() {
328
329
		$status_label = sanitize_text_field( $this->get_status_nicename() );
330
		$status       = sanitize_html_class( $this->get_status() );
331
		$class        = esc_attr( $this->get_status_class() );
332
333
		return "<span class='bsui'><span class='badge $class $status'>$status_label</span></span>";
334
	}
335
336
    /**
337
	 * Get plugin version when the invoice was created.
338
	 *
339
	 * @since 1.0.19
340
	 * @param  string $context View or edit context.
341
	 * @return string
342
	 */
343
	public function get_version( $context = 'view' ) {
344
		return $this->get_prop( 'version', $context );
345
	}
346
347
	/**
348
	 * @deprecated
349
	 */
350
	public function get_invoice_date( $format = true ) {
351
		$date      = getpaid_format_date( $this->get_date_completed() );
352
		$date      = empty( $date ) ? $this->get_date_created() : $this->get_date_completed();
353
		$formatted = getpaid_format_date( $date );
354
355
		if ( $format ) {
356
			return $formatted;
357
		}
358
359
		return empty( $formatted ) ? '' : $date;
360
361
    }
362
363
    /**
364
	 * Get date when the invoice was created.
365
	 *
366
	 * @since 1.0.19
367
	 * @param  string $context View or edit context.
368
	 * @return string
369
	 */
370
	public function get_date_created( $context = 'view' ) {
371
		return $this->get_prop( 'date_created', $context );
372
	}
373
	
374
	/**
375
	 * Alias for self::get_date_created().
376
	 *
377
	 * @since 1.0.19
378
	 * @param  string $context View or edit context.
379
	 * @return string
380
	 */
381
	public function get_created_date( $context = 'view' ) {
382
		return $this->get_date_created( $context );
383
    }
384
385
    /**
386
	 * Get GMT date when the invoice was created.
387
	 *
388
	 * @since 1.0.19
389
	 * @param  string $context View or edit context.
390
	 * @return string
391
	 */
392
	public function get_date_created_gmt( $context = 'view' ) {
393
        $date = $this->get_date_created( $context );
394
395
        if ( $date ) {
396
            $date = get_gmt_from_date( $date );
397
        }
398
		return $date;
399
    }
400
401
    /**
402
	 * Get date when the invoice was last modified.
403
	 *
404
	 * @since 1.0.19
405
	 * @param  string $context View or edit context.
406
	 * @return string
407
	 */
408
	public function get_date_modified( $context = 'view' ) {
409
		return $this->get_prop( 'date_modified', $context );
410
	}
411
412
	/**
413
	 * Alias for self::get_date_modified().
414
	 *
415
	 * @since 1.0.19
416
	 * @param  string $context View or edit context.
417
	 * @return string
418
	 */
419
	public function get_modified_date( $context = 'view' ) {
420
		return $this->get_date_modified( $context );
421
    }
422
423
    /**
424
	 * Get GMT date when the invoice was last modified.
425
	 *
426
	 * @since 1.0.19
427
	 * @param  string $context View or edit context.
428
	 * @return string
429
	 */
430
	public function get_date_modified_gmt( $context = 'view' ) {
431
        $date = $this->get_date_modified( $context );
432
433
        if ( $date ) {
434
            $date = get_gmt_from_date( $date );
435
        }
436
		return $date;
437
    }
438
439
    /**
440
	 * Get the invoice due date.
441
	 *
442
	 * @since 1.0.19
443
	 * @param  string $context View or edit context.
444
	 * @return string
445
	 */
446
	public function get_due_date( $context = 'view' ) {
447
		return $this->get_prop( 'due_date', $context );
448
    }
449
450
    /**
451
	 * Alias for self::get_due_date().
452
	 *
453
	 * @since 1.0.19
454
	 * @param  string $context View or edit context.
455
	 * @return string
456
	 */
457
	public function get_date_due( $context = 'view' ) {
458
		return $this->get_due_date( $context );
459
    }
460
461
    /**
462
	 * Get the invoice GMT due date.
463
	 *
464
	 * @since 1.0.19
465
	 * @param  string $context View or edit context.
466
	 * @return string
467
	 */
468
	public function get_due_date_gmt( $context = 'view' ) {
469
        $date = $this->get_due_date( $context );
470
471
        if ( $date ) {
472
            $date = get_gmt_from_date( $date );
473
        }
474
		return $date;
475
    }
476
477
    /**
478
	 * Alias for self::get_due_date_gmt().
479
	 *
480
	 * @since 1.0.19
481
	 * @param  string $context View or edit context.
482
	 * @return string
483
	 */
484
	public function get_gmt_date_due( $context = 'view' ) {
485
		return $this->get_due_date_gmt( $context );
486
    }
487
488
    /**
489
	 * Get date when the invoice was completed.
490
	 *
491
	 * @since 1.0.19
492
	 * @param  string $context View or edit context.
493
	 * @return string
494
	 */
495
	public function get_completed_date( $context = 'view' ) {
496
		return $this->get_prop( 'completed_date', $context );
497
    }
498
499
    /**
500
	 * Alias for self::get_completed_date().
501
	 *
502
	 * @since 1.0.19
503
	 * @param  string $context View or edit context.
504
	 * @return string
505
	 */
506
	public function get_date_completed( $context = 'view' ) {
507
		return $this->get_completed_date( $context );
508
    }
509
510
    /**
511
	 * Get GMT date when the invoice was was completed.
512
	 *
513
	 * @since 1.0.19
514
	 * @param  string $context View or edit context.
515
	 * @return string
516
	 */
517
	public function get_completed_date_gmt( $context = 'view' ) {
518
        $date = $this->get_completed_date( $context );
519
520
        if ( $date ) {
521
            $date = get_gmt_from_date( $date );
522
        }
523
		return $date;
524
    }
525
526
    /**
527
	 * Alias for self::get_completed_date_gmt().
528
	 *
529
	 * @since 1.0.19
530
	 * @param  string $context View or edit context.
531
	 * @return string
532
	 */
533
	public function get_gmt_completed_date( $context = 'view' ) {
534
		return $this->get_completed_date_gmt( $context );
535
    }
536
537
    /**
538
	 * Get the invoice number.
539
	 *
540
	 * @since 1.0.19
541
	 * @param  string $context View or edit context.
542
	 * @return string
543
	 */
544
	public function get_number( $context = 'view' ) {
545
		$number = $this->get_prop( 'number', $context );
546
547
		if ( empty( $number ) ) {
548
			$number = $this->generate_number();
549
			$this->set_number( $this->generate_number() );
550
		}
551
552
		return $number;
553
    }
554
555
	/**
556
	 * Set the invoice number.
557
	 *
558
	 * @since 1.0.19
559
	 */
560
	public function maybe_set_number() {
561
        $number = $this->get_number();
562
563
        if ( empty( $number ) || $this->get_id() == $number ) {
564
			$this->set_number( $this->generate_number() );
565
        }
566
567
	}
568
569
    /**
570
	 * Get the invoice key.
571
	 *
572
	 * @since 1.0.19
573
	 * @param  string $context View or edit context.
574
	 * @return string
575
	 */
576
	public function get_key( $context = 'view' ) {
577
        return $this->get_prop( 'key', $context );
578
	}
579
580
	/**
581
	 * Set the invoice key.
582
	 *
583
	 * @since 1.0.19
584
	 */
585
	public function maybe_set_key() {
586
        $key = $this->get_key();
587
588
        if ( empty( $key ) ) {
589
            $key = $this->generate_key( $this->get_type() . '_' );
590
            $this->set_key( $key );
591
        }
592
593
    }
594
595
    /**
596
	 * Get the invoice type.
597
	 *
598
	 * @since 1.0.19
599
	 * @param  string $context View or edit context.
600
	 * @return string
601
	 */
602
	public function get_type( $context = 'view' ) {
603
        return $this->get_prop( 'type', $context );
604
	}
605
606
	/**
607
	 * Returns the post type name.
608
	 *
609
	 * @since 1.0.19
610
	 * @return string
611
	 */
612
	public function get_invoice_quote_type() {
613
        return getpaid_get_post_type_label( $this->get_post_type(), false );
614
    }
615
616
    /**
617
	 * Get the invoice post type label.
618
	 *
619
	 * @since 1.0.19
620
	 * @param  string $context View or edit context.
621
	 * @return string
622
	 */
623
	public function get_label( $context = 'view' ) {
624
        return getpaid_get_post_type_label( $this->get_post_type( $context ), false );
625
	}
626
627
	/**
628
	 * Get the invoice post type.
629
	 *
630
	 * @since 1.0.19
631
	 * @param  string $context View or edit context.
632
	 * @return string
633
	 */
634
	public function get_post_type( $context = 'view' ) {
635
        return $this->get_prop( 'post_type', $context );
636
    }
637
638
    /**
639
	 * Get the invoice mode.
640
	 *
641
	 * @since 1.0.19
642
	 * @param  string $context View or edit context.
643
	 * @return string
644
	 */
645
	public function get_mode( $context = 'view' ) {
646
        return $this->get_prop( 'mode', $context );
647
    }
648
649
    /**
650
	 * Get the invoice path.
651
	 *
652
	 * @since 1.0.19
653
	 * @param  string $context View or edit context.
654
	 * @return string
655
	 */
656
	public function get_path( $context = 'view' ) {
657
        $path   = $this->get_prop( 'path', $context );
658
		$prefix = $this->get_type();
659
660
		if ( 0 !== strpos( $path, $prefix ) ) {
661
			$path = sanitize_title(  $prefix . '-' . $this->get_id()  );
662
			$this->set_path( $path );
663
		}
664
665
		return $path;
666
    }
667
668
    /**
669
	 * Get the invoice name/title.
670
	 *
671
	 * @since 1.0.19
672
	 * @param  string $context View or edit context.
673
	 * @return string
674
	 */
675
	public function get_name( $context = 'view' ) {
676
        return $this->get_prop( 'title', $context );
677
    }
678
679
    /**
680
	 * Alias of self::get_name().
681
	 *
682
	 * @since 1.0.19
683
	 * @param  string $context View or edit context.
684
	 * @return string
685
	 */
686
	public function get_title( $context = 'view' ) {
687
		return $this->get_name( $context );
688
    }
689
690
    /**
691
	 * Get the invoice description.
692
	 *
693
	 * @since 1.0.19
694
	 * @param  string $context View or edit context.
695
	 * @return string
696
	 */
697
	public function get_description( $context = 'view' ) {
698
		return $this->get_prop( 'description', $context );
699
    }
700
701
    /**
702
	 * Alias of self::get_description().
703
	 *
704
	 * @since 1.0.19
705
	 * @param  string $context View or edit context.
706
	 * @return string
707
	 */
708
	public function get_excerpt( $context = 'view' ) {
709
		return $this->get_description( $context );
710
    }
711
712
    /**
713
	 * Alias of self::get_description().
714
	 *
715
	 * @since 1.0.19
716
	 * @param  string $context View or edit context.
717
	 * @return string
718
	 */
719
	public function get_summary( $context = 'view' ) {
720
		return $this->get_description( $context );
721
    }
722
723
    /**
724
	 * Returns the user info.
725
	 *
726
	 * @since 1.0.19
727
     * @param  string $context View or edit context.
728
	 * @return array
729
	 */
730
    public function get_user_info( $context = 'view' ) {
731
732
        $user_info = array(
733
            'user_id'    => $this->get_user_id( $context ),
734
            'email'      => $this->get_email( $context ),
735
            'first_name' => $this->get_first_name( $context ),
736
            'last_name'  => $this->get_last_name( $context ),
737
            'address'    => $this->get_address( $context ),
738
            'phone'      => $this->get_phone( $context ),
739
            'city'       => $this->get_city( $context ),
740
            'country'    => $this->get_country( $context ),
741
            'state'      => $this->get_state( $context ),
742
            'zip'        => $this->get_zip( $context ),
743
            'company'    => $this->get_company( $context ),
744
            'vat_number' => $this->get_vat_number( $context ),
745
            'discount'   => $this->get_discount_code( $context ),
746
		);
747
748
		return apply_filters( 'wpinv_user_info', $user_info, $this->get_id(), $this );
749
750
    }
751
752
    /**
753
	 * Get the customer id.
754
	 *
755
	 * @since 1.0.19
756
	 * @param  string $context View or edit context.
757
	 * @return int
758
	 */
759
	public function get_author( $context = 'view' ) {
760
		return (int) $this->get_prop( 'author', $context );
761
    }
762
763
    /**
764
	 * Alias of self::get_author().
765
	 *
766
	 * @since 1.0.19
767
	 * @param  string $context View or edit context.
768
	 * @return int
769
	 */
770
	public function get_user_id( $context = 'view' ) {
771
		return $this->get_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_customer_id( $context = 'view' ) {
782
		return $this->get_author( $context );
783
    }
784
785
    /**
786
	 * Get the customer's ip.
787
	 *
788
	 * @since 1.0.19
789
	 * @param  string $context View or edit context.
790
	 * @return string
791
	 */
792
	public function get_ip( $context = 'view' ) {
793
		return $this->get_prop( 'user_ip', $context );
794
    }
795
796
    /**
797
	 * Alias of self::get_ip().
798
	 *
799
	 * @since 1.0.19
800
	 * @param  string $context View or edit context.
801
	 * @return string
802
	 */
803
	public function get_user_ip( $context = 'view' ) {
804
		return $this->get_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_customer_ip( $context = 'view' ) {
815
		return $this->get_ip( $context );
816
    }
817
818
    /**
819
	 * Get the customer's first name.
820
	 *
821
	 * @since 1.0.19
822
	 * @param  string $context View or edit context.
823
	 * @return string
824
	 */
825
	public function get_first_name( $context = 'view' ) {
826
		return $this->get_prop( 'first_name', $context );
827
    }
828
829
    /**
830
	 * Alias of self::get_first_name().
831
	 *
832
	 * @since 1.0.19
833
	 * @param  string $context View or edit context.
834
	 * @return string
835
	 */
836
	public function get_user_first_name( $context = 'view' ) {
837
		return $this->get_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_customer_first_name( $context = 'view' ) {
848
		return $this->get_first_name( $context );
849
    }
850
851
    /**
852
	 * Get the customer's last name.
853
	 *
854
	 * @since 1.0.19
855
	 * @param  string $context View or edit context.
856
	 * @return string
857
	 */
858
	public function get_last_name( $context = 'view' ) {
859
		return $this->get_prop( 'last_name', $context );
860
    }
861
862
    /**
863
	 * Alias of self::get_last_name().
864
	 *
865
	 * @since 1.0.19
866
	 * @param  string $context View or edit context.
867
	 * @return string
868
	 */
869
	public function get_user_last_name( $context = 'view' ) {
870
		return $this->get_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_customer_last_name( $context = 'view' ) {
881
		return $this->get_last_name( $context );
882
    }
883
884
    /**
885
	 * Get the customer's full name.
886
	 *
887
	 * @since 1.0.19
888
	 * @param  string $context View or edit context.
889
	 * @return string
890
	 */
891
	public function get_full_name( $context = 'view' ) {
892
		return trim( $this->get_first_name( $context ) . ' ' . $this->get_last_name( $context ) );
893
    }
894
895
    /**
896
	 * Alias of self::get_full_name().
897
	 *
898
	 * @since 1.0.19
899
	 * @param  string $context View or edit context.
900
	 * @return string
901
	 */
902
	public function get_user_full_name( $context = 'view' ) {
903
		return $this->get_full_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_customer_full_name( $context = 'view' ) {
914
		return $this->get_full_name( $context );
915
    }
916
917
    /**
918
	 * Get the customer's phone number.
919
	 *
920
	 * @since 1.0.19
921
	 * @param  string $context View or edit context.
922
	 * @return string
923
	 */
924
	public function get_phone( $context = 'view' ) {
925
		return $this->get_prop( 'phone', $context );
926
    }
927
928
    /**
929
	 * Alias of self::get_phone().
930
	 *
931
	 * @since 1.0.19
932
	 * @param  string $context View or edit context.
933
	 * @return string
934
	 */
935
	public function get_phone_number( $context = 'view' ) {
936
		return $this->get_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_user_phone( $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_customer_phone( $context = 'view' ) {
958
		return $this->get_phone( $context );
959
    }
960
961
    /**
962
	 * Get the customer's email address.
963
	 *
964
	 * @since 1.0.19
965
	 * @param  string $context View or edit context.
966
	 * @return string
967
	 */
968
	public function get_email( $context = 'view' ) {
969
		return $this->get_prop( 'email', $context );
970
    }
971
972
    /**
973
	 * Alias of self::get_email().
974
	 *
975
	 * @since 1.0.19
976
	 * @param  string $context View or edit context.
977
	 * @return string
978
	 */
979
	public function get_email_address( $context = 'view' ) {
980
		return $this->get_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_user_email( $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_customer_email( $context = 'view' ) {
1002
		return $this->get_email( $context );
1003
    }
1004
1005
    /**
1006
	 * Get the customer's country.
1007
	 *
1008
	 * @since 1.0.19
1009
	 * @param  string $context View or edit context.
1010
	 * @return string
1011
	 */
1012
	public function get_country( $context = 'view' ) {
1013
		$country = $this->get_prop( 'country', $context );
1014
		return empty( $country ) ? wpinv_get_default_country() : $country;
1015
    }
1016
1017
    /**
1018
	 * Alias of self::get_country().
1019
	 *
1020
	 * @since 1.0.19
1021
	 * @param  string $context View or edit context.
1022
	 * @return string
1023
	 */
1024
	public function get_user_country( $context = 'view' ) {
1025
		return $this->get_country( $context );
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_customer_country( $context = 'view' ) {
1036
		return $this->get_country( $context );
1037
    }
1038
1039
    /**
1040
	 * Get the customer's state.
1041
	 *
1042
	 * @since 1.0.19
1043
	 * @param  string $context View or edit context.
1044
	 * @return string
1045
	 */
1046
	public function get_state( $context = 'view' ) {
1047
		$state = $this->get_prop( 'state', $context );
1048
		return empty( $state ) ? wpinv_get_default_state() : $state;
1049
    }
1050
1051
    /**
1052
	 * Alias of self::get_state().
1053
	 *
1054
	 * @since 1.0.19
1055
	 * @param  string $context View or edit context.
1056
	 * @return string
1057
	 */
1058
	public function get_user_state( $context = 'view' ) {
1059
		return $this->get_state( $context );
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_customer_state( $context = 'view' ) {
1070
		return $this->get_state( $context );
1071
    }
1072
1073
    /**
1074
	 * Get the customer's city.
1075
	 *
1076
	 * @since 1.0.19
1077
	 * @param  string $context View or edit context.
1078
	 * @return string
1079
	 */
1080
	public function get_city( $context = 'view' ) {
1081
		return $this->get_prop( 'city', $context );
1082
    }
1083
1084
    /**
1085
	 * Alias of self::get_city().
1086
	 *
1087
	 * @since 1.0.19
1088
	 * @param  string $context View or edit context.
1089
	 * @return string
1090
	 */
1091
	public function get_user_city( $context = 'view' ) {
1092
		return $this->get_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_customer_city( $context = 'view' ) {
1103
		return $this->get_city( $context );
1104
    }
1105
1106
    /**
1107
	 * Get the customer's zip.
1108
	 *
1109
	 * @since 1.0.19
1110
	 * @param  string $context View or edit context.
1111
	 * @return string
1112
	 */
1113
	public function get_zip( $context = 'view' ) {
1114
		return $this->get_prop( 'zip', $context );
1115
    }
1116
1117
    /**
1118
	 * Alias of self::get_zip().
1119
	 *
1120
	 * @since 1.0.19
1121
	 * @param  string $context View or edit context.
1122
	 * @return string
1123
	 */
1124
	public function get_user_zip( $context = 'view' ) {
1125
		return $this->get_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_customer_zip( $context = 'view' ) {
1136
		return $this->get_zip( $context );
1137
    }
1138
1139
    /**
1140
	 * Get the customer's company.
1141
	 *
1142
	 * @since 1.0.19
1143
	 * @param  string $context View or edit context.
1144
	 * @return string
1145
	 */
1146
	public function get_company( $context = 'view' ) {
1147
		return $this->get_prop( 'company', $context );
1148
    }
1149
1150
    /**
1151
	 * Alias of self::get_company().
1152
	 *
1153
	 * @since 1.0.19
1154
	 * @param  string $context View or edit context.
1155
	 * @return string
1156
	 */
1157
	public function get_user_company( $context = 'view' ) {
1158
		return $this->get_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_customer_company( $context = 'view' ) {
1169
		return $this->get_company( $context );
1170
    }
1171
1172
    /**
1173
	 * Get the customer's vat number.
1174
	 *
1175
	 * @since 1.0.19
1176
	 * @param  string $context View or edit context.
1177
	 * @return string
1178
	 */
1179
	public function get_vat_number( $context = 'view' ) {
1180
		return $this->get_prop( 'vat_number', $context );
1181
    }
1182
1183
    /**
1184
	 * Alias of self::get_vat_number().
1185
	 *
1186
	 * @since 1.0.19
1187
	 * @param  string $context View or edit context.
1188
	 * @return string
1189
	 */
1190
	public function get_user_vat_number( $context = 'view' ) {
1191
		return $this->get_vat_number( $context );
1192
    }
1193
1194
    /**
1195
	 * Alias of self::get_vat_number().
1196
	 *
1197
	 * @since 1.0.19
1198
	 * @param  string $context View or edit context.
1199
	 * @return string
1200
	 */
1201
	public function get_customer_vat_number( $context = 'view' ) {
1202
		return $this->get_vat_number( $context );
1203
    }
1204
1205
    /**
1206
	 * Get the customer's vat rate.
1207
	 *
1208
	 * @since 1.0.19
1209
	 * @param  string $context View or edit context.
1210
	 * @return string
1211
	 */
1212
	public function get_vat_rate( $context = 'view' ) {
1213
		return $this->get_prop( 'vat_rate', $context );
1214
    }
1215
1216
    /**
1217
	 * Alias of self::get_vat_rate().
1218
	 *
1219
	 * @since 1.0.19
1220
	 * @param  string $context View or edit context.
1221
	 * @return string
1222
	 */
1223
	public function get_user_vat_rate( $context = 'view' ) {
1224
		return $this->get_vat_rate( $context );
1225
    }
1226
1227
    /**
1228
	 * Alias of self::get_vat_rate().
1229
	 *
1230
	 * @since 1.0.19
1231
	 * @param  string $context View or edit context.
1232
	 * @return string
1233
	 */
1234
	public function get_customer_vat_rate( $context = 'view' ) {
1235
		return $this->get_vat_rate( $context );
1236
    }
1237
1238
    /**
1239
	 * Get the customer's address.
1240
	 *
1241
	 * @since 1.0.19
1242
	 * @param  string $context View or edit context.
1243
	 * @return string
1244
	 */
1245
	public function get_address( $context = 'view' ) {
1246
		return $this->get_prop( 'address', $context );
1247
    }
1248
1249
    /**
1250
	 * Alias of self::get_address().
1251
	 *
1252
	 * @since 1.0.19
1253
	 * @param  string $context View or edit context.
1254
	 * @return string
1255
	 */
1256
	public function get_user_address( $context = 'view' ) {
1257
		return $this->get_address( $context );
1258
    }
1259
1260
    /**
1261
	 * Alias of self::get_address().
1262
	 *
1263
	 * @since 1.0.19
1264
	 * @param  string $context View or edit context.
1265
	 * @return string
1266
	 */
1267
	public function get_customer_address( $context = 'view' ) {
1268
		return $this->get_address( $context );
1269
    }
1270
1271
    /**
1272
	 * Get whether the customer has viewed the invoice or not.
1273
	 *
1274
	 * @since 1.0.19
1275
	 * @param  string $context View or edit context.
1276
	 * @return bool
1277
	 */
1278
	public function get_is_viewed( $context = 'view' ) {
1279
		return (bool) $this->get_prop( 'is_viewed', $context );
1280
	}
1281
1282
	/**
1283
	 * Get other recipients for invoice communications.
1284
	 *
1285
	 * @since 1.0.19
1286
	 * @param  string $context View or edit context.
1287
	 * @return bool
1288
	 */
1289
	public function get_email_cc( $context = 'view' ) {
1290
		return $this->get_prop( 'email_cc', $context );
1291
	}
1292
1293
	/**
1294
	 * Get invoice template.
1295
	 *
1296
	 * @since 1.0.19
1297
	 * @param  string $context View or edit context.
1298
	 * @return bool
1299
	 */
1300
	public function get_template( $context = 'view' ) {
1301
		return $this->get_prop( 'template', $context );
1302
	}
1303
1304
	/**
1305
	 * Get invoice source.
1306
	 *
1307
	 * @since 1.0.19
1308
	 * @param  string $context View or edit context.
1309
	 * @return bool
1310
	 */
1311
	public function get_created_via( $context = 'view' ) {
1312
		return $this->get_prop( 'created_via', $context );
1313
	}
1314
1315
	/**
1316
	 * Get whether the customer has confirmed their address.
1317
	 *
1318
	 * @since 1.0.19
1319
	 * @param  string $context View or edit context.
1320
	 * @return bool
1321
	 */
1322
	public function get_address_confirmed( $context = 'view' ) {
1323
		return (bool) $this->get_prop( 'address_confirmed', $context );
1324
    }
1325
1326
    /**
1327
	 * Alias of self::get_address_confirmed().
1328
	 *
1329
	 * @since 1.0.19
1330
	 * @param  string $context View or edit context.
1331
	 * @return bool
1332
	 */
1333
	public function get_user_address_confirmed( $context = 'view' ) {
1334
		return $this->get_address_confirmed( $context );
1335
    }
1336
1337
    /**
1338
	 * Alias of self::get_address().
1339
	 *
1340
	 * @since 1.0.19
1341
	 * @param  string $context View or edit context.
1342
	 * @return bool
1343
	 */
1344
	public function get_customer_address_confirmed( $context = 'view' ) {
1345
		return $this->get_address_confirmed( $context );
1346
    }
1347
1348
    /**
1349
	 * Get the invoice subtotal.
1350
	 *
1351
	 * @since 1.0.19
1352
	 * @param  string $context View or edit context.
1353
	 * @return float
1354
	 */
1355
	public function get_subtotal( $context = 'view' ) {
1356
        $subtotal = (float) $this->get_prop( 'subtotal', $context );
1357
1358
        // Backwards compatibility.
1359
        if ( is_bool( $context ) && $context ) {
0 ignored issues
show
introduced by
The condition is_bool($context) is always false.
Loading history...
1360
            return wpinv_price( $subtotal, $this->get_currency() );
1361
        }
1362
1363
        return $subtotal;
1364
    }
1365
1366
    /**
1367
	 * Get the invoice discount total.
1368
	 *
1369
	 * @since 1.0.19
1370
	 * @param  string $context View or edit context.
1371
	 * @return float
1372
	 */
1373
	public function get_total_discount( $context = 'view' ) {
1374
		return wpinv_round_amount( wpinv_sanitize_amount( $this->get_prop( 'total_discount', $context ) ) );
1375
    }
1376
1377
    /**
1378
	 * Get the invoice tax total.
1379
	 *
1380
	 * @since 1.0.19
1381
	 * @param  string $context View or edit context.
1382
	 * @return float
1383
	 */
1384
	public function get_total_tax( $context = 'view' ) {
1385
		return wpinv_round_amount( wpinv_sanitize_amount( $this->get_prop( 'total_tax', $context ) ) );
1386
	}
1387
1388
	/**
1389
	 * @deprecated
1390
	 */
1391
	public function get_final_tax( $currency = false ) {
1392
		$tax = $this->get_total_tax();
1393
1394
        if ( $currency ) {
1395
			return wpinv_price( $tax, $this->get_currency() );
1396
        }
1397
1398
        return $tax;
1399
    }
1400
1401
    /**
1402
	 * Get the invoice fees total.
1403
	 *
1404
	 * @since 1.0.19
1405
	 * @param  string $context View or edit context.
1406
	 * @return float
1407
	 */
1408
	public function get_total_fees( $context = 'view' ) {
1409
		return wpinv_round_amount( wpinv_sanitize_amount( $this->get_prop( 'total_fees', $context ) ) );
1410
    }
1411
1412
    /**
1413
	 * Alias for self::get_total_fees().
1414
	 *
1415
	 * @since 1.0.19
1416
	 * @param  string $context View or edit context.
1417
	 * @return float
1418
	 */
1419
	public function get_fees_total( $context = 'view' ) {
1420
		return $this->get_total_fees( $context );
1421
    }
1422
1423
    /**
1424
	 * Get the invoice total.
1425
	 *
1426
	 * @since 1.0.19
1427
     * @return float
1428
	 */
1429
	public function get_total() {
1430
		$total = $this->is_renewal() ? $this->get_recurring_total() : $this->get_initial_total();
1431
		return apply_filters( 'getpaid_get_invoice_total_amount', $total, $this  );
1432
	}
1433
	
1434
	/**
1435
	 * Get the invoice totals.
1436
	 *
1437
	 * @since 1.0.19
1438
     * @return array
1439
	 */
1440
	public function get_totals() {
1441
		return $this->totals;
1442
    }
1443
1444
    /**
1445
	 * Get the initial invoice total.
1446
	 *
1447
	 * @since 1.0.19
1448
     * @param  string $context View or edit context.
1449
     * @return float
1450
	 */
1451
    public function get_initial_total() {
1452
1453
		if ( empty( $this->totals ) ) {
1454
			$this->recalculate_total();
1455
		}
1456
1457
		$tax      = $this->totals['tax']['initial'];
1458
		$fee      = $this->totals['fee']['initial'];
1459
		$discount = $this->totals['discount']['initial'];
1460
		$subtotal = $this->totals['subtotal']['initial'];
1461
		$total    = $tax + $fee - $discount + $subtotal;
1462
1463
		if ( 0 > $total ) {
1464
			$total = 0;
1465
		}
1466
1467
		$total = wpinv_round_amount( wpinv_sanitize_amount( $total ) );
1468
        return apply_filters( 'wpinv_get_initial_invoice_total', $total, $this );
1469
	}
1470
1471
	/**
1472
	 * Get the recurring invoice total.
1473
	 *
1474
	 * @since 1.0.19
1475
     * @param  string $context View or edit context.
1476
     * @return float
1477
	 */
1478
    public function get_recurring_total() {
1479
1480
		if ( empty( $this->totals ) ) {
1481
			$this->recalculate_total();
1482
		}
1483
1484
		$tax      = $this->totals['tax']['recurring'];
1485
		$fee      = $this->totals['fee']['recurring'];
1486
		$discount = $this->totals['discount']['recurring'];
1487
		$subtotal = $this->totals['subtotal']['recurring'];
1488
		$total    = $tax + $fee - $discount + $subtotal;
1489
1490
		if ( 0 > $total ) {
1491
			$total = 0;
1492
		}
1493
1494
		$total = wpinv_round_amount( wpinv_sanitize_amount( $total ) );
1495
        return apply_filters( 'wpinv_get_recurring_invoice_total', $total, $this );
1496
	}
1497
1498
	/**
1499
	 * Returns recurring payment details.
1500
	 *
1501
	 * @since 1.0.19
1502
     * @param  string $field Optionally provide a field to return.
1503
	 * @param string $currency Whether to include the currency.
1504
     * @return float|string
1505
	 */
1506
    public function get_recurring_details( $field = '', $currency = false ) {
1507
1508
		// Maybe recalculate totals.
1509
		if ( empty( $this->totals ) ) {
1510
			$this->recalculate_total();
1511
		}
1512
1513
		// Prepare recurring totals.
1514
        $data = apply_filters(
1515
			'wpinv_get_invoice_recurring_details',
1516
			array(
1517
				'cart_details' => $this->get_cart_details(),
1518
				'subtotal'     => $this->totals['subtotal']['recurring'],
1519
				'discount'     => $this->totals['discount']['recurring'],
1520
				'tax'          => $this->totals['tax']['recurring'],
1521
				'fee'          => $this->totals['fee']['recurring'],
1522
				'total'        => $this->get_recurring_total(),
1523
			),
1524
			$this,
1525
			$field,
1526
			$currency
1527
		);
1528
1529
        if ( isset( $data[$field] ) ) {
1530
            return ( $currency ? wpinv_price( $data[$field], $this->get_currency() ) : $data[$field] );
1531
        }
1532
1533
        return $data;
1534
    }
1535
1536
    /**
1537
	 * Get the invoice fees.
1538
	 *
1539
	 * @since 1.0.19
1540
	 * @param  string $context View or edit context.
1541
	 * @return array
1542
	 */
1543
	public function get_fees( $context = 'view' ) {
1544
		return wpinv_parse_list( $this->get_prop( 'fees', $context ) );
1545
    }
1546
1547
    /**
1548
	 * Get the invoice discounts.
1549
	 *
1550
	 * @since 1.0.19
1551
	 * @param  string $context View or edit context.
1552
	 * @return array
1553
	 */
1554
	public function get_discounts( $context = 'view' ) {
1555
		return wpinv_parse_list( $this->get_prop( 'discounts', $context ) );
1556
    }
1557
1558
    /**
1559
	 * Get the invoice taxes.
1560
	 *
1561
	 * @since 1.0.19
1562
	 * @param  string $context View or edit context.
1563
	 * @return array
1564
	 */
1565
	public function get_taxes( $context = 'view' ) {
1566
		return wpinv_parse_list( $this->get_prop( 'taxes', $context ) );
1567
    }
1568
1569
    /**
1570
	 * Get the invoice items.
1571
	 *
1572
	 * @since 1.0.19
1573
	 * @param  string $context View or edit context.
1574
	 * @return GetPaid_Form_Item[]
1575
	 */
1576
	public function get_items( $context = 'view' ) {
1577
        return $this->get_prop( 'items', $context );
1578
	}
1579
	
1580
	/**
1581
	 * Get the invoice item ids.
1582
	 *
1583
	 * @since 1.0.19
1584
	 * @return string
1585
	 */
1586
	public function get_item_ids() {
1587
		return implode( ', ', wp_list_pluck( $this->get_cart_details(), 'item_id' ) );
1588
    }
1589
1590
    /**
1591
	 * Get the invoice's payment form.
1592
	 *
1593
	 * @since 1.0.19
1594
	 * @param  string $context View or edit context.
1595
	 * @return int
1596
	 */
1597
	public function get_payment_form( $context = 'view' ) {
1598
		return intval( $this->get_prop( 'payment_form', $context ) );
1599
    }
1600
1601
    /**
1602
	 * Get the invoice's submission id.
1603
	 *
1604
	 * @since 1.0.19
1605
	 * @param  string $context View or edit context.
1606
	 * @return string
1607
	 */
1608
	public function get_submission_id( $context = 'view' ) {
1609
		return $this->get_prop( 'submission_id', $context );
1610
    }
1611
1612
    /**
1613
	 * Get the invoice's discount code.
1614
	 *
1615
	 * @since 1.0.19
1616
	 * @param  string $context View or edit context.
1617
	 * @return string
1618
	 */
1619
	public function get_discount_code( $context = 'view' ) {
1620
		return $this->get_prop( 'discount_code', $context );
1621
    }
1622
1623
    /**
1624
	 * Get the invoice's gateway.
1625
	 *
1626
	 * @since 1.0.19
1627
	 * @param  string $context View or edit context.
1628
	 * @return string
1629
	 */
1630
	public function get_gateway( $context = 'view' ) {
1631
		return $this->get_prop( 'gateway', $context );
1632
    }
1633
1634
    /**
1635
	 * Get the invoice's gateway display title.
1636
	 *
1637
	 * @since 1.0.19
1638
	 * @return string
1639
	 */
1640
    public function get_gateway_title() {
1641
        $title =  wpinv_get_gateway_checkout_label( $this->get_gateway() );
1642
        return apply_filters( 'wpinv_gateway_title', $title, $this->get_id(), $this );
1643
    }
1644
1645
    /**
1646
	 * Get the invoice's transaction id.
1647
	 *
1648
	 * @since 1.0.19
1649
	 * @param  string $context View or edit context.
1650
	 * @return string
1651
	 */
1652
	public function get_transaction_id( $context = 'view' ) {
1653
		return $this->get_prop( 'transaction_id', $context );
1654
    }
1655
1656
    /**
1657
	 * Get the invoice's currency.
1658
	 *
1659
	 * @since 1.0.19
1660
	 * @param  string $context View or edit context.
1661
	 * @return string
1662
	 */
1663
	public function get_currency( $context = 'view' ) {
1664
        $currency = $this->get_prop( 'currency', $context );
1665
        return empty( $currency ) ? wpinv_get_currency() : $currency;
1666
    }
1667
1668
    /**
1669
	 * Checks if we are charging taxes for this invoice.
1670
	 *
1671
	 * @since 1.0.19
1672
	 * @param  string $context View or edit context.
1673
	 * @return bool
1674
	 */
1675
	public function get_disable_taxes( $context = 'view' ) {
1676
        return (bool) $this->get_prop( 'disable_taxes', $context );
1677
    }
1678
1679
    /**
1680
	 * Retrieves the subscription id for an invoice.
1681
	 *
1682
	 * @since 1.0.19
1683
	 * @param  string $context View or edit context.
1684
	 * @return int
1685
	 */
1686
    public function get_subscription_id( $context = 'view' ) {
1687
		return $this->is_renewal() ? $this->get_parent()->get_subscription_id( $context ) : $this->get_prop( 'subscription_id', $context );
1688
	}
1689
1690
	/**
1691
	 * Retrieves the remote subscription id for an invoice.
1692
	 *
1693
	 * @since 1.0.19
1694
	 * @param  string $context View or edit context.
1695
	 * @return int
1696
	 */
1697
    public function get_remote_subscription_id( $context = 'view' ) {
1698
        $subscription_id = $this->get_prop( 'remote_subscription_id', $context );
1699
1700
        if ( empty( $subscription_id ) && $this->is_renewal() ) {
1701
            $parent = $this->get_parent();
1702
            return $parent->get_subscription_id( $context );
1703
        }
1704
1705
        return $subscription_id;
1706
    }
1707
1708
    /**
1709
	 * Retrieves the payment meta for an invoice.
1710
	 *
1711
	 * @since 1.0.19
1712
	 * @param  string $context View or edit context.
1713
	 * @return array
1714
	 */
1715
    public function get_payment_meta( $context = 'view' ) {
1716
1717
        return array(
1718
            'price'        => $this->get_total( $context ),
0 ignored issues
show
Unused Code introduced by
The call to WPInv_Invoice::get_total() has too many arguments starting with $context. ( Ignorable by Annotation )

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

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

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

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

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

3566
        $this->/** @scrutinizer ignore-call */ 
3567
               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...
3567
        return $this;
3568
    }
3569
3570
    /**
3571
     * Convert this to an array.
3572
     */
3573
    public function array_convert() {
3574
        return $this->get_data();
3575
    }
3576
3577
    /**
3578
     * Adds a note to an invoice.
3579
     *
3580
     * @param string $note The note being added.
3581
	 * @return int|false The new note's ID on success, false on failure.
3582
     *
3583
     */
3584
    public function add_note( $note = '', $customer_type = false, $added_by_user = false, $system = false ) {
3585
3586
        // Bail if no note specified or this invoice is not yet saved.
3587
        if ( ! $note || $this->get_id() == 0 || ( ! is_user_logged_in() && ! $system ) ) {
3588
            return false;
3589
        }
3590
3591
		$author       = 'System';
3592
		$author_email = '[email protected]';
3593
3594
		// If this is an admin comment or it has been added by the user.
3595
		if ( is_user_logged_in() && ( ! $system || $added_by_user ) ) {
3596
			$user         = get_user_by( 'id', get_current_user_id() );
3597
            $author       = $user->display_name;
3598
            $author_email = $user->user_email;
3599
		}
3600
3601
		return getpaid_notes()->add_invoice_note( $this, $note, $author, $author_email, $customer_type );
3602
3603
	}
3604
3605
	/**
3606
     * Generates a unique key for the invoice.
3607
     */
3608
    public function generate_key( $string = '' ) {
3609
        $auth_key  = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
3610
        return strtolower(
3611
            $string . md5( $this->get_id() . date( 'Y-m-d H:i:s' ) . $auth_key . uniqid( 'wpinv', true ) )
3612
        );
3613
    }
3614
3615
    /**
3616
     * Generates a new number for the invoice.
3617
     */
3618
    public function generate_number() {
3619
        $number = $this->get_id();
3620
3621
        if ( wpinv_sequential_number_active( $this->get_post_type() ) ) {
3622
            $number = wpinv_get_next_invoice_number( $this->get_post_type() );
3623
        }
3624
3625
		return wpinv_format_invoice_number( $number, $this->get_post_type() );
3626
3627
	}
3628
3629
	/**
3630
	 * Handle the status transition.
3631
	 */
3632
	protected function status_transition() {
3633
		$status_transition = $this->status_transition;
3634
3635
		// Reset status transition variable.
3636
		$this->status_transition = false;
3637
3638
		if ( $status_transition ) {
3639
			try {
3640
3641
				// Fire a hook for the status change.
3642
				do_action( 'getpaid_invoice_status_' . $status_transition['to'], $this, $status_transition );
3643
3644
				// @deprecated this is deprecated and will be removed in the future.
3645
				do_action( 'wpinv_status_' . $status_transition['to'], $this->get_id(), $status_transition['from'] );
3646
3647
				if ( ! empty( $status_transition['from'] ) ) {
3648
3649
					/* translators: 1: old invoice status 2: new invoice status */
3650
					$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  ) );
3651
3652
					// Fire another hook.
3653
					do_action( 'getpaid_invoice_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this );
3654
					do_action( 'getpaid_invoice_status_changed', $this, $status_transition['from'], $status_transition['to'] );
3655
3656
					// @deprecated this is deprecated and will be removed in the future.
3657
					do_action( 'wpinv_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $status_transition['from'] );
3658
3659
					// Note the transition occurred.
3660
					$this->add_note( trim( $status_transition['note'] . ' ' . $transition_note ), false, $status_transition['manual'] );
3661
3662
					// Work out if this was for a payment, and trigger a payment_status hook instead.
3663
					if (
3664
						in_array( $status_transition['from'], array( 'wpi-cancelled', 'wpi-pending', 'wpi-failed', 'wpi-refunded', 'wpi-onhold' ), true )
3665
						&& in_array( $status_transition['to'], array( 'publish', 'wpi-processing', 'wpi-renewal' ), true )
3666
					) {
3667
						do_action( 'getpaid_invoice_payment_status_changed', $this, $status_transition );
3668
					}
3669
3670
					// Work out if this was for a payment reversal, and trigger a payment_status_reversed hook instead.
3671
					if (
3672
						in_array( $status_transition['from'], array( 'publish', 'wpi-processing', 'wpi-renewal' ), true )
3673
						&& in_array( $status_transition['to'], array( 'wpi-cancelled', 'wpi-pending', 'wpi-failed', 'wpi-refunded', 'wpi-onhold' ), true )
3674
					) {
3675
						do_action( 'getpaid_invoice_payment_status_reversed', $this, $status_transition );
3676
					}
3677
				} else {
3678
					/* translators: %s: new invoice status */
3679
					$transition_note = sprintf( __( 'Status set to %s.', 'invoicing' ), wpinv_status_nicename( $status_transition['to'], $this  ) );
3680
3681
					// Note the transition occurred.
3682
					$this->add_note( trim( $status_transition['note'] . ' ' . $transition_note ), 0, $status_transition['manual'] );
3683
3684
				}
3685
			} catch ( Exception $e ) {
3686
				$this->add_note( __( 'Error during status transition.', 'invoicing' ) . ' ' . $e->getMessage() );
3687
			}
3688
		}
3689
	}
3690
3691
	/**
3692
	 * Updates an invoice status.
3693
	 */
3694
	public function update_status( $new_status = false, $note = '', $manual = false ) {
3695
3696
		// Fires before updating a status.
3697
		do_action( 'wpinv_before_invoice_status_change', $this->get_id(), $new_status, $this->get_status( 'edit' ) );
3698
3699
		// Update the status.
3700
		$this->set_status( $new_status, $note, $manual );
3701
3702
		// Save the order.
3703
		return $this->save();
3704
3705
	}
3706
3707
	/**
3708
	 * @deprecated
3709
	 */
3710
	public function refresh_item_ids() {
3711
        $item_ids = implode( ',', array_unique( array_keys( $this->get_items() ) ) );
3712
        update_post_meta( $this->get_id(), '_wpinv_item_ids', $item_ids );
3713
	}
3714
3715
	/**
3716
	 * @deprecated
3717
	 */
3718
	public function update_items( $temp = false ) {
3719
3720
		$this->set_items( $this->get_items() );
3721
3722
		if ( ! $temp ) {
3723
			$this->save();
3724
		}
3725
3726
        return $this;
3727
	}
3728
3729
	/**
3730
	 * @deprecated
3731
	 */
3732
    public function validate_discount() {
3733
3734
        $discount_code = $this->get_discount_code();
3735
3736
        if ( empty( $discount_code ) ) {
3737
            return false;
3738
        }
3739
3740
        $discount = wpinv_get_discount_obj( $discount_code );
3741
3742
        // Ensure it is active.
3743
        return $discount->exists();
3744
3745
    }
3746
3747
	/**
3748
	 * Refunds an invoice.
3749
	 */
3750
    public function refund() {
3751
		$this->set_status( 'wpi-refunded' );
3752
        $this->save();
3753
	}
3754
3755
	/**
3756
	 * Marks an invoice as paid.
3757
	 * 
3758
	 * @param string $transaction_id
3759
	 */
3760
    public function mark_paid( $transaction_id = null, $note = '' ) {
3761
3762
		// Set the transaction id.
3763
		if ( empty( $transaction_id ) ) {
3764
			$transaction_id = $this->generate_key('trans_');
3765
		}
3766
3767
		if ( ! $this->get_transaction_id() ) {
3768
			$this->set_transaction_id( $transaction_id );
3769
		}
3770
3771
		if ( $this->is_paid() && 'wpi-processing' != $this->get_status() ) {
3772
			return $this->save();
3773
		}
3774
3775
		// Set the completed date.
3776
		$this->set_date_completed( current_time( 'mysql' ) );
3777
3778
		// Set the new status.
3779
		$gateway = sanitize_text_field( $this->get_gateway_title() );
3780
		if ( $this->is_renewal() ) {
3781
3782
			$_note = wp_sprintf( __( 'Renewed via %s', 'invoicing' ), $gateway );
3783
			$_note = $_note . empty( $note ) ? '' : " ($note)";
3784
3785
			if ( 'none' == $this->get_gateway() ) {
3786
				$_note = $note;
3787
			}
3788
3789
			$this->set_status( 'wpi-renewal', $_note );
3790
3791
		} else {
3792
3793
			$_note = wp_sprintf( __( 'Paid via %s', 'invoicing' ), $gateway );
3794
			$_note = $_note . empty( $note ) ? '' : " ($note)";
3795
3796
			if ( 'none' == $this->get_gateway() ) {
3797
				$_note = $note;
3798
			}
3799
3800
			$this->set_status( 'publish', $_note );
3801
3802
		}
3803
3804
		// Set checkout mode.
3805
		$mode = wpinv_is_test_mode( $this->get_gateway() ) ? 'test' : 'live';
3806
		$this->set_mode( $mode );
3807
3808
		// Save the invoice.
3809
        $this->save();
3810
	}
3811
3812
	/**
3813
	 * Save data to the database.
3814
	 *
3815
	 * @since 1.0.19
3816
	 * @return int invoice ID
3817
	 */
3818
	public function save() {
3819
		$this->maybe_set_date_paid();
3820
		$this->maybe_set_key();
3821
		parent::save();
3822
		$this->clear_cache();
3823
		$this->status_transition();
3824
		return $this->get_id();
3825
	}
3826
3827
	/**
3828
     * Clears the subscription's cache.
3829
     */
3830
    public function clear_cache() {
3831
		wp_cache_delete( $this->get_key(), 'getpaid_invoice_keys_to_invoice_ids' );
3832
		wp_cache_delete( $this->get_number(), 'getpaid_invoice_numbers_to_invoice_ids' );
3833
		wp_cache_delete( $this->get_transaction_id(), 'getpaid_invoice_transaction_ids_to_invoice_ids' );
3834
	}
3835
3836
}
3837