Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions src/Generator/Interchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,48 @@ public function getMessages()
public function compose()
{
$temp = [];
$unb = ['UNB', $this->charset, $this->sender, $this->receiver, [$this->date, $this->time], $this->interchangeCode];
if ($this->appref !== null) {
$unb[] = '';
$unb[] = $this->appref;
}
$composedMessages = [];

$temp[] = $unb;
foreach ($this->messages as $msg) {
$msgContent = $msg->getComposed();
if ($msgContent === null) {
$msgContent = $msg->compose()->getComposed();
}
foreach ($msgContent as $i) {
$temp[] = $i;

$composedMessages[] = $msgContent;
}

$applicationReference = $this->appref;
if ($applicationReference === null && isset($composedMessages[0][0][2][0])) {
$applicationReference = (string) $composedMessages[0][0][2][0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems quite specific? Seems to pick a field from UNH?

}

$unb = [
'UNB',
$this->charset,
$this->sender,
$this->receiver,
[$this->date, $this->time],
$this->interchangeCode,
];

if ($applicationReference !== null && $applicationReference !== '') {
$unb[] = $applicationReference;
$unb[] = [];
$unb[] = [];
$unb[] = [];
$unb[] = [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the empty fields?

}

$temp[] = $unb;

foreach ($composedMessages as $msgContent) {
foreach ($msgContent as $entry) {
$temp[] = $entry;
}
}
$temp[] = ['UNZ', (string)count($this->messages), $this->interchangeCode];

$temp[] = ['UNZ', (string) count($this->messages), $this->interchangeCode];
$this->composed = $temp;

return $this;
Expand Down
42 changes: 39 additions & 3 deletions src/Generator/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class Orders extends Message
/** @var array */
protected $objectDescription1;

/** @var array */
protected $customIdentifier;

/** @var array */
protected $objectDescription2;

Expand Down Expand Up @@ -99,6 +102,7 @@ class Orders extends Message
'objectNumber',
'objectDescription1',
'objectDescription2',
'customIdentifier',
'vatNumber',
'currency',
'manufacturerAddress',
Expand Down Expand Up @@ -219,13 +223,33 @@ public function setOrderNumber($orderNumber, $documentType = '220')

/**
* Order number without documentType validation
* @param $orderNumber
*
* If the document type is non-numeric (e.g. EANCOM style), use code-list agency 28.
*
* @param string $orderNumber
* @param string $documentType
* @param string|null $documentTypeCodeListAgency
* @return $this
*/
public function setCustomOrderNumber($orderNumber, $documentType = '220')
{
public function setCustomOrderNumber($orderNumber, $documentType = '220', $documentTypeCodeListAgency = null)
{
if ($documentTypeCodeListAgency !== null || preg_match('/[A-Za-z]/', (string) $documentType) === 1) {
$this->orderNumber = [
'BGM',
[
(string) $documentType,
'',
(string) ($documentTypeCodeListAgency ?? '28'),
],
(string) $orderNumber,
'9',
];

return $this;
}

$this->orderNumber = ['BGM', $documentType, $orderNumber, '9'];

return $this;
}

Expand Down Expand Up @@ -442,6 +466,18 @@ public function setObjectNumber($objectNumber)
return $this;
}

/**
* set a custom identifier reference for qualifier ON
* @param string $customOrderIdentifier
* @return $this
*/
public function setCustomIdentifier($customOrderIdentifier)
{
$this->customIdentifier = $this->addRFFSegment('ON', $customOrderIdentifier);

return $this;
}

/**
* @return array
*/
Expand Down
117 changes: 108 additions & 9 deletions src/Generator/Traits/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ trait Item
/** @var array */
protected $deliveryNotePosition;


/** @var array */
protected $qli;

/** @var array */
protected $appendedSegments = [];

/** @var array IMD ZU */
protected $additionalText;

Expand All @@ -62,14 +69,21 @@ trait Item
'orderPosition',
'deliveryNoteNumber',
'deliveryNotePosition',
'qli',
];

/**
* @return array
*/
public function compose()
{
return $this->composeByKeys($this->composeKeys);
$content = $this->composeByKeys($this->composeKeys);

foreach ($this->appendedSegments as $segment) {
$content[] = $segment;
}

return $content;
}

/**
Expand Down Expand Up @@ -171,14 +185,88 @@ public function setQuantity($quantity, $unit = 'PCE', $qualifier = '21')
]
);

$qty = [
(string) $qualifier,
(string) $quantity,
];

if ((string) $qualifier !== '1') {
$qty[] = $unit;
}

$this->quantity = [
'QTY',
$qty,
];

return $this;
}

/**
* Add item information line (IMD).
*
* @param string $code
* @param string $information
* @return $this
*/
public function addInformation($code, $information)
{
return $this->addDynamicSegment([
'IMD',
'L',
(string) $code,
[
(string)$qualifier,
(string)$quantity,
$unit,
'',
'',
'',
(string) $information,
],
];
]);
}

/**
* Add item details line (GIR).
*
* @param int $index
* @param string $lloLocationCode
* @param string $lfnPurchaseFundCode
* @param string $lcvDecimalPrice
* @param string|null $lsqFundCode
* @return $this
*/
public function addGir($index, $lloLocationCode, $lfnPurchaseFundCode, $lcvDecimalPrice, $lsqFundCode = '')
{
return $this->addDynamicSegment([
'GIR',
str_pad((string) $index, 3, '0', STR_PAD_LEFT),
[
(string) $lloLocationCode,
'LLO',
],
[
(string) ($lsqFundCode ?? ''),
'LSQ',
],
[
(string) $lfnPurchaseFundCode,
'LFN',
],
[
(string) $lcvDecimalPrice,
'LCV',
],
]);
}

/**
* Register a dynamic segment key while preserving insertion order.
*
* @param array $segment
* @return $this
*/
private function addDynamicSegment($segment)
{
$this->appendedSegments[] = $segment;

return $this;
}
Expand Down Expand Up @@ -294,11 +382,9 @@ public function setFeaturesText($text)
private function splitTexts($varName, $text, $maxLength, $lineLength, $type = 'ZU')
{
$this->{$varName} = str_split(mb_substr($text, 0, $maxLength), $lineLength);
$nr = 0;

foreach ($this->{$varName} as $line) {
$property = $varName . $nr++;
$this->{$property} = self::addIMDSegment($line, $type);
$this->addKeyToCompose($property);
$this->addDynamicSegment(self::addIMDSegment($line, $type));
}

return $this;
Expand Down Expand Up @@ -365,6 +451,19 @@ public function setOrderPosition($orderPosition)
return $this;
}


/**
* Set quote/order line identifier.
*
* @param string $orderPosition
* @return $this
*/
public function setQli($orderPosition)
{
$this->qli = $this->addRFFSegment('QLI', $orderPosition);

return $this;
}
/**
* @return array
*/
Expand Down
30 changes: 27 additions & 3 deletions src/Generator/Traits/ItemPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ trait ItemPrice
/** @var array */
protected $netPrice;

/** @var array */
protected $ourPrice;

/**
* @param $qualifier
* @param $value
Expand All @@ -35,9 +38,9 @@ public static function addPRISegment($qualifier, $value, $priceBase = 1, $priceB
EdiFactNumber::convert($value, $decimals, $format),
'',
'',
(string)$priceBase,
$priceBaseUnit
]
(string) $priceBase,
$priceBaseUnit,
],
];
}

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

return $this;
}

/**
* @param string $ourPrice
* @param string $format
* @param int $decimals
* @return $this
*/
public function setOurPrice($ourPrice, $format = EdiFactNumber::DECIMAL_POINT, $decimals = 2)
{
$this->ourPrice = [
'PRI',
[
'AAE',
EdiFactNumber::convert($ourPrice, $decimals, $format),
],
];

$this->addKeyToCompose('ourPrice');

return $this;
}
}
41 changes: 40 additions & 1 deletion src/Generator/Traits/NameAndAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ public function addNameAndAddress(
if ($name3) {
$name[] = self::maxChars($name3);
}
return [

$segment = [
'NAD',
$type,
[
Expand All @@ -188,6 +189,44 @@ public function addNameAndAddress(
self::maxChars($countryCode, 2),
],
];

return $this->trimTrailingEmptyValues($segment);
}

/**
* @param array $segment
* @return array
*/
private function trimTrailingEmptyValues($segment)
{
while (!empty($segment)) {
$last = end($segment);
if (!$this->isEmptyValue($last)) {
break;
}
array_pop($segment);
}

return $segment;
}

/**
* @param mixed $value
* @return bool
*/
private function isEmptyValue($value)
{
if (is_array($value)) {
foreach ($value as $entry) {
if (!$this->isEmptyValue($entry)) {
return false;
}
}

return true;
}

return $value === '' || $value === null;
}

/**
Expand Down
Loading