Untitled

mail@pastecode.io avatar
unknown
plain_text
13 days ago
1.6 kB
2
Indexable
Never
 public function exportPdf(Request $request)
    {
        $client = new Client();
        $token  = Session::get('auth') ?? null;

        $rangeDate  = $request->input('range_date');
        $partnerId  = $request->input('filter.partner_id');
        $productId  = $request->input('filter.product_id');
        $state      = $request->input('filter.state');
    
        $queryString = http_build_query([
            'range_date' => $rangeDate,
            'filter' => [
                'partner_id' => $partnerId,
                'product_id' => $productId,
                'state'      => $state
            ]
        ]);
    
        // Lakukan permintaan API dengan query string yang terbangun
        $response = $client->get(env('VITE_API_URL', '') . '/api/transaction-live/show?' . $queryString, [
            'headers' => [
                'Authorization' => 'Bearer ' . $token['auth']['access_token'] ?? '',
            ],
        ]);
    
        $responseData = json_decode($response->getBody(), true);
    
        if ($responseData['success'] && isset($responseData['result']['data'])) {
            $data   = $responseData['result']['data'];
            $totals = $this->calculateTotals($data);
            $pdf = $this->generatePdf($data, $totals);
            $response = Response::make($pdf, 200);
            $response->header('Content-Type', 'application/pdf');
            $response->header('Content-Disposition', 'attachment; filename=Rekap Data Laporan Transaksi.pdf');
    
            return $response;
        } else {
            return response()->json(['error' => 'Failed to retrieve data'], 500);
        }
    }
Leave a Comment