| 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 |
* Script to update departure capacities from availability dates |
| 13 |
*/ |
| 14 |
class DepartureCapacitySyncScript |
| 15 |
{ |
| 16 |
private DepartureRepository $departureRepository; |
| 17 |
private CapacityService $capacityService; |
| 18 |
|
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
$this->departureRepository = new DepartureRepository(); |
| 22 |
$this->capacityService = new CapacityService(); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Run the capacity sync process |
| 27 |
*/ |
| 28 |
public function run(): array |
| 29 |
{ |
| 30 |
$results = [ |
| 31 |
'total_departures' => 0, |
| 32 |
'updated_departures' => 0, |
| 33 |
'errors' => [] |
| 34 |
]; |
| 35 |
|
| 36 |
try { |
| 37 |
// Get all departures |
| 38 |
$allDepartures = $this->departureRepository->findAll(); |
| 39 |
$results['total_departures'] = count($allDepartures); |
| 40 |
|
| 41 |
foreach ($allDepartures as $departure) { |
| 42 |
$date = $departure->start_date ?: $departure->date; |
| 43 |
|
| 44 |
// Get correct capacity from availability |
| 45 |
$correctCapacity = $this->capacityService->getCapacityForDate($departure->trip_id, $date); |
| 46 |
|
| 47 |
// Only update if capacity is different and correct capacity > 0 |
| 48 |
if ($correctCapacity > 0 && $departure->max_capacity !== $correctCapacity) { |
| 49 |
$updated = $this->departureRepository->update($departure->id, [ |
| 50 |
'max_capacity' => $correctCapacity |
| 51 |
]); |
| 52 |
|
| 53 |
if ($updated) { |
| 54 |
$results['updated_departures']++; |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
} catch (\Throwable $e) { |
| 60 |
$results['errors'][] = $e->getMessage(); |
| 61 |
} |
| 62 |
|
| 63 |
return $results; |
| 64 |
} |
| 65 |
} |
| 66 |
|