diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ebe4d2..fb272d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- [#96](https://github.com/itk-dev/devops_itksites/pull/96) + Show the Service Agreements monthly price as Danish kroner, + `12.500,50 kr.`, on index and detail - [#95](https://github.com/itk-dev/devops_itksites/pull/95) Update `vincentlanglet/twig-cs-fixer` to 4.0. Every other dependency is already at its latest minor; the remaining majors are held back by their diff --git a/src/Controller/Admin/SecurityContractCrudController.php b/src/Controller/Admin/SecurityContractCrudController.php index 1cac814..d0e0cbb 100644 --- a/src/Controller/Admin/SecurityContractCrudController.php +++ b/src/Controller/Admin/SecurityContractCrudController.php @@ -72,7 +72,12 @@ public function configureFields(string $pageName): iterable yield TextField::new('clientContactEmail')->hideOnIndex(); yield FormField::addFieldset('Budget'); - yield NumberField::new('monthlyPrice')->setTextAlign('right')->setColumns(6); + // The amount is Danish kroner, which the admin never said anywhere. + // 5.5's prepend()/append() addons would be the way to show a unit inside + // an input, but they render on form pages only and this CRUD disables + // NEW and EDIT (see configureActions), so index and detail are the only + // pages it has. Hence formatting instead of an addon. + yield NumberField::new('monthlyPrice')->setTextAlign('right')->setColumns(6)->formatValue(self::formatKroner(...)); yield FormField::addFieldset('Infrastructure'); yield BooleanField::new('dedicatedServer')->renderAsSwitch(false)->hideOnIndex(); @@ -83,6 +88,29 @@ public function configureFields(string $pageName): iterable yield DateField::new('validTo')->setColumns(6); } + /** + * An amount as Danish kroner: 12.500,50 kr. + * + * Through Intl rather than by pasting a suffix on, so the grouping and the + * decimal separator are Danish too. The application locale is `en`, which + * would otherwise render 12,500.5 with no currency at all. + */ + public static function formatKroner(?float $value): ?string + { + if (null === $value) { + return null; + } + + return (new \NumberFormatter('da_DK', \NumberFormatter::CURRENCY))->formatCurrency($value, 'DKK') ?: null; + } + + /** + * The attribute is what makes this method reachable as a CRUD action. + * + * Without it EasyAdmin throws while rendering the "Sync all" button, which + * took the whole index page with it — see the "Custom CRUD Actions" section + * of the bundle's UPGRADE.md. + */ #[AdminRoute] public function syncAll(): RedirectResponse { diff --git a/tests/Controller/Admin/SecurityContractCurrencyTest.php b/tests/Controller/Admin/SecurityContractCurrencyTest.php new file mode 100644 index 0000000..e4f8fd3 --- /dev/null +++ b/tests/Controller/Admin/SecurityContractCurrencyTest.php @@ -0,0 +1,65 @@ +get(EntityManagerInterface::class); + $client->loginUser($entityManager->getRepository(User::class)->findOneBy([])); + + $project = new Project(); + $project->setEconomicsId(4711); + $project->setName('Kroner probe'); + + $contract = new SecurityContract(); + $contract->setEconomicsId(4711); + $contract->setProject($project); + $contract->setMonthlyPrice(12500.5); + + $entityManager->persist($project); + $entityManager->persist($contract); + $entityManager->flush(); + + $url = static::getContainer()->get(AdminUrlGenerator::class) + ->setController(SecurityContractCrudController::class) + ->setAction(Crud::PAGE_INDEX) + ->generateUrl(); + + $client->request('GET', $url); + + $this->assertResponseIsSuccessful(); + + $content = (string) $client->getResponse()->getContent(); + + // Danish grouping and separator, not the application locale's 12,500.5. + // Amount and unit are asserted apart because Intl joins them with a + // non-breaking space. + $this->assertStringContainsString('12.500,50', $content); + $this->assertStringContainsString('kr.', $content); + } +}