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

102 lines
3.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
*
* @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;
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;
/**
* 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)
->with('company')
2021-05-26 11:51:04 +02:00
->cursor()->each(function ($invoice){
$this->runAutoBiller($invoice);
});
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)
->with('company')
2021-05-26 11:51:04 +02:00
->cursor()->each(function ($invoice){
$this->runAutoBiller($invoice);
});
2021-05-26 08:04:38 +02:00
} else {
//multiDB environment, need to
foreach (MultiDB::$dbs as $db) {
MultiDB::setDB($db);
$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)
->with('company')
2021-05-26 11:51:04 +02:00
->cursor()->each(function ($invoice){
$this->runAutoBiller($invoice);
});
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)
->with('company')
2021-05-26 11:51:04 +02:00
->cursor()->each(function ($invoice){
$this->runAutoBiller($invoice);
});
2021-05-26 08:04:38 +02:00
}
}
}
private function runAutoBiller(Invoice $invoice)
{
2021-08-05 02:46:03 +02:00
info("Firing autobill for {$invoice->company_id} - {$invoice->number}");
2021-05-26 08:04:38 +02:00
$invoice->service()->autoBill()->save();
}
}