activities_helper.php
3 months ago
agent_helper.php
3 months ago
analytics_helper.php
1 week ago
auth_helper.php
1 week ago
blocks_helper.php
3 weeks ago
booking_helper.php
5 days ago
bricks_helper.php
3 months ago
bundles_helper.php
5 days ago
calendar_helper.php
4 days ago
carts_helper.php
3 months ago
connector_helper.php
3 months ago
csv_helper.php
3 months ago
customer_helper.php
1 month ago
customer_import_helper.php
1 month ago
database_helper.php
1 week ago
debug_helper.php
3 months ago
defaults_helper.php
3 months ago
elementor_helper.php
3 months ago
email_helper.php
3 months ago
encrypt_helper.php
3 months ago
events_helper.php
3 months ago
form_helper.php
3 months ago
icalendar_helper.php
3 months ago
image_helper.php
3 months ago
invoices_helper.php
3 weeks ago
license_helper.php
3 months ago
location_helper.php
3 months ago
marketing_systems_helper.php
3 months ago
meeting_systems_helper.php
3 months ago
menu_helper.php
3 weeks ago
meta_helper.php
3 months ago
migrations_helper.php
3 months ago
money_helper.php
3 months ago
notifications_helper.php
3 months ago
nps_survey_helper.php
3 months ago
order_intent_helper.php
2 months ago
orders_helper.php
1 day ago
otp_helper.php
3 months ago
pages_helper.php
3 months ago
params_helper.php
3 months ago
payments_helper.php
1 day ago
plugin_version_update_helper.php
2 months ago
price_breakdown_helper.php
3 months ago
process_jobs_helper.php
3 months ago
processes_helper.php
3 months ago
razorpay_connect_helper.php
1 week ago
replacer_helper.php
3 months ago
resource_helper.php
5 days ago
roles_helper.php
3 weeks ago
router_helper.php
3 months ago
service_helper.php
3 months ago
sessions_helper.php
3 months ago
settings_helper.php
1 week ago
short_links_systems_helper.php
3 months ago
shortcodes_helper.php
3 weeks ago
sms_helper.php
3 months ago
steps_helper.php
1 day ago
stripe_connect_helper.php
1 week ago
styles_helper.php
3 months ago
support_topics_helper.php
3 months ago
time_helper.php
2 months ago
timeline_helper.php
2 months ago
transaction_helper.php
1 month ago
transaction_intent_helper.php
3 months ago
util_helper.php
1 month ago
version_specific_updates_helper.php
3 months ago
whatsapp_helper.php
1 month ago
work_periods_helper.php
3 months ago
wp_datetime.php
3 months ago
wp_user_helper.php
3 months ago
migrations_helper.php
224 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (c) 2024 LatePoint LLC. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | class OsMigrationsHelper { |
| 7 | |
| 8 | function __construct() { |
| 9 | } |
| 10 | |
| 11 | public static function migrate_from_version_4(): string { |
| 12 | |
| 13 | |
| 14 | $bookings = new OsBookingModel(); |
| 15 | $bookings_to_migrate = $bookings->where( [ 'order_item_id' => 'IS NOT NULL' ] )->get_results_as_models(); |
| 16 | $report = ''; |
| 17 | foreach ( $bookings_to_migrate as $booking ) { |
| 18 | $report .= '<div>Start migrating totals for Booking ID:' . $booking->id . '</div>'; |
| 19 | $order_item = new OsOrderItemModel( $booking->order_item_id ); |
| 20 | if ( $order_item->is_new_record() ) { |
| 21 | $report .= '<div>Skipping as there is no associated order item</div>'; |
| 22 | } else { |
| 23 | $order_item->total = $booking->price; |
| 24 | $order_item->subtotal = $booking->subtotal; |
| 25 | $order_item->coupon_code = $booking->coupon_code; |
| 26 | $order_item->coupon_discount = $booking->coupon_discount; |
| 27 | $tax_total = $booking->total - $booking->subtotal - $booking->coupon_discount; |
| 28 | $order_item->tax_total = ( $tax_total > 0 ) ? $tax_total : 0; |
| 29 | $order_item->save(); |
| 30 | if ( $tax_total > 0 ) { |
| 31 | $order = new OsOrderModel( $order_item->order_id ); |
| 32 | if ( $order->is_new_record() ) { |
| 33 | |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | |
| 40 | $report = ''; |
| 41 | $bookings = new OsBookingModel(); |
| 42 | $v4_bookings = $bookings->where( [ 'order_item_id' => 'IS NULL' ] )->get_results_as_models(); |
| 43 | foreach ( $v4_bookings as $booking ) { |
| 44 | $report .= '<div>Start migrating Booking ID:' . $booking->id . '</div>'; |
| 45 | $order = new OsOrderModel(); |
| 46 | $order->total = $booking->price; |
| 47 | $order->subtotal = $booking->subtotal; |
| 48 | $order->customer_id = $booking->customer_id; |
| 49 | $order->coupon_code = $booking->coupon_code; |
| 50 | $order->coupon_discount = $booking->coupon_discount; |
| 51 | try { |
| 52 | $tax_total = $booking->total - $booking->subtotal - $booking->coupon_discount; |
| 53 | $order->tax_total = ( $tax_total > 0 ) ? $tax_total : 0; |
| 54 | } catch ( Exception $e ) { |
| 55 | $report .= '<div>Error calculating Total Tax:' . $booking->id . '</div>'; |
| 56 | $order->tax_total = 0; |
| 57 | } |
| 58 | $order->ip_address = $booking->ip_address; |
| 59 | $order->source_id = $booking->source_id; |
| 60 | $order->source_url = $booking->source_url; |
| 61 | $order->price_breakdown = $booking->get_meta_by_key( 'price_breakdown' ); |
| 62 | $order->customer_comment = $booking->customer_comment; |
| 63 | $order->updated_at = $booking->updated_at; |
| 64 | $order->created_at = $booking->created_at; |
| 65 | if ( $booking->status == LATEPOINT_BOOKING_STATUS_CANCELLED ) { |
| 66 | $order->status = LATEPOINT_ORDER_STATUS_CANCELLED; |
| 67 | } else { |
| 68 | $order->status = $booking->is_upcoming() ? LATEPOINT_ORDER_STATUS_OPEN : LATEPOINT_ORDER_STATUS_COMPLETED; |
| 69 | } |
| 70 | $order->fulfillment_status = ( $order->status == LATEPOINT_ORDER_STATUS_COMPLETED ) ? LATEPOINT_ORDER_FULFILLMENT_STATUS_FULFILLED : LATEPOINT_ORDER_FULFILLMENT_STATUS_NOT_FULFILLED; |
| 71 | $order->payment_status = $booking->payment_status; |
| 72 | if ( $order->save() ) { |
| 73 | $report .= '<div>Created Order ID:' . $order->id . '</div>'; |
| 74 | $order_item = new OsOrderItemModel(); |
| 75 | $order_item->order_id = $order->id; |
| 76 | $order_item->total = $order->total; |
| 77 | $order_item->subtotal = $order->subtotal; |
| 78 | $order_item->coupon_code = $order->coupon_code; |
| 79 | $order_item->coupon_discount = $order->coupon_discount; |
| 80 | $order_item->tax_total = $order->tax_total; |
| 81 | $order_item->variant = LATEPOINT_ITEM_VARIANT_BOOKING; |
| 82 | $order_item->updated_at = $booking->updated_at; |
| 83 | $order_item->created_at = $booking->created_at; |
| 84 | $order_item->item_data = wp_json_encode( $booking->generate_params_for_booking_form(), true ); |
| 85 | if ( $order_item->save() ) { |
| 86 | $report .= '<div>Created Order Item ID:' . $order_item->id . '</div>'; |
| 87 | $booking->order_item_id = $order_item->id; |
| 88 | if ( $booking->save() ) { |
| 89 | $report .= '<div>Migration Finished Booking ID:' . $booking->id . '</div>'; |
| 90 | } else { |
| 91 | $report .= '<div>Migration Error [booking]</div>'; |
| 92 | OsDebugHelper::log( 'Error updating booking for v4 booking', 'v4_update_migration_error', $booking->get_error_messages() ); |
| 93 | } |
| 94 | } else { |
| 95 | $report .= '<div>Migration Error [order item]</div>'; |
| 96 | OsDebugHelper::log( 'Error creating order item for v4 booking', 'v4_update_migration_error', $order_item->get_error_messages() ); |
| 97 | } |
| 98 | } else { |
| 99 | $report .= '<div>Migration Error [order]</div>'; |
| 100 | OsDebugHelper::log( 'Error creating order for v4 booking', 'v4_update_migration_error', $order->get_error_messages() ); |
| 101 | } |
| 102 | } |
| 103 | $transactions = new OsTransactionModel(); |
| 104 | $v4_transactions = $transactions->where( [ 'order_id' => 'IS NULL' ] )->get_results_as_models(); |
| 105 | foreach ( $v4_transactions as $transaction ) { |
| 106 | $report .= '<div>Start migrating Transaction ID:' . $transaction->id . '</div>'; |
| 107 | if ( empty( $transaction->booking_id ) ) { |
| 108 | continue; |
| 109 | } |
| 110 | $booking = new OsBookingModel( $transaction->booking_id ); |
| 111 | if ( $booking->id && $booking->order_item_id ) { |
| 112 | $order_item = new OsOrderItemModel( $booking->order_item_id ); |
| 113 | if ( $order_item->id && $order_item->order_id ) { |
| 114 | $transaction->order_id = $order_item->order_id; |
| 115 | $transaction->kind = LATEPOINT_TRANSACTION_KIND_CAPTURE; |
| 116 | if ( ! empty( $transaction->funds_status ) ) { |
| 117 | switch ( $transaction->funds_status ) { |
| 118 | case 'captured': |
| 119 | $transaction->kind = LATEPOINT_TRANSACTION_KIND_CAPTURE; |
| 120 | break; |
| 121 | case 'authorized': |
| 122 | $transaction->kind = LATEPOINT_TRANSACTION_KIND_AUTHORIZATION; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | if ( $transaction->save() ) { |
| 127 | $report .= '<div>Migration Finished Transaction ID:' . $transaction->id . '</div>'; |
| 128 | } else { |
| 129 | $report .= '<div>Migration Error [transaction]</div>'; |
| 130 | OsDebugHelper::log( 'Error updating transaction', 'v4_update_migration_error', $transaction->get_error_messages() ); |
| 131 | } |
| 132 | } else { |
| 133 | $report .= '<div>Migration Error [order item ID: ' . $booking->order_item_id . ']</div>'; |
| 134 | } |
| 135 | } else { |
| 136 | $report .= '<div>Migration Error [booking ID: ' . $transaction->booking_id . ']</div>'; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // migrate steps |
| 141 | $report .= '<div>Migrating Steps</div>'; |
| 142 | self::migrate_step_descriptions_from_version_4(); |
| 143 | $report .= '<div>Finished Migrating Steps</div>'; |
| 144 | |
| 145 | return $report; |
| 146 | } |
| 147 | |
| 148 | public static function migrate_step_descriptions_from_version_4(): string { |
| 149 | global $wpdb; |
| 150 | $report = ''; |
| 151 | |
| 152 | $steps_table = $wpdb->prefix . 'latepoint_step_settings'; |
| 153 | $steps_rows = $wpdb->get_results( $wpdb->prepare( 'SELECT label, value, step FROM %i', esc_sql( $steps_table ) ) ); |
| 154 | $steps = []; |
| 155 | |
| 156 | $conversions = [ |
| 157 | 'sub_title' => 'main_panel_heading', |
| 158 | 'title' => 'side_panel_heading', |
| 159 | 'description' => 'side_panel_description', |
| 160 | 'icon_image_id' => 'side_panel_custom_image_id', |
| 161 | 'use_custom_image' => 'use_custom_image', |
| 162 | ]; |
| 163 | |
| 164 | foreach ( $steps_rows as $step ) { |
| 165 | if ( isset( $conversions[ $step->label ] ) ) { |
| 166 | $steps[ $step->step ][ $conversions[ $step->label ] ] = $step->value; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | $steps_settings = OsStepsHelper::get_steps_settings(); |
| 171 | |
| 172 | foreach ( $steps as $old_step_code => $data ) { |
| 173 | $new_step_codes = self::clean_up_step_code_from_version_4( $old_step_code ); |
| 174 | if ( ! empty( $new_step_codes ) ) { |
| 175 | foreach ( $new_step_codes as $step_code ) { |
| 176 | $steps_settings[ $step_code ]['main_panel_heading'] = $data['main_panel_heading']; |
| 177 | $steps_settings[ $step_code ]['side_panel_heading'] = $data['side_panel_heading']; |
| 178 | $steps_settings[ $step_code ]['side_panel_description'] = $data['side_panel_description']; |
| 179 | if ( $data['use_custom_image'] == 'on' && ! empty( $data['side_panel_custom_image_id'] ) ) { |
| 180 | $steps_settings[ $step_code ]['side_panel_custom_image_id'] = $data['side_panel_custom_image_id']; |
| 181 | } |
| 182 | $report .= '<div>- ' . $step_code . '</div>'; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | $steps_support_text = OsSettingsHelper::get_settings_value( 'steps_support_text', '' ); |
| 187 | if ( ! empty( $steps_support_text ) ) { |
| 188 | $steps_settings['shared']['steps_support_text'] = $steps_support_text; |
| 189 | $report .= '<div>- Support Text</div>'; |
| 190 | } |
| 191 | OsStepsHelper::save_steps_settings( $steps_settings ); |
| 192 | |
| 193 | return $report; |
| 194 | } |
| 195 | |
| 196 | public static function clean_up_step_code_from_version_4( string $step_code ): array { |
| 197 | $steps = [ |
| 198 | 'services' => [ |
| 199 | 'booking__services', |
| 200 | 'booking__service_extras', |
| 201 | 'booking__service_durations', |
| 202 | 'booking__group_bookings', |
| 203 | ], |
| 204 | 'locations' => [ 'booking__locations' ], |
| 205 | 'agents' => [ 'booking__agents' ], |
| 206 | 'datepicker' => [ 'booking__datepicker' ], |
| 207 | 'contact' => [ 'customer' ], |
| 208 | 'custom_fields_for_booking' => [ 'booking__custom_fields' ], |
| 209 | 'payment' => [ |
| 210 | 'payment__methods', |
| 211 | 'payment__times', |
| 212 | 'payment__portions', |
| 213 | 'payment__methods', |
| 214 | 'payment__processors', |
| 215 | 'payment__pay', |
| 216 | ], |
| 217 | 'verify' => [ 'verify' ], |
| 218 | 'confirmation' => [ 'confirmation' ], |
| 219 | ]; |
| 220 | |
| 221 | return $steps[ $step_code ] ?? []; |
| 222 | } |
| 223 | } |
| 224 |