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

103 lines
3.6 KiB
PHP
Raw Normal View History

2023-01-06 16:32:40 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2023-01-06 16:32:40 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
2023-01-06 16:38:08 +01:00
namespace App\Jobs\Invoice;
2023-01-06 16:32:40 +01:00
use App\Jobs\Util\WebhookHandler;
use App\Libraries\MultiDB;
use App\Models\Invoice;
use App\Models\Webhook;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class InvoiceCheckLateWebhook implements ShouldQueue
{
2024-01-14 05:05:00 +01:00
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
2023-01-06 16:32:40 +01:00
/**
* Create a new job instance.
*/
2023-02-16 02:36:09 +01:00
public function __construct()
{
}
2023-01-06 16:32:40 +01:00
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
nlog("sending overdue webhooks for invoices");
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
if (! config('ninja.db.multi_db_enabled')) {
2023-01-06 16:32:40 +01:00
$company_ids = Webhook::where('event_id', Webhook::EVENT_LATE_INVOICE)
->where('is_deleted', 0)
->pluck('company_id');
Invoice::query()
->where('is_deleted', false)
->whereNull('deleted_at')
->whereNotNull('due_date')
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('balance', '>', 0)
->whereIn('company_id', $company_ids)
->whereHas('client', function ($query) {
2023-02-16 02:36:09 +01:00
$query->where('is_deleted', 0)
->where('deleted_at', null);
})
->whereHas('company', function ($query) {
2023-01-06 16:32:40 +01:00
$query->where('is_disabled', 0);
})
->whereBetween('due_date', [now()->subDay()->startOfDay(), now()->startOfDay()->subSecond()])
->cursor()
2023-02-16 02:36:09 +01:00
->each(function ($invoice) {
2023-11-10 02:06:49 +01:00
(new WebhookHandler(Webhook::EVENT_LATE_INVOICE, $invoice, $invoice->company, 'client'))->handle();
2023-01-06 16:32:40 +01:00
});
2023-02-16 02:36:09 +01:00
} else {
foreach (MultiDB::$dbs as $db) {
2023-01-06 16:32:40 +01:00
MultiDB::setDB($db);
$company_ids = Webhook::where('event_id', Webhook::EVENT_LATE_INVOICE)
->where('is_deleted', 0)
->pluck('company_id');
Invoice::query()
->where('is_deleted', false)
->whereNull('deleted_at')
->whereNotNull('due_date')
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('balance', '>', 0)
->whereIn('company_id', $company_ids)
->whereHas('client', function ($query) {
2023-02-16 02:36:09 +01:00
$query->where('is_deleted', 0)
->where('deleted_at', null);
})
->whereHas('company', function ($query) {
2023-01-06 16:32:40 +01:00
$query->where('is_disabled', 0);
})
->whereBetween('due_date', [now()->subDay()->startOfDay(), now()->startOfDay()->subSecond()])
->cursor()
2023-02-16 02:36:09 +01:00
->each(function ($invoice) {
2023-11-10 02:06:49 +01:00
(new WebhookHandler(Webhook::EVENT_LATE_INVOICE, $invoice, $invoice->company, 'client'))->handle();
2023-01-06 16:32:40 +01:00
});
}
}
}
}