diff --git a/src/Command/MigrateSubscriptionToNewMessage.php b/src/Command/MigrateSubscriptionToNewMessage.php new file mode 100644 index 0000000..c66a2af --- /dev/null +++ b/src/Command/MigrateSubscriptionToNewMessage.php @@ -0,0 +1,87 @@ +writeln('Updating Mollie subscription to new message'); + + try { + $this->confirmChangeCount($input, $output); + } catch (\Exception $e) { + $output->writeln($e->getMessage()); + return Command::FAILURE; + } + + $memberRepository = $this->entityManager->getRepository(Member::class); + + $allSubscriptions = $this->mollieApiClient->subscriptions->iterator(); + foreach ($allSubscriptions as $subscription) { + /** @var Subscription $subscription */ + if (!$subscription->isActive()) { + continue; + } + $output->writeln('Updating subscription ' . $subscription->description); + /** @var Member $member */ + $members = $memberRepository->findBy(['mollieSubscriptionId' => $subscription->id]); + if (count($members) === 0) { + $output->writeln('Could not find member for subscription ' . $subscription->id . '! (skipping)'); + continue; + } + $member = $members[0]; + $newDescription = $this->subscriptionService->generateDescription($member); + $subscription->description = $newDescription; + try { + $subscription->update(); + } catch (ApiException $exception) { + $output->writeln('' . $exception->getMessage() . ''); + } + } + + return Command::SUCCESS; + } + + /** + * @throws \Exception + */ + private function confirmChangeCount(InputInterface $input, OutputInterface $output): void + { + $allSubscriptions = $this->mollieApiClient->subscriptions->iterator(); + $amount = $allSubscriptions->count(); + $helper = $this->getHelper('question'); + $question = new Question("You are about to change $amount descriptions, to continue confirm the amount: ", false); + $answer = (int)$helper->ask($input, $output, $question); + if ($answer === $amount) { + return; + } + throw new \Exception('Did not confirm'); + } +} diff --git a/src/Controller/Admin/MembershipApplicationCrud.php b/src/Controller/Admin/MembershipApplicationCrud.php index bc32e23..d36a316 100644 --- a/src/Controller/Admin/MembershipApplicationCrud.php +++ b/src/Controller/Admin/MembershipApplicationCrud.php @@ -98,21 +98,6 @@ public function acceptApplication(AdminContext $context) /** @var MembershipApplication $application */ $application = $context->getEntity()->getInstance(); - $mollieIntervals = [ - Member::PERIOD_MONTHLY => '1 month', - Member::PERIOD_QUARTERLY => '3 months', - Member::PERIOD_ANNUALLY => '1 year' - ]; - $dateTimeIntervals = [ - Member::PERIOD_MONTHLY => 'P1M', - Member::PERIOD_QUARTERLY => 'P3M', - Member::PERIOD_ANNUALLY => 'P1Y' - ]; - - $startDate = new DateTime(); - $startDate->setDate(date('Y'), floor(date('m') / 3) * 3, 1); - $startDate->add(new DateInterval($dateTimeIntervals[$application->getContributionPeriod()])); - $member = $application->createMember(null); $member->generateNewPasswordToken(); $em = $this->getDoctrine()->getManager(); diff --git a/src/Service/SubscriptionSetupService.php b/src/Service/SubscriptionSetupService.php index 6988e23..f1d1829 100644 --- a/src/Service/SubscriptionSetupService.php +++ b/src/Service/SubscriptionSetupService.php @@ -24,6 +24,16 @@ public function __construct( { } + public function generateDescription(Member $member): string + { + $projectRoot = $this->params->get('kernel.project_dir'); + $org_config = Yaml::parseFile($projectRoot . '/config/instances/' . $this->params->get('app.organizationID') . '.yaml'); + + $description = $org_config['mollie_payment_description']; + $description = str_replace('{{organisation_name}}', $this->params->get("app.organizationName"), $description); + return str_replace('{{member_number}}', (string)$member->getId(), $description); + } + /** * @throws ApiException */ @@ -44,12 +54,7 @@ public function createSubscription(Member $member, Customer $customer): Subscrip $startDate->setDate((int)date('Y'), (int)floor(date('m') / 3) * 3, 1); $startDate->add(new \DateInterval($dateTimeIntervals[$member->getContributionPeriod()])); - $projectRoot = $this->params->get('kernel.project_dir'); - $org_config = Yaml::parseFile($projectRoot . '/config/instances/' . $this->params->get('app.organizationID') . '.yaml'); - - $description = $org_config['mollie_payment_description']; - $description = str_replace('{{organisation_name}}', $this->params->get("app.organizationName"), $description); - $description = str_replace('{{member_number}}', (string)$member->getId(), $description); + $description = $this->generateDescription($member); $subscription = $customer->createSubscription([ 'amount' => [