From 15890f279e3de1424858320d0b6b40e9f184640c 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 and register the first payment in the payments array so the new users show up as having paid in the xlsx database export. Closes: https://github.com/roodjong/mijnrood/issues/203 Signed-off-by: Nowa Ammerlaan --- migrations/Version20241117160044.php | 31 +++++++++++++++++++ .../Admin/MembershipApplicationCrud.php | 31 +++++++++++++++++++ src/Entity/Member.php | 14 ++++----- 3 files changed, 69 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 d36a316..923ce63 100644 --- a/src/Controller/Admin/MembershipApplicationCrud.php +++ b/src/Controller/Admin/MembershipApplicationCrud.php @@ -5,6 +5,7 @@ use App\Service\SubscriptionSetupService; 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; @@ -116,6 +117,36 @@ public function acceptApplication(AdminContext $context) $this->addFlash("warning", "Het net geaccepteerde lid heeft nog geen automatisch incasso. Het nieuwe lid kan dit alleen zelf instellen."); } + $member = $application->createMember($subscriptionId); + $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 $member::PERIOD_MONTHLY: + $init_payment->setPeriodMonthEnd($member->getRegistrationTime()->modify('+1 month')->format('n')); + break; + case $member::PERIOD_QUARTERLY: + $init_payment->setPeriodMonthEnd($member->getRegistrationTime()->modify('+3 month')->format('n')); + break; + case $member::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 b42bff5..12b010e 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}) @@ -271,20 +271,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; }