Passed
Pull Request — master (#822)
by
unknown
09:10
created

WPInv_Invoice::get_payment_form()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

3775
				$r_taxes  = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, /** @scrutinizer ignore-type */ true ), $rates );
Loading history...
3776
				foreach ( $taxes as $name => $amount ) {
3777
					$recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0;
3778
					$tax       = getpaid_prepare_item_tax( $item, $name, $amount, $recurring );
3779
3780
					if ( ! isset( $item_taxes[ $name ] ) ) {
3781
						$item_taxes[ $name ] = $tax;
3782
						continue;
3783
					}
3784
3785
					$item_taxes[ $name ]['initial_tax']   += $tax['initial_tax'];
3786
					$item_taxes[ $name ]['recurring_tax'] += $tax['recurring_tax'];
3787
3788
				}
3789
			}
3790
3791
			$this->set_taxes( $item_taxes );
3792
3793
			$initial_tax   = array_sum( wp_list_pluck( $item_taxes, 'initial_tax' ) );
3794
			$recurring_tax = array_sum( wp_list_pluck( $item_taxes, 'recurring_tax' ) );
3795
3796
			$current = $this->is_renewal() ? $recurring_tax : $initial_tax;
3797
3798
			$this->totals['tax'] = array(
3799
				'initial'   => $initial_tax,
3800
				'recurring' => $recurring_tax,
3801
			);
3802
3803
		}
3804
3805
		$this->set_total_tax( $current );
3806
3807
		return $current;
3808
    }
3809
3810
    /**
3811
	 * Recalculates the invoice fees total.
3812
	 *
3813
	 * @since 1.0.19
3814
	 * @return float The recalculated fee
3815
	 */
3816
	public function recalculate_total_fees() {
3817
		$fees      = $this->get_fees();
3818
		$fee       = 0;
3819
		$recurring = 0;
3820
3821
        foreach ( $fees as $data ) {
3822
			$fee       += wpinv_sanitize_amount( $data['initial_fee'] );
3823
			$recurring += wpinv_sanitize_amount( $data['recurring_fee'] );
3824
		}
3825
3826
		$current = $this->is_renewal() ? $recurring : $fee;
3827
		$this->set_total_fees( $current );
3828
3829
		$this->totals['fee'] = array(
3830
			'initial'   => $fee,
3831
			'recurring' => $recurring,
3832
		);
3833
3834
        $this->set_total_fees( $fee );
3835
        return $current;
3836
    }
3837
3838
    /**
3839
	 * Recalculates the invoice total.
3840
	 *
3841
	 * @since 1.0.19
3842
     * @return float The invoice total
3843
	 */
3844
	public function recalculate_total() {
3845
        $this->recalculate_total_fees();
3846
        $this->recalculate_total_discount();
3847
		$this->recalculate_total_tax();
3848
		$this->recalculate_subtotal();
3849
		$this->set_total( $this->get_total_tax( 'edit' ) + $this->get_total_fees( 'edit' ) + $this->get_subtotal( 'edit' ) - $this->get_total_discount( 'edit' ) );
3850
		return $this->get_total();
3851
	}
3852
3853
	/**
3854
	 * @deprecated
3855
	 */
3856
    public function recalculate_totals() {
3857
        $this->recalculate_total();
3858
        $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

3858
        $this->/** @scrutinizer ignore-call */ 
3859
               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...
3859
        return $this;
3860
    }
3861
3862
    /**
3863
     * Convert this to an array.
3864
     */
3865
    public function array_convert() {
3866
        return $this->get_data();
3867
    }
3868
3869
	/**
3870
     * Adds a system note to an invoice.
3871
     *
3872
     * @param string $note The note being added.
3873
	 * @return int|false The new note's ID on success, false on failure.
3874
     *
3875
     */
3876
    public function add_system_note( $note ) {
3877
		return $this->add_note( $note, false, false, true );
3878
	}
3879
3880
    /**
3881
     * Adds a note to an invoice.
3882
     *
3883
     * @param string $note The note being added.
3884
	 * @return int|false The new note's ID on success, false on failure.
3885
     *
3886
     */
3887
    public function add_note( $note = '', $customer_type = false, $added_by_user = false, $system = false ) {
3888
3889
        // Bail if no note specified or this invoice is not yet saved.
3890
        if ( ! $note || $this->get_id() == 0 || ( ! is_user_logged_in() && ! $system ) ) {
3891
            return false;
3892
        }
3893
3894
		$author       = 'System';
3895
		$author_email = '[email protected]';
3896
3897
		// If this is an admin comment or it has been added by the user.
3898
		if ( is_user_logged_in() && ( ! $system || $added_by_user ) ) {
3899
			$user         = get_user_by( 'id', get_current_user_id() );
3900
            $author       = $user->display_name;
3901
            $author_email = $user->user_email;
3902
		}
3903
3904
		return getpaid_notes()->add_invoice_note( $this, $note, $author, $author_email, $customer_type );
3905
	}
