Utils.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Cotisation;
  4. use App\Entity\Organization\Organization;
  5. use App\Enum\Cotisation\AlertStateEnum;
  6. use App\Repository\Cotisation\CotisationApiResourcesRepository;
  7. use App\Repository\Network\NetworkOrganizationRepository;
  8. use App\Service\Organization\Utils as OrganizationUtils;
  9. use App\Tests\Service\Cotisation\UtilsTest;
  10. use App\Service\Network\Utils as NetworkUtils;
  11. /**
  12. * Class Utils : Service rassemblant des fonctions d'interrogation pour gérer des conditions dans les Cotisations
  13. * @package App\Service\Cotisation
  14. */
  15. class Utils
  16. {
  17. const MEMBERSHIP_WAITING = 495; // Affiliation in progress
  18. const MEMBERSHIP_NOPAYMENT = 517; // Waiting paiement
  19. const SUBMIT_IN_PROGRESS = 540; // Affiliation in progress
  20. function __construct(
  21. private NetworkUtils $networkUtils,
  22. private OrganizationUtils $organizationUtils,
  23. private NetworkOrganizationRepository $networkOrganizationRepository,
  24. private CotisationApiResourcesRepository $cotisationApiResourcesRepository
  25. )
  26. {
  27. }
  28. /**
  29. * Test si l'organisation est un dernier parent ET appartient à la CMF.
  30. * @param Organization $organization
  31. * @return bool
  32. * @see UtilsTest::testIsLastParentAndCMF()
  33. */
  34. public function isLastParentAndCMF(Organization $organization): bool
  35. {
  36. return $this->networkOrganizationRepository->isLastParent($organization) && $this->networkUtils->isCMF($organization);
  37. }
  38. /**
  39. * Test si l'organisation est une structure (non manager) ET appartient à la CMF
  40. * @param Organization $organization
  41. * @return bool
  42. * @see UtilsTest::testIsStructureAndCMF()
  43. */
  44. public function isStructureAndCMF(Organization $organization): bool
  45. {
  46. return $this->organizationUtils->isStructure($organization) && $this->networkUtils->isCMF($organization);
  47. }
  48. /**
  49. * Test si la structure est un manager ET qu'elle appartient à la CMF
  50. * @param Organization $organization
  51. * @return bool
  52. * @see UtilsTest::testIsManagerAndCMF()
  53. */
  54. public function isManagerAndCMF(Organization $organization): bool
  55. {
  56. return $this->organizationUtils->isManager($organization) && $this->networkUtils->isCMF($organization);
  57. }
  58. /**
  59. * Test si l'organisation est un manager ET un dernier parent ET appartient à la CMF
  60. * @param Organization $organization
  61. * @return bool
  62. * @see UtilsTest::testIsManagerAndLastParentAndCMF()
  63. */
  64. public function isManagerAndLastParentAndCMF(Organization $organization): bool
  65. {
  66. return $this->organizationUtils->isManager($organization) && $this->isLastParentAndCMF($organization);
  67. }
  68. /**
  69. * Test si l'organisation est un manager ET n'est pas un dernier parent ET appartient à la CMF
  70. * @param Organization $organization
  71. * @return bool
  72. * @see UtilsTest::testIsManagerAndNotLastParentAndCMF()
  73. */
  74. public function isManagerAndNotLastParentAndCMF(Organization $organization): bool
  75. {
  76. return $this->organizationUtils->isManager($organization) && !$this->isLastParentAndCMF($organization);
  77. }
  78. /**
  79. * Retourne le niveau d'alerte de l'appel de cotisation pour une année.
  80. * @param Organization $organization
  81. * @param int $year
  82. * @return string|null
  83. * @see UtilsTest::testGetAlertStateAffiliation()
  84. */
  85. public function getAlertState(Organization $organization, int $year)
  86. {
  87. $state = $this->cotisationApiResourcesRepository->getAffiliationState($organization->getId(), $year);
  88. $alertState = null;
  89. if ($state == self::MEMBERSHIP_WAITING || $state == self::SUBMIT_IN_PROGRESS) {
  90. $alertState = AlertStateEnum::AFFILIATION()->getValue();
  91. } else if ($state == self::MEMBERSHIP_NOPAYMENT) {
  92. $alertState = AlertStateEnum::INVOICE()->getValue();
  93. } else if ($this->cotisationApiResourcesRepository->isInsuranceNotDone($organization->getId(), $year)) {
  94. $alertState = AlertStateEnum::INSURANCE()->getValue();
  95. } else if ($this->cotisationApiResourcesRepository->isNotDGVCustomer($organization->getId())) {
  96. $alertState = AlertStateEnum::ADVERTISINGINSURANCE()->getValue();
  97. }
  98. return $alertState;
  99. }
  100. /**
  101. * Retourne dans quelle année de cotisation on est aujourd'hui
  102. * @return int
  103. * @throws \Exception
  104. * @see UtilsTest::testGetCurrentCotisationYear()
  105. */
  106. public function getCurrentCotisationYear(): int {
  107. $today = new \DateTime('now');
  108. $year = intval($today->format('Y'));
  109. $base_date = new \DateTime($year . '-09-01');
  110. $dateStart = new \DateTime($year . '-01-01');
  111. if ($today >= $dateStart && $today <= $base_date) {
  112. $cotisationYear = $year;
  113. } else {
  114. $cotisationYear = $year + 1;
  115. }
  116. return $cotisationYear;
  117. }
  118. }