PluginProbe
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress / 5.2.2
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress v5.2.2
5.6.11 5.6.10 5.6.9 5.6.8 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 All 48 releases
← All changes | lib/controllers/orders_controller.php +116 -209 trunk5.2.2 View file →
@@ -19,9 +19,9 @@
19 19 $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'orders/';
20 20 $this->vars['page_header'] = OsMenuHelper::get_menu_items_by_id( 'orders' );
21 21 $this->vars['breadcrumbs'][] = array(
22 22 'label' => __( 'Orders', 'latepoint' ),
23 - 'link' => OsRouterHelper::build_link( OsRouterHelper::build_route_name( 'orders', 'index' ) ),
23 + 'link' => OsRouterHelper::build_link( OsRouterHelper::build_route_name( 'orders', 'index' ) )
24 24 );
25 25
26 26 $this->action_access['public'] = array_merge( $this->action_access['public'], [ 'continue_order_intent', 'continue_transaction_intent' ] );
27 27 }
@@ -27,17 +27,12 @@
27 27 }
28 28
29 29
30 30 public function view_order_log() {
31 - $order_id = absint( $this->params['order_id'] );
32 - $order = ( new OsOrderModel() )->where( [ LATEPOINT_TABLE_ORDERS . '.id' => $order_id ] )->filter_allowed_records()->set_limit( 1 )->get_results_as_models();
33 - if ( ! $order ) {
34 - $this->access_not_allowed();
35 - return;
36 - }
31 + $activities = new OsActivityModel();
32 + $activities = $activities->where( [ 'order_id' => absint($this->params['order_id']) ] )->order_by( 'id desc' )->get_results_as_models();
37 33
38 - $activities = new OsActivityModel();
39 - $activities = $activities->where( [ 'order_id' => $order_id ] )->order_by( 'id desc' )->get_results_as_models();
34 + $order = new OsOrderModel( $this->params['order_id'] );
40 35
41 36 $this->vars['order'] = $order;
42 37 $this->vars['activities'] = $activities;
43 38
@@ -46,38 +41,36 @@
46 41
47 42
48 43 public function continue_order_intent() {
49 44 $order_intent_key = $this->params['order_intent_key'];
50 - $order_intent = OsOrderIntentHelper::get_order_intent_by_intent_key( $order_intent_key );
45 + $order_intent = OsOrderIntentHelper::get_order_intent_by_intent_key($order_intent_key);
51 46
52 - if ( $order_intent->is_new_record() ) {
47 + if($order_intent->is_new_record()){
53 48 http_response_code( 400 );
54 - OsDebugHelper::log( 'Order intent not found, id:' . $order_intent_key );
49 + OsDebugHelper::log('Order intent not found, id:'. $order_intent_key);
55 50 exit();
56 - } else {
51 + }else{
57 52
58 53 $order_intent->convert_to_order();
59 54
60 - nocache_headers();
61 - // Redirect with the intent key in either outcome, front.js resumes the booking flow at the confirmation
62 - // step when convert_to_order succeeded, or at the payment step when it didn't (e.g. cancelled Mollie/PayPal
63 - // payment). The "double payment information" issue is fixed at the cart-model layer (see
64 - // OsCartModel::clear) without that fix, the resume lightbox showed every cart item twice.
65 - wp_redirect( $order_intent->get_page_url_with_intent(), 302 );
66 - exit;
55 + if ( $order_intent ) {
56 + nocache_headers();
57 + wp_redirect( $order_intent->get_page_url_with_intent(), 302 );
58 + }
67 59 }
60 +
68 61 }
69 62
70 63
71 64 public function continue_transaction_intent() {
72 - $intent_key = $this->params['transaction_intent_key'];
73 - $transaction_intent = OsTransactionIntentHelper::get_transaction_intent_by_intent_key( $intent_key );
65 + $intent_key = $this->params['transaction_intent_key'];
66 + $transaction_intent = OsTransactionIntentHelper::get_transaction_intent_by_intent_key($intent_key);
74 67
75 - if ( $transaction_intent->is_new_record() ) {
68 + if($transaction_intent->is_new_record()){
76 69 http_response_code( 400 );
77 - OsDebugHelper::log( 'Transaction intent not found, id:' . $intent_key );
70 + OsDebugHelper::log('Transaction intent not found, id:'. $intent_key);
78 71 exit();
79 - } else {
72 + }else{
80 73 $transaction_intent->convert_to_transaction();
81 74
82 75 if ( $transaction_intent ) {
83 76 nocache_headers();
@@ -109,17 +102,8 @@
109 102 $validation_errors = [];
110 103
111 104 if ( ! empty( $this->params['order']['id'] ) ) {
112 105 $this->check_nonce( 'edit_order_' . $this->params['order']['id'] );
113 - $allowed_order = ( new OsOrderModel() )->where( [ LATEPOINT_TABLE_ORDERS . '.id' => absint( $this->params['order']['id'] ) ] )->filter_allowed_records()->set_limit( 1 )->get_results_as_models();
114 - if ( ! $allowed_order ) {
115 - $this->send_json(
116 - array(
117 - 'status' => LATEPOINT_STATUS_ERROR,
118 - 'message' => __( 'Not Allowed', 'latepoint' ),
119 - )
120 - );
121 - }
122 106 } else {
123 107 $this->check_nonce( 'new_order' );
124 108 }
125 109
@@ -125,10 +109,12 @@
125 109
126 110 $order_params = $this->params['order'];
127 111 $customer_params = $this->params['customer'];
128 112
113 +
129 114 $order_items_params = $this->params['order_items'] ?? [];
130 115
116 +
131 117 $order = new OsOrderModel( $order_params['id'] );
132 118
133 119 // if we are updating a order - save a copy by cloning old order
134 120 $old_order = ( $order->is_new_record() ) ? [] : clone $order;
@@ -133,10 +119,10 @@
133 119 // if we are updating a order - save a copy by cloning old order
134 120 $old_order = ( $order->is_new_record() ) ? [] : clone $order;
135 121 $order->set_data( $order_params );
136 122
123 +
137 124 // first validate & create a customer the customer
138 - $old_customer_data = [];
139 125 if ( $order->customer_id ) {
140 126 $customer = new OsCustomerModel( $order->customer_id );
141 127 $old_customer_data = $customer->get_data_vars();
142 128 $is_new_customer = false;
@@ -143,66 +129,42 @@
143 129 } else {
144 130 $customer = new OsCustomerModel();
145 131 $is_new_customer = true;
146 132 }
147 -
148 - // The submitted customer[...] fields may overwrite an EXISTING customer record only when
149 - // (1) the current user is authorized to edit that customer
150 - // (2) the customer is is an allowed customer list.
151 - // Attaching any customer to an order is always permitted; new customers proceed normally.
152 - if ( $is_new_customer || $customer->is_new_record() ) {
153 - $can_modify_customer = true;
154 - } else {
155 - $wp_link_is_safe = empty( $customer->wordpress_user_id )
156 - || OsCustomerHelper::is_wp_user_safe_for_customer_link( (int) $customer->wordpress_user_id );
157 - $can_modify_customer = $wp_link_is_safe
158 - && OsRolesHelper::can_user_make_action_on_model_record( $customer, 'edit' );
159 - }
160 -
161 - if ( $can_modify_customer ) {
162 - // Set customer data only if is allowed. Prevent mass assignment of wordpress_user_id.
163 - // Use admin scope for backend panel users (admin, agent, custom roles), otherwise restrict to public fields
164 - $customer->set_data( $customer_params, OsAuthHelper::get_current_user()->has_backend_access() ? LATEPOINT_PARAMS_SCOPE_ADMIN : LATEPOINT_PARAMS_SCOPE_PUBLIC );
165 - if ( $customer->save() ) {
166 - if ( $is_new_customer ) {
167 - do_action( 'latepoint_customer_created', $customer );
168 - $this->fields_to_update['order[customer_id]'] = $customer->id;
169 - } else {
170 - do_action( 'latepoint_customer_updated', $customer, $old_customer_data );
171 - }
172 -
173 - $order->customer_id = $customer->id;
133 + $customer->set_data( $customer_params );
134 + if ( $customer->save() ) {
135 + if ( $is_new_customer ) {
136 + do_action( 'latepoint_customer_created', $customer );
137 + $this->fields_to_update['order[customer_id]'] = $customer->id;
174 138 } else {
175 - $this->send_json(
176 - [
177 - 'status' => LATEPOINT_STATUS_ERROR,
178 - // translators: %s is the description of an error
179 - 'message' => sprintf( __( 'Error: %s', 'latepoint' ), implode( ', ', $customer->get_error_messages() ) ),
180 - ]
181 - );
139 + do_action( 'latepoint_customer_updated', $customer, $old_customer_data );
182 140 }
141 +
142 + $order->customer_id = $customer->id;
143 + }else{
144 + $this->send_json( [
145 + 'status' => LATEPOINT_STATUS_ERROR,
146 + // translators: %s is the description of an error
147 + 'message' => sprintf(__( 'Error: %s', 'latepoint'), implode( ', ', $customer->get_error_messages() ) ),
148 + ]
149 + );
183 150 }
184 151
185 152 // validate order items
186 153 foreach ( $order_items_params as $order_item_id => $order_item ) {
187 154 foreach ( $order_item['bookings'] as $booking_id => $booking_params ) {
188 - $booking = OsOrdersHelper::create_booking_object_from_booking_data_form( $booking_params );
189 - $booking->customer_id = $order->customer_id;
190 - if ( ! $booking->validate( false, [ 'order_item_id' ] ) ) {
191 - $validation_errors = array_merge( $validation_errors, $booking->get_error_messages() );
155 + $booking = OsOrdersHelper::create_booking_object_from_booking_data_form( $booking_params );
156 + $booking->customer_id = $order->customer_id;
157 + if ( !$booking->validate(false, ['order_item_id']) ) {
158 + $validation_errors = array_merge($validation_errors, $booking->get_error_messages());
192 159 }
193 160 }
194 161 }
195 162
196 163 // check if there are errors saving bookings
197 - if ( $validation_errors ) {
164 + if($validation_errors){
198 165 // translators: %s is the description of an error
199 - $this->send_json(
200 - array(
201 - 'status' => LATEPOINT_STATUS_ERROR,
202 - 'message' => sprintf( __( 'Error: %s', 'latepoint' ), implode( ', ', $validation_errors ) ),
203 - )
204 - );
166 + $this->send_json( array( 'status' => LATEPOINT_STATUS_ERROR, 'message' => sprintf(__( 'Error: %s', 'latepoint'), implode( ', ', $validation_errors ) ) ) );
205 167 }
206 168
207 169 if ( $old_order ) {
208 170 // make sure old order items are still there, if not - remove them
@@ -232,9 +194,9 @@
232 194 * @hook latepoint_order_item_deleted
233 195 *
234 196 */
235 197 do_action( 'latepoint_order_item_deleted', $order_item_id_to_delete );
236 - OsActivitiesHelper::log_order_item_deleted( $order_item );
198 + OsActivitiesHelper::log_order_item_deleted($order_item);
237 199 } else {
238 200 // it's a bundle order item - search for bookings that are attached to this bundle and remove them if not found in passed params list
239 201 if ( $order_item->is_bundle() ) {
240 202 $old_bundle_bookings = OsOrdersHelper::get_bookings_for_order_item( $order_item->id );
@@ -266,9 +228,9 @@
266 228 * @hook latepoint_booking_deleted
267 229 *
268 230 */
269 231 do_action( 'latepoint_booking_deleted', $booking_id_to_delete );
270 - OsActivitiesHelper::log_booking_deleted( $old_bundle_booking );
232 + OsActivitiesHelper::log_booking_deleted($old_bundle_booking);
271 233 } else {
272 234 OsDebugHelper::log( 'Not allowed: Deleting Booking', 'permissions_error' );
273 235 }
274 236 }
@@ -288,28 +250,28 @@
288 250 }
289 251
290 252 // save price breakdown, we only need to save before and after subtotal, as total and subtotal values are stored on the Order record itself
291 253 if ( ! empty( $this->params['price_breakdown'] ) ) {
292 - $order->price_breakdown = wp_json_encode( OsOrdersHelper::generate_price_breakdown_from_params( $this->params['price_breakdown'] ) );
254 + $order->price_breakdown = wp_json_encode( OsOrdersHelper::generate_price_breakdown_from_params($this->params['price_breakdown']) );
293 255 }
294 256
295 257 // Check if we have to create a payment request
296 - $create_payment_request = ( sanitize_text_field( $this->params['create_payment_request'] ?? '' ) == LATEPOINT_VALUE_ON );
297 - if ( $create_payment_request ) {
298 - $payment_request_data = $this->params['payment_request'];
299 - $payment_request_data['portion'] = sanitize_text_field( $payment_request_data['portion'] );
300 - $payment_request_data['charge_amount'] = sanitize_text_field( $payment_request_data[ 'charge_amount_' . $payment_request_data['portion'] ] );
301 - $payment_request_data['due_at'] = OsTimeHelper::custom_datetime_utc_in_db_format( sanitize_text_field( $payment_request_data['due_at'] ) . ' 23:59:59' );
302 - $order->set_initial_payment_data_value( 'time', LATEPOINT_PAYMENT_TIME_NOW, false );
303 - $order->set_initial_payment_data_value( 'portion', $payment_request_data['portion'], false );
304 - $order->set_initial_payment_data_value( 'charge_amount', OsMoneyHelper::convert_amount_from_money_input_to_db_format( $payment_request_data['charge_amount'] ) );
258 + $create_payment_request = (sanitize_text_field($this->params['create_payment_request'] ?? '') == LATEPOINT_VALUE_ON);
259 + if($create_payment_request){
260 + $payment_request_data = $this->params['payment_request'];
261 + $payment_request_data['portion'] = sanitize_text_field($payment_request_data['portion']);
262 + $payment_request_data['charge_amount'] = sanitize_text_field($payment_request_data['charge_amount_'.$payment_request_data['portion']]);
263 + $payment_request_data['due_at'] = OsTimeHelper::custom_datetime_utc_in_db_format(sanitize_text_field($payment_request_data['due_at']).' 23:59:59');
264 + $order->set_initial_payment_data_value('time', LATEPOINT_PAYMENT_TIME_NOW, false);
265 + $order->set_initial_payment_data_value('portion', $payment_request_data['portion'], false);
266 + $order->set_initial_payment_data_value('charge_amount', OsMoneyHelper::convert_amount_from_money_input_to_db_format($payment_request_data['charge_amount'], false));
305 267
306 268 $payment_request = new OsPaymentRequestModel();
307 269
308 - $payment_request = $payment_request->set_data( $payment_request_data );
270 + $payment_request = $payment_request->set_data($payment_request_data);
309 271
310 - } else {
311 - $order->set_initial_payment_data_value( 'time', LATEPOINT_PAYMENT_TIME_LATER );
272 + }else{
273 + $order->set_initial_payment_data_value('time', LATEPOINT_PAYMENT_TIME_LATER);
312 274 $payment_request = null;
313 275 }
314 276
315 277 if ( $order->save() ) {
@@ -382,11 +344,11 @@
382 344 $order_item_model->save();
383 345 }
384 346 if ( $old_booking ) {
385 347 do_action( 'latepoint_booking_updated', $booking, $old_booking );
386 - if ( $old_booking->status != $booking->status ) {
348 + if($old_booking->status != $booking->status){
387 349 do_action( 'latepoint_booking_change_status', $booking, $old_booking );
388 - OsActivitiesHelper::log_booking_change_status( $booking, $old_booking );
350 + OsActivitiesHelper::log_booking_change_status($booking, $old_booking);
389 351 }
390 352 } else {
391 353 do_action( 'latepoint_booking_created', $booking );
392 354 }
@@ -413,9 +375,9 @@
413 375 *
414 376 */
415 377 do_action( 'latepoint_order_updated', $order, $old_order );
416 378 } else {
417 - OsInvoicesHelper::create_invoices_for_new_order( $order, $payment_request );
379 + OsInvoicesHelper::create_invoices_for_new_order($order, $payment_request);
418 380 /**
419 381 * Order was created
420 382 *
421 383 * @param {OsOrderModel} $order instance of order model that was created
@@ -426,27 +388,23 @@
426 388 */
427 389 do_action( 'latepoint_order_created', $order );
428 390 }
429 391
430 - $status = LATEPOINT_STATUS_SUCCESS;
392 + $status = LATEPOINT_STATUS_SUCCESS;
431 393 // translators: %s is a link to the updated order
432 394 $response_html = sprintf( ( ( $old_order ) ? __( 'Order Updated ID: %s', 'latepoint' ) : __( 'Order Created ID: %s', 'latepoint' ) ), '<span class="os-notification-link" ' . OsOrdersHelper::quick_order_btn_html( $order->id ) . '>' . $order->id . '</span>' );
433 395 } else {
434 396 OsDebugHelper::log( 'Error saving order (admin)', 'order_save_error', $order->get_error_messages() );
435 - $status = LATEPOINT_STATUS_ERROR;
397 + $status = LATEPOINT_STATUS_ERROR;
436 398
437 399 // translators: %s is an error message
438 - $response_html = sprintf( __( 'Error: %s', 'latepoint' ), implode( ', ', $order->get_error_messages() ) );
400 + $response_html = sprintf(__( 'Error: %s', 'latepoint'), implode( ', ', $order->get_error_messages() ));
439 401 }
440 402
441 403 if ( $this->get_return_format() == 'json' ) {
442 - $this->send_json(
443 - array(
444 - 'status' => $status,
445 - 'message' => $response_html,
446 - )
447 - );
404 + $this->send_json( array( 'status' => $status, 'message' => $response_html ) );
448 405 }
406 +
449 407 }
450 408
451 409
452 410 // reloads a section of a quick edit form that has a price breakdown
@@ -470,9 +428,9 @@
470 428 *
471 429 * @param {OsOrderModel} $order Order for which to reload price breakdown
472 430 * @returns {OsOrderModel} Filtered order with updated price breakdown rows
473 431 */
474 - $order = apply_filters( 'latepoint_order_reload_price_breakdown', $order );
432 + $order = apply_filters( 'latepoint_order_reload_price_breakdown', $order );
475 433
476 434 // we don't need to generate balance and payments info as it is printed in a separate block
477 435 $this->vars['price_breakdown_rows'] = $order->generate_price_breakdown_rows( [ 'balance', 'payments' ], true );
478 436
@@ -480,9 +438,9 @@
480 438 $this->format_render( __FUNCTION__ );
481 439 }
482 440
483 441 function reload_balance_and_payments() {
484 - $order_params = $this->params['order'];
442 + $order_params = $this->params['order'];
485 443 $order_items_params = $this->params['order_items'] ?? [];
486 444
487 445 $order = new OsOrderModel();
488 446 $order->set_data( $order_params );
@@ -508,20 +466,15 @@
508 466
509 467 function generate_bundle_order_item_block() {
510 468 $bundle = new OsBundleModel( $this->params['bundle_id'] );
511 469
512 - $order_item_id = OsUtilHelper::generate_form_id();
513 - $response_html = '<div class="order-item order-item-variant-bundle" data-order-item-id="' . $order_item_id . '">';
470 + $order_item_id = OsUtilHelper::generate_form_id();
471 + $response_html = '<div class="order-item order-item-variant-bundle" data-order-item-id="' . $order_item_id . '">';
514 472 $response_html .= OsOrdersHelper::generate_order_item_pill_for_bundle( $bundle, $order_item_id );
515 473 $response_html .= '</div>';
516 474
517 475 if ( $this->get_return_format() == 'json' ) {
518 - $this->send_json(
519 - array(
520 - 'status' => LATEPOINT_STATUS_SUCCESS,
521 - 'message' => $response_html,
522 - )
523 - );
476 + $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $response_html ) );
524 477 }
525 478 }
526 479
527 480 function generate_booking_order_item_block() {
@@ -531,11 +484,11 @@
531 484 $booking = OsBookingHelper::build_booking_model_from_item_data( json_decode( base64_decode( $this->params['booking_item_data'] ), true ) );
532 485 $response_html = OsOrdersHelper::booking_data_form_for_order_item_id( $this->params['order_item_id'], $booking, LATEPOINT_ITEM_VARIANT_BUNDLE, false );
533 486 } else {
534 487 // regular booking
535 - $booking = OsBookingHelper::prepare_new_from_params( $this->params );
536 - $order_item_id = OsUtilHelper::generate_form_id();
537 - $response_html = '<div class="order-item order-item-variant-booking" data-order-item-id="' . $order_item_id . '">';
488 + $booking = OsBookingHelper::prepare_new_from_params( $this->params );
489 + $order_item_id = OsUtilHelper::generate_form_id();
490 + $response_html = '<div class="order-item order-item-variant-booking" data-order-item-id="' . $order_item_id . '">';
538 491 $response_html .= OsOrdersHelper::booking_data_form_for_order_item_id( $order_item_id, $booking, LATEPOINT_ITEM_VARIANT_BOOKING, false );
539 492 $response_html .= '</div>';
540 493 }
541 494
@@ -540,14 +493,9 @@
540 493 }
541 494
542 495
543 496 if ( $this->get_return_format() == 'json' ) {
544 - $this->send_json(
545 - array(
546 - 'status' => LATEPOINT_STATUS_SUCCESS,
547 - 'message' => $response_html,
548 - )
549 - );
497 + $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $response_html ) );
550 498 }
551 499 }
552 500
553 501 function fold_booking_data_form() {
@@ -562,14 +510,9 @@
562 510 $response_html = OsOrdersHelper::generate_order_item_pill_for_bundle_booking( $booking, $order_item_id );
563 511 } else {
564 512 $response_html = OsOrdersHelper::generate_order_item_pill_for_booking( $booking, $order_item_id );
565 513 }
566 - $this->send_json(
567 - array(
568 - 'status' => LATEPOINT_STATUS_SUCCESS,
569 - 'message' => $response_html,
570 - )
571 - );
514 + $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $response_html ) );
572 515 }
573 516
574 517 function generate_order_item_booking_data_form() {
575 518 $order_item = new OsOrderItemModel();
@@ -593,14 +536,9 @@
593 536 $booking = OsBookingHelper::prepare_new_from_params( $this->params );
594 537 }
595 538
596 539 $response_html = OsOrdersHelper::booking_data_form_for_order_item_id( $order_item->get_form_id(), $booking, $order_item->variant );
597 - $this->send_json(
598 - array(
599 - 'status' => LATEPOINT_STATUS_SUCCESS,
600 - 'message' => $response_html,
601 - )
602 - );
540 + $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $response_html ) );
603 541 }
604 542
605 543 function quick_edit() {
606 544
@@ -628,13 +566,13 @@
628 566 }
629 567
630 568 if ( $order_id ) {
631 569 // EDITING EXISTING ORDER
632 - $order = ( new OsOrderModel() )->where( [ LATEPOINT_TABLE_ORDERS . '.id' => absint( $order_id ) ] )->filter_allowed_records()->set_limit( 1 )->get_results_as_models();
633 - if ( ! $order ) {
634 - $this->access_not_allowed();
635 - return;
636 - }
570 + $order = new OsOrderModel( $order_id );
571 + // TODO add this check for order
572 +// if(!OsRolesHelper::can_user_make_action_on_model_record($order, 'view')){
573 +// $this->send_json(array('status' => LATEPOINT_STATUS_ERROR, 'message' => 'Not Allowed'));
574 +// }
637 575
638 576 $transactions = $order->get_transactions();
639 577
640 578 } else {
@@ -687,52 +625,27 @@
687 625 $this->vars['default_fields_for_customer'] = OsSettingsHelper::get_default_fields_for_customer();
688 626 $this->format_render( __FUNCTION__ );
689 627 }
690 628
691 - /**
692 - * Legacy method. Not used anymore.
693 - * But kept for backward compatibility with old code that might be using it
694 - */
695 - // public function edit_form() {
696 - // if ( empty( $this->params['id'] ) ) {
697 - // $order = new OsOrderModel();
698 - // } else {
699 - // $order = ( new OsOrderModel() )->where( [ LATEPOINT_TABLE_ORDERS . '.id' => absint( $this->params['id'] ) ] )->filter_allowed_records()->set_limit( 1 )->get_results_as_models();
700 - // if ( ! $order ) {
701 - // $this->send_json(
702 - // array(
703 - // 'status' => LATEPOINT_STATUS_ERROR,
704 - // 'message' => __( 'Not Allowed', 'latepoint' ),
705 - // )
706 - // );
707 - // return;
708 - // }
709 - // }
710 - // // legacy fix for older orders that didn't have portion column, get it from connected order
711 - // if ( ! $order->is_new_record() && empty( $order->payment_portion ) && ! empty( $order->booking_id ) ) {
712 - // $booking = new OsBookingModel( $order->booking_id );
713 - // if ( ! empty( $booking->id ) ) {
714 - // $order->payment_portion = $booking->payment_portion;
715 - // }
716 - // }
717 - // $this->vars['real_or_rand_id'] = ( $order->is_new_record() ) ? 'new_order_' . OsUtilHelper::random_text( 'alnum', 5 ) : $order->id;
718 - // $this->vars['order'] = $order;
629 + public function edit_form() {
630 + $order = ( empty( $this->params['id'] ) ) ? new OsOrderModel() : new OsOrderModel( $this->params['id'] );
631 + // legacy fix for older orders that didn't have portion column, get it from connected order
632 + if ( ! $order->is_new_record() && empty( $order->payment_portion ) && ! empty( $order->booking_id ) ) {
633 + $booking = new OsBookingModel( $order->booking_id );
634 + if ( ! empty( $booking->id ) ) {
635 + $order->payment_portion = $booking->payment_portion;
636 + }
637 + }
638 + $this->vars['real_or_rand_id'] = ( $order->is_new_record() ) ? 'new_order_' . OsUtilHelper::random_text( 'alnum', 5 ) : $order->id;
639 + $this->vars['order'] = $order;
719 640
720 - // $this->format_render( __FUNCTION__ );
721 - // }
641 + $this->format_render( __FUNCTION__ );
642 + }
722 643
723 644 public function destroy() {
724 645 if ( filter_var( $this->params['id'], FILTER_VALIDATE_INT ) ) {
725 646 $this->check_nonce( 'destroy_order_' . $this->params['id'] );
726 - $order = ( new OsOrderModel() )->where( [ LATEPOINT_TABLE_ORDERS . '.id' => absint( $this->params['id'] ) ] )->filter_allowed_records()->set_limit( 1 )->get_results_as_models();
727 - if ( ! $order ) {
728 - $this->send_json(
729 - array(
730 - 'status' => LATEPOINT_STATUS_ERROR,
731 - 'message' => __( 'Not Allowed', 'latepoint' ),
732 - )
733 - );
734 - }
647 + $order = new OsOrderModel( $this->params['id'] );
735 648 if ( $order->delete() ) {
736 649 $status = LATEPOINT_STATUS_SUCCESS;
737 650 $response_html = __( 'Order Removed', 'latepoint' );
738 651 } else {
@@ -743,14 +656,9 @@
743 656 $status = LATEPOINT_STATUS_ERROR;
744 657 $response_html = __( 'Error Removing Order', 'latepoint' );
745 658 }
746 659 if ( $this->get_return_format() == 'json' ) {
747 - $this->send_json(
748 - array(
749 - 'status' => $status,
750 - 'message' => $response_html,
751 - )
752 - );
660 + $this->send_json( array( 'status' => $status, 'message' => $response_html ) );
753 661 }
754 662 }
755 663
756 664 /*
@@ -758,10 +666,10 @@
758 666 */
759 667
760 668 public function index() {
761 669
762 - $per_page = OsSettingsHelper::get_number_of_records_per_page();
763 - $page_number = isset( $this->params['page_number'] ) ? $this->params['page_number'] : 1;
670 + $per_page = OsSettingsHelper::get_number_of_records_per_page();
671 + $page_number = isset($this->params['page_number']) ? $this->params['page_number'] : 1;
764 672
765 673 $this->vars['page_header'] = false;
766 674
767 675 $orders = new OsOrderModel();
@@ -794,9 +702,9 @@
794 702 $orders->select( LATEPOINT_TABLE_ORDERS . '.*, ' . LATEPOINT_TABLE_CUSTOMERS . '.first_name, ' . LATEPOINT_TABLE_CUSTOMERS . '.last_name' );
795 703 $orders->join( LATEPOINT_TABLE_CUSTOMERS, [ 'id' => LATEPOINT_TABLE_ORDERS . '.customer_id' ] );
796 704
797 705 $query_args[ 'concat_ws(" ", ' . LATEPOINT_TABLE_CUSTOMERS . '.first_name,' . LATEPOINT_TABLE_CUSTOMERS . '.last_name) LIKE' ] = '%' . $filter['customer']['full_name'] . '%';
798 - $this->vars['customer_name_query'] = $filter['customer']['full_name'];
706 + $this->vars['customer_name_query'] = $filter['customer']['full_name'];
799 707
800 708 }
801 709
802 710 if ( ! empty( $filter['created_at_from'] ) && ! empty( $filter['created_at_to'] ) ) {
@@ -807,11 +715,11 @@
807 715
808 716
809 717 // OUTPUT CSV IF REQUESTED
810 718 if ( isset( $this->params['download'] ) && $this->params['download'] == 'csv' ) {
811 - $csv_filename = 'orders_' . OsUtilHelper::random_text() . '.csv';
719 + $csv_filename = 'payments_' . OsUtilHelper::random_text() . '.csv';
812 720
813 - header( 'Content-Type: text/csv' );
721 + header( "Content-Type: text/csv" );
814 722 header( "Content-Disposition: attachment; filename={$csv_filename}" );
815 723
816 724 $labels_row = [
817 725 __( 'ID', 'latepoint' ),
@@ -822,9 +730,9 @@
822 730 __( 'Method', 'latepoint' ),
823 731 __( 'Amount', 'latepoint' ),
824 732 __( 'Status', 'latepoint' ),
825 733 __( 'Type', 'latepoint' ),
826 - __( 'Date', 'latepoint' ),
734 + __( 'Date', 'latepoint' )
827 735 ];
828 736
829 737
830 738 $orders_data = [];
@@ -841,9 +749,9 @@
841 749 $order->booking_id,
842 750 ( $order->customer_id ? $order->customer->full_name : 'n/a' ),
843 751 $order->processor,
844 752 $order->payment_method,
845 - OsMoneyHelper::format_price( $order->total, true, false ),
753 + OsMoneyHelper::format_price( $order->amount, true, false ),
846 754 $order->status,
847 755 $order->kind,
848 756 $order->created_at,
849 757 ];
@@ -849,8 +757,9 @@
849 757 ];
850 758 $values_row = apply_filters( 'latepoint_order_row_for_csv_export', $values_row, $order, $this->params );
851 759 $orders_data[] = $values_row;
852 760 }
761 +
853 762 }
854 763
855 764 $orders_data = apply_filters( 'latepoint_orders_data_for_csv_export', $orders_data, $this->params );
856 765 OsCSVHelper::array_to_csv( $orders_data );
@@ -882,22 +791,20 @@
882 791
883 792 $this->vars['showing_from'] = ( ( $page_number - 1 ) * $per_page ) ? ( ( $page_number - 1 ) * $per_page ) : 1;
884 793 $this->vars['showing_to'] = min( $page_number * $per_page, $total_orders );
885 794
886 - $this->format_render(
887 - [
888 - 'json_view_name' => '_table_body',
889 - 'html_view_name' => __FUNCTION__,
890 - ],
891 - [],
892 - [
893 - 'total_pages' => $total_pages,
894 - 'showing_from' => $this->vars['showing_from'],
895 - 'showing_to' => $this->vars['showing_to'],
896 - 'total_records' => $total_orders,
897 - ]
898 - );
795 + $this->format_render( [
796 + 'json_view_name' => '_table_body',
797 + 'html_view_name' => __FUNCTION__
798 + ], [], [
799 + 'total_pages' => $total_pages,
800 + 'showing_from' => $this->vars['showing_from'],
801 + 'showing_to' => $this->vars['showing_to'],
802 + 'total_records' => $total_orders
803 + ] );
899 804 }
805 +
806 +
900 807 }
901 808
902 809
903 -endif;
810 +endif;