PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.2.2
LatePoint – Calendar Booking Plugin for Appointments and Events v5.2.2
5.6.7 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 / transaction_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_meta_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 9 months 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 9 months ago off_period_model.php 1 year ago order_intent_meta_model.php 1 year ago order_intent_model.php 9 months ago order_item_model.php 1 year ago order_meta_model.php 1 year ago order_model.php 1 year ago otp_model.php 9 months ago payment_request_model.php 1 year ago process_job_model.php 1 year ago process_model.php 1 year ago recurrence_model.php 1 year ago service_category_model.php 9 months ago service_meta_model.php 1 year ago service_model.php 9 months 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
213 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() ) return false;
60 $can_refund = apply_filters('latepoint_transaction_is_refund_available', false, $this);
61 if (!$can_refund) return false;
62 if ( $this->is_fully_refunded()) return false;
63
64 return true;
65 }
66
67 public function properties_to_query(): array{
68 return [
69 'payment_method' => __('Payment Method', 'latepoint'),
70 'payment_portion' => __('Payment Portion', 'latepoint'),
71 'kind' => __('Type', 'latepoint'),
72 ];
73 }
74
75 public function generate_data_vars(): array {
76 return [
77 'id' => $this->id,
78 'order_id' => $this->order_id,
79 'invoice_id' => $this->invoice_id,
80 'token' => $this->token,
81 'customer_id' => $this->customer_id,
82 'processor' => $this->processor,
83 'payment_method' => $this->payment_method,
84 'payment_portion' => $this->payment_portion_nice_name,
85 'kind' => $this->kind,
86 'status' => $this->status,
87 'amount' => OsMoneyHelper::format_price($this->amount),
88 'notes' => $this->notes,
89 ];
90 }
91
92
93 public function get_receipt_url(): string{
94 if(empty($this->access_key)) $this->update_attributes(['access_key' => OsUtilHelper::generate_uuid()]);
95 return OsRouterHelper::build_admin_post_link( [ 'transactions', 'view_receipt_by_key' ], [ 'key' => $this->access_key ] );
96 }
97
98 public function get_invoice_url(): string{
99 if(empty($this->invoice_id)) return '';
100 $invoice = new OsInvoiceModel($this->invoice_id);
101 if($invoice->is_new_record()) return '';
102 return $invoice->get_access_url();
103 }
104
105
106 public function filter_allowed_records(): OsModel{
107 if(!OsRolesHelper::are_all_records_allowed()){
108 // join orders table to filter allowed transactions
109 $this->join(LATEPOINT_TABLE_BOOKINGS, ['id' => $this->table_name.'.order_id']);
110 $this->select(LATEPOINT_TABLE_TRANSACTIONS.'.*');
111 if(!OsRolesHelper::are_all_records_allowed('agent')){
112 $this->select(LATEPOINT_TABLE_BOOKINGS.'.agent_id');
113 $this->filter_where_conditions([LATEPOINT_TABLE_BOOKINGS.'.agent_id' => OsRolesHelper::get_allowed_records('agent')]);
114 }
115 if(!OsRolesHelper::are_all_records_allowed('location')){
116 $this->select(LATEPOINT_TABLE_BOOKINGS.'.location_id');
117 $this->filter_where_conditions([LATEPOINT_TABLE_BOOKINGS.'.location_id' => OsRolesHelper::get_allowed_records('location')]);
118 }
119 if(!OsRolesHelper::are_all_records_allowed('service')){
120 $this->select(LATEPOINT_TABLE_BOOKINGS.'.service_id');
121 $this->filter_where_conditions([LATEPOINT_TABLE_BOOKINGS.'.service_id' => OsRolesHelper::get_allowed_records('service')]);
122 }
123 }
124 return $this;
125 }
126
127 protected function params_to_sanitize() {
128 return ['amount' => 'money'];
129 }
130
131 public function generate_receipt_number() : string{
132 return strtoupper( OsUtilHelper::random_text( 'distinct', 8 ) );
133 }
134
135
136 protected function before_save() {
137
138 if ( empty( $this->receipt_number ) ) {
139 $this->receipt_number = $this->generate_receipt_number();
140 }
141 if ( empty( $this->access_key ) ) {
142 $this->access_key = OsUtilHelper::generate_uuid();
143 }
144 }
145
146 public function get_payment_portion_nice_name($default = '') {
147 $payment_portions = OsPaymentsHelper::get_payment_portions_list();
148 $nice_name = (!empty($this->payment_portion) && isset($payment_portions[$this->payment_portion])) ? $payment_portions[$this->payment_portion] : $default;
149 return $nice_name;
150 }
151
152
153 protected function get_customer(): OsCustomerModel {
154 $order = $this->get_order();
155 return $order->customer;
156 }
157
158 protected function get_order(): OsOrderModel {
159 if ($this->order_id) {
160 if (!isset($this->order) || (isset($this->order) && ($this->order->id != $this->order_id))) {
161 $this->order = new OsOrderModel($this->order_id);
162 }
163 } else {
164 $this->order = new OsOrderModel();
165 }
166 return $this->order;
167 }
168
169 protected function params_to_save($role = 'admin'): array {
170 $params_to_save = array('id',
171 'token',
172 'invoice_id',
173 'order_id',
174 'processor',
175 'customer_id',
176 'payment_method',
177 'payment_portion',
178 'access_key',
179 'receipt_number',
180 'kind',
181 'amount',
182 'status',
183 'notes');
184 return $params_to_save;
185 }
186
187
188 protected function allowed_params($role = 'admin'): array {
189 $allowed_params = array('id',
190 'token',
191 'invoice_id',
192 'order_id',
193 'processor',
194 'customer_id',
195 'payment_method',
196 'payment_portion',
197 'access_key',
198 'receipt_number',
199 'kind',
200 'amount',
201 'status',
202 'notes');
203 return $allowed_params;
204 }
205
206
207 protected function properties_to_validate() :array {
208 $validations = array(
209 'order_id' => array('presence'),
210 );
211 return $validations;
212 }
213 }