您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ExportService.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Services;
  3. use App\Contracts\ExportableReport;
  4. use App\Models\Company;
  5. use App\Support\Column;
  6. use Barryvdh\Snappy\Facades\SnappyPdf;
  7. use Carbon\Exceptions\InvalidFormatException;
  8. use Illuminate\Support\Carbon;
  9. use Symfony\Component\HttpFoundation\StreamedResponse;
  10. class ExportService
  11. {
  12. public function exportToCsv(Company $company, ExportableReport $report, string $startDate, string $endDate): StreamedResponse
  13. {
  14. $formattedStartDate = Carbon::parse($startDate)->toDateString();
  15. $formattedEndDate = Carbon::parse($endDate)->toDateString();
  16. $timestamp = Carbon::now()->format('Y-m-d_H-i-s');
  17. $filename = $company->name . ' ' . $report->getTitle() . ' ' . $formattedStartDate . ' to ' . $formattedEndDate . ' ' . $timestamp . '.csv';
  18. $headers = [
  19. 'Content-Type' => 'text/csv',
  20. 'Content-Disposition' => 'attachment; filename="' . $filename . '"',
  21. ];
  22. $callback = function () use ($startDate, $endDate, $report, $company) {
  23. $file = fopen('php://output', 'wb');
  24. $defaultStartDateFormat = Carbon::parse($startDate)->toDefaultDateFormat();
  25. $defaultEndDateFormat = Carbon::parse($endDate)->toDefaultDateFormat();
  26. fputcsv($file, [$report->getTitle()]);
  27. fputcsv($file, [$company->name]);
  28. fputcsv($file, ['Date Range: ' . $defaultStartDateFormat . ' to ' . $defaultEndDateFormat]);
  29. fputcsv($file, []);
  30. fputcsv($file, $report->getHeaders());
  31. foreach ($report->getCategories() as $category) {
  32. if (isset($category->header[0]) && is_array($category->header[0])) {
  33. foreach ($category->header as $headerRow) {
  34. fputcsv($file, $headerRow);
  35. }
  36. } else {
  37. fputcsv($file, $category->header);
  38. }
  39. foreach ($category->data as $accountRow) {
  40. $row = [];
  41. $columns = $report->getColumns();
  42. /**
  43. * @var Column $column
  44. */
  45. foreach ($columns as $index => $column) {
  46. $cell = $accountRow[$index] ?? '';
  47. if ($column->isDate()) {
  48. try {
  49. $row[] = Carbon::parse($cell)->toDateString();
  50. } catch (InvalidFormatException) {
  51. $row[] = $cell;
  52. }
  53. } elseif (is_array($cell)) {
  54. // Handle array cells by extracting 'name' or 'description'
  55. $row[] = $cell['name'] ?? $cell['description'] ?? '';
  56. } else {
  57. $row[] = $cell;
  58. }
  59. }
  60. fputcsv($file, $row);
  61. }
  62. if (filled($category->summary)) {
  63. fputcsv($file, $category->summary);
  64. }
  65. fputcsv($file, []); // Empty row for spacing
  66. }
  67. if (filled($report->getOverallTotals())) {
  68. fputcsv($file, $report->getOverallTotals());
  69. }
  70. fclose($file);
  71. };
  72. return response()->streamDownload($callback, $filename, $headers);
  73. }
  74. public function exportToPdf(Company $company, ExportableReport $report, string $startDate, string $endDate): StreamedResponse
  75. {
  76. $formattedStartDate = Carbon::parse($startDate)->toDateString();
  77. $formattedEndDate = Carbon::parse($endDate)->toDateString();
  78. $timestamp = Carbon::now()->format('Y-m-d_H-i-s');
  79. $filename = $company->name . ' ' . $report->getTitle() . ' ' . $formattedStartDate . ' to ' . $formattedEndDate . ' ' . $timestamp . '.pdf';
  80. $pdf = SnappyPdf::loadView($report->getPdfView(), [
  81. 'company' => $company,
  82. 'report' => $report,
  83. 'startDate' => Carbon::parse($startDate)->toDefaultDateFormat(),
  84. 'endDate' => Carbon::parse($endDate)->toDefaultDateFormat(),
  85. ]);
  86. return response()->streamDownload(function () use ($pdf) {
  87. echo $pdf->inline();
  88. }, $filename);
  89. }
  90. }