2020-02-15 10:01:15 +01:00
|
|
|
<?php
|
2020-07-01 03:06:40 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
|
|
|
* @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)
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-07-01 03:06:40 +02:00
|
|
|
*/
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
namespace App\Services\Payment;
|
|
|
|
|
2023-03-18 08:24:56 +01:00
|
|
|
use App\Jobs\Payment\EmailPayment;
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Payment;
|
2024-01-29 04:03:54 +01:00
|
|
|
use Illuminate\Database\QueryException;
|
2020-02-15 10:01:15 +01:00
|
|
|
|
|
|
|
class SendEmail
|
|
|
|
{
|
2024-01-29 04:03:54 +01:00
|
|
|
public function __construct(public Payment $payment, public ?ClientContact $contact) {}
|
2020-02-15 10:01:15 +01:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Builds the correct template to send.
|
2020-02-15 10:01:15 +01:00
|
|
|
*/
|
2020-05-09 00:35:49 +02:00
|
|
|
public function run()
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
2024-01-29 04:03:54 +01:00
|
|
|
$this->payment->load('company', 'invoices');
|
2021-10-08 07:23:00 +02:00
|
|
|
|
2023-03-18 08:24:56 +01:00
|
|
|
if (!$this->contact) {
|
2024-01-29 04:03:54 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-18 08:24:56 +01:00
|
|
|
}
|
2022-08-04 08:22:48 +02:00
|
|
|
|
2023-03-18 08:24:56 +01:00
|
|
|
EmailPayment::dispatch($this->payment, $this->payment->company, $this->contact);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
}
|