From 86015c66e0ec81206177a178324599fa6069bf7e Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 16 Nov 2023 11:36:14 +1100 Subject: [PATCH] refactor for download routes --- .../ClientPortal/InvoiceController.php | 8 ++-- app/Http/Controllers/InvoiceController.php | 23 +++++----- app/Models/BaseModel.php | 16 +++++++ .../DownloadHistoricalInvoiceTest.php | 46 ++++++++++++++++++- 4 files changed, 75 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/InvoiceController.php b/app/Http/Controllers/ClientPortal/InvoiceController.php index 6216f41cfcae..871642b2747a 100644 --- a/app/Http/Controllers/ClientPortal/InvoiceController.php +++ b/app/Http/Controllers/ClientPortal/InvoiceController.php @@ -252,11 +252,9 @@ class InvoiceController extends Controller if ($invoices->count() == 1) { $invoice = $invoices->first(); - $file = $invoice->service()->getInvoicePdf(auth()->guard('contact')->user()); - - return response()->streamDownload(function () use ($file) { - echo Storage::get($file); - }, basename($file), ['Content-Type' => 'application/pdf']); + return response()->streamDownload(function () use ($invoice) { + echo $invoice->service()->getInvoicePdf(auth()->guard('contact')->user()); + }, $invoice->getFileName(), ['Content-Type' => 'application/pdf']); } return $this->buildZip($invoices); diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 0ebdf34896c4..728eccc15c0d 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -526,11 +526,11 @@ class InvoiceController extends BaseController } if ($action == 'download' && $invoices->count() >=1 && $user->can('view', $invoices->first())) { - $file = $invoices->first()->service()->getInvoicePdf(); + $filename = $invoices->first()->getFileName(); - nlog($filename); - return response()->streamDownload(function () use ($file) { - echo Storage::get($file); + + return response()->streamDownload(function () use($invoices) { + echo $invoices->first()->service()->getInvoicePdf(); }, $filename, ['Content-Type' => 'application/pdf']); } @@ -539,10 +539,10 @@ class InvoiceController extends BaseController return (new \App\Jobs\Entity\CreateRawPdf($invoice->invitations->first()))->handle(); }); - $merge = (new PdfMerge($paths->toArray()))->run(); + - return response()->streamDownload(function () use ($merge) { - echo($merge); + return response()->streamDownload(function () use ($paths) { + echo $merge = (new PdfMerge($paths->toArray()))->run(); }, 'print.pdf', ['Content-Type' => 'application/pdf']); } @@ -938,11 +938,10 @@ class InvoiceController extends BaseController */ public function deliveryNote(ShowInvoiceRequest $request, Invoice $invoice) { - $file = $invoice->service()->getInvoiceDeliveryNote($invoice, $invoice->invitations->first()->contact); - $file_name = ctrans('texts.delivery_note'). " " .$invoice->getFileName(); - return response()->streamDownload(function () use ($file) { - echo $file; - }, $file_name, ['Content-Type' => 'application/pdf']); + + return response()->streamDownload(function () use ($invoice) { + echo $invoice->service()->getInvoiceDeliveryNote($invoice, $invoice->invitations->first()->contact); + }, $invoice->getDeliveryNoteName(), ['Content-Type' => 'application/pdf']); } diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index e9f566b06835..083115035696 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -236,6 +236,22 @@ class BaseModel extends Model return $this->numberFormatter().'.'.$extension; } + public function getDeliveryNoteName($extension = 'pdf') + { + + $number = ctrans("texts.delivery_note"). "_" . $this->numberFormatter().'.'.$extension; + + $formatted_number = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $number); + + $formatted_number = mb_ereg_replace("([\.]{2,})", '', $formatted_number); + + $formatted_number = preg_replace('/\s+/', '_', $formatted_number); + + return \Illuminate\Support\Str::ascii($formatted_number); + + + } + /** * @param string $extension * @return string diff --git a/tests/Integration/DownloadHistoricalInvoiceTest.php b/tests/Integration/DownloadHistoricalInvoiceTest.php index acce97b9afa4..ca692976aade 100644 --- a/tests/Integration/DownloadHistoricalInvoiceTest.php +++ b/tests/Integration/DownloadHistoricalInvoiceTest.php @@ -21,7 +21,7 @@ use Tests\TestCase; /** * @test * @covers App\Http\Controllers\ActivityController - */ +*/ class DownloadHistoricalInvoiceTest extends TestCase { use MockAccountData; @@ -39,6 +39,50 @@ class DownloadHistoricalInvoiceTest extends TestCase } } + public function testDownloadInvoiceRoute() + { + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get("/api/v1/invoices/{$this->invoice->hashed_id}/download"); + + $response->assertStatus(200); + $response->assertDownload(); + + } + + public function testDownloadDeliveryRoute() + { + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get("/api/v1/invoices/{$this->invoice->hashed_id}/delivery_note"); + + $response->assertStatus(200); + $response->assertDownload(); + + } + + public function testDownloadInvoiceBulkActionRoute() + { + $data = [ + 'action' => 'download', + 'ids' => [$this->invoice->hashed_id], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post("/api/v1/invoices/bulk", $data); + + $response->assertStatus(200); + $response->assertDownload(); + + } + + private function mockActivity() { $activity_repo = new ActivityRepository();