1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Jobs/Cron/RecurringInvoicesCron.php

89 lines
3.0 KiB
PHP
Raw Normal View History

2019-05-23 07:08:31 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-23 07:08:31 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-23 07:08:31 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Cron;
use App\Jobs\RecurringInvoice\SendRecurring;
use App\Libraries\MultiDB;
2019-05-23 07:08:31 +02:00
use App\Models\RecurringInvoice;
2020-11-01 06:18:40 +01:00
use Illuminate\Database\Eloquent\Builder;
2020-09-26 01:48:42 +02:00
use Illuminate\Foundation\Bus\Dispatchable;
2019-05-23 07:08:31 +02:00
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
class RecurringInvoicesCron
{
2020-09-26 01:48:42 +02:00
use Dispatchable;
2019-05-23 07:08:31 +02:00
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle() : void
{
/* Get all invoices where the send date is less than NOW + 30 minutes() */
2020-09-09 12:05:10 +02:00
info("Sending recurring invoices ".Carbon::now()->format('Y-m-d h:i:s'));
if (! config('ninja.db.multi_db_enabled')) {
2020-09-08 12:34:14 +02:00
2020-09-28 04:56:11 +02:00
$recurring_invoices = RecurringInvoice::whereDate('next_send_date', '=', now())
->where('status_id', RecurringInvoice::STATUS_ACTIVE)
2020-11-01 11:29:34 +01:00
->with('company')
2020-09-28 04:56:11 +02:00
->cursor();
2020-09-26 01:48:42 +02:00
2020-09-28 04:56:11 +02:00
Log::info(now()->format('Y-m-d') . ' Sending Recurring Invoices. Count = '.$recurring_invoices->count());
2020-09-26 01:48:42 +02:00
$recurring_invoices->each(function ($recurring_invoice, $key) {
2019-05-23 07:08:31 +02:00
2020-09-26 01:48:42 +02:00
info("Current date = " . now()->format("Y-m-d") . " Recurring date = " .$recurring_invoice->next_send_date);
2019-05-23 07:08:31 +02:00
2020-11-01 11:29:34 +01:00
if(!$recurring_invoice->company->is_disabled)
SendRecurring::dispatchNow($recurring_invoice, $recurring_invoice->company->db);
2020-09-26 01:48:42 +02:00
2019-05-24 00:37:47 +02:00
});
2020-09-08 12:34:14 +02:00
} else {
//multiDB environment, need to
foreach (MultiDB::$dbs as $db) {
2020-09-26 01:48:42 +02:00
2019-05-24 00:37:47 +02:00
MultiDB::setDB($db);
2019-05-23 07:08:31 +02:00
2020-09-28 04:56:11 +02:00
$recurring_invoices = RecurringInvoice::whereDate('next_send_date', '=', now())
->where('status_id', RecurringInvoice::STATUS_ACTIVE)
2020-11-01 11:29:34 +01:00
->with('company')
2020-09-28 04:56:11 +02:00
->cursor();
2019-05-24 00:37:47 +02:00
2020-09-26 01:48:42 +02:00
Log::info(now()->format('Y-m-d') . ' Sending Recurring Invoices. Count = '.$recurring_invoices->count().' On Database # '.$db);
2019-05-24 00:37:47 +02:00
$recurring_invoices->each(function ($recurring_invoice, $key) {
2020-09-26 01:48:42 +02:00
info("Current date = " . now()->format("Y-m-d") . " Recurring date = " .$recurring_invoice->next_send_date);
2020-11-01 11:29:34 +01:00
if(!$recurring_invoice->company->is_disabled)
SendRecurring::dispatchNow($recurring_invoice, $recurring_invoice->company->db);
2020-09-26 01:48:42 +02:00
2019-05-24 00:37:47 +02:00
});
}
}
2019-05-23 07:08:31 +02:00
}
}