1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Jobs/Cron/AutoBillCron.php

156 lines
5.8 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
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. 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;
2021-10-03 12:51:42 +02:00
use App\Jobs\Cron\AutoBill;
2021-05-26 08:04:38 +02:00
use App\Jobs\RecurringInvoice\SendRecurring;
use App\Libraries\MultiDB;
use App\Models\Invoice;
use App\Models\RecurringInvoice;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Carbon;
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
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) {
2022-11-28 10:49:32 +01:00
foreach($invoices as $invoice)
{
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) {
2022-11-28 10:49:32 +01:00
foreach($invoices as $invoice)
{
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
2022-11-30 22:02:51 +01:00
$auto_bill_partial_invoices->chunk(400, function ($invoices) use($db){
2022-11-28 10:49:32 +01:00
foreach($invoices as $invoice)
{
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
2022-11-30 22:02:51 +01:00
$auto_bill_invoices->chunk(400, function ($invoices) use($db){
2022-11-28 10:49:32 +01:00
foreach($invoices as $invoice)
{
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
}
}
}