| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Models; |
| 6 |
|
| 7 |
/** |
| 8 |
* Discount Model |
| 9 |
* Represents a discount coupon entity |
| 10 |
*/ |
| 11 |
class Discount |
| 12 |
{ |
| 13 |
public int $id; |
| 14 |
public string $code; |
| 15 |
public string $description; |
| 16 |
public string $type; // 'percentage' | 'fixed' |
| 17 |
public float $amount; |
| 18 |
public ?float $max_discount_amount; |
| 19 |
public int $usage_limit; |
| 20 |
public int $usage_limit_per_customer; |
| 21 |
public int $usage_count; |
| 22 |
public ?string $valid_from; |
| 23 |
public ?string $expiry_date; |
| 24 |
public string $status; // 'active' | 'inactive' | 'expired' |
| 25 |
public string $applicable_to; // 'all' | 'specific_trips' |
| 26 |
public ?array $trip_ids; // Array of trip IDs |
| 27 |
public ?float $min_amount; |
| 28 |
public bool $first_time_customer_only; |
| 29 |
public bool $is_group_discount; |
| 30 |
public ?int $min_group_size; |
| 31 |
public ?int $max_group_size; // DEPRECATED - kept for backward compatibility |
| 32 |
public ?string $group_discount_type; // DEPRECATED - kept for backward compatibility |
| 33 |
public ?float $group_discount_amount; // DEPRECATED - kept for backward compatibility |
| 34 |
public ?string $group_discount_mode; // 'total' | 'category_based' - DEPRECATED, replaced by ranges |
| 35 |
public ?array $category_discounts; // DEPRECATED - replaced by ranges |
| 36 |
|
| 37 |
// New: Multiple group size ranges |
| 38 |
public ?array $group_discount_ranges; // JSON array of ranges with different rates |
| 39 |
public string $created_at; |
| 40 |
public string $updated_at; |
| 41 |
public int $created_by; |
| 42 |
public int $updated_by; |
| 43 |
|
| 44 |
public static function fromArray(array $data): self |
| 45 |
{ |
| 46 |
$discount = new self(); |
| 47 |
|
| 48 |
$discount->id = (int) ($data['id'] ?? 0); |
| 49 |
$discount->code = $data['code'] ?? ''; |
| 50 |
$discount->description = $data['description'] ?? ''; |
| 51 |
$discount->type = $data['type'] ?? 'percentage'; |
| 52 |
$discount->amount = (float) ($data['amount'] ?? 0); |
| 53 |
$discount->max_discount_amount = isset($data['max_discount_amount']) ? (float) $data['max_discount_amount'] : null; |
| 54 |
$discount->usage_limit = (int) ($data['usage_limit'] ?? 0); |
| 55 |
$discount->usage_limit_per_customer = (int) ($data['usage_limit_per_customer'] ?? 0); |
| 56 |
$discount->usage_count = (int) ($data['usage_count'] ?? 0); |
| 57 |
$discount->valid_from = $data['valid_from'] ?? null; |
| 58 |
$discount->expiry_date = $data['expiry_date'] ?? null; |
| 59 |
$discount->status = $data['status'] ?? 'active'; |
| 60 |
$discount->applicable_to = $data['applicable_to'] ?? 'all'; |
| 61 |
if (!isset($data['trip_ids']) || $data['trip_ids'] === '' || $data['trip_ids'] === null) { |
| 62 |
$discount->trip_ids = null; |
| 63 |
} elseif (is_array($data['trip_ids'])) { |
| 64 |
$discount->trip_ids = $data['trip_ids']; |
| 65 |
} elseif (is_string($data['trip_ids'])) { |
| 66 |
$raw = trim($data['trip_ids']); |
| 67 |
if ($raw !== '' && ($raw[0] === '[' || $raw[0] === '{')) { |
| 68 |
$decoded = json_decode($raw, true); |
| 69 |
$discount->trip_ids = is_array($decoded) ? $decoded : null; |
| 70 |
} else { |
| 71 |
$unser = maybe_unserialize($data['trip_ids']); |
| 72 |
$discount->trip_ids = is_array($unser) ? $unser : array_values(array_filter(array_map('trim', explode(',', $raw)))); |
| 73 |
} |
| 74 |
} else { |
| 75 |
$discount->trip_ids = null; |
| 76 |
} |
| 77 |
$discount->min_amount = isset($data['min_amount']) ? (float) $data['min_amount'] : null; |
| 78 |
$discount->first_time_customer_only = (bool) ($data['first_time_customer_only'] ?? false); |
| 79 |
$discount->is_group_discount = (bool) ($data['is_group_discount'] ?? false); |
| 80 |
$discount->min_group_size = isset($data['min_group_size']) ? (int) $data['min_group_size'] : null; |
| 81 |
$discount->max_group_size = isset($data['max_group_size']) ? (int) $data['max_group_size'] : null; |
| 82 |
$discount->group_discount_type = $data['group_discount_type'] ?? null; |
| 83 |
$discount->group_discount_amount = isset($data['group_discount_amount']) ? (float) $data['group_discount_amount'] : null; |
| 84 |
$discount->group_discount_mode = $data['group_discount_mode'] ?? 'total'; |
| 85 |
$discount->category_discounts = isset($data['category_discounts']) ? |
| 86 |
(is_array($data['category_discounts']) ? $data['category_discounts'] : json_decode($data['category_discounts'], true)) : null; |
| 87 |
|
| 88 |
// New: Group discount ranges |
| 89 |
$discount->group_discount_ranges = isset($data['group_discount_ranges']) ? |
| 90 |
(is_array($data['group_discount_ranges']) ? $data['group_discount_ranges'] : json_decode($data['group_discount_ranges'], true)) : null; |
| 91 |
|
| 92 |
$discount->created_at = $data['created_at'] ?? ''; |
| 93 |
$discount->updated_at = $data['updated_at'] ?? ''; |
| 94 |
$discount->created_by = (int) ($data['created_by'] ?? 0); |
| 95 |
$discount->updated_by = (int) ($data['updated_by'] ?? 0); |
| 96 |
|
| 97 |
return $discount; |
| 98 |
} |
| 99 |
|
| 100 |
public function toArray(): array |
| 101 |
{ |
| 102 |
return [ |
| 103 |
'id' => $this->id, |
| 104 |
'code' => $this->code, |
| 105 |
'description' => $this->description, |
| 106 |
'type' => $this->type, |
| 107 |
'amount' => $this->amount, |
| 108 |
'max_discount_amount' => $this->max_discount_amount, |
| 109 |
'usage_limit' => $this->usage_limit, |
| 110 |
'usage_limit_per_customer' => $this->usage_limit_per_customer, |
| 111 |
'usage_count' => $this->usage_count, |
| 112 |
'valid_from' => $this->valid_from, |
| 113 |
'expiry_date' => $this->expiry_date, |
| 114 |
'status' => $this->status, |
| 115 |
'applicable_to' => $this->applicable_to, |
| 116 |
'trip_ids' => $this->trip_ids, |
| 117 |
'min_amount' => $this->min_amount, |
| 118 |
'first_time_customer_only' => $this->first_time_customer_only, |
| 119 |
'is_group_discount' => $this->is_group_discount, |
| 120 |
'min_group_size' => $this->min_group_size, |
| 121 |
'max_group_size' => $this->max_group_size, |
| 122 |
'group_discount_type' => $this->group_discount_type, |
| 123 |
'group_discount_amount' => $this->group_discount_amount, |
| 124 |
'group_discount_mode' => $this->group_discount_mode, |
| 125 |
'category_discounts' => $this->category_discounts, |
| 126 |
'group_discount_ranges' => $this->group_discount_ranges, |
| 127 |
'created_at' => $this->created_at, |
| 128 |
'updated_at' => $this->updated_at, |
| 129 |
'created_by' => $this->created_by, |
| 130 |
'updated_by' => $this->updated_by, |
| 131 |
]; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|