PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.10
Yatra – Travel Booking & Tour Operator Software v3.0.10
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 / Services / CapacityService.php

CapacityService.php in Yatra – Travel Booking & Tour Operator Software 3.0.10, at app/Services/CapacityService.php

79 lines 3.1 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\Services;
6
7 use Yatra\Repositories\AvailabilityRepository;
8 use Yatra\Repositories\RecurringAvailabilityRepository;
9 use Yatra\Repositories\TripRepository;
10
11 class CapacityService
12 {
13 private AvailabilityRepository $availabilityRepository;
14 private RecurringAvailabilityRepository $recurringAvailabilityRepository;
15 private TripRepository $tripRepository;
16
17 public function __construct(
18 ?AvailabilityRepository $availabilityRepository = null,
19 ?RecurringAvailabilityRepository $recurringAvailabilityRepository = null,
20 ?TripRepository $tripRepository = null
21 ) {
22 $this->availabilityRepository = $availabilityRepository ?? new AvailabilityRepository();
23 $this->recurringAvailabilityRepository = $recurringAvailabilityRepository ?? new RecurringAvailabilityRepository();
24 $this->tripRepository = $tripRepository ?? new TripRepository();
25 }
26
27 /**
28 * Get capacity for a specific trip and date based on priority
29 *
30 * @param int $tripId Trip ID
31 * @param string $date Date in YYYY-MM-DD format
32 * @return int Maximum capacity
33 */
34 public function getCapacityForDate(int $tripId, string $date): int
35 {
36 // Normalize to Y-m-d. The availability-date and recurring-rule columns are
37 // DATE type, so a datetime input (e.g. "2026-07-18 12:30:00") breaks the
38 // rule's `end_date >= %s` boundary match (the DATE end_date is treated as
39 // midnight) and silently falls through to the trip default. Strip any time
40 // component so matching is date-only.
41 if (preg_match('/^(\d{4}-\d{2}-\d{2})/', $date, $m)) {
42 $date = $m[1];
43 }
44
45 // 1. Check Availability Date first (specific date overrides)
46 $availability = $this->availabilityRepository->findByTripIdAndDate($tripId, $date);
47 if ($availability && isset($availability->seats_total) && $availability->seats_total > 0) {
48 return (int) $availability->seats_total;
49 }
50
51 // 2. Check Recurring Availability Rules
52 $recurringRules = $this->recurringAvailabilityRepository->findActiveRulesForDate($tripId, $date);
53 if (!empty($recurringRules)) {
54 // Sort by priority (if applicable) and get the first matching rule
55 $matchingRule = reset($recurringRules);
56 $seats = (int) ($matchingRule->seats_total ?? 0);
57 if ($seats <= 0 && !empty($matchingRule->capacity_value)) {
58 $capType = $matchingRule->capacity_type ?? 'fixed';
59 if ($capType === 'fixed') {
60 $seats = (int) $matchingRule->capacity_value;
61 }
62 }
63 if ($seats > 0) {
64 return $seats;
65 }
66 }
67
68 // 3. Fall back to trip's default capacity (column is max_travelers; max_travellers kept for legacy rows)
69 $trip = $this->tripRepository->find($tripId);
70 if (!$trip) {
71 return 0;
72 }
73
74 $cap = (int) ($trip->max_travelers ?? $trip->max_travellers ?? 0);
75
76 return $cap > 0 ? $cap : 0;
77 }
78 }
79