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

546 lines
19 KiB
PHP
Raw Normal View History

2022-06-29 03:37:40 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2022-06-29 03:37:40 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers;
2023-11-26 08:41:42 +01:00
use App\DataMapper\Analytics\LivePreview;
use App\Factory\PurchaseOrderFactory;
use App\Http\Requests\Preview\PreviewPurchaseOrderRequest;
2023-10-27 10:30:42 +02:00
use App\Jobs\Util\PreviewPdf;
2023-11-26 08:41:42 +01:00
use App\Libraries\MultiDB;
use App\Models\Client;
2023-10-27 10:30:42 +02:00
use App\Models\PurchaseOrder;
2023-11-26 08:41:42 +01:00
use App\Models\PurchaseOrderInvitation;
use App\Models\Vendor;
2022-06-29 03:37:40 +02:00
use App\Models\VendorContact;
2023-11-26 08:41:42 +01:00
use App\Repositories\PurchaseOrderRepository;
2023-10-27 10:30:42 +02:00
use App\Services\Pdf\PdfService;
2023-02-16 02:36:09 +01:00
use App\Services\PdfMaker\Design;
2023-11-26 08:41:42 +01:00
use App\Services\PdfMaker\Design as PdfDesignModel;
use App\Services\PdfMaker\Design as PdfMakerDesign;
2023-10-27 10:30:42 +02:00
use App\Services\PdfMaker\PdfMaker;
2023-11-26 08:41:42 +01:00
use App\Utils\HostedPDF\NinjaPdf;
use App\Utils\Ninja;
use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\MakesHash;
2022-06-29 03:37:40 +02:00
use App\Utils\Traits\MakesInvoiceHtml;
use App\Utils\Traits\Pdf\PageNumbering;
2023-11-26 08:41:42 +01:00
use App\Utils\VendorHtmlEngine;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
2022-06-29 03:37:40 +02:00
use Illuminate\Support\Facades\Response;
2023-11-26 08:41:42 +01:00
use Turbo124\Beacon\Facades\LightLogs;
2022-06-29 03:37:40 +02:00
class PreviewPurchaseOrderController extends BaseController
{
use MakesHash;
use MakesInvoiceHtml;
use PageNumbering;
public function __construct()
{
parent::__construct();
}
/**
* Returns a template filled with entity variables.
*
* @return \Illuminate\Http\Response
*
* @OA\Post(
* path="/api/v1/preview/purchase_order",
* operationId="getPreviewPurchaseOrder",
* tags={"preview"},
* summary="Returns a pdf preview for purchase order",
* description="Returns a pdf preview for purchase order.",
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Response(
* response=200,
* description="The pdf response",
* @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\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 show()
{
if (request()->has('entity') &&
request()->has('entity_id') &&
! empty(request()->input('entity')) &&
! empty(request()->input('entity_id')) &&
request()->has('body')) {
$design_object = json_decode(json_encode(request()->input('design')));
if (! is_object($design_object)) {
return response()->json(['message' => ctrans('texts.invalid_design_object')], 400);
}
2023-08-06 09:03:12 +02:00
$entity_obj = PurchaseOrder::query()->whereId($this->decodePrimaryKey(request()->input('entity_id')))->company()->first();
2022-06-29 03:37:40 +02:00
if (! $entity_obj) {
return $this->blankEntity();
}
App::forgetInstance('translator');
$t = app('translator');
App::setLocale($entity_obj->company->locale());
$t->replace(Ninja::transformTranslations($entity_obj->company->settings));
$html = new VendorHtmlEngine($entity_obj->invitations()->first());
$design_namespace = 'App\Services\PdfMaker\Designs\\'.request()->design['name'];
$design_class = new $design_namespace();
$state = [
'template' => $design_class->elements([
'client' => null,
'vendor' => $entity_obj->vendor,
'entity' => $entity_obj,
'pdf_variables' => (array) $entity_obj->company->settings->pdf_variables,
'variables' => $html->generateLabelsAndValues(),
]),
'variables' => $html->generateLabelsAndValues(),
'process_markdown' => $entity_obj->company->markdown_enabled,
2023-10-26 08:13:06 +02:00
'options' => [
'vendor' => $entity_obj->vendor ?? [],
request()->input('entity')."s" => [$entity_obj],
]
2022-06-29 03:37:40 +02:00
];
$design = new Design(request()->design['name']);
$maker = new PdfMaker($state);
$maker
->design($design)
->build();
if (request()->query('html') == 'true') {
return $maker->getCompiledHTML();
}
//if phantom js...... inject here..
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
2024-01-14 05:05:00 +01:00
return (new Phantom())->convertHtmlToPdf($maker->getCompiledHTML(true));
2022-06-29 03:37:40 +02:00
}
2024-01-14 05:05:00 +01:00
2023-08-04 09:12:21 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2023-02-16 02:36:09 +01:00
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
2022-06-29 03:37:40 +02:00
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
2023-08-04 09:12:21 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $user->company());
2022-06-29 03:37:40 +02:00
2023-02-16 02:36:09 +01:00
if ($numbered_pdf) {
2022-06-29 03:37:40 +02:00
$pdf = $numbered_pdf;
2023-02-16 02:36:09 +01:00
}
2022-06-29 03:37:40 +02:00
return $pdf;
}
//else
2023-08-04 09:12:21 +02:00
$file_path = (new PreviewPdf($maker->getCompiledHTML(true), $user->company()))->handle();
2022-06-29 03:37:40 +02:00
return response()->download($file_path, basename($file_path), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);
}
return $this->blankEntity();
}
public function live(PreviewPurchaseOrderRequest $request)
{
2023-10-27 12:09:53 +02:00
$start = microtime(true);
2023-08-04 09:12:21 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2023-10-27 10:30:42 +02:00
$invitation = $request->resolveInvitation();
$vendor = $request->getVendor();
$settings = $user->company()->settings;
$entity_obj = $invitation->purchase_order;
$entity_obj->fill($request->all());
2023-10-27 12:09:53 +02:00
if(!$entity_obj->id) {
$entity_obj->design_id = intval($this->decodePrimaryKey($settings->{"purchase_order_design_id"}));
$entity_obj->footer = empty($entity_obj->footer) ? $settings->{"purchase_order_footer"} : $entity_obj->footer;
$entity_obj->terms = empty($entity_obj->terms) ? $settings->{"purchase_order_terms"} : $entity_obj->terms;
$entity_obj->public_notes = empty($entity_obj->public_notes) ? $request->getVendor()->public_notes : $entity_obj->public_notes;
$invitation->setRelation($request->entity, $entity_obj);
}
2024-01-14 05:05:00 +01:00
2023-10-27 10:30:42 +02:00
$ps = new PdfService($invitation, 'purchase_order', [
'client' => $entity_obj->client ?? false,
'vendor' => $vendor ?? false,
"purchase_orders" => [$entity_obj],
]);
$pdf = $ps->boot()->getPdf();
2023-10-27 12:09:53 +02:00
if (Ninja::isHosted()) {
LightLogs::create(new LivePreview())
->increment()
->batch();
}
/** Return PDF */
return response()->streamDownload(function () use ($pdf) {
echo $pdf;
}, 'preview.pdf', [
'Content-Disposition' => 'inline',
'Content-Type' => 'application/pdf',
'Cache-Control:' => 'no-cache',
2024-01-14 05:05:00 +01:00
'Server-Timing' => microtime(true) - $start
2023-10-27 12:09:53 +02:00
]);
2023-10-27 10:30:42 +02:00
}
public function livex(PreviewPurchaseOrderRequest $request)
{
/** @var \App\Models\User $user */
$user = auth()->user();
2023-08-04 09:12:21 +02:00
$company = $user->company();
$file_path = (new PreviewPdf('<html></html>', $company))->handle();
$response = Response::make($file_path, 200);
$response->header('Content-Type', 'application/pdf');
return $response;
2022-06-29 03:37:40 +02:00
MultiDB::setDb($company->db);
$repo = new PurchaseOrderRepository();
2023-08-04 09:12:21 +02:00
$entity_obj = PurchaseOrderFactory::create($company->id, $user->id);
2022-06-29 03:37:40 +02:00
$class = PurchaseOrder::class;
try {
DB::connection(config('database.default'))->beginTransaction();
2023-02-16 02:36:09 +01:00
if ($request->has('entity_id')) {
2023-08-11 06:18:58 +02:00
/** @var \App\Models\PurchaseOrder|\Illuminate\Contracts\Database\Eloquent\Builder $entity_obj **/
$entity_obj = \App\Models\PurchaseOrder::on(config('database.default'))
2022-06-29 03:37:40 +02:00
->with('vendor.company')
->where('id', $this->decodePrimaryKey($request->input('entity_id')))
->where('company_id', $company->id)
->withTrashed()
->first();
}
$entity_obj = $repo->save($request->all(), $entity_obj);
2023-02-16 02:36:09 +01:00
if (!$request->has('entity_id')) {
2022-06-29 03:37:40 +02:00
$entity_obj->service()->fillDefaults()->save();
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2022-06-29 03:37:40 +02:00
App::forgetInstance('translator');
$t = app('translator');
App::setLocale($entity_obj->company->locale());
$t->replace(Ninja::transformTranslations($entity_obj->company->settings));
$html = new VendorHtmlEngine($entity_obj->invitations()->first());
2023-08-04 09:12:21 +02:00
/** @var \App\Models\Design $design */
$design = \App\Models\Design::withTrashed()->find($entity_obj->design_id);
2022-06-29 03:37:40 +02:00
/* Catch all in case migration doesn't pass back a valid design */
2023-02-16 02:36:09 +01:00
if (!$design) {
2022-06-29 03:37:40 +02:00
$design = \App\Models\Design::find(2);
2023-02-16 02:36:09 +01:00
}
2022-06-29 03:37:40 +02:00
if ($design->is_custom) {
$options = [
'custom_partials' => json_decode(json_encode($design->design), true)
];
$template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options);
} else {
$template = new PdfMakerDesign(strtolower($design->name));
}
$variables = $html->generateLabelsAndValues();
$state = [
'template' => $template->elements([
'client' => null,
'vendor' => $entity_obj->vendor,
'entity' => $entity_obj,
'pdf_variables' => (array) $entity_obj->company->settings->pdf_variables,
'variables' => $html->generateLabelsAndValues(),
'$product' => $design->design->product,
]),
'variables' => $html->generateLabelsAndValues(),
2023-09-19 02:05:13 +02:00
'options' => [
'client' => null,
'vendor' => $entity_obj->vendor,
2023-10-26 08:13:06 +02:00
'purchase_orders' => [$entity_obj],
2023-09-19 02:05:13 +02:00
'variables' => $html->generateLabelsAndValues(),
],
2022-06-29 03:37:40 +02:00
'process_markdown' => $entity_obj->company->markdown_enabled,
];
$maker = new PdfMaker($state);
$maker
->design($template)
->build();
DB::connection(config('database.default'))->rollBack();
if (request()->query('html') == 'true') {
return $maker->getCompiledHTML();
}
2023-02-16 02:36:09 +01:00
} catch(\Exception $e) {
2022-06-29 03:37:40 +02:00
DB::connection(config('database.default'))->rollBack();
return;
}
2024-02-13 05:25:18 +01:00
/** @var \App\Models\User $user */
$user = auth()->user();
2022-06-29 03:37:40 +02:00
2023-10-26 04:57:44 +02:00
//if phantom js...... inject here..
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
2024-01-14 05:05:00 +01:00
return (new Phantom())->convertHtmlToPdf($maker->getCompiledHTML(true));
2023-10-26 04:57:44 +02:00
}
2024-01-14 05:05:00 +01:00
2023-10-26 04:57:44 +02:00
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
2022-06-29 03:37:40 +02:00
2023-10-26 04:57:44 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $user->company());
2022-06-29 03:37:40 +02:00
2023-10-26 04:57:44 +02:00
if ($numbered_pdf) {
$pdf = $numbered_pdf;
2022-06-29 03:37:40 +02:00
}
2023-10-26 04:57:44 +02:00
return $pdf;
}
$file_path = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
2022-06-29 03:37:40 +02:00
2023-02-16 02:36:09 +01:00
if (Ninja::isHosted()) {
LightLogs::create(new LivePreview())
->increment()
->batch();
}
2022-06-29 03:37:40 +02:00
$response = Response::make($file_path, 200);
$response->header('Content-Type', 'application/pdf');
return $response;
}
private function blankEntity()
{
2023-08-04 09:12:21 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2022-06-29 03:37:40 +02:00
App::forgetInstance('translator');
$t = app('translator');
2023-08-04 09:12:21 +02:00
$t->replace(Ninja::transformTranslations($user->company()->settings));
2022-06-29 03:37:40 +02:00
2023-08-04 09:12:21 +02:00
/** @var \App\Models\PurchaseOrderInvitation $invitation */
$invitation = PurchaseOrderInvitation::where('company_id', $user->company()->id)->orderBy('id', 'desc')->first();
2022-06-29 03:37:40 +02:00
/* If we don't have a valid invitation in the system - create a mock using transactions */
2023-02-16 02:36:09 +01:00
if (!$invitation) {
2022-06-29 03:37:40 +02:00
return $this->mockEntity();
2023-02-16 02:36:09 +01:00
}
2022-06-29 03:37:40 +02:00
$design_object = json_decode(json_encode(request()->input('design')));
if (! is_object($design_object)) {
return response()->json(['message' => 'Invalid custom design object'], 400);
}
$html = new VendorHtmlEngine($invitation);
$design = new Design(Design::CUSTOM, ['custom_partials' => request()->design['design']]);
$state = [
'template' => $design->elements([
'client' => null,
'vendor' => $invitation->purchase_order->vendor,
'entity' => $invitation->purchase_order,
'pdf_variables' => (array) $invitation->company->settings->pdf_variables,
'products' => request()->design['design']['product'],
]),
'variables' => $html->generateLabelsAndValues(),
'process_markdown' => $invitation->company->markdown_enabled,
2023-10-26 08:13:06 +02:00
'options' => [
'vendor' => $invitation->purchase_order->vendor,
'purchase_orders' => [$invitation->purchase_order],
2023-10-26 08:13:06 +02:00
],
2022-06-29 03:37:40 +02:00
];
$maker = new PdfMaker($state);
$maker
->design($design)
->build();
if (request()->query('html') == 'true') {
return $maker->getCompiledHTML();
}
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
2024-01-14 05:05:00 +01:00
return (new Phantom())->convertHtmlToPdf($maker->getCompiledHTML(true));
2022-06-29 03:37:40 +02:00
}
2023-08-04 09:12:21 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2023-02-16 02:36:09 +01:00
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
2022-06-29 03:37:40 +02:00
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
2023-08-04 09:12:21 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $user->company());
2022-06-29 03:37:40 +02:00
2023-02-16 02:36:09 +01:00
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
2022-06-29 03:37:40 +02:00
2023-02-16 02:36:09 +01:00
return $pdf;
2022-06-29 03:37:40 +02:00
}
2024-01-14 05:05:00 +01:00
2023-08-04 09:12:21 +02:00
$file_path = (new PreviewPdf($maker->getCompiledHTML(true), $user->company()))->handle();
2022-06-29 03:37:40 +02:00
$response = Response::make($file_path, 200);
$response->header('Content-Type', 'application/pdf');
return $response;
}
private function mockEntity()
{
2023-08-04 09:12:21 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
DB::connection($user->company()->db)->beginTransaction();
2022-06-29 03:37:40 +02:00
2023-08-04 09:12:21 +02:00
/** @var \App\Models\Vendor $vendor */
2022-06-29 03:37:40 +02:00
$vendor = Vendor::factory()->create([
2023-08-04 09:12:21 +02:00
'user_id' => $user->id,
'company_id' => $user->company()->id,
2022-06-29 03:37:40 +02:00
]);
2023-08-04 09:12:21 +02:00
/** @var \App\Models\VendorContact $contact */
2022-06-29 03:37:40 +02:00
$contact = VendorContact::factory()->create([
2023-08-04 09:12:21 +02:00
'user_id' => $user->id,
'company_id' => $user->company()->id,
2022-06-29 03:37:40 +02:00
'vendor_id' => $vendor->id,
'is_primary' => 1,
'send_email' => true,
]);
2023-08-04 09:12:21 +02:00
/** @var \App\Models\PurchaseOrder $purchase_order */
2022-06-29 03:37:40 +02:00
$purchase_order = PurchaseOrder::factory()->create([
2023-08-04 09:12:21 +02:00
'user_id' => $user->id,
'company_id' => $user->company()->id,
2022-06-29 03:37:40 +02:00
'vendor_id' => $vendor->id,
'terms' => 'Sample Terms',
'footer' => 'Sample Footer',
'public_notes' => 'Sample Public Notes',
]);
2023-08-04 09:12:21 +02:00
/** @var \App\Models\PurchaseOrderInvitation $invitation */
2022-06-29 03:37:40 +02:00
$invitation = PurchaseOrderInvitation::factory()->create([
2023-08-04 09:12:21 +02:00
'user_id' => $user->id,
'company_id' => $user->company()->id,
2022-06-29 03:37:40 +02:00
'purchase_order_id' => $purchase_order->id,
'vendor_contact_id' => $contact->id,
]);
$purchase_order->setRelation('invitations', $invitation);
$purchase_order->setRelation('vendor', $vendor);
2023-08-04 09:12:21 +02:00
$purchase_order->setRelation('company', $user->company());
2022-06-29 03:37:40 +02:00
$purchase_order->load('vendor.company');
$design_object = json_decode(json_encode(request()->input('design')));
if (! is_object($design_object)) {
return response()->json(['message' => 'Invalid custom design object'], 400);
}
$html = new VendorHtmlEngine($purchase_order->invitations()->first());
$design = new Design(Design::CUSTOM, ['custom_partials' => request()->design['design']]);
$state = [
'template' => $design->elements([
'client' => null,
'vendor' => $purchase_order->vendor,
'entity' => $purchase_order,
'pdf_variables' => (array) $purchase_order->company->settings->pdf_variables,
'products' => request()->design['design']['product'],
]),
'variables' => $html->generateLabelsAndValues(),
'process_markdown' => $purchase_order->company->markdown_enabled,
2023-10-26 08:13:06 +02:00
'options' => [
'vendor' => $invitation->purchase_order->vendor,
'purchase_orders' => [$invitation->purchase_order],
2023-10-26 08:13:06 +02:00
],
2022-06-29 03:37:40 +02:00
];
$maker = new PdfMaker($state);
$maker
->design($design)
->build();
2023-08-04 09:12:21 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
DB::connection($user->company()->db)->rollBack();
2022-06-29 03:37:40 +02:00
if (request()->query('html') == 'true') {
return $maker->getCompiledHTML();
}
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
2024-01-14 05:05:00 +01:00
return (new Phantom())->convertHtmlToPdf($maker->getCompiledHTML(true));
2022-06-29 03:37:40 +02:00
}
2023-02-16 02:36:09 +01:00
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
2022-06-29 03:37:40 +02:00
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
2023-08-04 09:12:21 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $user->company());
2022-06-29 03:37:40 +02:00
2023-02-16 02:36:09 +01:00
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
2022-06-29 03:37:40 +02:00
2023-02-16 02:36:09 +01:00
return $pdf;
2022-06-29 03:37:40 +02:00
}
2024-01-14 05:05:00 +01:00
2023-08-04 09:12:21 +02:00
$file_path = (new PreviewPdf($maker->getCompiledHTML(true), $user->company()))->handle();
2022-06-29 03:37:40 +02:00
$response = Response::make($file_path, 200);
$response->header('Content-Type', 'application/pdf');
return $response;
}
}