Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AccountBalancesExportService.php 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Services;
  3. use App\DTO\AccountBalanceReportDTO;
  4. use App\Models\Company;
  5. use Symfony\Component\HttpFoundation\StreamedResponse;
  6. class AccountBalancesExportService
  7. {
  8. public function exportToCsv(Company $company, AccountBalanceReportDTO $accountBalanceReport, string $startDate, string $endDate): StreamedResponse
  9. {
  10. // Construct the filename
  11. $filename = $company->name . ' Account Balances ' . $startDate . ' to ' . $endDate . '.csv';
  12. $headers = [
  13. 'Content-Type' => 'text/csv',
  14. 'Content-Disposition' => 'attachment; filename="' . $filename . '"',
  15. ];
  16. $callback = static function () use ($company, $startDate, $endDate, $accountBalanceReport) {
  17. $file = fopen('php://output', 'wb');
  18. fputcsv($file, ['Account Balances']);
  19. fputcsv($file, [$company->name]);
  20. fputcsv($file, ['Date Range: ' . $startDate . ' to ' . $endDate]);
  21. fputcsv($file, []);
  22. fputcsv($file, ['ACCOUNT CODE', 'ACCOUNT', 'STARTING BALANCE', 'DEBIT', 'CREDIT', 'NET MOVEMENT', 'ENDING BALANCE']);
  23. foreach ($accountBalanceReport->categories as $accountCategoryName => $accountCategory) {
  24. fputcsv($file, ['', $accountCategoryName]);
  25. foreach ($accountCategory->accounts as $account) {
  26. fputcsv($file, [
  27. $account->accountCode,
  28. $account->accountName,
  29. $account->balance->startingBalance ?? '',
  30. $account->balance->debitBalance,
  31. $account->balance->creditBalance,
  32. $account->balance->netMovement,
  33. $account->balance->endingBalance ?? '',
  34. ]);
  35. }
  36. // Category Summary row
  37. fputcsv($file, [
  38. '',
  39. 'Total ' . $accountCategoryName,
  40. $accountCategory->summary->startingBalance ?? '',
  41. $accountCategory->summary->debitBalance,
  42. $accountCategory->summary->creditBalance,
  43. $accountCategory->summary->netMovement,
  44. $accountCategory->summary->endingBalance ?? '',
  45. ]);
  46. fputcsv($file, []);
  47. }
  48. // Final Row for overall totals
  49. fputcsv($file, [
  50. '',
  51. 'Total for all accounts',
  52. '',
  53. $accountBalanceReport->overallTotal->debitBalance,
  54. $accountBalanceReport->overallTotal->creditBalance,
  55. '',
  56. '',
  57. ]);
  58. fclose($file);
  59. };
  60. return response()->streamDownload($callback, $filename, $headers);
  61. }
  62. }