diff --git a/app/Http/Controllers/PreviewController.php b/app/Http/Controllers/PreviewController.php index c9b3830baae3..1c9b66fcd6ad 100644 --- a/app/Http/Controllers/PreviewController.php +++ b/app/Http/Controllers/PreviewController.php @@ -182,7 +182,6 @@ class PreviewController extends BaseController return response()->json(['message' => 'This server cannot handle this request.'], 400); } - $stub = new StubBuilder(auth()->user()->company(), auth()->user()); $stub->setEntityType($request->entity_type) ->setSettings($request->settings) @@ -197,177 +196,6 @@ class PreviewController extends BaseController return $response; } - public function oldDesign(DesignPreviewRequest $request) - { - if (Ninja::isHosted() && !in_array($request->getHost(), ['preview.invoicing.co','staging.invoicing.co'])) { - return response()->json(['message' => 'This server cannot handle this request.'], 400); - } - - $company = auth()->user()->company(); - - MultiDB::setDb($company->db); - - if ($request->input('entity') == 'quote') { - $repo = new QuoteRepository(); - $entity_obj = QuoteFactory::create($company->id, auth()->user()->id); - $class = Quote::class; - } elseif ($request->input('entity') == 'credit') { - $repo = new CreditRepository(); - $entity_obj = CreditFactory::create($company->id, auth()->user()->id); - $class = Credit::class; - } elseif ($request->input('entity') == 'recurring_invoice') { - $repo = new RecurringInvoiceRepository(); - $entity_obj = RecurringInvoiceFactory::create($company->id, auth()->user()->id); - $class = RecurringInvoice::class; - } else { //assume it is either an invoice or a null object - $repo = new InvoiceRepository(); - $entity_obj = InvoiceFactory::create($company->id, auth()->user()->id); - $class = Invoice::class; - } - - try { - DB::connection(config('database.default'))->beginTransaction(); - - if ($request->has('entity_id')) { - $entity_obj = $class::on(config('database.default')) - ->with('client.company') - ->where('id', $this->decodePrimaryKey($request->input('entity_id'))) - ->where('company_id', $company->id) - ->withTrashed() - ->first(); - } - - if ($request->has('client_id') && strlen($request->client_id) > 4) { - $client = Client::withTrashed()->find($this->decodePrimaryKey($request->client_id)); - if ($request->settings_type == 'client' && $client) { - $client->settings = $request->settings; - $client->save(); - } - } - - if ($request->has('group_id')) { - $group = GroupSetting::withTrashed()->find($this->decodePrimaryKey($request->group_id)); - if ($request->settings_type == 'group') { - $group->settings = $request->settings; - $group->save(); - } - } - - if ($request->settings_type == 'company') { - $company->settings = $request->settings; - $company->save(); - } - - if ($request->has('footer') && !$request->filled('footer') && $request->input('entity') == 'recurring_invoice') { - $request->merge(['footer' => $company->settings->invoice_footer]); - } - - if ($request->has('terms') && !$request->filled('terms') && $request->input('entity') == 'recurring_invoice') { - $request->merge(['terms' => $company->settings->invoice_terms]); - } - - // $entity_obj = $repo->save($request->all(), $entity_obj); - - if (! $request->has('entity_id')) { - $entity_obj->service()->fillDefaults()->save(); - } - - App::forgetInstance('translator'); - $t = app('translator'); - App::setLocale($entity_obj->client->locale()); - $t->replace(Ninja::transformTranslations($entity_obj->client->getMergedSettings())); - - $html = new HtmlEngine($entity_obj->invitations()->first()); - - $design = \App\Models\Design::find($entity_obj->design_id); - - /* Catch all in case migration doesn't pass back a valid design */ - if (! $design) { - $design = \App\Models\Design::find(2); - } - - 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' => $entity_obj->client, - 'entity' => $entity_obj, - 'pdf_variables' => (array) $entity_obj->company->settings->pdf_variables, - '$product' => $design->design->product, - 'variables' => $variables, - ]), - 'variables' => $variables, - 'options' => [ - 'all_pages_header' => $entity_obj->client->getSetting('all_pages_header'), - 'all_pages_footer' => $entity_obj->client->getSetting('all_pages_footer'), - ], - 'process_markdown' => $entity_obj->client->company->markdown_enabled, - ]; - - $maker = new PdfMaker($state); - - $maker - ->design($template) - ->build(); - - DB::connection(config('database.default'))->rollBack(); - - if (request()->query('html') == 'true') { - nlog($maker->getCompiledHTML()); - return $maker->getCompiledHTML(); - } - } catch(\Exception $e) { - nlog($e->getMessage()); - DB::connection(config('database.default'))->rollBack(); - - return; - } - - //if phantom js...... inject here..et - if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') { - $pdf = (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true)); - - $headers = ['Content-Type' => 'application/pdf']; - - if (request()->input('inline') == 'true') { - $headers = array_merge($headers, ['Content-Disposition' => 'inline']); - } - - return response()->streamDownload(function () use ($pdf) { - echo $pdf; - }, "preview.pdf", $headers); - } - - if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') { - $pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true)); - - $headers = ['Content-Type' => 'application/pdf']; - - if (request()->input('inline') == 'true') { - $headers = array_merge($headers, ['Content-Disposition' => 'inline']); - } - - return response()->streamDownload(function () use ($pdf) { - echo $pdf; - }, "preview.pdf", $headers); - } - - $file_path = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle(); - $response = Response::make($file_path, 200); - $response->header('Content-Type', 'application/pdf'); - - return $response; - } - public function live(PreviewInvoiceRequest $request) { if (Ninja::isHosted() && !in_array($request->getHost(), ['preview.invoicing.co','staging.invoicing.co'])) { diff --git a/app/Services/Pdf/PdfMock.php b/app/Services/Pdf/PdfMock.php index af9e55ec2270..0bd107121e37 100644 --- a/app/Services/Pdf/PdfMock.php +++ b/app/Services/Pdf/PdfMock.php @@ -11,40 +11,62 @@ namespace App\Services\Pdf; +use App\Models\Quote; use App\Models\Client; +use App\Models\Credit; +use App\Models\Vendor; use App\Models\Account; use App\Models\Company; use App\Models\Invoice; +use App\Models\PurchaseOrder; use App\Models\InvoiceInvitation; class PdfMock { - - public function __construct() - { - - } + + public function __construct(public mixed $entity_type) + {} public function build() { - $mock = Invoice::factory()->make(); - $mock->client = Client::factory()->make(); - $mock->tax_map = $this->getTaxMap(); - $mock->total_tax_map = $this->getTotalTaxMap(); - $mock->invitation = InvoiceInvitation::factory()->make(); - $mock->invitation->company = Company::factory()->make(); - $mock->invitation->company->account = Account::factory()->make(); + $mock = $this->initEntity(); + return $mock; } + private function initEntity(): mixed + { + match ($this->entity_type) { + Invoice::class => $entity = Invoice::factory()->make(), + Quote::class => $entity = Quote::factory()->make(), + Credit::class => $entity = Credit::factory()->make(), + PurchaseOrder::class => $entity = PurchaseOrder::factory()->make(), + default => $entity = Invoice::factory()->make() + }; + + if($this->entity_type == PurchaseOrder::class){ + $entity->vendor = Vendor::factory()->make(); + } + else{ + $entity->client = Client::factory()->make(); + } + + $entity->tax_map = $this->getTaxMap(); + $entity->total_tax_map = $this->getTotalTaxMap(); + $entity->invitation = InvoiceInvitation::factory()->make(); + $entity->invitation->company = Company::factory()->make(); + $entity->invitation->company->account = Account::factory()->make(); + + return $entity; + + } + private function getTaxMap() { - return collect( [['name' => 'GST', 'total' => 10]]); - } private function getTotalTaxMap() diff --git a/app/Services/Preview/StubBuilder.php b/app/Services/Preview/StubBuilder.php index e8ba8b845ffc..c17d3de593ba 100644 --- a/app/Services/Preview/StubBuilder.php +++ b/app/Services/Preview/StubBuilder.php @@ -32,6 +32,7 @@ use App\Utils\Traits\Pdf\PageNumbering; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; +//@deprecated version class StubBuilder { use PageNumbering; diff --git a/tests/Pdf/PdfmockTest.php b/tests/Pdf/PdfmockTest.php index ee66688657f0..5b919ed5ca03 100644 --- a/tests/Pdf/PdfmockTest.php +++ b/tests/Pdf/PdfmockTest.php @@ -40,7 +40,7 @@ class PdfmockTest extends TestCase public function testPdfInstance () { - $entity = (new \App\Services\Pdf\PdfMock())->build(); + $entity = (new \App\Services\Pdf\PdfMock(Invoice::class))->build(); $this->assertInstanceOf(Invoice::class, $entity); $this->assertNotNull($entity->client); @@ -59,7 +59,7 @@ class PdfmockTest extends TestCase public function testHtmlGeneration() { - $pdf_mock = (new PdfMock()); + $pdf_mock = (new PdfMock(Invoice::class)); $mock = $pdf_mock->build(); $pdf_service = new PdfService($mock->invitation);