From 498d8e4efca817191a66ad435bcf1efccaf86fe8 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 7 Apr 2021 13:35:16 +1000 Subject: [PATCH] Subscription Crons --- app/Jobs/Cron/SubscriptionCron.php | 21 +++++++++++++++------ app/Models/Invoice.php | 5 +++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/Jobs/Cron/SubscriptionCron.php b/app/Jobs/Cron/SubscriptionCron.php index 1fdd1812d8..00ed302a2f 100644 --- a/app/Jobs/Cron/SubscriptionCron.php +++ b/app/Jobs/Cron/SubscriptionCron.php @@ -12,6 +12,7 @@ namespace App\Jobs\Cron; use App\Libraries\MultiDB; +use App\Models\Invoice; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Support\Carbon; @@ -51,13 +52,21 @@ class SubscriptionCron private function loopSubscriptions() { - //looop recurring invoices with subscription id - // $client_subs = ClientSubscription::whereNull('deleted_at') - // ->cursor() - // ->each(function ($cs){ - // $this->processSubscription($cs); - // }); + $invoices = Invoice::where('is_deleted', 0) + ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) + ->where('balance', '>', 0) + ->whereDate('due_date', '<=', now()->startOfDay()) + ->whereNotNull('subscription_id') + ->cursor(); + + + $invoices->each(function ($invoice){ + + $subscription = $invoice->subscription; + + }); + } /* Our daily cron should check diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index ccfd120c35..d48e401d4a 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -181,6 +181,11 @@ class Invoice extends BaseModel return $this->belongsTo(Client::class)->withTrashed(); } + public function subscription() + { + return $this->belongsTo(Subscription::class)->withTrashed(); + } + public function documents() { return $this->morphMany(Document::class, 'documentable');