| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Models; |
| 6 |
|
| 7 |
/** |
| 8 |
* Checkout Model |
| 9 |
* |
| 10 |
* Encapsulates all checkout/booking data and provides clean getter methods |
| 11 |
* for templates to access pricing, trip, traveler, and discount information. |
| 12 |
* |
| 13 |
* @package Yatra\Models |
| 14 |
* @since 3.0.0 |
| 15 |
*/ |
| 16 |
class Checkout |
| 17 |
{ |
| 18 |
private \stdClass $trip; |
| 19 |
private array $session; |
| 20 |
private array $pricingCalculation; |
| 21 |
private array $taxCalculation; |
| 22 |
|
| 23 |
public function __construct(\stdClass $trip, array $session, array $pricingCalculation) |
| 24 |
{ |
| 25 |
$this->trip = $trip; |
| 26 |
$this->session = $session; |
| 27 |
$this->pricingCalculation = $pricingCalculation; |
| 28 |
$this->taxCalculation = $pricingCalculation['tax_calculation'] ?? [ |
| 29 |
'enable_tax' => false, |
| 30 |
'tax_breakdown' => [], |
| 31 |
'total_tax_amount' => 0, |
| 32 |
'tax_inclusive' => false, |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
// ========== Trip Information ========== |
| 37 |
|
| 38 |
public function getTrip(): \stdClass |
| 39 |
{ |
| 40 |
return $this->trip; |
| 41 |
} |
| 42 |
|
| 43 |
public function getTripId(): int |
| 44 |
{ |
| 45 |
return (int) $this->trip->id; |
| 46 |
} |
| 47 |
|
| 48 |
public function getTripName(): string |
| 49 |
{ |
| 50 |
return $this->trip->name ?? ''; |
| 51 |
} |
| 52 |
|
| 53 |
public function getTravelDate(): string |
| 54 |
{ |
| 55 |
return $this->session['travel_date'] ?? ''; |
| 56 |
} |
| 57 |
|
| 58 |
public function getDepartureTime(): string |
| 59 |
{ |
| 60 |
return $this->session['departure_time'] ?? ''; |
| 61 |
} |
| 62 |
|
| 63 |
// ========== Pricing Type & Travelers ========== |
| 64 |
|
| 65 |
public function getPricingType(): string |
| 66 |
{ |
| 67 |
return $this->session['pricing_type'] ?? 'regular'; |
| 68 |
} |
| 69 |
|
| 70 |
public function isTravelerBased(): bool |
| 71 |
{ |
| 72 |
return $this->getPricingType() === 'traveler_based'; |
| 73 |
} |
| 74 |
|
| 75 |
public function getTotalTravelers(): int |
| 76 |
{ |
| 77 |
return (int) ($this->session['travelers'] ?? 1); |
| 78 |
} |
| 79 |
|
| 80 |
public function getTravelerCounts(): array |
| 81 |
{ |
| 82 |
return $this->session['traveler_counts'] ?? []; |
| 83 |
} |
| 84 |
|
| 85 |
public function getPriceTypes(): array |
| 86 |
{ |
| 87 |
return $this->session['price_types'] ?? []; |
| 88 |
} |
| 89 |
|
| 90 |
public function getCategoryBreakdown(): array |
| 91 |
{ |
| 92 |
if (!$this->isTravelerBased() || empty($this->getPriceTypes())) { |
| 93 |
return []; |
| 94 |
} |
| 95 |
|
| 96 |
$breakdown = []; |
| 97 |
$priceTypes = $this->getPriceTypes(); |
| 98 |
$travelerCounts = $this->getTravelerCounts(); |
| 99 |
|
| 100 |
foreach ($priceTypes as $index => $pt) { |
| 101 |
$pt = (object) $pt; |
| 102 |
$categoryId = $pt->category_id ?? $index; |
| 103 |
$categoryLabel = $pt->category_label ?? __('Traveler', 'yatra'); |
| 104 |
$categoryPrice = isset($pt->effective_price) |
| 105 |
? (float) $pt->effective_price |
| 106 |
: \Yatra\Services\TripPricingService::resolveCategoryEffectivePrice((array) $pt); |
| 107 |
$count = isset($travelerCounts[$categoryId]) |
| 108 |
? (int) $travelerCounts[$categoryId] |
| 109 |
: ($index === 0 ? 1 : 0); |
| 110 |
|
| 111 |
if ($count > 0) { |
| 112 |
$breakdown[] = [ |
| 113 |
'category_id' => $categoryId, |
| 114 |
'label' => $categoryLabel, |
| 115 |
'count' => $count, |
| 116 |
'price' => $categoryPrice, |
| 117 |
'subtotal' => $categoryPrice * $count, |
| 118 |
]; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return $breakdown; |
| 123 |
} |
| 124 |
|
| 125 |
// ========== Pricing Amounts ========== |
| 126 |
|
| 127 |
public function getPricePerPerson(): float |
| 128 |
{ |
| 129 |
// Use unit_price from pricing calculation, or fallback to trip's discounted/original price |
| 130 |
if (isset($this->pricingCalculation['unit_price']) && $this->pricingCalculation['unit_price'] > 0) { |
| 131 |
return (float) $this->pricingCalculation['unit_price']; |
| 132 |
} |
| 133 |
|
| 134 |
// Fallback to trip pricing via centralized TripPricingService |
| 135 |
return \Yatra\Services\TripPricingService::resolveRegularCurrentPrice($this->trip); |
| 136 |
} |
| 137 |
|
| 138 |
public function getBaseAmount(): float |
| 139 |
{ |
| 140 |
return (float) ($this->pricingCalculation['base_amount'] ?? 0); |
| 141 |
} |
| 142 |
|
| 143 |
public function getGrossTotal(): float |
| 144 |
{ |
| 145 |
return (float) ($this->pricingCalculation['gross_total'] ?? $this->getBaseAmount()); |
| 146 |
} |
| 147 |
|
| 148 |
public function getSubtotal(): float |
| 149 |
{ |
| 150 |
return $this->getGrossTotal(); |
| 151 |
} |
| 152 |
|
| 153 |
public function getNetTotal(): float |
| 154 |
{ |
| 155 |
return (float) ($this->pricingCalculation['final_total'] ?? 0); |
| 156 |
} |
| 157 |
|
| 158 |
public function getFinalTotal(): float |
| 159 |
{ |
| 160 |
return $this->getNetTotal(); |
| 161 |
} |
| 162 |
|
| 163 |
public function getAmountDue(): float |
| 164 |
{ |
| 165 |
return (float) ($this->pricingCalculation['amount_due'] ?? $this->getNetTotal()); |
| 166 |
} |
| 167 |
|
| 168 |
// ========== Discounts ========== |
| 169 |
|
| 170 |
public function getGroupDiscount(): ?array |
| 171 |
{ |
| 172 |
$discount = $this->pricingCalculation['group_discount'] ?? null; |
| 173 |
if (empty($discount) || empty($discount['amount'])) { |
| 174 |
return null; |
| 175 |
} |
| 176 |
return $discount; |
| 177 |
} |
| 178 |
|
| 179 |
public function getGroupDiscountAmount(): float |
| 180 |
{ |
| 181 |
$discount = $this->getGroupDiscount(); |
| 182 |
return $discount ? (float) ($discount['amount'] ?? 0) : 0.0; |
| 183 |
} |
| 184 |
|
| 185 |
public function getGroupDiscountLabel(): string |
| 186 |
{ |
| 187 |
$discount = $this->getGroupDiscount(); |
| 188 |
return $discount ? ($discount['label'] ?? __('Group Discount', 'yatra')) : ''; |
| 189 |
} |
| 190 |
|
| 191 |
public function getCouponDiscount(): ?array |
| 192 |
{ |
| 193 |
$discount = $this->pricingCalculation['coupon_discount'] ?? null; |
| 194 |
if (empty($discount) || empty($discount['calculated_amount'])) { |
| 195 |
return null; |
| 196 |
} |
| 197 |
return $discount; |
| 198 |
} |
| 199 |
|
| 200 |
public function getCouponDiscountAmount(): float |
| 201 |
{ |
| 202 |
$discount = $this->getCouponDiscount(); |
| 203 |
return $discount ? (float) ($discount['calculated_amount'] ?? 0) : 0.0; |
| 204 |
} |
| 205 |
|
| 206 |
public function getCouponDiscountLabel(): string |
| 207 |
{ |
| 208 |
$discount = $this->getCouponDiscount(); |
| 209 |
return $discount ? ($discount['label'] ?? __('Coupon Discount', 'yatra')) : ''; |
| 210 |
} |
| 211 |
|
| 212 |
public function getCouponCode(): string |
| 213 |
{ |
| 214 |
$discount = $this->getCouponDiscount(); |
| 215 |
return $discount ? ($discount['code'] ?? '') : ''; |
| 216 |
} |
| 217 |
|
| 218 |
public function hasCoupon(): bool |
| 219 |
{ |
| 220 |
return !empty($this->getCouponCode()); |
| 221 |
} |
| 222 |
|
| 223 |
public function getTotalDiscountAmount(): float |
| 224 |
{ |
| 225 |
return $this->getGroupDiscountAmount() + $this->getCouponDiscountAmount(); |
| 226 |
} |
| 227 |
|
| 228 |
// ========== Tax Information ========== |
| 229 |
|
| 230 |
public function isTaxEnabled(): bool |
| 231 |
{ |
| 232 |
return !empty($this->taxCalculation['enable_tax']); |
| 233 |
} |
| 234 |
|
| 235 |
public function getTaxBreakdown(): array |
| 236 |
{ |
| 237 |
return $this->taxCalculation['tax_breakdown'] ?? []; |
| 238 |
} |
| 239 |
|
| 240 |
public function getTotalTaxAmount(): float |
| 241 |
{ |
| 242 |
return (float) ($this->taxCalculation['total_tax_amount'] ?? 0); |
| 243 |
} |
| 244 |
|
| 245 |
public function isTaxInclusive(): bool |
| 246 |
{ |
| 247 |
return !empty($this->taxCalculation['tax_inclusive']); |
| 248 |
} |
| 249 |
|
| 250 |
public function hasTaxes(): bool |
| 251 |
{ |
| 252 |
return $this->isTaxEnabled() && !empty($this->getTaxBreakdown()); |
| 253 |
} |
| 254 |
|
| 255 |
public function getTaxableAmount(): float |
| 256 |
{ |
| 257 |
return (float) ($this->pricingCalculation['taxable_amount'] ?? 0); |
| 258 |
} |
| 259 |
|
| 260 |
// ========== Payment Methods ========== |
| 261 |
|
| 262 |
public function getPaymentMethod(): string |
| 263 |
{ |
| 264 |
return $this->session['payment_method'] ?? 'full'; |
| 265 |
} |
| 266 |
|
| 267 |
public function isDepositPayment(): bool |
| 268 |
{ |
| 269 |
return $this->getPaymentMethod() === 'deposit'; |
| 270 |
} |
| 271 |
|
| 272 |
public function isPartialPayment(): bool |
| 273 |
{ |
| 274 |
return $this->getPaymentMethod() === 'partial'; |
| 275 |
} |
| 276 |
|
| 277 |
public function getDepositPercentage(): int |
| 278 |
{ |
| 279 |
return (int) ($this->session['deposit_percentage'] ?? 20); |
| 280 |
} |
| 281 |
|
| 282 |
public function getPartialPercentage(): int |
| 283 |
{ |
| 284 |
return (int) ($this->session['partial_payment_percentage'] ?? 30); |
| 285 |
} |
| 286 |
|
| 287 |
// ========== Additional Services ========== |
| 288 |
|
| 289 |
public function getAdditionalServices(): array |
| 290 |
{ |
| 291 |
return $this->pricingCalculation['additional_services'] ?? []; |
| 292 |
} |
| 293 |
|
| 294 |
public function getServicesTotal(): float |
| 295 |
{ |
| 296 |
return (float) ($this->pricingCalculation['services_total'] ?? 0); |
| 297 |
} |
| 298 |
|
| 299 |
public function hasAdditionalServices(): bool |
| 300 |
{ |
| 301 |
return !empty($this->getAdditionalServices()); |
| 302 |
} |
| 303 |
|
| 304 |
// ========== Itinerary Costs ========== |
| 305 |
|
| 306 |
public function getItineraryCosts(): array |
| 307 |
{ |
| 308 |
return $this->pricingCalculation['itinerary_costs'] ?? []; |
| 309 |
} |
| 310 |
|
| 311 |
public function getItineraryCostsTotal(): float |
| 312 |
{ |
| 313 |
return (float) ($this->pricingCalculation['itinerary_costs_total'] ?? 0); |
| 314 |
} |
| 315 |
|
| 316 |
public function hasItineraryCosts(): bool |
| 317 |
{ |
| 318 |
return !empty($this->getItineraryCosts()); |
| 319 |
} |
| 320 |
|
| 321 |
// ========== Currency ========== |
| 322 |
|
| 323 |
public function getCurrency(): ?string |
| 324 |
{ |
| 325 |
return $this->pricingCalculation['currency'] ?? null; |
| 326 |
} |
| 327 |
|
| 328 |
public function getCurrencySymbol(): string |
| 329 |
{ |
| 330 |
return yatra_get_currency_symbol($this->getCurrency()); |
| 331 |
} |
| 332 |
|
| 333 |
// ========== Formatted Prices ========== |
| 334 |
|
| 335 |
public function formatPrice(float $amount): string |
| 336 |
{ |
| 337 |
return yatra_format_price($amount, $this->getCurrency()); |
| 338 |
} |
| 339 |
|
| 340 |
public function getFormattedPricePerPerson(): string |
| 341 |
{ |
| 342 |
return $this->formatPrice($this->getPricePerPerson()); |
| 343 |
} |
| 344 |
|
| 345 |
public function getFormattedGrossTotal(): string |
| 346 |
{ |
| 347 |
return $this->formatPrice($this->getGrossTotal()); |
| 348 |
} |
| 349 |
|
| 350 |
public function getFormattedNetTotal(): string |
| 351 |
{ |
| 352 |
return $this->formatPrice($this->getNetTotal()); |
| 353 |
} |
| 354 |
|
| 355 |
public function getFormattedAmountDue(): string |
| 356 |
{ |
| 357 |
return $this->formatPrice($this->getAmountDue()); |
| 358 |
} |
| 359 |
|
| 360 |
public function getFormattedCouponDiscount(): string |
| 361 |
{ |
| 362 |
return $this->formatPrice($this->getCouponDiscountAmount()); |
| 363 |
} |
| 364 |
|
| 365 |
public function getFormattedGroupDiscount(): string |
| 366 |
{ |
| 367 |
return $this->formatPrice($this->getGroupDiscountAmount()); |
| 368 |
} |
| 369 |
|
| 370 |
// ========== Raw Data Access (for advanced use) ========== |
| 371 |
|
| 372 |
public function getSession(): array |
| 373 |
{ |
| 374 |
return $this->session; |
| 375 |
} |
| 376 |
|
| 377 |
public function getPricingCalculation(): array |
| 378 |
{ |
| 379 |
return $this->pricingCalculation; |
| 380 |
} |
| 381 |
|
| 382 |
public function getTaxCalculation(): array |
| 383 |
{ |
| 384 |
return $this->taxCalculation; |
| 385 |
} |
| 386 |
} |
| 387 |
|