Skip to content

Commit

Permalink
Merge pull request nabeelio#11 from arthurpar06/filamentFinance
Browse files Browse the repository at this point in the history
Finance Page (wip)
  • Loading branch information
arthurpar06 authored Nov 1, 2023
2 parents b7ad659 + c037995 commit c0d2cb7
Show file tree
Hide file tree
Showing 7 changed files with 444 additions and 152 deletions.
61 changes: 61 additions & 0 deletions app/Filament/Pages/Finances.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Filament\Pages;

use App\Repositories\AirlineRepository;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Pages\Page;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Url;

class Finances extends Page
{
protected static ?string $navigationGroup = 'Operations';
protected static ?int $navigationSort = 5;

protected static ?string $navigationLabel = 'Finances';

protected static ?string $navigationIcon = 'heroicon-o-chart-bar';

protected static string $view = 'filament.pages.finances';

#[Url]
public ?array $filters = [];

public function mount(): void
{
$this->filters = [
'start_date' => $this->filters['start_date'] ?? now()->subYear(),
'end_date' => $this->filters['end_date'] ?? now(),
'airline_id' => $this->filters['airline_id'] ?? Auth::user()->airline_id,
];
$this->fillForm();
}

protected function fillForm(): void
{
$this->callHook('beforeFill');

$this->form->fill($this->filters);

$this->callHook('afterFill');
}

public function form(Form $form): Form
{
return $form->statePath('filters')->schema([
Forms\Components\DatePicker::make('start_date')->native(false)->maxDate(fn (Get $get) => $get('end_date'))->live()->afterStateUpdated(function () { $this->filtersUpdated(); }),
Forms\Components\DatePicker::make('end_date')->native(false)->minDate(fn (Get $get) => $get('start_date'))->maxDate(now())->live()->afterStateUpdated(function () { $this->filtersUpdated(); }),
Forms\Components\Select::make('airline_id')->label('Airline')->options(app(AirlineRepository::class)->selectBoxList())->live()->afterStateUpdated(function (?string $state) { if (!$state || $state == '') {
$this->filters['airline_id'] = Auth::user()->airline_id;
} $this->filtersUpdated(); }),
])->columns(3);
}

public function filtersUpdated()
{
$this->dispatch('updateFinanceFilters', start_date: $this->filters['start_date'] ?? now()->subYear(), end_date: $this->filters['end_date'], airline_id: $this->filters['airline_id']);
}
}
80 changes: 80 additions & 0 deletions app/Filament/Widgets/AirlineFinanceChart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Filament\Widgets;

use App\Models\Airline;
use App\Models\JournalTransaction;
use Filament\Support\Colors\Color;
use Filament\Widgets\ChartWidget;
use Flowframe\Trend\Trend;
use Flowframe\Trend\TrendValue;
use Illuminate\Support\Carbon;
use Livewire\Attributes\On;

class AirlineFinanceChart extends ChartWidget
{
protected static ?string $heading = 'Finance';
protected static ?string $pollingInterval = null;

public int $airline_id;
public string $start_date;
public string $end_date;

#[On('updateFinanceFilters')]
public function refresh(int $airline_id, string $start_date, string $end_date): void
{
$this->airline_id = $airline_id;
$this->start_date = Carbon::createFromTimeString($start_date);
$this->end_date = Carbon::createFromTimeString($end_date);
$this->updateChartData();
}

protected function getData(): array
{
$airline = Airline::find($this->airline_id);

$debit = Trend::query(JournalTransaction::where(['journal_id' => $airline->journal->id]))
->between(
start: Carbon::createFromTimeString($this->start_date),
end: Carbon::createFromTimeString($this->end_date)
)
->perMonth()
->sum('debit');

$credit = Trend::query(JournalTransaction::where(['journal_id' => $airline->journal->id]))
->between(
start: Carbon::createFromTimeString($this->start_date),
end: Carbon::createFromTimeString($this->end_date)
)
->perMonth()
->sum('credit');

return [
'datasets' => [
[
'label' => 'Debit',
'data' => $debit->map(fn (TrendValue $value) => money($value->aggregate, setting('units.currency'))->getValue()),
'backgroundColor' => 'rgba('.Color::Red[400].', 0.1)',
'borderColor' => 'rgb('.Color::Red[400].')',
],
[
'label' => 'Credit',
'data' => $credit->map(fn (TrendValue $value) => money($value->aggregate, setting('units.currency'))->getValue()),
'backgroundColor' => 'rgba('.Color::Green[400].', 0.1)',
'borderColor' => 'rgb('.Color::Green[400].')',
],
],
'labels' => $debit->map(fn (TrendValue $value) => $value->date),
];
}

protected function getType(): string
{
return 'bar';
}

public static function canView(): bool
{
return false;
}
}
73 changes: 73 additions & 0 deletions app/Filament/Widgets/AirlineFinanceTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Filament\Widgets;

use App\Models\Airline;
use App\Models\JournalTransaction;
use Filament\Widgets\Widget;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Carbon;
use Livewire\Attributes\On;

class AirlineFinanceTable extends Widget
{
protected static ?string $pollingInterval = null;
protected static string $view = 'filament.widgets.airline_finance_table';

public int $airline_id;
public int $airline_journal_id;
public string $start_date;
public string $end_date;

public Collection $transactions;
public int $sum_all_credits;
public int $sum_all_debits;

public function mount(): void
{
$this->airline_journal_id = Airline::find($this->airline_id)->journal->id;
$this->updateTransactions();
}

#[On('updateFinanceFilters')]
public function refresh(int $airline_id, string $start_date, string $end_date): void
{
if ($this->airline_id != $airline_id) {
$this->airline_id = $airline_id;
$this->airline_journal_id = Airline::find($airline_id)->journal->id;
}

$this->start_date = Carbon::createFromTimeString($start_date);
$this->end_date = Carbon::createFromTimeString($end_date);

$this->updateTransactions();
}

public function updateTransactions(): void
{
$this->transactions = JournalTransaction::groupBy('transaction_group', 'currency')
->selectRaw('transaction_group,
currency,
SUM(credit) as sum_credits,
SUM(debit) as sum_debits')
->where(['journal_id' => $this->airline_journal_id])
->whereBetween('created_at', [$this->start_date, $this->end_date], '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
$this->sum_all_credits = 0;
$this->sum_all_debits = 0;
foreach ($this->transactions as $ta) {
$this->sum_all_credits += $ta->sum_credits ?? 0;
$this->sum_all_debits += $ta->sum_debits ?? 0;
}
}

public static function canView(): bool
{
return false;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"spatie/laravel-ignition": "^2.0",
"kyslik/column-sortable": "^6.5",
"jlorente/laravel-data-migrations": "^2.0",
"filament/filament": "^3.0.69",
"filament/filament": "^3.0.88",
"coolsam/modules": "3.x-dev",
"flowframe/laravel-trend": "^0.1.5"
},
Expand Down
Loading

0 comments on commit c0d2cb7

Please sign in to comment.