280 lines
9.5 KiB
PHP
280 lines
9.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Car;
|
|
use Inertia\Inertia;
|
|
use App\Models\Brand;
|
|
use App\Models\Contract;
|
|
use App\Enums\InsuranceType;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
|
|
class CarController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
return $this->renderCarsList($request, Car::query(), 'Cars/Index');
|
|
}
|
|
|
|
public function unsold(Request $request)
|
|
{
|
|
return $this->renderCarsList($request, Car::unsoldOnly(), 'Cars/Unsold');
|
|
}
|
|
|
|
public function sold(Request $request)
|
|
{
|
|
return $this->renderCarsList($request, Car::soldOnly(), 'Cars/Sold');
|
|
}
|
|
|
|
private function renderCarsList(Request $request, $cars, string $renderPage) {
|
|
$direction = $this->getDirection($request);
|
|
$sortBy = $this->getSortBy($request);
|
|
$cars = $this->getWithCustomSort($cars, $sortBy, $direction);
|
|
|
|
return Inertia::render($renderPage, [
|
|
'filters' => $request->all('search', 'trashed'),
|
|
'sort' => [
|
|
'by' => $sortBy,
|
|
'direction' => $direction,
|
|
],
|
|
'cars' => $cars->filter($request->only('search', 'trashed'))
|
|
->paginate(50)
|
|
->withQueryString()
|
|
->through(fn ($car) => [
|
|
'id' => $car->id,
|
|
'stammnummer' => $car->stammnummer,
|
|
'vin' => $car->vin,
|
|
'buy_contract' => $this->getContractFields($car->latestBuyContract),
|
|
'sell_contract' => $this->getContractFields($car->latestSellContract),
|
|
'car_model' => $car->carModel->only('name'),
|
|
'name' => $car->name,
|
|
'initial_date' => $car->initial_date,
|
|
'deleted_at' => $car->deleted_at,
|
|
'link' => route('cars.show', $car),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
private function getContractFields(?Contract $contract) {
|
|
if (!$contract) {
|
|
return null;
|
|
}
|
|
$contact = $contract->contact;
|
|
return [
|
|
'id' => $contract->id,
|
|
'date' => $contract->date,
|
|
'price' => $contract->price,
|
|
'type' => $contract->type,
|
|
'insurance_type' => InsuranceType::fromValue((int)$contract->insurance_type)->key,
|
|
'contact' => [
|
|
'id' => $contact->id,
|
|
'name' => $contact->name,
|
|
'firstname' => $contact->firstname,
|
|
'lastname' => $contact->lastname,
|
|
'phone' => $contact->phone,
|
|
'address' => $contact->address,
|
|
'zip' => $contact->zip,
|
|
'city' => $contact->city,
|
|
'country' => $contact->country,
|
|
'company' => $contact->company,
|
|
'email' => $contact->email,
|
|
'link' => route('contacts.edit', $contact),
|
|
],
|
|
'link' => route('contracts.show', $contract),
|
|
];
|
|
}
|
|
|
|
private function getWithCustomSort($cars, string $sortBy, string $direction)
|
|
{
|
|
switch($sortBy) {
|
|
case 'initial_date':
|
|
return $cars->orderBy('initial_date', $direction);
|
|
case 'stammnummer':
|
|
return $cars->orderBy('stammnummer', $direction);
|
|
default:
|
|
//return $cars->orderByName($direction);
|
|
return $cars->orderBy('initial_date', $direction);
|
|
}
|
|
}
|
|
|
|
private function getSortBy(Request $request)
|
|
{
|
|
if ($request->has('sortby')) {
|
|
return $request->get('sortby');
|
|
}
|
|
|
|
return 'name';
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
return Inertia::render('Cars/Create', [
|
|
'brands' => Brand::all()->map(function ($brand) {
|
|
return [
|
|
'id' => $brand->id,
|
|
'name' => $brand->name,
|
|
'models' => $brand->carModels()->get()
|
|
->map(function ($carModel) {
|
|
return [
|
|
'id' => $carModel->id,
|
|
'name' => $carModel->name,
|
|
];
|
|
}),
|
|
];
|
|
}),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$car = Car::create(
|
|
$request->validate([
|
|
'stammnummer' => ['required', 'unique:cars', 'string', 'size:11', 'regex:/[0-9]{3}[.][0-9]{3}[.][0-9]{3}/i'],
|
|
'vin' => ['required', 'unique:cars', 'string', 'size:17'],
|
|
'initial_date' => ['required', 'date'],
|
|
'last_check_date' => ['required', 'date'],
|
|
'colour' => ['nullable', 'max:75'],
|
|
'car_model_id' => ['required', 'exists:App\Models\CarModel,id'],
|
|
'kilometers' => ['required', 'max:75'],
|
|
'known_damage' => ['nullable'],
|
|
'notes' => ['nullable'],
|
|
])
|
|
);
|
|
|
|
return Redirect::route('cars.show', $car);
|
|
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Car $car
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show(Car $car)
|
|
{
|
|
return Inertia::render('Cars/Show', [
|
|
'car' => [
|
|
'id' => $car->id,
|
|
'stammnummer' => $car->stammnummer,
|
|
'vin' => $car->vin,
|
|
'car_model' => $car->carModel->only('id', 'name'),
|
|
'brand' => $car->brand,
|
|
'name' => $car->name,
|
|
'initial_date' => $car->initial_date,
|
|
'colour' => $car->colour,
|
|
'last_check_date' => $car->last_check_date,
|
|
'kilometers' => $car->kilometers,
|
|
'known_damage' => $car->known_damage,
|
|
'notes' => $car->notes,
|
|
'deleted_at' => $car->deleted_at,
|
|
'buy_contracts' => $car->buyContracts()
|
|
->orderBy('date', 'desc')
|
|
->paginate(50)
|
|
->through(fn ($contract) => $this->getContractFields($contract)),
|
|
'sell_contracts' => $car->sellContracts()
|
|
->orderBy('date', 'desc')
|
|
->paginate(50)
|
|
->through(fn ($contract) => $this->getContractFields($contract)),
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Car $car
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit(Car $car)
|
|
{
|
|
return Inertia::render('Cars/Edit', [
|
|
'car' => [
|
|
'id' => $car->id,
|
|
'stammnummer' => $car->stammnummer,
|
|
'vin' => $car->vin,
|
|
'car_model' => $car->carModel->only('id', 'name'),
|
|
'brand' => $car->brand,
|
|
'name' => $car->name,
|
|
'initial_date' => $car->initial_date,
|
|
'colour' => $car->colour,
|
|
'last_check_date' => $car->last_check_date,
|
|
'kilometers' => $car->kilometers,
|
|
'known_damage' => $car->known_damage,
|
|
'notes' => $car->notes,
|
|
'deleted_at' => $car->deleted_at,
|
|
],
|
|
'brands' => Brand::all()->map(function ($brand) {
|
|
return [
|
|
'id' => $brand->id,
|
|
'name' => $brand->name,
|
|
'models' => $brand->carModels()->get()
|
|
->map(function ($carModel) {
|
|
return [
|
|
'id' => $carModel->id,
|
|
'name' => $carModel->name,
|
|
];
|
|
}),
|
|
];
|
|
}),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \App\Models\Car $car
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, Car $car)
|
|
{
|
|
$car->update(
|
|
$request->validate([
|
|
'stammnummer' => ['required', 'unique:cars,stammnummer,' . $car->id, 'string', 'size:11', 'regex:/[0-9]{3}[.][0-9]{3}[.][0-9]{3}/i'],
|
|
'vin' => ['required', 'unique:cars,vin,' . $car->id, 'string', 'size:17'],
|
|
'initial_date' => ['required', 'date'],
|
|
'last_check_date' => ['required', 'date'],
|
|
'colour' => ['nullable', 'max:75'],
|
|
'car_model_id' => ['required', 'exists:App\Models\CarModel,id'],
|
|
'kilometers' => ['required', 'max:75'],
|
|
'known_damage' => ['nullable'],
|
|
'notes' => ['nullable'],
|
|
])
|
|
);
|
|
|
|
return Redirect::route('cars.show', $car)->with('success', 'Auto geändert.');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Car $car
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(Car $car)
|
|
{
|
|
$car->delete();
|
|
return Redirect::route('cars.show', $car)->with('success', 'Auto gelöscht.');
|
|
}
|
|
|
|
public function restore(Car $car)
|
|
{
|
|
$car->restore();
|
|
return Redirect::back()->with('success', 'Auto wiederhergestellt.');
|
|
}
|
|
}
|