3906
3907
	/**
3908
     * Generates a unique key for the invoice.
3909
     */
3910
    public function generate_key( $string = '' ) {
3911
        $auth_key  = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
3912
        return strtolower(
3913
            $string . md5( $this->get_id() . date( 'Y-m-d H:i:s' ) . $auth_key . uniqid( 'wpinv', true ) )
3914
        );
3915
    }
3916
3917
    /**
3918
     * Generates a new number for the invoice.
3919
     */
3920
    public function generate_number() {
3921
        $number = $this->get_id();
3922
3923
        if ( wpinv_sequential_number_active( $this->get_post_type() ) ) {
3924
            $number = wpinv_get_next_invoice_number( $this->get_post_type() );
3925
        }
3926
3927
		return wpinv_format_invoice_number( $number, $this->get_post_type() );
3928
	}
3929
3930
	/**
3931
	 * Handle the status transition.
3932
	 */
3933
	protected function status_transition() {
3934
		$status_transition = $this->status_transition;
3935
3936
		// Reset status transition variable.
3937
		$this->status_transition = false;
3938
3939
		if ( $status_transition ) {
3940
			try {
3941
3942
				// Fire a hook for the status change.
3943
				do_action( 'getpaid_invoice_status_' . $status_transition['to'], $this, $status_transition );
3944
3945
				// @deprecated this is deprecated and will be removed in the future.
3946
				do_action( 'wpinv_status_' . $status_transition['to'], $this->get_id(), $status_transition['from'] );
3947
3948
				if ( ! empty( $status_transition['from'] ) ) {
3949
3950
					/* translators: 1: old invoice status 2: new invoice status */
3951
					$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 ) );
3952
3953
					// Fire another hook.
3954
					do_action( 'getpaid_invoice_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this );
3955
					do_action( 'getpaid_invoice_status_changed', $this, $status_transition['from'], $status_transition['to'] );
3956
3957
					// @deprecated this is deprecated and will be removed in the future.
3958
					do_action( 'wpinv_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $status_transition['from'] );
3959
3960
					// Note the transition occurred.
3961
					$this->add_note( trim( $status_transition['note'] . ' ' . $transition_note ), false, $status_transition['manual'] );
3962
3963
					// Work out if this was for a payment, and trigger a payment_status hook instead.
3964
					if (
3965
						in_array( $status_transition['from'], array( 'wpi-cancelled', 'pending', 'wpi-pending', 'wpi-failed', 'wpi-refunded', 'wpi-onhold' ), true )
3966
						&& in_array( $status_transition['to'], array( 'publish', 'wpi-processing', 'wpi-renewal' ), true )
3967
					) {
3968
						do_action( 'getpaid_invoice_payment_status_changed', $this, $status_transition );
3969
					}
3970
3971
					// Work out if this was for a payment reversal, and trigger a payment_status_reversed hook instead.
3972
					if (
3973
						in_array( $status_transition['from'], array( 'publish', 'wpi-processing', 'wpi-renewal' ), true )
3974
						&& in_array( $status_transition['to'], array( 'wpi-cancelled', 'pending', 'wpi-pending', 'wpi-failed', 'wpi-refunded', 'wpi-onhold' ), true )
3975
					) {
3976
						do_action( 'getpaid_invoice_payment_status_reversed', $this, $status_transition );
3977
					}
3978
				} else {
3979
					/* translators: %s: new invoice status */
3980
					$transition_note = sprintf( __( 'Status set to %s.', 'invoicing' ), wpinv_status_nicename( $status_transition['to'], $this ) );
3981
3982
					// Note the transition occurred.
3983
					$this->add_note( trim( $status_transition['note'] . ' ' . $transition_note ), 0, $status_transition['manual'] );
3984
3985
				}
3986
			} catch ( Exception $e ) {
3987
				$this->add_note( __( 'Error during status transition.', 'invoicing' ) . ' ' . $e->getMessage() );
3988
			}
3989
		}
3990
	}
3991
3992
	/**
3993
	 * Updates an invoice status.
3994
	 */
3995
	public function update_status( $new_status = false, $note = '', $manual = false ) {
3996
3997
		// Fires before updating a status.
3998
		do_action( 'wpinv_before_invoice_status_change', $this->get_id(), $new_status, $this->get_status( 'edit' ) );
3999
4000
		// Update the status.
4001
		$this->set_status( $new_status, $note, $manual );
4002
4003
		// Save the order.
4004
		return $this->save();
4005
	}
