Skip to content

Commit 76bd75c

Browse files
authored
Merge pull request #41 from JshGrn/master
Add ORDERS compatibility APIs and preserve legacy EDI output formatting
2 parents fb1dbec + 32716fd commit 76bd75c

6 files changed

Lines changed: 374 additions & 25 deletions

File tree

src/Generator/Interchange.php

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,48 @@ public function getMessages()
106106
public function compose()
107107
{
108108
$temp = [];
109-
$unb = ['UNB', $this->charset, $this->sender, $this->receiver, [$this->date, $this->time], $this->interchangeCode];
110-
if ($this->appref !== null) {
111-
$unb[] = '';
112-
$unb[] = $this->appref;
113-
}
109+
$composedMessages = [];
114110

115-
$temp[] = $unb;
116111
foreach ($this->messages as $msg) {
117112
$msgContent = $msg->getComposed();
118113
if ($msgContent === null) {
119114
$msgContent = $msg->compose()->getComposed();
120115
}
121-
foreach ($msgContent as $i) {
122-
$temp[] = $i;
116+
117+
$composedMessages[] = $msgContent;
118+
}
119+
120+
$applicationReference = $this->appref;
121+
if ($applicationReference === null && isset($composedMessages[0][0][2][0])) {
122+
$applicationReference = (string) $composedMessages[0][0][2][0];
123+
}
124+
125+
$unb = [
126+
'UNB',
127+
$this->charset,
128+
$this->sender,
129+
$this->receiver,
130+
[$this->date, $this->time],
131+
$this->interchangeCode,
132+
];
133+
134+
if ($applicationReference !== null && $applicationReference !== '') {
135+
$unb[] = $applicationReference;
136+
$unb[] = [];
137+
$unb[] = [];
138+
$unb[] = [];
139+
$unb[] = [];
140+
}
141+
142+
$temp[] = $unb;
143+
144+
foreach ($composedMessages as $msgContent) {
145+
foreach ($msgContent as $entry) {
146+
$temp[] = $entry;
123147
}
124148
}
125-
$temp[] = ['UNZ', (string)count($this->messages), $this->interchangeCode];
149+
150+
$temp[] = ['UNZ', (string) count($this->messages), $this->interchangeCode];
126151
$this->composed = $temp;
127152

128153
return $this;

src/Generator/Orders.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class Orders extends Message
5959
/** @var array */
6060
protected $objectDescription1;
6161

62+
/** @var array */
63+
protected $customIdentifier;
64+
6265
/** @var array */
6366
protected $objectDescription2;
6467

@@ -99,6 +102,7 @@ class Orders extends Message
99102
'objectNumber',
100103
'objectDescription1',
101104
'objectDescription2',
105+
'customIdentifier',
102106
'vatNumber',
103107
'currency',
104108
'manufacturerAddress',
@@ -219,13 +223,33 @@ public function setOrderNumber($orderNumber, $documentType = '220')
219223

220224
/**
221225
* Order number without documentType validation
222-
* @param $orderNumber
226+
*
227+
* If the document type is non-numeric (e.g. EANCOM style), use code-list agency 28.
228+
*
229+
* @param string $orderNumber
223230
* @param string $documentType
231+
* @param string|null $documentTypeCodeListAgency
224232
* @return $this
225233
*/
226-
public function setCustomOrderNumber($orderNumber, $documentType = '220')
227-
{
234+
public function setCustomOrderNumber($orderNumber, $documentType = '220', $documentTypeCodeListAgency = null)
235+
{
236+
if ($documentTypeCodeListAgency !== null || preg_match('/[A-Za-z]/', (string) $documentType) === 1) {
237+
$this->orderNumber = [
238+
'BGM',
239+
[
240+
(string) $documentType,
241+
'',
242+
(string) ($documentTypeCodeListAgency ?? '28'),
243+
],
244+
(string) $orderNumber,
245+
'9',
246+
];
247+
248+
return $this;
249+
}
250+
228251
$this->orderNumber = ['BGM', $documentType, $orderNumber, '9'];
252+
229253
return $this;
230254
}
231255

@@ -442,6 +466,18 @@ public function setObjectNumber($objectNumber)
442466
return $this;
443467
}
444468

469+
/**
470+
* set a custom identifier reference for qualifier ON
471+
* @param string $customOrderIdentifier
472+
* @return $this
473+
*/
474+
public function setCustomIdentifier($customOrderIdentifier)
475+
{
476+
$this->customIdentifier = $this->addRFFSegment('ON', $customOrderIdentifier);
477+
478+
return $this;
479+
}
480+
445481
/**
446482
* @return array
447483
*/

src/Generator/Traits/Item.php

Lines changed: 108 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ trait Item
3838
/** @var array */
3939
protected $deliveryNotePosition;
4040

41+
42+
/** @var array */
43+
protected $qli;
44+
45+
/** @var array */
46+
protected $appendedSegments = [];
47+
4148
/** @var array IMD ZU */
4249
protected $additionalText;
4350

@@ -62,14 +69,21 @@ trait Item
6269
'orderPosition',
6370
'deliveryNoteNumber',
6471
'deliveryNotePosition',
72+
'qli',
6573
];
6674

6775
/**
6876
* @return array
6977
*/
7078
public function compose()
7179
{
72-
return $this->composeByKeys($this->composeKeys);
80+
$content = $this->composeByKeys($this->composeKeys);
81+
82+
foreach ($this->appendedSegments as $segment) {
83+
$content[] = $segment;
84+
}
85+
86+
return $content;
7387
}
7488

7589
/**
@@ -171,14 +185,88 @@ public function setQuantity($quantity, $unit = 'PCE', $qualifier = '21')
171185
]
172186
);
173187

188+
$qty = [
189+
(string) $qualifier,
190+
(string) $quantity,
191+
];
192+
193+
if ((string) $qualifier !== '1') {
194+
$qty[] = $unit;
195+
}
196+
174197
$this->quantity = [
175198
'QTY',
199+
$qty,
200+
];
201+
202+
return $this;
203+
}
204+
205+
/**
206+
* Add item information line (IMD).
207+
*
208+
* @param string $code
209+
* @param string $information
210+
* @return $this
211+
*/
212+
public function addInformation($code, $information)
213+
{
214+
return $this->addDynamicSegment([
215+
'IMD',
216+
'L',
217+
(string) $code,
176218
[
177-
(string)$qualifier,
178-
(string)$quantity,
179-
$unit,
219+
'',
220+
'',
221+
'',
222+
(string) $information,
180223
],
181-
];
224+
]);
225+
}
226+
227+
/**
228+
* Add item details line (GIR).
229+
*
230+
* @param int $index
231+
* @param string $lloLocationCode
232+
* @param string $lfnPurchaseFundCode
233+
* @param string $lcvDecimalPrice
234+
* @param string|null $lsqFundCode
235+
* @return $this
236+
*/
237+
public function addGir($index, $lloLocationCode, $lfnPurchaseFundCode, $lcvDecimalPrice, $lsqFundCode = '')
238+
{
239+
return $this->addDynamicSegment([
240+
'GIR',
241+
str_pad((string) $index, 3, '0', STR_PAD_LEFT),
242+
[
243+
(string) $lloLocationCode,
244+
'LLO',
245+
],
246+
[
247+
(string) ($lsqFundCode ?? ''),
248+
'LSQ',
249+
],
250+
[
251+
(string) $lfnPurchaseFundCode,
252+
'LFN',
253+
],
254+
[
255+
(string) $lcvDecimalPrice,
256+
'LCV',
257+
],
258+
]);
259+
}
260+
261+
/**
262+
* Register a dynamic segment key while preserving insertion order.
263+
*
264+
* @param array $segment
265+
* @return $this
266+
*/
267+
private function addDynamicSegment($segment)
268+
{
269+
$this->appendedSegments[] = $segment;
182270

183271
return $this;
184272
}
@@ -294,11 +382,9 @@ public function setFeaturesText($text)
294382
private function splitTexts($varName, $text, $maxLength, $lineLength, $type = 'ZU')
295383
{
296384
$this->{$varName} = str_split(mb_substr($text, 0, $maxLength), $lineLength);
297-
$nr = 0;
385+
298386
foreach ($this->{$varName} as $line) {
299-
$property = $varName . $nr++;
300-
$this->{$property} = self::addIMDSegment($line, $type);
301-
$this->addKeyToCompose($property);
387+
$this->addDynamicSegment(self::addIMDSegment($line, $type));
302388
}
303389

304390
return $this;
@@ -365,6 +451,19 @@ public function setOrderPosition($orderPosition)
365451
return $this;
366452
}
367453

