Add table/models and admin for expenses #136

This commit is contained in:
Nabeel Shahzad 2018-02-26 15:16:12 -06:00
parent 286ed78436
commit 4a73a5a6b3
27 changed files with 556 additions and 41 deletions

View File

@ -0,0 +1,37 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateExpensesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('expenses', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('airline_id')->nullable();
$table->string('name');
$table->unsignedDecimal('amount');
$table->unsignedTinyInteger('type');
$table->boolean('multiplier')->nullable()->default(0);
$table->boolean('active')->nullable()->default(1);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('expenses');
}
}

View File

@ -173,6 +173,31 @@ aircraft:
registration:
status: 2
expenses:
- name: Per-Flight (no muliplier)
amount: 100
type: 0
active: 1
- name: Per-Flight (multiplier)
amount: 100
type: 0
multiplier: 1
active: 1
- name: Per-Flight (multiplier, on airline)
airline_id: 1
amount: 200
type: 0
multiplier: 1
active: 1
- name: A daily fee
amount: 800
type: 1
active: 1
- name: A monthly fee
amount: 5000
type: 2
active: 1
fares:
- id: 1
code: Y

45
app/Events/Expenses.php Normal file
View File

@ -0,0 +1,45 @@
<?php
namespace App\Events;
use App\Models\Pirep;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
/**
* This event is dispatched when the expenses for a flight report
* are collected. Your listeners should return a list of Expense
* models. Don't call save on the model!
*
* Example return:
*
* [
* new Expense([
* 'airline_id': '', # < optional field
* 'name': '',
* 'amount': [DECIMAL],
* 'type': int from ExpenseType enum class
* ]),
* ]
*
* The event caller will check the 'type' to make sure that it
* will filter out expenses that only apply to the current process
*
* The event will have a copy of the PIREP model, if it's applicable
*
* @package App\Events
*/
class Expenses
{
use Dispatchable, SerializesModels;
public $pirep;
/**
* @param Pirep|null $pirep
*/
public function __construct(Pirep $pirep=null)
{
$this->pirep = $pirep;
}
}

View File

@ -0,0 +1,156 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\CreateAirlineRequest;
use App\Http\Requests\UpdateAirlineRequest;
use App\Models\Enums\ExpenseType;
use App\Repositories\AirlineRepository;
use App\Repositories\ExpenseRepository;
use Flash;
use Illuminate\Http\Request;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;
class ExpenseController extends BaseController
{
private $airlineRepo,
$expenseRepo;
/**
* expensesController constructor.
* @param AirlineRepository $airlineRepo
* @param ExpenseRepository $expenseRepo
*/
public function __construct(
AirlineRepository $airlineRepo,
ExpenseRepository $expenseRepo
) {
$this->airlineRepo = $airlineRepo;
$this->expenseRepo = $expenseRepo;
}
/**
* Display a listing of the expenses.
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Prettus\Repository\Exceptions\RepositoryException
*/
public function index(Request $request)
{
$this->expenseRepo->pushCriteria(new RequestCriteria($request));
$expenses = $this->expenseRepo->all();
return view('admin.expenses.index', [
'expenses' => $expenses
]);
}
/**
* 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(),
]);
}
/**
* Store a newly created expenses in storage.
* @param Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
public function store(Request $request)
{
$input = $request->all();
$this->expenseRepo->create($input);
Flash::success('Expense saved successfully.');
return redirect(route('admin.expenses.index'));
}
/**
* Display the specified expenses.
* @param int $id
* @return mixed
*/
public function show($id)
{
$expenses = $this->expenseRepo->findWithoutFail($id);
if (empty($expenses)) {
Flash::error('expenses not found');
return redirect(route('admin.expenses.index'));
}
return view('admin.expenses.show', [
'expenses' => $expenses,
]);
}
/**
* Show the form for editing the specified expenses.
* @param int $id
* @return Response
*/
public function edit($id)
{
$expense = $this->expenseRepo->findWithoutFail($id);
if (empty($expense)) {
Flash::error('Expense not found');
return redirect(route('admin.expenses.index'));
}
return view('admin.expenses.edit', [
'expense' => $expense,
'airlines_list' => $this->airlineRepo->selectBoxList(true),
'expense_types' => ExpenseType::select(),
]);
}
/**
* Update the specified expenses in storage.
* @param int $id
* @param Request $request
* @return Response
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
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.
* @param int $id
* @return Response
*/
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'));
}
}

View File

