1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Jobs/Subscription/CleanStaleInvoiceOrder.php

93 lines
2.6 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Subscription;
use App\Libraries\MultiDB;
use App\Models\Invoice;
use App\Repositories\InvoiceRepository;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2023-05-04 01:42:42 +02:00
use Illuminate\Support\Facades\Auth;
class CleanStaleInvoiceOrder implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
*/
2023-02-16 02:36:09 +01:00
public function __construct()
{
}
/**
2023-02-16 02:36:09 +01:00
* @param InvoiceRepository $repo
* @return void
*/
public function handle(InvoiceRepository $repo) : void
{
2023-03-22 07:45:33 +01:00
nlog("Cleaning Stale Invoices:");
2023-05-04 01:42:42 +02:00
Auth::logout();
2023-02-16 02:36:09 +01:00
if (! config('ninja.db.multi_db_enabled')) {
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);
});
2023-03-23 06:51:45 +01:00
Invoice::query()
->withTrashed()
->where('status_id', Invoice::STATUS_SENT)
2023-07-09 09:50:30 +02:00
->whereBetween('created_at', [now()->subHours(1), now()->subMinutes(10)])
2023-03-23 06:51:45 +01:00
->where('balance', '>', 0)
->cursor()
->each(function ($invoice){
if (collect($invoice->line_items)->contains('type_id', 3)) {
$invoice->service()->removeUnpaidGatewayFees();
}
});
return;
2023-02-16 02:36:09 +01:00
}
2023-02-16 02:36:09 +01:00
foreach (MultiDB::$dbs as $db) {
MultiDB::setDB($db);
Invoice::query()
->withTrashed()
->where('is_proforma', 1)
2023-07-09 09:50:30 +02:00
->whereBetween('created_at', [now()->subHours(1), now()->subMinutes(10)])
->cursor()
->each(function ($invoice) use ($repo) {
$invoice->is_proforma = false;
$repo->delete($invoice);
});
}
}
public function failed($exception = null)
{
}
}