first(); $start_date = Carbon::create($fields['start_date']); $end_date = Carbon::create($fields['end_date']); $response = $this->fetchApi( passport_serie: $model->passport_serie, passport_id: $model->passport_id, card_number_masked: Str::mask($model->card_number, '*', 6, 6), card_expire_date: $model->card_month.'/'.substr($model->card_year, 2), start_date: $start_date->format('d.m.Y'), end_date: $end_date->format('d.m.Y'), ); /** @var ResponseTypes\AzatApiClientInfoAllResponse */ $data = Str::isJson($response) ? json_decode($response) : emptyClass(errCode: 1, message: 'Connection issue to VP'); if ($data->errCode != 0) { return ActionResponse::danger($data->message); } $unique_folder_name = Str::snake(str_replace(':', '-', $model->created_at->toDateTimeString())); $dir = public_path("files/{$unique_folder_name}"); File::makeDirectory($dir, 0777, true, true); $fileDest = $dir."/{$model->id}.pdf"; $this->generateFile($data, $fileDest); return ActionResponse::openInNewTab(url("files/{$unique_folder_name}/{$model->id}.pdf")); } /** * Get the fields available on the action. * * @param \Laravel\Nova\Http\Requests\NovaRequest $request * @return array */ public function fields(NovaRequest $request): array { return [ Date::make(__('Start date'), 'start_date') ->default(date('Y-m-d', strtotime('-6 months'))) ->rules('required'), Date::make(__('End date'), 'end_date') ->default(date('Y-m-d')) ->rules('required'), ]; } /** * Fetch from internal API * * @param string $passport_serie * @param string $passport_id * @param string $card_number_masked * @param string $card_expire_date * @param string $start_date * @param string $end_date */ public function fetchApi( string $passport_serie, string $passport_id, string $card_number_masked, string $card_expire_date, string $start_date, string $end_date ) { $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => 'http://10.3.158.102:9999/api/clientinfo/all', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => sprintf('{ "idSeria": "%s", "idNo": "%s", "clientType": "recipient", "cardMaskNumber": "%s", "expDate": "%s", "fromDate" : "%s", "toDate" : "%s" }', $passport_serie, $passport_id, $card_number_masked, $card_expire_date, $start_date, $end_date), CURLOPT_HTTPHEADER => [ 'Authorization: Basic dGJ1c2VyOlFBWndzeDEyMw==', 'Content-Type: application/json', ], ]); $response = curl_exec($curl); curl_close($curl); return $response; } /** * Generate file * * @param ResponseTypes\AzatApiClientInfoAllResponse $data * @param string $fileDest */ public function generateFile($data, $fileDest) { $mpdf = new Mpdf; // Write HTML content $html = Blade::render('orders.cards.card-transaction.download-card-transaction', [ 'data' => $data, ]); $mpdf->WriteHTML($html); // Save the PDF to a file $mpdf->Output($fileDest, \Mpdf\Output\Destination::FILE); } }