Skip to content

Commit

Permalink
ZugferdSettings: Give possitibility to let different tags have differ…
Browse files Browse the repository at this point in the history
…ent nubmer of decimals

Example usage:
`ZugferdSettings::setAmountDecimals(5, "ram:ChargeAmount");`
  • Loading branch information
danielmarschall committed May 26, 2024
1 parent a42cdc6 commit dcdecb1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
42 changes: 24 additions & 18 deletions src/ZugferdSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ class ZugferdSettings
/**
* The number of decimals for amount values
*
* @var integer
* @var array
*/
protected static $amountDecimals = 2;
private static $amountDecimals = [];

/**
* The number of decimals for quantity values
*
* @var integer
* @var array
*/
protected static $quantityDecimals = 2;
private static $quantityDecimals = [];

/**
* The number of decimals for percent values
*
* @var integer
* @var array
*/
protected static $percentDecimals = 2;
private static $percentDecimals = [];

/**
* The decimal separator
Expand Down Expand Up @@ -74,64 +74,70 @@ class ZugferdSettings
/**
* Get the number of decimals to use for amount values
*
* @param string|null $tagName The tag name, e.g. "ram:ChargeAmount", or null for the rest
* @return integer
*/
public static function getAmountDecimals(): int
public static function getAmountDecimals(?string $tagName=null): int
{
return static::$amountDecimals;
return static::$amountDecimals[$tagName??""] ?? 2;
}

/**
* Set the number of decimals to use for amount values
*
* @param integer $amountDecimals
* @param string|null $tagName The tag name, e.g. "ram:ChargeAmount", or null for the rest
* @return void
*/
public static function setAmountDecimals(int $amountDecimals): void
public static function setAmountDecimals(int $amountDecimals, ?string $tagName=null): void
{
static::$amountDecimals = $amountDecimals;
static::$amountDecimals[$tagName??""] = $amountDecimals;
}

/**
* Get the number of decimals to use for amount values
*
* @param string|null $tagName The tag name, e.g. "ram:ChargeAmount", or null for the rest
* @return integer
*/
public static function getQuantityDecimals(): int
public static function getQuantityDecimals(?string $tagName=null): int
{
return static::$quantityDecimals;
return static::$quantityDecimals[$tagName??""] ?? 2;
}

/**
* Set the number of decimals to use for quantity values
*
* @param integer $quantityDecimals
* @param string|null $tagName The tag name, e.g. "ram:ChargeAmount", or null for the rest
* @return void
*/
public static function setQuantityDecimals(int $quantityDecimals): void
public static function setQuantityDecimals(int $quantityDecimals, ?string $tagName=null): void
{
static::$quantityDecimals = $quantityDecimals;
static::$quantityDecimals[$tagName??""] = $quantityDecimals;
}

/**
* Get the number of decimals to use for percent values
*
* @param string|null $tagName The tag name, e.g. "ram:ChargeAmount", or null for the rest
* @return integer
*/
public static function getPercentDecimals(): int
public static function getPercentDecimals(?string $tagName=null): int
{
return static::$percentDecimals;
return static::$percentDecimals[$tagName??""] ?? 2;
}

/**
* Set the number of decimals to use for percent values
*
* @param integer $percentDecimals
* @param string|null $tagName The tag name, e.g. "ram:ChargeAmount", or null for the rest
* @return void
*/
public static function setPercentDecimals(int $percentDecimals): void
public static function setPercentDecimals(int $percentDecimals, ?string $tagName=null): void
{
static::$percentDecimals = $percentDecimals;
static::$percentDecimals[$tagName??""] = $percentDecimals;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/jms/ZugferdTypesHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function serializeAmountType(XmlSerializationVisitor $visitor, $data, arr
$node = $visitor->getDocument()->createTextNode(
number_format(
$data->value(),
ZugferdSettings::getAmountDecimals(),
ZugferdSettings::getAmountDecimals($visitor->getCurrentNode()->tagName),
ZugferdSettings::getDecimalSeparator(),
ZugferdSettings::getThousandsSeparator()
)
Expand Down Expand Up @@ -195,7 +195,7 @@ public function serializeQuantityType(XmlSerializationVisitor $visitor, $data, a
$node = $visitor->getDocument()->createTextNode(
number_format(
$data->value(),
ZugferdSettings::getQuantityDecimals(),
ZugferdSettings::getQuantityDecimals($visitor->getCurrentNode()->tagName),
ZugferdSettings::getDecimalSeparator(),
ZugferdSettings::getThousandsSeparator()
)
Expand Down Expand Up @@ -225,7 +225,7 @@ public function serializePercentType(XmlSerializationVisitor $visitor, $data, ar
$node = $visitor->getDocument()->createTextNode(
number_format(
$data->value(),
ZugferdSettings::getPercentDecimals(),
ZugferdSettings::getPercentDecimals($visitor->getCurrentNode()->tagName),
ZugferdSettings::getDecimalSeparator(),
ZugferdSettings::getThousandsSeparator()
)
Expand Down

0 comments on commit dcdecb1

Please sign in to comment.