Skip to content

Commit

Permalink
[Tags] Refactor adherent status tag generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Remg committed Oct 30, 2024
1 parent 7732d2c commit 657d6e1
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 27 deletions.
92 changes: 65 additions & 27 deletions src/Adherent/Tag/TagGenerator/AdherentStatusTagGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
use App\Adherent\Tag\TagEnum;
use App\Entity\Adherent;
use App\Membership\MembershipSourceEnum;
use App\Repository\Contribution\PaymentRepository;
use App\Repository\DonationRepository;

class AdherentStatusTagGenerator extends AbstractTagGenerator
{
public function __construct(private readonly DonationRepository $donationRepository)
{
public function __construct(
private readonly DonationRepository $donationRepository,
private readonly PaymentRepository $paymentRepository,
) {
}

public static function getDefaultPriority(): int
Expand All @@ -20,46 +23,81 @@ public static function getDefaultPriority(): int

public function generate(Adherent $adherent, array $previousTags): array
{
if (MembershipSourceEnum::LEGISLATIVE === $adherent->getSource()) {
return [TagEnum::SYMPATHISANT_ENSEMBLE2024];
}

if ($adherent->isBesoinDEuropeUser()) {
return [TagEnum::SYMPATHISANT_BESOIN_D_EUROPE];
}

if (\in_array($adherent->getSource(), [MembershipSourceEnum::AVECVOUS, MembershipSourceEnum::JEMENGAGE])) {
return [TagEnum::SYMPATHISANT_COMPTE_AVECVOUS_JEMENGAGE];
if ($adherent->isOtherPartyMembership()) {
return [TagEnum::SYMPATHISANT_AUTRE_PARTI];
}

$mainTag = null;

if (\count($adherent->getConfirmedPayments())) {
$mainTag = \sprintf(TagEnum::ADHERENT_YEAR_ELU_TAG_PATTERN, date('Y'));
}

$countCotisationByYear = $this->donationRepository->countCotisationByYearForAdherent($adherent);

if ($countTotalCotisation = \count($countCotisationByYear)) {
$currentYear = date('Y');
$lastYear = key($countCotisationByYear);

if ($lastYear == date('Y')) {
if ($lastYear == $currentYear) {
if (1 === $countTotalCotisation) {
$mainTag = \sprintf(TagEnum::ADHERENT_YEAR_PRIMO_TAG_PATTERN, $lastYear);
} else {
$mainTag = \sprintf(TagEnum::ADHERENT_YEAR_RECOTISATION_TAG_PATTERN, $lastYear);
return [sprintf(TagEnum::ADHERENT_YEAR_PRIMO_TAG_PATTERN, $currentYear)];
}
} elseif (null === $mainTag) {
$mainTag = \sprintf(TagEnum::ADHERENT_YEAR_TAG_PATTERN, $lastYear);

return [sprintf(TagEnum::ADHERENT_YEAR_RECOTISATION_TAG_PATTERN, $currentYear)];
}

if (
\count($adherent->getConfirmedPayments())
|| $adherent->hasRecentContribution()
) {
return [sprintf(TagEnum::ADHERENT_YEAR_ELU_TAG_PATTERN, date('Y'))];
}

$totalContributionPaymentsByYear = $this->paymentRepository->getTotalPaymentByYearForAdherent($adherent);

if (!empty($totalContributionPaymentsByYear)) {
if (
array_key_exists($currentYear, $totalContributionPaymentsByYear)
&& $totalContributionPaymentsByYear[$currentYear] > 30
&& (
!$adherent->findElectedRepresentativeMandates(true)
|| $adherent->exemptFromCotisation
)
) {
return [sprintf(TagEnum::ADHERENT_YEAR_ELU_TAG_PATTERN, $currentYear)];
}
}

$allYears = array_unique(array_merge(
$countCotisationByYear,
$totalContributionPaymentsByYear
));
unset($allYears[$currentYear]);

foreach ($allYears as $year) {
if (array_key_exists($year, $countCotisationByYear)) {
return [\sprintf(TagEnum::ADHERENT_YEAR_TAG_PATTERN, $lastYear)];
}

if (
array_key_exists($year, $totalContributionPaymentsByYear)
&& $totalContributionPaymentsByYear[$year] > 30
&& (
!$adherent->findElectedRepresentativeMandates(true)
|| $adherent->exemptFromCotisation
)
) {
return [sprintf(TagEnum::ADHERENT_YEAR_ELU_TAG_PATTERN, $currentYear)];
}
}
}

if (\in_array($adherent->getSource(), [MembershipSourceEnum::AVECVOUS, MembershipSourceEnum::JEMENGAGE])) {
return [TagEnum::SYMPATHISANT_COMPTE_AVECVOUS_JEMENGAGE];
}

if ($mainTag) {
return [$mainTag];
if ($adherent->isBesoinDEuropeUser()) {
return [TagEnum::SYMPATHISANT_BESOIN_D_EUROPE];
}

if ($adherent->isOtherPartyMembership()) {
return [TagEnum::SYMPATHISANT_AUTRE_PARTI];
if (MembershipSourceEnum::LEGISLATIVE === $adherent->getSource()) {
return [TagEnum::SYMPATHISANT_ENSEMBLE2024];
}

if (!$adherent->isV2() && $adherent->getActivatedAt() && $adherent->getActivatedAt() < new \DateTime('2022-09-17')) {
Expand Down
7 changes: 7 additions & 0 deletions src/Entity/Adherent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2387,6 +2387,13 @@ public function removeContribution(Contribution $contribution): void
$this->contributions->removeElement($contribution);
}

public function hasRecentContribution(): bool
{
$date = new \DateTime('-21 days 00:00');

return $this->contributedAt && $this->contributedAt >= $date;
}

#[Groups(['adherent_elect_read'])]
public function getContributionAmount(): ?int
{
Expand Down
23 changes: 23 additions & 0 deletions src/Repository/Contribution/PaymentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Repository\Contribution;

use App\Entity\Adherent;
use App\Entity\Contribution\Payment;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
Expand All @@ -28,4 +29,26 @@ public function save(Payment $payment): void
$this->_em->persist($payment);
$this->_em->flush();
}

public function getTotalPaymentByYearForAdherent(Adherent $adherent): array
{
return array_column($this->createQueryBuilder('payment')
->select('YEAR(payment.date) AS year')
->addSelect('SUM(payment.amount) AS total')
->where('payment.adherent = :adherent')
->andWhere('donation.status IN (:status)')
->setParameters([
'adherent' => $adherent,
'status' => [
'paid_out',
'confirmed',
'cheque_cashed',
],
])
->groupBy('year')
->orderBy('year', 'DESC')
->getQuery()
->getResult(), 'total', 'year'
);
}
}

0 comments on commit 657d6e1

Please sign in to comment.