Skip to content

Commit

Permalink
number format
Browse files Browse the repository at this point in the history
  • Loading branch information
Mads Møller committed May 14, 2019
1 parent 9ac3aca commit bb3344b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/Generator/traits/ItemPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ trait ItemPrice
* @param $value
* @param int $priceBase
* @param string $priceBaseUnit
* @param int $decimals
* @param string $format
* @return array
*/
public static function addPRISegment($qualifier, $value, $priceBase = 1, $priceBaseUnit = 'PCE')
public static function addPRISegment($qualifier, $value, $priceBase = 1, $priceBaseUnit = 'PCE', $decimals = 2, $format = EdiFactNumber::DECIMAL_COMMA)
{
return [
'PRI',
[
$qualifier,
EdiFactNumber::convert($value),
EdiFactNumber::convert($value, $decimals, $format),
'',
'',
(string)$priceBase,
Expand All @@ -49,11 +51,13 @@ public function getGrossPrice()

/**
* @param string $grossPrice
* @param string $format
* @param int $decimals
* @return $this
*/
public function setGrossPrice($grossPrice)
public function setGrossPrice($grossPrice, $format = EdiFactNumber::DECIMAL_COMMA, $decimals = 2)
{
$this->grossPrice = self::addPRISegment('AAB', $grossPrice);
$this->grossPrice = self::addPRISegment('AAB', $grossPrice, 1, 'PCE', $decimals, $format);
$this->addKeyToCompose('grossPrice');

return $this;
Expand All @@ -69,11 +73,13 @@ public function getNetPrice()

/**
* @param string $netPrice
* @param string $format
* @param int $decimals
* @return $this
*/
public function setNetPrice($netPrice)
public function setNetPrice($netPrice, $format = EdiFactNumber::DECIMAL_COMMA, $decimals = 2)
{
$this->netPrice = self::addPRISegment('AAA', $netPrice);
$this->netPrice = self::addPRISegment('AAA', $netPrice, 1, 'PCE', $decimals, $format);
$this->addKeyToCompose('netPrice');

return $this;
Expand Down
16 changes: 16 additions & 0 deletions tests/GeneratorTest/EdifactNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,20 @@ public function testNumberNegative()
EdiFactNumber::convert('-100,223')
);
}

public function testDecimalSeparator()
{
$this->assertEquals(
'100.22',
EdiFactNumber::convert('100,223', 2, '.')
);
}

public function testDecimals()
{
$this->assertEquals(
'100,2230',
EdiFactNumber::convert('100,223', 4)
);
}
}
19 changes: 19 additions & 0 deletions tests/GeneratorTest/OrdersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,25 @@ public function testOrders96AEancom()
}
}

public function testUSAStyle()
{
$interchange = new Interchange('UNB-Identifier-Sender','UNB-Identifier-Receiver');
$interchange->setCharset('UNOA', '2');
$orders = new Orders();

$item = new Orders\Item();
$item->setNetPrice('1,8562', '.');
$orders->addItem($item);

$orders->compose();
$encoder = new Encoder($interchange->addMessage($orders)->getComposed(), true);
$encoder->setUNA(":+.? '");

$message = str_replace("'", "'\n", $encoder->get());
$this->assertStringContainsString('PRI+AAA:1.86:::1:PCE', $message);

}


public function testFreeText()
{
Expand Down

0 comments on commit bb3344b

Please sign in to comment.