refactor for download routes

This commit is contained in:
David Bomba 2023-11-16 11:36:14 +11:00
parent ce4f808945
commit 86015c66e0
4 changed files with 75 additions and 18 deletions

View File

@ -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);

View File

@ -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']);
}

View File

@ -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

View File

@ -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();