Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

range-of-motion/budget

1057 stars
2 code files
View range-of-motion/budget on GitHub

app/Mail/WeeklyReport.php

Open in GitHub
use 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 GitHub
use 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
));
}
}
}
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.