| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Models; |
| 6 |
|
| 7 |
/** |
| 8 |
* Recurring Rule Model |
| 9 |
* Represents a recurring rule that generates departure dates dynamically |
| 10 |
* |
| 11 |
* Rules do NOT generate dates automatically in the database. |
| 12 |
* They only act as rules for dynamic date generation on the frontend. |
| 13 |
*/ |
| 14 |
class RecurringRule |
| 15 |
{ |
| 16 |
public int $id = 0; |
| 17 |
public int $trip_id = 0; |
| 18 |
public string $name = ''; |
| 19 |
public string $status = 'active'; // active | inactive | paused |
| 20 |
public string $recurrence_type = 'weekly'; // daily | weekly | monthly | yearly | custom |
| 21 |
public ?string $recurrence_pattern = null; |
| 22 |
public ?string $start_date = null; // Start date for the rule |
| 23 |
public ?string $end_date = null; // Optional end date for the rule |
| 24 |
public ?array $days_of_week = null; // JSON array: [0,1,2,3,4,5,6] (Sun-Sat) |
| 25 |
public ?int $day_of_month = null; |
| 26 |
public ?int $month_of_year = null; |
| 27 |
public int $interval = 1; |
| 28 |
public string $availability_status = 'available'; |
| 29 |
/** @var int[]|null Months 1–12 when rule is limited to specific months; null = all months */ |
| 30 |
public ?array $allowed_months = null; |
| 31 |
public ?int $max_bookings = null; |
| 32 |
public ?float $price_override = null; |
| 33 |
public string $price_type = 'fixed'; |
| 34 |
public ?string $departure_time = null; |
| 35 |
public ?string $arrival_time = null; |
| 36 |
public ?float $duration_hours = null; |
| 37 |
public ?string $from_location = null; |
| 38 |
public ?string $to_location = null; |
| 39 |
public ?string $from_latitude = null; |
| 40 |
public ?string $from_longitude = null; |
| 41 |
public ?string $to_latitude = null; |
| 42 |
public ?string $to_longitude = null; |
| 43 |
public ?array $exceptions = null; |
| 44 |
public string $exception_type = 'exclude'; |
| 45 |
public string $capacity_type = 'fixed'; |
| 46 |
public ?int $capacity_value = null; |
| 47 |
public float $pricing_adjustment = 0.00; |
| 48 |
public string $pricing_adjustment_type = 'amount'; |
| 49 |
public ?int $cutoff_hours = null; |
| 50 |
public ?int $cutoff_days = null; |
| 51 |
public ?int $advance_booking_days = null; |
| 52 |
public ?int $minimum_participants = null; |
| 53 |
public ?int $maximum_participants = null; |
| 54 |
public ?array $age_restrictions = null; |
| 55 |
public ?string $skill_level = null; |
| 56 |
public string $season_type = 'all'; |
| 57 |
public bool $holiday_only = false; |
| 58 |
public bool $weekend_only = false; |
| 59 |
public bool $weekday_only = false; |
| 60 |
public ?string $notes = null; |
| 61 |
public int $priority = 10; |
| 62 |
public bool $auto_generate = true; |
| 63 |
public ?string $last_generated = null; |
| 64 |
public string $created_at = ''; |
| 65 |
public string $updated_at = ''; |
| 66 |
public ?int $created_by = null; |
| 67 |
public ?int $updated_by = null; |
| 68 |
|
| 69 |
/** |
| 70 |
* Create from array (database row) |
| 71 |
*/ |
| 72 |
public static function fromArray(array $data): self |
| 73 |
{ |
| 74 |
$rule = new self(); |
| 75 |
|
| 76 |
$rule->id = (int) ($data['id'] ?? 0); |
| 77 |
$rule->trip_id = (int) ($data['trip_id'] ?? 0); |
| 78 |
$rule->name = sanitize_text_field($data['name'] ?? ''); |
| 79 |
$rule->status = sanitize_text_field($data['status'] ?? 'active'); |
| 80 |
$rule->recurrence_type = sanitize_text_field($data['recurrence_type'] ?? 'weekly'); |
| 81 |
$rule->start_date = !empty($data['start_date']) ? sanitize_text_field($data['start_date']) : null; |
| 82 |
$rule->end_date = !empty($data['end_date']) ? sanitize_text_field($data['end_date']) : null; |
| 83 |
$rule->capacity_value = (int) ($data['capacity_value'] ?? 0); |
| 84 |
$rule->price_override = !empty($data['price_override']) ? (float) $data['price_override'] : null; |
| 85 |
$rule->created_at = $data['created_at'] ?? ''; |
| 86 |
$rule->updated_at = $data['updated_at'] ?? ''; |
| 87 |
$rule->availability_status = sanitize_text_field($data['availability_status'] ?? 'available'); |
| 88 |
|
| 89 |
$allowedMonths = null; |
| 90 |
if (isset($data['months']) && $data['months'] !== '' && $data['months'] !== null) { |
| 91 |
$mRaw = $data['months']; |
| 92 |
if (is_string($mRaw)) { |
| 93 |
$decodedM = json_decode($mRaw, true); |
| 94 |
$allowedMonths = is_array($decodedM) ? array_values(array_unique(array_map('intval', $decodedM))) : null; |
| 95 |
} elseif (is_array($mRaw)) { |
| 96 |
$allowedMonths = array_values(array_unique(array_map('intval', $mRaw))); |
| 97 |
} |
| 98 |
} |
| 99 |
if (($allowedMonths === null || $allowedMonths === []) && !empty($data['recurrence_pattern'])) { |
| 100 |
$rp = $data['recurrence_pattern']; |
| 101 |
if (is_string($rp)) { |
| 102 |
$rp = json_decode($rp, true); |
| 103 |
} |
| 104 |
if (is_array($rp) && !empty($rp['months']) && is_array($rp['months'])) { |
| 105 |
$allowedMonths = array_values(array_unique(array_map('intval', $rp['months']))); |
| 106 |
} |
| 107 |
} |
| 108 |
$rule->allowed_months = ($allowedMonths !== null && $allowedMonths !== []) ? $allowedMonths : null; |
| 109 |
|
| 110 |
// Handle days_of_week as JSON or comma-separated string |
| 111 |
if (isset($data['days_of_week'])) { |
| 112 |
if (is_string($data['days_of_week'])) { |
| 113 |
// Try JSON first, then comma-separated |
| 114 |
$decoded = json_decode($data['days_of_week'], true); |
| 115 |
if (is_array($decoded)) { |
| 116 |
$rule->days_of_week = array_map('intval', $decoded); |
| 117 |
} else { |
| 118 |
$rule->days_of_week = array_map('intval', explode(',', $data['days_of_week'])); |
| 119 |
} |
| 120 |
} elseif (is_array($data['days_of_week'])) { |
| 121 |
$rule->days_of_week = array_map('intval', $data['days_of_week']); |
| 122 |
} else { |
| 123 |
$rule->days_of_week = []; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// Handle exceptions as JSON |
| 128 |
if (isset($data['exceptions'])) { |
| 129 |
if (is_string($data['exceptions'])) { |
| 130 |
$rule->exceptions = json_decode($data['exceptions'], true) ?: []; |
| 131 |
} elseif (is_array($data['exceptions'])) { |
| 132 |
$rule->exceptions = $data['exceptions']; |
| 133 |
} else { |
| 134 |
$rule->exceptions = []; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return $rule; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Convert to array |
| 143 |
*/ |
| 144 |
public function toArray(): array |
| 145 |
{ |
| 146 |
return [ |
| 147 |
'id' => $this->id, |
| 148 |
'trip_id' => $this->trip_id, |
| 149 |
'recurrence_type' => $this->recurrence_type, |
| 150 |
'weekdays' => $this->weekdays, |
| 151 |
'start_date' => $this->start_date, |
| 152 |
'end_date' => $this->end_date, |
| 153 |
'max_capacity' => $this->max_capacity, |
| 154 |
'pricing_by_traveler_type' => $this->pricing_by_traveler_type, |
| 155 |
'base_price' => $this->base_price, |
| 156 |
'is_active' => $this->is_active, |
| 157 |
'created_at' => $this->created_at, |
| 158 |
'updated_at' => $this->updated_at, |
| 159 |
]; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Check if rule is active for a given date |
| 164 |
*/ |
| 165 |
public function isActiveForDate(string $date): bool |
| 166 |
{ |
| 167 |
if ($this->status !== 'active') { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
if ($this->availability_status === 'unavailable') { |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
// Check start date |
| 176 |
if ($this->start_date && $date < $this->start_date) { |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
// Check end date |
| 181 |
if ($this->end_date && $date > $this->end_date) { |
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
if ($this->allowed_months !== null && $this->allowed_months !== []) { |
| 186 |
$month = (int) date('n', strtotime($date . ' 00:00:00')); |
| 187 |
if (!in_array($month, $this->allowed_months, true)) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return true; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|