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
3 months 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
transaction_model.php
230 lines
| 1 | <?php |
| 2 | |
| 3 | class OsTransactionModel extends OsModel { |
| 4 | public $id, |
| 5 | $token, |
| 6 | $invoice_id, |
| 7 | $order_id, |
| 8 | $customer_id, |
| 9 | $processor, |
| 10 | $payment_method, |
| 11 | $payment_portion, |
| 12 | $access_key, |
| 13 | $receipt_number, |
| 14 | $kind, |
| 15 | $amount, |
| 16 | $status, |
| 17 | $notes, |
| 18 | $updated_at, |
| 19 | $created_at; |
| 20 | |
| 21 | function __construct( $id = false ) { |
| 22 | parent::__construct(); |
| 23 | $this->table_name = LATEPOINT_TABLE_TRANSACTIONS; |
| 24 | $this->nice_names = [ 'token' => __( 'Confirmation Number', 'latepoint' ) ]; |
| 25 | |
| 26 | if ( $id ) { |
| 27 | $this->load_by_id( $id ); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @return OsTransactionRefundModel[] |
| 33 | */ |
| 34 | public function get_refunds( $force_query = false ): array { |
| 35 | if ( ! isset( $this->refunds ) || $force_query ) { |
| 36 | $transaction_refunds = new OsTransactionRefundModel(); |
| 37 | $transaction_refunds = $transaction_refunds->where( [ 'transaction_id' => $this->id ] )->get_results_as_models(); |
| 38 | $this->refunds = $transaction_refunds; |
| 39 | } |
| 40 | return $this->refunds; |
| 41 | } |
| 42 | |
| 43 | public function get_total_refunded_amount() { |
| 44 | $refunds = $this->get_refunds(); |
| 45 | $total = 0; |
| 46 | if ( $refunds ) { |
| 47 | foreach ( $refunds as $refund ) { |
| 48 | $total = $total + $refund->amount; |
| 49 | } |
| 50 | } |
| 51 | return $total; |
| 52 | } |
| 53 | |
| 54 | public function is_fully_refunded(): bool { |
| 55 | return ! ( $this->get_total_refunded_amount() < $this->amount ); |
| 56 | } |
| 57 | |
| 58 | public function can_refund(): bool { |
| 59 | if ( $this->is_new_record() ) { |
| 60 | return false; |
| 61 | } |
| 62 | $can_refund = apply_filters( 'latepoint_transaction_is_refund_available', false, $this ); |
| 63 | if ( ! $can_refund ) { |
| 64 | return false; |
| 65 | } |
| 66 | if ( $this->is_fully_refunded() ) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | public function properties_to_query(): array { |
| 74 | return [ |
| 75 | 'payment_method' => __( 'Payment Method', 'latepoint' ), |
| 76 | 'payment_portion' => __( 'Payment Portion', 'latepoint' ), |
| 77 | 'kind' => __( 'Type', 'latepoint' ), |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | public function generate_data_vars(): array { |
| 82 | return [ |
| 83 | 'id' => $this->id, |
| 84 | 'order_id' => $this->order_id, |
| 85 | 'invoice_id' => $this->invoice_id, |
| 86 | 'token' => $this->token, |
| 87 | 'customer_id' => $this->customer_id, |
| 88 | 'processor' => $this->processor, |
| 89 | 'payment_method' => $this->payment_method, |
| 90 | 'payment_portion' => $this->payment_portion_nice_name, |
| 91 | 'kind' => $this->kind, |
| 92 | 'status' => $this->status, |
| 93 | 'amount' => OsMoneyHelper::format_price( $this->amount ), |
| 94 | 'notes' => $this->notes, |
| 95 | ]; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | public function get_receipt_url(): string { |
| 100 | if ( empty( $this->access_key ) ) { |
| 101 | $this->update_attributes( [ 'access_key' => OsUtilHelper::generate_uuid() ] ); |
| 102 | } |
| 103 | return OsRouterHelper::build_admin_post_link( [ 'transactions', 'view_receipt_by_key' ], [ 'key' => $this->access_key ] ); |
| 104 | } |
| 105 | |
| 106 | public function get_invoice_url(): string { |
| 107 | if ( empty( $this->invoice_id ) ) { |
| 108 | return ''; |
| 109 | } |
| 110 | $invoice = new OsInvoiceModel( $this->invoice_id ); |
| 111 | if ( $invoice->is_new_record() ) { |
| 112 | return ''; |
| 113 | } |
| 114 | return $invoice->get_access_url(); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | public function filter_allowed_records(): OsModel { |
| 119 | if ( ! OsRolesHelper::are_all_records_allowed() ) { |
| 120 | // join orders table to filter allowed transactions |
| 121 | $this->join( LATEPOINT_TABLE_BOOKINGS, [ 'id' => $this->table_name . '.order_id' ] ); |
| 122 | $this->select( LATEPOINT_TABLE_TRANSACTIONS . '.*' ); |
| 123 | if ( ! OsRolesHelper::are_all_records_allowed( 'agent' ) ) { |
| 124 | $this->select( LATEPOINT_TABLE_BOOKINGS . '.agent_id' ); |
| 125 | $this->filter_where_conditions( [ LATEPOINT_TABLE_BOOKINGS . '.agent_id' => OsRolesHelper::get_allowed_records( 'agent' ) ] ); |
| 126 | } |
| 127 | if ( ! OsRolesHelper::are_all_records_allowed( 'location' ) ) { |
| 128 | $this->select( LATEPOINT_TABLE_BOOKINGS . '.location_id' ); |
| 129 | $this->filter_where_conditions( [ LATEPOINT_TABLE_BOOKINGS . '.location_id' => OsRolesHelper::get_allowed_records( 'location' ) ] ); |
| 130 | } |
| 131 | if ( ! OsRolesHelper::are_all_records_allowed( 'service' ) ) { |
| 132 | $this->select( LATEPOINT_TABLE_BOOKINGS . '.service_id' ); |
| 133 | $this->filter_where_conditions( [ LATEPOINT_TABLE_BOOKINGS . '.service_id' => OsRolesHelper::get_allowed_records( 'service' ) ] ); |
| 134 | } |
| 135 | } |
| 136 | return $this; |
| 137 | } |
| 138 | |
| 139 | protected function params_to_sanitize() { |
| 140 | return [ 'amount' => 'money' ]; |
| 141 | } |
| 142 | |
| 143 | public function generate_receipt_number(): string { |
| 144 | return strtoupper( OsUtilHelper::random_text( 'distinct', 8 ) ); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | protected function before_save() { |
| 149 | |
| 150 | if ( empty( $this->receipt_number ) ) { |
| 151 | $this->receipt_number = $this->generate_receipt_number(); |
| 152 | } |
| 153 | if ( empty( $this->access_key ) ) { |
| 154 | $this->access_key = OsUtilHelper::generate_uuid(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public function get_payment_portion_nice_name( $default = '' ) { |
| 159 | $payment_portions = OsPaymentsHelper::get_payment_portions_list(); |
| 160 | $nice_name = ( ! empty( $this->payment_portion ) && isset( $payment_portions[ $this->payment_portion ] ) ) ? $payment_portions[ $this->payment_portion ] : $default; |
| 161 | return $nice_name; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | protected function get_customer(): OsCustomerModel { |
| 166 | $order = $this->get_order(); |
| 167 | return $order->customer; |
| 168 | } |
| 169 | |
| 170 | protected function get_order(): OsOrderModel { |
| 171 | if ( $this->order_id ) { |
| 172 | if ( ! isset( $this->order ) || ( isset( $this->order ) && ( $this->order->id != $this->order_id ) ) ) { |
| 173 | $this->order = new OsOrderModel( $this->order_id ); |
| 174 | } |
| 175 | } else { |
| 176 | $this->order = new OsOrderModel(); |
| 177 | } |
| 178 | return $this->order; |
| 179 | } |
| 180 | |
| 181 | protected function params_to_save( $role = 'admin' ): array { |
| 182 | $params_to_save = array( |
| 183 | 'id', |
| 184 | 'token', |
| 185 | 'invoice_id', |
| 186 | 'order_id', |
| 187 | 'processor', |
| 188 | 'customer_id', |
| 189 | 'payment_method', |
| 190 | 'payment_portion', |
| 191 | 'access_key', |
| 192 | 'receipt_number', |
| 193 | 'kind', |
| 194 | 'amount', |
| 195 | 'status', |
| 196 | 'notes', |
| 197 | ); |
| 198 | return $params_to_save; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | protected function allowed_params( $role = 'admin' ): array { |
| 203 | $allowed_params = array( |
| 204 | 'id', |
| 205 | 'token', |
| 206 | 'invoice_id', |
| 207 | 'order_id', |
| 208 | 'processor', |
| 209 | 'customer_id', |
| 210 | 'payment_method', |
| 211 | 'payment_portion', |
| 212 | 'access_key', |
| 213 | 'receipt_number', |
| 214 | 'kind', |
| 215 | 'amount', |
| 216 | 'status', |
| 217 | 'notes', |
| 218 | ); |
| 219 | return $allowed_params; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | protected function properties_to_validate(): array { |
| 224 | $validations = array( |
| 225 | 'order_id' => array( 'presence' ), |
| 226 | ); |
| 227 | return $validations; |
| 228 | } |
| 229 | } |
| 230 |