| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Repositories; |
| 6 |
|
| 7 |
use Yatra\Database\Tables\TripsTable; |
| 8 |
use Yatra\Database\Tables\TripAvailabilityDatesTable; |
| 9 |
|
| 10 |
/** |
| 11 |
* Trip Availability Repository |
| 12 |
* Handles database operations for trip availability |
| 13 |
*/ |
| 14 |
class TripAvailabilityRepository extends BaseRepository |
| 15 |
{ |
| 16 |
protected function getTableName(): string |
| 17 |
{ |
| 18 |
return TripAvailabilityDatesTable::getTableName(); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Count available departures by date |
| 23 |
*/ |
| 24 |
public function countAvailableDeparturesByDate(int $tripId, string $date): int |
| 25 |
{ |
| 26 |
global $wpdb; |
| 27 |
$availability_table = $this->getTableName(); |
| 28 |
|
| 29 |
return (int) $wpdb->get_var($wpdb->prepare( |
| 30 |
"SELECT COUNT(*) FROM {$availability_table} WHERE trip_id = %d AND departure_date = %s AND status IN ('available', 'limited')", |
| 31 |
$tripId, |
| 32 |
$date |
| 33 |
)); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get availability by date |
| 38 |
*/ |
| 39 |
public function getAvailabilityByDate(int $tripId, string $date): ?\stdClass |
| 40 |
{ |
| 41 |
global $wpdb; |
| 42 |
$availability_table = $this->getTableName(); |
| 43 |
|
| 44 |
return $wpdb->get_row($wpdb->prepare( |
| 45 |
"SELECT * FROM {$availability_table} WHERE trip_id = %d AND departure_date = %s", |
| 46 |
$tripId, |
| 47 |
$date |
| 48 |
)); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get total booked seats by date |
| 53 |
*/ |
| 54 |
public function getTotalBookedByDate(int $tripId, string $date): int |
| 55 |
{ |
| 56 |
global $wpdb; |
| 57 |
$availability_table = $this->getTableName(); |
| 58 |
|
| 59 |
return (int) $wpdb->get_var($wpdb->prepare( |
| 60 |
"SELECT COALESCE(SUM(seats_booked), 0) FROM {$availability_table} WHERE trip_id = %d AND departure_date = %s", |
| 61 |
$tripId, |
| 62 |
$date |
| 63 |
)); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Update availability status |
| 68 |
*/ |
| 69 |
public function updateAvailabilityStatus(int $tripId, string $date, string $status): bool |
| 70 |
{ |
| 71 |
global $wpdb; |
| 72 |
$availability_table = $this->getTableName(); |
| 73 |
|
| 74 |
return $wpdb->update( |
| 75 |
$availability_table, |
| 76 |
['status' => $status, 'updated_at' => current_time('mysql')], |
| 77 |
['trip_id' => $tripId, 'departure_date' => $date], |
| 78 |
['%s', '%s'], |
| 79 |
['%d', '%s'] |
| 80 |
) !== false; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Update booked seats |
| 85 |
*/ |
| 86 |
public function updateBookedSeats(int $tripId, string $date, int $bookedSeats): bool |
| 87 |
{ |
| 88 |
global $wpdb; |
| 89 |
$availability_table = $this->getTableName(); |
| 90 |
|
| 91 |
return $wpdb->update( |
| 92 |
$availability_table, |
| 93 |
['seats_booked' => $bookedSeats, 'updated_at' => current_time('mysql')], |
| 94 |
['trip_id' => $tripId, 'departure_date' => $date], |
| 95 |
['%d', '%s'], |
| 96 |
['%d', '%s'] |
| 97 |
) !== false; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get availability dates for trip |
| 102 |
*/ |
| 103 |
public function getAvailabilityDates(int $tripId, array $filters = []): array |
| 104 |
{ |
| 105 |
global $wpdb; |
| 106 |
$availability_table = $this->getTableName(); |
| 107 |
|
| 108 |
$where_conditions = ["trip_id = %d"]; |
| 109 |
$where_values = [$tripId]; |
| 110 |
|
| 111 |
// Apply filters |
| 112 |
if (!empty($filters['status'])) { |
| 113 |
$where_conditions[] = "status = %s"; |
| 114 |
$where_values[] = $filters['status']; |
| 115 |
} |
| 116 |
|
| 117 |
if (!empty($filters['date_from'])) { |
| 118 |
$where_conditions[] = "departure_date >= %s"; |
| 119 |
$where_values[] = $filters['date_from']; |
| 120 |
} |
| 121 |
|
| 122 |
if (!empty($filters['date_to'])) { |
| 123 |
$where_conditions[] = "departure_date <= %s"; |
| 124 |
$where_values[] = $filters['date_to']; |
| 125 |
} |
| 126 |
|
| 127 |
$where_clause = implode(' AND ', $where_conditions); |
| 128 |
|
| 129 |
return $wpdb->get_results($wpdb->prepare( |
| 130 |
"SELECT * FROM {$availability_table} WHERE {$where_clause} ORDER BY departure_date ASC", |
| 131 |
...$where_values |
| 132 |
)); |
| 133 |
} |
| 134 |
} |
| 135 |
|