PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.1.3
LatePoint – Calendar Booking Plugin for Appointments and Events v5.1.3
5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / models / invoice_model.php
latepoint / lib / models Last commit date
activity_model.php 1 year ago agent_meta_model.php 1 year ago agent_model.php 1 year ago booking_meta_model.php 1 year ago booking_model.php 1 year ago bundle_model.php 1 year ago cart_item_model.php 1 year ago cart_meta_model.php 1 year ago cart_model.php 1 year ago connector_model.php 1 year ago customer_meta_model.php 1 year ago customer_model.php 1 year ago invoice_model.php 1 year ago join_bundles_services_model.php 1 year ago location_category_model.php 1 year ago location_model.php 1 year ago meta_model.php 1 year ago model.php 1 year ago order_intent_meta_model.php 1 year ago order_intent_model.php 1 year ago order_item_model.php 1 year ago order_meta_model.php 1 year ago order_model.php 1 year ago payment_request_model.php 1 year ago process_job_model.php 1 year ago process_model.php 1 year ago service_category_model.php 1 year ago service_meta_model.php 1 year ago service_model.php 1 year ago session_model.php 1 year ago settings_model.php 1 year ago step_settings_model.php 1 year ago transaction_intent_model.php 1 year ago transaction_model.php 1 year ago transaction_refund_model.php 1 year ago work_period_model.php 1 year ago
invoice_model.php
222 lines
1 <?php
2
3 class OsInvoiceModel extends OsModel {
4 public $id,
5 $order_id,
6 $invoice_number,
7 $data,
8 $charge_amount,
9 $payment_portion,
10 $status,
11 $access_key,
12 $due_at,
13 $updated_at,
14 $created_at;
15
16 function __construct( $id = false ) {
17 parent::__construct();
18 $this->table_name = LATEPOINT_TABLE_ORDER_INVOICES;
19
20 if ( $id ) {
21 $this->load_by_id( $id );
22 }
23 }
24
25 public function get_receipt_url(): string {
26 $transaction = new OsTransactionModel();
27 $transaction = $transaction->where( [ 'invoice_id' => $this->id, 'status' => LATEPOINT_TRANSACTION_STATUS_SUCCEEDED ] )->set_limit( 1 )->get_results_as_models();
28 if ( $transaction && ! $transaction->is_new_record() ) {
29 return $transaction->get_access_url();
30 } else {
31 return $this->get_access_url();
32 }
33 }
34
35 public function properties_to_query(): array {
36 return [
37 'status' => __( 'Status', 'latepoint' ),
38 'payment_portion' => __( 'Payment Portion', 'latepoint' ),
39 ];
40 }
41
42 public function get_customer(): OsCustomerModel {
43 return $this->get_order()->get_customer();
44 }
45
46
47 public function generate_data_vars(): array {
48
49 $vars['id'] = $this->id;
50 $vars['order_id'] = $this->order_id;
51 $vars['invoice_number'] = $this->get_invoice_number();
52 $vars['payment_portion'] = $this->payment_portion;
53 $vars['status'] = $this->status;
54 $vars['data'] = $this->data;
55 $vars['charge_amount'] = $this->charge_amount;
56 $vars['access_key'] = $this->access_key;
57 $vars['due_at'] = $this->due_at;
58
59 $vars['customer'] = $this->get_customer()->get_data_vars();
60 $vars['order'] = $this->get_order()->get_first_level_data_vars();
61 $vars['transactions'] = [];
62
63 $transactions = $this->get_successful_payments();
64 if ( $transactions ) {
65 foreach ( $transactions as $transaction ) {
66 $vars['transactions'][] = $transaction->get_data_vars();
67 }
68 }
69
70 return $vars;
71 }
72
73 public function get_order(): OsOrderModel {
74 if ( $this->order_id ) {
75 if ( ! isset( $this->order ) || ( isset( $this->order ) && ( $this->order->id != $this->order_id ) ) ) {
76 $this->order = new OsOrderModel( $this->order_id );
77 }
78 } else {
79 $this->order = new OsOrderModel();
80 }
81
82 return $this->order;
83 }
84
85 public function get_successful_payments(): array {
86 $transactions = new OsTransactionModel();
87 $transactions = $transactions->where( [ 'status' => LATEPOINT_TRANSACTION_STATUS_SUCCEEDED, 'invoice_id' => $this->id ] )->get_results_as_models();
88
89 return $transactions;
90 }
91
92
93 protected function params_to_sanitize() {
94 return [
95 'charge_amount' => 'money',
96 ];
97 }
98
99 /**
100 * @param string $new_status
101 *
102 * @return bool
103 */
104 public function change_status( string $new_status ): bool {
105 $old_status = $this->status;
106 $old_invoice = clone $this;
107 if ( $old_status == $new_status ) {
108 return true;
109 }
110 if ( $this->update_attributes( [ 'status' => $new_status ] ) ) {
111 /**
112 * Invoice status has been changed
113 *
114 * @param {OsInvoiceModel} $invoice invoice model
115 * @param {string} $old_status previous status of the invoice
116 * @param {string} $new_status new status of the invoice
117 *
118 * @since 5.1.0
119 * @hook latepoint_invoice_status_changed
120 *
121 */
122 do_action( 'latepoint_invoice_status_changed', $this, $old_status, $new_status );
123 /**
124 * Invoice was updated
125 *
126 * @param {OsInvoiceModel} $invoice instance of invoice model after it was updated
127 * @param {OsInvoiceModel} $old_invoice instance of invoice model before it was updated
128 *
129 * @since 5.1.0
130 * @hook latepoint_invoice_updated
131 *
132 */
133 do_action( 'latepoint_invoice_updated', $this, $old_invoice );
134
135 return true;
136 } else {
137 return false;
138 }
139
140 }
141
142 public function get_invoice_number(): string {
143 if(!empty($this->invoice_number)) return $this->invoice_number;
144 if(empty($this->id)) return '';
145 $this->invoice_number = OsSettingsHelper::get_settings_value( 'invoices_number_prefix', 'INV-' ).sprintf('1%06d', $this->id);
146 $this->update_attributes('invoice_number', $this->invoice_number);
147 return $this->invoice_number;
148 }
149
150
151 protected function before_save() {
152 if ( empty( $this->status ) ) {
153 $this->status = LATEPOINT_INVOICE_STATUS_OPEN;
154 }
155 if ( empty( $this->due_at ) ) {
156 $this->due_at = OsTimeHelper::now_datetime_in_format( LATEPOINT_DATETIME_DB_FORMAT );
157 }
158 if ( empty( $this->access_key ) ) {
159 $this->access_key = OsUtilHelper::generate_uuid();
160 }
161 }
162
163 public function get_readable_due_at(): string {
164 try {
165 return OsTimeHelper::get_readable_date( new OsWpDateTime( $this->due_at, new DateTimeZone( 'UTC' ) ) );
166 } catch ( Exception $e ) {
167 return 'n/a';
168 }
169 }
170
171 public function get_access_url(): string {
172 return OsRouterHelper::build_admin_post_link( [ 'invoices', 'view_by_key' ], [ 'key' => $this->access_key ] );
173 }
174
175 public function get_pay_url(): string {
176 return OsRouterHelper::build_admin_post_link( [ 'invoices', 'summary_before_payment' ], [ 'key' => $this->access_key ] );
177 }
178
179
180 protected function params_to_save( $role = 'admin' ): array {
181 $params_to_save = [
182 'id',
183 'order_id',
184 'invoice_number',
185 'payment_portion',
186 'status',
187 'data',
188 'charge_amount',
189 'access_key',
190 'due_at',
191 ];
192
193 return $params_to_save;
194 }
195
196
197 protected function allowed_params( $role = 'admin' ): array {
198 $allowed_params = [
199 'id',
200 'order_id',
201 'invoice_number',
202 'payment_portion',
203 'status',
204 'data',
205 'charge_amount',
206 'access_key',
207 'due_at',
208 ];
209
210 return $allowed_params;
211 }
212
213
214 protected function properties_to_validate(): array {
215 $validations = [
216 'order_id' => [ 'presence' ],
217 'status' => [ 'presence' ],
218 ];
219
220 return $validations;
221 }
222 }