Skip to content

Commit bb3344b

Browse files
author
Mads Møller
committed
number format
1 parent 9ac3aca commit bb3344b

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

src/Generator/traits/ItemPrice.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ trait ItemPrice
2222
* @param $value
2323
* @param int $priceBase
2424
* @param string $priceBaseUnit
25+
* @param int $decimals
26+
* @param string $format
2527
* @return array
2628
*/
27-
public static function addPRISegment($qualifier, $value, $priceBase = 1, $priceBaseUnit = 'PCE')
29+
public static function addPRISegment($qualifier, $value, $priceBase = 1, $priceBaseUnit = 'PCE', $decimals = 2, $format = EdiFactNumber::DECIMAL_COMMA)
2830
{
2931
return [
3032
'PRI',
3133
[
3234
$qualifier,
33-
EdiFactNumber::convert($value),
35+
EdiFactNumber::convert($value, $decimals, $format),
3436
'',
3537
'',
3638
(string)$priceBase,
@@ -49,11 +51,13 @@ public function getGrossPrice()
4951

5052
/**
5153
* @param string $grossPrice
54+
* @param string $format
55+
* @param int $decimals
5256
* @return $this
5357
*/
54-
public function setGrossPrice($grossPrice)
58+
public function setGrossPrice($grossPrice, $format = EdiFactNumber::DECIMAL_COMMA, $decimals = 2)
5559
{
56-
$this->grossPrice = self::addPRISegment('AAB', $grossPrice);
60+
$this->grossPrice = self::addPRISegment('AAB', $grossPrice, 1, 'PCE', $decimals, $format);
5761
$this->addKeyToCompose('grossPrice');
5862

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

7074
/**
7175
* @param string $netPrice
76+
* @param string $format
77+
* @param int $decimals
7278
* @return $this
7379
*/
74-
public function setNetPrice($netPrice)
80+
public function setNetPrice($netPrice, $format = EdiFactNumber::DECIMAL_COMMA, $decimals = 2)
7581
{
76-
$this->netPrice = self::addPRISegment('AAA', $netPrice);
82+
$this->netPrice = self::addPRISegment('AAA', $netPrice, 1, 'PCE', $decimals, $format);
7783
$this->addKeyToCompose('netPrice');
7884

7985
return $this;

tests/GeneratorTest/EdifactNumberTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,20 @@ public function testNumberNegative()
5151
EdiFactNumber::convert('-100,223')
5252
);
5353
}
54+
55+
public function testDecimalSeparator()
56+
{
57+
$this->assertEquals(
58+
'100.22',
59+
EdiFactNumber::convert('100,223', 2, '.')
60+
);
61+
}
62+
63+
public function testDecimals()
64+
{
65+
$this->assertEquals(
66+
'100,2230',
67+
EdiFactNumber::convert('100,223', 4)
68+
);
69+
}
5470
}

tests/GeneratorTest/OrdersTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,25 @@ public function testOrders96AEancom()
180180
}
181181
}
182182

183+
public function testUSAStyle()
184+
{
185+
$interchange = new Interchange('UNB-Identifier-Sender','UNB-Identifier-Receiver');
186+
$interchange->setCharset('UNOA', '2');
187+
$orders = new Orders();
188+
189+
$item = new Orders\Item();
190+
$item->setNetPrice('1,8562', '.');
191+
$orders->addItem($item);
192+
193+
$orders->compose();
194+
$encoder = new Encoder($interchange->addMessage($orders)->getComposed(), true);
195+
$encoder->setUNA(":+.? '");
196+
197+
$message = str_replace("'", "'\n", $encoder->get());
198+
$this->assertStringContainsString('PRI+AAA:1.86:::1:PCE', $message);
199+
200+
}
201+
183202

184203
public function testFreeText()
185204
{

0 commit comments

Comments
 (0)