1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Jobs/Cron/AutoBillCron.php

140 lines
5.9 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
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
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() */
2021-08-05 02:46:03 +02:00
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)
->where('balance', '>', 0)
2021-09-02 01:28:27 +02:00
->where('is_deleted', false)
->whereHas('company', function ($query) {
$query->where('is_disabled',0);
})
2021-10-04 16:11:43 +02:00
->orderBy('id', 'DESC')
2021-09-02 08:17:46 +02:00
->with('company');
nlog($auto_bill_partial_invoices->count(). " partial invoices to auto bill");
$auto_bill_partial_invoices->cursor()->each(function ($invoice){
2021-10-03 12:51:42 +02:00
AutoBill::dispatch($invoice, false);
2021-05-26 11:51:04 +02:00
});
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)
->where('balance', '>', 0)
2021-09-02 01:28:27 +02:00
->where('is_deleted', false)
->whereHas('company', function ($query) {
$query->where('is_disabled',0);
})
2021-10-04 16:11:43 +02:00
->orderBy('id', 'DESC')
2021-09-02 08:17:46 +02:00
->with('company');
nlog($auto_bill_invoices->count(). " full invoices to auto bill");
2021-10-03 12:51:42 +02:00
$auto_bill_invoices->cursor()->each(function ($invoice){
AutoBill::dispatch($invoice, false);
2021-05-26 11:51:04 +02:00
});
2021-05-26 08:04:38 +02:00
} else {
//multiDB environment, need to
foreach (MultiDB::$dbs as $db) {
2021-09-02 01:26:09 +02:00
2021-05-26 08:04:38 +02:00
MultiDB::setDB($db);
2021-09-02 08:17:46 +02:00
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)
->where('balance', '>', 0)
2021-09-02 01:28:27 +02:00
->where('is_deleted', false)
->whereHas('company', function ($query) {
$query->where('is_disabled',0);
})
2021-10-04 16:11:43 +02:00
->orderBy('id', 'DESC')
2021-09-02 08:17:46 +02:00
->with('company');
nlog($auto_bill_partial_invoices->count(). " partial invoices to auto bill db = {$db}");
2021-09-03 11:30:44 +02:00
$auto_bill_partial_invoices->cursor()->each(function ($invoice) use($db){
2021-10-03 12:51:42 +02:00
AutoBill::dispatch($invoice, $db);
2021-05-26 11:51:04 +02:00
});
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)
->where('balance', '>', 0)
2021-09-02 01:28:27 +02:00
->where('is_deleted', false)
->whereHas('company', function ($query) {
$query->where('is_disabled',0);
})
2021-10-04 16:11:43 +02:00
->orderBy('id', 'DESC')
2021-09-02 08:17:46 +02:00
->with('company');
nlog($auto_bill_invoices->count(). " full invoices to auto bill db = {$db}");
2021-10-03 12:51:42 +02:00
$auto_bill_invoices->cursor()->each(function ($invoice) use($db){
2021-10-04 23:17:41 +02:00
nlog($this->counter);
2021-10-03 12:51:42 +02:00
AutoBill::dispatch($invoice, $db);
2021-10-04 23:17:41 +02:00
$this->counter++;
2021-05-26 11:51:04 +02:00
});
2021-05-26 08:04:38 +02:00
2021-10-04 15:48:43 +02:00
2021-05-26 08:04:38 +02:00
}
2021-10-04 15:48:43 +02:00
2021-10-04 23:17:41 +02:00
nlog("Auto Bill - fine");
2021-05-26 08:04:38 +02:00
}
}
}