Merge pull request #6761 from turbo124/v5-develop

Auto Bill Job
This commit is contained in:
David Bomba 2021-10-03 21:52:55 +11:00 committed by GitHub
commit 665730e29e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 28 deletions

View File

@ -0,0 +1,63 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Cron;
use App\Libraries\MultiDB;
use App\Models\Invoice;
use Illuminate\Foundation\Bus\Dispatchable;
class AutoBill
{
use Dispatchable;
public $tries = 1;
public Invoice $invoice;
public string $db;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Invoice $invoice, ?string $db)
{
$this->invoice = $invoice;
$this->db = $db;
}
/**
* Execute the job.
*
* @return void
*/
public function handle() : void
{
set_time_limit(0);
if($this->db)
MultiDB::setDb($this->db);
try{
$this->invoice->service()->autoBill()->save();
}
catch(\Exception $e) {
nlog("Failed to capture payment for {$this->invoice->company_id} - {$this->invoice->number} ->" . $e->getMessage());
}
}
}

View File

@ -11,6 +11,7 @@
namespace App\Jobs\Cron; namespace App\Jobs\Cron;
use App\Jobs\Cron\AutoBill;
use App\Jobs\RecurringInvoice\SendRecurring; use App\Jobs\RecurringInvoice\SendRecurring;
use App\Libraries\MultiDB; use App\Libraries\MultiDB;
use App\Models\Invoice; use App\Models\Invoice;
@ -60,7 +61,7 @@ class AutoBillCron
nlog($auto_bill_partial_invoices->count(). " partial invoices to auto bill"); nlog($auto_bill_partial_invoices->count(). " partial invoices to auto bill");
$auto_bill_partial_invoices->cursor()->each(function ($invoice){ $auto_bill_partial_invoices->cursor()->each(function ($invoice){
$this->runAutoBiller($invoice, false); AutoBill::dispatch($invoice, false);
}); });
$auto_bill_invoices = Invoice::whereDate('due_date', '<=', now()) $auto_bill_invoices = Invoice::whereDate('due_date', '<=', now())
@ -75,11 +76,9 @@ class AutoBillCron
nlog($auto_bill_invoices->count(). " full invoices to auto bill"); nlog($auto_bill_invoices->count(). " full invoices to auto bill");
$x = 1;
$auto_bill_invoices->cursor()->each(function ($invoice) use($x){ $auto_bill_invoices->cursor()->each(function ($invoice){
nlog("partial auto bill counter {$x}"); AutoBill::dispatch($invoice, false);
$this->runAutoBiller($invoice, false);
$x++;
}); });
@ -102,7 +101,7 @@ class AutoBillCron
nlog($auto_bill_partial_invoices->count(). " partial invoices to auto bill db = {$db}"); nlog($auto_bill_partial_invoices->count(). " partial invoices to auto bill db = {$db}");
$auto_bill_partial_invoices->cursor()->each(function ($invoice) use($db){ $auto_bill_partial_invoices->cursor()->each(function ($invoice) use($db){
$this->runAutoBiller($invoice, $db); AutoBill::dispatch($invoice, $db);
}); });
$auto_bill_invoices = Invoice::whereDate('due_date', '<=', now()) $auto_bill_invoices = Invoice::whereDate('due_date', '<=', now())
@ -117,31 +116,12 @@ class AutoBillCron
nlog($auto_bill_invoices->count(). " full invoices to auto bill db = {$db}"); nlog($auto_bill_invoices->count(). " full invoices to auto bill db = {$db}");
$x = 1; $auto_bill_invoices->cursor()->each(function ($invoice) use($db){
$auto_bill_invoices->cursor()->each(function ($invoice) use($db, $x){ AutoBill::dispatch($invoice, $db);
nlog("auto bill counter {$x}");
$this->runAutoBiller($invoice, $db);
$x++;
}); });
} }
} }
} }
private function runAutoBiller(Invoice $invoice, $db)
{
info("Firing autobill for {$invoice->company_id} - {$invoice->number}");
try{
if($db)
MultiDB::setDB($db);
$invoice->service()->autoBill()->save();
}
catch(\Exception $e) {
nlog("Failed to capture payment for {$invoice->company_id} - {$invoice->number} ->" . $e->getMessage());
}
}
} }