Initial commit
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Concerns\SortsLists;
|
||||
use App\Http\Requests\StorePumpRequest;
|
||||
use App\Http\Requests\UpdatePumpRequest;
|
||||
use App\Models\Manufacturer;
|
||||
use App\Models\Pump;
|
||||
use App\Services\PumpService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Katalog pomp - pełny CRUD (legacy site203 / site203A / site203E / site203inf).
|
||||
* Newest methods: Eloquent, resource controller, Form Requests, serwis domenowy.
|
||||
*/
|
||||
class PumpController extends Controller
|
||||
{
|
||||
use SortsLists;
|
||||
|
||||
/** Producent „Wszyscy" (jak legacy: 100). */
|
||||
private const MCF_ALL = 100;
|
||||
|
||||
/** Sortowalne kolumny listy: klucz nagłówka => kolumna SQL. */
|
||||
private const SORTABLE = [
|
||||
'name' => 'PUP_NAME',
|
||||
'flow' => 'PUP_Q_N',
|
||||
'hp' => 'PUP_H_P',
|
||||
'mcf' => 'm.MCF_NAME',
|
||||
];
|
||||
|
||||
public function __construct(private readonly PumpService $pumps) {}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$by = $request->query('by', 'name'); // 'name' | 'flow'
|
||||
$q = trim((string) $request->query('q', ''));
|
||||
$mcf = (int) $request->query('mcf', self::MCF_ALL);
|
||||
|
||||
[$sort, $dir] = $this->sortParams($request);
|
||||
|
||||
$pumps = Pump::query()
|
||||
->visible()
|
||||
->with('manufacturer')
|
||||
->where('PUP_Q_N', '>', 0)
|
||||
->when($q !== '' && $by === 'name', fn ($w) => $w->where('PUP_NAME', 'like', '%'.$q.'%'))
|
||||
->when($q !== '' && $by === 'flow', function ($w) use ($q) {
|
||||
$v = (float) str_replace(',', '.', $q);
|
||||
$w->whereBetween('PUP_Q_N', [$v - $v * 0.1, $v + $v * 0.1]); // skala UAT bez ×60
|
||||
})
|
||||
->when($mcf !== self::MCF_ALL, fn ($w) => $w->where('PUP_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_pumps.PUP_MCF_ID')
|
||||
->select('seg_pumps.*'))
|
||||
->when($sort !== null, fn ($w) => $w->orderBy(self::SORTABLE[$sort], $dir))
|
||||
->orderBy('PUP_Q_N')
|
||||
->orderBy('PUP_NAME')
|
||||
->paginate(20)
|
||||
->withQueryString();
|
||||
|
||||
return view('catalog.pumps.index', [
|
||||
'pumps' => $pumps,
|
||||
'manufacturers' => $this->manufacturers(),
|
||||
'by' => $by,
|
||||
'q' => $q,
|
||||
'mcf' => $mcf,
|
||||
'mcfAll' => self::MCF_ALL,
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
return view('catalog.pumps.create', ['manufacturers' => $this->manufacturers()]);
|
||||
}
|
||||
|
||||
public function store(StorePumpRequest $request)
|
||||
{
|
||||
$pump = $this->pumps->create($request->validated());
|
||||
|
||||
return redirect()
|
||||
->route('pumps.show', $pump)
|
||||
->with('status', 'Dodano pompę „'.$pump->PUP_NAME.'".');
|
||||
}
|
||||
|
||||
public function show(Request $request, Pump $pump)
|
||||
{
|
||||
$pump->load('manufacturer');
|
||||
|
||||
$h = $this->pumps->curve($pump, 'H'); // x = m³/h (bez /60)
|
||||
$eta = $this->pumps->curve($pump, 'ETA');
|
||||
|
||||
// Warunki rysowania jak legacy: H bez zer, η dopuszcza 1 zero (punkt Q=0).
|
||||
$hDrawable = $this->pumps->curveDrawable($h, 0);
|
||||
$etaDrawable = $this->pumps->curveDrawable($eta, 1);
|
||||
|
||||
// Przelicz dla zadanego Q [m³/h]. Domyślnie Q_n; clamp do zakresu.
|
||||
$maxX = (float) ($h->max('x') ?? 0);
|
||||
$qn = round((float) $pump->PUP_Q_N, 3);
|
||||
$qIn = $request->query('q');
|
||||
$q = $qIn === null ? $qn : (float) str_replace(',', '.', (string) $qIn);
|
||||
$outOfRange = $q < 0 || $q > $maxX;
|
||||
$q = max(0, min($q, $maxX));
|
||||
|
||||
$relatedEngines = DB::connection('softspm')->table('seg_pump_engine_rel as r')
|
||||
->leftJoin('seg_engines as e', 'e.ENG_ID', '=', 'r.RPE_ENG_ID')
|
||||
->leftJoin('seg_manufacturers as m', 'm.MCF_ID', '=', 'e.ENG_MCF_ID')
|
||||
->where('r.RPE_PUP_ID', $pump->PUP_ID)
|
||||
->select('e.ENG_NAME', 'e.ENG_VOLTAGE', 'e.ENG_POWER', 'm.MCF_NAME')
|
||||
->get();
|
||||
|
||||
return view('catalog.pumps.show', [
|
||||
'pump' => $pump,
|
||||
'h' => $h,
|
||||
'eta' => $eta,
|
||||
'hDrawable' => $hDrawable,
|
||||
'etaDrawable' => $etaDrawable,
|
||||
'hPlot' => $hDrawable ? $this->pumps->plot($h) : null,
|
||||
'etaPlot' => $etaDrawable ? $this->pumps->plot($eta) : null,
|
||||
'relatedEngines' => $relatedEngines,
|
||||
'q' => $q,
|
||||
'hAtQ' => $hDrawable ? $this->pumps->valueAt($h, $q) : null,
|
||||
'etaAtQ' => $etaDrawable ? $this->pumps->valueAt($eta, $q) : null,
|
||||
'outOfRange' => $outOfRange,
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(Pump $pump)
|
||||
{
|
||||
$pump->load('manufacturer');
|
||||
|
||||
return view('catalog.pumps.edit', [
|
||||
'pump' => $pump,
|
||||
'manufacturers' => $this->manufacturers(),
|
||||
'h' => $this->pumps->curve($pump, 'H'),
|
||||
'eta' => $this->pumps->curve($pump, 'ETA'),
|
||||
'qn' => round((float) $pump->PUP_Q_N, 3),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(UpdatePumpRequest $request, Pump $pump)
|
||||
{
|
||||
$this->pumps->update($pump, $request->validated());
|
||||
|
||||
return redirect()
|
||||
->route('pumps.show', $pump)
|
||||
->with('status', 'Zaktualizowano pompę „'.$pump->PUP_NAME.'".');
|
||||
}
|
||||
|
||||
public function destroy(Pump $pump)
|
||||
{
|
||||
$this->pumps->delete($pump);
|
||||
|
||||
return redirect()
|
||||
->route('pumps.index')
|
||||
->with('status', 'Usunięto pompę „'.$pump->PUP_NAME.'".');
|
||||
}
|
||||
|
||||
/** MCF_ID => MCF_NAME do selectów. */
|
||||
private function manufacturers()
|
||||
{
|
||||
return Manufacturer::orderBy('MCF_NAME')->pluck('MCF_NAME', 'MCF_ID');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user