|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models\enso\companies; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Notifications\RoutesNotifications; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Support\Facades\Config; |
|
9
|
|
|
use LaravelEnso\Addresses\Traits\Addressable; |
|
10
|
|
|
use LaravelEnso\DynamicMethods\Traits\Abilities; |
|
11
|
|
|
use LaravelEnso\Helpers\Traits\AvoidsDeletionConflicts; |
|
12
|
|
|
use LaravelEnso\Helpers\Traits\CascadesMorphMap; |
|
13
|
|
|
use App\Person; |
|
14
|
|
|
use LaravelEnso\Rememberable\Traits\Rememberable; |
|
15
|
|
|
use LaravelEnso\Tables\Traits\TableCache; |
|
16
|
|
|
use LaravelEnso\TrackWho\Traits\CreatedBy; |
|
17
|
|
|
use LaravelEnso\TrackWho\Traits\UpdatedBy; |
|
18
|
|
|
|
|
19
|
|
|
class Company extends Model |
|
20
|
|
|
{ |
|
21
|
|
|
use Abilities, |
|
|
|
|
|
|
22
|
|
|
Addressable, |
|
23
|
|
|
AvoidsDeletionConflicts, |
|
24
|
|
|
CascadesMorphMap, |
|
25
|
|
|
CreatedBy, |
|
26
|
|
|
Rememberable, |
|
27
|
|
|
RoutesNotifications, |
|
28
|
|
|
TableCache, |
|
29
|
|
|
UpdatedBy; |
|
30
|
|
|
|
|
31
|
|
|
protected $guarded = ['id']; |
|
32
|
|
|
|
|
33
|
|
|
protected $casts = ['pays_vat' => 'boolean', 'is_tenant' => 'boolean']; |
|
34
|
|
|
|
|
35
|
|
|
public function people() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->belongsToMany(Person::class) |
|
38
|
|
|
->withPivot('position'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public static function owner() |
|
42
|
|
|
{ |
|
43
|
|
|
return static::cacheGet(Config::get('enso.config.ownerCompanyId')); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function isTenant() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->is_tenant; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function scopeTenant($query) |
|
52
|
|
|
{ |
|
53
|
|
|
$query->whereIsTenant(true); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function mandatary() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->people() |
|
59
|
|
|
->withPivot('position') |
|
60
|
|
|
->wherePivot('is_mandatary', true) |
|
61
|
|
|
->first(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function attachPerson(int $personId, ?string $position = null) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->people()->attach($personId, [ |
|
67
|
|
|
'is_main' => false, |
|
68
|
|
|
'is_mandatary' => false, |
|
69
|
|
|
'position' => $position, |
|
70
|
|
|
]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function updateMandatary(?int $mandataryId) |
|
74
|
|
|
{ |
|
75
|
|
|
$pivotIds = $this->people->pluck('id')->reduce( |
|
76
|
|
|
fn ($pivot, $value) => $pivot |
|
77
|
|
|
->put($value, ['is_mandatary' => $value === $mandataryId]), |
|
78
|
|
|
new Collection() |
|
79
|
|
|
)->toArray(); |
|
80
|
|
|
|
|
81
|
|
|
$this->people()->sync($pivotIds); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|