1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/app/Jobs/Cron/AutoBillCron.php

145 lines
5.6 KiB
PHP
Raw Normal View History

2021-05-26 08:04:38 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-05-26 08:04:38 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2021-05-26 08:04:38 +02:00
*/
namespace App\Jobs\Cron;
use App\Models\Invoice;
2023-05-04 01:42:42 +02:00
use App\Libraries\MultiDB;
2021-05-26 08:04:38 +02:00
use Illuminate\Support\Carbon;
2023-05-04 01:42:42 +02:00
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Bus\Dispatchable;
2021-05-26 08:04:38 +02:00
class AutoBillCron
{
use Dispatchable;
public $tries = 1;
2021-10-04 23:17:41 +02:00
private $counter = 1;
2021-05-26 08:04:38 +02:00
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle() : void
{
set_time_limit(0);
/* Get all invoices where the send date is less than NOW + 30 minutes() */
info('Performing Autobilling '.Carbon::now()->format('Y-m-d h:i:s'));
2021-05-26 08:04:38 +02:00
2023-05-04 01:42:42 +02:00
Auth::logout();
2021-05-26 08:04:38 +02:00
if (! config('ninja.db.multi_db_enabled')) {
$auto_bill_partial_invoices = Invoice::whereDate('partial_due_date', '<=', now())
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('auto_bill_enabled', true)
2022-05-17 06:50:10 +02:00
->where('auto_bill_tries', '<', 3)
2021-05-26 08:04:38 +02:00
->where('balance', '>', 0)
2021-09-02 01:28:27 +02:00
->where('is_deleted', false)
->whereHas('company', function ($query) {
$query->where('is_disabled', 0);
})
->orderBy('id', 'DESC');
2021-09-02 08:17:46 +02:00
nlog($auto_bill_partial_invoices->count().' partial invoices to auto bill');
2021-09-02 08:17:46 +02:00
2022-11-30 22:02:51 +01:00
$auto_bill_partial_invoices->chunk(400, function ($invoices) {
2023-02-16 02:36:09 +01:00
foreach ($invoices as $invoice) {
2022-11-28 10:49:32 +01:00
AutoBill::dispatch($invoice->id, false);
}
sleep(2);
});
2021-05-26 08:04:38 +02:00
$auto_bill_invoices = Invoice::whereDate('due_date', '<=', now())
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('auto_bill_enabled', true)
2022-05-17 06:50:10 +02:00
->where('auto_bill_tries', '<', 3)
2021-05-26 08:04:38 +02:00
->where('balance', '>', 0)
2021-09-02 01:28:27 +02:00
->where('is_deleted', false)
->whereHas('company', function ($query) {
$query->where('is_disabled', 0);
})
->orderBy('id', 'DESC');
2021-09-02 08:17:46 +02:00
nlog($auto_bill_invoices->count().' full invoices to auto bill');
2021-05-26 08:04:38 +02:00
2022-11-30 22:02:51 +01:00
$auto_bill_invoices->chunk(400, function ($invoices) {
2023-02-16 02:36:09 +01:00
foreach ($invoices as $invoice) {
2022-11-28 10:49:32 +01:00
AutoBill::dispatch($invoice->id, false);
}
sleep(2);
});
2021-05-26 08:04:38 +02:00
} else {
//multiDB environment, need to
foreach (MultiDB::$dbs as $db) {
MultiDB::setDB($db);
2021-05-26 08:04:38 +02:00
$auto_bill_partial_invoices = Invoice::whereDate('partial_due_date', '<=', now())
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('auto_bill_enabled', true)
2022-05-17 06:50:10 +02:00
->where('auto_bill_tries', '<', 3)
2021-05-26 08:04:38 +02:00
->where('balance', '>', 0)
2021-09-02 01:28:27 +02:00
->where('is_deleted', false)
->whereHas('company', function ($query) {
$query->where('is_disabled', 0);
})
->orderBy('id', 'DESC');
2021-09-02 08:17:46 +02:00
nlog($auto_bill_partial_invoices->count()." partial invoices to auto bill db = {$db}");
2021-09-02 08:17:46 +02:00
2023-02-16 02:36:09 +01:00
$auto_bill_partial_invoices->chunk(400, function ($invoices) use ($db) {
foreach ($invoices as $invoice) {
2022-11-28 10:49:32 +01:00
AutoBill::dispatch($invoice->id, $db);
}
sleep(2);
});
2021-05-26 08:04:38 +02:00
$auto_bill_invoices = Invoice::whereDate('due_date', '<=', now())
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('auto_bill_enabled', true)
2022-05-17 06:50:10 +02:00
->where('auto_bill_tries', '<', 3)
2021-05-26 08:04:38 +02:00
->where('balance', '>', 0)
2021-09-02 01:28:27 +02:00
->where('is_deleted', false)
->whereHas('company', function ($query) {
$query->where('is_disabled', 0);
})
->orderBy('id', 'DESC');
2021-09-02 08:17:46 +02:00
nlog($auto_bill_invoices->count()." full invoices to auto bill db = {$db}");
2021-10-04 15:48:43 +02:00
2023-02-16 02:36:09 +01:00
$auto_bill_invoices->chunk(400, function ($invoices) use ($db) {
foreach ($invoices as $invoice) {
2022-11-28 10:49:32 +01:00
AutoBill::dispatch($invoice->id, $db);
}
sleep(2);
});
2021-05-26 08:04:38 +02:00
}
2021-10-04 15:48:43 +02:00
nlog('Auto Bill - fine');
2021-05-26 08:04:38 +02:00
}
}
}