mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-12 14:12:44 +01:00
Refactor for emailing client statements
This commit is contained in:
parent
c11654a7d2
commit
0ba0937a18
@ -11,14 +11,15 @@
|
||||
|
||||
namespace App\Services\Client;
|
||||
|
||||
use App\Utils\Number;
|
||||
use App\Models\Client;
|
||||
use App\Models\Credit;
|
||||
use App\Services\Email\Email;
|
||||
use App\Utils\Traits\MakesDates;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Services\Email\EmailObject;
|
||||
use App\Services\Email\EmailService;
|
||||
use App\Utils\Number;
|
||||
use App\Utils\Traits\MakesDates;
|
||||
use Illuminate\Mail\Mailables\Address;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ClientService
|
||||
{
|
||||
@ -149,9 +150,12 @@ class ClientService
|
||||
$this->client_start_date = $this->translateDate($options['start_date'], $this->client->date_format(), $this->client->locale());
|
||||
$this->client_end_date = $this->translateDate($options['end_date'], $this->client->date_format(), $this->client->locale());
|
||||
|
||||
$email_service = new EmailService($this->buildStatementMailableData($pdf), $this->client->company);
|
||||
// $email_service = new EmailService($this->buildStatementMailableData($pdf), $this->client->company);
|
||||
// $email_service->send();
|
||||
|
||||
$email_object = $this->buildStatementMailableData($pdf);
|
||||
Email::dispatch($email_object, $this->client->company);
|
||||
|
||||
$email_service->send();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -25,13 +25,19 @@ use Illuminate\Mail\Mailable;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use App\Libraries\Google\Google;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use App\Services\Email\EmailMailable;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Turbo124\Beacon\Facades\LightLogs;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use App\DataMapper\Analytics\EmailFailure;
|
||||
use App\DataMapper\Analytics\EmailSuccess;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
|
||||
use App\Events\Payment\PaymentWasEmailedAndFailed;
|
||||
|
||||
class Email implements ShouldQueue
|
||||
{
|
||||
@ -77,6 +83,8 @@ class Email implements ShouldQueue
|
||||
|
||||
$this->email();
|
||||
|
||||
$this->tearDown();
|
||||
|
||||
}
|
||||
|
||||
public function setOverride(): self
|
||||
@ -101,6 +109,8 @@ class Email implements ShouldQueue
|
||||
|
||||
$this->email_object->company_key = $this->company->company_key;
|
||||
|
||||
$this->email_object->company = $this->company;
|
||||
|
||||
$this->email_object->vendor_contact_id ? $this->email_object->contact = VendorContact::withTrashed()->find($this->email_object->vendor_contact_id) : null;
|
||||
|
||||
$this->email_object->client_contact_id ? $this->email_object->contact = ClientContact::withTrashed()->find($this->email_object->client_contact_id) : null;
|
||||
@ -116,6 +126,21 @@ class Email implements ShouldQueue
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function tearDown(): self
|
||||
{
|
||||
|
||||
$this->email_object->entity = null;
|
||||
$this->email_object->invitation = null;
|
||||
$this->email_object->client = null;
|
||||
$this->email_object->vendor = null;
|
||||
$this->email_object->user = null;
|
||||
$this->email_object->contact = null;
|
||||
$this->email_object->settings = null;
|
||||
|
||||
return $this;
|
||||
|
||||
}
|
||||
|
||||
public function setDefaults(): self
|
||||
{
|
||||
|
||||
@ -161,6 +186,7 @@ class Email implements ShouldQueue
|
||||
|
||||
LightLogs::create(new EmailSuccess($this->company->company_key))
|
||||
->send();
|
||||
|
||||
} catch (\Exception | \RuntimeException | \Google\Service\Exception $e) {
|
||||
nlog("Mailer failed with {$e->getMessage()}");
|
||||
|
||||
@ -196,7 +222,7 @@ class Email implements ShouldQueue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$this->tearDown();
|
||||
/* Releasing immediately does not add in the backoff */
|
||||
$this->release($this->backoff()[$this->attempts()-1]);
|
||||
|
||||
@ -613,6 +639,34 @@ class Email implements ShouldQueue
|
||||
return $user->oauth_user_token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Entity notification when an email fails to send
|
||||
*
|
||||
* @param string $message
|
||||
* @return void
|
||||
*/
|
||||
private function entityEmailFailed($message): void
|
||||
{
|
||||
$class = get_class($this->email_object->entity);
|
||||
|
||||
switch ($class) {
|
||||
case Invoice::class:
|
||||
event(new InvoiceWasEmailedAndFailed($this->email_object->invitation, $this->company, $message, $this->email_object->html_template, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
||||
break;
|
||||
case Payment::class:
|
||||
event(new PaymentWasEmailedAndFailed($this->email_object->entity, $this->company, $message, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
||||
break;
|
||||
default:
|
||||
# code...
|
||||
break;
|
||||
}
|
||||
|
||||
if ($this->email_object->client) {
|
||||
$this->logMailError($message, $this->email_object->client);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function failed($exception = null)
|
||||
{
|
||||
|
||||
@ -622,4 +676,6 @@ class Email implements ShouldQueue
|
||||
config(['queue.failed.driver' => null]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -11,11 +11,12 @@
|
||||
|
||||
namespace App\Services\Email;
|
||||
|
||||
use Illuminate\Mail\Attachment;
|
||||
use App\Models\Document;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Attachment;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Mail\Mailables\Headers;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
|
||||
class EmailMailable extends Mailable
|
||||
{
|
||||
@ -81,6 +82,13 @@ class EmailMailable extends Mailable
|
||||
$attachments[] = Attachment::fromData(fn () => base64_decode($file['file']), $file['name']);
|
||||
}
|
||||
|
||||
foreach($this->email_object->documents as $doc_id){
|
||||
$document = Document::find($doc_id);
|
||||
|
||||
if($document)
|
||||
$attachments[] = Attachment::fromData(fn () => $document->getFile(), $document->name);
|
||||
}
|
||||
|
||||
return $attachments;
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,9 @@ use App\Models\Quote;
|
||||
use App\Models\Client;
|
||||
use App\Models\Credit;
|
||||
use App\Models\Vendor;
|
||||
use App\Models\Company;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Payment;
|
||||
use App\Models\ClientContact;
|
||||
use App\Models\PurchaseOrder;
|
||||
use App\Models\VendorContact;
|
||||
@ -49,6 +51,8 @@ class EmailObject
|
||||
|
||||
public string $company_key;
|
||||
|
||||
public Company $company;
|
||||
|
||||
public ?object $settings = null;
|
||||
|
||||
public bool $whitelabel = false;
|
||||
@ -65,7 +69,7 @@ class EmailObject
|
||||
|
||||
public ?int $entity_id = null;
|
||||
|
||||
public Invoice | Quote | Credit | PurchaseOrder | null $entity;
|
||||
public Invoice | Quote | Credit | PurchaseOrder | Payment | null $entity;
|
||||
|
||||
public ?int $client_id = null;
|
||||
|
||||
@ -102,4 +106,6 @@ class EmailObject
|
||||
public bool $override = false;
|
||||
|
||||
public ?string $invitation_key = null;
|
||||
|
||||
public array $documents = [];
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user