Skip to content

Commit

Permalink
Merge pull request #459 from range-of-motion/show-chart-of-balance-pe…
Browse files Browse the repository at this point in the history
…r-day-on-dashboard

Show chart of balance per day on dashboard
  • Loading branch information
range-of-motion authored Dec 9, 2023
2 parents ba98076 + 3d05e49 commit 8ee2da3
Show file tree
Hide file tree
Showing 8 changed files with 566 additions and 1,911 deletions.
28 changes: 28 additions & 0 deletions app/Http/Controllers/Api/DashboardController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Repositories\DashboardRepository;
use Carbon\CarbonImmutable;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
public function __invoke(DashboardRepository $dashboardRepository, Request $request)
{
/** @var ApiKey $apiKey */
$apiKey = $request->get('apiKey');

$space = $apiKey->user->spaces()->first();

return response()
->json([
'daily_balance' => $dashboardRepository->getDailyBalance(
spaceId: $space->id,
year: CarbonImmutable::now()->year,
month: CarbonImmutable::now()->month,
),
]);
}
}
6 changes: 5 additions & 1 deletion app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public function __invoke(Request $request)
'mostExpensiveTags' => $mostExpensiveTags,

'daysInMonth' => $daysInMonth,
'dailyBalance' => $this->dashboardRepository->getDailyBalance($currentYear, $currentMonth)
'dailyBalance' => $this->dashboardRepository->getDailyBalance(
session('space_id'),
$currentYear,
$currentMonth,
)
]);
}
}
6 changes: 3 additions & 3 deletions app/Repositories/DashboardRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ public function getTotalAmountSpent(string $year, string $month)
->sum('amount');
}

public function getDailyBalance(string $year, string $month): array
public function getDailyBalance(int $spaceId, string $year, string $month): array
{
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);

$balanceTick = 0;
$dailyBalance = [];

for ($i = 1; $i <= $daysInMonth; $i++) {
$balanceTick -= Spending::ofSpace(session('space_id'))
$balanceTick -= Spending::ofSpace($spaceId)
->where('happened_on', $year . '-' . $month . '-' . $i)
->sum('amount');

$balanceTick += Earning::ofSpace(session('space_id'))
$balanceTick += Earning::ofSpace($spaceId)
->where('happened_on', $year . '-' . $month . '-' . $i)
->sum('amount');

Expand Down
Loading

0 comments on commit 8ee2da3

Please sign in to comment.