diff --git a/README.md b/README.md index 9b55d88..2d713f3 100644 --- a/README.md +++ b/README.md @@ -101,67 +101,7 @@ For detailed eplanation you may have a look in the [examples](https://github.com ### Configuration -By means of the `ZugferdSettings` class it is possible to control various options for XML and PDF generation: - -```php -public static function getAmountDecimals(): int -``` - -Returns the currently configured number of decimal places for amount fields _(Default: 2)_. - -```php -public static function setAmountDecimals(int $amountDecimals): void -``` - -Set the number of decimal places for amount fields. - -```php -public static function getQuantityDecimals(): int -``` - -Returns the currently configured number of decimal places for quantity fields _(Default: 2)_. - -```php -public static function setQuantityDecimals(int $quantityDecimals): void -``` - -Set the number of decimal places for quantity fields. - -```php -public static function getPercentDecimals(): int -``` - -Returns the currently configured number of decimal places for percentage fields _(Default: 2)_. - -```php -public static function setPercentDecimals(int $percentDecimals): void -``` - -Set the number of decimal places for percentage fields. - -```php -public static function getDecimalSeparator(): string -``` - -Returns the currently configured character for the decimal separator. _(Default: .) - -```php -public static function setDecimalSeparator(string $decimalSeparator): void -``` - -Set the character to use as the decimal separator. - -```php -public static function getThousandsSeparator(): string -``` - -Returns the currently configured character for the thousands separator. _(Default: Empty)_ - -```php -public static function setThousandsSeparator(string $thousandsSeparator): void -``` - -Set the character to use as the thousands separator. +This library can be configured in various ways. For more information please visit our [Wiki](https://github.com/horstoeko/zugferd/wiki/Configuration). ### Reading a xml file diff --git a/src/ZugferdSettings.php b/src/ZugferdSettings.php index 73dc2fd..6f4a59f 100644 --- a/src/ZugferdSettings.php +++ b/src/ZugferdSettings.php @@ -71,6 +71,13 @@ class ZugferdSettings */ protected static $xmpMetaDataFilename = "facturx_extension_schema.xmp"; + /** + * Node paths which present a unit amount. Used for special amount formatting. See unitAmountDecimals property. + * + * @var array + */ + protected static $specialDecimalPlacesMaps = []; + /** * Get the number of decimals to use for amount values * @@ -218,6 +225,64 @@ public static function setXmpMetaDataFilename(string $xmpMetaDataFilename): void static::$xmpMetaDataFilename = $xmpMetaDataFilename; } + /** + * Returns a list of node paths which have a special number of decimal places + * + * @return array + */ + public static function getSpecialDecimalPlacesMaps(): array + { + return static::$specialDecimalPlacesMaps; + } + + /** + * Get a specific map for node paths with a special number of decimal places. If not map + * is found then the default value is returns + * + * @param string $nodePath + * @param integer $defaultDecimalPlaces + * @return integer + */ + public static function getSpecialDecimalPlacesMap(string $nodePath, int $defaultDecimalPlaces): int + { + return static::$specialDecimalPlacesMaps[$nodePath] ?? $defaultDecimalPlaces; + } + + /** + * Update the map of node paths which have a special number of decimal places + * + * @param array $specialDecimalPlacesMaps + * @return void + */ + public static function setSpecialDecimalPlacesMaps(array $specialDecimalPlacesMaps): void + { + static::$specialDecimalPlacesMaps = $specialDecimalPlacesMaps; + } + + /** + * Add a new map for a node path with a special number of decimal places + * + * @param string $nodePath + * @param integer $defaultDecimalPlaces + * @return void + */ + public static function addSpecialDecimalPlacesMap(string $nodePath, int $defaultDecimalPlaces): void + { + static::$specialDecimalPlacesMaps[$nodePath] = $defaultDecimalPlaces; + } + + /** + * Set the number of decimals to use for unit single amount (unit prices) values + * + * @param integer $amountDecimals + * @return void + */ + public static function setUnitAmountDecimals(int $amountDecimals): void + { + ZugferdSettings::addSpecialDecimalPlacesMap('/rsm:CrossIndustryInvoice/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:GrossPriceProductTradePrice/ram:ChargeAmount', $amountDecimals); + ZugferdSettings::addSpecialDecimalPlacesMap('/rsm:CrossIndustryInvoice/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:NetPriceProductTradePrice/ram:ChargeAmount', $amountDecimals); + } + /** * Get root directory * diff --git a/src/jms/ZugferdTypesHandler.php b/src/jms/ZugferdTypesHandler.php index 0bfdfca..1fff919 100644 --- a/src/jms/ZugferdTypesHandler.php +++ b/src/jms/ZugferdTypesHandler.php @@ -9,13 +9,13 @@ namespace horstoeko\zugferd\jms; -use DOMText; use DOMElement; +use DOMText; +use horstoeko\zugferd\ZugferdSettings; use JMS\Serializer\Context; use JMS\Serializer\GraphNavigator; -use horstoeko\zugferd\ZugferdSettings; -use JMS\Serializer\XmlSerializationVisitor; use JMS\Serializer\Handler\SubscribingHandlerInterface; +use JMS\Serializer\XmlSerializationVisitor; /** * Class representing a collection of serialization handlers for amount formatting and so on @@ -152,7 +152,7 @@ public static function getSubscribingMethods() /** * Serialize Anount type - * The amounts will be serialized with a precission of 2 digits + * The amounts will be serialized (by default) with a precission of 2 digits * * @param XmlSerializationVisitor $visitor * @param mixed $data @@ -165,7 +165,7 @@ public function serializeAmountType(XmlSerializationVisitor $visitor, $data, arr $node = $visitor->getDocument()->createTextNode( number_format( $data->value(), - ZugferdSettings::getAmountDecimals(), + ZugferdSettings::getSpecialDecimalPlacesMap($visitor->getCurrentNode()->getNodePath(), ZugferdSettings::getAmountDecimals()), ZugferdSettings::getDecimalSeparator(), ZugferdSettings::getThousandsSeparator() ) @@ -182,7 +182,7 @@ public function serializeAmountType(XmlSerializationVisitor $visitor, $data, arr /** * Serialize quantity type - * The quantity will be serialized with a precission of 4 digits + * The quantity will be serialized (by default) with a precission of 2 digits * * @param XmlSerializationVisitor $visitor * @param mixed $data @@ -195,7 +195,7 @@ public function serializeQuantityType(XmlSerializationVisitor $visitor, $data, a $node = $visitor->getDocument()->createTextNode( number_format( $data->value(), - ZugferdSettings::getQuantityDecimals(), + ZugferdSettings::getSpecialDecimalPlacesMap($visitor->getCurrentNode()->getNodePath(), ZugferdSettings::getQuantityDecimals()), ZugferdSettings::getDecimalSeparator(), ZugferdSettings::getThousandsSeparator() ) @@ -212,7 +212,7 @@ public function serializeQuantityType(XmlSerializationVisitor $visitor, $data, a /** * Serialize a percantage value - * The valze will be serialized with a precission of 2 digits + * The valze will be serialized (by default) with a precission of 2 digits * * @param XmlSerializationVisitor $visitor * @param mixed $data @@ -225,7 +225,7 @@ public function serializePercentType(XmlSerializationVisitor $visitor, $data, ar $node = $visitor->getDocument()->createTextNode( number_format( $data->value(), - ZugferdSettings::getPercentDecimals(), + ZugferdSettings::getSpecialDecimalPlacesMap($visitor->getCurrentNode()->getNodePath(), ZugferdSettings::getPercentDecimals()), ZugferdSettings::getDecimalSeparator(), ZugferdSettings::getThousandsSeparator() ) diff --git a/tests/testcases/SettingsTest.php b/tests/testcases/SettingsTest.php index 703f96d..4f0585a 100644 --- a/tests/testcases/SettingsTest.php +++ b/tests/testcases/SettingsTest.php @@ -190,4 +190,26 @@ public function testGetFullIccProfileFilename(): void $actual ); } + + /** + * @covers \horstoeko\zugferd\ZugferdSettings + */ + public function testSpecialDecimalPlacesMaps(): void + { + $this->assertEquals(2, ZugferdSettings::getSpecialDecimalPlacesMap('/rsm:CrossIndustryInvoice/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:GrossPriceProductTradePrice/ram:ChargeAmount', 2)); + $this->assertEquals(2, ZugferdSettings::getSpecialDecimalPlacesMap('/rsm:CrossIndustryInvoice/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:NetPriceProductTradePrice/ram:ChargeAmount', 2)); + + ZugferdSettings::addSpecialDecimalPlacesMap('/rsm:CrossIndustryInvoice/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:GrossPriceProductTradePrice/ram:ChargeAmount', 5); + + $this->assertEquals(5, ZugferdSettings::getSpecialDecimalPlacesMap('/rsm:CrossIndustryInvoice/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:GrossPriceProductTradePrice/ram:ChargeAmount', 2)); + $this->assertEquals(2, ZugferdSettings::getSpecialDecimalPlacesMap('/rsm:CrossIndustryInvoice/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:NetPriceProductTradePrice/ram:ChargeAmount', 2)); + + ZugferdSettings::setSpecialDecimalPlacesMaps([ + '/rsm:CrossIndustryInvoice/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:GrossPriceProductTradePrice/ram:ChargeAmount' => 6, + '/rsm:CrossIndustryInvoice/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:NetPriceProductTradePrice/ram:ChargeAmount' => 6 + ]); + + $this->assertEquals(6, ZugferdSettings::getSpecialDecimalPlacesMap('/rsm:CrossIndustryInvoice/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:GrossPriceProductTradePrice/ram:ChargeAmount', 2)); + $this->assertEquals(6, ZugferdSettings::getSpecialDecimalPlacesMap('/rsm:CrossIndustryInvoice/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:NetPriceProductTradePrice/ram:ChargeAmount', 2)); + } }