1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/Services/Payment/SendEmail.php
2024-02-13 15:25:18 +11:00

57 lines
1.5 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Payment;
use App\Jobs\Payment\EmailPayment;
use App\Models\ClientContact;
use App\Models\Payment;
use Illuminate\Database\QueryException;
class SendEmail
{
public function __construct(public Payment $payment, public ?ClientContact $contact)
{
}
/**
* Builds the correct template to send.
*/
public function run()
{
$this->payment->load('company', 'invoices');
if (!$this->contact) {
if($invoice = $this->payment->invoices->first() ?? false) {
$invitation =
$invoice
->invitations()
->whereHas("contact", function ($q) {
$q->where('send_email', true)->orderBy("is_primary", 'ASC');
})
->first();
if($invitation) {
$this->contact = $invitation->contact;
} else {
$this->contact = $this->payment->client->contacts()->orderBy('send_email', 'desc')->orderBy('is_primary', 'desc')->first();
}
}
}
EmailPayment::dispatch($this->payment, $this->payment->company, $this->contact);
}
}