Adjustments for logic for sending payment emails

This commit is contained in:
David Bomba 2024-01-29 14:03:54 +11:00
parent c8cf4aca42
commit 82888cccff
2 changed files with 22 additions and 6 deletions

View File

@ -68,7 +68,7 @@ class EmailPayment implements ShouldQueue
$this->payment->load('invoices'); $this->payment->load('invoices');
if (!$this->contact) { if (!$this->contact) {
$this->contact = $this->payment->client->contacts()->first(); $this->contact = $this->payment->client->contacts()->orderBy('is_primary', 'desc')->first();
} }
$this->contact->load('client'); $this->contact->load('client');

View File

@ -14,22 +14,38 @@ namespace App\Services\Payment;
use App\Jobs\Payment\EmailPayment; use App\Jobs\Payment\EmailPayment;
use App\Models\ClientContact; use App\Models\ClientContact;
use App\Models\Payment; use App\Models\Payment;
use Illuminate\Database\QueryException;
class SendEmail class SendEmail
{ {
public function __construct(public Payment $payment, public ?ClientContact $contact) public function __construct(public Payment $payment, public ?ClientContact $contact) {}
{
}
/** /**
* Builds the correct template to send. * Builds the correct template to send.
*/ */
public function run() public function run()
{ {
$this->payment->load('company', 'client.contacts', 'invoices'); $this->payment->load('company', 'invoices');
if (!$this->contact) { if (!$this->contact) {
$this->contact = $this->payment->client->contacts()->first();
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); EmailPayment::dispatch($this->payment, $this->payment->company, $this->contact);