選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

48 行
1.4KB

  1. <?php
  2. namespace domain\User\User;
  3. use common\helpers\CSV;
  4. use domain\_\AbstractGenerator;
  5. use yii\base\ErrorException;
  6. class UsersCreditCsvGenerator extends AbstractGenerator
  7. {
  8. protected UserSolver $userSolver;
  9. protected UserRepository $userRepository;
  10. public function loadDependencies(): void
  11. {
  12. $this->userSolver = $this->loadService(UserSolver::class);
  13. $this->userRepository = $this->loadService(UserRepository::class);
  14. }
  15. public function exportUsersCreditAsCsv(string $type)
  16. {
  17. if($type == 'negative') {
  18. $filename = 'Utilisateurs_credit_negatif.csv';
  19. $usersArray = $this->userRepository->findUsersWithNegativeCredit();
  20. }
  21. elseif($type == 'positive') {
  22. $filename = 'Utilisateurs_credit_positif.csv';
  23. $usersArray = $this->userRepository->findUsersWithPositiveCredit();
  24. }
  25. else {
  26. throw new ErrorException('Le paramètre $type est invalide.');
  27. }
  28. $data = [
  29. ['Client', 'Email', 'Téléphone', 'Cagnotte']
  30. ];
  31. foreach($usersArray as $user) {
  32. $data[] = [
  33. $this->userSolver->getUsernameFromArray($user, true),
  34. $user['email'],
  35. $user['phone'],
  36. CSV::formatNumber($user['credit']),
  37. ];
  38. }
  39. CSV::send($filename, $data);
  40. }
  41. }