1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 14:12:44 +01:00

Refactor and removal of deprecated code paths

This commit is contained in:
David Bomba 2023-10-26 10:49:09 +11:00
parent 952f96de3b
commit 4fe9e1cef4
8 changed files with 80 additions and 213 deletions

View File

@ -280,7 +280,7 @@ class PreviewController extends BaseController
'process_markdown' => $entity_obj->client->company->markdown_enabled,
'options' => [
'client' => $entity_obj->client,
'entity' => $entity_obj,
request()->input('entity_type', 'invoice')."s" => [$entity_obj],
]
];
@ -433,7 +433,7 @@ class PreviewController extends BaseController
'process_markdown' => $invitation->invoice->client->company->markdown_enabled,
'options' => [
'client' => $invitation->invoice->client,
'entity' => $invitation->invoice,
'invoices' => [$invitation->invoice],
]
];
@ -485,79 +485,84 @@ class PreviewController extends BaseController
/** @var \App\Models\Company $company */
$company = $user->company();
try {
DB::connection($company->db)->beginTransaction();
DB::connection($company->db)->beginTransaction();
/** @var \App\Models\Client $client */
$client = Client::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => $company->id,
]);
/** @var \App\Models\Client $client */
$client = Client::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => $company->id,
]);
/** @var \App\Models\ClientContact $contact */
$contact = ClientContact::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => $company->id,
'client_id' => $client->id,
'is_primary' => 1,
'send_email' => true,
]);
/** @var \App\Models\ClientContact $contact */
$contact = ClientContact::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => $company->id,
'client_id' => $client->id,
'is_primary' => 1,
'send_email' => true,
]);
/** @var \App\Models\Invoice $invoice */
/** @var \App\Models\Invoice $invoice */
$invoice = Invoice::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => $company->id,
'client_id' => $client->id,
'terms' => $company->settings->invoice_terms,
'footer' => $company->settings->invoice_footer,
'public_notes' => 'Sample Public Notes',
]);
$invoice = Invoice::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => $company->id,
'client_id' => $client->id,
'terms' => $company->settings->invoice_terms,
'footer' => $company->settings->invoice_footer,
'public_notes' => 'Sample Public Notes',
]);
$invitation = InvoiceInvitation::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => $company->id,
'invoice_id' => $invoice->id,
'client_contact_id' => $contact->id,
]);
$invitation = InvoiceInvitation::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => $company->id,
'invoice_id' => $invoice->id,
'client_contact_id' => $contact->id,
]);
$invoice->setRelation('invitations', $invitation);
$invoice->setRelation('client', $client);
$invoice->setRelation('company', $company);
$invoice->load('client.company');
$invoice->setRelation('invitations', $invitation);
$invoice->setRelation('client', $client);
$invoice->setRelation('company', $company);
$invoice->load('client.company');
$design_object = json_decode(json_encode(request()->input('design')));
$design_object = json_decode(json_encode(request()->input('design')));
if (! is_object($design_object)) {
return response()->json(['message' => 'Invalid custom design object'], 400);
}
if (! is_object($design_object)) {
return response()->json(['message' => 'Invalid custom design object'], 400);
$html = new HtmlEngine($invoice->invitations()->first());
$design = new Design(Design::CUSTOM, ['custom_partials' => request()->design['design']]);
$state = [
'template' => $design->elements([
'client' => $invoice->client,
'entity' => $invoice,
'pdf_variables' => (array) $invoice->company->settings->pdf_variables,
'products' => request()->design['design']['product'],
]),
'variables' => $html->generateLabelsAndValues(),
'process_markdown' => $invoice->client->company->markdown_enabled,
'options' => [
'client' => $invoice->client,
'invoices' => [$invoice],
]
];
$maker = new PdfMaker($state);
$maker
->design($design)
->build();
DB::connection($company->db)->rollBack();
}
catch(\Exception $e){
DB::connection($company->db)->rollBack();
return response()->json(['message' => $e->getMessage()], 400);
}
$html = new HtmlEngine($invoice->invitations()->first());
$design = new Design(Design::CUSTOM, ['custom_partials' => request()->design['design']]);
$state = [
'template' => $design->elements([
'client' => $invoice->client,
'entity' => $invoice,
'pdf_variables' => (array) $invoice->company->settings->pdf_variables,
'products' => request()->design['design']['product'],
]),
'variables' => $html->generateLabelsAndValues(),
'process_markdown' => $invoice->client->company->markdown_enabled,
'options' => [
'client' => $invoice->client,
'entity' => $invoice,
]
];
$maker = new PdfMaker($state);
$maker
->design($design)
->build();
DB::connection($company->db)->rollBack();
if (request()->query('html') == 'true') {
return $maker->getCompiledHTML();

View File

@ -11,7 +11,6 @@
namespace App\Jobs\Credit;
use App\Jobs\Entity\CreateEntityPdf;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Util\UnlinkFile;
@ -31,8 +30,6 @@ class ZipCredits implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $settings;
public $tries = 1;
public function __construct(protected array $credit_ids, protected Company $company, protected User $user)
@ -48,7 +45,7 @@ class ZipCredits implements ShouldQueue
{
MultiDB::setDb($this->company->db);
$this->settings = $this->company->settings;
$settings = $this->company->settings;
$zipFile = new \PhpZip\ZipFile();
$file_name = now()->addSeconds($this->company->timezone_offset())->format('Y-m-d-h-m-s').'_'.str_replace(' ', '_', trans('texts.credits')).'.zip';
@ -67,7 +64,7 @@ class ZipCredits implements ShouldQueue
$nmo = new NinjaMailerObject;
$nmo->mailable = new DownloadCredits(Storage::url($path.$file_name), $this->company);
$nmo->to_user = $this->user;
$nmo->settings = $this->settings;
$nmo->settings = $settings;
$nmo->company = $this->company;
NinjaMailerJob::dispatch($nmo);

View File

@ -11,7 +11,6 @@
namespace App\Jobs\Invoice;
use App\Jobs\Entity\CreateEntityPdf;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Util\UnlinkFile;
@ -30,14 +29,6 @@ class ZipInvoices implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $invoices;
private $company;
private $user;
public $settings;
public $tries = 1;
/**
@ -47,15 +38,8 @@ class ZipInvoices implements ShouldQueue
* @deprecated confirm to be deleted
* Create a new job instance.
*/
public function __construct($invoices, Company $company, User $user)
public function __construct(public mixed $invoices, public Company $company, public User $user)
{
$this->invoices = $invoices;
$this->company = $company;
$this->user = $user;
$this->settings = $company->settings;
}
/**
@ -66,6 +50,7 @@ class ZipInvoices implements ShouldQueue
public function handle(): void
{
MultiDB::setDb($this->company->db);
$settings = $this->company->settings;
// create new zip object
$zipFile = new \PhpZip\ZipFile();
@ -93,7 +78,7 @@ class ZipInvoices implements ShouldQueue
$nmo = new NinjaMailerObject;
$nmo->mailable = new DownloadInvoices(Storage::url($path.$file_name), $this->company);
$nmo->to_user = $this->user;
$nmo->settings = $this->settings;
$nmo->settings = $settings;
$nmo->company = $this->company;
NinjaMailerJob::dispatch($nmo);

View File

@ -1,61 +0,0 @@
<?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\Jobs\Util;
use App\Jobs\Entity\CreateEntityPdf;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\CreditInvitation;
use App\Models\InvoiceInvitation;
use App\Models\QuoteInvitation;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class RefreshPdfs implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $company;
public function __construct(Company $company)
{
$this->company = $company;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
MultiDB::setDb($this->company->db);
InvoiceInvitation::where('company_id', $this->company->id)->cursor()->each(function ($invitation) {
nlog("generating invoice pdf for {$invitation->invoice_id}");
CreateEntityPdf::dispatch($invitation);
});
QuoteInvitation::where('company_id', $this->company->id)->cursor()->each(function ($invitation) {
nlog("generating quote pdf for {$invitation->quote_id}");
CreateEntityPdf::dispatch($invitation);
});
CreditInvitation::where('company_id', $this->company->id)->cursor()->each(function ($invitation) {
nlog("generating credit pdf for {$invitation->credit_id}");
CreateEntityPdf::dispatch($invitation);
});
}
}

View File

@ -1,57 +0,0 @@
<?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\Listeners\Invoice;
use App\Jobs\Entity\CreateEntityPdf;
use App\Libraries\MultiDB;
use Illuminate\Contracts\Queue\ShouldQueue;
class CreateInvoicePdf implements ShouldQueue
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
MultiDB::setDb($event->company->db);
if (isset($event->invoice)) {
$event->invoice->invitations->each(function ($invitation) {
(new CreateEntityPdf($invitation->load('invoice', 'contact.client.company')))->handle();
});
}
if (isset($event->quote)) {
$event->quote->invitations->each(function ($invitation) {
(new CreateEntityPdf($invitation->load('quote', 'contact.client.company')))->handle();
});
}
if (isset($event->credit)) {
$event->credit->invitations->each(function ($invitation) {
(new CreateEntityPdf($invitation->load('credit', 'contact.client.company')))->handle();
});
}
}
}

View File

@ -125,7 +125,6 @@ use App\Events\Document\DocumentWasCreated;
use App\Events\Document\DocumentWasDeleted;
use App\Events\Document\DocumentWasUpdated;
use App\Events\Invoice\InvoiceWasCancelled;
use App\Listeners\Invoice\CreateInvoicePdf;
use App\Listeners\Quote\QuoteEmailActivity;
use App\Listeners\User\CreatedUserActivity;
use App\Listeners\User\DeletedUserActivity;
@ -378,7 +377,6 @@ class EventServiceProvider extends ServiceProvider
],
CreditWasUpdated::class => [
UpdatedCreditActivity::class,
CreateInvoicePdf::class,
],
CreditWasEmailedAndFailed::class => [
],
@ -459,7 +457,6 @@ class EventServiceProvider extends ServiceProvider
],
InvoiceWasReversed::class => [
InvoiceReversedActivity::class,
CreateInvoicePdf::class,
],
InvoiceWasCancelled::class => [
InvoiceCancelledActivity::class,
@ -515,7 +512,6 @@ class EventServiceProvider extends ServiceProvider
],
QuoteWasUpdated::class => [
QuoteUpdatedActivity::class,
CreateInvoicePdf::class,
],
QuoteWasEmailed::class => [
QuoteEmailActivity::class,

View File

@ -80,14 +80,16 @@ class PdfMaker
$replacements = [];
$contents = $this->document->getElementsByTagName('ninja');
$twig = (new TemplateService())->twig;
$ts = new TemplateService();
$data = $ts->processData($this->options)->getData();
$twig = $ts->twig;
foreach ($contents as $content) {
$template = $content->ownerDocument->saveHTML($content);
$template = $twig->createTemplate(html_entity_decode($template));
$template = $template->render($this->options);
$template = $template->render($data);
$f = $this->document->createDocumentFragment();
$f->appendXML($template);

View File

@ -79,7 +79,7 @@ class TemplateService
$this->twig = new Environment($loader, [
'debug' => true,
]);
$string_extension = new StringLoaderExtension();
$this->twig->addExtension($string_extension);
$this->twig->addExtension(new IntlExtension());