phpvms/app/Http/Controllers/Admin/AircraftController.php

290 lines
7.7 KiB
PHP
Raw Normal View History

2017-06-09 09:37:51 +08:00
<?php
namespace App\Http\Controllers\Admin;
2019-07-16 03:51:35 +08:00
use App\Contracts\Controller;
use App\Http\Controllers\Admin\Traits\Importable;
2017-06-09 09:37:51 +08:00
use App\Http\Requests\CreateAircraftRequest;
use App\Http\Requests\UpdateAircraftRequest;
use App\Models\Aircraft;
use App\Models\Enums\AircraftStatus;
use App\Models\Enums\ImportExportType;
use App\Models\Expense;
2018-02-21 12:33:09 +08:00
use App\Models\Subfleet;
2017-06-25 03:00:56 +08:00
use App\Repositories\AircraftRepository;
2018-09-22 10:40:30 +08:00
use App\Repositories\AirportRepository;
2018-03-23 01:55:56 +08:00
use App\Services\ExportService;
use App\Services\ImportService;
2018-02-21 12:33:09 +08:00
use Illuminate\Http\Request;
use Laracasts\Flash\Flash;
2017-12-02 22:29:53 +08:00
class AircraftController extends Controller
2017-06-09 09:37:51 +08:00
{
use Importable;
2018-08-27 00:40:04 +08:00
private $aircraftRepo;
2018-09-22 10:40:30 +08:00
private $airportRepo;
2018-08-27 00:40:04 +08:00
private $importSvc;
2017-06-09 09:37:51 +08:00
2018-02-21 12:33:09 +08:00
/**
* AircraftController constructor.
2018-08-27 00:40:04 +08:00
*
2018-09-22 10:40:30 +08:00
* @param AirportRepository $airportRepo
2018-02-21 12:33:09 +08:00
* @param AircraftRepository $aircraftRepo
* @param ImportService $importSvc
2018-02-21 12:33:09 +08:00
*/
2017-06-25 00:09:27 +08:00
public function __construct(
2018-09-22 10:40:30 +08:00
AirportRepository $airportRepo,
AircraftRepository $aircraftRepo,
ImportService $importSvc
2017-06-25 00:09:27 +08:00
) {
$this->aircraftRepo = $aircraftRepo;
2018-09-22 10:40:30 +08:00
$this->airportRepo = $airportRepo;
$this->importSvc = $importSvc;
2017-06-09 09:37:51 +08:00
}
/**
* Display a listing of the Aircraft.
2018-08-27 00:40:04 +08:00
*
2018-03-26 22:33:45 +08:00
* @param Request $request
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2017-06-09 09:37:51 +08:00
*/
public function index(Request $request)
{
// If subfleet ID is passed part of the query string, then only
// show the aircraft that are in that subfleet
$w = [];
2018-08-27 00:40:04 +08:00
if ($request->filled('subfleet')) {
$w['subfleet_id'] = $request->input('subfleet');
}
2018-09-04 20:02:16 +08:00
$aircraft = $this->aircraftRepo->with(['subfleet'])->whereOrder($w, 'registration', 'asc');
$aircraft = $aircraft->all();
2017-06-09 09:37:51 +08:00
2017-12-02 22:29:53 +08:00
return view('admin.aircraft.index', [
2018-08-27 00:40:04 +08:00
'aircraft' => $aircraft,
'subfleet_id' => $request->input('subfleet'),
2017-12-02 22:29:53 +08:00
]);
2017-06-09 09:37:51 +08:00
}
/**
* Show the form for creating a new Aircraft.
2018-08-27 00:40:04 +08:00
*
* @param Request $request
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2017-06-09 09:37:51 +08:00
*/
public function create(Request $request)
2017-06-09 09:37:51 +08:00
{
return view('admin.aircraft.create', [
2018-10-26 07:10:37 +08:00
'airports' => $this->airportRepo->selectBoxList(),
2018-08-27 00:40:04 +08:00
'subfleets' => Subfleet::all()->pluck('name', 'id'),
'statuses' => AircraftStatus::select(true),
'subfleet_id' => $request->query('subfleet'),
]);
2017-06-09 09:37:51 +08:00
}
/**
* Store a newly created Aircraft in storage.
2018-08-27 00:40:04 +08:00
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
2017-06-09 09:37:51 +08:00
*/
public function store(CreateAircraftRequest $request)
{
2018-02-23 04:59:09 +08:00
$attrs = $request->all();
$aircraft = $this->aircraftRepo->create($attrs);
2017-06-09 09:37:51 +08:00
Flash::success('Aircraft saved successfully.');
2019-09-14 06:20:50 +08:00
return redirect(route('admin.aircraft.edit', [$aircraft->id]));
2017-06-09 09:37:51 +08:00
}
/**
* Display the specified Aircraft.
2018-08-27 00:40:04 +08:00
*
* @param mixed $id
2018-08-27 02:50:08 +08:00
*
* @return mixed
2017-06-09 09:37:51 +08:00
*/
public function show($id)
{
$aircraft = $this->aircraftRepo->findWithoutFail($id);
2017-06-09 09:37:51 +08:00
if (empty($aircraft)) {
Flash::error('Aircraft not found');
return redirect(route('admin.aircraft.index'));
}
return view('admin.aircraft.show', [
'aircraft' => $aircraft,
]);
2017-06-09 09:37:51 +08:00
}
/**
* Show the form for editing the specified Aircraft.
2018-08-27 00:40:04 +08:00
*
* @param mixed $id
2018-08-27 02:50:08 +08:00
*
* @return mixed
2017-06-09 09:37:51 +08:00
*/
public function edit($id)
{
$aircraft = $this->aircraftRepo->findWithoutFail($id);
2017-06-09 09:37:51 +08:00
if (empty($aircraft)) {
Flash::error('Aircraft not found');
return redirect(route('admin.aircraft.index'));
}
return view('admin.aircraft.edit', [
2018-09-22 10:40:30 +08:00
'aircraft' => $aircraft,
2018-10-26 07:10:37 +08:00
'airports' => $this->airportRepo->selectBoxList(),
'subfleets' => Subfleet::all()->pluck('name', 'id'),
'statuses' => AircraftStatus::select(true),
]);
2017-06-09 09:37:51 +08:00
}
/**
* Update the specified Aircraft in storage.
2018-08-27 00:40:04 +08:00
*
* @param mixed $id
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
2018-08-27 02:50:08 +08:00
*
* @return mixed
2017-06-09 09:37:51 +08:00
*/
public function update($id, UpdateAircraftRequest $request)
{
$aircraft = $this->aircraftRepo->findWithoutFail($id);
2017-06-09 09:37:51 +08:00
if (empty($aircraft)) {
Flash::error('Aircraft not found');
return redirect(route('admin.aircraft.index'));
}
2018-02-23 04:59:09 +08:00
$attrs = $request->all();
$this->aircraftRepo->update($attrs, $id);
2017-12-02 22:29:53 +08:00
2017-06-09 09:37:51 +08:00
Flash::success('Aircraft updated successfully.');
return redirect(route('admin.aircraft.index'));
}
/**
* Remove the specified Aircraft from storage.
2018-08-27 00:40:04 +08:00
*
* @param mixed $id
2018-08-27 02:50:08 +08:00
*
* @return mixed
2017-06-09 09:37:51 +08:00
*/
public function destroy($id)
{
$aircraft = $this->aircraftRepo->findWithoutFail($id);
2017-06-09 09:37:51 +08:00
if (empty($aircraft)) {
Flash::error('Aircraft not found');
return redirect(route('admin.aircraft.index'));
}
$this->aircraftRepo->delete($id);
2017-06-09 09:37:51 +08:00
Flash::success('Aircraft deleted successfully.');
return redirect(route('admin.aircraft.index'));
}
2018-03-23 01:55:56 +08:00
/**
2018-03-23 02:04:13 +08:00
* Run the aircraft exporter
2018-08-27 00:40:04 +08:00
*
2018-03-23 01:55:56 +08:00
* @param Request $request
2018-08-27 00:40:04 +08:00
*
2018-03-23 01:55:56 +08:00
* @throws \League\Csv\Exception
2018-08-27 00:40:04 +08:00
*
2018-08-27 02:50:08 +08:00
* @return mixed
2018-03-23 01:55:56 +08:00
*/
public function export(Request $request)
{
$exporter = app(ExportService::class);
$aircraft = $this->aircraftRepo->all();
$path = $exporter->exportAircraft($aircraft);
return response()
->download($path, 'aircraft.csv', [
'content-type' => 'text/csv',
])
->deleteFileAfterSend(true);
}
/**
* @param Request $request
2018-08-27 00:40:04 +08:00
*
2018-08-27 02:50:08 +08:00
* @return mixed
*/
public function import(Request $request)
{
$logs = [
'success' => [],
2018-03-23 23:37:34 +08:00
'errors' => [],
];
if ($request->isMethod('post')) {
$logs = $this->importFile($request, ImportExportType::AIRCRAFT);
}
return view('admin.aircraft.import', [
'logs' => $logs,
]);
}
/**
* @param Aircraft|null $aircraft
2018-08-27 00:40:04 +08:00
*
* @return mixed
*/
2018-03-23 06:48:57 +08:00
protected function return_expenses_view(Aircraft $aircraft)
{
$aircraft->refresh();
return view('admin.aircraft.expenses', [
'aircraft' => $aircraft,
]);
}
/**
* Operations for associating ranks to the subfleet
2018-08-27 00:40:04 +08:00
*
* @param $id
* @param Request $request
2018-08-27 00:40:04 +08:00
*
* @throws \Exception
2018-08-27 00:40:04 +08:00
*
2018-08-27 02:50:08 +08:00
* @return mixed
*/
public function expenses($id, Request $request)
{
$aircraft = $this->aircraftRepo->findWithoutFail($id);
if (empty($aircraft)) {
return $this->return_expenses_view($aircraft);
}
if ($request->isMethod('get')) {
return $this->return_expenses_view($aircraft);
}
if ($request->isMethod('post')) {
$expense = new Expense($request->post());
2018-04-02 03:32:01 +08:00
$expense->ref_model = Aircraft::class;
$expense->ref_model_id = $aircraft->id;
$expense->save();
} elseif ($request->isMethod('put')) {
$expense = Expense::findOrFail($request->input('expense_id'));
$expense->{$request->name} = $request->value;
$expense->save();
} // dissassociate fare from teh aircraft
elseif ($request->isMethod('delete')) {
$expense = Expense::findOrFail($request->input('expense_id'));
$expense->delete();
}
return $this->return_expenses_view($aircraft);
}
2017-06-09 09:37:51 +08:00
}