2022-12-20 11:46:20 +01:00
|
|
|
<?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)
|
2022-12-20 11:46:20 +01:00
|
|
|
*
|
|
|
|
* @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;
|
2022-12-20 11:46:20 +01:00
|
|
|
|
|
|
|
class CleanStaleInvoiceOrder implements ShouldQueue
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
use Dispatchable;
|
|
|
|
use InteractsWithQueue;
|
|
|
|
use Queueable;
|
|
|
|
use SerializesModels;
|
2022-12-20 11:46:20 +01:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
*/
|
2023-02-16 02:36:09 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
2022-12-20 11:46:20 +01:00
|
|
|
|
|
|
|
/**
|
2023-02-16 02:36:09 +01:00
|
|
|
* @param InvoiceRepository $repo
|
|
|
|
* @return void
|
2022-12-20 11:46:20 +01:00
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function handle(InvoiceRepository $repo): void
|
2022-12-20 11:46:20 +01:00
|
|
|
{
|
2023-03-22 07:45:33 +01:00
|
|
|
nlog("Cleaning Stale Invoices:");
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-05-04 01:42:42 +02:00
|
|
|
Auth::logout();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2022-12-20 12:32:13 +01:00
|
|
|
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)
|
2024-01-27 05:43:37 +01:00
|
|
|
->whereBetween('created_at', [now()->subHours(1), now()->subMinutes(30)])
|
2023-03-23 06:51:45 +01:00
|
|
|
->where('balance', '>', 0)
|
|
|
|
->cursor()
|
2023-10-26 04:57:44 +02:00
|
|
|
->each(function ($invoice) {
|
2023-03-23 06:51:45 +01:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if (collect($invoice->line_items)->contains('type_id', 3)) {
|
|
|
|
$invoice->service()->removeUnpaidGatewayFees();
|
|
|
|
}
|
2023-03-23 06:51:45 +01:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2022-12-20 12:32:13 +01:00
|
|
|
return;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-12-20 12:32:13 +01:00
|
|
|
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
foreach (MultiDB::$dbs as $db) {
|
2022-12-20 12:32:13 +01:00
|
|
|
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)])
|
2022-12-20 12:32:13 +01:00
|
|
|
->cursor()
|
|
|
|
->each(function ($invoice) use ($repo) {
|
|
|
|
$invoice->is_proforma = false;
|
|
|
|
$repo->delete($invoice);
|
|
|
|
});
|
2022-12-20 11:46:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function failed($exception = null)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|