1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Livewire/PdfSlot.php

320 lines
11 KiB
PHP
Raw Normal View History

<?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
*/
2023-12-13 17:52:49 +01:00
namespace App\Livewire;
2023-10-26 04:57:44 +02:00
use App\Jobs\Invoice\CreateEInvoice;
2023-07-10 14:52:03 +02:00
use App\Libraries\MultiDB;
2023-07-11 13:16:15 +02:00
use App\Models\CreditInvitation;
use App\Models\InvoiceInvitation;
use App\Models\PurchaseOrderInvitation;
2023-10-26 04:57:44 +02:00
use App\Models\QuoteInvitation;
2023-07-11 13:16:15 +02:00
use App\Models\RecurringInvoiceInvitation;
use App\Services\PdfMaker\Designs\Utilities\DesignHelpers;
2023-10-26 04:57:44 +02:00
use App\Utils\HtmlEngine;
use App\Utils\Number;
use App\Utils\VendorHtmlEngine;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Livewire\Component;
class PdfSlot extends Component
{
public $invitation;
public $db;
public $entity;
public $pdf;
2023-07-09 09:07:05 +02:00
public $url;
2023-08-07 21:55:12 +02:00
2023-07-11 13:16:15 +02:00
private $settings;
private $html_variables;
private $entity_type;
public $show_cost = true;
public $show_quantity = true;
2023-07-19 11:25:01 +02:00
public $show_line_total = true;
public $route_entity = 'client';
2023-07-25 10:24:37 +02:00
public $is_quote = false;
private $entity_calc;
public $html_entity_option = true;
public function mount()
{
MultiDB::setDb($this->db);
2023-10-01 02:06:40 +02:00
if(!$this->invitation) {
$this->entity->service()->createInvitations();
$this->invitation = $this->entity->invitations()->first();
}
}
2023-07-11 13:16:15 +02:00
public function getPdf()
2023-08-07 21:55:12 +02:00
{
2023-10-26 04:57:44 +02:00
if(!$this->invitation) {
2023-09-25 06:40:02 +02:00
$this->entity->service()->createInvitations();
$this->invitation = $this->entity->invitations()->first();
}
$blob = [
'entity_type' => $this->resolveEntityType(),
'entity_id' => $this->entity->id,
'invitation_id' => $this->invitation->id,
'download' => false,
];
$hash = Str::random(64);
2023-10-01 02:06:40 +02:00
Cache::put($hash, $blob, 1800);
2023-08-07 21:55:12 +02:00
$this->pdf = $hash;
2023-07-11 13:16:15 +02:00
}
2023-07-12 06:55:48 +02:00
public function downloadPdf()
{
2023-08-07 21:55:12 +02:00
2023-07-12 06:55:48 +02:00
$file_name = $this->entity->numberFormatter().'.pdf';
$file = (new \App\Jobs\Entity\CreateRawPdf($this->invitation))->handle();
2023-07-12 06:55:48 +02:00
$headers = ['Content-Type' => 'application/pdf'];
return response()->streamDownload(function () use ($file) {
echo $file;
}, $file_name, $headers);
2023-08-07 21:55:12 +02:00
}
2023-08-07 21:55:12 +02:00
public function downloadEInvoice()
{
$file_name = $this->entity->numberFormatter().'.xml';
2023-08-16 11:55:35 +02:00
$file = (new CreateEInvoice($this->entity))->handle();
2023-08-07 21:55:12 +02:00
$headers = ['Content-Type' => 'application/xml'];
return response()->streamDownload(function () use ($file) {
echo $file;
}, $file_name, $headers);
2023-07-12 06:55:48 +02:00
}
public function render()
{
2023-08-07 21:55:12 +02:00
2023-07-11 13:16:15 +02:00
$this->entity_type = $this->resolveEntityType();
$this->entity_calc = $this->entity->calc();
2023-07-11 13:16:15 +02:00
$this->settings = $this->entity->client ? $this->entity->client->getMergedSettings() : $this->entity->company->settings;
$this->html_entity_option = $this->entity->client ? $this->entity->client->getSetting('show_pdfhtml_on_mobile') : $this->entity->company->getSetting('show_pdfhtml_on_mobile');
$this->show_cost = in_array('$product.unit_cost', $this->settings->pdf_variables->product_columns);
2023-07-19 11:25:01 +02:00
$this->show_line_total = in_array('$product.line_total', $this->settings->pdf_variables->product_columns);
$this->show_quantity = in_array('$product.quantity', $this->settings->pdf_variables->product_columns);
2023-10-26 04:57:44 +02:00
if($this->entity_type == 'quote' && !$this->settings->sync_invoice_quote_columns) {
$this->show_cost = in_array('$product.unit_cost', $this->settings->pdf_variables->product_quote_columns);
$this->show_quantity = in_array('$product.quantity', $this->settings->pdf_variables->product_quote_columns);
2023-07-19 11:25:01 +02:00
$this->show_line_total = in_array('$product.line_total', $this->settings->pdf_variables->product_quote_columns);
}
2023-07-26 03:59:04 +02:00
$this->html_variables = $this->entity_type == 'purchase_order' ?
(new VendorHtmlEngine($this->invitation))->generateLabelsAndValues() :
(new HtmlEngine($this->invitation))->generateLabelsAndValues();
2023-07-11 13:16:15 +02:00
return render('components.livewire.pdf-slot', [
'invitation' => $this->invitation,
'entity' => $this->entity,
2023-07-11 13:16:15 +02:00
'data' => $this->invitation->company->settings,
'entity_type' => $this->entity_type,
'products' => $this->getProducts(),
'services' => $this->getServices(),
'amount' => Number::formatMoney($this->entity->amount, $this->entity->client ?: $this->entity->vendor),
'balance' => Number::formatMoney($this->entity->balance, $this->entity->client ?: $this->entity->vendor),
'discount' => $this->entity_calc->getTotalDiscount() > 0 ? Number::formatMoney($this->entity_calc->getTotalDiscount(), $this->entity->client ?: $this->entity->vendor) : false,
'taxes' => $this->entity_calc->getTotalTaxes() > 0 ? Number::formatMoney($this->entity_calc->getTotalTaxes(), $this->entity->client ?: $this->entity->vendor) : false,
2023-07-11 13:16:15 +02:00
'company_details' => $this->getCompanyDetails(),
'company_address' => $this->getCompanyAddress(),
'entity_details' => $this->getEntityDetails(),
'user_details' => $this->getUserDetails(),
2023-07-12 04:50:57 +02:00
'user_name' => $this->getUserName(),
]);
}
2023-07-11 13:16:15 +02:00
private function convertVariables($string): string
{
2023-07-11 13:16:15 +02:00
$html = strtr($string, $this->html_variables['labels']);
$html = strtr($html, $this->html_variables['values']);
2023-07-10 14:52:03 +02:00
2023-07-11 13:16:15 +02:00
return $html;
2023-07-10 14:52:03 +02:00
2023-07-10 15:01:12 +02:00
}
2023-07-10 14:52:03 +02:00
2023-07-11 13:16:15 +02:00
private function getCompanyAddress()
2023-07-11 09:18:12 +02:00
{
2023-08-07 21:55:12 +02:00
2023-07-11 13:16:15 +02:00
$company_address = "";
2023-07-10 14:52:03 +02:00
2023-07-11 13:16:15 +02:00
foreach($this->settings->pdf_variables->company_address as $variable) {
$company_address .= "<p>{$variable}</p>";
}
2023-07-10 14:52:03 +02:00
2023-07-11 13:16:15 +02:00
return $this->convertVariables($company_address);
2023-07-10 14:52:03 +02:00
2023-07-11 13:16:15 +02:00
}
2023-07-10 14:52:03 +02:00
2023-07-11 13:16:15 +02:00
private function getCompanyDetails()
{
$company_details = "";
2023-07-10 14:52:03 +02:00
2023-07-11 13:16:15 +02:00
foreach($this->settings->pdf_variables->company_details as $variable) {
$company_details .= "<p>{$variable}</p>";
}
2023-07-11 09:18:12 +02:00
2023-07-11 13:16:15 +02:00
return $this->convertVariables($company_details);
2023-08-07 21:55:12 +02:00
2023-07-11 13:16:15 +02:00
}
2023-07-11 09:18:12 +02:00
2023-07-11 13:16:15 +02:00
private function getEntityDetails()
{
2023-07-12 04:50:57 +02:00
$entity_details = "";
2023-07-11 13:16:15 +02:00
if($this->entity_type == 'invoice' || $this->entity_type == 'recurring_invoice') {
2023-10-26 04:57:44 +02:00
foreach($this->settings->pdf_variables->invoice_details as $variable) {
2023-12-22 21:22:29 +01:00
$entity_details .= "<div class='flex px-5 block'><p class= w-36 block'>{$variable}_label</p><p class='ml-5 w-36 block entity-field'>{$variable}</p></div>";
2023-10-26 04:57:44 +02:00
}
2023-08-07 21:55:12 +02:00
2023-10-26 04:57:44 +02:00
} elseif($this->entity_type == 'quote') {
foreach($this->settings->pdf_variables->quote_details ?? [] as $variable) {
2023-12-22 21:22:29 +01:00
$entity_details .= "<div class='flex px-5 block'><p class= w-36 block'>{$variable}_label</p><p class='ml-5 w-36 block entity-field'>{$variable}</p></div>";
2023-10-26 04:57:44 +02:00
}
} elseif($this->entity_type == 'credit') {
foreach($this->settings->pdf_variables->credit_details ?? [] as $variable) {
2023-12-22 21:22:29 +01:00
$entity_details .= "<div class='flex px-5 block'><p class= w-36 block'>{$variable}_label</p><p class='ml-5 w-36 block entity-field'>{$variable}</p></div>";
2023-10-26 04:57:44 +02:00
}
} elseif($this->entity_type == 'purchase_order') {
foreach($this->settings->pdf_variables->purchase_order_details ?? [] as $variable) {
2023-12-22 21:22:29 +01:00
$entity_details .= "<div class='flex px-5 block'><p class= w-36 block'>{$variable}_label</p><p class='ml-5 w-36 block entity-field'>{$variable}</p></div>";
2023-10-26 04:57:44 +02:00
}
2023-07-11 13:16:15 +02:00
}
2023-08-07 21:55:12 +02:00
2023-07-11 13:16:15 +02:00
return $this->convertVariables($entity_details);
2023-07-11 09:18:12 +02:00
2023-07-11 13:16:15 +02:00
}
2023-07-11 09:18:12 +02:00
2023-07-12 04:50:57 +02:00
private function getUserName()
{
2023-07-12 06:06:32 +02:00
$name = ctrans('texts.details');
if($this->entity_type == 'purchase_order' && isset($this->settings->pdf_variables->vendor_details[0])) {
2023-07-12 04:50:57 +02:00
$name = $this->settings->pdf_variables->vendor_details[0];
2023-07-12 06:06:32 +02:00
} elseif(isset($this->settings->pdf_variables->client_details[0])) {
2023-07-12 04:50:57 +02:00
$name = $this->settings->pdf_variables->client_details[0];
}
2023-08-07 21:55:12 +02:00
2023-07-12 04:50:57 +02:00
return $this->convertVariables($name);
}
2023-07-11 13:16:15 +02:00
private function getUserDetails()
{
$user_details = "";
2023-07-11 09:18:12 +02:00
2023-07-11 13:16:15 +02:00
if($this->entity_type == 'purchase_order') {
2023-10-26 04:57:44 +02:00
foreach(array_slice($this->settings->pdf_variables->vendor_details, 1) as $variable) {
2023-07-11 13:16:15 +02:00
$user_details .= "<p>{$variable}</p>";
}
2023-10-26 04:57:44 +02:00
} else {
foreach(array_slice($this->settings->pdf_variables->client_details, 1) as $variable) {
2023-07-11 13:16:15 +02:00
$user_details .= "<p>{$variable}</p>";
}
2023-07-11 09:18:12 +02:00
}
2023-08-07 21:55:12 +02:00
2023-07-11 13:16:15 +02:00
return $this->convertVariables($user_details);
}
2023-07-11 09:18:12 +02:00
2023-07-11 13:16:15 +02:00
private function getProducts()
{
2023-07-11 13:16:15 +02:00
$product_items = collect($this->entity->line_items)->filter(function ($item) {
return $item->type_id == 1 || $item->type_id == 6 || $item->type_id == 5;
2023-10-26 04:57:44 +02:00
})->map(function ($item) {
2023-07-25 10:16:57 +02:00
$notes = strlen($item->notes) > 4 ? $item->notes : $item->product_key;
2023-07-11 13:16:15 +02:00
return [
'quantity' => $item->quantity,
'cost' => Number::formatMoney($item->cost, $this->entity->client ?: $this->entity->vendor),
2023-07-25 10:16:57 +02:00
'notes' => $this->invitation->company->markdown_enabled ? DesignHelpers::parseMarkdownToHtml($notes) : $notes,
2023-07-11 13:16:15 +02:00
'line_total' => Number::formatMoney($item->line_total, $this->entity->client ?: $this->entity->vendor),
];
});
2024-01-14 05:05:00 +01:00
2023-07-11 13:16:15 +02:00
return $product_items;
2023-07-11 09:18:12 +02:00
}
2023-07-11 13:16:15 +02:00
private function getServices()
2023-07-11 09:18:12 +02:00
{
2023-07-11 13:16:15 +02:00
$task_items = collect($this->entity->line_items)->filter(function ($item) {
return $item->type_id == 2;
2023-10-26 04:57:44 +02:00
})->map(function ($item) {
2023-07-11 13:16:15 +02:00
return [
'quantity' => $item->quantity,
'cost' => Number::formatMoney($item->cost, $this->entity->client ?: $this->entity->vendor),
'notes' => $this->invitation->company->markdown_enabled ? DesignHelpers::parseMarkdownToHtml($item->notes) : $item->notes,
2023-07-11 13:16:15 +02:00
'line_total' => Number::formatMoney($item->line_total, $this->entity->client ?: $this->entity->vendor),
];
});
return $task_items;
2023-07-11 09:18:12 +02:00
}
2024-01-14 05:05:00 +01:00
private function resolveEntityType(): string
2023-07-11 09:18:12 +02:00
{
2023-07-11 13:16:15 +02:00
if ($this->invitation instanceof InvoiceInvitation) {
return 'invoice';
} elseif ($this->invitation instanceof QuoteInvitation) {
2023-07-25 10:24:37 +02:00
$this->is_quote = true;
2023-07-11 13:16:15 +02:00
return 'quote';
} elseif ($this->invitation instanceof CreditInvitation) {
return 'credit';
} elseif ($this->invitation instanceof RecurringInvoiceInvitation) {
return 'recurring_invoice';
} elseif ($this->invitation instanceof PurchaseOrderInvitation) {
$this->route_entity = 'vendor';
2023-07-11 13:16:15 +02:00
return 'purchase_order';
}
2024-01-14 05:05:00 +01:00
2023-07-11 13:16:15 +02:00
return '';
}
}