1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Jobs/Cron/AutoBill.php

91 lines
2.4 KiB
PHP
Raw Normal View History

2021-10-03 12:51:42 +02: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)
2021-10-03 12:51:42 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Cron;
2023-10-26 04:57:44 +02:00
use App\Models\Invoice;
2023-12-12 08:43:55 +01:00
use App\Models\Webhook;
use App\Libraries\MultiDB;
2022-08-07 08:47:37 +02:00
use Illuminate\Bus\Queueable;
2023-12-12 08:43:55 +01:00
use App\Jobs\Entity\EmailEntity;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
2022-08-07 08:47:37 +02:00
use Illuminate\Contracts\Queue\ShouldQueue;
2023-02-16 02:36:09 +01:00
use Illuminate\Foundation\Bus\Dispatchable;
2021-10-03 12:51:42 +02:00
2022-08-07 08:47:37 +02:00
class AutoBill implements ShouldQueue
2021-10-03 12:51:42 +02:00
{
2024-01-14 05:05:00 +01:00
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
2021-10-03 12:51:42 +02:00
public $tries = 1;
2021-10-03 12:51:42 +02:00
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(public int $invoice_id, public ?string $db, public bool $send_email_on_failure = false)
2021-10-03 12:51:42 +02:00
{
}
/**
* Execute the job.
*
* @return void
*/
2024-01-14 05:05:00 +01:00
public function handle(): void
2021-10-03 12:51:42 +02:00
{
set_time_limit(0);
if ($this->db) {
2021-10-03 12:51:42 +02:00
MultiDB::setDb($this->db);
}
2021-10-03 12:51:42 +02:00
2023-04-26 23:13:45 +02:00
$invoice = false;
2024-01-14 05:05:00 +01:00
try {
nlog("autobill {$this->invoice_id}");
2024-01-14 05:05:00 +01:00
$invoice = Invoice::withTrashed()->find($this->invoice_id);
2021-10-03 12:51:42 +02:00
$invoice->service()->autoBill();
2023-04-26 23:13:45 +02:00
} catch (\Exception $e) {
nlog("Failed to capture payment for {$this->invoice_id} ->".$e->getMessage());
2023-10-26 04:57:44 +02:00
if($this->send_email_on_failure && $invoice) {
$invoice->invitations->each(function ($invitation) use ($invoice) {
if ($invitation->contact && ! $invitation->contact->trashed() && strlen($invitation->contact->email) >= 1 && $invoice->client->getSetting('auto_email_invoice')) {
try {
EmailEntity::dispatch($invitation, $invoice->company)->delay(rand(1, 2));
$invoice->entityEmailEvent($invitation, 'invoice', 'email_template_invoice');
2024-01-14 05:05:00 +01:00
} catch (\Exception $e) {
nlog($e->getMessage());
}
nlog("Firing email for invoice {$invoice->number}");
}
});
2023-12-12 08:43:55 +01:00
$invoice->sendEvent(Webhook::EVENT_SENT_INVOICE, "client");
}
2021-10-03 12:51:42 +02:00
}
}
}