1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Jobs/Cron/SendCompanyRecurring.php

75 lines
3.0 KiB
PHP
Raw Normal View History

2022-08-01 03:28:19 +02: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-08-01 03:28:19 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Cron;
use App\Jobs\RecurringInvoice\SendRecurring;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\RecurringInvoice;
use Illuminate\Foundation\Bus\Dispatchable;
/*@not used*/
class SendCompanyRecurring
{
use Dispatchable;
public $tries = 1;
2023-08-07 07:30:34 +02:00
/** @var \App\Models\Company $company */
2022-08-01 03:28:19 +02:00
public $company;
2023-08-07 07:30:34 +02:00
public function __construct(private int $company_id, private string $db)
2022-08-01 03:28:19 +02:00
{
}
/**
* Execute the job.
*
* @return void
*/
2024-01-14 05:05:00 +01:00
public function handle(): void
2022-08-01 03:28:19 +02:00
{
MultiDB::setDB($this->db);
$recurring_invoices = Company::where('id', $this->company_id)
->where('is_disabled', 0)
2023-02-16 02:36:09 +01:00
->whereHas('recurring_invoices', function ($query) {
$query->where('next_send_date', '<=', now()->toDateTimeString())
->whereNotNull('next_send_date')
->whereNull('deleted_at')
->where('is_deleted', false)
->where('status_id', RecurringInvoice::STATUS_ACTIVE)
->where('remaining_cycles', '!=', '0')
->whereHas('client', function ($query) {
$query->where('is_deleted', 0)
->where('deleted_at', null);
});
})
->cursor()->each(function ($recurring_invoice) {
nlog("Trying to send {$recurring_invoice->number}");
if ($recurring_invoice->company->stop_on_unpaid_recurring) {
if ($recurring_invoice->invoices()->whereIn('status_id', [2, 3])->where('is_deleted', 0)->where('balance', '>', 0)->exists()) {
return;
}
}
try {
(new SendRecurring($recurring_invoice, $recurring_invoice->company->db))->handle();
} catch (\Exception $e) {
nlog("Unable to sending recurring invoice {$recurring_invoice->id} ".$e->getMessage());
}
});
2022-08-01 03:28:19 +02:00
}
}