diff --git a/app/Jobs/Cron/CompanyRecurringCron.php b/app/Jobs/Cron/CompanyRecurringCron.php new file mode 100644 index 0000000000..5e0a1d765c --- /dev/null +++ b/app/Jobs/Cron/CompanyRecurringCron.php @@ -0,0 +1,70 @@ +whereHas('recurring_invoices', function ($query){ + $query->where('next_send_date', '<=', now()->toDateTimeString()) + ->whereNotNull('next_send_date') + ->whereNull('deleted_at') + ->where('is_deleted', false) + ->where('status_id', RecurringInvoice::STATUS_ACTIVE) + ->where('remaining_cycles', '!=', '0') + ->whereHas('client', function ($query) { + $query->where('is_deleted', 0) + ->where('deleted_at', null); + }); + }) + ->cursor()->each(function ($company){ + + SendCompanyRecurring::dispatch($company->id, $company->db); + + }); + } + } +} diff --git a/app/Jobs/Cron/SendCompanyRecurring.php b/app/Jobs/Cron/SendCompanyRecurring.php new file mode 100644 index 0000000000..fc2fe63a01 --- /dev/null +++ b/app/Jobs/Cron/SendCompanyRecurring.php @@ -0,0 +1,91 @@ +company_id = $company_id; + + $this->db = $db; + + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() : void + { + + MultiDB::setDB($this->db); + + $recurring_invoices = Company::where('id', $this->company_id) + ->where('is_disabled', 0) + ->whereHas('recurring_invoices', function ($query){ + $query->where('next_send_date', '<=', now()->toDateTimeString()) + ->whereNotNull('next_send_date') + ->whereNull('deleted_at') + ->where('is_deleted', false) + ->where('status_id', RecurringInvoice::STATUS_ACTIVE) + ->where('remaining_cycles', '!=', '0') + ->whereHas('client', function ($query) { + $query->where('is_deleted', 0) + ->where('deleted_at', null); + }); + }) + ->cursor()->each(function ($recurring_invoice){ + + nlog("Trying to send {$recurring_invoice->number}"); + + if ($recurring_invoice->company->stop_on_unpaid_recurring) { + if ($recurring_invoice->invoices()->whereIn('status_id', [2, 3])->where('is_deleted', 0)->where('balance', '>', 0)->exists()) { + return; + } + } + + try { + (new SendRecurring($recurring_invoice, $recurring_invoice->company->db))->handle(); + } catch (\Exception $e) { + nlog("Unable to sending recurring invoice {$recurring_invoice->id} ".$e->getMessage()); + } + + }); + + } +}