Add ProcessRecurrings job and schedule it for execution every minute
MODIFY
app/Console/Kernel.php
app/Console/Kernel.php
namespace App\Console;
+use App\Jobs\ProcessRecurrings;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@@ -26,6 +27,8 @@ class Kernel extends ConsoleKernel
{
// $schedule->command('inspire')
// ->hourly();
+
+ $schedule->job(new ProcessRecurrings)->everyMinute();
}
/**

ADD
app/Jobs/ProcessRecurrings.php
+<?php
+
+namespace App\Jobs;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use App\Recurring;
+use App\Spending;
+
+class ProcessRecurrings implements ShouldQueue {
+ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+ public function __construct() {
+ //
+ }
+
+ public function handle() {
+ $recurrings = Recurring
+ ::where('type', 'monthly')
+ ->where('day', date('j'))
+ ->where('starts_on', '<=', date('Y-m-d'))
+ ->where('ends_on', '>=', date('Y-m-d'))
+ ->where(function ($query) {
+ $query
+ ->where('last_used_on', date('j'))
+ ->orWhere('last_used_on', null);
+ })->get();
+
+ foreach ($recurrings as $recurring) {
+ $spending = new Spending;
+ $spending->user_id = $recurring->user_id;
+ $spending->recurring_id = $recurring->id;
+ $spending->tag_id = $recurring->tag_id;
+ $spending->happened_on = date('Y-m-d');
+ $spending->description = $recurring->description;
+ $spending->amount = $recurring->amount;
+ $spending->save();
+
+ $recurring->last_used_on = date('Y-m-d');
+ $recurring->save();
+ }
+ }
+}

Add ProcessRecurrings job and schedule it for execution every minute