PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.5
Yatra – Travel Booking & Tour Operator Software v3.0.5
3.0.16 3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 All 84 releases
yatra / app / Migrations / AddTaxFieldsToBookingsMigration.php

AddTaxFieldsToBookingsMigration.php in Yatra – Travel Booking & Tour Operator Software 3.0.5, at app/Migrations/AddTaxFieldsToBookingsMigration.php

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