PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.16
Yatra – Travel Booking & Tour Operator Software v3.0.16
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
← All changes | app/Repositories/BookingDepartureRepository.php +113 -8 3.0.2.6 → 3.0.16 View file →
@@ -9,13 +9,36 @@
9 9 /**
10 10 * Booking Departure Repository
11 11 * Manages the relationship between bookings and departures
12 12 *
13 - * Table: wp_yatra_new_booking_departures
13 + * Table: wp_yatra_booking_departures
14 14 */
15 15 class BookingDepartureRepository extends BaseRepository
16 16 {
17 17 /**
18 + * Check if booking_departures table exists
19 + */
20 + public function tableExists(): bool
21 + {
22 + $table = $this->getTableName();
23 + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
24 + $exists = $this->wpdb->get_var($this->wpdb->prepare("SHOW TABLES LIKE %s", $table));
25 + return $exists === $table;
26 + }
27 +
28 + /**
29 + * Create booking_departures table if missing
30 + */
31 + private function createTable(): void
32 + {
33 + if (!function_exists('dbDelta')) {
34 + require_once ABSPATH . 'wp-admin/includes/upgrade.php';
35 + }
36 + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
37 + dbDelta(\Yatra\Database\Tables\BookingDeparturesTable::getSchema());
38 + }
39 +
40 + /**
18 41 * Get table name
19 42 */
20 43 protected function getTableName(): string
21 44 {
@@ -31,8 +54,13 @@
31 54 */
32 55 public function link(int $bookingId, int $departureId): bool
33 56 {
34 57 $table = $this->getTableName();
58 +
59 + // Ensure table exists and detect optional columns (older installs may differ).
60 + $columns = $this->wpdb->get_col("DESCRIBE {$table}") ?: [];
61 + $hasTravelDate = in_array('travel_date', $columns, true);
62 + $hasDepartureTime = in_array('departure_time', $columns, true);
35 63
36 64 // Check if relationship already exists
37 65 $existing = $this->wpdb->get_var($this->wpdb->prepare(
38 66 "SELECT id FROM `{$table}` WHERE booking_id = %d AND departure_id = %d LIMIT 1",
@@ -40,19 +68,96 @@
40 68 $departureId
41 69 ));
42 70
43 71 if ($existing) {
44 - return true; // Already linked
72 + // Already linked; best-effort backfill date/time if the columns exist and are empty.
73 + if ($hasTravelDate || $hasDepartureTime) {
74 + $update = [];
75 +
76 + $booking = (new BookingRepository())->find($bookingId);
77 + $departure = (new DepartureRepository())->find($departureId);
78 +
79 + $travelDate = '';
80 + if (is_object($departure)) {
81 + $travelDate = (string) ($departure->start_date ?? $departure->date ?? '');
82 + }
83 + if ($travelDate === '' && is_object($booking)) {
84 + $travelDate = (string) ($booking->travel_date ?? $booking->start_date ?? '');
85 + }
86 +
87 + $time = '';
88 + if (is_object($departure)) {
89 + $time = (string) ($departure->time ?? '');
90 + }
91 + if ($time !== '') {
92 + $ts = strtotime($time);
93 + $time = $ts !== false ? date('H:i:s', $ts) : $time;
94 + }
95 +
96 + if ($hasTravelDate && $travelDate !== '') {
97 + $update['travel_date'] = $travelDate;
98 + }
99 + if ($hasDepartureTime && $time !== '' && $time !== '00:00:00') {
100 + $update['departure_time'] = $time;
101 + }
102 +
103 + if (!empty($update)) {
104 + $this->wpdb->update($table, $update, ['id' => (int) $existing]);
105 + }
106 + }
107 +
108 + return true;
45 109 }
110 +
111 + $booking = (new BookingRepository())->find($bookingId);
112 + $departure = (new DepartureRepository())->find($departureId);
113 +
114 + $travelDate = '';
115 + if (is_object($departure)) {
116 + $travelDate = (string) ($departure->start_date ?? $departure->date ?? '');
117 + }
118 + if ($travelDate === '' && is_object($booking)) {
119 + $travelDate = (string) ($booking->travel_date ?? $booking->start_date ?? '');
120 + }
121 +
122 + $time = '';
123 + if (is_object($departure)) {
124 + $time = (string) ($departure->time ?? '');
125 + }
126 + if ($time !== '') {
127 + $ts = strtotime($time);
128 + $time = $ts !== false ? date('H:i:s', $ts) : $time;
129 + }
130 +
131 + // Build insert payload compatible with both new and legacy schemas.
132 + $insert = [
133 + 'booking_id' => $bookingId,
134 + 'departure_id' => $departureId,
135 + 'created_at' => current_time('mysql'),
136 + ];
137 + $formats = ['%d', '%d', '%s'];
138 +
139 + if ($hasTravelDate) {
140 + // travel_date is NOT NULL in the new schema; fail safe to booking travel_date.
141 + if ($travelDate === '' && is_object($booking) && !empty($booking->travel_date)) {
142 + $travelDate = (string) $booking->travel_date;
143 + }
144 + if ($travelDate === '') {
145 + // If we can't infer a date, don't attempt insert (would violate NOT NULL in new schema).
146 + return false;
147 + }
148 + $insert['travel_date'] = $travelDate;
149 + $formats[] = '%s';
150 + }
151 + if ($hasDepartureTime) {
152 + $insert['departure_time'] = ($time !== '' && $time !== '00:00:00') ? $time : null;
153 + $formats[] = '%s';
154 + }
46 155
47 156 $result = $this->wpdb->insert(
48 157 $table,
49 - [
50 - 'booking_id' => $bookingId,
51 - 'departure_id' => $departureId,
52 - 'created_at' => current_time('mysql'),
53 - ],
54 - ['%d', '%d', '%s']
158 + $insert,
159 + $formats
55 160 );
56 161
57 162 return $result !== false;
58 163 }