Skip to content

Commit 4cfdbd6

Browse files
committed
Add the availability to change equipment organization
1 parent e65dac4 commit 4cfdbd6

File tree

6 files changed

+886
-747
lines changed

6 files changed

+886
-747
lines changed

app/Actions/Equipment/UpdateEquipmentAction.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function execute(Equipment $equipment, array $data, array $images = []):
2626
'brand' => $data['brand'] ?? null,
2727
'description' => $data['description'],
2828
'category_id' => $data['category_id'],
29+
'organization_id' => $data['organization_id'],
2930
'condition' => $data['condition'],
3031
'quantity' => $data['quantity'] ?? 1,
3132
'depot_id' => $data['depot_id'],
@@ -53,7 +54,7 @@ public function execute(Equipment $equipment, array $data, array $images = []):
5354
$this->processImages($equipment, $images);
5455
}
5556

56-
return $equipment->load(['category', 'depot', 'images']);
57+
return $equipment->load(['category', 'depot', 'images', 'organization']);
5758
}
5859

5960
protected function processImages(Equipment $equipment, array $images): void

app/Http/Controllers/App/EquipmentController.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,23 @@ public function store(StoreRequest $request, StoreEquipmentAction $storeEquipmen
112112
public function edit(Request $request, Equipment $equipment)
113113
{
114114
$this->authorize('update', $equipment);
115-
$organization = $request->user()->currentOrganization;
115+
$user = $request->user();
116116

117117
$equipment->load(['category', 'depot', 'images']);
118118

119+
// Get all organizations the user belongs to
120+
$organizations = $user->organizations()->orderBy('name')->get(['organizations.id', 'organizations.name']);
121+
122+
// Get all depots from all organizations the user belongs to
123+
$depots = \App\Models\Depot::whereIn('organization_id', $organizations->pluck('id'))
124+
->orderBy('name')
125+
->get(['id', 'name', 'city', 'organization_id']);
126+
119127
return Inertia::render('App/Organizations/Equipments/Edit', [
120128
'equipment' => $equipment,
121-
'organization' => $organization,
122-
'depots' => $organization->depots()->orderBy('name')->get(['id', 'name', 'city']),
129+
'organization' => $equipment->organization,
130+
'organizations' => $organizations,
131+
'depots' => $depots,
123132
]);
124133
}
125134

app/Http/Requests/Equipment/UpdateRequest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function rules(): array
2727
'brand' => ['nullable', 'string', 'max:255'],
2828
'description' => ['nullable', 'string'],
2929
'category_id' => ['required', 'exists:categories,id'],
30+
'organization_id' => ['required', 'exists:organizations,id'],
3031

3132
// Step 2: État et stockage
3233
'condition' => ['required', 'string', 'in:new,good,fair,poor'],
@@ -62,6 +63,8 @@ public function messages(): array
6263
'brand.max' => 'La marque ne peut pas dépasser 255 caractères.',
6364
'category_id.required' => 'La catégorie est requise.',
6465
'category_id.exists' => 'La catégorie sélectionnée n\'existe pas.',
66+
'organization_id.required' => 'L\'organisation propriétaire est requise.',
67+
'organization_id.exists' => 'L\'organisation sélectionnée n\'existe pas.',
6568

6669
// Step 2
6770
'condition.required' => 'L\'état est requis.',
@@ -92,4 +95,20 @@ public function messages(): array
9295
'next_maintenance_date.after_or_equal' => 'La date de maintenance suivante doit être postérieure ou égale à la dernière maintenance.',
9396
];
9497
}
98+
99+
/**
100+
* Configure the validator instance.
101+
*/
102+
public function withValidator($validator)
103+
{
104+
$validator->after(function ($validator) {
105+
// Vérifier que le dépôt appartient à l'organisation sélectionnée
106+
if ($this->filled('organization_id') && $this->filled('depot_id')) {
107+
$depot = \App\Models\Depot::find($this->depot_id);
108+
if ($depot && $depot->organization_id != $this->organization_id) {
109+
$validator->errors()->add('depot_id', 'Le lieu de stockage doit appartenir à l\'organisation sélectionnée.');
110+
}
111+
}
112+
});
113+
}
95114
}

0 commit comments

Comments
 (0)