input('startDate'))->startOfDay(); $endDate = Carbon::createFromFormat('d-m-Y', $request->input('endDate'))->endOfDay(); $companyReference = Company::find($request->input('supplier'))->reference; $groups = Group::where('issuer', $request->input('supplier')) ->whereDate('created_at', '>=', $startDate) ->whereDate('created_at', '<=', $endDate) ->orderBy('created_at', 'DESC') ->get(); if (count($groups) > 0) { $zipDirectory = storage_path('app/bulk_whiteform'); // Update this with the actual directory path if (!file_exists($zipDirectory)) { mkdir($zipDirectory, 0755, true); } $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ $startDate = $startDate->format('d-m-Y'); $endDate = $endDate->format('d-m-Y'); $zipFileName = "currency_vendor_orders_{$startDate}_to_{$endDate}_{$companyReference}.zip"; $zip_file = "{$zipDirectory}/{$zipFileName}"; $zip = new ZipArchive(); if ($zip->open($zip_file, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) { foreach($groups as $group) { $document = $group->documents()->where('document_type', DocumentType::CURRENCY_VENDOR_ORDER)->whereNull('deleted_at')->first()->files()->first(); $created_at = $group->created_at->format('Y-m-d'); Log::info('BulkDownloadSupplierWhiteFormsLogic processing: ' . $document->file->file_info->original->file); if(!Storage::disk('s3')->exists($document->file->file_info->original->file)) { Log::info('BulkDownloadSupplierWhiteFormsLogic file does not exist: ' . $document->file->file_info->original->file); } else{ $fileContent = Storage::disk('s3')->get($document->file->file_info->original->file); $zip->addFromString('invoice-' . $created_at . "_" . $group->amount . "_" . $group->reference . '.pdf', $fileContent); } } $zip->close(); $filePathForS3 = 'bulk_whiteform/'.$zipFileName; Storage::disk('s3')->put($filePathForS3, file_get_contents($zip_file)); Log::info('BulkDownloadSupplierWhiteFormsLogic finished processing files to zip count: '.count($groups)); $temporaryUrl = Storage::disk('s3')->temporaryUrl( $filePathForS3, Carbon::now()->addMinutes(10) ); return response(['src' => $temporaryUrl ]); } else { return response()->json([ 'status' => 'Error', 'message' => 'Failed to create the Zip archive for supplier white forms.', ]); } } else{ $zip_file = "{$zipDirectory}/currency_vendor_orders_{$request->input('startDate')}_to_{$request->input('endDate')}_{$companyReference}.zip"; $zip = new ZipArchive(); if ($zip->open($zip_file, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) { foreach($groups as $group) { $document = $group->documents()->where('document_type', DocumentType::CURRENCY_VENDOR_ORDER)->whereNull('deleted_at')->first()->files()->first(); $created_at = $group->created_at->format('Y-m-d'); $zip->addFile(Storage::disk('documents')->path($document->file->file_info->original->file), $created_at . "_" . $group->amount . "_" . $group->reference . '.pdf'); } $zip->close(); while (ob_get_level()) { ob_end_clean(); } return response()->download($zip_file); } } } return response()->json([ 'status' => 'Failed', 'message' => 'No white form found for this supplier in the given date range.', ]); } catch (\Exception $e) { // Log the exception for debugging Log::error('Error in BulkDownloadSupplierWhiteFormsLogic: ' . $e->getMessage()); return response()->json([ 'status' => 'Error', 'message' => 'An error occurred while processing the request.', ]); } } }