activity_model.php
3 months ago
agent_meta_model.php
3 months ago
agent_model.php
3 months ago
booking_meta_model.php
3 months ago
booking_model.php
3 months ago
bundle_meta_model.php
3 months ago
bundle_model.php
3 months ago
cart_item_model.php
3 months ago
cart_meta_model.php
3 months ago
cart_model.php
3 months ago
connector_model.php
3 months ago
customer_meta_model.php
3 months ago
customer_model.php
1 month ago
invoice_model.php
3 months ago
join_bundles_services_model.php
3 months ago
location_category_model.php
3 months ago
location_model.php
3 months ago
meta_model.php
3 months ago
model.php
3 months ago
off_period_model.php
3 months ago
order_intent_meta_model.php
3 months ago
order_intent_model.php
3 months ago
order_item_model.php
3 months ago
order_meta_model.php
3 months ago
order_model.php
3 months ago
otp_model.php
3 months ago
payment_request_model.php
3 months ago
process_job_model.php
3 months ago
process_model.php
3 months ago
recurrence_model.php
3 months ago
service_category_model.php
3 months ago
service_meta_model.php
3 months ago
service_model.php
3 months ago
session_model.php
3 months ago
settings_model.php
3 months ago
step_settings_model.php
3 months ago
transaction_intent_model.php
3 months ago
transaction_model.php
3 months ago
transaction_refund_model.php
3 months ago
work_period_model.php
3 months ago
invoice_model.php
236 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( |
| 28 | [ |
| 29 | 'invoice_id' => $this->id, |
| 30 | 'status' => LATEPOINT_TRANSACTION_STATUS_SUCCEEDED, |
| 31 | ] |
| 32 | )->set_limit( 1 )->get_results_as_models(); |
| 33 | if ( $transaction && ! $transaction->is_new_record() ) { |
| 34 | return $transaction->get_receipt_url(); |
| 35 | } else { |
| 36 | return $this->get_access_url(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | public function properties_to_query(): array { |
| 41 | return [ |
| 42 | 'status' => __( 'Status', 'latepoint' ), |
| 43 | 'payment_portion' => __( 'Payment Portion', 'latepoint' ), |
| 44 | ]; |
| 45 | } |
| 46 | |
| 47 | public function get_customer(): OsCustomerModel { |
| 48 | return $this->get_order()->get_customer(); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | public function generate_data_vars(): array { |
| 53 | |
| 54 | $vars['id'] = $this->id; |
| 55 | $vars['order_id'] = $this->order_id; |
| 56 | $vars['invoice_number'] = $this->get_invoice_number(); |
| 57 | $vars['payment_portion'] = $this->payment_portion; |
| 58 | $vars['status'] = $this->status; |
| 59 | $vars['data'] = $this->data; |
| 60 | $vars['charge_amount'] = $this->charge_amount; |
| 61 | $vars['access_key'] = $this->access_key; |
| 62 | $vars['due_at'] = $this->due_at; |
| 63 | |
| 64 | $vars['customer'] = $this->get_customer()->get_data_vars(); |
| 65 | $vars['order'] = $this->get_order()->get_first_level_data_vars(); |
| 66 | $vars['transactions'] = []; |
| 67 | |
| 68 | $transactions = $this->get_successful_payments(); |
| 69 | if ( $transactions ) { |
| 70 | foreach ( $transactions as $transaction ) { |
| 71 | $vars['transactions'][] = $transaction->get_data_vars(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return $vars; |
| 76 | } |
| 77 | |
| 78 | public function get_order(): OsOrderModel { |
| 79 | if ( $this->order_id ) { |
| 80 | if ( ! isset( $this->order ) || ( isset( $this->order ) && ( $this->order->id != $this->order_id ) ) ) { |
| 81 | $this->order = new OsOrderModel( $this->order_id ); |
| 82 | } |
| 83 | } else { |
| 84 | $this->order = new OsOrderModel(); |
| 85 | } |
| 86 | |
| 87 | return $this->order; |
| 88 | } |
| 89 | |
| 90 | public function get_successful_payments(): array { |
| 91 | $transactions = new OsTransactionModel(); |
| 92 | $transactions = $transactions->where( |
| 93 | [ |
| 94 | 'status' => LATEPOINT_TRANSACTION_STATUS_SUCCEEDED, |
| 95 | 'invoice_id' => $this->id, |
| 96 | ] |
| 97 | )->get_results_as_models(); |
| 98 | |
| 99 | return $transactions; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | protected function params_to_sanitize() { |
| 104 | return [ |
| 105 | 'charge_amount' => 'money', |
| 106 | ]; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @param string $new_status |
| 111 | * |
| 112 | * @return bool |
| 113 | */ |
| 114 | public function change_status( string $new_status ): bool { |
| 115 | $old_status = $this->status; |
| 116 | $old_invoice = clone $this; |
| 117 | if ( $old_status == $new_status ) { |
| 118 | return true; |
| 119 | } |
| 120 | if ( $this->update_attributes( [ 'status' => $new_status ] ) ) { |
| 121 | /** |
| 122 | * Invoice status has been changed |
| 123 | * |
| 124 | * @param {OsInvoiceModel} $invoice invoice model |
| 125 | * @param {string} $old_status previous status of the invoice |
| 126 | * @param {string} $new_status new status of the invoice |
| 127 | * |
| 128 | * @since 5.1.0 |
| 129 | * @hook latepoint_invoice_status_changed |
| 130 | * |
| 131 | */ |
| 132 | do_action( 'latepoint_invoice_status_changed', $this, $old_status, $new_status ); |
| 133 | /** |
| 134 | * Invoice was updated |
| 135 | * |
| 136 | * @param {OsInvoiceModel} $invoice instance of invoice model after it was updated |
| 137 | * @param {OsInvoiceModel} $old_invoice instance of invoice model before it was updated |
| 138 | * |
| 139 | * @since 5.1.0 |
| 140 | * @hook latepoint_invoice_updated |
| 141 | * |
| 142 | */ |
| 143 | do_action( 'latepoint_invoice_updated', $this, $old_invoice ); |
| 144 | |
| 145 | return true; |
| 146 | } else { |
| 147 | return false; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | public function get_invoice_number(): string { |
| 152 | if ( ! empty( $this->invoice_number ) ) { |
| 153 | return $this->invoice_number; |
| 154 | } |
| 155 | if ( empty( $this->id ) ) { |
| 156 | return ''; |
| 157 | } |
| 158 | $this->invoice_number = OsSettingsHelper::get_settings_value( 'invoices_number_prefix', 'INV-' ) . sprintf( '1%06d', $this->id ); |
| 159 | $this->update_attributes( [ 'invoice_number' => $this->invoice_number ] ); |
| 160 | return $this->invoice_number; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | protected function before_save() { |
| 165 | if ( empty( $this->status ) ) { |
| 166 | $this->status = LATEPOINT_INVOICE_STATUS_OPEN; |
| 167 | } |
| 168 | if ( empty( $this->due_at ) ) { |
| 169 | $this->due_at = OsTimeHelper::now_datetime_in_format( LATEPOINT_DATETIME_DB_FORMAT ); |
| 170 | } |
| 171 | if ( empty( $this->access_key ) ) { |
| 172 | $this->access_key = OsUtilHelper::generate_uuid(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public function get_readable_due_at(): string { |
| 177 | try { |
| 178 | return OsTimeHelper::get_readable_date( new OsWpDateTime( $this->due_at, new DateTimeZone( 'UTC' ) ) ); |
| 179 | } catch ( Exception $e ) { |
| 180 | return 'n/a'; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | public function get_access_url(): string { |
| 185 | return OsRouterHelper::build_admin_post_link( [ 'invoices', 'view_by_key' ], [ 'key' => $this->access_key ] ); |
| 186 | } |
| 187 | |
| 188 | public function get_pay_url(): string { |
| 189 | return OsRouterHelper::build_admin_post_link( [ 'invoices', 'summary_before_payment' ], [ 'key' => $this->access_key ] ); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | protected function params_to_save( $role = 'admin' ): array { |
| 194 | $params_to_save = [ |
| 195 | 'id', |
| 196 | 'order_id', |
| 197 | 'invoice_number', |
| 198 | 'payment_portion', |
| 199 | 'status', |
| 200 | 'data', |
| 201 | 'charge_amount', |
| 202 | 'access_key', |
| 203 | 'due_at', |
| 204 | ]; |
| 205 | |
| 206 | return $params_to_save; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | protected function allowed_params( $role = 'admin' ): array { |
| 211 | $allowed_params = [ |
| 212 | 'id', |
| 213 | 'order_id', |
| 214 | 'invoice_number', |
| 215 | 'payment_portion', |
| 216 | 'status', |
| 217 | 'data', |
| 218 | 'charge_amount', |
| 219 | 'access_key', |
| 220 | 'due_at', |
| 221 | ]; |
| 222 | |
| 223 | return $allowed_params; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | protected function properties_to_validate(): array { |
| 228 | $validations = [ |
| 229 | 'order_id' => [ 'presence' ], |
| 230 | 'status' => [ 'presence' ], |
| 231 | ]; |
| 232 | |
| 233 | return $validations; |
| 234 | } |
| 235 | } |
| 236 |