@ -3,16 +3,16 @@
namespace App\Models\Enums;
/**
* Class GenericState
* Class ActiveState
* @package App\Models\Enums
*/
class GenericState extends EnumBase
class ActiveState extends EnumBase
{
public const INACTIVE = 0;
public const ACTIVE = 1;
public static $labels = [
GenericState::INACTIVE => 'Inactive',
GenericState::ACTIVE => 'Active',
ActiveState::INACTIVE => 'Inactive',
ActiveState::ACTIVE => 'Active',
];
}

View File

@ -0,0 +1,20 @@
<?php
namespace App\Models\Enums;
/**
* Class ExpenseType
* @package App\Models\Enums
*/
class ExpenseType extends EnumBase {
public const FLIGHT = 0;
public const DAILY = 1;
public const MONTHLY = 2;
protected static $labels = [
ExpenseType::FLIGHT => 'Flight',
ExpenseType::DAILY => 'Daily',
ExpenseType::MONTHLY => 'Monthly',
];
}

38
app/Models/Expense.php Normal file
View File

@ -0,0 +1,38 @@
<?php
namespace App\Models;
/**
* Class Expense
* @package App\Models
*/
class Expense extends BaseModel
{
public $table = 'expenses';
public $fillable = [
'airline_id',
'name',
'amount',
'type',
'multiplier',
'active',
];
public static $rules = [
'active' => 'boolean',
'airline_id' => 'integer',
'amount' => 'float',
'multiplier' => 'integer',
'type' => 'integer',
];
/**
* Foreign Keys
*/
public function airline()
{
return $this->belongsTo(Airline::class, 'airline_id');
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace App\Repositories;
use App\Models\Expense;
use Prettus\Repository\Contracts\CacheableInterface;
use Prettus\Repository\Traits\CacheableRepository;
/**
* Class ExpenseRepository
* @package App\Repositories
*/
class ExpenseRepository extends BaseRepository implements CacheableInterface
{
use CacheableRepository;
public function model()
{
return Expense::class;
}
}

View File

@ -12,30 +12,21 @@ Route::group([
Route::match(['get', 'put'], 'airports/fuel', 'AirportController@fuel');
Route::resource('airports', 'AirportController');
Route::resource('fares', 'FareController');
# subfleet
Route::resource('subfleets', 'SubfleetController');
Route::match(['get', 'post', 'put', 'delete'], 'subfleets/{id}/fares', 'SubfleetController@fares');
Route::match(['get', 'post', 'delete'], 'subfleets/{id}/ranks', 'SubfleetController@ranks');
# aircraft and fare associations
Route::resource('aircraft', 'AircraftController');
# expenses
Route::resource('expenses', 'ExpenseController');
# fares
Route::resource('fares', 'FareController');
# flights and aircraft associations
Route::resource('flights', 'FlightController');
Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/fares', 'FlightController@fares');
Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/fields', 'FlightController@fields');
Route::match(['get', 'post', 'put', 'delete'], 'flights/{id}/subfleets', 'FlightController@subfleets');
# rankings
Route::resource('ranks', 'RankController');
Route::match(['get', 'post', 'put', 'delete'], 'ranks/{id}/subfleets', 'RankController@subfleets');
# view/update settings
Route::match(['get'], 'settings', 'SettingsController@index');
Route::match(['post', 'put'], 'settings', 'SettingsController@update')->name('settings.update');
# pirep related routes
Route::get('pireps/fares', 'PirepController@fares');
Route::get('pireps/pending', 'PirepController@pending');
@ -45,6 +36,19 @@ Route::group([
Route::resource('pirepfields', 'PirepFieldController');
# rankings
Route::resource('ranks', 'RankController');
Route::match(['get', 'post', 'put', 'delete'], 'ranks/{id}/subfleets', 'RankController@subfleets');
# settings
Route::match(['get'], 'settings', 'SettingsController@index');
Route::match(['post', 'put'], 'settings', 'SettingsController@update')->name('settings.update');
# subfleet
Route::resource('subfleets', 'SubfleetController');
Route::match(['get', 'post', 'put', 'delete'], 'subfleets/{id}/fares', 'SubfleetController@fares');
Route::match(['get', 'post', 'delete'], 'subfleets/{id}/ranks', 'SubfleetController@ranks');
Route::resource('users', 'UserController');
Route::get('users/{id}/regen_apikey',
'UserController@regen_apikey')->name('users.regen_apikey');

View File

@ -127,7 +127,7 @@ return [
'Yaml' => Symfony\Component\Yaml\Yaml::class,
# ENUMS
'GenericState' => App\Models\Enums\GenericState::class,
'ActiveState' => App\Models\Enums\ActiveState::class,
'UserState' => App\Models\Enums\UserState::class,
'PirepSource' => App\Models\Enums\PirepSource::class,
'PirepState' => App\Models\Enums\PirepState::class,

View File

@ -15,8 +15,10 @@
@endsection
@section('content')
<div class="card">
@include('admin.aircraft.table')
<div class="card border-blue-bottom">
<div class="content">
@include('admin.aircraft.table')
</div>
</div>
@endsection

View File

@ -10,8 +10,10 @@
@endsection
@section('content')
<div class="card">
<div class="card border-blue-bottom">
<div class="content">
@include('admin.airlines.table')
</div>
</div>
@endsection

View File

@ -62,14 +62,13 @@
<div class="row">
<div class="form-group col-sm-6">
{!! Form::label('ground_handling_cost', 'Ground Handling Cost:') !!}
{!! Form::number('ground_handling_cost', null, ['class' => 'form-control']) !!}
<p class="text-danger">{{ $errors->first('ground_handling_cost') }}</p>
@component('admin.components.info')
This is the base rate per-flight. A multiplier for this rate can be
set in the subfleet, so you can modulate those costs from there.
@endcomponent
{!! Form::number('ground_handling_cost', null, ['class' => 'form-control']) !!}
<p class="text-danger">{{ $errors->first('ground_handling_cost') }}</p>
</div>
<div class="form-group col-md-6">

View File

@ -14,8 +14,10 @@
@include('admin.airports.search')
</div>
<div class="card">
<div class="card border-blue-bottom">
<div class="content">
@include('admin.airports.table')
</div>
</div>
<div class="row">

View File

@ -0,0 +1,11 @@
@extends('admin.app')
@section('title', 'Add Expense')
@section('content')
<div class="card border-blue-bottom">
<div class="content">
{!! Form::open(['route' => 'admin.expenses.store']) !!}
@include('admin.expenses.fields')
{!! Form::close() !!}
</div>
</div>
@endsection

View File

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

View File

@ -0,0 +1,64 @@
<div class="row">
<!-- Code Field -->
<div class="form-group col-sm-6">
{!! Form::label('airline_id', 'Airline:') !!}
{!! Form::select('airline_id', $airlines_list, null , ['class' => 'form-control select2']) !!}
<p class="text-danger">{{ $errors->first('airline_id') }}</p>
@component('admin.components.info')
If an airline is selected, then the expense will only be applied
to the selected airline, or flights in that airline.
@endcomponent
</div>
<!-- Name Field -->
<div class="form-group col-sm-6">
{!! Form::label('type', 'Expense Type:') !!}&nbsp;<span class="required">*</span>
{!! Form::select('type', $expense_types, null , ['class' => 'form-control select2']) !!}
<p class="text-danger">{{ $errors->first('type') }}</p>
</div>
</div>
<div class="row">
<div class="form-group col-sm-6">
{!! Form::label('name', 'Expense Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
<p class="text-danger">{{ $errors->first('name') }}</p>
</div>
<div class="form-group col-sm-6">
{!! Form::label('amount', 'Amount:') !!}
{!! Form::number('amount', null, ['class' => 'form-control', 'min' => 0]) !!}
<p class="text-danger">{{ $errors->first('amount') }}</p>
</div>
</div>
<div class="row">
<div class="col-sm-5">
{!! Form::label('multiplier', 'Multiplier:') !!}
<label class="checkbox-inline">
{!! Form::hidden('multiplier', 0, false) !!}
{!! Form::checkbox('multiplier', 1, null) !!}
</label>
@component('admin.components.info')
If checked, with a PIREP, this expense can be modified by a multiplier
on the subfleet. This is ignored for daily and monthly expenses
@endcomponent
</div>
<div class="col-sm-3">
{!! Form::label('active', 'Active:') !!}
<label class="checkbox-inline">
{!! Form::hidden('active', 0, false) !!}
{!! Form::checkbox('active', 1, null) !!}
</label>
</div>
<div class="form-group col-sm-4">
<div class="pull-right">
{!! Form::button('Save', ['type' => 'submit', 'class' => 'btn btn-success']) !!}
<a href="{!! route('admin.expenses.index') !!}" class="btn btn-default">Cancel</a>
</div>
</div>
</div>

View File

@ -0,0 +1,25 @@
@extends('admin.app')
@section('title', 'Expenses')
@section('actions')
<li>
<a href="{!! route('admin.expenses.create') !!}">
<i class="ti-plus"></i>
Add New</a>
</li>
@endsection
@section('content')
<div class="card border-blue-bottom">
<div class="content">
@if(!filled($expenses))
<p class="text-center">
You must add a subfleet before you can add an aircraft!
</p>
@else
@include('admin.expenses.table')
@endif
</div>
</div>
@endsection

View File

@ -0,0 +1,40 @@
<table class="table table-hover table-responsive" id="expenses-table">
<thead>
<th>Name</th>
<th>Type</th>
<th style="text-align: center;">Amount</th>
<th>Airline</th>
<th class="text-center">Active</th>
<th></th>
</thead>
<tbody>
@foreach($expenses as $expense)
<tr>
<td><a href="{!! route('admin.expenses.edit', [$expense->id]) !!}">
{!! $expense->name !!}</a>
</td>
<td>{!! \App\Models\Enums\ExpenseType::label($expense->type) !!}</td>
<td style="text-align: center;">{!! $expense->amount !!}</td>
<td>
@if(filled($expense->airline))
{!! $expense->airline->name !!}
@else
<span class="description">-</span>
@endif
</td>
<td class="text-center">
<span class="label label-{!! $expense->active?'success':'default' !!}">
{!! \App\Models\Enums\ActiveState::label($expense->active) !!}
</span>
</td>
<td class="text-right">
{!! Form::open(['route' => ['admin.expenses.destroy', $expense->id], 'method' => 'delete']) !!}
<a href="{!! route('admin.expenses.edit', [$expense->id]) !!}"
class='btn btn-sm btn-success btn-icon'><i class="fas fa-pencil-alt"></i></a>
{!! 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>

View File

@ -11,8 +11,10 @@
@endsection
@section('content')
<div class="card">
@include('admin.fares.table')
<div class="card border-blue-bottom">
<div class="content">
@include('admin.fares.table')
</div>
</div>
@endsection

View File

@ -117,6 +117,7 @@
<!-- Active Field -->
<div class="col-sm-4">
{!! Form::label('active', 'Active:') !!}
{!! Form::hidden('active', 0, false) !!}
@if($flight!==null)
{!! Form::checkbox('active', $flight->active, ['class' => 'form-control icheck']) !!}
@else

View File

@ -14,8 +14,10 @@
@include('admin.flights.search')
</div>
<div class="card">
@include('admin.flights.table')
<div class="card border-blue-bottom">
<div class="content">
@include('admin.flights.table')
</div>
</div>
<div class="row">

View File

@ -33,9 +33,10 @@
class="pe-7s-paper-plane"></i>airlines</a></li>
<li><a href="{!! url('/admin/airports') !!}"><i
class="pe-7s-map-marker"></i>airports</a></li>
<li><a href="{!! url('/admin/expenses') !!}"><i class="pe-7s-cash"></i>expenses</a></li>
<li><a href="{!! url('/admin/users') !!}"><i class="pe-7s-users"></i>users</a></li>
<li><a href="{!! url('/admin/ranks') !!}"><i class="pe-7s-id"></i>ranks</a></li>
<li><a href="{!! url('/admin/settings') !!}"><i class="pe-7s-id"></i>settings</a></li>
<li><a href="{!! url('/admin/ranks') !!}"><i class="pe-7s-graph1"></i>ranks</a></li>
<li><a href="{!! url('/admin/settings') !!}"><i class="pe-7s-config"></i>settings</a></li>
</ul>
</div>
</li>

View File

@ -9,8 +9,10 @@
@endsection
@section('content')
<div class="card">
@include('admin.ranks.table')
<div class="card border-blue-bottom">
<div class="content">
@include('admin.ranks.table')
</div>
</div>
@endsection
@include('admin.ranks.scripts')

View File

@ -13,7 +13,11 @@
<tr>
<td width="70%">
<p>{!! $setting->name !!}</p>
<p class="description">{{$setting->description}}</p></td>
<p class="description">
@component('admin.components.info')
{{$setting->description}}
@endcomponent
</p></td>
<td align="center">
@if($setting->type === 'date')
{!! Form::input('text', $setting->id, $setting->value, ['class' => 'form-control', 'id' => 'datepicker']) !!}

View File

@ -28,14 +28,13 @@
<div class="form-group col-sm-4">
{!! Form::label('ground_handling_multiplier', 'Ground Handling Multiplier:') !!}
{!! Form::text('ground_handling_multiplier', null, ['class' => 'form-control']) !!}
<p class="text-danger">{{ $errors->first('ground_handling_multiplier') }}</p>
@component('admin.components.info')
This is the multiplier of the airport ground-handling cost to charge for
aircraft in this subfleet, as a percentage. Defaults to 100.
@endcomponent
{!! Form::text('ground_handling_multiplier', null, ['class' => 'form-control']) !!}
<p class="text-danger">{{ $errors->first('ground_handling_multiplier') }}</p>
</div>
</div>
<div class="row">

View File

@ -12,8 +12,10 @@
@include('admin.users.search')
</div>
<div class="card">
@include('admin.users.table')
<div class="card border-blue-bottom">
<div class="content">
@include('admin.users.table')
</div>
</div>
<div class="row">