2020-02-15 10:01:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\Quote;
|
|
|
|
|
|
|
|
use App\Helpers\Email\QuoteEmail;
|
|
|
|
use App\Jobs\Quote\EmailQuote;
|
|
|
|
use App\Models\Quote;
|
|
|
|
|
|
|
|
class SendEmail
|
|
|
|
{
|
|
|
|
public $quote;
|
|
|
|
|
|
|
|
public function __construct($quote)
|
|
|
|
{
|
|
|
|
$this->quote = $quote;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the correct template to send
|
|
|
|
* @param string $reminder_template The template name ie reminder1
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function run($reminder_template = null, $contact = null): array
|
|
|
|
{
|
|
|
|
if (!$reminder_template) {
|
|
|
|
$reminder_template = $this->quote->status_id == Quote::STATUS_DRAFT || Carbon::parse($this->quote->due_date) > now() ? 'invoice' : $this->quote->calculateTemplate();
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->quote->invitations->each(function ($invitation) {
|
|
|
|
if ($invitation->contact->send && $invitation->contact->email) {
|
2020-02-17 10:37:44 +01:00
|
|
|
$email_builder = (new QuoteEmail())->build($invitation, $reminder_template);
|
2020-02-15 10:01:15 +01:00
|
|
|
|
|
|
|
EmailQuote::dispatchNow($email_builder, $invitation);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|