|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Tables\Builders; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\Person; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use LaravelEnso\Tables\Contracts\Table; |
|
8
|
|
|
|
|
9
|
|
|
class PersonTable implements Table |
|
10
|
|
|
{ |
|
11
|
|
|
protected const TemplatePath = __DIR__.'/../Templates/people.json'; |
|
12
|
|
|
|
|
13
|
|
|
public function query(): Builder |
|
14
|
|
|
{ |
|
15
|
|
|
$role = \Auth::user()->role_id; |
|
|
|
|
|
|
16
|
|
|
$userId = \Auth::user()->id; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
if (in_array($role, [1, 2])) { |
|
19
|
|
|
return Person::selectRaw(' |
|
20
|
|
|
people.id, people.name, |
|
21
|
|
|
people.titl AS title, people.givn, people.surn, people.appellative, people.email, people.phone, |
|
22
|
|
|
people.birthday, people.deathday, CASE WHEN users.id is null THEN 0 ELSE 1 END as "user", |
|
23
|
|
|
companies.name as company, people.created_at |
|
24
|
|
|
')->leftJoin('users', 'people.id', '=', 'users.person_id') |
|
25
|
|
|
->leftJoin( |
|
26
|
|
|
'company_person', |
|
27
|
|
|
fn ($join) => $join |
|
28
|
|
|
->on('people.id', '=', 'company_person.person_id') |
|
29
|
|
|
->where('company_person.is_main', true) |
|
30
|
|
|
)->leftJoin('companies', 'company_person.company_id', 'companies.id'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return Person::selectRaw(' |
|
34
|
|
|
people.id, people.name, |
|
35
|
|
|
people.titl AS title, people.givn, people.surn, people.appellative, people.email, people.phone, |
|
36
|
|
|
people.birthday, people.deathday, CASE WHEN users.id is null THEN 0 ELSE 1 END as "user", |
|
37
|
|
|
companies.name as company, people.created_at |
|
38
|
|
|
')->leftJoin('users', function ($join) use ($userId) { |
|
39
|
|
|
$join->on('people.id', '=', 'users.person_id') |
|
40
|
|
|
->where('users.id', $userId); |
|
41
|
|
|
}) |
|
42
|
|
|
->leftJoin( |
|
43
|
|
|
'company_person', |
|
44
|
|
|
function ($join) use ($userId) { |
|
45
|
|
|
$join |
|
46
|
|
|
->on('people.id', '=', 'company_person.person_id') |
|
47
|
|
|
->where('company_person.company_id', $userId) |
|
48
|
|
|
->where('company_person.is_main', true); |
|
49
|
|
|
} |
|
50
|
|
|
)->leftJoin('companies', function ($join) use ($userId) { |
|
51
|
|
|
$join->on('company_person.company_id', '=', 'companies.id') |
|
52
|
|
|
->where('companies.id', $userId); |
|
53
|
|
|
}); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function templatePath(): string |
|
57
|
|
|
{ |
|
58
|
|
|
return static::TemplatePath; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|