| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yatra\Migrations; |
| 4 |
|
| 5 |
/** |
| 6 |
* Add Tax Fields to Bookings Migration |
| 7 |
* |
| 8 |
* Adds tax-related columns to the bookings table for comprehensive tax tracking |
| 9 |
* |
| 10 |
* @package Yatra\Migrations |
| 11 |
* @since 3.0.0 |
| 12 |
*/ |
| 13 |
class AddTaxFieldsToBookingsMigration |
| 14 |
{ |
| 15 |
/** |
| 16 |
* Migration version |
| 17 |
*/ |
| 18 |
const VERSION = '3.0.0'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Run the migration |
| 22 |
* |
| 23 |
* @return bool True if successful, false otherwise |
| 24 |
*/ |
| 25 |
public static function up(): bool |
| 26 |
{ |
| 27 |
global $wpdb; |
| 28 |
|
| 29 |
$tableName = $wpdb->prefix . 'yatra_new_bookings'; |
| 30 |
|
| 31 |
// Check if tax_amount column already exists |
| 32 |
$columnExists = $wpdb->get_var($wpdb->prepare( |
| 33 |
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS |
| 34 |
WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s AND COLUMN_NAME = 'tax_amount'", |
| 35 |
DB_NAME, |
| 36 |
$tableName |
| 37 |
)); |
| 38 |
|
| 39 |
if ($columnExists) { |
| 40 |
return true; // Already migrated |
| 41 |
} |
| 42 |
|
| 43 |
// Add tax columns |
| 44 |
$sql = "ALTER TABLE `{$tableName}` |
| 45 |
ADD COLUMN `subtotal` decimal(12,2) DEFAULT NULL COMMENT 'Base amount before tax' AFTER `discount_code`, |
| 46 |
ADD COLUMN `tax_amount` decimal(12,2) DEFAULT 0 COMMENT 'Total tax amount' AFTER `subtotal`, |
| 47 |
ADD COLUMN `tax_rate` decimal(5,2) DEFAULT 0 COMMENT 'Total tax rate percentage' AFTER `tax_amount`, |
| 48 |
ADD COLUMN `tax_inclusive` tinyint(1) DEFAULT 0 COMMENT 'Whether tax is included in price' AFTER `tax_rate`, |
| 49 |
ADD COLUMN `tax_details` text COMMENT 'JSON with detailed tax breakdown' AFTER `tax_inclusive`"; |
| 50 |
|
| 51 |
// Add indexes for tax fields |
| 52 |
$sql .= ", ADD INDEX `idx_tax_amount` (`tax_amount`)"; |
| 53 |
|
| 54 |
return $wpdb->query($sql) !== false; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Rollback the migration |
| 59 |
* |
| 60 |
* @return bool True if successful, false otherwise |
| 61 |
*/ |
| 62 |
public static function down(): bool |
| 63 |
{ |
| 64 |
global $wpdb; |
| 65 |
|
| 66 |
$tableName = $wpdb->prefix . 'yatra_new_bookings'; |
| 67 |
|
| 68 |
// Remove tax columns |
| 69 |
$sql = "ALTER TABLE `{$tableName}` |
| 70 |
DROP COLUMN `subtotal`, |
| 71 |
DROP COLUMN `tax_amount`, |
| 72 |
DROP COLUMN `tax_rate`, |
| 73 |
DROP COLUMN `tax_inclusive`, |
| 74 |
DROP COLUMN `tax_details`"; |
| 75 |
|
| 76 |
return $wpdb->query($sql) !== false; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get migration description |
| 81 |
* |
| 82 |
* @return string Migration description |
| 83 |
*/ |
| 84 |
public static function getDescription(): string |
| 85 |
{ |
| 86 |
return 'Add tax tracking fields to bookings table for comprehensive tax management'; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Update existing bookings with calculated tax data |
| 91 |
* |
| 92 |
* This method can be called after the migration to populate tax fields |
| 93 |
* for existing bookings based on current tax settings |
| 94 |
* |
| 95 |
* @return int Number of bookings updated |
| 96 |
*/ |
| 97 |
public static function updateExistingBookings(): int |
| 98 |
{ |
| 99 |
global $wpdb; |
| 100 |
|
| 101 |
$tableName = $wpdb->prefix . 'yatra_new_bookings'; |
| 102 |
|
| 103 |
// Get bookings without tax data |
| 104 |
$bookings = $wpdb->get_results(" |
| 105 |
SELECT id, total_amount, contact_country |
| 106 |
FROM {$tableName} |
| 107 |
WHERE tax_amount = 0 OR tax_amount IS NULL |
| 108 |
AND total_amount > 0 |
| 109 |
"); |
| 110 |
|
| 111 |
if (empty($bookings)) { |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
// Load tax service |
| 116 |
if (!class_exists('\Yatra\Services\TaxService')) { |
| 117 |
require_once YATRA_PLUGIN_DIR . 'app/Services/TaxService.php'; |
| 118 |
} |
| 119 |
|
| 120 |
if (!class_exists('\Yatra\Services\SettingsService')) { |
| 121 |
require_once YATRA_PLUGIN_DIR . 'app/Services/SettingsService.php'; |
| 122 |
} |
| 123 |
|
| 124 |
$updated = 0; |
| 125 |
|
| 126 |
foreach ($bookings as $booking) { |
| 127 |
try { |
| 128 |
// Calculate tax for existing booking |
| 129 |
$taxDetails = \Yatra\Services\TaxService::calculateTax( |
| 130 |
(float) $booking->total_amount, |
| 131 |
$booking->contact_country |
| 132 |
); |
| 133 |
|
| 134 |
// Update booking with tax data |
| 135 |
$wpdb->update( |
| 136 |
$tableName, |
| 137 |
[ |
| 138 |
'subtotal' => $taxDetails['tax_inclusive'] |
| 139 |
? $booking->total_amount - $taxDetails['tax_amount'] |
| 140 |
: $booking->total_amount, |
| 141 |
'tax_amount' => $taxDetails['tax_amount'], |
| 142 |
'tax_rate' => $taxDetails['tax_rate'], |
| 143 |
'tax_inclusive' => $taxDetails['tax_inclusive'] ? 1 : 0, |
| 144 |
'tax_details' => json_encode($taxDetails['taxes']), |
| 145 |
'updated_at' => current_time('mysql'), |
| 146 |
], |
| 147 |
['id' => $booking->id], |
| 148 |
['%f', '%f', '%f', '%d', '%s', '%s'], |
| 149 |
['%d'] |
| 150 |
); |
| 151 |
|
| 152 |
if ($wpdb->last_error === '') { |
| 153 |
$updated++; |
| 154 |
} |
| 155 |
} catch (\Exception $e) { |
| 156 |
// Log error but continue with other bookings |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return $updated; |
| 161 |
} |
| 162 |
} |
| 163 |
|