2019-05-23 07:08:31 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-23 07:08:31 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-23 07:08:31 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-23 07:08:31 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Cron;
|
|
|
|
|
|
|
|
use App\Jobs\RecurringInvoice\SendRecurring;
|
2020-02-24 11:15:30 +01:00
|
|
|
use App\Libraries\MultiDB;
|
2019-05-23 07:08:31 +02:00
|
|
|
use App\Models\RecurringInvoice;
|
2020-09-26 01:48:42 +02:00
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
2019-05-23 07:08:31 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
|
|
|
class RecurringInvoicesCron
|
|
|
|
{
|
2020-11-25 15:19:52 +01:00
|
|
|
use Dispatchable;
|
2020-09-26 01:48:42 +02:00
|
|
|
|
2021-04-30 04:51:38 +02:00
|
|
|
public $tries = 1;
|
|
|
|
|
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-12-29 22:10:03 +01:00
|
|
|
nlog("Sending recurring invoices ".Carbon::now()->format('Y-m-d h:i:s'));
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2021-06-22 10:46:46 +02:00
|
|
|
$recurring_invoices = RecurringInvoice::where('next_send_date', '<=', now()->toDateTimeString())
|
2021-03-30 00:30:41 +02:00
|
|
|
->whereNotNull('next_send_date')
|
2021-06-25 02:14:45 +02:00
|
|
|
->whereNull('deleted_at')
|
2021-09-02 04:08:16 +02:00
|
|
|
->where('is_deleted', false)
|
2020-09-28 04:56:11 +02:00
|
|
|
->where('status_id', RecurringInvoice::STATUS_ACTIVE)
|
2021-01-10 22:06:32 +01:00
|
|
|
->where('remaining_cycles', '!=', '0')
|
2021-07-02 11:40:44 +02:00
|
|
|
->whereHas('client', function ($query) {
|
|
|
|
$query->where('is_deleted',0)
|
|
|
|
->where('deleted_at', NULL);
|
|
|
|
})
|
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-12-29 22:10:03 +01:00
|
|
|
nlog(now()->format('Y-m-d') . ' Sending Recurring Invoices. Count = '.$recurring_invoices->count());
|
2019-05-23 07:08:31 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
$recurring_invoices->each(function ($recurring_invoice, $key) {
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog("Current date = " . now()->format("Y-m-d") . " Recurring date = " .$recurring_invoice->next_send_date);
|
2019-05-23 07:08:31 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if (!$recurring_invoice->company->is_disabled) {
|
2021-09-02 04:08:16 +02:00
|
|
|
|
|
|
|
try{
|
|
|
|
SendRecurring::dispatchNow($recurring_invoice, $recurring_invoice->company->db);
|
|
|
|
}
|
|
|
|
catch(\Exception $e){
|
|
|
|
nlog("Unable to sending recurring invoice {$recurring_invoice->id}");
|
|
|
|
}
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2019-05-24 00:37:47 +02:00
|
|
|
});
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
|
|
|
//multiDB environment, need to
|
|
|
|
foreach (MultiDB::$dbs as $db) {
|
2019-05-24 00:37:47 +02:00
|
|
|
MultiDB::setDB($db);
|
2019-05-23 07:08:31 +02:00
|
|
|
|
2021-06-22 10:46:46 +02:00
|
|
|
$recurring_invoices = RecurringInvoice::where('next_send_date', '<=', now()->toDateTimeString())
|
2021-03-30 00:30:41 +02:00
|
|
|
->whereNotNull('next_send_date')
|
2021-06-25 02:14:45 +02:00
|
|
|
->whereNull('deleted_at')
|
2021-09-02 04:08:16 +02:00
|
|
|
->where('is_deleted', false)
|
2020-09-28 04:56:11 +02:00
|
|
|
->where('status_id', RecurringInvoice::STATUS_ACTIVE)
|
2021-01-10 22:06:32 +01:00
|
|
|
->where('remaining_cycles', '!=', '0')
|
2021-07-02 11:40:44 +02:00
|
|
|
->whereHas('client', function ($query) {
|
|
|
|
$query->where('is_deleted',0)
|
|
|
|
->where('deleted_at', NULL);
|
|
|
|
})
|
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
|
|
|
|
2021-09-03 04:25:41 +02:00
|
|
|
nlog(now()->format('Y-m-d') . ' Sending Recurring Invoices. Count = '.$recurring_invoices->count());
|
2019-05-24 00:37:47 +02:00
|
|
|
|
|
|
|
$recurring_invoices->each(function ($recurring_invoice, $key) {
|
2021-06-26 02:32:38 +02:00
|
|
|
nlog("Current date = " . now()->format("Y-m-d") . " Recurring date = " .$recurring_invoice->next_send_date ." Recurring #id = ". $recurring_invoice->id);
|
2020-09-26 01:48:42 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if (!$recurring_invoice->company->is_disabled) {
|
2021-09-02 04:08:16 +02:00
|
|
|
|
|
|
|
try{
|
|
|
|
SendRecurring::dispatchNow($recurring_invoice, $recurring_invoice->company->db);
|
|
|
|
}
|
|
|
|
catch(\Exception $e){
|
2021-09-03 04:25:41 +02:00
|
|
|
nlog("Unable to sending recurring invoice {$recurring_invoice->id}");
|
2021-09-02 04:08:16 +02:00
|
|
|
}
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2019-05-24 00:37:47 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-05-23 07:08:31 +02:00
|
|
|
}
|
|
|
|
}
|