1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Services/Credit/SendEmail.php

62 lines
1.7 KiB
PHP
Raw Normal View History

2020-10-28 00:02:32 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @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-10-28 00:02:32 +01:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-10-28 00:02:32 +01:00
*/
namespace App\Services\Credit;
2023-10-26 04:57:44 +02:00
use App\Utils\Ninja;
2023-12-12 08:43:55 +01:00
use App\Models\Webhook;
use App\Models\ClientContact;
use App\Jobs\Entity\EmailEntity;
use App\Events\Credit\CreditWasEmailed;
2020-10-28 00:02:32 +01:00
class SendEmail
{
public $credit;
protected $reminder_template;
protected $contact;
public function __construct($credit, $reminder_template = null, ClientContact $contact = null)
{
$this->credit = $credit;
$this->reminder_template = $reminder_template;
$this->contact = $contact;
}
/**
* Builds the correct template to send.
*/
public function run()
{
if (! $this->reminder_template) {
$this->reminder_template = $this->credit->calculateTemplate('credit');
}
2023-09-07 04:07:46 +02:00
$this->credit->service()->markSent()->save();
2020-10-28 00:02:32 +01:00
$this->credit->invitations->each(function ($invitation) {
if (! $invitation->contact->trashed() && $invitation->contact->email) {
2022-11-27 09:58:30 +01:00
EmailEntity::dispatch($invitation, $invitation->company, $this->reminder_template)->delay(2);
2020-10-28 00:02:32 +01:00
}
});
2023-09-07 04:07:46 +02:00
if ($this->credit->invitations->count() >= 1) {
event(new CreditWasEmailed($this->credit->invitations->first(), $this->credit->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), 'credit'));
2023-12-12 08:43:55 +01:00
$this->credit->sendEvent(Webhook::EVENT_SENT_CREDIT, "client");
2023-09-07 04:07:46 +02:00
}
2020-10-28 00:02:32 +01:00
}
}