From 5eb24878f9c0fc2c87e10fbe6a4c05038fa182cb Mon Sep 17 00:00:00 2001 From: ahmedsophyudden Date: Sun, 27 Feb 2022 23:09:12 +0800 Subject: [PATCH] Create download api for booking document (invoice, purchase order, deliver order, supplier deliver order) --- .../DownloadBookingDocumentLogic.php | 94 +++++++++++++++++++ .../DownloadBookingDocumentController.php | 20 ++++ routes/booking.php | 2 + 3 files changed, 116 insertions(+) create mode 100644 app/Classes/Modules/Bookings/ControllersLogic/DownloadBookingDocumentLogic.php create mode 100644 app/Http/Controllers/Bookings/DownloadBookingDocumentController.php diff --git a/app/Classes/Modules/Bookings/ControllersLogic/DownloadBookingDocumentLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/DownloadBookingDocumentLogic.php new file mode 100644 index 00000000..3c3459e2 --- /dev/null +++ b/app/Classes/Modules/Bookings/ControllersLogic/DownloadBookingDocumentLogic.php @@ -0,0 +1,94 @@ + 'Download Booking Document', + 'message' => 'You have successfully retrieved a Booking Document' + ]; + } + + /** + * DownloadBookingDocumentLogic constructor. + */ + public function __construct() + { + } + + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\AccessForbiddenException + * @throws \App\Classes\Exceptions\RequestValidationException + */ + public function logic(Request $request): JsonResponse + { + + Auth::login(User::findOrFail(1)); + $zip_file = 'do_' . Carbon::yesterday()->format('Y_m_d') . '.zip'; + $attachment = storage_path() . '/' . $zip_file; + + $zip = new ZipArchive(); + if ($zip->open($attachment, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) { + + $bookings = Booking::where('status', ApprovalStatus::COMPLETED)->whereDate('updated_at', '>=', $request->input('startDate'))->whereDate('updated_at', '<=', $request->input('endDate')) + ->whereHas('transactions', function ($query) { + return $query->where('type', TransactionType::PAYMENT)->whereHas('transactions', function ($query) { + return $query->where('issuer', 2); + }); + })->get(); + + if (!count($bookings)) throw new MalformedRequestException('No available file to download'); + + foreach ($bookings as $booking) { + $file = $booking->documents()->where('document_type', $request->input('type'))->first()->files()->first(); + $zip->addFile(Storage::disk('documents')->path($file->file->file_info->original->file), $booking->created_at->format('d_m_Y') . '_' . $booking->marking . '.pdf'); + } + + $zip->close(); + + while (ob_get_level()) { + ob_end_clean(); + } + ob_start(); + header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK'); + header("Content-Type: application/zip"); + header("Content-Transfer-Encoding: Binary"); + header("Content-Length: " . filesize($attachment)); + header('Pragma: no-cache'); + header("Content-Disposition: attachment; filename=\"" . basename($attachment) . "\""); + ob_flush(); + ob_clean(); + + readfile($attachment); + File::delete($attachment); + + exit; + + } + } +} diff --git a/app/Http/Controllers/Bookings/DownloadBookingDocumentController.php b/app/Http/Controllers/Bookings/DownloadBookingDocumentController.php new file mode 100644 index 00000000..461e48fc --- /dev/null +++ b/app/Http/Controllers/Bookings/DownloadBookingDocumentController.php @@ -0,0 +1,20 @@ +execute($request); + } + +} \ No newline at end of file diff --git a/routes/booking.php b/routes/booking.php index d83bb4af..2087d0fa 100644 --- a/routes/booking.php +++ b/routes/booking.php @@ -32,4 +32,6 @@ Route::group(['prefix' => 'booking', 'as' => 'booking.', 'namespace' => 'Booking Route::post('{id}/proforma/create', 'CreateProformaInvoiceTransaction@create')->name('proforma.create'); + Route::get('/document/download', 'DownloadBookingDocumentController@download')->name('download'); + }); \ No newline at end of file