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)
|
2021-09-02 01:28:27 +02:00
|
|
|
->where('is_deleted', false)
|
2021-09-02 08:17:46 +02:00
|
|
|
->with('company');
|
|
|
|
|
|
|
|
nlog($auto_bill_partial_invoices->count(). " partial invoices to auto bill");
|
|
|
|
|
2021-09-03 11:30:44 +02:00
|
|
|
$auto_bill_partial_invoices->cursor()->each(function ($invoice) use($db){
|
|
|
|
$this->runAutoBiller($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)
|
2021-09-02 08:17:46 +02:00
|
|
|
->with('company');
|
|
|
|
|
|
|
|
nlog($auto_bill_invoices->count(). " full invoices to auto bill");
|
|
|
|
|
2021-09-03 11:30:44 +02:00
|
|
|
$auto_bill_invoices->cursor()->each(function ($invoice) use($db){
|
|
|
|
$this->runAutoBiller($invoice, $db);
|
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)
|
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-09-03 11:28:04 +02:00
|
|
|
$this->runAutoBiller($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)
|
2021-09-02 08:17:46 +02:00
|
|
|
->with('company');
|
|
|
|
|
|
|
|
nlog($auto_bill_invoices->count(). " full invoices to auto bill db = {$db}");
|
|
|
|
|
2021-09-03 11:28:04 +02:00
|
|
|
$auto_bill_invoices->cursor()->each(function ($invoice) use($db){
|
|
|
|
$this->runAutoBiller($invoice, $db);
|
2021-05-26 11:51:04 +02:00
|
|
|
});
|
2021-05-26 08:04:38 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 11:30:44 +02:00
|
|
|
private function runAutoBiller(Invoice $invoice, $db)
|
2021-05-26 08:04:38 +02:00
|
|
|
{
|
2021-08-05 02:46:03 +02:00
|
|
|
info("Firing autobill for {$invoice->company_id} - {$invoice->number}");
|
2021-09-02 01:26:09 +02:00
|
|
|
|
|
|
|
try{
|
2021-09-03 11:28:04 +02:00
|
|
|
MultiDB::setDB($db);
|
2021-09-02 01:26:09 +02:00
|
|
|
$invoice->service()->autoBill()->save();
|
|
|
|
}
|
|
|
|
catch(\Exception $e) {
|
|
|
|
nlog("Failed to capture payment for {$invoice->company_id} - {$invoice->number} ->" . $e->getMessage());
|
|
|
|
}
|
2021-05-26 08:04:38 +02:00
|
|
|
}
|
|
|
|
}
|