Add expenses on airports as well #130 #136

This commit is contained in:
Nabeel Shahzad 2018-03-05 22:10:00 -06:00
parent 88a8fd2bbd
commit db10ebf807
9 changed files with 217 additions and 29 deletions

View File

@ -27,6 +27,7 @@ class CreateJournalTransactionsTable extends Migration
$table->dateTime('post_date');
$table->softDeletes();
$table->primary('id');
$table->index('journal_id');
$table->index('transaction_group');
$table->index(['ref_class', 'ref_class_id']);

View File

@ -4,6 +4,8 @@ namespace App\Http\Controllers\Admin;
use App\Http\Requests\CreateAirportRequest;
use App\Http\Requests\UpdateAirportRequest;
use App\Models\Airport;
use App\Models\Expense;
use App\Repositories\AirportRepository;
use App\Repositories\Criteria\WhereCriteria;
use Flash;
@ -15,7 +17,7 @@ use Response;
class AirportController extends BaseController
{
/** @var AirportRepository */
private $airportRepository;
private $airportRepo;
/**
* AirportController constructor.
@ -24,7 +26,7 @@ class AirportController extends BaseController
public function __construct(
AirportRepository $airportRepo
) {
$this->airportRepository = $airportRepo;
$this->airportRepo = $airportRepo;
}
/**
@ -40,8 +42,8 @@ class AirportController extends BaseController
$where['icao'] = $request->get('icao');
}
$this->airportRepository->pushCriteria(new WhereCriteria($request, $where));
$airports = $this->airportRepository
$this->airportRepo->pushCriteria(new WhereCriteria($request, $where));
$airports = $this->airportRepo
->orderBy('icao', 'asc')
->paginate();
@ -72,7 +74,7 @@ class AirportController extends BaseController
$input = $request->all();
$input['hub'] = get_truth_state($input['hub']);
$this->airportRepository->create($input);
$this->airportRepo->create($input);
Flash::success('Airport saved successfully.');
return redirect(route('admin.airports.index'));
@ -85,7 +87,7 @@ class AirportController extends BaseController
*/
public function show($id)
{
$airport = $this->airportRepository->findWithoutFail($id);
$airport = $this->airportRepo->findWithoutFail($id);
if (empty($airport)) {
Flash::error('Airport not found');
@ -104,7 +106,7 @@ class AirportController extends BaseController
*/
public function edit($id)
{
$airport = $this->airportRepository->findWithoutFail($id);
$airport = $this->airportRepo->findWithoutFail($id);
if (empty($airport)) {
Flash::error('Airport not found');
@ -126,7 +128,7 @@ class AirportController extends BaseController
*/
public function update($id, UpdateAirportRequest $request)
{
$airport = $this->airportRepository->findWithoutFail($id);
$airport = $this->airportRepo->findWithoutFail($id);
if (empty($airport)) {
Flash::error('Airport not found');
@ -136,7 +138,7 @@ class AirportController extends BaseController
$attrs = $request->all();
$attrs['hub'] = get_truth_state($attrs['hub']);
$this->airportRepository->update($attrs, $id);
$this->airportRepo->update($attrs, $id);
Flash::success('Airport updated successfully.');
return redirect(route('admin.airports.index'));
@ -149,19 +151,70 @@ class AirportController extends BaseController
*/
public function destroy($id)
{
$airport = $this->airportRepository->findWithoutFail($id);
$airport = $this->airportRepo->findWithoutFail($id);
if (empty($airport)) {
Flash::error('Airport not found');
return redirect(route('admin.airports.index'));
}
$this->airportRepository->delete($id);
$this->airportRepo->delete($id);
Flash::success('Airport deleted successfully.');
return redirect(route('admin.airports.index'));
}
/**
* @param Airport|null $airport
* @return mixed
*/
protected function return_expenses_view(?Airport $airport)
{
$airport->refresh();
return view('admin.airports.expenses', [
'airport' => $airport,
]);
}
/**
* 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)
{
$airport = $this->airportRepo->findWithoutFail($id);
if (empty($airport)) {
return $this->return_expenses_view($airport);
}
if ($request->isMethod('get')) {
return $this->return_expenses_view($airport);
}
/**
* update specific rank data
*/
if ($request->isMethod('post')) {
$expense = new Expense($request->post());
$expense->ref_class = Airport::class;
$expense->ref_class_id = $airport->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($airport);
}
/**
* Set fuel prices for this airport
* @param Request $request
@ -171,7 +224,7 @@ class AirportController extends BaseController
{
$id = $request->id;
$airport = $this->airportRepository->findWithoutFail($id);
$airport = $this->airportRepo->findWithoutFail($id);
if (empty($airport)) {
Flash::error('Flight not found');
return redirect(route('admin.flights.index'));

View File

@ -342,7 +342,6 @@ class SubfleetController extends BaseController
$expense->ref_class = Subfleet::class;
$expense->ref_class_id = $subfleet->id;
$expense->save();
$subfleet->refresh();
} elseif ($request->isMethod('put')) {
$expense = Expense::findOrFail($request->input('expense_id'));
$expense->{$request->name} = $request->value;

View File

@ -6,14 +6,9 @@ use App\Models\Expense;
trait Expensable
{
/**
* Initialize a new journal when a new record is created
*/
public static function bootExpensable()
{
/*static::created(function ($model) {
$model->initJournal(config('phpvms.currency'));
});*/
}
/**
@ -22,7 +17,11 @@ trait Expensable
*/
public function expenses()
{
return $this->hasMany(Expense::class, 'ref_class_id')
->where('ref_class', __CLASS__);
return $this->morphMany(
Expense::class,
'expenses', # overridden by the next two anyway
'ref_class',
'ref_class_id'
);
}
}

View File

@ -11,6 +11,7 @@ Route::group([
Route::match(['get', 'put'], 'airports/fuel', 'AirportController@fuel');
Route::resource('airports', 'AirportController');
Route::match(['get', 'post', 'put', 'delete'], 'airports/{id}/expenses', 'AirportController@expenses');
# aircraft and fare associations
Route::resource('aircraft', 'AircraftController');

View File

@ -1,12 +1,22 @@
@extends('admin.app')
@section('title', "Edit \"$airport->name\"")
@section('content')
<div class="card border-blue-bottom">
<div class="content">
{!! Form::model($airport, ['route' => ['admin.airports.update', $airport->id], 'method' => 'patch', 'id' => 'airportForm']) !!}
@include('admin.airports.fields')
{!! Form::close() !!}
<div class="card border-blue-bottom">
<div class="content">
{!! Form::model($airport, [
'route' => ['admin.airports.update', $airport->id],
'method' => 'patch',
'id' => 'airportForm'])
!!}
@include('admin.airports.fields')
{!! Form::close() !!}
</div>
</div>
<div class="card border-blue-bottom">
<div class="content">
@include('admin.airports.expenses')
</div>
</div>
</div>
@endsection
@include('admin.airports.script')

View File

@ -0,0 +1,75 @@
<div id="airport-expenses-wrapper" class="col-12">
<div class="header">
<h3>expenses</h3>
@component('admin.components.info')
These expenses are only applied to this airport.
@endcomponent
</div>
<br/>
<table class="table table-responsive" id="airport-expenses">
@if(count($airport->expenses))
<thead>
<th>Name</th>
<th>Cost&nbsp;<span class="small">{!! currency(config('phpvms.currency')) !!}</span></th>
<th>Type</th>
<th></th>
</thead>
@endif
<tbody>
@foreach($airport->expenses as $expense)
<tr>
<td>
<p>
<a class="text" href="#" data-pk="{!! $expense->id !!}"
data-name="name">{!! $expense->name !!}</a>
</p>
</td>
<td>
<p>
<a class="text" href="#" data-pk="{!! $expense->id !!}"
data-name="amount">{!! $expense->amount !!}</a>
</p>
</td>
<td>
<p>
<a href="#"
class="dropdown"
data-pk="{!! $expense->id !!}"
data-name="type">{!! \App\Models\Enums\ExpenseType::label($expense->type) !!}</a>
</p>
</td>
<td align="right">
{!! Form::open(['url' => url('/admin/airports/'.$airport->id.'/expenses'),
'method' => 'delete', 'class' => 'modify_expense form-inline']) !!}
{!! Form::hidden('expense_id', $expense->id) !!}
{!! Form::button('<i class="fa fa-times"></i>', ['type' => 'submit',
'class' => 'btn btn-sm btn-danger btn-icon',
'onclick' => "return confirm('Are you sure?')",
]) !!}
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
</table>
<hr/>
<div class="row">
<div class="col-sm-12">
<div class="text-right">
{!! Form::open(['url' => url('/admin/airports/'.$airport->id.'/expenses'),
'method' => 'post', 'class' => 'modify_expense form-inline']) !!}
{!! Form::input('text', 'name', null, ['class' => 'form-control input-sm', 'placeholder' => 'Name']) !!}
{!! Form::number('amount', null, ['class' => 'form-control input-sm', 'placeholder' => 'Amount']) !!}
{!! Form::select('type', \App\Models\Enums\ExpenseType::select(), null, ['class' => 'select2']) !!}
{!! Form::button('<i class="fa fa-plus"></i> Add', ['type' => 'submit',
'class' => 'btn btn-success btn-small']) !!}
{!! Form::close() !!}
<p class="text-danger">{{ $errors->first('name') }}</p>
<p class="text-danger">{{ $errors->first('amount') }}</p>
</div>
</div>
</div>
</div>

View File

@ -1,5 +1,39 @@
@section('scripts')
<script>
function setEditable() {
@if(isset($airport))
$('#airport-expenses a.text').editable({
emptytext: '0',
url: '{!! url('/admin/airports/'.$airport->id.'/expenses') !!}',
title: 'Enter override value',
ajaxOptions: {'type': 'put'},
params: function (params) {
return {
expense_id: params.pk,
name: params.name,
value: params.value
}
}
});
$('#airport-expenses a.dropdown').editable({
type: 'select',
emptytext: '0',
source: {!! json_encode(list_to_editable(\App\Models\Enums\ExpenseType::select())) !!},
url: '{!! url('/admin/airports/'.$airport->id.'/expenses') !!}',
title: 'Enter override value',
ajaxOptions: {'type': 'put'},
params: function (params) {
return {
expense_id: params.pk,
name: params.name,
value: params.value
}
}
});
@endif
}
function phpvms_vacentral_airport_lookup(icao, callback) {
$.ajax({
url: BASE_URL + '/api/airports/'+ icao + '/lookup',
@ -14,6 +48,8 @@ function phpvms_vacentral_airport_lookup(icao, callback) {
$(document).ready(function() {
setEditable();
$('#airports-table a.inline').editable({
type: 'text',
mode: 'inline',
@ -53,6 +89,17 @@ $(document).ready(function() {
});
});
});
$(document).on('submit', 'form.modify_expense', function (event) {
event.preventDefault();
console.log(event);
$.pjax.submit(event, '#airport-expenses-wrapper', {push: false});
});
$(document).on('pjax:complete', function () {
$(".select2").select2();
setEditable();
});
});
</script>
@endsection

View File

@ -58,12 +58,15 @@
<div class="text-right">
{!! Form::open(['url' => url('/admin/subfleets/'.$subfleet->id.'/expenses'),
'method' => 'post', 'class' => 'modify_expense form-inline']) !!}
{!! Form::input('text', 'name', null, ['class' => 'form-control input-sm', 'placeholder' => 'Name']) !!}
{!! Form::number('cost', null, ['class' => 'form-control input-sm', 'placeholder' => 'Amount']) !!}
{!! Form::text('name', null, ['class' => 'form-control input-sm', 'placeholder' => 'Name']) !!}
{!! Form::number('amount', null, ['class' => 'form-control input-sm', 'placeholder' => 'Amount']) !!}
{!! Form::select('type', \App\Models\Enums\ExpenseType::select(), null, ['class' => 'select2']) !!}
{!! Form::button('<i class="fa fa-plus"></i> Add', ['type' => 'submit',
'class' => 'btn btn-success btn-small']) !!}
{!! Form::close() !!}
<p class="text-danger">{{ $errors->first('name') }}</p>
<p class="text-danger">{{ $errors->first('amount') }}</p>
</div>
</div>
</div>