139 lines
4.0 KiB
PHP
139 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
use App\Person;
|
|
use App\MemberState;
|
|
|
|
|
|
class PeopleController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$people = Person::orderBy('name_or_company')->get();
|
|
|
|
return view('people.index', compact('people'));
|
|
}
|
|
|
|
public function show(Person $person)
|
|
{
|
|
$person->load('targets.actuals.accountType.accountClass.ioType',
|
|
'targets.actuals.booking',
|
|
'targets.ioType');
|
|
|
|
return view('people.show', compact('person'));
|
|
}
|
|
|
|
// Show the form for creating a new person
|
|
public function create() {
|
|
$person = new Person();
|
|
return view('people.create', compact('person'));
|
|
}
|
|
|
|
// Store a new person in the database
|
|
public function store() {
|
|
// Create a new Person model and fill it with data from the request
|
|
$person = new Person();
|
|
|
|
$person->fill(request([
|
|
'title',
|
|
'name_or_company',
|
|
'surname',
|
|
'street',
|
|
'postal_code',
|
|
'city',
|
|
'email',
|
|
'mobile',
|
|
'phone',
|
|
'date_of_birth'
|
|
]));
|
|
|
|
$person->save();
|
|
|
|
// If the member states data is non-empty, save a record to the database.
|
|
if (!is_null(request('memberState')[0]['joining_date'])) {
|
|
$memberState = new MemberState();
|
|
|
|
$memberState->joining_date = request('memberState')[0]['joining_date'];
|
|
$memberState->leaving_date = request('memberState')[0]['leaving_date'];
|
|
|
|
$person->memberStates()->save($memberState);
|
|
}
|
|
|
|
// Redirect to the people index page
|
|
return redirect('/people');
|
|
}
|
|
|
|
// Show the form for editing a existing person
|
|
public function edit(Person $person)
|
|
{
|
|
return view('people.edit', compact('person'));
|
|
}
|
|
|
|
// Store the edited data of an existing person in the database
|
|
public function update(Person $person)
|
|
{
|
|
// Create a new Person model and fill it with data from the request
|
|
$person->fill(request([
|
|
'title',
|
|
'name_or_company',
|
|
'surname',
|
|
'street',
|
|
'postal_code',
|
|
'city',
|
|
'email',
|
|
'mobile',
|
|
'phone',
|
|
'date_of_birth'
|
|
]));
|
|
|
|
$person->save();
|
|
|
|
// If the member states data is non-empty, save a record to the database.
|
|
if ($person->title != 'Firma') {
|
|
foreach (request('memberState') as $id => $rMemberState) {
|
|
if ($rMemberState['joining_date'] === null) {
|
|
continue;
|
|
}
|
|
|
|
$memberState = $id === 0 ?
|
|
new MemberState() :
|
|
MemberState::find($id);
|
|
|
|
$memberState->joining_date = $rMemberState['joining_date'];
|
|
$memberState->leaving_date = $rMemberState['leaving_date'];
|
|
$memberState->person_id = $person->id;
|
|
|
|
$memberState->save();
|
|
}
|
|
}
|
|
|
|
// Redirect to the people index page
|
|
return redirect('/people');
|
|
}
|
|
|
|
// Show the delete dialog for a given person
|
|
public function delete(Person $person)
|
|
{
|
|
$person->load('targets:id,person_id,io_type_id,amount',
|
|
'targets.ioType:id,factor',
|
|
'targets.actuals:id,person_target_id,account_type_id,amount',
|
|
'targets.actuals.accountType:id,account_class_id',
|
|
'targets.actuals.accountType.accountClass:id,io_type_id',
|
|
'targets.actuals.accountType.accountClass.ioType:id,factor');
|
|
|
|
return view('people.delete', compact('person'));
|
|
}
|
|
|
|
// Actually delete a person from the database
|
|
public function destroy(Person $person)
|
|
{
|
|
$person->delete();
|
|
|
|
return redirect('/people');
|
|
}
|
|
}
|