2018-02-27 05:16:12 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Controller;
|
2020-02-11 23:51:18 +08:00
|
|
|
use App\Http\Controllers\Admin\Traits\Importable;
|
2018-02-27 05:16:12 +08:00
|
|
|
use App\Models\Enums\ExpenseType;
|
2020-02-12 23:40:52 +08:00
|
|
|
use App\Models\Enums\FlightType;
|
2020-02-11 23:51:18 +08:00
|
|
|
use App\Models\Enums\ImportExportType;
|
2018-03-06 11:24:49 +08:00
|
|
|
use App\Models\Expense;
|
2018-02-27 05:16:12 +08:00
|
|
|
use App\Repositories\AirlineRepository;
|
|
|
|
use App\Repositories\ExpenseRepository;
|
2018-03-23 06:17:37 +08:00
|
|
|
use App\Services\ExportService;
|
|
|
|
use App\Services\ImportService;
|
2018-02-27 05:16:12 +08:00
|
|
|
use Illuminate\Http\Request;
|
2019-12-11 02:53:55 +08:00
|
|
|
use Laracasts\Flash\Flash;
|
2018-02-27 05:16:12 +08:00
|
|
|
use Prettus\Repository\Criteria\RequestCriteria;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
class ExpenseController extends Controller
|
2018-02-27 05:16:12 +08:00
|
|
|
{
|
2020-02-11 23:51:18 +08:00
|
|
|
use Importable;
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
private $airlineRepo;
|
|
|
|
private $expenseRepo;
|
|
|
|
private $importSvc;
|
2018-02-27 05:16:12 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* expensesController constructor.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-27 05:16:12 +08:00
|
|
|
* @param AirlineRepository $airlineRepo
|
|
|
|
* @param ExpenseRepository $expenseRepo
|
2018-03-23 06:17:37 +08:00
|
|
|
* @param ImportService $importSvc
|
2018-02-27 05:16:12 +08:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
AirlineRepository $airlineRepo,
|
2018-03-23 06:17:37 +08:00
|
|
|
ExpenseRepository $expenseRepo,
|
|
|
|
ImportService $importSvc
|
2018-02-27 05:16:12 +08:00
|
|
|
) {
|
|
|
|
$this->airlineRepo = $airlineRepo;
|
|
|
|
$this->expenseRepo = $expenseRepo;
|
2018-03-23 06:17:37 +08:00
|
|
|
$this->importSvc = $importSvc;
|
2018-02-27 05:16:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the expenses.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-27 05:16:12 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-27 05:16:12 +08:00
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2018-02-27 05:16:12 +08:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
|
|
|
$this->expenseRepo->pushCriteria(new RequestCriteria($request));
|
2018-03-06 11:24:49 +08:00
|
|
|
$expenses = $this->expenseRepo->findWhere([
|
2018-08-27 00:40:04 +08:00
|
|
|
'ref_model' => Expense::class,
|
2018-03-06 11:24:49 +08:00
|
|
|
]);
|
2018-02-27 05:16:12 +08:00
|
|
|
|
|
|
|
return view('admin.expenses.index', [
|
2018-08-27 00:40:04 +08:00
|
|
|
'expenses' => $expenses,
|
2018-02-27 05:16:12 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new expenses.
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
return view('admin.expenses.create', [
|
|
|
|
'airlines_list' => $this->airlineRepo->selectBoxList(true),
|
|
|
|
'expense_types' => ExpenseType::select(),
|
2020-02-12 23:40:52 +08:00
|
|
|
'flight_types' => FlightType::select(),
|
2018-02-27 05:16:12 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created expenses in storage.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-27 05:16:12 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-27 05:16:12 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2018-02-27 05:16:12 +08:00
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$input = $request->all();
|
2018-04-02 03:32:01 +08:00
|
|
|
$input['ref_model'] = Expense::class;
|
2018-02-27 05:16:12 +08:00
|
|
|
$this->expenseRepo->create($input);
|
|
|
|
|
|
|
|
Flash::success('Expense saved successfully.');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-02-27 05:16:12 +08:00
|
|
|
return redirect(route('admin.expenses.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified expenses.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
2018-02-27 05:16:12 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
$expenses = $this->expenseRepo->findWithoutFail($id);
|
|
|
|
|
|
|
|
if (empty($expenses)) {
|
|
|
|
Flash::error('expenses not found');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-02-27 05:16:12 +08:00
|
|
|
return redirect(route('admin.expenses.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('admin.expenses.show', [
|
|
|
|
'expenses' => $expenses,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified expenses.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
2019-12-11 02:53:55 +08:00
|
|
|
* @return mixed
|
2018-02-27 05:16:12 +08:00
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
|
|
|
$expense = $this->expenseRepo->findWithoutFail($id);
|
|
|
|
|
|
|
|
if (empty($expense)) {
|
|
|
|
Flash::error('Expense not found');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-02-27 05:16:12 +08:00
|
|
|
return redirect(route('admin.expenses.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('admin.expenses.edit', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'expense' => $expense,
|
2018-02-27 05:16:12 +08:00
|
|
|
'airlines_list' => $this->airlineRepo->selectBoxList(true),
|
|
|
|
'expense_types' => ExpenseType::select(),
|
2020-02-12 23:40:52 +08:00
|
|
|
'flight_types' => FlightType::select(),
|
2018-02-27 05:16:12 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified expenses in storage.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param int $id
|
2018-02-27 05:16:12 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-27 05:16:12 +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
|
2018-02-27 05:16:12 +08:00
|
|
|
*/
|
|
|
|
public function update($id, Request $request)
|
|
|
|
{
|
|
|
|
$expenses = $this->expenseRepo->findWithoutFail($id);
|
|
|
|
|
|
|
|
if (empty($expenses)) {
|
|
|
|
Flash::error('Expense not found');
|
|
|
|
return redirect(route('admin.expenses.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->expenseRepo->update($request->all(), $id);
|
|
|
|
|
|
|
|
Flash::success('Expense updated successfully.');
|
|
|
|
return redirect(route('admin.expenses.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified expenses from storage.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
2019-12-11 02:53:55 +08:00
|
|
|
* @return mixed
|
2018-02-27 05:16:12 +08:00
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
$expenses = $this->expenseRepo->findWithoutFail($id);
|
|
|
|
|
|
|
|
if (empty($expenses)) {
|
|
|
|
Flash::error('Expense not found');
|
|
|
|
return redirect(route('admin.expenses.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->expenseRepo->delete($id);
|
|
|
|
|
|
|
|
Flash::success('Expense deleted successfully.');
|
|
|
|
return redirect(route('admin.expenses.index'));
|
|
|
|
}
|
2018-03-23 06:17:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the airport exporter
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:17:37 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:17:37 +08:00
|
|
|
* @throws \League\Csv\Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
|
2018-03-23 06:17:37 +08:00
|
|
|
*/
|
|
|
|
public function export(Request $request)
|
|
|
|
{
|
|
|
|
$exporter = app(ExportService::class);
|
|
|
|
$expenses = $this->expenseRepo->all();
|
|
|
|
|
|
|
|
$path = $exporter->exportExpenses($expenses);
|
|
|
|
return response()
|
|
|
|
->download($path, 'expenses.csv', [
|
|
|
|
'content-type' => 'text/csv',
|
|
|
|
])
|
|
|
|
->deleteFileAfterSend(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 06:17:37 +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:17:37 +08:00
|
|
|
*/
|
|
|
|
public function import(Request $request)
|
|
|
|
{
|
|
|
|
$logs = [
|
|
|
|
'success' => [],
|
|
|
|
'errors' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($request->isMethod('post')) {
|
2020-02-11 23:51:18 +08:00
|
|
|
$logs = $this->importFile($request, ImportExportType::EXPENSES);
|
2018-03-23 06:17:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return view('admin.expenses.import', [
|
|
|
|
'logs' => $logs,
|
|
|
|
]);
|
|
|
|
}
|
2018-02-27 05:16:12 +08:00
|
|
|
}
|