2017-06-11 07:27:19 +08:00
|
|
|
<?php
|
|
|
|
|
2017-06-11 11:17:45 +08:00
|
|
|
namespace App\Http\Controllers\Admin;
|
2017-06-11 07:27:19 +08:00
|
|
|
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Contracts\Controller;
|
2020-02-11 23:51:18 +08:00
|
|
|
use App\Http\Controllers\Admin\Traits\Importable;
|
2017-06-11 07:27:19 +08:00
|
|
|
use App\Http\Requests\CreateFareRequest;
|
|
|
|
use App\Http\Requests\UpdateFareRequest;
|
2020-03-07 04:10:03 +08:00
|
|
|
use App\Models\Enums\FareType;
|
2020-02-11 23:51:18 +08:00
|
|
|
use App\Models\Enums\ImportExportType;
|
2017-06-11 07:27:19 +08:00
|
|
|
use App\Repositories\FareRepository;
|
2018-03-23 06:48:57 +08:00
|
|
|
use App\Services\ExportService;
|
|
|
|
use App\Services\ImportService;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Http\Request;
|
2020-10-14 06:10:31 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-12-11 02:53:55 +08:00
|
|
|
use Laracasts\Flash\Flash;
|
2017-06-11 07:27:19 +08:00
|
|
|
use Prettus\Repository\Criteria\RequestCriteria;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
class FareController extends Controller
|
2017-06-11 07:27:19 +08:00
|
|
|
{
|
2020-02-11 23:51:18 +08:00
|
|
|
use Importable;
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
private $fareRepo;
|
|
|
|
private $importSvc;
|
2017-06-11 07:27:19 +08:00
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
|
|
|
* FareController constructor.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param FareRepository $fareRepo
|
2018-03-23 06:48:57 +08:00
|
|
|
* @param ImportService $importSvc
|
2018-02-21 12:33:09 +08:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2018-03-23 06:48:57 +08:00
|
|
|
FareRepository $fareRepo,
|
|
|
|
ImportService $importSvc
|
2018-02-21 12:33:09 +08:00
|
|
|
) {
|
2018-03-23 06:48:57 +08:00
|
|
|
$this->fareRepo = $fareRepo;
|
|
|
|
$this->importSvc = $importSvc;
|
2017-06-11 07:27:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the Fare.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-06-11 07:27:19 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2019-12-11 02:53:55 +08:00
|
|
|
* @return mixed
|
2017-06-11 07:27:19 +08:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2018-03-23 06:48:57 +08:00
|
|
|
$this->fareRepo->pushCriteria(new RequestCriteria($request));
|
|
|
|
$fares = $this->fareRepo->all();
|
2017-06-11 07:27:19 +08:00
|
|
|
|
|
|
|
return view('admin.fares.index')
|
|
|
|
->with('fares', $fares);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new Fare.
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2020-03-07 04:10:03 +08:00
|
|
|
return view('admin.fares.create', [
|
|
|
|
'fare_types' => FareType::select(),
|
|
|
|
]);
|
2017-06-11 07:27:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created Fare in storage.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-06-11 07:27:19 +08:00
|
|
|
* @param CreateFareRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2019-12-11 02:53:55 +08:00
|
|
|
* @return mixed
|
2017-06-11 07:27:19 +08:00
|
|
|
*/
|
|
|
|
public function store(CreateFareRequest $request)
|
|
|
|
{
|
|
|
|
$input = $request->all();
|
2018-03-23 06:48:57 +08:00
|
|
|
$fare = $this->fareRepo->create($input);
|
2017-06-11 07:27:19 +08:00
|
|
|
|
2018-03-23 06:48:57 +08:00
|
|
|
Flash::success('Fare saved successfully.');
|
2017-06-11 07:27:19 +08:00
|
|
|
return redirect(route('admin.fares.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified Fare.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
2019-12-11 02:53:55 +08:00
|
|
|
* @return mixed
|
2017-06-11 07:27:19 +08:00
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
2018-03-23 06:48:57 +08:00
|
|
|
$fare = $this->fareRepo->findWithoutFail($id);
|
2017-06-11 07:27:19 +08:00
|
|
|
if (empty($fare)) {
|
|
|
|
Flash::error('Fare not found');
|
|
|
|
return redirect(route('admin.fares.index'));
|
|
|
|
}
|
|
|
|
|
2020-03-07 04:10:03 +08:00
|
|
|
return view('admin.fares.show', [
|
|
|
|
'fare' => $fare,
|
|
|
|
]);
|
2017-06-11 07:27:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified Fare.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
2019-12-11 02:53:55 +08:00
|
|
|
* @return mixed
|
2017-06-11 07:27:19 +08:00
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
2018-03-23 06:48:57 +08:00
|
|
|
$fare = $this->fareRepo->findWithoutFail($id);
|
2017-06-11 07:27:19 +08:00
|
|
|
if (empty($fare)) {
|
|
|
|
Flash::error('Fare not found');
|
|
|
|
return redirect(route('admin.fares.index'));
|
|
|
|
}
|
|
|
|
|
2020-03-07 04:10:03 +08:00
|
|
|
return view('admin.fares.edit', [
|
|
|
|
'fare' => $fare,
|
|
|
|
'fare_types' => FareType::select(),
|
|
|
|
]);
|
2017-06-11 07:27:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified Fare in storage.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param int $id
|
2017-06-11 07:27:19 +08:00
|
|
|
* @param UpdateFareRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2019-12-11 02:53:55 +08:00
|
|
|
* @return mixed
|
2017-06-11 07:27:19 +08:00
|
|
|
*/
|
|
|
|
public function update($id, UpdateFareRequest $request)
|
|
|
|
{
|
2018-03-23 06:48:57 +08:00
|
|
|
$fare = $this->fareRepo->findWithoutFail($id);
|
2017-06-11 07:27:19 +08:00
|
|
|
if (empty($fare)) {
|
|
|
|
Flash::error('Fare not found');
|
|
|
|
return redirect(route('admin.fares.index'));
|
|
|
|
}
|
2017-06-11 11:17:45 +08:00
|
|
|
|
2018-03-23 06:48:57 +08:00
|
|
|
$fare = $this->fareRepo->update($request->all(), $id);
|
2017-06-11 07:27:19 +08:00
|
|
|
|
2018-03-23 06:48:57 +08:00
|
|
|
Flash::success('Fare updated successfully.');
|
2017-06-11 07:27:19 +08:00
|
|
|
return redirect(route('admin.fares.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified Fare from storage.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
2019-12-11 02:53:55 +08:00
|
|
|
* @return mixed
|
2017-06-11 07:27:19 +08:00
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2018-03-23 06:48:57 +08:00
|
|
|
$fare = $this->fareRepo->findWithoutFail($id);
|
2017-06-11 07:27:19 +08:00
|
|
|
if (empty($fare)) {
|
|
|
|
Flash::error('Fare not found');
|
|
|
|
return redirect(route('admin.fares.index'));
|
|
|
|
}
|
2017-06-11 11:17:45 +08:00
|
|
|
|
2020-10-14 06:10:31 +08:00
|
|
|
Log::info('Deleting fare "'.$fare->name.'", id='.$fare->id);
|
|
|
|
|
2018-03-23 06:48:57 +08:00
|
|
|
$this->fareRepo->delete($id);
|
2017-06-11 07:27:19 +08:00
|
|
|
|
2018-03-23 06:48:57 +08:00
|
|
|
Flash::success('Fare deleted successfully.');
|
2017-06-11 07:27:19 +08:00
|
|
|
return redirect(route('admin.fares.index'));
|
|
|
|
}
|
2018-03-23 06:48:57 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the aircraft exporter
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:48:57 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:48:57 +08:00
|
|
|
* @throws \League\Csv\Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
|
2018-03-23 06:48:57 +08:00
|
|
|
*/
|
|
|
|
public function export(Request $request)
|
|
|
|
{
|
|
|
|
$exporter = app(ExportService::class);
|
|
|
|
$fares = $this->fareRepo->all();
|
|
|
|
|
|
|
|
$path = $exporter->exportFares($fares);
|
|
|
|
return response()
|
|
|
|
->download($path, 'fares.csv', [
|
|
|
|
'content-type' => 'text/csv',
|
|
|
|
])
|
|
|
|
->deleteFileAfterSend(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:48:57 +08:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2018-03-23 06:48:57 +08:00
|
|
|
*/
|
|
|
|
public function import(Request $request)
|
|
|
|
{
|
|
|
|
$logs = [
|
|
|
|
'success' => [],
|
2018-03-23 23:37:34 +08:00
|
|
|
'errors' => [],
|
2018-03-23 06:48:57 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($request->isMethod('post')) {
|
2020-02-11 23:51:18 +08:00
|
|
|
$logs = $this->importFile($request, ImportExportType::FARES);
|
2018-03-23 06:48:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return view('admin.fares.import', [
|
|
|
|
'logs' => $logs,
|
|
|
|
]);
|
|
|
|
}
|
2017-06-11 07:27:19 +08:00
|
|
|
}
|