1 | <?php namespace VojtaSvoboda\Reservations\Models; |
||
19 | class Reservation extends Model |
||
20 | { |
||
21 | use SoftDeleteTrait; |
||
22 | |||
23 | use ValidationTrait; |
||
24 | |||
25 | /** @var string $table The database table used by the model */ |
||
26 | public $table = 'vojtasvoboda_reservations_reservations'; |
||
27 | |||
28 | /** @var array Rules */ |
||
29 | public $rules = [ |
||
30 | 'date' => 'required|date', |
||
31 | 'locale' => 'max:20', |
||
32 | 'email' => 'required|email', |
||
33 | 'name' => 'required|max:300', |
||
34 | 'street' => 'required|max:300', |
||
35 | 'town' => 'max:300', |
||
36 | 'zip' => 'numeric', |
||
37 | 'phone' => 'required|max:300', |
||
38 | 'message' => 'required|max:3000', |
||
39 | ]; |
||
40 | |||
41 | public $fillable = [ |
||
42 | 'status', 'date', 'locale', 'email', 'name', 'lastname', |
||
43 | 'street', 'town', 'zip', 'phone', 'message', |
||
44 | ]; |
||
45 | |||
46 | public $dates = ['date', 'created_at', 'updated_at', 'deleted_at']; |
||
47 | |||
48 | public $belongsTo = [ |
||
49 | 'status' => 'VojtaSvoboda\Reservations\Models\Status', |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | 8 | * Before create reservation. |
|
54 | */ |
||
55 | 8 | public function beforeCreate() |
|
56 | 8 | { |
|
57 | $this->hash = $this->getUniqueHash(); |
||
58 | 8 | $this->number = $this->getUniqueNumber(); |
|
59 | |||
60 | 8 | $this->locale = App::getLocale(); |
|
61 | 8 | ||
62 | 8 | $this->ip = Request::server('REMOTE_ADDR'); |
|
63 | $this->ip_forwarded = Request::server('HTTP_X_FORWARDED_FOR'); |
||
64 | 8 | $this->user_agent = Request::server('HTTP_USER_AGENT'); |
|
65 | 6 | ||
66 | if ($this->status === null) { |
||
67 | 8 | $this->status = $this->getDefaultStatus(); |
|
68 | } |
||
69 | } |
||
70 | |||
71 | public function beforeSave() |
||
72 | { |
||
73 | $this->validateAvailability(); |
||
74 | } |
||
75 | |||
76 | 7 | /** |
|
77 | * Check reservation date availability. |
||
78 | 7 | */ |
|
79 | public function validateAvailability() |
||
80 | 7 | { |
|
81 | 7 | if (!$this->getFacade()->validateReservation($this)) { |
|
82 | 7 | throw new ApplicationException($this->date->format('d.m.Y H:i') . ' is already booked.'); |
|
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Scope for getting non cancelled reservations. |
||
88 | * |
||
89 | * @param $query |
||
90 | * |
||
91 | * @return mixed |
||
92 | 7 | */ |
|
93 | public function scopeNotCancelled($query) |
||
94 | 7 | { |
|
95 | 7 | $cancelledStatuses = Config::get('vojtasvoboda.reservations::config.statuses.cancelled', ['cancelled']); |
|
96 | 7 | ||
97 | return $query->whereHas('status', function($query) use ($cancelledStatuses) { |
||
98 | 7 | $query->whereNotIn('ident', $cancelledStatuses); |
|
99 | }); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Set machine scope |
||
104 | * |
||
105 | * @param $query |
||
106 | 7 | * |
|
107 | * @return mixed |
||
108 | */ |
||
109 | 7 | public function scopeMachine($query) |
|
110 | 7 | { |
|
111 | $ip_addr = Request::server('REMOTE_ADDR'); |
||
112 | $ip_forwarded = Request::server('HTTP_X_FORWARDED_FOR'); |
||
113 | 7 | $user_agent = Request::server('HTTP_USER_AGENT'); |
|
114 | |||
115 | 7 | return $query->whereIp($ip_addr)->whereIpForwarded($ip_forwarded)->whereUserAgent($user_agent); |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Get default reservation status. |
||
120 | * |
||
121 | * @return Status |
||
122 | */ |
||
123 | 6 | public function getDefaultStatus() |
|
129 | |||
130 | /** |
||
131 | * Generate unique hash for each reservation. |
||
132 | * |
||
133 | * @return string|null |
||
134 | */ |
||
135 | 9 | public function getUniqueHash() |
|
144 | |||
145 | /** |
||
146 | * Generate unique number for each reservation. With this hash you can reference |
||
147 | * concrete reservation instead of using internal Reservation ID. |
||
148 | * |
||
149 | * @return string|null |
||
150 | */ |
||
151 | 9 | public function getUniqueNumber() |
|
169 | |||
170 | /** |
||
171 | * Get Reservations facade. |
||
172 | * |
||
173 | * @return ReservationsFacade |
||
174 | */ |
||
175 | private function getFacade() |
||
179 | } |
||
180 |