454+
455+
/**
456+
* Set quote/order line identifier.
457+
*
458+
* @param string $orderPosition
459+
* @return $this
460+
*/
461+
public function setQli($orderPosition)
462+
{
463+
$this->qli = $this->addRFFSegment('QLI', $orderPosition);
464+
465+
return $this;
466+
}
368467
/**
369468
* @return array
370469
*/

src/Generator/Traits/ItemPrice.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ trait ItemPrice
1717
/** @var array */
1818
protected $netPrice;
1919

20+
/** @var array */
21+
protected $ourPrice;
22+
2023
/**
2124
* @param $qualifier
2225
* @param $value
@@ -35,9 +38,9 @@ public static function addPRISegment($qualifier, $value, $priceBase = 1, $priceB
3538
EdiFactNumber::convert($value, $decimals, $format),
3639
'',
3740
'',
38-
(string)$priceBase,
39-
$priceBaseUnit
40-
]
41+
(string) $priceBase,
42+
$priceBaseUnit,
43+
],
4144
];
4245
}
4346

@@ -84,4 +87,25 @@ public function setNetPrice($netPrice, $format = EdiFactNumber::DECIMAL_COMMA, $
8487

8588
return $this;
8689
}
90+
91+
/**
92+
* @param string $ourPrice
93+
* @param string $format
94+
* @param int $decimals
95+
* @return $this
96+
*/
97+
public function setOurPrice($ourPrice, $format = EdiFactNumber::DECIMAL_POINT, $decimals = 2)
98+
{
99+
$this->ourPrice = [
100+
'PRI',
101+
[
102+
'AAE',
103+
EdiFactNumber::convert($ourPrice, $decimals, $format),
104+
],
105+
];
106+
107+
$this->addKeyToCompose('ourPrice');
108+
109+
return $this;
110+
}
87111
}

