From fcaee29be38b90c94ca2cbab520fbebaa60cfb15 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Tue, 18 Jun 2024 11:38:43 +0800 Subject: [PATCH] Export pdf file from Laravel Vapor through S3 bucket --- app/Classes/General/AWSS3Helper.php | 28 +++++++++++++++---- .../GenerateCreditNotePdfLogic.php | 12 +++++++- .../ExportAnalyticToExcelController.php | 4 +-- .../ExportCustomersToExcelController.php | 26 ++++++++--------- ...mersWalletTransactionToExcelController.php | 2 +- ...omerTransactionHistorySectionComponent.vue | 22 +++++++++++++-- .../CustomerWalletTransactionComponent.vue | 24 +++++++++++++--- routes/web.php | 19 +++++++++++-- 8 files changed, 106 insertions(+), 31 deletions(-) diff --git a/app/Classes/General/AWSS3Helper.php b/app/Classes/General/AWSS3Helper.php index 12cf6661..5d9ca856 100644 --- a/app/Classes/General/AWSS3Helper.php +++ b/app/Classes/General/AWSS3Helper.php @@ -2,19 +2,18 @@ namespace App\Classes\General; -use Illuminate\Http\Resources\Json\ResourceCollection; -use Illuminate\Support\Str; -use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; use Carbon\Carbon; +use Maatwebsite\Excel\Concerns\Exportable; class AWSS3Helper { /** - * @param string $methodName + * @param string $exportFileName + * @param Exportable $exportableObject * @return string */ - static function S3($exportFileName, $exportableObject){ + static function S3Exportable($exportFileName, $exportableObject){ //Step 1: Upload to S3 $filePathForS3 = 'temp/' . $exportFileName; $exportableObject->store($filePathForS3, 's3'); @@ -28,4 +27,23 @@ class AWSS3Helper return $temporaryUrl; } + /** + * @param string $exportFileName + * @param string|resource $contents + * @return string + */ + static function S3PDF($exportFileName, $contents){ + //Step 1: Upload to S3 + $filePathForS3 = 'temp/' . $exportFileName; + Storage::disk('s3')->put($filePathForS3, $contents); + + //Step 2: Return temporary URL from S3 + $temporaryUrl = Storage::disk('s3')->temporaryUrl( + $filePathForS3, + Carbon::now()->addMinutes(10) + ); + + return $temporaryUrl; + } + } diff --git a/app/Classes/Modules/Transactions/ControllersLogic/GenerateCreditNotePdfLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/GenerateCreditNotePdfLogic.php index 8476507a..e16bfb23 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/GenerateCreditNotePdfLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/GenerateCreditNotePdfLogic.php @@ -10,6 +10,8 @@ use Mccarlosen\LaravelMpdf\Facades\LaravelMpdf; use App\Classes\Modules\Companies\Services\FetchesCompany; use App\Classes\Modules\Transactions\Services\FetchesTransaction; use App\Classes\ValueObjects\Constants\TransactionType; +use App\Classes\General\AWSS3Helper; +use Illuminate\Support\Facades\Storage; class GenerateCreditNotePdfLogic { @@ -46,6 +48,14 @@ class GenerateCreditNotePdfLogic $pdf = LaravelMpdf::loadView('pages.pdfs.credit_note', ['transaction' => $transaction, 'booking' => $booking, 'supplier' => $supplier]); - return $pdf->stream('CreditNote.pdf'); + $exportFileName = 'CreditNote.pdf'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + $pdfContent = $pdf->output(); + AWSS3Helper::S3PDF($exportFileName, $pdfContent); + } + else{ + return $pdf->stream($exportFileName); + } } } diff --git a/app/Http/Controllers/Exports/ExportAnalyticToExcelController.php b/app/Http/Controllers/Exports/ExportAnalyticToExcelController.php index eeaad69c..eedf6821 100644 --- a/app/Http/Controllers/Exports/ExportAnalyticToExcelController.php +++ b/app/Http/Controllers/Exports/ExportAnalyticToExcelController.php @@ -29,7 +29,7 @@ class ExportAnalyticToExcelController $exportFileName = 'bookingData.csv'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsAnalyticBookingTransactions) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsAnalyticBookingTransactions) ]); } else{ $response = $exportsAnalyticBookingTransactions->download($exportFileName, Excel::CSV, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); @@ -42,7 +42,7 @@ class ExportAnalyticToExcelController $exportFileName = 'billingData.csv'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsAnalyticBillingTransactions) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsAnalyticBillingTransactions) ]); } else{ $response = $exportsAnalyticBillingTransactions->download($exportFileName, Excel::CSV, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); diff --git a/app/Http/Controllers/Exports/ExportCustomersToExcelController.php b/app/Http/Controllers/Exports/ExportCustomersToExcelController.php index 6c4d04a5..91ace417 100644 --- a/app/Http/Controllers/Exports/ExportCustomersToExcelController.php +++ b/app/Http/Controllers/Exports/ExportCustomersToExcelController.php @@ -41,7 +41,7 @@ class ExportCustomersToExcelController $exportFileName = 'customers.csv'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsCustomers) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsCustomers) ]); } else{ return $exportsCustomers->download($exportFileName, Excel::CSV, ['Content-Type' => 'text/csv']); @@ -52,7 +52,7 @@ class ExportCustomersToExcelController $exportFileName = 'CurrencyVendorOrder.xls'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportCurrencyVendorOrder) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportCurrencyVendorOrder) ]); } else{ $response = $exportCurrencyVendorOrder->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); @@ -65,7 +65,7 @@ class ExportCustomersToExcelController $exportFileName = 'transactions.csv'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]); } else{ return $exportsTransactions->download($exportFileName, Excel::CSV, ['Content-Type' => 'text/csv']); @@ -76,7 +76,7 @@ class ExportCustomersToExcelController $exportFileName = 'nullDebtor.xls'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsNullDebtors) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsNullDebtors) ]); } else{ $response = $exportsNullDebtors->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); @@ -90,7 +90,7 @@ class ExportCustomersToExcelController $exportFileName = 'payment-transactions.xls'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]); } else{ $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); @@ -104,7 +104,7 @@ class ExportCustomersToExcelController $exportFileName = 'white-form-transactions.xls'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]); } else{ $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); @@ -118,7 +118,7 @@ class ExportCustomersToExcelController $exportFileName = 'wallet-transactions.xls'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]); } else{ $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); @@ -132,7 +132,7 @@ class ExportCustomersToExcelController $exportFileName = 'invoice-transactions.xls'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]); } else{ $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); @@ -146,7 +146,7 @@ class ExportCustomersToExcelController $exportFileName = 'receipt-transactions.xls'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]); } else{ $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); @@ -159,7 +159,7 @@ class ExportCustomersToExcelController $exportFileName = 'bookingTransactions.xls'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsBookingTransactions) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsBookingTransactions) ]); } else{ $response = $exportsBookingTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); @@ -172,7 +172,7 @@ class ExportCustomersToExcelController $exportFileName = 'leadsData.xls'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsLeadsTransactions) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsLeadsTransactions) ]); } else{ $response = $exportsLeadsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); @@ -185,7 +185,7 @@ class ExportCustomersToExcelController $exportFileName = $request->input('fileName').'.xls'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsImportedInvoiceMappeds) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsImportedInvoiceMappeds) ]); } else{ $response = $exportsImportedInvoiceMappeds->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); @@ -198,7 +198,7 @@ class ExportCustomersToExcelController $exportFileName = $request->input('fileName').'.xls'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsImportedReceiptMappeds) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsImportedReceiptMappeds) ]); } else{ $response = $exportsImportedReceiptMappeds->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); diff --git a/app/Http/Controllers/Exports/ExportCustomersWalletTransactionToExcelController.php b/app/Http/Controllers/Exports/ExportCustomersWalletTransactionToExcelController.php index 7843a1af..353da00d 100644 --- a/app/Http/Controllers/Exports/ExportCustomersWalletTransactionToExcelController.php +++ b/app/Http/Controllers/Exports/ExportCustomersWalletTransactionToExcelController.php @@ -29,7 +29,7 @@ class ExportCustomersWalletTransactionToExcelController $exportFileName = $request->route('marking') . '-wallet-' . ($request->route('is_precise') == 'true' ? 'precise-' : '') . 'transaction-history.xls'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]); + return response([ 'src' => AWSS3Helper::S3Exportable($exportFileName, $exportsTransactions) ]); } else{ $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); diff --git a/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue b/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue index d1448193..7826998d 100644 --- a/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue +++ b/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue @@ -22,13 +22,13 @@
{{item.created_at}}
-
{{ convertTransactionType(item.type) }} {{ item.payment_reference ? ' - ' + item.payment_reference : '' }}
+
{{ convertTransactionType(item.type) }} {{ item.payment_reference ? ' - ' + item.payment_reference : '' }}
{{[5, 9].includes(parseFloat(item.type)) ? (Math.round((parseFloat(item.amount) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : ''}}
{{(Math.round(((parseFloat(item.amount) / parseFloat(item.currency_rate) + parseFloat(item.service_charge)) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}
{{[1, 11].includes(parseFloat(item.type)) ? '- ' + (Math.round((parseFloat(item.amount) + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : ''}}
{{remainingBalance(index)}}
-
+ @@ -119,7 +119,23 @@ export default { successHandler(response){ this.isLoading = false; this.transaction = response.payload.data; - } + }, + downloadTransaction(paramItem) { + let url = this.route('transaction.credit_note.download', paramItem.id); + if(window.LARAVEL_VAPOR_ENABLED){ + return fetch(url, { + method: 'GET', + responseType: 'json', + }).then(response => { + if (response.src) { + window.location.href = response.src; + } + }); + } + else{ + window.open(url, '_blank'); + } + }, } } diff --git a/resources/assets/vue/components/wallets/elements/CustomerWalletTransactionComponent.vue b/resources/assets/vue/components/wallets/elements/CustomerWalletTransactionComponent.vue index bf9864e1..5882610e 100644 --- a/resources/assets/vue/components/wallets/elements/CustomerWalletTransactionComponent.vue +++ b/resources/assets/vue/components/wallets/elements/CustomerWalletTransactionComponent.vue @@ -1,11 +1,11 @@ \ No newline at end of file + diff --git a/routes/web.php b/routes/web.php index 69b1b76d..224bb7ce 100644 --- a/routes/web.php +++ b/routes/web.php @@ -226,7 +226,14 @@ Route::post('/support', function (Request $request) { Route::get('/online_payment/redirect', 'Billplz\CallbackBillplzController@callback')->name('online_payment.redirect'); Route::get('/products', function (\App\Classes\Modules\Exports\Services\ExportsProducts $exportsProducts) { - return $exportsProducts->download('products.csv', Excel::CSV, ['Content-Type' => 'text/csv']); + $exportFileName = 'products.csv'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + return response([ 'src' => App\Classes\General\AWSS3Helper::S3Exportable($exportFileName, $exportsProducts) ]); + } + else{ + return $exportsProducts->download($exportFileName, Excel::CSV, ['Content-Type' => 'text/csv']); + } })->name('products.random.1'); //duplicate name Route::get('/fix_bills', function () { @@ -306,7 +313,15 @@ Route::get('/products', function (\App\Classes\Modules\Exports\Services\ExportsP $q->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED]); })->get(); dd($bookings->count()); - return $exportsProducts->download('products.csv', Excel::CSV, ['Content-Type' => 'text/csv']); + + $exportFileName = 'products.csv'; + $filesystemDriver = Storage::getDefaultDriver(); + if($filesystemDriver === 's3'){ + return response([ 'src' => App\Classes\General\AWSS3Helper::S3Exportable($exportFileName, $exportsProducts) ]); + } + else{ + return $exportsProducts->download('products.csv', Excel::CSV, ['Content-Type' => 'text/csv']); + } })->name('products.random.3'); Route::get('/auto-purchase-order-fill', 'Bookings\AutoPurchaseOrderFillController@auto')->name('assign');