From 505931736cf1512efba350cb4cc2630e990e643b Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Mon, 5 Mar 2018 19:55:48 -0600 Subject: [PATCH] Finance overview page added to admin with monthly breakdown #130 --- ...17_06_23_011011_create_subfleet_tables.php | 1 + ...018_02_28_231813_create_journals_table.php | 1 + app/Database/seeds/sample.yml | 24 +++---- .../Controllers/Admin/FinanceController.php | 65 ++++++++++++++++++- app/Listeners/ExpenseListener.php | 4 +- app/Models/Airline.php | 6 ++ app/Models/Enums/JournalType.php | 13 ++++ app/Models/Journal.php | 24 ++++--- app/Models/SubfleetExpense.php | 2 + app/Models/Traits/JournalTrait.php | 1 + app/Models/User.php | 6 ++ app/Repositories/JournalRepository.php | 2 +- app/Services/FinanceService.php | 12 ++-- app/Support/Dates.php | 47 ++++++++++++++ .../views/admin/finances/index.blade.php | 24 +++++++ .../views/admin/finances/scripts.blade.php | 12 ++++ .../views/admin/finances/table.blade.php | 61 +++++++++++++++++ .../views/admin/pireps/transactions.blade.php | 2 +- 18 files changed, 276 insertions(+), 31 deletions(-) create mode 100644 app/Models/Enums/JournalType.php create mode 100644 app/Support/Dates.php create mode 100644 resources/views/admin/finances/index.blade.php create mode 100644 resources/views/admin/finances/scripts.blade.php create mode 100644 resources/views/admin/finances/table.blade.php diff --git a/app/Database/migrations/2017_06_23_011011_create_subfleet_tables.php b/app/Database/migrations/2017_06_23_011011_create_subfleet_tables.php index dd2ce40f..f0bc297f 100644 --- a/app/Database/migrations/2017_06_23_011011_create_subfleet_tables.php +++ b/app/Database/migrations/2017_06_23_011011_create_subfleet_tables.php @@ -30,6 +30,7 @@ class CreateSubfleetTables extends Migration $table->increments('id'); $table->unsignedBigInteger('subfleet_id'); $table->string('name', 50); + $table->unsignedTinyInteger('type'); # ExpenseType $table->unsignedDecimal('amount'); $table->timestamps(); diff --git a/app/Database/migrations/2018_02_28_231813_create_journals_table.php b/app/Database/migrations/2018_02_28_231813_create_journals_table.php index 30985ffa..d3c5dcfc 100644 --- a/app/Database/migrations/2018_02_28_231813_create_journals_table.php +++ b/app/Database/migrations/2018_02_28_231813_create_journals_table.php @@ -16,6 +16,7 @@ class CreateJournalsTable extends Migration Schema::create('journals', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('ledger_id')->nullable(); + $table->unsignedTinyInteger('type')->default(0); $table->bigInteger('balance')->default(0); $table->string('currency', 5); $table->nullableMorphs('morphed'); diff --git a/app/Database/seeds/sample.yml b/app/Database/seeds/sample.yml index 2a7337ce..409b5ed4 100644 --- a/app/Database/seeds/sample.yml +++ b/app/Database/seeds/sample.yml @@ -441,6 +441,7 @@ pirep_comments: journals: - id: '1' ledger_id: null + type: 0 balance: '15840000' currency: USD morphed_type: App\Models\Airline @@ -449,6 +450,7 @@ journals: updated_at: now - id: '2' ledger_id: null + type: 1 balance: '30000' currency: USD morphed_type: App\Models\User @@ -458,7 +460,7 @@ journals: journal_transactions: - id: 81e9d86c-fede-467d-befd-887e046d9c48 - transaction_group: fares + transaction_group: Fares journal_id: '1' credit: '6000000' debit: '0' @@ -471,7 +473,7 @@ journal_transactions: post_date: now deleted_at: null - id: b12a81a9-1273-4413-a46a-96b2925cfefb - transaction_group: fares + transaction_group: Fares journal_id: '1' credit: '1100000' debit: '0' @@ -484,7 +486,7 @@ journal_transactions: post_date: now deleted_at: null - id: 8688ff40-aed4-4d60-90b7-0c5a88c12fbc - transaction_group: fares + transaction_group: Fares journal_id: '1' credit: '1000000' debit: '0' @@ -497,7 +499,7 @@ journal_transactions: post_date: now deleted_at: null - id: d34a9d1e-0d54-4191-bf9f-0043062c04c9 - transaction_group: expenses + transaction_group: Expenses journal_id: '1' credit: null debit: '10000' @@ -510,7 +512,7 @@ journal_transactions: post_date: now deleted_at: null - id: bdc9d50d-ac3d-4334-997c-8f13b8328ab8 - transaction_group: expenses + transaction_group: Expenses journal_id: '1' credit: null debit: '10000' @@ -523,7 +525,7 @@ journal_transactions: post_date: now deleted_at: null - id: b5c45ad5-af73-4d7c-9352-3dfb8de292a0 - transaction_group: expenses + transaction_group: Expenses journal_id: '1' credit: null debit: '20000' @@ -536,12 +538,12 @@ journal_transactions: post_date: now deleted_at: null - id: e65083f9-23c3-4e98-8d63-cd7f35732f7b - transaction_group: ground_handling + transaction_group: Ground Handling journal_id: '1' credit: null debit: '75000' currency: USD - memo: 'Ground handling' + memo: 'Ground Handling' ref_class: App\Models\Pirep ref_class_id: pirepid_1 created_at: now @@ -549,7 +551,7 @@ journal_transactions: post_date: now deleted_at: null - id: 9825a96e-58b5-465f-8fb8-4c8e1e5567eb - transaction_group: pilot_pay + transaction_group: Pilot Pay journal_id: '1' credit: null debit: '15000' @@ -562,7 +564,7 @@ journal_transactions: post_date: now deleted_at: null - id: 2e3118b3-c98f-41d1-b2b6-ccb4f34e86b0 - transaction_group: pilot_pay + transaction_group: Pilot Pay journal_id: '2' credit: '15000' debit: null @@ -575,7 +577,7 @@ journal_transactions: post_date: now deleted_at: null - id: b98a837a-aa59-4630-a547-5a9d90b5b541 - transaction_group: subfleet_expense + transaction_group: Subfleet Expense journal_id: 1 credit: null debit: 100000 diff --git a/app/Http/Controllers/Admin/FinanceController.php b/app/Http/Controllers/Admin/FinanceController.php index b4a51fbc..85756417 100644 --- a/app/Http/Controllers/Admin/FinanceController.php +++ b/app/Http/Controllers/Admin/FinanceController.php @@ -2,8 +2,14 @@ namespace App\Http\Controllers\Admin; +use App\Models\Enums\JournalType; +use App\Models\Journal; +use App\Models\JournalTransaction; +use App\Repositories\AirlineRepository; use App\Repositories\JournalRepository; use App\Services\FinanceService; +use App\Support\Dates; +use App\Support\Money; use Illuminate\Http\Request; /** @@ -12,7 +18,8 @@ use Illuminate\Http\Request; */ class FinanceController extends BaseController { - private $financeSvc, + private $airlineRepo, + $financeSvc, $journalRepo; /** @@ -20,19 +27,73 @@ class FinanceController extends BaseController * @param JournalRepository $journalRepo */ public function __construct( + AirlineRepository $airlineRepo, FinanceService $financeSvc, JournalRepository $journalRepo ) { + $this->airlineRepo = $airlineRepo; $this->financeSvc = $financeSvc; $this->journalRepo = $journalRepo; } /** - * Display a listing of the Aircraft. + * Display the summation tables for a given month by airline + * @param Request $request + * @return mixed + * @throws \UnexpectedValueException + * @throws \InvalidArgumentException */ public function index(Request $request) { + $month = $request->query('month', date('Y-m')); + $between = Dates::getMonthBoundary($month); + $first_journal = Journal::where(['type' => JournalType::AIRLINE]) + ->orderBy('created_at', 'asc') + ->limit(1) + ->first(); + + $transaction_groups = []; + $airlines = $this->airlineRepo->orderBy('icao')->all(); + + # group by the airline + foreach($airlines as $airline) { + + # Return all the transactions, grouped by the transaction group + $transactions = JournalTransaction::groupBy('transaction_group') + ->selectRaw('transaction_group, currency, + SUM(credit) as sum_credits, + SUM(debit) as sum_debits') + ->where([ + 'journal_id' => $airline->journal->id + ]) + ->whereBetween('created_at', $between, 'AND') + ->orderBy('sum_credits', 'desc') + ->orderBy('sum_debits', 'desc') + ->orderBy('transaction_group', 'asc') + ->get(); + + # Summate it so we can show it on the footer of the table + $sum_all_credits = 0; + $sum_all_debits = 0; + foreach ($transactions as $ta) { + $sum_all_credits += $ta->sum_credits ?? 0; + $sum_all_debits += $ta->sum_debits ?? 0; + } + + $transaction_groups[] = [ + 'airline' => $airline, + 'credits' => new Money($sum_all_credits), + 'debits' => new Money($sum_all_debits), + 'transactions' => $transactions, + ]; + } + + return view('admin.finances.index', [ + 'current_month' => $month, + 'months_list' => Dates::getMonthsList($first_journal->created_at), + 'transaction_groups' => $transaction_groups, + ]); } /** diff --git a/app/Listeners/ExpenseListener.php b/app/Listeners/ExpenseListener.php index 2718e59b..5360b6c4 100644 --- a/app/Listeners/ExpenseListener.php +++ b/app/Listeners/ExpenseListener.php @@ -19,9 +19,11 @@ class ExpenseListener # This is an example of an expense to return # You have the pirep on $event->pirep, and any associated data + # The transaction group is how it will show as a line item /*$expenses[] = new Expense([ 'type' => ExpenseType::FLIGHT, - 'amount' => 15000 # $150 + 'amount' => 15000, # $150 + 'transaction_group' => '', ]);*/ return $expenses; diff --git a/app/Models/Airline.php b/app/Models/Airline.php index f7f47a7e..69e8d6b9 100644 --- a/app/Models/Airline.php +++ b/app/Models/Airline.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Models\Enums\JournalType; use App\Models\Traits\JournalTrait; /** @@ -15,6 +16,11 @@ class Airline extends BaseModel public $table = 'airlines'; + /** + * The journal type for the callback + */ + public $journal_type = JournalType::AIRLINE; + public $fillable = [ 'icao', 'iata', diff --git a/app/Models/Enums/JournalType.php b/app/Models/Enums/JournalType.php new file mode 100644 index 00000000..37a0cbab --- /dev/null +++ b/app/Models/Enums/JournalType.php @@ -0,0 +1,13 @@ + 'float', + 'type' => 'integer', ]; public static $rules = [ diff --git a/app/Models/Traits/JournalTrait.php b/app/Models/Traits/JournalTrait.php index 3979445c..2594243b 100644 --- a/app/Models/Traits/JournalTrait.php +++ b/app/Models/Traits/JournalTrait.php @@ -38,6 +38,7 @@ trait JournalTrait { if (!$this->journal) { $journal = new Journal(); + $journal->type = $this->journal_type; $journal->currency = $currency_code; $journal->balance = 0; $this->journal()->save($journal); diff --git a/app/Models/User.php b/app/Models/User.php index a98dbb2a..6061cf5b 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Models\Enums\JournalType; use App\Models\Enums\PirepState; use App\Models\Traits\JournalTrait; use Illuminate\Foundation\Auth\User as Authenticatable; @@ -32,6 +33,11 @@ class User extends Authenticatable public $table = 'users'; + /** + * The journal type for when it's being created + */ + public $journal_type = JournalType::USER; + protected $fillable = [ 'name', 'email', diff --git a/app/Repositories/JournalRepository.php b/app/Repositories/JournalRepository.php index bc1ecc09..234fc3dc 100644 --- a/app/Repositories/JournalRepository.php +++ b/app/Repositories/JournalRepository.php @@ -60,7 +60,7 @@ class JournalRepository extends BaseRepository implements CacheableInterface 'debit' => $debit ? $debit->getAmount():null, 'currency' => config('phpvms.currency'), 'memo' => $memo, - 'post_date' => $post_date ?: Carbon::now(), + 'post_date' => $post_date ?? Carbon::now(), ]; if($reference !== null) { diff --git a/app/Services/FinanceService.php b/app/Services/FinanceService.php index 66eff4fe..3cea9525 100644 --- a/app/Services/FinanceService.php +++ b/app/Services/FinanceService.php @@ -137,7 +137,7 @@ class FinanceService extends BaseService 'Fares ' . $fare->code . $fare->count .'; price: '.$fare->price.', cost: '.$fare->cost, null, - 'fares' + 'Fares' ); } } @@ -165,7 +165,7 @@ class FinanceService extends BaseService $pirep, 'Expense: ' . $expense->name, null, - 'expenses' + 'Expenses' ); } } @@ -198,7 +198,7 @@ class FinanceService extends BaseService $pirep, 'Subfleet ('.$subfleet->type.'): '.$expense->name, null, - 'subfleet_expense' + 'Subfleet Expense' ); } } @@ -221,7 +221,7 @@ class FinanceService extends BaseService $pirep, 'Ground Handling', null, - 'ground_handling' + 'Ground Handling' ); } @@ -249,7 +249,7 @@ class FinanceService extends BaseService $pirep, $memo, null, - 'pilot_pay' + 'Pilot Pay' ); $this->journalRepo->post( @@ -259,7 +259,7 @@ class FinanceService extends BaseService $pirep, $memo, null, - 'pilot_pay' + 'Pilot Pay' ); } diff --git a/app/Support/Dates.php b/app/Support/Dates.php new file mode 100644 index 00000000..3c00320e --- /dev/null +++ b/app/Support/Dates.php @@ -0,0 +1,47 @@ +format('Y-m'); + $months[$last_value] = $last_month->format ('Y F'); + $last_month = $last_month->addMonth(); + } while($last_value !== $now); + + return $months; + } + + /** + * Return the start/end dates for a given month/year + * @param $month YYYY-MM + * @return array + */ + public static function getMonthBoundary($month) + { + [$year, $month] = explode('-', $month); + $days = cal_days_in_month(CAL_GREGORIAN, $month, $year); + + return [ + "$year-$month-01", + "$year-$month-$days" + ]; + } +} diff --git a/resources/views/admin/finances/index.blade.php b/resources/views/admin/finances/index.blade.php new file mode 100644 index 00000000..6bdf2a7b --- /dev/null +++ b/resources/views/admin/finances/index.blade.php @@ -0,0 +1,24 @@ +@extends('admin.app') + +@section('title', 'Financial Reports') +@section('actions') +
  • Overview
  • +@endsection +@section('content') +
    +
    +
    + {!! Form::select( + 'month_select', + $months_list, + $current_month, + ['id' => 'month_select'] + ) !!} +
    + + @include('admin.finances.table') + +
    +
    +@endsection +@include('admin.finances.scripts') diff --git a/resources/views/admin/finances/scripts.blade.php b/resources/views/admin/finances/scripts.blade.php new file mode 100644 index 00000000..98c648fe --- /dev/null +++ b/resources/views/admin/finances/scripts.blade.php @@ -0,0 +1,12 @@ +@section('scripts') + +@endsection diff --git a/resources/views/admin/finances/table.blade.php b/resources/views/admin/finances/table.blade.php new file mode 100644 index 00000000..00e560bd --- /dev/null +++ b/resources/views/admin/finances/table.blade.php @@ -0,0 +1,61 @@ + +@foreach($transaction_groups as $group) + +

    {!! $group['airline']->icao !!} - {!! $group['airline']->name !!}

    + + + + + + + + + @foreach($group['transactions'] as $ta) + + + + + + @endforeach + + {{-- show sums --}} + + + + + + + {{-- final total --}} + + + + + + + +
    ExpensesCreditDebit
    + {!! $ta->transaction_group !!} + + @if($ta->sum_credits) + {!! money($ta->sum_credits, $ta->currency) !!} + @else + - + @endif + + @if($ta->sum_debits) + {!! money($ta->sum_debits, $ta->currency) !!} + @else + - + @endif +
    + {!! $group['credits'] !!} + + ({!! $group['debits'] !!}) +
    Total + {!! $group['credits']->subtract($group['debits']) !!} +
    + + @if(!$loop->last) +
    + @endif +@endforeach diff --git a/resources/views/admin/pireps/transactions.blade.php b/resources/views/admin/pireps/transactions.blade.php index 7a1fa299..0c0d5162 100644 --- a/resources/views/admin/pireps/transactions.blade.php +++ b/resources/views/admin/pireps/transactions.blade.php @@ -30,7 +30,7 @@ {{-- final total --}} - + Total