Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ExportService.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Services;
  3. use App\Contracts\ExportableReport;
  4. use App\Models\Company;
  5. use Barryvdh\Snappy\Facades\SnappyPdf;
  6. use Illuminate\Support\Carbon;
  7. use Symfony\Component\HttpFoundation\StreamedResponse;
  8. class ExportService
  9. {
  10. public function exportToCsv(Company $company, ExportableReport $report, string $startDate, string $endDate, bool $separateCategoryHeaders = false): StreamedResponse
  11. {
  12. $formattedStartDate = Carbon::parse($startDate)->format('Y-m-d');
  13. $formattedEndDate = Carbon::parse($endDate)->format('Y-m-d');
  14. $timestamp = Carbon::now()->format('Y-m-d-H_i');
  15. $filename = $company->name . ' ' . $report->getTitle() . ' ' . $formattedStartDate . ' to ' . $formattedEndDate . ' ' . $timestamp . '.csv';
  16. $headers = [
  17. 'Content-Type' => 'text/csv',
  18. 'Content-Disposition' => 'attachment; filename="' . $filename . '"',
  19. ];
  20. $callback = function () use ($report, $company, $formattedStartDate, $formattedEndDate) {
  21. $file = fopen('php://output', 'wb');
  22. fputcsv($file, [$report->getTitle()]);
  23. fputcsv($file, [$company->name]);
  24. fputcsv($file, ['Date Range: ' . $formattedStartDate . ' to ' . $formattedEndDate]);
  25. fputcsv($file, []);
  26. fputcsv($file, $report->getHeaders());
  27. foreach ($report->getCategories() as $category) {
  28. if (isset($category->header[0]) && is_array($category->header[0])) {
  29. foreach ($category->header as $headerRow) {
  30. fputcsv($file, $headerRow);
  31. }
  32. } else {
  33. fputcsv($file, $category->header);
  34. }
  35. foreach ($category->data as $accountRow) {
  36. fputcsv($file, $accountRow);
  37. }
  38. if (filled($category->summary)) {
  39. fputcsv($file, $category->summary);
  40. }
  41. fputcsv($file, []); // Empty row for spacing
  42. }
  43. if (filled($report->getOverallTotals())) {
  44. fputcsv($file, $report->getOverallTotals());
  45. }
  46. fclose($file);
  47. };
  48. return response()->streamDownload($callback, $filename, $headers);
  49. }
  50. public function exportToPdf(Company $company, ExportableReport $report, string $startDate, string $endDate): StreamedResponse
  51. {
  52. $formattedStartDate = Carbon::parse($startDate)->format('Y-m-d');
  53. $formattedEndDate = Carbon::parse($endDate)->format('Y-m-d');
  54. $timestamp = Carbon::now()->format('Y-m-d-H_i');
  55. $filename = $company->name . ' ' . $report->getTitle() . ' ' . $formattedStartDate . ' to ' . $formattedEndDate . ' ' . $timestamp . '.pdf';
  56. $pdf = SnappyPdf::loadView($report->getPdfView(), [
  57. 'company' => $company,
  58. 'report' => $report,
  59. 'startDate' => Carbon::parse($startDate)->format('M d, Y'),
  60. 'endDate' => Carbon::parse($endDate)->format('M d, Y'),
  61. ]);
  62. return response()->streamDownload(function () use ($pdf) {
  63. echo $pdf->inline();
  64. }, $filename);
  65. }
  66. }