Calculate total amount spent for WeeklyReport mail
MODIFY
app/Jobs/SendWeeklyReports.php
app/Jobs/SendWeeklyReports.php
@@ -9,6 +9,7 @@ use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
class SendWeeklyReports implements ShouldQueue {
@@ -21,14 +22,20 @@ class SendWeeklyReports implements ShouldQueue {
public function handle() {
$spaces = Space::all();
$week = date('W');
+ $lastWeekDate = date('Y-m-d', strtotime('-7 days'));
+ $currentDate = date('Y-m-d');
foreach ($spaces as $space) {
- // TODO CALCULATE STATISTICS
+ $totalSpent = DB::select('SELECT SUM(amount) AS foo FROM spendings WHERE space_id = ? AND happened_on >= ? AND happened_on <= ?', [$space->id, $lastWeekDate, $currentDate])[0]->foo;
foreach ($space->users as $user) {
// Only send if user wants to receive report
if ($user->weekly_report) {
- Mail::to($user->email)->queue(new WeeklyReport($space, $week));
+ Mail::to($user->email)->queue(new WeeklyReport(
+ $space,
+ $week,
+ $totalSpent
+ ));
}
}
}

MODIFY
app/Mail/WeeklyReport.php
app/Mail/WeeklyReport.php
@@ -13,17 +13,20 @@ class WeeklyReport extends Mailable {
protected $space;
protected $week;
+ protected $totalSpent;
- public function __construct(Space $space, $week) {
+ public function __construct(Space $space, $week, $totalSpent) {
$this->space = $space;
$this->week = $week;
+ $this->totalSpent = $totalSpent;
}
public function build() {
return $this->view('emails.weekly_report')
->with([
'space' => $this->space,
- 'week' => $this->week
+ 'week' => $this->week,
+ 'totalSpent' => $this->totalSpent
]);
}
}

MODIFY
resources/views/emails/weekly_report.blade.php
resources/views/emails/weekly_report.blade.php
@@ -2,7 +2,7 @@ Here's your weekly report for {{ $space->name }}.
This week (#{{ $week }}) you've
<ul>
- <li>Spent CURRENCY SPENT_TOTAL_AMOUNT</li>
+ <li>Spent CURRENCY {{ number_format($totalSpent / 100, 2) }}</li>
<li>Most of which you've spent on SPENT_MOST_TAG_NAME (CURRENCY SPENT_MOST_TAG_AMOUNT)</li>
</ul>

Calculate total amount spent for WeeklyReport mail