diff --git a/app/Http/Controllers/PreviewController.php b/app/Http/Controllers/PreviewController.php index 2cb31c4e3c..cd64d5da4f 100644 --- a/app/Http/Controllers/PreviewController.php +++ b/app/Http/Controllers/PreviewController.php @@ -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(); diff --git a/app/Jobs/Credit/ZipCredits.php b/app/Jobs/Credit/ZipCredits.php index 2239fad4f8..625e02494c 100644 --- a/app/Jobs/Credit/ZipCredits.php +++ b/app/Jobs/Credit/ZipCredits.php @@ -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); diff --git a/app/Jobs/Invoice/ZipInvoices.php b/app/Jobs/Invoice/ZipInvoices.php index f7292988b0..82ddade105 100644 --- a/app/Jobs/Invoice/ZipInvoices.php +++ b/app/Jobs/Invoice/ZipInvoices.php @@ -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); diff --git a/app/Jobs/Util/RefreshPdfs.php b/app/Jobs/Util/RefreshPdfs.php deleted file mode 100644 index dba50bf67c..0000000000 --- a/app/Jobs/Util/RefreshPdfs.php +++ /dev/null @@ -1,61 +0,0 @@ -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); - }); - } -} \ No newline at end of file diff --git a/app/Listeners/Invoice/CreateInvoicePdf.php b/app/Listeners/Invoice/CreateInvoicePdf.php deleted file mode 100644 index a50c13ef46..0000000000 --- a/app/Listeners/Invoice/CreateInvoicePdf.php +++ /dev/null @@ -1,57 +0,0 @@ -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(); - }); - } - } -} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 028eeb4dff..9a2d4754af 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -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, diff --git a/app/Services/PdfMaker/PdfMaker.php b/app/Services/PdfMaker/PdfMaker.php index 2d028563f0..f54cbbf266 100644 --- a/app/Services/PdfMaker/PdfMaker.php +++ b/app/Services/PdfMaker/PdfMaker.php @@ -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); diff --git a/app/Services/Template/TemplateService.php b/app/Services/Template/TemplateService.php index 898dd63f89..6ca56d0bdd 100644 --- a/app/Services/Template/TemplateService.php +++ b/app/Services/Template/TemplateService.php @@ -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());