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

253 lines
7.0 KiB
PHP
Raw Normal View History

2017-06-09 09:37:51 +08:00
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\CreateAircraftRequest;
2018-03-23 06:17:37 +08:00
use App\Http\Requests\ImportRequest;
2017-06-09 09:37:51 +08:00
use App\Http\Requests\UpdateAircraftRequest;
use App\Interfaces\Controller;
use App\Models\Aircraft;
use App\Models\Enums\AircraftStatus;
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-03-23 01:55:56 +08:00
use App\Services\ExportService;
use App\Services\ImportService;
2017-06-09 09:37:51 +08:00
use Flash;
2018-02-21 12:33:09 +08:00
use Illuminate\Http\Request;
use Log;
2017-06-09 09:37:51 +08:00
use Prettus\Repository\Criteria\RequestCriteria;
use Storage;
2017-12-02 22:29:53 +08:00
/**
* Class AircraftController
* @package App\Http\Controllers\Admin
*/
class AircraftController extends Controller
2017-06-09 09:37:51 +08:00
{
private $aircraftRepo,
$importSvc;
2017-06-09 09:37:51 +08:00
2018-02-21 12:33:09 +08:00
/**
* AircraftController constructor.
* @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(
AircraftRepository $aircraftRepo,
ImportService $importSvc
2017-06-25 00:09:27 +08:00
) {
$this->aircraftRepo = $aircraftRepo;
$this->importSvc = $importSvc;
2017-06-09 09:37:51 +08:00
}
/**
* Display a listing of the Aircraft.
* @throws \Prettus\Repository\Exceptions\RepositoryException
2017-06-09 09:37:51 +08:00
*/
public function index(Request $request)
{
$this->aircraftRepo->pushCriteria(new RequestCriteria($request));
2018-03-24 05:41:09 +08:00
$aircraft = $this->aircraftRepo->orderBy('registration', 'asc')->all();
2017-06-09 09:37:51 +08:00
2017-12-02 22:29:53 +08:00
return view('admin.aircraft.index', [
'aircraft' => $aircraft
2017-12-02 22:29:53 +08:00
]);
2017-06-09 09:37:51 +08:00
}
/**
* Show the form for creating a new Aircraft.
*/
public function create()
{
return view('admin.aircraft.create', [
'subfleets' => Subfleet::all()->pluck('name', 'id'),
'statuses' => AircraftStatus::select(true),
]);
2017-06-09 09:37:51 +08:00
}
/**
* Store a newly created Aircraft in storage.
* @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.');
2018-02-13 00:26:27 +08:00
return redirect(route('admin.aircraft.edit', ['id' => $aircraft->id]));
2017-06-09 09:37:51 +08:00
}
/**
* Display the specified Aircraft.
*/
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.
*/
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');
2017-06-09 09:37:51 +08:00
return redirect(route('admin.aircraft.index'));
}
return view('admin.aircraft.edit', [
'subfleets' => Subfleet::all()->pluck('name', 'id'),
'statuses' => AircraftStatus::select(true),
2017-06-25 00:09:27 +08:00
'aircraft' => $aircraft,
]);
2017-06-09 09:37:51 +08:00
}
/**
* Update the specified Aircraft in storage.
* @throws \Prettus\Validator\Exceptions\ValidatorException
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.
*/
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-03-23 01:55:56 +08:00
* @param Request $request
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
* @throws \League\Csv\Exception
*/
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
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2018-03-23 06:17:37 +08:00
* @throws \Illuminate\Validation\ValidationException
*/
public function import(Request $request)
{
$logs = [
'success' => [],
2018-03-23 23:37:34 +08:00
'errors' => [],
];
if ($request->isMethod('post')) {
2018-03-23 06:17:37 +08:00
ImportRequest::validate($request);
$path = Storage::putFileAs(
2018-03-23 06:48:57 +08:00
'import', $request->file('csv_file'), 'import_aircraft.csv'
);
$path = storage_path('app/'.$path);
2018-03-23 06:48:57 +08:00
Log::info('Uploaded aircraft import file to '.$path);
$logs = $this->importSvc->importAircraft($path);
}
return view('admin.aircraft.import', [
'logs' => $logs,
]);
}
/**
* @param Aircraft|null $aircraft
* @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
* @param $id
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Exception
*/
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());
$expense->ref_class = Aircraft::class;
$expense->ref_class_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
}