2023-10-06 08:47:36 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Template;
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2023-10-06 08:47:36 +02:00
|
|
|
use App\Models\Client;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Models\Company;
|
2023-10-06 08:47:36 +02:00
|
|
|
use App\Models\Credit;
|
|
|
|
use App\Models\Design;
|
|
|
|
use App\Models\Expense;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\Product;
|
|
|
|
use App\Models\Project;
|
|
|
|
use App\Models\PurchaseOrder;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Models\Quote;
|
2023-10-06 08:47:36 +02:00
|
|
|
use App\Models\RecurringInvoice;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Models\Task;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Vendor;
|
2023-10-08 05:09:36 +02:00
|
|
|
use App\Services\Email\AdminEmail;
|
|
|
|
use App\Services\Email\EmailObject;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Bus\Queueable;
|
2023-10-06 08:47:36 +02:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
2023-10-26 04:57:44 +02:00
|
|
|
use Illuminate\Mail\Mailables\Address;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
2023-10-06 08:47:36 +02:00
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
2023-10-26 04:57:44 +02:00
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
2023-10-06 08:47:36 +02:00
|
|
|
|
|
|
|
class TemplateAction implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash;
|
|
|
|
|
|
|
|
public $tries = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @param array $ids The array of entity IDs
|
|
|
|
* @param string $template The template id
|
2023-10-08 05:09:36 +02:00
|
|
|
* @param string $entity The entity class name
|
2023-10-06 08:47:36 +02:00
|
|
|
* @param int $user_id requesting the template
|
|
|
|
* @param string $db The database name
|
|
|
|
* @param bool $send_email Determines whether to send an email
|
2023-10-26 04:57:44 +02:00
|
|
|
*
|
2023-10-06 08:47:36 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2023-10-26 04:57:44 +02:00
|
|
|
public function __construct(
|
|
|
|
public array $ids,
|
|
|
|
private string $template,
|
|
|
|
private string $entity,
|
|
|
|
private int $user_id,
|
|
|
|
private Company $company,
|
|
|
|
private string $db,
|
|
|
|
private string $hash,
|
|
|
|
private bool $send_email = false
|
|
|
|
) {
|
2023-10-06 08:47:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function handle()
|
2023-10-26 04:57:44 +02:00
|
|
|
{
|
2023-10-08 12:03:07 +02:00
|
|
|
// nlog("inside template action");
|
2023-10-08 06:06:34 +02:00
|
|
|
|
2023-10-06 08:47:36 +02:00
|
|
|
MultiDB::setDb($this->db);
|
|
|
|
|
|
|
|
$key = $this->resolveEntityString();
|
|
|
|
|
2023-10-06 13:21:58 +02:00
|
|
|
$resource = $this->entity::query();
|
2023-10-06 08:47:36 +02:00
|
|
|
|
|
|
|
$template = Design::withTrashed()->find($this->decodePrimaryKey($this->template));
|
|
|
|
|
2023-10-30 10:10:19 +01:00
|
|
|
$template_service = new \App\Services\Template\TemplateService($template);
|
2023-10-06 08:47:36 +02:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
match($this->entity) {
|
2023-10-08 06:06:34 +02:00
|
|
|
Invoice::class => $resource->with('payments', 'client'),
|
|
|
|
Quote::class => $resource->with('client'),
|
|
|
|
Task::class => $resource->with('client'),
|
|
|
|
Credit::class => $resource->with('client'),
|
|
|
|
RecurringInvoice::class => $resource->with('client'),
|
|
|
|
Project::class => $resource->with('client'),
|
|
|
|
Expense::class => $resource->with('client'),
|
|
|
|
Payment::class => $resource->with('invoices', 'client'),
|
|
|
|
};
|
2023-10-06 13:08:23 +02:00
|
|
|
|
2023-10-06 13:35:54 +02:00
|
|
|
$result = $resource->withTrashed()
|
2023-10-06 13:21:58 +02:00
|
|
|
->whereIn('id', $this->transformKeys($this->ids))
|
2023-10-06 13:35:54 +02:00
|
|
|
->where('company_id', $this->company->id)
|
|
|
|
->get();
|
2023-10-06 13:08:23 +02:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if($result->count() <= 1) {
|
2023-10-06 13:35:54 +02:00
|
|
|
$data[$key] = collect($result);
|
2023-10-26 04:57:44 +02:00
|
|
|
} else {
|
2023-10-06 13:35:54 +02:00
|
|
|
$data[$key] = $result;
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2023-10-06 08:47:36 +02:00
|
|
|
|
2023-10-08 05:09:36 +02:00
|
|
|
$ts = $template_service->build($data);
|
|
|
|
|
2023-10-08 12:03:07 +02:00
|
|
|
// nlog($ts->getHtml());
|
2023-10-06 08:47:36 +02:00
|
|
|
|
2023-10-08 06:06:34 +02:00
|
|
|
if($this->send_email) {
|
|
|
|
$pdf = $ts->getPdf();
|
2023-10-08 05:09:36 +02:00
|
|
|
$this->sendEmail($pdf, $template);
|
2023-10-26 04:57:44 +02:00
|
|
|
} else {
|
2023-10-08 06:06:34 +02:00
|
|
|
$pdf = $ts->getPdf();
|
2023-10-06 12:07:31 +02:00
|
|
|
$filename = "templates/{$this->hash}.pdf";
|
|
|
|
Storage::disk(config('filesystems.default'))->put($filename, $pdf);
|
2023-10-08 06:06:34 +02:00
|
|
|
return $pdf;
|
2023-10-06 12:07:31 +02:00
|
|
|
}
|
2023-10-06 08:47:36 +02:00
|
|
|
}
|
|
|
|
|
2023-10-08 05:09:36 +02:00
|
|
|
private function sendEmail(mixed $pdf, Design $template)
|
2023-10-06 08:47:36 +02:00
|
|
|
{
|
2023-10-08 05:09:36 +02:00
|
|
|
$user = $this->user_id ? User::find($this->user_id) : $this->company->owner();
|
|
|
|
|
|
|
|
$template_name = " [{$template->name}]";
|
|
|
|
$email_object = new EmailObject;
|
|
|
|
$email_object->to = [new Address($user->email, $user->present()->name())];
|
|
|
|
$email_object->attachments = [['file' => base64_encode($pdf), 'name' => ctrans('texts.template') . ".pdf"]];
|
|
|
|
$email_object->company_key = $this->company->company_key;
|
|
|
|
$email_object->company = $this->company;
|
|
|
|
$email_object->settings = $this->company->settings;
|
|
|
|
$email_object->logo = $this->company->present()->logo();
|
|
|
|
$email_object->whitelabel = $this->company->account->isPaid() ? true : false;
|
|
|
|
$email_object->user_id = $user->id;
|
|
|
|
$email_object->text_body = ctrans('texts.download_report_description') . $template_name;
|
|
|
|
$email_object->body = ctrans('texts.download_report_description') . $template_name;
|
|
|
|
$email_object->subject = ctrans('texts.download_report_description') . $template_name;
|
|
|
|
|
|
|
|
(new AdminEmail($email_object, $this->company))->handle();
|
2023-10-06 08:47:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Context
|
2023-10-26 04:57:44 +02:00
|
|
|
*
|
2023-10-06 08:47:36 +02:00
|
|
|
* If I have an array of invoices, what could I possib
|
2023-10-26 04:57:44 +02:00
|
|
|
*
|
|
|
|
*
|
2023-10-06 08:47:36 +02:00
|
|
|
*/
|
|
|
|
private function resolveEntityString()
|
|
|
|
{
|
|
|
|
return match ($this->entity) {
|
|
|
|
Invoice::class => 'invoices',
|
|
|
|
Quote::class => 'quotes',
|
|
|
|
Task::class => 'tasks',
|
|
|
|
Credit::class => 'credits',
|
|
|
|
RecurringInvoice::class => 'recurring_invoices',
|
|
|
|
Project::class => 'projects',
|
|
|
|
Expense::class => 'expenses',
|
|
|
|
Payment::class => 'payments',
|
|
|
|
Product::class => 'products',
|
|
|
|
PurchaseOrder::class => 'purchase_orders',
|
|
|
|
Project::class => 'projects',
|
|
|
|
Client::class => 'clients',
|
|
|
|
Vendor::class => 'vendors',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public function middleware()
|
|
|
|
{
|
|
|
|
return [new WithoutOverlapping("template-{$this->company->company_key}")];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|