Skip to content

Commit

Permalink
Merge pull request #64 from horstoeko/Issue_59
Browse files Browse the repository at this point in the history
Define decimal places for node paths
  • Loading branch information
horstoeko authored May 29, 2024
2 parents d21a4c5 + a0f8b04 commit 0f4b2d5
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 70 deletions.
62 changes: 1 addition & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
65 changes: 65 additions & 0 deletions src/ZugferdSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
18 changes: 9 additions & 9 deletions src/jms/ZugferdTypesHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
)
Expand All @@ -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
Expand All @@ -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()
)
Expand All @@ -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
Expand All @@ -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()
)
Expand Down
22 changes: 22 additions & 0 deletions tests/testcases/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 0f4b2d5

Please sign in to comment.