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
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. 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;
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Jobs\RecurringInvoice\SendRecurring;
|
2023-05-04 01:42:42 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Models\Invoice;
|
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;
|
2023-10-26 04:57:44 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2019-05-23 07:08:31 +02:00
|
|
|
|
|
|
|
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;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-23 07:08:31 +02:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function handle(): void
|
2019-05-23 07:08:31 +02:00
|
|
|
{
|
|
|
|
/* Get all invoices where the send date is less than NOW + 30 minutes() */
|
2022-08-08 23:26:43 +02:00
|
|
|
$start = Carbon::now()->format('Y-m-d h:i:s');
|
|
|
|
nlog('Sending recurring invoices '.$start);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-05-04 01:42:42 +02:00
|
|
|
Auth::logout();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2024-08-04 12:21:30 +02:00
|
|
|
$recurring_invoices = RecurringInvoice::query()->where('status_id', RecurringInvoice::STATUS_ACTIVE)
|
|
|
|
->where('is_deleted', false)
|
|
|
|
->where('remaining_cycles', '!=', '0')
|
|
|
|
->whereNotNull('next_send_date')
|
|
|
|
->whereNull('deleted_at')
|
|
|
|
->where('next_send_date', '<=', now()->toDateTimeString())
|
2024-07-05 04:04:42 +02:00
|
|
|
->whereHas('client', function ($query) {
|
|
|
|
$query->where('is_deleted', 0)
|
|
|
|
->where('deleted_at', null);
|
2021-07-02 11:40:44 +02:00
|
|
|
})
|
2024-07-05 04:04:42 +02:00
|
|
|
->whereHas('company', function ($query) {
|
|
|
|
$query->where('is_disabled', 0);
|
2021-10-02 04:49:18 +02:00
|
|
|
})
|
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
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog(now()->format('Y-m-d').' Sending Recurring Invoices. Count = '.$recurring_invoices->count());
|
2019-05-23 07:08:31 +02:00
|
|
|
|
2024-02-15 22:36:38 +01:00
|
|
|
$recurring_invoices->each(function ($recurring_invoice, $key) {
|
2022-08-01 02:39:47 +02:00
|
|
|
// nlog('Current date = '.now()->format('Y-m-d').' Recurring date = '.$recurring_invoice->next_send_date);
|
2021-10-02 04:49:18 +02:00
|
|
|
nlog("Trying to send {$recurring_invoice->number}");
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-05-17 09:36:28 +02:00
|
|
|
/* Special check if we should generate another invoice is the previous one is yet to be paid */
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($recurring_invoice->company->stop_on_unpaid_recurring && $recurring_invoice->invoices()->whereIn('status_id', [2, 3])->where('is_deleted', 0)->where('balance', '>', 0)->exists()) {
|
|
|
|
nlog('Existing invoice exists, skipping');
|
2022-05-17 09:36:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
try {
|
2022-07-31 23:30:04 +02:00
|
|
|
(new SendRecurring($recurring_invoice, $recurring_invoice->company->db))->handle();
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
nlog("Unable to sending recurring invoice {$recurring_invoice->id} ".$e->getMessage());
|
2021-10-02 04:49:18 +02: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
|
|
|
|
2024-08-04 12:21:30 +02:00
|
|
|
$recurring_invoices = RecurringInvoice::query()->where('status_id', RecurringInvoice::STATUS_ACTIVE)
|
|
|
|
->where('is_deleted', false)
|
|
|
|
->where('remaining_cycles', '!=', '0')
|
|
|
|
->whereNull('deleted_at')
|
|
|
|
->whereNotNull('next_send_date')
|
|
|
|
->where('next_send_date', '<=', now()->toDateTimeString())
|
|
|
|
->whereHas('client', function ($query) {
|
|
|
|
$query->where('is_deleted', 0)
|
|
|
|
->where('deleted_at', null);
|
2021-07-02 11:40:44 +02:00
|
|
|
})
|
2024-08-04 12:21:30 +02:00
|
|
|
->whereHas('company', function ($query) {
|
|
|
|
$query->where('is_disabled', 0);
|
2021-10-02 04:49:18 +02:00
|
|
|
})
|
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
|
|
|
|
2022-06-21 11:57:17 +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) {
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog("Trying to send {$recurring_invoice->number}");
|
2022-05-17 09:36:28 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($recurring_invoice->company->stop_on_unpaid_recurring) {
|
2022-09-08 03:30:40 +02:00
|
|
|
if (Invoice::where('recurring_id', $recurring_invoice->id)->whereIn('status_id', [2, 3])->where('is_deleted', 0)->where('balance', '>', 0)->exists()) {
|
2022-06-21 11:57:17 +02:00
|
|
|
return;
|
2022-05-17 09:36:28 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-05-17 09:36:28 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
try {
|
2022-07-31 23:30:04 +02:00
|
|
|
(new SendRecurring($recurring_invoice, $recurring_invoice->company->db))->handle();
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
nlog("Unable to sending recurring invoice {$recurring_invoice->id} ".$e->getMessage());
|
|
|
|
}
|
2019-05-24 00:37:47 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-08-08 23:26:43 +02:00
|
|
|
|
|
|
|
nlog("Recurring invoice send duration " . $start . " - " . Carbon::now()->format('Y-m-d h:i:s'));
|
2019-05-23 07:08:31 +02:00
|
|
|
}
|
|
|
|
}
|