forked from nabeelio/phpvms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nabeelio#11 from arthurpar06/filamentFinance
Finance Page (wip)
- Loading branch information
Showing
7 changed files
with
444 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.