-
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.
feat: projected cash flow dashboard widget (#16)
* explicit declare widgets * carbon namespace fix * recurring cash flow widget added * test added * url change * test name fix * dead property cleaned * test fix * currency symbol added
- Loading branch information
1 parent
77808b8
commit 02222ed
Showing
7 changed files
with
137 additions
and
1 deletion.
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
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
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,22 @@ | ||
<?php | ||
|
||
namespace App\Filament\Pages; | ||
|
||
use App\Filament\Widgets; | ||
use Filament\Pages\Dashboard as BaseDashboard; | ||
|
||
class RecurringCashFlowOverviewDashboard extends BaseDashboard | ||
{ | ||
protected static ?string $navigationGroup = 'Recurring Transactions'; | ||
|
||
protected static string $routePath = 'recurring-overview'; | ||
|
||
protected static ?string $title = 'Recurring CashFlow Overview'; | ||
|
||
public function getWidgets(): array | ||
{ | ||
return [ | ||
Widgets\RecurringCashFlowOverviewWidget::class, | ||
]; | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace App\Filament\Widgets; | ||
|
||
use App\Services\RecurringCashFlowService; | ||
use Filament\Widgets\StatsOverviewWidget as BaseWidget; | ||
use Filament\Widgets\StatsOverviewWidget\Stat; | ||
|
||
class RecurringCashFlowOverviewWidget extends BaseWidget | ||
{ | ||
protected function getStats(): array | ||
{ | ||
$startDate = today(); | ||
$endDate = today()->addYear(); | ||
|
||
$totalIncomes = RecurringCashFlowService::processRecurringIncomes( | ||
auth()->user(), | ||
$startDate, | ||
$endDate | ||
); | ||
|
||
$totalExpenses = RecurringCashFlowService::processRecurringExpenses( | ||
auth()->user(), | ||
$startDate, | ||
$endDate | ||
); | ||
|
||
$disposableIncome = $totalIncomes - $totalExpenses; | ||
|
||
$symbol = config('penny.currency_symbol'); | ||
|
||
return [ | ||
Stat::make('Total Estimated Incomes', $symbol.' '.number_format($totalIncomes)), | ||
Stat::make('Total Estimated Expenses', $symbol.' '.number_format($totalExpenses)), | ||
Stat::make('Estimated Disposable Income', $symbol.' '.number_format($disposableIncome)), | ||
]; | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\RecurringExpense; | ||
use App\Models\RecurringIncome; | ||
use App\Models\User; | ||
use Carbon\Carbon; | ||
|
||
class RecurringCashFlowService | ||
{ | ||
public static function processRecurringIncomes(User $user, Carbon $startDate, Carbon $endDate): float | ||
{ | ||
return RecurringIncome::query() | ||
->where('user_id', $user->id) | ||
->whereDate('next_transaction_at', '>=', $startDate) | ||
->whereDate('next_transaction_at', '<=', $endDate) | ||
->get() | ||
->reduce(function (?float $carry, RecurringIncome $recurringIncome) use ($endDate) { | ||
$count = $recurringIncome->frequency->getRemainingIterations( | ||
$recurringIncome->next_transaction_at, | ||
$endDate, | ||
$recurringIncome->remaining_recurrences | ||
); | ||
|
||
return $carry + $count * $recurringIncome->amount; | ||
}); | ||
} | ||
|
||
public static function processRecurringExpenses(User $user, Carbon $startDate, Carbon $endDate): float | ||
{ | ||
return RecurringExpense::query() | ||
->where('user_id', $user->id) | ||
->whereDate('next_transaction_at', '>=', $startDate) | ||
->whereDate('next_transaction_at', '<=', $endDate) | ||
->get() | ||
->reduce(function (?float $carry, RecurringExpense $recurringExpense) use ($endDate) { | ||
$count = $recurringExpense->frequency->getRemainingIterations( | ||
$recurringExpense->next_transaction_at, | ||
$endDate, | ||
$recurringExpense->remaining_recurrences | ||
); | ||
|
||
return $carry + $count * $recurringExpense->amount; | ||
}); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,4 +2,6 @@ | |
|
||
return [ | ||
'currency' => env('CURRENCY', 'INR'), | ||
|
||
'currency_symbol' => env('CURRENCY_SYMBOL', '₹'), | ||
]; |
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,18 @@ | ||
<?php | ||
|
||
use App\Filament\Widgets\RecurringCashFlowOverviewWidget; | ||
use App\Models\User; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
|
||
uses(RefreshDatabase::class); | ||
|
||
beforeEach(function () { | ||
$this->user = User::factory()->create(); | ||
$this->actingAs($this->user); | ||
}); | ||
|
||
it('sees recurring cash flow overview widget', function () { | ||
$this->get('/app/recurring-overview') | ||
->assertOk() | ||
->assertSeeLivewire(RecurringCashFlowOverviewWidget::class); | ||
}); |