-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConvertOcasionalBeneficiaryAction.php
More file actions
96 lines (77 loc) · 3.42 KB
/
Copy pathConvertOcasionalBeneficiaryAction.php
File metadata and controls
96 lines (77 loc) · 3.42 KB
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
<?php
declare(strict_types=1);
namespace App\Filament\Resources\Beneficiaries\Actions;
use App\Enums\Beneficiary\Type;
use App\Enums\Intervention\CaseInitiator;
use App\Enums\Intervention\Status;
use App\Filament\Resources\Beneficiaries\BeneficiaryResource;
use App\Models\Beneficiary;
use App\Models\Intervention\InterventionableCase;
use App\Models\Intervention\InterventionableIndividualService;
use App\Models\Intervention\OcasionalIntervention;
use App\Models\Service\Service;
use Filament\Actions\Action;
use Filament\Support\Enums\Width;
class ConvertOcasionalBeneficiaryAction extends Action
{
public static function getDefaultName(): ?string
{
return 'convert';
}
protected function setUp(): void
{
parent::setUp();
$this->visible(fn (Beneficiary $record): bool => $record->isOcasional());
$this->disabled(fn (Beneficiary $record): bool => blank($record->full_name));
$this->tooltip(
fn (Beneficiary $record): ?string => blank($record->full_name)
? __('beneficiary.action_convert.disabled_tooltip')
: null
);
$this->label(__('beneficiary.action_convert.action'));
$this->modalHeading(__('beneficiary.action_convert_confirm.title'));
$this->modalDescription(__('beneficiary.action_convert_confirm.text'));
$this->modalSubmitActionLabel(__('beneficiary.action_convert_confirm.action'));
$this->modalWidth(Width::Medium);
$this->color('gray');
$this->action(function (Beneficiary $record) {
$record->update([
'type' => Type::REGULAR,
]);
$record
->ocasionalInterventions()
->with('services')
->get()
->map(function (OcasionalIntervention $ocasionalIntervention) use ($record) {
$interventionable = InterventionableCase::create([
'name' => $ocasionalIntervention->reason,
'initiator' => CaseInitiator::NURSE,
'is_imported' => true,
]);
$case = $interventionable->intervention()->create([
'closed_at' => now(),
'beneficiary_id' => $record->id,
'vulnerability_id' => 'NONE',
]);
$ocasionalIntervention->services
->each(function (Service $service) use ($ocasionalIntervention, $case) {
$interventionable = InterventionableIndividualService::create([
'service_id' => $service->id,
'date' => $ocasionalIntervention->date,
'status' => Status::REALIZED,
]);
$interventionable->intervention()->create([
'parent_id' => $case->id,
'beneficiary_id' => $case->beneficiary_id,
]);
});
$ocasionalIntervention->delete();
});
$this->success();
});
$this->successNotificationTitle(__('beneficiary.action_convert.success'));
$this->successRedirectUrl(fn (Beneficiary $record) => BeneficiaryResource::getUrl('view', [
'record' => $record,
]));
}
}