src/Generator/Traits/NameAndAddress.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ public function addNameAndAddress(
166166
if ($name3) {
167167
$name[] = self::maxChars($name3);
168168
}
169-
return [
169+
170+
$segment = [
170171
'NAD',
171172
$type,
172173
[
@@ -188,6 +189,44 @@ public function addNameAndAddress(
188189
self::maxChars($countryCode, 2),
189190
],
190191
];
192+
193+
return $this->trimTrailingEmptyValues($segment);
194+
}
195+
196+
/**
197+
* @param array $segment
198+
* @return array
199+
*/
200+
private function trimTrailingEmptyValues($segment)
201+
{
202+
while (!empty($segment)) {
203+
$last = end($segment);
204+
if (!$this->isEmptyValue($last)) {
205+
break;
206+
}
207+
array_pop($segment);
208+
}
209+
210+
return $segment;
211+
}
212+
213+
/**
214+
* @param mixed $value
215+
* @return bool
216+
*/
217+
private function isEmptyValue($value)
218+
{
219+
if (is_array($value)) {
220+
foreach ($value as $entry) {
221+
if (!$this->isEmptyValue($entry)) {
222+
return false;
223+
}
224+
}
225+
226+
return true;
227+
}
228+
229+
return $value === '' || $value === null;
191230
}
192231

193232
/**

0 commit comments

Comments
 (0)