Skip to content

Commit

Permalink
Add table/models and admin for expenses #136
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Feb 26, 2018
1 parent 286ed78 commit 4a73a5a
Show file tree
Hide file tree
Showing 27 changed files with 556 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -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');
}
}
25 changes: 25 additions & 0 deletions app/Database/seeds/sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions app/Events/Expenses.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
156 changes: 156 additions & 0 deletions app/Http/Controllers/Admin/ExpenseController.php
Original file line number Diff line number Diff line change
@@ -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'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}
20 changes: 20 additions & 0 deletions app/Models/Enums/ExpenseType.php
Original file line number Diff line number Diff line change
@@ -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 changes: 38 additions & 0 deletions app/Models/Expense.php
Original file line number Diff line number Diff line change
@@ -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');
}
}
21 changes: 21 additions & 0 deletions app/Repositories/ExpenseRepository.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 4a73a5a

Please sign in to comment.