Repository: EasyCorp/EasyAdminBundle (language: php)

Issue:
Can not set custom divisor on MoneyType
**Describe the bug**
It's impossible to set a different divisor than 1 or 100 on Money fields, since the MoneyConfigurator ALWAYS sets it to either 1 or 100.

**To Reproduce**
Try to set a different divisor

**(OPTIONAL) Additional context**
I'm trying to store fractional cents as integer by using a divisor of 10000, sadly the money configurator alywas blocks this.


Discussion / hints:

Fix patch:
diff --git a/src/Field/Configurator/MoneyConfigurator.php b/src/Field/Configurator/MoneyConfigurator.php
index d36cc5cf8f..a11a52f959 100644
--- a/src/Field/Configurator/MoneyConfigurator.php
+++ b/src/Field/Configurator/MoneyConfigurator.php
@@ -42,13 +42,20 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
         $field->setFormTypeOption('scale', $numDecimals);
 
         $isStoredAsCents = true === $field->getCustomOption(MoneyField::OPTION_STORED_AS_CENTS);
-        $field->setFormTypeOption('divisor', $isStoredAsCents ? 100 : 1);
+        $field->setFormTypeOptionIfNotSet('divisor', $isStoredAsCents ? 100 : 1);
+
+        $divisor = $field->getFormTypeOption('divisor');
+        $isCustomDivisor = 1 !== $divisor && 100 !== $divisor;
 
         if (null === $field->getValue()) {
             return;
         }
 
-        $amount = $isStoredAsCents ? $field->getValue() / 100 : $field->getValue();
+        if ($isCustomDivisor) {
+            $amount = $field->getValue() / $divisor;
+        } else {
+            $amount = $isStoredAsCents ? $field->getValue() / 100 : $field->getValue();
+        }
 
         $formattedValue = $this->intlFormatter->formatCurrency($amount, $currencyCode, ['fraction_digit' => $numDecimals]);
         $field->setFormattedValue($formattedValue);


Test patch:
diff --git a/tests/Field/MoneyFieldTest.php b/tests/Field/MoneyFieldTest.php
index 7d94d3effb..e65bd647ee 100644
--- a/tests/Field/MoneyFieldTest.php
+++ b/tests/Field/MoneyFieldTest.php
@@ -93,16 +93,29 @@ public function testFieldDecimals()
         self::assertSame(3, $fieldDto->getFormTypeOption('scale'));
     }
 
-    public function testFieldCents()
+    public function testFieldsDefaultsToCents()
     {
         $field = MoneyField::new('foo')->setValue(100)->setCurrency('EUR');
         $fieldDto = $this->configure($field);
         self::assertSame('1€', $fieldDto->getFormattedValue());
         self::assertSame(100, $fieldDto->getFormTypeOption('divisor'));
+    }
 
+    public function testFieldCents()
+    {
+        $field = MoneyField::new('foo')->setValue(100)->setCurrency('EUR');
         $field->setStoredAsCents(false);
         $fieldDto = $this->configure($field);
         self::assertSame('100€', $fieldDto->getFormattedValue());
         self::assertSame(1, $fieldDto->getFormTypeOption('divisor'));
     }
+
+    public function testFieldWithCustomDivisor()
+    {
+        $field = MoneyField::new('foo')->setValue(725)->setCurrency('EUR');
+        $field->setFormTypeOption('divisor', 10000);
+        $fieldDto = $this->configure($field);
+        self::assertSame('0.0725€', $fieldDto->getFormattedValue());
+        self::assertSame(10000, $fieldDto->getFormTypeOption('divisor'));
+    }
 }