Skip to content

Commit

Permalink
Merged decimal places handling from horstoeko/zugferd
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Oct 13, 2024
1 parent 54e5bdb commit e6979a2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 18 deletions.
67 changes: 67 additions & 0 deletions src/OrderSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ class OrderSettings
*/
protected static $xmpMetaDataFilename = "orderx_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,66 @@ 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
{
$nodePath = preg_replace('@\[\d+\]@', '', $nodePath);
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
{
$nodePath = preg_replace('@\[\d+\]@', '', $nodePath);
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
{
OrderSettings::addSpecialDecimalPlacesMap('/rsm:SCRDMCCBDACIOMessageStructure/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:GrossPriceProductTradePrice/ram:ChargeAmount', $amountDecimals);
OrderSettings::addSpecialDecimalPlacesMap('/rsm:SCRDMCCBDACIOMessageStructure/rsm:SupplyChainTradeTransaction/ram:IncludedSupplyChainTradeLineItem/ram:SpecifiedLineTradeAgreement/ram:NetPriceProductTradePrice/ram:ChargeAmount', $amountDecimals);
}

/**
* Get root directory
*
Expand Down
33 changes: 15 additions & 18 deletions src/jms/OrderTypesHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,17 @@ 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
* @return DOMText|false
* @param XmlSerializationVisitor $visitor
* @param mixed $data
*/
public function serializeAmountType(XmlSerializationVisitor $visitor, $data)
{
$node = $visitor->getDocument()->createTextNode(
number_format(
$data->value(),
OrderSettings::getAmountDecimals(),
OrderSettings::getSpecialDecimalPlacesMap($visitor->getCurrentNode()->getNodePath(), OrderSettings::getAmountDecimals()),
OrderSettings::getDecimalSeparator(),
OrderSettings::getThousandsSeparator()
)
Expand All @@ -152,18 +151,17 @@ public function serializeAmountType(XmlSerializationVisitor $visitor, $data)

/**
* 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
* @return DOMText|false
* @param XmlSerializationVisitor $visitor
* @param mixed $data
*/
public function serializeQuantityType(XmlSerializationVisitor $visitor, $data)
{
$node = $visitor->getDocument()->createTextNode(
number_format(
$data->value(),
OrderSettings::getQuantityDecimals(),
OrderSettings::getSpecialDecimalPlacesMap($visitor->getCurrentNode()->getNodePath(), OrderSettings::getQuantityDecimals()),
OrderSettings::getDecimalSeparator(),
OrderSettings::getThousandsSeparator()
)
Expand All @@ -180,22 +178,22 @@ public function serializeQuantityType(XmlSerializationVisitor $visitor, $data)

/**
* 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
* @return DOMText|false
* @param XmlSerializationVisitor $visitor
* @param mixed $data
*/
public function serializePercentType(XmlSerializationVisitor $visitor, $data)
{
$node = $visitor->getDocument()->createTextNode(
number_format(
$data->value(),
OrderSettings::getPercentDecimals(),
OrderSettings::getSpecialDecimalPlacesMap($visitor->getCurrentNode()->getNodePath(), OrderSettings::getPercentDecimals()),
OrderSettings::getDecimalSeparator(),
OrderSettings::getThousandsSeparator()
)
);

return $node;
}

Expand All @@ -204,9 +202,8 @@ public function serializePercentType(XmlSerializationVisitor $visitor, $data)
* False and true values will be serialized correctly (false won't be serialized
* in the default implementation)
*
* @param XmlSerializationVisitor $visitor
* @param mixed $data
* @return DOMElement|false
* @param XmlSerializationVisitor $visitor
* @param mixed $data
*/
public function serializeIndicatorType(XmlSerializationVisitor $visitor, $data)
{
Expand Down

0 comments on commit e6979a2

Please sign in to comment.