4006
4007
	/**
4008
	 * @deprecated
4009
	 */
4010
	public function refresh_item_ids() {
4011
        $item_ids = implode( ',', array_unique( wp_list_pluck( $this->get_cart_details(), 'item_id' ) ) );
4012
        update_post_meta( $this->get_id(), '_wpinv_item_ids', $item_ids );
4013
	}
4014
4015
	/**
4016
	 * @deprecated
4017
	 */
4018
	public function update_items( $temp = false ) {
4019
4020
		$this->set_items( $this->get_items() );
4021
4022
		if ( ! $temp ) {
4023
			$this->save();
4024
		}
4025
4026
        return $this;
4027
	}
4028
4029
	/**
4030
	 * @deprecated
4031
	 */
4032
    public function validate_discount() {
4033
4034
        $discount_code = $this->get_discount_code();
4035
4036
        if ( empty( $discount_code ) ) {
4037
            return false;
4038
        }
4039
4040
        $discount = wpinv_get_discount_obj( $discount_code );
4041
4042
        // Ensure it is active.
4043
        return $discount->exists();
4044
    }
4045
4046
	/**
4047
	 * Refunds an invoice.
4048
	 */
4049
    public function refund() {
4050
		$this->set_status( 'wpi-refunded' );
4051
        $this->save();
4052
	}
4053
4054
	/**
4055
	 * Marks an invoice as paid.
4056
	 *
4057
	 * @param string $transaction_id
4058
	 */
4059
    public function mark_paid( $transaction_id = null, $note = '' ) {
4060
4061
		// Set the transaction id.
4062
		if ( empty( $transaction_id ) ) {
4063
			$transaction_id = $this->generate_key( 'trans_' );
4064
		}
4065
4066
		if ( ! $this->get_transaction_id() ) {
4067
			$this->set_transaction_id( $transaction_id );
4068
		}
4069
4070
		if ( $this->is_paid() && 'wpi-processing' !== $this->get_status() ) {
4071
			return $this->save();
4072
		}
4073
4074
		// Set the completed date.
4075
		$this->set_date_completed( current_time( 'mysql' ) );
4076
4077
		// Set the new status.
4078
		$gateway = sanitize_text_field( $this->get_gateway_title() );
4079
		if ( $this->is_renewal() || ! $this->is_parent() ) {
4080
4081
			$_note = wp_sprintf( __( 'Renewed via %s', 'invoicing' ), $gateway );
4082
			$_note = $_note . empty( $note ) ? '' : " ($note)";
4083
4084
			if ( 'none' == $this->get_gateway() ) {
4085
				$_note = $note;
4086
			}
4087
4088
			$this->set_status( 'wpi-renewal', $_note );
4089
4090
		} else {
4091
4092
			$_note = wp_sprintf( __( 'Paid via %s', 'invoicing' ), $gateway );
4093
			$_note = $_note . empty( $note ) ? '' : " ($note)";
4094
4095
			if ( 'none' == $this->get_gateway() ) {
4096
				$_note = $note;
4097
			}
4098
4099
			$this->set_status( 'publish', $_note );
4100
4101
		}
4102
4103
		// Set checkout mode.
4104
		$mode = wpinv_is_test_mode( $this->get_gateway() ) ? 'test' : 'live';
4105
		$this->set_mode( $mode );
4106
4107
		// Save the invoice.
4108
        $this->save();
4109
	}
4110
4111
	/**
4112
	 * Save data to the database.
4113
	 *
4114
	 * @since 1.0.19
4115
	 * @return int invoice ID
4116
	 */
4117
	public function save() {
4118
		$this->maybe_set_date_paid();
4119
		$this->maybe_set_key();
4120
		parent::save();
4121
		$this->clear_cache();
4122
		$this->status_transition();
4123
		return $this->get_id();
4124
	}
4125
4126
	/**
4127
     * Clears the subscription's cache.
4128
     */
4129
    public function clear_cache() {
4130
		if ( $this->get_key() ) {
4131
			wp_cache_delete( $this->get_key(), 'getpaid_invoice_keys_to_invoice_ids' );
4132
		}
4133
4134
		if ( $this->get_number() ) {
4135
			wp_cache_delete( $this->get_number(), 'getpaid_invoice_numbers_to_invoice_ids' );
4136
		}
4137
4138
		if ( $this->get_transaction_id() ) {
4139
			wp_cache_delete( $this->get_transaction_id(), 'getpaid_invoice_transaction_ids_to_invoice_ids' );
4140
		}
4141
	}
4142
}
4143