Finance overview page added to admin with monthly breakdown #130

This commit is contained in:
Nabeel Shahzad 2018-03-05 19:55:48 -06:00
parent 01af6f6855
commit 505931736c
18 changed files with 276 additions and 31 deletions

View File

@ -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();

View File

@ -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');

View File

@ -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

View File

@ -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,
]);
}
/**

View File

@ -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;

View File

@ -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',

View File

@ -0,0 +1,13 @@
<?php
namespace App\Models\Enums;
/**
* Class AcarsType
* @package App\Models\Enums
*/
class JournalType extends EnumBase
{
public const AIRLINE = 0;
public const USER = 1;
}

View File

@ -12,20 +12,26 @@ use Carbon\Carbon;
/**
* Class Journal
* @package Scottlaurent\Accounting
* @property Money $balance
* @property string $currency
* @property Carbon $updated_at
* @property Carbon $post_date
* @property Carbon $created_at
* @property Money $balance
* @property string $currency
* @property Carbon $updated_at
* @property Carbon $post_date
* @property Carbon $created_at
* @property \App\Models\Enums\JournalType type
*/
class Journal extends BaseModel
{
/**
* @var string
*/
protected $table = 'journals';
public $fillable = [
'ledger_id',
'journal_type',
'balance',
'currency',
'morphed_type',
'morphed_id',
];
/**
* @var array
*/

View File

@ -14,6 +14,7 @@ class SubfleetExpense extends BaseModel
'subfleet_id',
'name',
'amount',
'type',
];
/**
@ -23,6 +24,7 @@ class SubfleetExpense extends BaseModel
*/
protected $casts = [
'amount' => 'float',
'type' => 'integer',
];
public static $rules = [

View File

@ -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);

View File

@ -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',

View File

@ -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) {

View File

@ -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'
);
}

47
app/Support/Dates.php Normal file
View File

@ -0,0 +1,47 @@
<?php
/**
*
*/
namespace App\Support;
use Carbon\Carbon;
class Dates
{
/**
* Get the list of months, given a start date
* @param Carbon $start_date
* @return array
*/
public static function getMonthsList(Carbon $start_date)
{
$months = [];
$now = date('Y-m');
$last_month = $start_date;
do {
$last_value = $last_month->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"
];
}
}

View File

@ -0,0 +1,24 @@
@extends('admin.app')
@section('title', 'Financial Reports')
@section('actions')
<li><a href="{!! route('admin.finances.index') !!}"><i class="ti-menu-alt"></i>Overview</a></li>
@endsection
@section('content')
<div class="card border-blue-bottom">
<div class="content">
<div style="float:right;">
{!! Form::select(
'month_select',
$months_list,
$current_month,
['id' => 'month_select']
) !!}
</div>
@include('admin.finances.table')
</div>
</div>
@endsection
@include('admin.finances.scripts')

View File

@ -0,0 +1,12 @@
@section('scripts')
<script>
$(document).ready(() => {
const select_id = "select#month_select";
$(select_id).change((e) => {
const date = $(select_id + " option:selected").val();
const location = window.location.toString().split('?')[0];
window.location = location + '?month='+date;
});
});
</script>
@endsection

View File

@ -0,0 +1,61 @@
@foreach($transaction_groups as $group)
<h4>{!! $group['airline']->icao !!} - {!! $group['airline']->name !!}</h4>
<table class="table table-hover table-responsive" id="finances-table">
<thead>
<th>Expenses</th>
<th>Credit</th>
<th>Debit</th>
</thead>
<tbody>
@foreach($group['transactions'] as $ta)
<tr>
<td>
{!! $ta->transaction_group !!}
</td>
<td>
@if($ta->sum_credits)
{!! money($ta->sum_credits, $ta->currency) !!}
@else
-
@endif
</td>
<td>
@if($ta->sum_debits)
<i>{!! money($ta->sum_debits, $ta->currency) !!}</i>
@else
-
@endif
</td>
</tr>
@endforeach
{{-- show sums --}}
<tr>
<td></td>
<td>
{!! $group['credits'] !!}
</td>
<td>
({!! $group['debits'] !!})
</td>
</tr>
{{-- final total --}}
<tr style="border-top: 3px; border-top-style: double;">
<td></td>
<td><b>Total</b></td>
<td>
{!! $group['credits']->subtract($group['debits']) !!}
</td>
</tr>
</tbody>
</table>
@if(!$loop->last)
<hr>
@endif
@endforeach

View File

@ -30,7 +30,7 @@
</tr>
{{-- final total --}}
<tr>
<tr style="border-top: 3px; border-top-style: double;">
<td></td>
<td><b>Total</b></td>
<td>