PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.7
Yatra – Travel Booking & Tour Operator Software v3.0.2.7
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 2.0.10 All 83 releases
yatra / app / Repositories / BookingDepartureRepository.php

BookingDepartureRepository.php in Yatra – Travel Booking & Tour Operator Software 3.0.2.7, at app/Repositories/BookingDepartureRepository.php

226 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Yatra\Repositories;
6
7 use Yatra\Database\Tables\BookingDeparturesTable;
8
9 /**
10 * Booking Departure Repository
11 * Manages the relationship between bookings and departures
12 *
13 * Table: wp_yatra_new_booking_departures
14 */
15 class BookingDepartureRepository extends BaseRepository
16 {
17 /**
18 * Get table name
19 */
20 protected function getTableName(): string
21 {
22 return BookingDeparturesTable::getTableName();
23 }
24
25 /**
26 * Link a booking to a departure
27 *
28 * @param int $bookingId Booking ID
29 * @param int $departureId Departure ID
30 * @return bool Success
31 */
32 public function link(int $bookingId, int $departureId): bool
33 {
34 $table = $this->getTableName();
35
36 // Check if relationship already exists
37 $existing = $this->wpdb->get_var($this->wpdb->prepare(
38 "SELECT id FROM `{$table}` WHERE booking_id = %d AND departure_id = %d LIMIT 1",
39 $bookingId,
40 $departureId
41 ));
42
43 if ($existing) {
44 return true; // Already linked
45 }
46
47 $result = $this->wpdb->insert(
48 $table,
49 [
50 'booking_id' => $bookingId,
51 'departure_id' => $departureId,
52 'created_at' => current_time('mysql'),
53 ],
54 ['%d', '%d', '%s']
55 );
56
57 return $result !== false;
58 }
59
60 /**
61 * Unlink a booking from a departure
62 *
63 * @param int $bookingId Booking ID
64 * @param int|null $departureId Optional departure ID (if null, removes all links for booking)
65 * @return bool Success
66 */
67 public function unlink(int $bookingId, ?int $departureId = null): bool
68 {
69 $table = $this->getTableName();
70
71 if ($departureId !== null) {
72 // Delete specific link
73 $result = $this->wpdb->delete(
74 $table,
75 [
76 'booking_id' => $bookingId,
77 'departure_id' => $departureId,
78 ],
79 ['%d', '%d']
80 );
81 } else {
82 // Delete all links for this booking
83 $result = $this->wpdb->delete(
84 $table,
85 ['booking_id' => $bookingId],
86 ['%d']
87 );
88 }
89
90 return $result !== false;
91 }
92
93 /**
94 * Get departure ID for a booking
95 *
96 * @param int $bookingId Booking ID
97 * @return int|null Departure ID or null if not linked
98 */
99 public function getDepartureIdForBooking(int $bookingId): ?int
100 {
101 $table = $this->getTableName();
102
103 $departureId = $this->wpdb->get_var($this->wpdb->prepare(
104 "SELECT departure_id FROM `{$table}` WHERE booking_id = %d LIMIT 1",
105 $bookingId
106 ));
107
108 return $departureId ? (int) $departureId : null;
109 }
110
111 /**
112 * Get all booking IDs for a departure
113 *
114 * @param int $departureId Departure ID
115 * @return array Array of booking IDs
116 */
117 public function getBookingIdsForDeparture(int $departureId): array
118 {
119 $table = $this->getTableName();
120
121 $rows = $this->wpdb->get_col($this->wpdb->prepare(
122 "SELECT booking_id FROM `{$table}` WHERE departure_id = %d",
123 $departureId
124 ));
125
126 $ids = [];
127 foreach ($rows as $id) {
128 $id = (int) $id;
129 if ($id > 0) {
130 $ids[] = $id;
131 }
132 }
133
134 return $ids;
135 }
136
137 /**
138 * Count bookings for a departure
139 *
140 * @param int $departureId Departure ID
141 * @return int Count
142 */
143 public function countBookingsForDeparture(int $departureId): int
144 {
145 $table = $this->getTableName();
146
147 $count = $this->wpdb->get_var($this->wpdb->prepare(
148 "SELECT COUNT(*) FROM `{$table}` WHERE departure_id = %d",
149 $departureId
150 ));
151
152 return (int) $count;
153 }
154
155 /**
156 * Update departure for a booking (handle date change)
157 *
158 * @param int $bookingId Booking ID
159 * @param int $newDepartureId New departure ID
160 * @return int|null Old departure ID (if existed)
161 */
162 public function updateDepartureForBooking(int $bookingId, int $newDepartureId): ?int
163 {
164 if (!$this->tableExists()) {
165 $this->createTable(); // Ensure table exists before trying to update
166 }
167
168 $table = $this->getTableName();
169
170 // Get old departure ID
171 $oldDepartureId = $this->getDepartureIdForBooking($bookingId);
172
173 // Remove old link
174 if ($oldDepartureId) {
175 $this->unlink($bookingId, $oldDepartureId);
176 }
177
178 // Create new link
179 $this->link($bookingId, $newDepartureId);
180
181 return $oldDepartureId;
182 }
183
184 /**
185 * Delete all links for a booking (when booking is deleted)
186 *
187 * @param int $bookingId Booking ID
188 * @return bool Success
189 */
190 public function deleteByBookingId(int $bookingId): bool
191 {
192 if (!$this->tableExists()) {
193 return false;
194 }
195
196 $table = $this->getTableName();
197
198 $result = $this->wpdb->delete(
199 $table,
200 ['booking_id' => $bookingId],
201 ['%d']
202 );
203
204 return $result !== false;
205 }
206
207 /**
208 * Delete all booking relationships for a departure
209 *
210 * @param int $departureId Departure ID
211 * @return bool Success
212 */
213 public function deleteByDepartureId(int $departureId): bool
214 {
215 $table = $this->getTableName();
216
217 $result = $this->wpdb->delete(
218 $table,
219 ['departure_id' => $departureId],
220 ['%d']
221 );
222
223 return $result !== false;
224 }
225 }
226