2020-03-05 08:14:57 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-03-05 08:14:57 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Jobs\Util\PreviewPdf;
|
2020-10-05 10:16:36 +02:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\InvoiceInvitation;
|
2020-09-04 13:17:30 +02:00
|
|
|
use App\Services\PdfMaker\Design;
|
2020-08-21 16:47:17 +02:00
|
|
|
use App\Services\PdfMaker\PdfMaker;
|
|
|
|
use App\Utils\HtmlEngine;
|
2020-12-13 00:20:03 +01:00
|
|
|
use App\Utils\Ninja;
|
2020-11-27 03:24:13 +01:00
|
|
|
use App\Utils\PhantomJS\Phantom;
|
2020-03-05 08:14:57 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use App\Utils\Traits\MakesInvoiceHtml;
|
2020-12-13 00:20:03 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
2020-12-16 12:52:40 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-12-13 00:20:03 +01:00
|
|
|
use Illuminate\Support\Facades\Lang;
|
2020-12-16 12:52:40 +01:00
|
|
|
use Illuminate\Support\Facades\Response;
|
2020-03-05 08:14:57 +01:00
|
|
|
|
|
|
|
class PreviewController extends BaseController
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use MakesInvoiceHtml;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Returns a template filled with entity variables.
|
2020-03-05 08:14:57 +01:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/preview",
|
|
|
|
* operationId="getPreview",
|
|
|
|
* tags={"preview"},
|
|
|
|
* summary="Returns a pdf preview",
|
|
|
|
* description="Returns a pdf preview.",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="The pdf response",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-03-05 08:14:57 +01:00
|
|
|
* @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()
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
if (request()->has('entity') &&
|
|
|
|
request()->has('entity_id') &&
|
2020-09-06 11:38:10 +02:00
|
|
|
! empty(request()->input('entity')) &&
|
|
|
|
! empty(request()->input('entity_id')) &&
|
2020-03-21 06:37:30 +01:00
|
|
|
request()->has('body')) {
|
2020-03-12 12:04:53 +01:00
|
|
|
$design_object = json_decode(json_encode(request()->input('design')));
|
2020-03-09 12:12:45 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! is_object($design_object)) {
|
2020-03-09 12:12:45 +01:00
|
|
|
return response()->json(['message' => 'Invalid custom design object'], 400);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-09 12:12:45 +01:00
|
|
|
|
2020-03-05 08:14:57 +01:00
|
|
|
$entity = ucfirst(request()->input('entity'));
|
|
|
|
|
|
|
|
$class = "App\Models\\$entity";
|
|
|
|
|
|
|
|
$pdf_class = "App\Jobs\\$entity\\Create{$entity}Pdf";
|
|
|
|
|
|
|
|
$entity_obj = $class::whereId($this->decodePrimaryKey(request()->input('entity_id')))->company()->first();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $entity_obj) {
|
2020-03-05 08:14:57 +01:00
|
|
|
return $this->blankEntity();
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-05 08:14:57 +01:00
|
|
|
|
|
|
|
$entity_obj->load('client');
|
|
|
|
|
2020-12-13 00:20:03 +01:00
|
|
|
App::setLocale($entity_obj->client->primary_contact()->preferredLocale());
|
|
|
|
App::forgetInstance('translator');
|
|
|
|
Lang::replace(Ninja::transformTranslations($entity_obj->client->getMergedSettings()));
|
|
|
|
|
2020-10-27 12:57:12 +01:00
|
|
|
$html = new HtmlEngine($entity_obj->invitations()->first());
|
2020-03-05 08:14:57 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$design_namespace = 'App\Services\PdfMaker\Designs\\'.request()->design['name'];
|
2020-08-21 16:47:17 +02:00
|
|
|
|
|
|
|
$design_class = new $design_namespace();
|
|
|
|
|
|
|
|
$state = [
|
|
|
|
'template' => $design_class->elements([
|
|
|
|
'client' => $entity_obj->client,
|
|
|
|
'entity' => $entity_obj,
|
|
|
|
'pdf_variables' => (array) $entity_obj->company->settings->pdf_variables,
|
2020-10-06 12:49:00 +02:00
|
|
|
'products' => request()->design['design']['product'],
|
2020-08-21 16:47:17 +02:00
|
|
|
]),
|
|
|
|
'variables' => $html->generateLabelsAndValues(),
|
|
|
|
];
|
|
|
|
|
2020-09-04 13:17:30 +02:00
|
|
|
$design = new Design(request()->design['name']);
|
2020-08-21 16:47:17 +02:00
|
|
|
$maker = new PdfMaker($state);
|
|
|
|
|
|
|
|
$maker
|
2020-09-04 13:17:30 +02:00
|
|
|
->design($design)
|
2020-08-21 16:47:17 +02:00
|
|
|
->build();
|
|
|
|
|
2020-12-21 10:58:55 +01:00
|
|
|
if (request()->query('html') == true) {
|
2020-12-11 21:51:10 +01:00
|
|
|
return $maker->getCompiledHTML;
|
2020-12-16 12:52:40 +01:00
|
|
|
}
|
2020-12-11 21:51:10 +01:00
|
|
|
|
2020-11-27 03:02:05 +01:00
|
|
|
//if phantom js...... inject here..
|
|
|
|
if (config('ninja.phantomjs_pdf_generation')) {
|
|
|
|
return (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
//else
|
2020-09-04 13:17:30 +02:00
|
|
|
$file_path = PreviewPdf::dispatchNow($maker->getCompiledHTML(true), auth()->user()->company());
|
2020-03-05 08:14:57 +01:00
|
|
|
|
|
|
|
return response()->download($file_path)->deleteFileAfterSend(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->blankEntity();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function blankEntity()
|
|
|
|
{
|
2020-12-13 00:20:03 +01:00
|
|
|
App::forgetInstance('translator');
|
|
|
|
Lang::replace(Ninja::transformTranslations(auth()->user()->company()->settings));
|
|
|
|
|
2020-07-05 13:39:59 +02:00
|
|
|
DB::beginTransaction();
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
$client = Client::factory()->create([
|
2020-03-05 08:50:50 +01:00
|
|
|
'user_id' => auth()->user()->id,
|
|
|
|
'company_id' => auth()->user()->company()->id,
|
|
|
|
]);
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
$contact = ClientContact::factory()->create([
|
2020-03-05 08:50:50 +01:00
|
|
|
'user_id' => auth()->user()->id,
|
|
|
|
'company_id' => auth()->user()->company()->id,
|
|
|
|
'client_id' => $client->id,
|
|
|
|
'is_primary' => 1,
|
|
|
|
'send_email' => true,
|
|
|
|
]);
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
$invoice = Invoice::factory()->create([
|
2020-03-05 08:50:50 +01:00
|
|
|
'user_id' => auth()->user()->id,
|
|
|
|
'company_id' => auth()->user()->company()->id,
|
|
|
|
'client_id' => $client->id,
|
|
|
|
]);
|
2020-08-04 13:00:19 +02:00
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
$invitation = InvoiceInvitation::factory()->create([
|
2020-08-04 10:37:28 +02:00
|
|
|
'user_id' => auth()->user()->id,
|
|
|
|
'company_id' => auth()->user()->company()->id,
|
|
|
|
'invoice_id' => $invoice->id,
|
|
|
|
'client_contact_id' => $contact->id,
|
|
|
|
]);
|
2020-03-05 08:50:50 +01:00
|
|
|
|
2020-08-04 10:37:28 +02:00
|
|
|
$invoice->setRelation('invitations', $invitation);
|
2020-03-21 06:37:30 +01:00
|
|
|
$invoice->setRelation('client', $client);
|
|
|
|
$invoice->setRelation('company', auth()->user()->company());
|
|
|
|
$invoice->load('client');
|
2020-03-07 23:15:11 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
// info(print_r($invoice->toArray(),1));
|
2020-08-04 10:37:28 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$design_object = json_decode(json_encode(request()->input('design')));
|
2020-03-07 23:15:11 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! is_object($design_object)) {
|
2020-03-21 06:37:30 +01:00
|
|
|
return response()->json(['message' => 'Invalid custom design object'], 400);
|
|
|
|
}
|
2020-03-05 08:50:50 +01:00
|
|
|
|
2020-10-27 12:57:12 +01:00
|
|
|
$html = new HtmlEngine($invoice->invitations()->first());
|
2020-08-28 09:51:02 +02:00
|
|
|
|
2020-09-09 14:47:26 +02:00
|
|
|
$design = new Design(Design::CUSTOM, ['custom_partials' => request()->design['design']]);
|
2020-08-28 09:51:02 +02:00
|
|
|
|
|
|
|
$state = [
|
2020-09-04 13:17:30 +02:00
|
|
|
'template' => $design->elements([
|
2020-08-28 09:51:02 +02:00
|
|
|
'client' => $invoice->client,
|
|
|
|
'entity' => $invoice,
|
|
|
|
'pdf_variables' => (array) $invoice->company->settings->pdf_variables,
|
2020-10-06 12:49:00 +02:00
|
|
|
'products' => request()->design['design']['product'],
|
2020-08-28 09:51:02 +02:00
|
|
|
]),
|
|
|
|
'variables' => $html->generateLabelsAndValues(),
|
|
|
|
];
|
2020-03-05 08:50:50 +01:00
|
|
|
|
2020-08-28 09:51:02 +02:00
|
|
|
$maker = new PdfMaker($state);
|
|
|
|
|
|
|
|
$maker
|
2020-09-04 13:17:30 +02:00
|
|
|
->design($design)
|
2020-08-28 09:51:02 +02:00
|
|
|
->build();
|
|
|
|
|
2020-11-27 12:08:42 +01:00
|
|
|
if (config('ninja.phantomjs_pdf_generation')) {
|
|
|
|
return (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true));
|
|
|
|
}
|
2020-11-27 03:23:02 +01:00
|
|
|
|
2020-09-04 13:17:30 +02:00
|
|
|
$file_path = PreviewPdf::dispatchNow($maker->getCompiledHTML(true), auth()->user()->company());
|
2020-03-05 08:50:50 +01:00
|
|
|
|
2020-07-05 13:39:59 +02:00
|
|
|
DB::rollBack();
|
2020-03-05 08:14:57 +01:00
|
|
|
|
2020-07-05 12:58:30 +02:00
|
|
|
$response = Response::make($file_path, 200);
|
|
|
|
$response->header('Content-Type', 'application/pdf');
|
|
|
|
|
2020-08-28 09:51:02 +02:00
|
|
|
return $response;
|
2020-03-05 08:14:57 +01:00
|
|
|
}
|
|
|
|
}
|