2019-03-18 08:53:00 -03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-09-08 19:45:49 -03:00
|
|
|
use App\Models\Organization;
|
2019-03-18 08:53:00 -03:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2019-04-15 20:08:08 -03:00
|
|
|
use Illuminate\Support\Facades\Redirect;
|
2020-07-29 11:58:25 -03:00
|
|
|
use Illuminate\Support\Facades\Request;
|
|
|
|
use Inertia\Inertia;
|
2019-03-18 08:53:00 -03:00
|
|
|
|
|
|
|
class OrganizationsController extends Controller
|
|
|
|
{
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return Inertia::render('Organizations/Index', [
|
2019-04-08 10:40:13 -03:00
|
|
|
'filters' => Request::all('search', 'trashed'),
|
2019-03-18 08:53:00 -03:00
|
|
|
'organizations' => Auth::user()->account->organizations()
|
|
|
|
->orderBy('name')
|
2019-04-08 10:40:13 -03:00
|
|
|
->filter(Request::only('search', 'trashed'))
|
2019-03-18 08:53:00 -03:00
|
|
|
->paginate()
|
2021-02-27 10:36:45 -03:00
|
|
|
->through(function ($organization) {
|
|
|
|
return [
|
|
|
|
'id' => $organization->id,
|
|
|
|
'name' => $organization->name,
|
|
|
|
'phone' => $organization->phone,
|
|
|
|
'city' => $organization->city,
|
|
|
|
'deleted_at' => $organization->deleted_at,
|
|
|
|
];
|
|
|
|
}),
|
2019-03-18 08:53:00 -03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
return Inertia::render('Organizations/Create');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function store()
|
|
|
|
{
|
2019-04-15 20:08:08 -03:00
|
|
|
Auth::user()->account->organizations()->create(
|
2019-03-18 08:53:00 -03:00
|
|
|
Request::validate([
|
|
|
|
'name' => ['required', 'max:100'],
|
|
|
|
'email' => ['nullable', 'max:50', 'email'],
|
|
|
|
'phone' => ['nullable', 'max:50'],
|
|
|
|
'address' => ['nullable', 'max:150'],
|
|
|
|
'city' => ['nullable', 'max:50'],
|
|
|
|
'region' => ['nullable', 'max:50'],
|
|
|
|
'country' => ['nullable', 'max:2'],
|
|
|
|
'postal_code' => ['nullable', 'max:25'],
|
|
|
|
])
|
2019-04-15 20:08:08 -03:00
|
|
|
);
|
|
|
|
|
2019-05-21 20:12:49 -03:00
|
|
|
return Redirect::route('organizations')->with('success', 'Organization created.');
|
2019-03-18 08:53:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function edit(Organization $organization)
|
|
|
|
{
|
|
|
|
return Inertia::render('Organizations/Edit', [
|
|
|
|
'organization' => [
|
|
|
|
'id' => $organization->id,
|
|
|
|
'name' => $organization->name,
|
|
|
|
'email' => $organization->email,
|
|
|
|
'phone' => $organization->phone,
|
|
|
|
'address' => $organization->address,
|
|
|
|
'city' => $organization->city,
|
|
|
|
'region' => $organization->region,
|
|
|
|
'country' => $organization->country,
|
|
|
|
'postal_code' => $organization->postal_code,
|
|
|
|
'deleted_at' => $organization->deleted_at,
|
|
|
|
'contacts' => $organization->contacts()->orderByName()->get()->map->only('id', 'name', 'city', 'phone'),
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update(Organization $organization)
|
|
|
|
{
|
|
|
|
$organization->update(
|
|
|
|
Request::validate([
|
|
|
|
'name' => ['required', 'max:100'],
|
|
|
|
'email' => ['nullable', 'max:50', 'email'],
|
|
|
|
'phone' => ['nullable', 'max:50'],
|
|
|
|
'address' => ['nullable', 'max:150'],
|
|
|
|
'city' => ['nullable', 'max:50'],
|
|
|
|
'region' => ['nullable', 'max:50'],
|
|
|
|
'country' => ['nullable', 'max:2'],
|
|
|
|
'postal_code' => ['nullable', 'max:25'],
|
|
|
|
])
|
|
|
|
);
|
2019-04-15 20:08:08 -03:00
|
|
|
|
2019-12-18 19:34:14 -03:00
|
|
|
return Redirect::back()->with('success', 'Organization updated.');
|
2019-03-18 08:53:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy(Organization $organization)
|
|
|
|
{
|
|
|
|
$organization->delete();
|
2019-04-15 20:08:08 -03:00
|
|
|
|
2019-12-18 19:34:14 -03:00
|
|
|
return Redirect::back()->with('success', 'Organization deleted.');
|
2019-03-18 08:53:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function restore(Organization $organization)
|
|
|
|
{
|
|
|
|
$organization->restore();
|
2019-04-15 20:08:08 -03:00
|
|
|
|
2019-12-18 19:34:14 -03:00
|
|
|
return Redirect::back()->with('success', 'Organization restored.');
|
2019-03-18 08:53:00 -03:00
|
|
|
}
|
|
|
|
}
|