| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Scripts; |
| 6 |
|
| 7 |
use Yatra\Repositories\DepartureRepository; |
| 8 |
use Yatra\Services\CapacityService; |
| 9 |
use Yatra\Models\Departure; |
| 10 |
|
| 11 |
/** |
| 12 |
* Comprehensive script to sync all departure capacities from availability |
| 13 |
* This will update all existing departures and ensure they match availability capacity |
| 14 |
*/ |
| 15 |
class DepartureCapacitySyncFromAvailabilityScript |
| 16 |
{ |
| 17 |
private DepartureRepository $departureRepository; |
| 18 |
private CapacityService $capacityService; |
| 19 |
|
| 20 |
public function __construct() |
| 21 |
{ |
| 22 |
$this->departureRepository = new DepartureRepository(); |
| 23 |
$this->capacityService = new CapacityService(); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Run the comprehensive capacity sync process |
| 28 |
*/ |
| 29 |
public function run(): array |
| 30 |
{ |
| 31 |
$results = [ |
| 32 |
'total_departures' => 0, |
| 33 |
'updated_departures' => 0, |
| 34 |
'no_availability_found' => 0, |
| 35 |
'errors' => [] |
| 36 |
]; |
| 37 |
|
| 38 |
try { |
| 39 |
// Get all departures |
| 40 |
$allDepartures = $this->departureRepository->findAll(); |
| 41 |
$results['total_departures'] = count($allDepartures); |
| 42 |
|
| 43 |
foreach ($allDepartures as $departure) { |
| 44 |
$date = $departure->start_date ?: $departure->date; |
| 45 |
|
| 46 |
// Get correct capacity from availability |
| 47 |
$correctCapacity = $this->capacityService->getCapacityForDate($departure->trip_id, $date, $departure->time ?? null); |
| 48 |
|
| 49 |
if ($correctCapacity > 0) { |
| 50 |
// Update if capacity is different |
| 51 |
if ($departure->max_capacity !== $correctCapacity) { |
| 52 |
$updated = $this->departureRepository->update($departure->id, [ |
| 53 |
'max_capacity' => $correctCapacity |
| 54 |
]); |
| 55 |
|
| 56 |
if ($updated) { |
| 57 |
$results['updated_departures']++; |
| 58 |
} |
| 59 |
} |
| 60 |
} else { |
| 61 |
$results['no_availability_found']++; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
} catch (\Throwable $e) { |
| 66 |
$results['errors'][] = $e->getMessage(); |
| 67 |
} |
| 68 |
|
| 69 |
return $results; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get a summary of what needs to be updated |
| 74 |
*/ |
| 75 |
public function getSyncSummary(): array |
| 76 |
{ |
| 77 |
$summary = [ |
| 78 |
'total_departures' => 0, |
| 79 |
'need_update' => 0, |
| 80 |
'no_availability' => 0, |
| 81 |
'already_correct' => 0 |
| 82 |
]; |
| 83 |
|
| 84 |
try { |
| 85 |
$allDepartures = $this->departureRepository->findAll(); |
| 86 |
$summary['total_departures'] = count($allDepartures); |
| 87 |
|
| 88 |
foreach ($allDepartures as $departure) { |
| 89 |
$date = $departure->start_date ?: $departure->date; |
| 90 |
$correctCapacity = $this->capacityService->getCapacityForDate($departure->trip_id, $date, $departure->time ?? null); |
| 91 |
|
| 92 |
if ($correctCapacity > 0) { |
| 93 |
if ($departure->max_capacity !== $correctCapacity) { |
| 94 |
$summary['need_update']++; |
| 95 |
} else { |
| 96 |
$summary['already_correct']++; |
| 97 |
} |
| 98 |
} else { |
| 99 |
$summary['no_availability']++; |
| 100 |
} |
| 101 |
} |
| 102 |
} catch (\Throwable $e) { |
| 103 |
$summary['errors'][] = $e->getMessage(); |
| 104 |
} |
| 105 |
|
| 106 |
return $summary; |
| 107 |
} |
| 108 |
} |
| 109 |
|