2020-03-28 12:34:04 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-03-28 12:34:04 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-03-28 12:34:04 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-03-28 12:34:04 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2023-12-12 08:43:55 +01:00
|
|
|
use App\Utils\Ninja;
|
|
|
|
use App\Models\Quote;
|
2020-03-29 14:53:00 +02:00
|
|
|
use App\Models\Credit;
|
|
|
|
use App\Models\Invoice;
|
2023-12-12 08:43:55 +01:00
|
|
|
use App\Models\Webhook;
|
2022-07-04 07:37:00 +02:00
|
|
|
use App\Models\PurchaseOrder;
|
2023-03-18 08:24:56 +01:00
|
|
|
use App\Services\Email\Email;
|
2023-12-12 08:43:55 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use App\Models\RecurringInvoice;
|
2023-03-07 12:36:50 +01:00
|
|
|
use App\Services\Email\EmailObject;
|
2023-12-12 08:43:55 +01:00
|
|
|
use App\Events\Quote\QuoteWasEmailed;
|
|
|
|
use App\Transformers\QuoteTransformer;
|
|
|
|
use Illuminate\Mail\Mailables\Address;
|
|
|
|
use App\Events\Credit\CreditWasEmailed;
|
2020-03-29 14:53:00 +02:00
|
|
|
use App\Transformers\CreditTransformer;
|
|
|
|
use App\Transformers\InvoiceTransformer;
|
2023-12-12 08:43:55 +01:00
|
|
|
use App\Http\Requests\Email\SendEmailRequest;
|
|
|
|
use App\Jobs\PurchaseOrder\PurchaseOrderEmail;
|
2022-07-04 07:37:37 +02:00
|
|
|
use App\Transformers\PurchaseOrderTransformer;
|
2020-10-28 00:02:32 +01:00
|
|
|
use App\Transformers\RecurringInvoiceTransformer;
|
2020-03-28 12:34:04 +01:00
|
|
|
|
|
|
|
class EmailController extends BaseController
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
protected $entity_type = Invoice::class;
|
2020-03-29 14:53:00 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
protected $entity_transformer = InvoiceTransformer::class;
|
2020-03-29 14:53:00 +02:00
|
|
|
|
2020-03-28 12:34:04 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function send(SendEmailRequest $request)
|
|
|
|
{
|
2020-03-29 14:22:14 +02:00
|
|
|
$entity = $request->input('entity');
|
2021-09-28 08:12:50 +02:00
|
|
|
$entity_obj = $entity::withTrashed()->with('invitations')->find($request->input('entity_id'));
|
2021-09-26 04:13:03 +02:00
|
|
|
$subject = $request->has('subject') ? $request->input('subject') : '';
|
|
|
|
$body = $request->has('body') ? $request->input('body') : '';
|
2022-06-21 11:57:17 +02:00
|
|
|
$template = str_replace('email_template_', '', $request->input('template'));
|
2020-11-05 11:14:30 +01:00
|
|
|
|
2021-01-20 04:49:22 +01:00
|
|
|
$data = [
|
|
|
|
'subject' => $subject,
|
2022-06-21 11:57:17 +02:00
|
|
|
'body' => $body,
|
2021-01-20 04:49:22 +01:00
|
|
|
];
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$mo = new EmailObject();
|
2023-03-07 13:17:03 +01:00
|
|
|
$mo->subject = strlen($subject) > 3 ? $subject : null;
|
|
|
|
$mo->body = strlen($body) > 3 ? $body : null;
|
2023-03-07 12:36:50 +01:00
|
|
|
$mo->entity_id = $request->input('entity_id');
|
2023-03-08 07:43:52 +01:00
|
|
|
$mo->template = $request->input('template'); //full template name in use
|
2023-03-07 12:36:50 +01:00
|
|
|
$mo->entity_class = $this->resolveClass($entity);
|
2023-03-08 07:43:52 +01:00
|
|
|
$mo->email_template_body = $request->input('template');
|
|
|
|
$mo->email_template_subject = str_replace("template", "subject", $request->input('template'));
|
2023-02-15 01:04:47 +01:00
|
|
|
|
2023-04-27 05:15:49 +02:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
2024-04-23 04:45:43 +02:00
|
|
|
if ($request->cc_email && (Ninja::isSelfHost() || $user->account->isPremium())) {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
foreach($request->cc_email as $email) {
|
2023-11-21 09:35:57 +01:00
|
|
|
$mo->cc[] = new Address($email);
|
2023-11-26 08:41:42 +01:00
|
|
|
}
|
2023-11-21 09:35:57 +01:00
|
|
|
|
2023-03-18 08:24:56 +01:00
|
|
|
}
|
2023-03-08 07:43:52 +01:00
|
|
|
|
2023-08-04 09:12:21 +02:00
|
|
|
$entity_obj->invitations->each(function ($invitation) use ($entity_obj, $mo) {
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $invitation->contact->trashed() && $invitation->contact->email) {
|
2021-01-12 00:21:17 +01:00
|
|
|
$entity_obj->service()->markSent()->save();
|
2021-01-20 04:49:22 +01:00
|
|
|
|
2023-03-07 12:36:50 +01:00
|
|
|
$mo->invitation_id = $invitation->id;
|
2023-08-18 13:36:50 +02:00
|
|
|
$mo->client_id = $invitation->contact->client_id ?? null;
|
|
|
|
$mo->vendor_id = $invitation->contact->vendor_id ?? null;
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-03-07 12:36:50 +01:00
|
|
|
Email::dispatch($mo, $invitation->company);
|
2020-03-29 14:22:14 +02:00
|
|
|
}
|
|
|
|
});
|
2020-07-14 11:55:28 +02:00
|
|
|
|
2022-12-01 05:33:40 +01:00
|
|
|
$entity_obj = $entity_obj->fresh();
|
2020-10-20 02:53:54 +02:00
|
|
|
$entity_obj->last_sent_date = now();
|
|
|
|
$entity_obj->save();
|
|
|
|
|
2020-07-14 11:55:28 +02:00
|
|
|
/*Only notify the admin ONCE, not once per contact/invite*/
|
2020-10-28 00:02:32 +01:00
|
|
|
if ($entity_obj instanceof Invoice) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->entity_type = Invoice::class;
|
|
|
|
$this->entity_transformer = InvoiceTransformer::class;
|
2020-12-09 10:52:08 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($entity_obj->invitations->count() >= 1) {
|
2021-01-13 08:20:46 +01:00
|
|
|
$entity_obj->entityEmailEvent($entity_obj->invitations->first(), 'invoice', $template);
|
2023-12-12 08:43:55 +01:00
|
|
|
$entity_obj->sendEvent(Webhook::EVENT_SENT_INVOICE, "client");
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-03-29 14:53:00 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 00:02:32 +01:00
|
|
|
if ($entity_obj instanceof Quote) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->entity_type = Quote::class;
|
|
|
|
$this->entity_transformer = QuoteTransformer::class;
|
2020-12-09 10:52:08 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($entity_obj->invitations->count() >= 1) {
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new QuoteWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), 'quote'));
|
2023-12-12 08:43:55 +01:00
|
|
|
$entity_obj->sendEvent(Webhook::EVENT_SENT_QUOTE, "client");
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-03-29 14:53:00 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 00:02:32 +01:00
|
|
|
if ($entity_obj instanceof Credit) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->entity_type = Credit::class;
|
|
|
|
$this->entity_transformer = CreditTransformer::class;
|
2020-12-09 11:05:26 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($entity_obj->invitations->count() >= 1) {
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new CreditWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), 'credit'));
|
2023-12-12 08:43:55 +01:00
|
|
|
$entity_obj->sendEvent(Webhook::EVENT_SENT_CREDIT, "client");
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-03-29 14:53:00 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 00:02:32 +01:00
|
|
|
if ($entity_obj instanceof RecurringInvoice) {
|
|
|
|
$this->entity_type = RecurringInvoice::class;
|
|
|
|
$this->entity_transformer = RecurringInvoiceTransformer::class;
|
|
|
|
}
|
|
|
|
|
2023-03-18 08:24:56 +01:00
|
|
|
if ($entity_obj instanceof PurchaseOrder) {
|
2023-03-08 07:56:34 +01:00
|
|
|
$this->entity_type = PurchaseOrder::class;
|
|
|
|
$this->entity_transformer = PurchaseOrderTransformer::class;
|
|
|
|
}
|
|
|
|
|
2020-12-13 10:46:29 +01:00
|
|
|
return $this->itemResponse($entity_obj->fresh());
|
2020-03-28 12:34:04 +01:00
|
|
|
}
|
2022-07-04 07:37:00 +02:00
|
|
|
|
2024-06-17 08:08:06 +02:00
|
|
|
// private function sendPurchaseOrder($entity_obj, $data, $template)
|
|
|
|
// {
|
|
|
|
// $this->entity_type = PurchaseOrder::class;
|
2022-07-04 07:37:00 +02:00
|
|
|
|
2024-06-17 08:08:06 +02:00
|
|
|
// $this->entity_transformer = PurchaseOrderTransformer::class;
|
2022-07-04 07:37:00 +02:00
|
|
|
|
2024-06-17 08:08:06 +02:00
|
|
|
// $data['template'] = $template;
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2024-06-17 08:08:06 +02:00
|
|
|
// PurchaseOrderEmail::dispatch($entity_obj, $entity_obj->company, $data);
|
|
|
|
// $entity_obj->sendEvent(Webhook::EVENT_SENT_PURCHASE_ORDER, "vendor");
|
2023-12-12 08:43:55 +01:00
|
|
|
|
2024-06-17 08:08:06 +02:00
|
|
|
// return $this->itemResponse($entity_obj);
|
|
|
|
// }
|
2023-03-07 12:36:50 +01:00
|
|
|
|
|
|
|
private function resolveClass(string $entity): string
|
|
|
|
{
|
2023-08-02 12:10:42 +02:00
|
|
|
$class = '';
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-03-18 08:24:56 +01:00
|
|
|
match ($entity) {
|
2023-03-07 12:36:50 +01:00
|
|
|
'invoice' => $class = Invoice::class,
|
2023-03-08 07:20:40 +01:00
|
|
|
'App\Models\Invoice' => $class = Invoice::class,
|
2023-03-07 12:36:50 +01:00
|
|
|
'credit' => $class = Credit::class,
|
2023-03-08 07:20:40 +01:00
|
|
|
'App\Models\Credit' => $class = Credit::class,
|
2023-03-07 12:36:50 +01:00
|
|
|
'quote' => $class = Quote::class,
|
2023-03-08 07:20:40 +01:00
|
|
|
'App\Models\Quote' => $class = Quote::class,
|
2023-03-07 12:36:50 +01:00
|
|
|
'purchase_order' => $class = PurchaseOrder::class,
|
|
|
|
'purchaseOrder' => $class = PurchaseOrder::class,
|
|
|
|
'App\Models\PurchaseOrder' => $class = PurchaseOrder::class,
|
|
|
|
default => $class = Invoice::class,
|
|
|
|
};
|
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
2020-03-28 12:34:04 +01:00
|
|
|
}
|