Initial commit
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Concerns\SortsLists;
|
||||
use App\Http\Requests\StoreEngineRequest;
|
||||
use App\Http\Requests\UpdateEngineRequest;
|
||||
use App\Models\Engine;
|
||||
use App\Models\Manufacturer;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Katalog silników - pełny CRUD (legacy site204 / site204A / site204E / site204inf).
|
||||
* Usuwanie logiczne (ENG_STATUS='0'). Newest methods: Eloquent + Form Requests.
|
||||
*/
|
||||
class EngineController extends Controller
|
||||
{
|
||||
use SortsLists;
|
||||
|
||||
private const MCF_ALL = 100;
|
||||
|
||||
/** Sortowalne kolumny listy: klucz nagłówka => kolumna SQL. */
|
||||
private const SORTABLE = [
|
||||
'name' => 'ENG_NAME',
|
||||
'voltage' => 'ENG_VOLTAGE',
|
||||
'power' => 'ENG_POWER',
|
||||
'mcf' => 'm.MCF_NAME',
|
||||
];
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$by = $request->query('by', 'name'); // 'name' | 'power'
|
||||
$q = trim((string) $request->query('q', ''));
|
||||
$mcf = (int) $request->query('mcf', self::MCF_ALL);
|
||||
|
||||
[$sort, $dir] = $this->sortParams($request);
|
||||
|
||||
$engines = Engine::query()
|
||||
->visible()
|
||||
->with('manufacturer')
|
||||
->when($q !== '' && $by === 'name', fn ($w) => $w->where('ENG_NAME', 'like', '%'.$q.'%'))
|
||||
->when($q !== '' && $by === 'power', function ($w) use ($q) {
|
||||
$v = (float) str_replace(',', '.', $q);
|
||||
$w->whereBetween('ENG_POWER', [$v - $v * 0.1, $v + $v * 0.1]);
|
||||
})
|
||||
->when($mcf !== self::MCF_ALL, fn ($w) => $w->where('ENG_MCF_ID', $mcf))
|
||||
// Sortowanie po producencie wymaga joinu (relacja nie jest w FROM).
|
||||
->when($sort === 'mcf', fn ($w) => $w
|
||||
->leftJoin('seg_manufacturers as m', 'm.MCF_ID', '=', 'seg_engines.ENG_MCF_ID')
|
||||
->select('seg_engines.*'))
|
||||
->when($sort !== null, fn ($w) => $w->orderBy(self::SORTABLE[$sort], $dir))
|
||||
->orderBy('ENG_NAME')
|
||||
->paginate(20)
|
||||
->withQueryString();
|
||||
|
||||
return view('catalog.engines.index', [
|
||||
'engines' => $engines,
|
||||
'manufacturers' => $this->manufacturers(),
|
||||
'by' => $by,
|
||||
'q' => $q,
|
||||
'mcf' => $mcf,
|
||||
'mcfAll' => self::MCF_ALL,
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
return view('catalog.engines.create', ['manufacturers' => $this->manufacturers()]);
|
||||
}
|
||||
|
||||
public function store(StoreEngineRequest $request)
|
||||
{
|
||||
$engine = Engine::create($this->toColumns($request->validated()) + ['ENG_STATUS' => '1']);
|
||||
|
||||
return redirect()->route('engines.show', $engine)
|
||||
->with('status', 'Dodano silnik „'.$engine->ENG_NAME.'".');
|
||||
}
|
||||
|
||||
public function show(Engine $engine)
|
||||
{
|
||||
$engine->load('manufacturer');
|
||||
|
||||
$relatedPumps = DB::connection('softspm')->table('seg_pump_engine_rel as r')
|
||||
->leftJoin('seg_pumps as p', 'p.PUP_ID', '=', 'r.RPE_PUP_ID')
|
||||
->leftJoin('seg_manufacturers as m', 'm.MCF_ID', '=', 'p.PUP_MCF_ID')
|
||||
->where('r.RPE_ENG_ID', $engine->ENG_ID)
|
||||
->select('p.PUP_NAME', DB::raw('p.PUP_Q_N as q_n'), 'p.PUP_H_P', 'm.MCF_NAME')
|
||||
->get();
|
||||
|
||||
return view('catalog.engines.show', ['engine' => $engine, 'relatedPumps' => $relatedPumps]);
|
||||
}
|
||||
|
||||
public function edit(Engine $engine)
|
||||
{
|
||||
return view('catalog.engines.edit', [
|
||||
'engine' => $engine,
|
||||
'manufacturers' => $this->manufacturers(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(UpdateEngineRequest $request, Engine $engine)
|
||||
{
|
||||
$engine->update($this->toColumns($request->validated()));
|
||||
|
||||
return redirect()->route('engines.show', $engine)
|
||||
->with('status', 'Zaktualizowano silnik „'.$engine->ENG_NAME.'".');
|
||||
}
|
||||
|
||||
public function destroy(Engine $engine)
|
||||
{
|
||||
$engine->update(['ENG_STATUS' => '0']); // usuwanie logiczne
|
||||
|
||||
return redirect()->route('engines.index')
|
||||
->with('status', 'Usunięto silnik „'.$engine->ENG_NAME.'".');
|
||||
}
|
||||
|
||||
/** Mapowanie pól formularza → kolumny seg_engines. */
|
||||
private function toColumns(array $data): array
|
||||
{
|
||||
return [
|
||||
'ENG_NAME' => $data['name'],
|
||||
'ENG_MCF_ID' => $data['manufacturer_id'],
|
||||
'ENG_POWER' => $data['power'],
|
||||
'ENG_VOLTAGE' => $data['voltage'],
|
||||
'ENG_ELECTRICITY' => $data['current'] ?? null,
|
||||
'ENG_ETA' => $data['eta'] ?? null,
|
||||
'ENG_COS_FI' => $data['cos_fi'] ?? null,
|
||||
'ENG_RPM' => $data['rpm'] ?? null,
|
||||
'ENG_LENGTH' => $data['length'] ?? null,
|
||||
'ENG_WEIGHT' => $data['weight'] ?? null,
|
||||
'ENG_DIAMETER' => $data['diameter'] ?? null,
|
||||
'ENG_WIRES' => $data['wires'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
private function manufacturers()
|
||||
{
|
||||
return Manufacturer::orderBy('MCF_NAME')->pluck('MCF_NAME', 'MCF_ID');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user