-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Location.php
485 lines (377 loc) · 12.2 KB
/
Location.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
namespace Igniter\Local\Classes;
use Admin\Facades\AdminAuth;
use Admin\Models\Location_areas_model;
use Admin\Models\Locations_model;
use Carbon\Carbon;
use Igniter\Flame\Geolite\Model\Location as UserLocation;
use Igniter\Flame\Location\AbstractOrderType;
use Igniter\Flame\Location\Contracts\AreaInterface;
use Igniter\Flame\Location\Manager;
/**
* Location Class
*/
class Location extends Manager
{
const CLOSED = 'closed';
const OPEN = 'open';
const OPENING = 'opening';
protected $locationModel = \Admin\Models\Locations_model::class;
/**
* @var \Igniter\Local\Classes\CoveredArea
*/
protected $coveredArea;
/**
* @var \Illuminate\Support\Collection
*/
protected $orderTypes;
protected $scheduleCache = [];
public function __construct()
{
$this->setDefaultLocation(params('default_location_id'));
$this->locationSlugResolver(function () {
return controller()->param('location');
});
}
//
// BOOT METHODS
//
public function updateNearbyArea(AreaInterface $area)
{
$this->setCurrent($area->location);
$this->setCoveredArea(new CoveredArea($area));
}
public function updateOrderType($code = null)
{
$oldOrderType = $this->getSession('orderType');
if (is_null($code))
$this->forgetSession('orderType');
if (strlen($code)) {
$this->forgetSession('order-timeslot');
$this->putSession('orderType', $code);
$this->fireSystemEvent('location.orderType.updated', [$code, $oldOrderType]);
}
}
public function updateUserPosition(UserLocation $position)
{
$oldPosition = $this->getSession('position');
$this->putSession('position', $position);
$this->clearCoveredArea();
$this->fireSystemEvent('location.position.updated', [$position, $oldPosition]);
}
public function updateScheduleTimeSlot($dateTime, $isAsap = true)
{
$oldSlot = $this->getSession('order-timeslot');
$slot['dateTime'] = (!$isAsap && !is_null($dateTime)) ? make_carbon($dateTime) : null;
$slot['isAsap'] = $isAsap;
if (!$slot) {
$this->forgetSession('order-timeslot');
}
else {
$this->putSession('order-timeslot', $slot);
}
$this->fireSystemEvent('location.timeslot.updated', [$slot, $oldSlot]);
}
/**
* Extend the query used for finding the location.
*
* @param \Igniter\Flame\Database\Builder $query
*
* @return void
*/
public function extendLocationQuery($query)
{
if (!optional(AdminAuth::getUser())->hasPermission('Admin.Locations'))
$query->IsEnabled();
}
//
// HELPER METHODS
//
public function getId()
{
return $this->getModel()->getKey();
}
public function orderType()
{
return $this->getSession('orderType', Locations_model::DELIVERY);
}
/**
* @return \Igniter\Flame\Geolite\Model\Location
*/
public function userPosition()
{
return $this->getSession('position', UserLocation::createFromArray([]));
}
public function requiresUserPosition()
{
return setting('location_order') == 1;
}
public function checkOrderType($code = null)
{
return !$this->getOrderType($code)->isDisabled();
}
public function orderTypeIsDelivery()
{
return $this->orderType() === Locations_model::DELIVERY;
}
public function orderTypeIsCollection()
{
return $this->orderType() === Locations_model::COLLECTION;
}
public function hasOrderType($code = null)
{
if (!$orderType = $this->getOrderType($code))
return false;
return !$orderType->isDisabled();
}
/**
* @param null $code
* @return \Igniter\Flame\Location\AbstractOrderType
*/
public function getOrderType($code = null)
{
$code = !is_null($code) ? $code : $this->orderType();
return $this->getOrderTypes()->get($code);
}
public function getOrderTypes()
{
if ($this->orderTypes)
return $this->orderTypes;
return $this->orderTypes = $this->getModel()->availableOrderTypes();
}
public function minimumOrderTotal($orderType = null)
{
return $this->getOrderType($orderType)->getMinimumOrderTotal();
}
public function checkMinimumOrderTotal($cartTotal, $orderType = null)
{
return $cartTotal >= $this->minimumOrderTotal($orderType);
}
//
// HOURS
//
public function openingSchedule()
{
return $this->workingSchedule(Locations_model::OPENING);
}
public function deliverySchedule()
{
return $this->workingSchedule(Locations_model::DELIVERY);
}
public function collectionSchedule()
{
return $this->workingSchedule(Locations_model::COLLECTION);
}
public function isOpened()
{
return $this->getOrderType()->getSchedule()->isOpen();
}
public function isClosed()
{
return $this->getOrderType()->getSchedule()->isClosed();
}
public function openTime($type = null, $format = null)
{
if (is_null($type))
$type = $this->orderType();
return $this->workingSchedule($type)->getOpenTime($format);
}
public function closeTime($type = null, $format = null)
{
if (is_null($type))
$type = $this->orderType();
return $this->workingSchedule($type)->getCloseTime($format);
}
protected function workingStatus($type = null, $timestamp = null)
{
if (is_null($type))
$type = $this->orderType();
return $this->workingSchedule($type)->checkStatus($timestamp);
}
//
// Timeslot
//
public function orderTimeInterval()
{
return $this->getOrderType()->getInterval();
}
public function lastOrderTime()
{
return Carbon::parse($this->getOrderType()->getSchedule()->getCloseTime());
}
public function orderLeadTime()
{
return $this->getOrderType()->getLeadTime();
}
public function orderTimeIsAsap()
{
if (!$this->hasAsapSchedule())
return false;
$dateTime = $this->getSession('order-timeslot.dateTime');
$orderTimeIsAsap = (bool)$this->getSession('order-timeslot.isAsap', true);
if (!$this->isOpened())
return false;
return $orderTimeIsAsap || ($dateTime && now()->isAfter($dateTime));
}
/**
* @return \Carbon\Carbon
*/
public function orderDateTime()
{
$dateTime = $this->getSession('order-timeslot.dateTime');
if ($this->orderTimeIsAsap())
$dateTime = $this->asapScheduleTimeslot();
if (!$dateTime || now()->isAfter($dateTime)) {
$dateTime = $this->hasAsapSchedule()
? $this->asapScheduleTimeslot()
: $this->firstScheduleTimeslot();
}
return make_carbon($dateTime);
}
public function scheduleTimeslot($orderType = null)
{
if (is_null($orderType))
$orderType = $this->orderType();
if (array_key_exists($orderType, $this->scheduleCache))
return $this->scheduleCache[$orderType];
$leadMinutes = $this->model->getOption($orderType.'_add_lead_time')
? $this->orderLeadTime() : 0;
$result = $this->getOrderType($orderType)->getSchedule()->getTimeslot(
$this->orderTimeInterval(), null, $leadMinutes
);
return $this->scheduleCache[$orderType] = $result;
}
public function firstScheduleTimeslot()
{
return $this->scheduleTimeslot()->collapse()->first();
}
public function asapScheduleTimeslot()
{
if ($this->isClosed() || (bool)$this->getModel()->getOption('limit_orders'))
return $this->firstScheduleTimeslot();
return Carbon::now();
}
public function checkOrderTime($timestamp = null, $orderTypeCode = null)
{
if (is_null($timestamp))
$timestamp = $this->orderDateTime();
if (!$timestamp instanceof \DateTime)
$timestamp = new \DateTime($timestamp);
if (Carbon::now()->subMinute()->gte($timestamp))
return false;
$orderType = $this->getOrderType($orderTypeCode);
if (!$orderType->getFutureDays() && $this->isClosed())
return false;
$minFutureDays = Carbon::now()->startOfDay()->addDays($orderType->getMinimumFutureDays());
$maxFutureDays = Carbon::now()->endOfDay()->addDays($orderType->getFutureDays());
if (!$timestamp->between($minFutureDays, $maxFutureDays))
return false;
return $orderType->getSchedule()->isOpenAt($timestamp);
}
public function checkNoOrderTypeAvailable()
{
return $this->getOrderTypes()->filter(function ($orderType) {
return !$orderType->isDisabled();
})->isEmpty();
}
public function hasAsapSchedule()
{
if ($this->getOrderType()->getMinimumFutureDays())
return false;
return $this->getOrderType()->getScheduleRestriction() !== AbstractOrderType::LATER_ONLY;
}
public function hasLaterSchedule()
{
return $this->getOrderType()->getScheduleRestriction() !== AbstractOrderType::ASAP_ONLY;
}
//
// DELIVERY AREA
//
public function getAreaId()
{
return $this->coveredArea()->getKey();
}
public function setCoveredArea(CoveredArea $coveredArea)
{
$this->coveredArea = $coveredArea;
$areaId = $this->getSession('area');
if ($areaId !== $coveredArea->getKey()) {
$this->putSession('area', $coveredArea->getKey());
$this->fireSystemEvent('location.area.updated', [$coveredArea]);
}
return $this;
}
public function isCurrentAreaId($areaId)
{
return $this->getAreaId() == $areaId;
}
public function clearCoveredArea()
{
$this->coveredArea = null;
$this->forgetSession('area');
}
/**
* @return \Igniter\Local\Classes\CoveredArea
* @throws \Igniter\Flame\Exception\ApplicationException
*/
public function coveredArea()
{
if (!is_null($this->coveredArea))
return $this->coveredArea;
$area = null;
if ($areaId = (int)$this->getSession('area'))
$area = $this->getModel()->findDeliveryArea($areaId);
if ($area && $this->getId() !== $area->getLocationId()) {
$area = null;
$this->clearCoveredArea();
}
if (is_null($area)) {
$area = $this->getModel()->searchOrDefaultDeliveryArea(
$this->userPosition()->getCoordinates()
);
}
if (!$area || !$area instanceof AreaInterface)
return new CoveredArea(new Location_areas_model());
$coveredArea = new CoveredArea($area);
$this->setCoveredArea($coveredArea);
return $coveredArea;
}
public function deliveryAreas()
{
return $this->getModel()->listDeliveryAreas();
}
public function deliveryAmount($cartTotal)
{
return $this->coveredArea()->deliveryAmount($cartTotal);
}
/**
* @deprecated remove after v4, use minimumOrderTotal() instead
*/
public function minimumOrder($cartTotal)
{
return $this->minimumOrderTotal();
}
public function getDeliveryChargeConditions()
{
return $this->coveredArea()->listConditions();
}
/**
* @deprecated remove after v4, use checkMinimumOrderTotal() instead
*/
public function checkMinimumOrder($cartTotal)
{
return $cartTotal >= $this->minimumOrderTotal();
}
public function checkDistance($decimalPoint)
{
$coordinates = $this->userPosition()->getCoordinates();
$distance = $this->getModel()->calculateDistance($coordinates);
return round($distance, $decimalPoint);
}
public function checkDeliveryCoverage(UserLocation $userPosition = null)
{
if (is_null($userPosition))
$userPosition = $this->userPosition();
return $this->coveredArea()->checkBoundary($userPosition->getCoordinates());
}
}