Move stale invoice cleanup to scheduler

This commit is contained in:
David Bomba 2022-12-20 22:32:13 +11:00
parent c3945a1bac
commit 3bb04b4edf
3 changed files with 35 additions and 8 deletions

View File

@ -23,6 +23,7 @@ use App\Jobs\Ninja\QueueSize;
use App\Jobs\Ninja\SystemMaintenance;
use App\Jobs\Ninja\TaskScheduler;
use App\Jobs\Quote\QuoteCheckExpired;
use App\Jobs\Subscription\CleanStaleInvoiceOrder;
use App\Jobs\Util\DiskCleanup;
use App\Jobs\Util\ReminderJob;
use App\Jobs\Util\SchedulerCheck;
@ -68,6 +69,9 @@ class Kernel extends ConsoleKernel
/* Sends recurring invoices*/
$schedule->job(new RecurringInvoicesCron)->hourly()->withoutOverlapping()->name('recurring-invoice-job')->onOneServer();
/* Stale Invoice Cleanup*/
$schedule->job(new CleanStaleInvoiceOrder)->hourly()->withoutOverlapping()->name('stale-invoice-job')->onOneServer();
/* Sends recurring invoices*/
$schedule->job(new RecurringExpensesCron)->dailyAt('00:10')->withoutOverlapping()->name('recurring-expense-job')->onOneServer();

View File

@ -534,8 +534,6 @@ class BillingPortalPurchasev2 extends Component
->adjustInventory()
->save();
CleanStaleInvoiceOrder::dispatch($this->invoice->id, $this->company->db)->delay(600);
Cache::put($this->hash, [
'subscription_id' => $this->subscription->id,
'email' => $this->email ?? $this->contact->email,

View File

@ -30,7 +30,7 @@ class CleanStaleInvoiceOrder implements ShouldQueue
* @param int invoice_id
* @param string $db
*/
public function __construct(private int $invoice_id, private string $db){}
public function __construct(){}
/**
* @param InvoiceRepository $repo
@ -38,13 +38,38 @@ class CleanStaleInvoiceOrder implements ShouldQueue
*/
public function handle(InvoiceRepository $repo) : void
{
MultiDB::setDb($this->db);
$invoice = Invoice::withTrashed()->find($this->invoice_id);
if (! config('ninja.db.multi_db_enabled')) {
if($invoice->is_proforma){
$invoice->is_proforma = false;
$repo->delete($invoice);
Invoice::query()
->withTrashed()
->where('is_proforma', 1)
->where('created_at', '<', now()->subHour())
->cursor()
->each(function ($invoice) use ($repo) {
$invoice->is_proforma = false;
$repo->delete($invoice);
});
return;
}
foreach (MultiDB::$dbs as $db)
{
MultiDB::setDB($db);
Invoice::query()
->withTrashed()
->where('is_proforma', 1)
->where('created_at', '<', now()->subHour())
->cursor()
->each(function ($invoice) use ($repo) {
$invoice->is_proforma = false;
$repo->delete($invoice);
});
}
}