1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IproSync\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute; |
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
10
|
|
|
use Illuminate\Support\Str; |
11
|
|
|
use IproSync\Database\Factories\BookingFactory; |
12
|
|
|
use IproSync\Ipro\Price; |
13
|
|
|
|
14
|
|
|
class Booking extends Model |
15
|
|
|
{ |
16
|
|
|
use HasFactory; |
17
|
|
|
use HasTraitsWithCasts, HasPullAt; |
18
|
|
|
|
19
|
|
|
public $incrementing = false; |
20
|
|
|
|
21
|
|
|
protected $guarded = []; |
22
|
|
|
|
23
|
|
|
protected $casts = [ |
24
|
|
|
'order_time' => 'datetime:Y-m-d', |
25
|
|
|
'modified_time' => 'datetime', |
26
|
|
|
'check_in' => 'datetime:Y-m-d', |
27
|
|
|
'check_out' => 'datetime:Y-m-d', |
28
|
|
|
'deposit_due_date' => 'datetime:Y-m-d', |
29
|
|
|
'balance_due_date' => 'datetime:Y-m-d', |
30
|
|
|
'property_types' => 'array', |
31
|
|
|
'booking_tags' => 'array', |
32
|
|
|
'guests' => 'array', |
33
|
|
|
'holiday_extras_ordered' => 'array', |
34
|
|
|
'payment_schedules' => 'array', |
35
|
|
|
'payments' => 'array', |
36
|
|
|
'bills' => 'array', |
37
|
|
|
'renter_amount' => 'float', |
38
|
|
|
'holiday_extras' => 'float', |
39
|
|
|
'discount' => 'float', |
40
|
|
|
]; |
41
|
|
|
|
42
|
3 |
|
public function getTable(): string |
43
|
|
|
{ |
44
|
3 |
|
return config('iprosoftware-sync.tables.bookings'); |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
protected static function newFactory(): BookingFactory |
48
|
|
|
{ |
49
|
3 |
|
return BookingFactory::new(); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function name(): Attribute |
53
|
|
|
{ |
54
|
1 |
|
return Attribute::get(fn () => implode(' - ', array_filter([ |
55
|
1 |
|
$this->check_in?->format(config('iprosoftware-sync.date_format.display')), |
|
|
|
|
56
|
1 |
|
$this->check_out?->format(config('iprosoftware-sync.date_format.display')), |
|
|
|
|
57
|
1 |
|
])) ?: '-'); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function renterRawTotal(): Attribute |
61
|
|
|
{ |
62
|
1 |
|
return Attribute::get(fn () => round($this->renter_amount + $this->holiday_extras - $this->discount, 2)); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function property(): BelongsTo |
66
|
|
|
{ |
67
|
|
|
return $this->belongsTo(Property::class, 'property_id', 'id'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function contact(): BelongsTo |
71
|
|
|
{ |
72
|
|
|
return $this->belongsTo(Contact::class, 'contact_id', 'id'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function scopeActive(Builder $query) |
76
|
|
|
{ |
77
|
|
|
$query->where('booking_status_id', 3); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function formattedPrice(string $attributeName): string |
81
|
|
|
{ |
82
|
1 |
|
return Price::format((float)$this->$attributeName, $this->currency); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function extractGuestNote(string $key, ?string $default = null, array $separators = [ |
86
|
|
|
':', |
87
|
|
|
' - ', |
88
|
|
|
]): ?string |
89
|
|
|
{ |
90
|
1 |
|
$key = rtrim($key); |
91
|
1 |
|
$keys = []; |
92
|
1 |
|
foreach ($separators as $separator) { |
93
|
1 |
|
$keys[] = $key . $separator; |
94
|
|
|
} |
95
|
1 |
|
foreach (preg_split("/((\r?\n)|(\r\n?))/", $this->guest_notes ?? '') as $note) { |
|
|
|
|
96
|
1 |
|
$note = trim($note); |
97
|
1 |
|
foreach ($keys as $formattedKey) { |
98
|
1 |
|
if (Str::startsWith($note, $formattedKey)) { |
99
|
1 |
|
return trim(Str::after($note, $formattedKey)); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
return $default; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: