From 5e0232fdacebe39841906b4d9c45f4ead2951940 Mon Sep 17 00:00:00 2001 From: Nowa Ammerlaan Date: Sun, 17 Nov 2024 16:41:39 +0100 Subject: [PATCH] Member.php: also properly resolve monthly and yearly payments Closes: https://github.com/roodjong/mijnrood/issues/203 Signed-off-by: Nowa Ammerlaan --- migrations/Version20241117160044.php | 31 +++++++++++++++++++ .../Admin/MembershipApplicationCrud.php | 27 ++++++++++++++++ src/Entity/Member.php | 14 ++++----- 3 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 migrations/Version20241117160044.php diff --git a/migrations/Version20241117160044.php b/migrations/Version20241117160044.php new file mode 100644 index 0000000..73b047f --- /dev/null +++ b/migrations/Version20241117160044.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE admin_member CHANGE contribution_period contribution_period INT DEFAULT 1 NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE admin_member CHANGE contribution_period contribution_period INT DEFAULT 2 NOT NULL'); + } +} diff --git a/src/Controller/Admin/MembershipApplicationCrud.php b/src/Controller/Admin/MembershipApplicationCrud.php index eb7ede0..1685727 100644 --- a/src/Controller/Admin/MembershipApplicationCrud.php +++ b/src/Controller/Admin/MembershipApplicationCrud.php @@ -4,6 +4,7 @@ use Doctrine\ORM\QueryBuilder; +use App\Entity\ContributionPayment; use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection; use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection; use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; @@ -132,6 +133,32 @@ public function acceptApplication(AdminContext $context) $member->generateNewPasswordToken(); $em = $this->getDoctrine()->getManager(); + + if ($application->getPaid()) { + $init_payment = new ContributionPayment(); + $init_payment->setMember($member); + $init_payment->setStatus(1); + $init_payment->setPaymentTime($member->getRegistrationTime()); + $init_payment->setPeriodYear($member->getRegistrationTime()->format('Y')); + $init_payment->setPeriodMonthStart($member->getRegistrationTime()->format('n')); + switch ($member->getContributionPeriod()) { + case self::PERIOD_MONTHLY: + $init_payment->setPeriodMonthEnd($member->getRegistrationTime()->modify('+1 month')->format('n')); + break; + case self::PERIOD_QUARTERLY: + $init_payment->setPeriodMonthEnd($member->getRegistrationTime()->modify('+3 month')->format('n')); + break; + case self::PERIOD_ANNUALLY: + $init_payment->setPeriodMonthEnd($member->getRegistrationTime()->modify('+12 month')->format('n')); + break; + default; + throw new \Exception('Period must be PERIOD_MONTHLY, PERIOD_QUARTERLY or PERIOD_ANNUALLY'); + } + $init_payment->setAmountInCents($member->getContributionPerPeriodInCents()); + $init_payment->setAmountInEuros($member->getContributionPerPeriodInEuros()); + $em->persist($init_payment); + } + $em->persist($member); $em->remove($application); $em->flush(); diff --git a/src/Entity/Member.php b/src/Entity/Member.php index 65108a8..507ecc6 100644 --- a/src/Entity/Member.php +++ b/src/Entity/Member.php @@ -123,9 +123,9 @@ class Member implements UserInterface { private bool $createSubscriptionAfterPayment = false; /** - * @ORM\Column(type="integer", options={"default": 2}) + * @ORM\Column(type="integer", options={"default": 1}) */ - private int $contributionPeriod = self::PERIOD_ANNUALLY; + private int $contributionPeriod = self::PERIOD_QUARTERLY; /** * @ORM\Column(type="integer", options={"default": 500}) @@ -264,20 +264,20 @@ public function setIsAdmin(bool $isAdmin): void { } public function isContributionCompleted(DateTime $when) { - $year = $when->format('Y'); - $month = $when->format('n'); switch ($this->getContributionPeriod()) { case self::PERIOD_MONTHLY: - $payments = $this->contributionPayments->filter(fn($payment) => $payment->getPeriodYear() == $year && $payment->getPeriodMonthStart() == $month); + $when->modify('-1 month'); break; case self::PERIOD_QUARTERLY: $when->modify('-3 months'); - $payments = $this->contributionPayments->filter(fn($payment) => $payment->getPaymentTime() >= $when); break; case self::PERIOD_ANNUALLY: - $payments = $this->contributionPayments->filter(fn($payment) => $payment->getPeriodYear() == $year); + $when->modify('-12 months'); break; + default; + throw new \Exception('Period must be PERIOD_MONTHLY, PERIOD_QUARTERLY or PERIOD_ANNUALLY'); } + $payments = $this->contributionPayments->filter(fn($payment) => $payment->getPaymentTime() >= $when); $payments = $payments->filter(fn($payment) => $payment->getStatus() == ContributionPayment::STATUS_PAID); return count($payments) > 0; }