Skip to content

Commit

Permalink
Add subfleet specific expenses, fixed bug for pirep fares #130 #136
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Mar 5, 2018
1 parent bf94a20 commit 96e8fbf
Show file tree
Hide file tree
Showing 12 changed files with 357 additions and 44 deletions.
11 changes: 11 additions & 0 deletions app/Database/factories/SubfleetExpenseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Faker\Generator as Faker;

$factory->define(App\Models\SubfleetExpense::class, function (Faker $faker) {
return [
'subfleet_id' => null,
'name' => $faker->text(20),
'amount' => $faker->randomFloat(2, 100, 1000),
];
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ public function up()
});

Schema::create('subfleet_expenses', function(Blueprint $table) {
$table->increments('id');
$table->unsignedBigInteger('subfleet_id');
$table->string('name', 50);
$table->unsignedDecimal('cost');
$table->unsignedDecimal('amount');
$table->timestamps();

$table->primary(['subfleet_id', 'name']);
$table->index('subfleet_id');
});

Schema::create('subfleet_fare', function (Blueprint $table) {
Expand Down
87 changes: 54 additions & 33 deletions app/Database/seeds/sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ subfleets:
type: 772-36ER-GE90-115B
ground_handling_multiplier: 150

subfleet_expenses:
- id: 1
subfleet_id: 1
name: Catering
amount: 1000
created_at: now
updated_at: now

# add a few mods to aircraft and fares
subfleet_fare:

Expand Down Expand Up @@ -433,20 +441,20 @@ pirep_comments:
journals:
- id: '1'
ledger_id: null
balance: '7970000'
balance: '15840000'
currency: USD
morphed_type: App\Models\Airline
morphed_id: '1'
created_at: '2018-03-02 23:50:01'
updated_at: '2018-03-02 23:50:01'
created_at: now
updated_at: now
- id: '2'
ledger_id: null
balance: '15000'
balance: '30000'
currency: USD
morphed_type: App\Models\User
morphed_id: '1'
created_at: '2018-03-02 23:50:01'
updated_at: '2018-03-02 23:50:01'
created_at: now
updated_at: now

journal_transactions:
- id: 81e9d86c-fede-467d-befd-887e046d9c48
Expand All @@ -458,9 +466,9 @@ journal_transactions:
memo: 'Fares Y300; price:200, cost: 0'
ref_class: App\Models\Pirep
ref_class_id: pirepid_1
created_at: '2018-03-02 23:50:01'
updated_at: '2018-03-02 23:50:01'
post_date: '2018-03-02 23:50:01'
created_at: now
updated_at: now
post_date: now
deleted_at: null
- id: b12a81a9-1273-4413-a46a-96b2925cfefb
transaction_group: fares
Expand All @@ -471,9 +479,9 @@ journal_transactions:
memo: 'Fares B10; price:1100, cost: 0'
ref_class: App\Models\Pirep
ref_class_id: pirepid_1
created_at: '2018-03-02 23:50:01'
updated_at: '2018-03-02 23:50:01'
post_date: '2018-03-02 23:50:01'
created_at: now
updated_at: now
post_date: now
deleted_at: null
- id: 8688ff40-aed4-4d60-90b7-0c5a88c12fbc
transaction_group: fares
Expand All @@ -484,9 +492,9 @@ journal_transactions:
memo: 'Fares F10; price:1000, cost: 0'
ref_class: App\Models\Pirep
ref_class_id: pirepid_1
created_at: '2018-03-02 23:50:01'
updated_at: '2018-03-02 23:50:01'
post_date: '2018-03-02 23:50:01'
created_at: now
updated_at: now
post_date: now
deleted_at: null
- id: d34a9d1e-0d54-4191-bf9f-0043062c04c9
transaction_group: expenses
Expand All @@ -497,9 +505,9 @@ journal_transactions:
memo: 'Expense: Per-Flight (no muliplier)'
ref_class: App\Models\Pirep
ref_class_id: pirepid_1
created_at: '2018-03-02 23:50:01'
updated_at: '2018-03-02 23:50:01'
post_date: '2018-03-02 23:50:01'
created_at: now
updated_at: now
post_date: now
deleted_at: null
- id: bdc9d50d-ac3d-4334-997c-8f13b8328ab8
transaction_group: expenses
Expand All @@ -510,9 +518,9 @@ journal_transactions:
memo: 'Expense: Per-Flight (multiplier)'
ref_class: App\Models\Pirep
ref_class_id: pirepid_1
created_at: '2018-03-02 23:50:01'
updated_at: '2018-03-02 23:50:01'
post_date: '2018-03-02 23:50:01'
created_at: now
updated_at: now
post_date: now
deleted_at: null
- id: b5c45ad5-af73-4d7c-9352-3dfb8de292a0
transaction_group: expenses
Expand All @@ -523,9 +531,9 @@ journal_transactions:
memo: 'Expense: Per-Flight (multiplier, on airline)'
ref_class: App\Models\Pirep
ref_class_id: pirepid_1
created_at: '2018-03-02 23:50:01'
updated_at: '2018-03-02 23:50:01'
post_date: '2018-03-02 23:50:01'
created_at: now
updated_at: now
post_date: now
deleted_at: null
- id: e65083f9-23c3-4e98-8d63-cd7f35732f7b
transaction_group: ground_handling
Expand All @@ -536,9 +544,9 @@ journal_transactions:
memo: 'Ground handling'
ref_class: App\Models\Pirep
ref_class_id: pirepid_1
created_at: '2018-03-02 23:50:01'
updated_at: '2018-03-02 23:50:01'
post_date: '2018-03-02 23:50:01'
created_at: now
updated_at: now
post_date: now
deleted_at: null
- id: 9825a96e-58b5-465f-8fb8-4c8e1e5567eb
transaction_group: pilot_pay
Expand All @@ -549,9 +557,9 @@ journal_transactions:
memo: 'Pilot Payment @ 50'
ref_class: App\Models\Pirep
ref_class_id: pirepid_1
created_at: '2018-03-02 23:50:01'
updated_at: '2018-03-02 23:50:01'
post_date: '2018-03-02 23:50:01'
created_at: now
updated_at: now
post_date: now
deleted_at: null
- id: 2e3118b3-c98f-41d1-b2b6-ccb4f34e86b0
transaction_group: pilot_pay
Expand All @@ -562,7 +570,20 @@ journal_transactions:
memo: 'Pilot Payment @ 50'
ref_class: App\Models\Pirep
ref_class_id: pirepid_1
created_at: '2018-03-02 23:50:01'
updated_at: '2018-03-02 23:50:01'
post_date: '2018-03-02 23:50:01'
created_at: now
updated_at: now
post_date: now
deleted_at: null
- id: b98a837a-aa59-4630-a547-5a9d90b5b541
transaction_group: subfleet_expense
journal_id: 1
credit: null
debit: 100000
currency: USD
memo: 'Subfleet Expense: Catering'
ref_class: App\Models\Pirep
ref_class_id: pirepid_1
created_at: now
updated_at: now
post_date: now
deleted_at: null
56 changes: 54 additions & 2 deletions app/Http/Controllers/Admin/SubfleetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\Airline;
use App\Models\Enums\FuelType;
use App\Models\Subfleet;
use App\Models\SubfleetExpense;
use App\Repositories\AircraftRepository;
use App\Repositories\FareRepository;
use App\Repositories\RankRepository;
Expand Down Expand Up @@ -232,7 +233,7 @@ public function destroy($id)
* @param Subfleet $subfleet
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
protected function return_ranks_view(Subfleet $subfleet)
protected function return_ranks_view(?Subfleet $subfleet)
{
$subfleet->refresh();

Expand All @@ -248,7 +249,7 @@ protected function return_ranks_view(Subfleet $subfleet)
* @param Subfleet $subfleet
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
protected function return_fares_view(Subfleet $subfleet)
protected function return_fares_view(?Subfleet $subfleet)
{
$subfleet->refresh();

Expand Down Expand Up @@ -303,6 +304,57 @@ public function ranks($id, Request $request)
return $this->return_ranks_view($subfleet);
}

/**
* @param Subfleet $subfleet
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
protected function return_expenses_view(?Subfleet $subfleet)
{
$subfleet->refresh();
return view('admin.subfleets.expenses', [
'subfleet' => $subfleet,
]);
}

/**
* 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)
{
$subfleet = $this->subfleetRepo->findWithoutFail($id);
if (empty($subfleet)) {
return $this->return_expenses_view($subfleet);
}

if ($request->isMethod('get')) {
return $this->return_expenses_view($subfleet);
}

/**
* update specific rank data
*/
if ($request->isMethod('post')) {
$expense = new SubfleetExpense($request->post());
$expense->subfleet_id = $subfleet->id;
$expense->save();
$subfleet->refresh();
} elseif ($request->isMethod('put')) {
$expense = SubfleetExpense::findOrFail($request->input('expense_id'));
$expense->{$request->name} = $request->value;
$expense->save();
} // dissassociate fare from teh aircraft
elseif ($request->isMethod('delete')) {
$expense = SubfleetExpense::findOrFail($request->input('expense_id'));
$expense->delete();
}

return $this->return_expenses_view($subfleet);
}

/**
* Operations on fares to the subfleet
* @param $id
Expand Down
11 changes: 11 additions & 0 deletions app/Models/Subfleet.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public static function boot()
* Relationships
*/

/**
* @return $this
*/
public function aircraft()
{
return $this->hasMany(Aircraft::class, 'subfleet_id')
Expand All @@ -85,6 +88,14 @@ public function airline()
return $this->belongsTo(Airline::class, 'airline_id');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function expenses()
{
return $this->hasMany(SubfleetExpense::class, 'subfleet_id');
}

public function fares()
{
return $this->belongsToMany(Fare::class, 'subfleet_fare')
Expand Down
45 changes: 45 additions & 0 deletions app/Models/SubfleetExpense.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Models;

/**
* Class SubfleetExpense
* @package App\Models
*/
class SubfleetExpense extends BaseModel
{
public $table = 'subfleet_expenses';

public $fillable = [
'subfleet_id',
'name',
'amount',
];

/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'amount' => 'float',
];

public static $rules = [
'name' => 'required',
'amount' => 'required|numeric',
];

/**
* Relationships
*/

/**
* Has a subfleet
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function subfleet()
{
return $this->belongsTo(Subfleet::class, 'subfleet_id');
}
}
1 change: 1 addition & 0 deletions app/Routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

# subfleet
Route::resource('subfleets', 'SubfleetController');
Route::match(['get', 'post', 'put', 'delete'], 'subfleets/{id}/expenses', 'SubfleetController@expenses');
Route::match(['get', 'post', 'put', 'delete'], 'subfleets/{id}/fares', 'SubfleetController@fares');
Route::match(['get', 'post', 'put', 'delete'], 'subfleets/{id}/ranks', 'SubfleetController@ranks');

Expand Down
Loading

0 comments on commit 96e8fbf

Please sign in to comment.