-
app/Mail/WeeklyReport.php
Open in GitHubuse App\Models\Space; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class WeeklyReport extends Mailable { use Queueable; use SerializesModels; protected $space; protected $week; protected $totalSpent; protected $largestSpendingWithTag; public function __construct(Space $space, $week, $totalSpent, $largestSpendingWithTag) { $this->space = $space; $this->week = $week; $this->totalSpent = $totalSpent; $this->largestSpendingWithTag = $largestSpendingWithTag; } public function build() { return $this ->view('emails.weekly_report') ->text('emails.weekly_report_plain') ->with([ 'space' => $this->space, 'week' => $this->week, 'totalSpent' => $this->totalSpent, 'largestSpendingWithTag' => $this->largestSpendingWithTag ]); } }
-
app/Jobs/SendWeeklyReports.php
Open in GitHubuse App\Helper; use App\Mail\WeeklyReport; use App\Models\Space; use Illuminate\Contracts\Queue\ShouldQueue; 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) { $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; $largestSpendingWithTag = DB::select(' SELECT spendings.amount AS amount, tags.name AS tag_name FROM spendings INNER JOIN tags ON spendings.tag_id = tags.id WHERE spendings.space_id = ? AND spendings.happened_on >= ? AND spendings.happened_on <= ? AND spendings.tag_id IS NOT NULL ORDER BY spendings.amount DESC LIMIT 1', [$space->id, $lastWeekDate, $currentDate]); foreach ($space->users as $user) { if (Helper::arePlansEnabled() && $user->plan === 'standard') { continue; } if ($user->weekly_report) { Mail::to($user->email)->queue(new WeeklyReport( $space, $week, $totalSpent, $largestSpendingWithTag )); } } } } }