You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExportService.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 = null, ?string $endDate = null): StreamedResponse
  13. {
  14. if ($startDate && $endDate) {
  15. $formattedStartDate = Carbon::parse($startDate)->toDateString();
  16. $formattedEndDate = Carbon::parse($endDate)->toDateString();
  17. $dateLabel = $formattedStartDate . ' to ' . $formattedEndDate;
  18. } else {
  19. $formattedAsOfDate = Carbon::parse($endDate)->toDateString();
  20. $dateLabel = $formattedAsOfDate;
  21. }
  22. $timestamp = Carbon::now()->format('Y-m-d_H-i-s');
  23. $filename = $company->name . ' ' . $report->getTitle() . ' ' . $dateLabel . ' ' . $timestamp . '.csv';
  24. $headers = [
  25. 'Content-Type' => 'text/csv',
  26. 'Content-Disposition' => 'attachment; filename="' . $filename . '"',
  27. ];
  28. $callback = function () use ($startDate, $endDate, $report, $company) {
  29. $file = fopen('php://output', 'wb');
  30. if ($startDate && $endDate) {
  31. $defaultStartDateFormat = Carbon::parse($startDate)->toDefaultDateFormat();
  32. $defaultEndDateFormat = Carbon::parse($endDate)->toDefaultDateFormat();
  33. $dateLabel = 'Date Range: ' . $defaultStartDateFormat . ' to ' . $defaultEndDateFormat;
  34. } else {
  35. $dateLabel = 'As of ' . Carbon::parse($endDate)->toDefaultDateFormat();
  36. }
  37. fputcsv($file, [$report->getTitle()]);
  38. fputcsv($file, [$company->name]);
  39. fputcsv($file, [$dateLabel]);
  40. fputcsv($file, []);
  41. fputcsv($file, $report->getHeaders());
  42. foreach ($report->getCategories() as $category) {
  43. if (isset($category->header[0]) && is_array($category->header[0])) {
  44. foreach ($category->header as $headerRow) {
  45. fputcsv($file, $headerRow);
  46. }
  47. } else {
  48. fputcsv($file, $category->header);
  49. }
  50. foreach ($category->data as $accountRow) {
  51. $row = [];
  52. $columns = $report->getColumns();
  53. /**
  54. * @var Column $column
  55. */
  56. foreach ($columns as $index => $column) {
  57. $cell = $accountRow[$index] ?? '';
  58. if ($column->isDate()) {
  59. try {
  60. $row[] = Carbon::parse($cell)->toDateString();
  61. } catch (InvalidFormatException) {
  62. $row[] = $cell;
  63. }
  64. } elseif (is_array($cell)) {
  65. // Handle array cells by extracting 'name' or 'description'
  66. $row[] = $cell['name'] ?? $cell['description'] ?? '';
  67. } else {
  68. $row[] = $cell;
  69. }
  70. }
  71. fputcsv($file, $row);
  72. }
  73. if (filled($category->summary)) {
  74. fputcsv($file, $category->summary);
  75. }
  76. fputcsv($file, []); // Empty row for spacing
  77. }
  78. if (filled($report->getOverallTotals())) {
  79. fputcsv($file, $report->getOverallTotals());
  80. }
  81. fclose($file);
  82. };
  83. return response()->streamDownload($callback, $filename, $headers);
  84. }
  85. public function exportToPdf(Company $company, ExportableReport $report, ?string $startDate = null, ?string $endDate = null): StreamedResponse
  86. {
  87. if ($startDate && $endDate) {
  88. $formattedStartDate = Carbon::parse($startDate)->toDateString();
  89. $formattedEndDate = Carbon::parse($endDate)->toDateString();
  90. $dateLabel = $formattedStartDate . ' to ' . $formattedEndDate;
  91. } else {
  92. $formattedAsOfDate = Carbon::parse($endDate)->toDateString();
  93. $dateLabel = $formattedAsOfDate;
  94. }
  95. $timestamp = Carbon::now()->format('Y-m-d_H-i-s');
  96. $filename = $company->name . ' ' . $report->getTitle() . ' ' . $dateLabel . ' ' . $timestamp . '.pdf';
  97. $pdf = SnappyPdf::loadView($report->getPdfView(), [
  98. 'company' => $company,
  99. 'report' => $report,
  100. 'startDate' => $startDate ? Carbon::parse($startDate)->toDefaultDateFormat() : null,
  101. 'endDate' => $endDate ? Carbon::parse($endDate)->toDefaultDateFormat() : null,
  102. ]);
  103. return response()->streamDownload(function () use ($pdf) {
  104. echo $pdf->inline();
  105. }, $filename);
  106. }
  107. }