| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yatra\Migration; |
| 4 |
|
| 5 |
use Yatra\Migration\MigrationProgress; |
| 6 |
use Yatra\Utils\Logger; |
| 7 |
|
| 8 |
// NOTE: YatraPro classes are NOT imported with `use` statements to avoid |
| 9 |
// fatal "class not found" errors when Yatra Pro is not active. |
| 10 |
// Instead, we check at runtime with class_exists(). |
| 11 |
|
| 12 |
class ServicesMigration extends BaseMigration |
| 13 |
{ |
| 14 |
public function __construct(MigrationProgress $service) |
| 15 |
{ |
| 16 |
parent::__construct($service); |
| 17 |
} |
| 18 |
|
| 19 |
public function run(): array |
| 20 |
{ |
| 21 |
global $wpdb; |
| 22 |
|
| 23 |
$migrated = 0; |
| 24 |
$skipped = 0; |
| 25 |
$failed = 0; |
| 26 |
|
| 27 |
try { |
| 28 |
// Check if Yatra Pro table classes are available at runtime |
| 29 |
if (!class_exists('\\YatraPro\\Database\\Tables\\AdditionalServicesTable') || |
| 30 |
!class_exists('\\YatraPro\\Database\\Tables\\TripAdditionalServicesTable')) { |
| 31 |
Logger::info('ServicesMigration: Yatra Pro table classes not available, skipping', [ |
| 32 |
'source' => 'migration', |
| 33 |
'data_type' => 'services', |
| 34 |
]); |
| 35 |
return compact('migrated', 'skipped', 'failed'); |
| 36 |
} |
| 37 |
|
| 38 |
// Check if Yatra Pro tables exist (created by Pro plugin) |
| 39 |
$services_table = \YatraPro\Database\Tables\AdditionalServicesTable::getTableName(); |
| 40 |
$trip_services_table = \YatraPro\Database\Tables\TripAdditionalServicesTable::getTableName(); |
| 41 |
|
| 42 |
$services_exists = $wpdb->get_var("SHOW TABLES LIKE '{$services_table}'"); |
| 43 |
$trip_services_exists = $wpdb->get_var("SHOW TABLES LIKE '{$trip_services_table}'"); |
| 44 |
|
| 45 |
if (!$services_exists || !$trip_services_exists) { |
| 46 |
return [ |
| 47 |
'migrated' => 0, |
| 48 |
'skipped' => 0, |
| 49 |
'failed' => 0, |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
// Check if services data exists in database (regardless of plugin/module status) |
| 54 |
$services_count = $wpdb->get_var( |
| 55 |
"SELECT COUNT(*) FROM {$wpdb->term_taxonomy} WHERE taxonomy = 'services'" |
| 56 |
); |
| 57 |
|
| 58 |
if ($services_count == 0) { |
| 59 |
return [ |
| 60 |
'migrated' => 0, |
| 61 |
'skipped' => 0, |
| 62 |
'failed' => 0, |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
// Get all service terms directly from database |
| 67 |
$services = $wpdb->get_results( |
| 68 |
"SELECT t.term_id, t.name, t.slug, tt.description |
| 69 |
FROM {$wpdb->terms} t |
| 70 |
INNER JOIN {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id |
| 71 |
WHERE tt.taxonomy = 'services'" |
| 72 |
); |
| 73 |
|
| 74 |
$total = count($services); |
| 75 |
foreach ($services as $service) { |
| 76 |
try { |
| 77 |
// Get service meta directly from database |
| 78 |
$price_type = $wpdb->get_var($wpdb->prepare( |
| 79 |
"SELECT meta_value FROM {$wpdb->termmeta} WHERE term_id = %d AND meta_key = 'price_type'", |
| 80 |
$service->term_id |
| 81 |
)) ?: 'fixed'; |
| 82 |
|
| 83 |
$price_per = $wpdb->get_var($wpdb->prepare( |
| 84 |
"SELECT meta_value FROM {$wpdb->termmeta} WHERE term_id = %d AND meta_key = 'price_per'", |
| 85 |
$service->term_id |
| 86 |
)) ?: 'tour'; |
| 87 |
|
| 88 |
$service_price = floatval($wpdb->get_var($wpdb->prepare( |
| 89 |
"SELECT meta_value FROM {$wpdb->termmeta} WHERE term_id = %d AND meta_key = 'service_price'", |
| 90 |
$service->term_id |
| 91 |
))); |
| 92 |
|
| 93 |
$is_required = (bool) $wpdb->get_var($wpdb->prepare( |
| 94 |
"SELECT meta_value FROM {$wpdb->termmeta} WHERE term_id = %d AND meta_key = 'is_required'", |
| 95 |
$service->term_id |
| 96 |
)); |
| 97 |
|
| 98 |
// Migrate service associations with tours/trips to custom table |
| 99 |
$services_migrated = $this->migrateServiceToTrips($service->term_id, $service->name, $service->description, $price_type, $price_per, $service_price, $is_required); |
| 100 |
|
| 101 |
$migrated += $services_migrated; |
| 102 |
} catch (\Exception $e) { |
| 103 |
$failed++; |
| 104 |
Logger::error("Service migration exception", [ |
| 105 |
'source' => 'migration', |
| 106 |
'service_id' => $service->term_id, |
| 107 |
'error' => $e->getMessage() |
| 108 |
]); |
| 109 |
} |
| 110 |
|
| 111 |
$this->updateProgress('services', 'running', $migrated, $skipped, $failed, $total, null, null); |
| 112 |
} |
| 113 |
|
| 114 |
} catch (\Throwable $e) { |
| 115 |
Logger::error("Services migration failed", [ |
| 116 |
'source' => 'migration', |
| 117 |
'error' => $e->getMessage() |
| 118 |
]); |
| 119 |
} |
| 120 |
|
| 121 |
return [ |
| 122 |
'migrated' => $migrated, |
| 123 |
'skipped' => $skipped, |
| 124 |
'failed' => $failed, |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Migrate service from old taxonomy to Yatra Pro additional services tables |
| 130 |
*/ |
| 131 |
private function migrateServiceToTrips(int $oldServiceId, string $serviceName, string $description, string $priceType, string $pricePer, float $servicePrice, bool $isRequired): int |
| 132 |
{ |
| 133 |
global $wpdb; |
| 134 |
$services_table = \YatraPro\Database\Tables\AdditionalServicesTable::getTableName(); |
| 135 |
$trip_services_table = \YatraPro\Database\Tables\TripAdditionalServicesTable::getTableName(); |
| 136 |
$migrated_count = 0; |
| 137 |
|
| 138 |
// First, check if service already exists in master table |
| 139 |
$service_id = $wpdb->get_var($wpdb->prepare( |
| 140 |
"SELECT id FROM {$services_table} WHERE name = %s", |
| 141 |
$serviceName |
| 142 |
)); |
| 143 |
|
| 144 |
$priceTypeNorm = in_array($priceType, ['fixed', 'percentage'], true) ? $priceType : 'fixed'; |
| 145 |
// Legacy 2.x used price_per=tour; 3.x enum is person|booking|day. |
| 146 |
switch ($pricePer) { |
| 147 |
case 'person': |
| 148 |
$pricePerNorm = 'person'; |
| 149 |
break; |
| 150 |
case 'day': |
| 151 |
$pricePerNorm = 'day'; |
| 152 |
break; |
| 153 |
case 'tour': |
| 154 |
case 'booking': |
| 155 |
default: |
| 156 |
$pricePerNorm = 'booking'; |
| 157 |
break; |
| 158 |
} |
| 159 |
|
| 160 |
// If service doesn't exist, create it in master table |
| 161 |
if (!$service_id) { |
| 162 |
$inserted = $wpdb->insert( |
| 163 |
$services_table, |
| 164 |
[ |
| 165 |
'name' => $serviceName, |
| 166 |
'description' => $description, |
| 167 |
'price' => $servicePrice, |
| 168 |
'price_type' => $priceTypeNorm, |
| 169 |
'price_per' => $pricePerNorm, |
| 170 |
'status' => 'publish', |
| 171 |
'applicable_to' => 'specific_trips', |
| 172 |
'is_required' => $isRequired ? 1 : 0, |
| 173 |
'sort_order' => 0, |
| 174 |
'created_at' => current_time('mysql'), |
| 175 |
], |
| 176 |
['%s', '%s', '%f', '%s', '%s', '%s', '%s', '%d', '%d', '%s'] |
| 177 |
); |
| 178 |
|
| 179 |
if ($inserted) { |
| 180 |
$service_id = $wpdb->insert_id; |
| 181 |
} else { |
| 182 |
return 0; |
| 183 |
} |
| 184 |
} else { |
| 185 |
} |
| 186 |
|
| 187 |
// Get all tours that had this service |
| 188 |
$tours = $wpdb->get_results($wpdb->prepare( |
| 189 |
"SELECT object_id FROM {$wpdb->term_relationships} |
| 190 |
WHERE term_taxonomy_id IN ( |
| 191 |
SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} |
| 192 |
WHERE term_id = %d AND taxonomy = 'services' |
| 193 |
)", |
| 194 |
$oldServiceId |
| 195 |
)); |
| 196 |
|
| 197 |
if (empty($tours)) { |
| 198 |
return 0; |
| 199 |
} |
| 200 |
|
| 201 |
foreach ($tours as $tour) { |
| 202 |
// Get the migrated trip ID |
| 203 |
$newTripId = $this->getRawPostMeta((int) $tour->object_id, '_migrated_to_trip_id'); |
| 204 |
|
| 205 |
if (!$newTripId) { |
| 206 |
continue; |
| 207 |
} |
| 208 |
|
| 209 |
// Check if relationship already exists |
| 210 |
if (!$this->isForceMigration()) { |
| 211 |
$existing = $wpdb->get_var($wpdb->prepare( |
| 212 |
"SELECT id FROM {$trip_services_table} WHERE trip_id = %d AND service_id = %d", |
| 213 |
$newTripId, |
| 214 |
$service_id |
| 215 |
)); |
| 216 |
|
| 217 |
if ($existing) { |
| 218 |
continue; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
// Insert trip-service relationship (minimal - just IDs) |
| 223 |
$inserted = $wpdb->insert( |
| 224 |
$trip_services_table, |
| 225 |
[ |
| 226 |
'trip_id' => $newTripId, |
| 227 |
'service_id' => $service_id, |
| 228 |
], |
| 229 |
['%d', '%d'] |
| 230 |
); |
| 231 |
|
| 232 |
if ($inserted) { |
| 233 |
$migrated_count++; |
| 234 |
} else { |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
return $migrated_count; |
| 239 |
} |
| 240 |
} |
| 241 |
|