| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCart\Models; |
| 4 |
|
| 5 |
use SureCart\Models\Traits\CanResendNotifications; |
| 6 |
use SureCart\Models\Traits\HasCheckout; |
| 7 |
use SureCart\Models\Traits\HasDates; |
| 8 |
use SureCart\Support\TimeDate; |
| 9 |
|
| 10 |
/** |
| 11 |
* Invoice model |
| 12 |
*/ |
| 13 |
class Invoice extends Model { |
| 14 |
use HasCheckout; |
| 15 |
use HasDates; |
| 16 |
use CanResendNotifications; |
| 17 |
|
| 18 |
/** |
| 19 |
* Rest API endpoint |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $endpoint = 'invoices'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Object name |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $object_name = 'invoice'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Need to pass the processor type on create |
| 34 |
* |
| 35 |
* @param array $attributes Optional attributes. |
| 36 |
* @param string $type stripe, paypal, etc. |
| 37 |
*/ |
| 38 |
public function __construct( $attributes = [], $type = '' ) { |
| 39 |
$this->processor_type = $type; |
| 40 |
parent::__construct( $attributes ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get the checkout page URL for the invoice. |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function getCheckoutUrlAttribute(): string { |
| 49 |
return add_query_arg( 'checkout_id', $this->checkout_id, \SureCart::pages()->url( 'checkout' ) ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Make draft invoice. |
| 54 |
* |
| 55 |
* @param string|null $id Invoice ID. |
| 56 |
* |
| 57 |
* @return self|\WP_Error |
| 58 |
*/ |
| 59 |
protected function makeDraft( $id = null ) { |
| 60 |
if ( $id ) { |
| 61 |
$this->setAttribute( 'id', $id ); |
| 62 |
} |
| 63 |
|
| 64 |
if ( $this->fireModelEvent( 'drafting' ) === false ) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
if ( empty( $this->attributes['id'] ) ) { |
| 69 |
return new \WP_Error( 'not_saved', __( 'Please create the invoice first.', 'surecart' ) ); |
| 70 |
} |
| 71 |
|
| 72 |
$drafted = \SureCart::request( |
| 73 |
$this->endpoint . '/' . $this->attributes['id'] . '/make_draft', |
| 74 |
[ |
| 75 |
'method' => 'PATCH', |
| 76 |
'query' => $this->query, |
| 77 |
] |
| 78 |
); |
| 79 |
|
| 80 |
if ( is_wp_error( $drafted ) ) { |
| 81 |
return $drafted; |
| 82 |
} |
| 83 |
|
| 84 |
$this->resetAttributes(); |
| 85 |
|
| 86 |
$this->fill( $drafted ); |
| 87 |
|
| 88 |
$this->fireModelEvent( 'drafted' ); |
| 89 |
|
| 90 |
return $this; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Open invoice. |
| 95 |
* |
| 96 |
* @param string|null $id Invoice ID. |
| 97 |
* |
| 98 |
* @return self|\WP_Error |
| 99 |
*/ |
| 100 |
protected function open( $id = null ) { |
| 101 |
if ( $id ) { |
| 102 |
$this->setAttribute( 'id', $id ); |
| 103 |
} |
| 104 |
|
| 105 |
if ( $this->fireModelEvent( 'opening' ) === false ) { |
| 106 |
return $this; |
| 107 |
} |
| 108 |
|
| 109 |
if ( empty( $this->attributes['id'] ) ) { |
| 110 |
return new \WP_Error( 'not_saved', __( 'Please create the invoice first.', 'surecart' ) ); |
| 111 |
} |
| 112 |
|
| 113 |
$opened = \SureCart::request( |
| 114 |
$this->endpoint . '/' . $this->attributes['id'] . '/open', |
| 115 |
[ |
| 116 |
'method' => 'PATCH', |
| 117 |
'query' => $this->query, |
| 118 |
] |
| 119 |
); |
| 120 |
|
| 121 |
if ( is_wp_error( $opened ) ) { |
| 122 |
return $opened; |
| 123 |
} |
| 124 |
|
| 125 |
$this->resetAttributes(); |
| 126 |
$this->fill( $opened ); |
| 127 |
$this->fireModelEvent( 'opened' ); |
| 128 |
|
| 129 |
return $this; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get the due date display. |
| 134 |
* |
| 135 |
* @return string |
| 136 |
*/ |
| 137 |
public function getDueDateDateAttribute() { |
| 138 |
return ! empty( $this->due_date ) ? TimeDate::formatDate( $this->due_date ) : ''; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get the due date time display. |
| 143 |
* |
| 144 |
* @return string |
| 145 |
*/ |
| 146 |
public function getDueDateTimeDateAttribute() { |
| 147 |
return ! empty( $this->due_date ) ? TimeDate::formatDateAndTime( $this->due_date ) : ''; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get the issue date display. |
| 152 |
* |
| 153 |
* @return string |
| 154 |
*/ |
| 155 |
public function getIssueDateDateAttribute() { |
| 156 |
return ! empty( $this->issue_date ) ? TimeDate::formatDate( $this->issue_date ) : ''; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Get the issue date time display. |
| 161 |
* |
| 162 |
* @return string |
| 163 |
*/ |
| 164 |
public function getIssueDateTimeDateAttribute() { |
| 165 |
return ! empty( $this->issue_date ) ? TimeDate::formatDateAndTime( $this->issue_date ) : ''; |
| 166 |
} |
| 167 |
} |
| 168 |
|