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;
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Interfaces\Controller;
|
2018-03-07 07:32:56 +08:00
|
|
|
use App\Models\Aircraft;
|
2018-02-24 06:37:10 +08:00
|
|
|
use App\Models\Enums\AircraftStatus;
|
2018-03-07 07:32:56 +08:00
|
|
|
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;
|
2018-03-22 08:12:36 +08:00
|
|
|
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;
|
2018-03-21 08:17:11 +08:00
|
|
|
use Log;
|
2017-06-09 09:37:51 +08:00
|
|
|
use Prettus\Repository\Criteria\RequestCriteria;
|
2018-03-21 08:17:11 +08:00
|
|
|
use Storage;
|
2017-12-02 22:29:53 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class AircraftController
|
|
|
|
* @package App\Http\Controllers\Admin
|
|
|
|
*/
|
|
|
|
class AircraftController extends Controller
|
2017-06-09 09:37:51 +08:00
|
|
|
{
|
2018-03-21 08:17:11 +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
|
2018-03-22 08:12:36 +08:00
|
|
|
* @param ImportService $importSvc
|
2018-02-21 12:33:09 +08:00
|
|
|
*/
|
2017-06-25 00:09:27 +08:00
|
|
|
public function __construct(
|
2018-03-21 08:17:11 +08:00
|
|
|
AircraftRepository $aircraftRepo,
|
2018-03-22 08:12:36 +08:00
|
|
|
ImportService $importSvc
|
2017-06-25 00:09:27 +08:00
|
|
|
) {
|
2018-03-07 07:32:56 +08:00
|
|
|
$this->aircraftRepo = $aircraftRepo;
|
2018-03-21 08:17:11 +08:00
|
|
|
$this->importSvc = $importSvc;
|
2017-06-09 09:37:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the Aircraft.
|
2018-01-10 03:34:19 +08:00
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
2017-06-09 09:37:51 +08:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2018-03-07 07:32:56 +08:00
|
|
|
$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', [
|
2017-12-30 07:34:09 +08:00
|
|
|
'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()
|
|
|
|
{
|
2017-06-23 09:55:45 +08:00
|
|
|
return view('admin.aircraft.create', [
|
|
|
|
'subfleets' => Subfleet::all()->pluck('name', 'id'),
|
2018-03-20 09:50:40 +08:00
|
|
|
'statuses' => AircraftStatus::select(true),
|
2017-06-23 09:55:45 +08:00
|
|
|
]);
|
2017-06-09 09:37:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created Aircraft in storage.
|
2018-01-10 03:34:19 +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();
|
2018-03-07 07:32:56 +08:00
|
|
|
$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)
|
|
|
|
{
|
2018-03-07 07:32:56 +08:00
|
|
|
$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'));
|
|
|
|
}
|
|
|
|
|
2017-06-23 09:55:45 +08:00
|
|
|
return view('admin.aircraft.show', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'aircraft' => $aircraft,
|
2017-06-23 09:55:45 +08:00
|
|
|
]);
|
2017-06-09 09:37:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified Aircraft.
|
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
2018-03-07 07:32:56 +08:00
|
|
|
$aircraft = $this->aircraftRepo->findWithoutFail($id);
|
2017-06-09 09:37:51 +08:00
|
|
|
|
|
|
|
if (empty($aircraft)) {
|
|
|
|
Flash::error('Aircraft not found');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-06-09 09:37:51 +08:00
|
|
|
return redirect(route('admin.aircraft.index'));
|
|
|
|
}
|
|
|
|
|
2017-06-23 09:55:45 +08:00
|
|
|
return view('admin.aircraft.edit', [
|
|
|
|
'subfleets' => Subfleet::all()->pluck('name', 'id'),
|
2018-03-20 09:50:40 +08:00
|
|
|
'statuses' => AircraftStatus::select(true),
|
2017-06-25 00:09:27 +08:00
|
|
|
'aircraft' => $aircraft,
|
2017-06-23 09:55:45 +08:00
|
|
|
]);
|
2017-06-09 09:37:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified Aircraft in storage.
|
2018-01-10 03:34:19 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2017-06-09 09:37:51 +08:00
|
|
|
*/
|
|
|
|
public function update($id, UpdateAircraftRequest $request)
|
|
|
|
{
|
2018-03-07 07:32:56 +08:00
|
|
|
$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();
|
2018-03-07 07:32:56 +08:00
|
|
|
$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)
|
|
|
|
{
|
2018-03-07 07:32:56 +08:00
|
|
|
$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-03-07 07:32:56 +08:00
|
|
|
$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-07 07:32:56 +08:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2018-03-23 06:17:37 +08:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2018-03-21 08:17:11 +08:00
|
|
|
*/
|
|
|
|
public function import(Request $request)
|
|
|
|
{
|
|
|
|
$logs = [
|
|
|
|
'success' => [],
|
2018-03-23 23:37:34 +08:00
|
|
|
'errors' => [],
|
2018-03-21 08:17:11 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($request->isMethod('post')) {
|
2018-03-23 06:17:37 +08:00
|
|
|
ImportRequest::validate($request);
|
2018-03-21 08:17:11 +08:00
|
|
|
$path = Storage::putFileAs(
|
2018-03-23 06:48:57 +08:00
|
|
|
'import', $request->file('csv_file'), 'import_aircraft.csv'
|
2018-03-21 08:17:11 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
$path = storage_path('app/'.$path);
|
2018-03-23 06:48:57 +08:00
|
|
|
Log::info('Uploaded aircraft import file to '.$path);
|
2018-03-21 08:17:11 +08:00
|
|
|
$logs = $this->importSvc->importAircraft($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('admin.aircraft.import', [
|
|
|
|
'logs' => $logs,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-03-07 07:32:56 +08:00
|
|
|
/**
|
|
|
|
* @param Aircraft|null $aircraft
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-03-23 06:48:57 +08:00
|
|
|
protected function return_expenses_view(Aircraft $aircraft)
|
2018-03-07 07:32:56 +08:00
|
|
|
{
|
|
|
|
$aircraft->refresh();
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-03-07 07:32:56 +08:00
|
|
|
return view('admin.aircraft.expenses', [
|
|
|
|
'aircraft' => $aircraft,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Operations for associating ranks to the subfleet
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $id
|
2018-03-07 07:32:56 +08:00
|
|
|
* @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
|
|
|
}
|