Utils.php 4.8 KB

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