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
transaction_model.php
197 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 properties_to_query(): array{ |
| 59 | return [ |
| 60 | 'payment_method' => __('Payment Method', 'latepoint'), |
| 61 | 'payment_portion' => __('Payment Portion', 'latepoint'), |
| 62 | 'kind' => __('Type', 'latepoint'), |
| 63 | ]; |
| 64 | } |
| 65 | |
| 66 | public function generate_data_vars(): array { |
| 67 | return [ |
| 68 | 'id' => $this->id, |
| 69 | 'order_id' => $this->order_id, |
| 70 | 'invoice_id' => $this->invoice_id, |
| 71 | 'token' => $this->token, |
| 72 | 'customer_id' => $this->customer_id, |
| 73 | 'processor' => $this->processor, |
| 74 | 'payment_method' => $this->payment_method, |
| 75 | 'payment_portion' => $this->payment_portion_nice_name, |
| 76 | 'kind' => $this->kind, |
| 77 | 'status' => $this->status, |
| 78 | 'amount' => OsMoneyHelper::format_price($this->amount), |
| 79 | 'notes' => $this->notes, |
| 80 | ]; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | public function get_access_url(): string{ |
| 85 | if(empty($this->access_key)) $this->update_attributes(['access_key' => OsUtilHelper::generate_uuid()]); |
| 86 | return OsRouterHelper::build_admin_post_link( [ 'transactions', 'view_receipt_by_key' ], [ 'key' => $this->access_key ] ); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | public function filter_allowed_records(): OsModel{ |
| 91 | if(!OsRolesHelper::are_all_records_allowed()){ |
| 92 | // join orders table to filter allowed transactions |
| 93 | $this->join(LATEPOINT_TABLE_BOOKINGS, ['id' => $this->table_name.'.order_id']); |
| 94 | $this->select(LATEPOINT_TABLE_TRANSACTIONS.'.*'); |
| 95 | if(!OsRolesHelper::are_all_records_allowed('agent')){ |
| 96 | $this->select(LATEPOINT_TABLE_BOOKINGS.'.agent_id'); |
| 97 | $this->filter_where_conditions([LATEPOINT_TABLE_BOOKINGS.'.agent_id' => OsRolesHelper::get_allowed_records('agent')]); |
| 98 | } |
| 99 | if(!OsRolesHelper::are_all_records_allowed('location')){ |
| 100 | $this->select(LATEPOINT_TABLE_BOOKINGS.'.location_id'); |
| 101 | $this->filter_where_conditions([LATEPOINT_TABLE_BOOKINGS.'.location_id' => OsRolesHelper::get_allowed_records('location')]); |
| 102 | } |
| 103 | if(!OsRolesHelper::are_all_records_allowed('service')){ |
| 104 | $this->select(LATEPOINT_TABLE_BOOKINGS.'.service_id'); |
| 105 | $this->filter_where_conditions([LATEPOINT_TABLE_BOOKINGS.'.service_id' => OsRolesHelper::get_allowed_records('service')]); |
| 106 | } |
| 107 | } |
| 108 | return $this; |
| 109 | } |
| 110 | |
| 111 | protected function params_to_sanitize() { |
| 112 | return ['amount' => 'money']; |
| 113 | } |
| 114 | |
| 115 | public function generate_receipt_number() : string{ |
| 116 | return strtoupper( OsUtilHelper::random_text( 'distinct', 8 ) ); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | protected function before_save() { |
| 121 | |
| 122 | if ( empty( $this->receipt_number ) ) { |
| 123 | $this->receipt_number = $this->generate_receipt_number(); |
| 124 | } |
| 125 | if ( empty( $this->access_key ) ) { |
| 126 | $this->access_key = OsUtilHelper::generate_uuid(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | public function get_payment_portion_nice_name($default = '') { |
| 131 | $payment_portions = OsPaymentsHelper::get_payment_portions_list(); |
| 132 | $nice_name = (!empty($this->payment_portion) && isset($payment_portions[$this->payment_portion])) ? $payment_portions[$this->payment_portion] : $default; |
| 133 | return $nice_name; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | protected function get_customer(): OsCustomerModel { |
| 138 | $order = $this->get_order(); |
| 139 | return $order->customer; |
| 140 | } |
| 141 | |
| 142 | protected function get_order(): OsOrderModel { |
| 143 | if ($this->order_id) { |
| 144 | if (!isset($this->order) || (isset($this->order) && ($this->order->id != $this->order_id))) { |
| 145 | $this->order = new OsOrderModel($this->order_id); |
| 146 | } |
| 147 | } else { |
| 148 | $this->order = new OsOrderModel(); |
| 149 | } |
| 150 | return $this->order; |
| 151 | } |
| 152 | |
| 153 | protected function params_to_save($role = 'admin'): array { |
| 154 | $params_to_save = array('id', |
| 155 | 'token', |
| 156 | 'invoice_id', |
| 157 | 'order_id', |
| 158 | 'processor', |
| 159 | 'customer_id', |
| 160 | 'payment_method', |
| 161 | 'payment_portion', |
| 162 | 'access_key', |
| 163 | 'receipt_number', |
| 164 | 'kind', |
| 165 | 'amount', |
| 166 | 'status', |
| 167 | 'notes'); |
| 168 | return $params_to_save; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | protected function allowed_params($role = 'admin'): array { |
| 173 | $allowed_params = array('id', |
| 174 | 'token', |
| 175 | 'invoice_id', |
| 176 | 'order_id', |
| 177 | 'processor', |
| 178 | 'customer_id', |
| 179 | 'payment_method', |
| 180 | 'payment_portion', |
| 181 | 'access_key', |
| 182 | 'receipt_number', |
| 183 | 'kind', |
| 184 | 'amount', |
| 185 | 'status', |
| 186 | 'notes'); |
| 187 | return $allowed_params; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | protected function properties_to_validate() :array { |
| 192 | $validations = array( |
| 193 | 'order_id' => array('presence'), |
| 194 | ); |
| 195 | return $validations; |
| 196 | } |
| 197 | } |