2017-06-09 09:37:51 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Http\Requests\CreateAircraftRequest;
|
|
|
|
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-21 08:17:11 +08:00
|
|
|
use App\Services\ImporterService;
|
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-21 08:17:11 +08:00
|
|
|
* @param ImporterService $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,
|
|
|
|
ImporterService $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));
|
|
|
|
$aircraft = $this->aircraftRepo->orderBy('name', '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-21 08:17:11 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
* @throws \League\Csv\Exception
|
|
|
|
*/
|
|
|
|
public function import(Request $request)
|
|
|
|
{
|
|
|
|
$logs = [
|
|
|
|
'success' => [],
|
|
|
|
'failed' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($request->isMethod('post')) {
|
|
|
|
$path = Storage::putFileAs(
|
|
|
|
'import', $request->file('csv_file'), 'aircraft'
|
|
|
|
);
|
|
|
|
|
|
|
|
$path = storage_path('app/'.$path);
|
|
|
|
Log::info('Uploaded flights import file to '.$path);
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
protected function return_expenses_view(?Aircraft $aircraft)
|
|
|
|
{
|
|
|
|
$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
|
|
|
}
|