Change cards on dashboard to show balance, recurrings and left to spend for that month
MODIFY
app/Http/Controllers/DashboardController.php
app/Http/Controllers/DashboardController.php
@@ -4,6 +4,9 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
+use App\Earning;
+use App\Recurring;
+use App\Spending;
use Auth;
use DB;
@@ -11,27 +14,9 @@ class DashboardController extends Controller {
public function __invoke() {
$space_id = session('space')->id;
- $totalEarnings = session('space')
- ->earnings()
- ->whereRaw('MONTH(happened_on) = ?', [date('m')])
- ->sum('amount');
-
- $totalSpendings = session('space')
- ->spendings()
- ->whereRaw('MONTH(happened_on) = ?', [date('m')])
- ->sum('amount');
-
- $recentEarnings = session('space')
- ->earnings()
- ->orderBy('happened_on', 'DESC')
- ->limit(3)
- ->get();
-
- $recentSpendings = session('space')
- ->spendings()
- ->orderBy('happened_on', 'DESC')
- ->limit(3)
- ->get();
+ $balance = session('space')->monthlyBalance(2018, 11);
+ $recurrings = session('space')->monthlyRecurrings(2018, 11);
+ $leftToSpend = $balance - $recurrings;
$mostExpensiveTags = DB::select('
SELECT
@@ -54,13 +39,13 @@ class DashboardController extends Controller {
', [$space_id, date('m')]);
return view('dashboard', [
- 'recentEarnings' => $recentEarnings,
- 'recentSpendings' => $recentSpendings,
-
'month' => date('n'),
- 'totalEarnings' => $totalEarnings,
- 'totalSpendings' => $totalSpendings,
- 'mostExpensiveTags' => $mostExpensiveTags,
+
+ 'balance' => $balance,
+ 'recurrings' => $recurrings,
+ 'leftToSpend' => $leftToSpend,
+
+ 'mostExpensiveTags' => $mostExpensiveTags
]);
}
}

MODIFY
app/Space.php
app/Space.php
@@ -4,6 +4,7 @@ namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
+use Illuminate\Support\Facades\DB;
class Space extends Model {
use SoftDeletes;
@@ -34,4 +35,31 @@ class Space extends Model {
public function imports() {
return $this->hasMany(Import::class);
}
+
+ //
+ public function monthlyBalance($year, $month) {
+ $query = DB::selectOne('
+ SELECT (
+ SELECT SUM(amount)
+ FROM earnings
+ WHERE space_id = :e_space_id AND YEAR(happened_on) = :e_year AND MONTH(happened_on) = :e_month
+ ) - (
+ SELECT SUM(amount)
+ FROM spendings
+ WHERE space_id = :s_space_id AND YEAR(happened_on) = :s_year AND MONTH(happened_on) = :s_month
+ ) AS balance;
+ ', ['e_space_id' => $this->id, 'e_year' => $year, 'e_month' => $month, 's_space_id' => $this->id, 's_year' => $year, 's_month' => $month]);
+
+ return $query->balance;
+ }
+
+ public function monthlyRecurrings($year, $month) {
+ $query = DB::selectOne('
+ SELECT SUM(amount) as amount
+ FROM recurrings
+ WHERE space_id = :space_id AND YEAR(starts_on) >= :start_year AND MONTH(starts_on) >= :start_month AND ((YEAR(ends_on) <= :end_year AND MONTH(ends_on) <= :end_month) OR ends_on IS NULL)
+ ', ['space_id' => $this->id, 'start_year' => $year, 'start_month' => $month, 'end_year' => $year, 'end_month' => $month]);
+
+ return $query->amount;
+ }
}

MODIFY
resources/views/dashboard.blade.php
resources/views/dashboard.blade.php
<p class="mt-1">{{ __('calendar.months.' . $month) }} {{ date('Y') }}</p>
<div class="row row--gutter row--responsive my-3">
<div class="row__column">
- <div class="card card--green">
- <h2 style="font-size: 20px;">{!! $currency !!} {{ number_format($totalEarnings / 100, 2) }}</h2>
- <div class="mt-1" style="color: #A7AEBB;">{{ __('general.total_earned') }}</div>
+ <div class="card card--blue">
+ <h2 style="font-size: 20px;">{!! $currency !!} {{ number_format($balance / 100, 2) }}</h2>
+ <div class="mt-1" style="color: #A7AEBB;">{{ __('general.balance') }}</div>
</div>
</div>
<div class="row__column">
<div class="card card--red">
- <h2 style="font-size: 20px;">{!! $currency !!} {{ number_format($totalSpendings / 100, 2) }}</h2>
- <div class="mt-1" style="color: #A7AEBB;">{{ __('general.total_spent') }}</div>
+ <h2 style="font-size: 20px;">{!! $currency !!} {{ number_format($recurrings / 100, 2) }}</h2>
+ <div class="mt-1" style="color: #A7AEBB;">{{ __('general.recurrings') }}</div>
</div>
</div>
<div class="row__column">
- <!-- OFFSET -->
+ <div class="card card--green">
+ <h2 style="font-size: 20px;">{!! $currency !!} {{ number_format($leftToSpend / 100, 2) }}</h2>
+ <div class="mt-1" style="color: #A7AEBB;">{{ __('general.left_to_spend') }}</div>
+ </div>
</div>
</div>
<div class="box mb-3">

Change cards on dashboard to show balance, recurrings and left to spend for that month