1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Http/Controllers/EmailController.php

202 lines
7.3 KiB
PHP
Raw Normal View History

<?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)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers;
use App\Events\Credit\CreditWasEmailed;
use App\Events\Quote\QuoteWasEmailed;
2021-02-25 22:06:43 +01:00
use App\Http\Middleware\UserVerified;
use App\Http\Requests\Email\SendEmailRequest;
2020-10-27 12:57:12 +01:00
use App\Jobs\Entity\EmailEntity;
use App\Jobs\Mail\EntitySentMailer;
2022-07-04 07:27:09 +02:00
use App\Jobs\PurchaseOrder\PurchaseOrderEmail;
use App\Models\Credit;
use App\Models\Invoice;
2022-07-04 07:37:00 +02:00
use App\Models\PurchaseOrder;
use App\Models\Quote;
2020-10-28 00:02:32 +01:00
use App\Models\RecurringInvoice;
use App\Transformers\CreditTransformer;
use App\Transformers\InvoiceTransformer;
2022-07-04 07:37:37 +02:00
use App\Transformers\PurchaseOrderTransformer;
use App\Transformers\QuoteTransformer;
2020-10-28 00:02:32 +01:00
use App\Transformers\RecurringInvoiceTransformer;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
2020-10-28 11:10:49 +01:00
use Illuminate\Http\Response;
class EmailController extends BaseController
{
use MakesHash;
protected $entity_type = Invoice::class;
protected $entity_transformer = InvoiceTransformer::class;
public function __construct()
{
parent::__construct();
}
/**
* Returns a template filled with entity variables.
*
2020-10-28 11:10:49 +01:00
* @param SendEmailRequest $request
* @return Response
*
* @OA\Post(
* path="/api/v1/emails",
* operationId="sendEmailTemplate",
* tags={"emails"},
* summary="Sends an email for an entity",
* description="Sends an email for an entity",
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\RequestBody(
* description="The template subject and body",
* required=true,
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="object",
* @OA\Property(
* property="subject",
* description="The email subject",
* type="string",
* ),
* @OA\Property(
* property="body",
* description="The email body",
* type="string",
* ),
* @OA\Property(
* property="entity",
* description="The entity name",
* type="string",
* ),
* @OA\Property(
* property="entity_id",
* description="The entity_id",
* type="string",
* ),
* @OA\Property(
* property="template",
* description="The template required",
* type="string",
* ),
* )
* )
* ),
* @OA\Response(
* response=200,
* description="success",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Template"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function send(SendEmailRequest $request)
{
$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') : '';
$entity_string = strtolower(class_basename($entity_obj));
$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,
'body' => $body,
2021-01-20 04:49:22 +01:00
];
2022-07-27 03:21:12 +02:00
if(Ninja::isHosted() && !$entity_obj->company->account->account_sms_verified)
return response(['message' => 'Please verify your account to send emails.'], 400);
if($entity == 'purchaseOrder' || $entity == 'purchase_order' || $template == 'purchase_order' || $entity == 'App\Models\PurchaseOrder'){
return $this->sendPurchaseOrder($entity_obj, $data, $template);
2022-07-04 07:27:09 +02:00
}
2021-01-20 04:49:22 +01:00
$entity_obj->invitations->each(function ($invitation) use ($data, $entity_string, $entity_obj, $template) {
2022-12-02 23:50:55 +01: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
EmailEntity::dispatch($invitation->fresh(), $invitation->company, $template, $data);
}
2022-12-02 23:50:55 +01:00
});
2020-07-14 11:55:28 +02:00
2022-12-01 05:33:40 +01:00
$entity_obj = $entity_obj->fresh();
$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) {
$this->entity_type = Invoice::class;
$this->entity_transformer = InvoiceTransformer::class;
if ($entity_obj->invitations->count() >= 1) {
$entity_obj->entityEmailEvent($entity_obj->invitations->first(), 'invoice', $template);
}
}
2020-10-28 00:02:32 +01:00
if ($entity_obj instanceof Quote) {
$this->entity_type = Quote::class;
$this->entity_transformer = QuoteTransformer::class;
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'));
}
}
2020-10-28 00:02:32 +01:00
if ($entity_obj instanceof Credit) {
$this->entity_type = Credit::class;
$this->entity_transformer = CreditTransformer::class;
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'));
}
}
2020-10-28 00:02:32 +01:00
if ($entity_obj instanceof RecurringInvoice) {
$this->entity_type = RecurringInvoice::class;
$this->entity_transformer = RecurringInvoiceTransformer::class;
}
return $this->itemResponse($entity_obj->fresh());
}
2022-07-04 07:37:00 +02:00
private function sendPurchaseOrder($entity_obj, $data, $template)
2022-07-04 07:37:00 +02:00
{
$this->entity_type = PurchaseOrder::class;
$this->entity_transformer = PurchaseOrderTransformer::class;
$data['template'] = $template;
PurchaseOrderEmail::dispatch($entity_obj, $entity_obj->company, $data);
2022-07-04 07:37:00 +02:00
return $this->itemResponse($entity_obj);
}
}