diff --git a/.gitattributes b/.gitattributes index a9579af..06f15ff 100644 --- a/.gitattributes +++ b/.gitattributes @@ -11,3 +11,4 @@ /phpunit.gitlab.xml export-ignore /gitlab-test-mysql.sh export-ignore /.gitlab-ci.yml export-ignore +codeCoverage/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index af0cf17..bc40637 100755 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,6 @@ coverage coverage.xml phpunit.local.xml .phpunit.result.cache +.phpunit.cache/ composer.lock /tests/_output diff --git a/composer.json b/composer.json index 2b16ce7..2f7c597 100644 --- a/composer.json +++ b/composer.json @@ -15,8 +15,8 @@ "codestyle:lint": "vendor/bin/php-cs-fixer fix --dry-run" }, "require-dev": { - "sabas/edifact": "^0.4.1", - "phpunit/phpunit": "~8.0", + "sabas/edifact": "^1.0", + "phpunit/phpunit": "~11.0", "friendsofphp/php-cs-fixer": "^3.9.5" } } diff --git a/phpunit.xml b/phpunit.xml index bde5a57..ab96210 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,21 +1,13 @@ - - - - ./tests/GeneratorTest - - - - - ./src - - + + + + ./tests/GeneratorTest + + + + + ./src + + diff --git a/src/Generator/Base.php b/src/Generator/Base.php index 0b9f7bc..f1031f8 100755 --- a/src/Generator/Base.php +++ b/src/Generator/Base.php @@ -2,6 +2,8 @@ namespace EDI\Generator; +use EDI\Generator\Traits\Segments; + /** * Class Base * @@ -9,6 +11,8 @@ */ class Base { + use Segments; + /** @var array */ protected $messageContent = []; @@ -16,14 +20,7 @@ class Base protected $composed; /** @var string */ - protected $sender; - - /** @var string */ - protected $receiver; - protected $composeKeys; - - /** @var string */ -// protected $managingOrganisation = '89'; + // protected $managingOrganisation = '89'; /** * @param $keyName @@ -34,7 +31,7 @@ public function addKeyToCompose($keyName) } /** - * compose message by keys givven in an ordered array + * compose message by keys given in an ordered array * * @param array $keys * @@ -75,108 +72,6 @@ public function getComposed() return $this->composed; } - /** - * @return string - */ - public function getSender() - { - return $this->sender; - } - - /** - * @param string $sender - * - * @return $this - */ - public function setSender($sender) - { - $this->sender = $sender; - - return $this; - } - - /** - * @return string - */ - public function getReceiver() - { - return $this->receiver; - } - - /** - * @param string $receiver - * - * @return $this - */ - public function setReceiver($receiver) - { - $this->receiver = $receiver; - - return $this; - } - - - /** - * @param string, $functionCode - * @param $identifier - * - * @return array|bool - */ - protected function addRFFSegment($functionCode, $identifier) - { - if (empty($identifier)) { - return false; - } - - return [ - 'RFF', - [ - $functionCode, - self::maxChars($identifier, 35), - ], - ]; - } - - /** - * @param string|\DateTime $date - * @param string $type - * @param int $formatQualifier - * - * @return array - * @throws EdifactException - * @see http://www.unece.org/trade/untdid/d96a/trsd/trsddtm.htm - */ - protected function addDTMSegment($date, $type, $formatQualifier = EdifactDate::DATE) - { - $data = []; - $data[] = (string)$type; - if (!empty($date)) { - $data[] = EdifactDate::get($date, $formatQualifier); - $data[] = (string)$formatQualifier; - } - - return ['DTM', $data]; - } - - /** - * @param $documentNumber - * @param $type - * - * @return array - */ - public static function addBGMSegment($documentNumber, $type) - { - return [ - 'BGM', - [ - $type, - '', - '89', - ], - $documentNumber, - ]; - } - /** * Crop String to max char length * @@ -214,22 +109,4 @@ protected function isAllowed($value, $array, $errorMessage = null) } } - - /** - * @param $qualifier - * @param $value - * - * @return array - */ - public static function addMOASegment($qualifier, $value) - { - return [ - 'MOA', - [ - '', - (string)$qualifier, - EdiFactNumber::convert($value), - ], - ]; - } } diff --git a/src/Generator/Iftmin.php b/src/Generator/Iftmin.php index c766944..ead24c3 100644 --- a/src/Generator/Iftmin.php +++ b/src/Generator/Iftmin.php @@ -77,6 +77,16 @@ public function setDeliveryDateRange($earliest, $latest) return $this; } + /** + * @param $date + * @return $this + */ + public function setDeliveryDate($date) + { + $this->deliveryDate = self::dtmSegment(2, $date); + return $this; + } + /** * $currency ISO 4217-3 * @param $price @@ -202,23 +212,46 @@ public function compose(?string $sMessageFunctionCode = "5", ?string $sDocumentN $this->messageContent[] = $this->messageSender; $this->messageContent[] = $this->messageSenderInformation; $this->messageContent[] = $this->dtmSend; - $this->messageContent[] = $this->pickupDate[0]; - $this->messageContent[] = $this->pickupDate[1]; - $this->messageContent[] = $this->deliveryDate[0]; - $this->messageContent[] = $this->deliveryDate[1]; - $this->messageContent[] = $this->agreedAmount; - $this->messageContent[] = $this->freeTextInstructions; - $this->messageContent[] = $this->weight; - $this->messageContent[] = $this->cargoNature; - $this->messageContent[] = $this->transportOrderNumber; - $this->messageContent[] = $this->booking; + if (isset($this->pickupDate)) { + $this->messageContent[] = $this->pickupDate[0]; + $this->messageContent[] = $this->pickupDate[1]; + } + if (isset($this->deliveryDate)) { + if (count($this->deliveryDate) > 2) { + $this->messageContent[] = $this->deliveryDate[0]; + $this->messageContent[] = $this->deliveryDate[1]; + } else { + $this->messageContent[] = $this->deliveryDate; + } + } + if ($this->agreedAmount !== null) { + $this->messageContent[] = $this->agreedAmount; + } + if ($this->freeTextInstructions !== null) { + $this->messageContent[] = $this->freeTextInstructions; + } + if ($this->weight !== null) { + $this->messageContent[] = $this->weight; + } + if ($this->cargoNature !== null) { + $this->messageContent[] = $this->cargoNature; + } + if ($this->transportOrderNumber !== null) { + $this->messageContent[] = $this->transportOrderNumber; + } + if ($this->booking !== null) { + $this->messageContent[] = $this->booking; + } if ($this->bookingSequence !== null) { $this->messageContent[] = $this->bookingSequence; } - $this->messageContent[] = $this->vessel; - - $this->messageContent[] = $this->weightKg; + if ($this->vessel !== null) { + $this->messageContent[] = $this->vessel; + } + if ($this->weightKg !== null) { + $this->messageContent[] = $this->weightKg; + } return parent::compose(); } } diff --git a/src/Generator/Interchange.php b/src/Generator/Interchange.php index d4e3d48..0c7e101 100644 --- a/src/Generator/Interchange.php +++ b/src/Generator/Interchange.php @@ -65,6 +65,12 @@ public function setCharset($identifier, $version) return $this; } + /** + * Set the application reference + * $appref Application reference + * @param $appref + * @return $this + */ public function setApplicationReference($appref) { $this->appref = $appref; @@ -72,7 +78,6 @@ public function setApplicationReference($appref) return $this; } - /** * Add a Message to the Interchange * @param $msg @@ -85,6 +90,15 @@ public function addMessage($msg) return $this; } + /** + * Return the messages array + * @return array + */ + public function getMessages() + { + return $this->messages; + } + /** * Format the Interchange segments * @return $this @@ -94,12 +108,17 @@ 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; } - d($this->appref); + $temp[] = $unb; foreach ($this->messages as $msg) { - foreach ($msg->getComposed() as $i) { + $msgContent = $msg->getComposed(); + if ($msgContent === null) { + $msgContent = $msg->compose()->getComposed(); + } + foreach ($msgContent as $i) { $temp[] = $i; } } diff --git a/src/Generator/Invoic/Item.php b/src/Generator/Invoic/Item.php index f02f310..e4b95f5 100755 --- a/src/Generator/Invoic/Item.php +++ b/src/Generator/Invoic/Item.php @@ -11,6 +11,7 @@ * Class Item * @package EDI\Generator\Invoic */ +#[\AllowDynamicProperties] class Item extends Base { use ItemTrait; @@ -119,7 +120,6 @@ public function addDiscount($value, $discountType = self::DISCOUNT_TYPE_PERCENT) $index = 'discount' . $this->discountIndex++; $this->{$index} = [ 'ALC', - '', floatval($value) > 0 ? 'C' : 'A', '', '', diff --git a/src/Generator/Message.php b/src/Generator/Message.php index 84b6521..f707a24 100644 --- a/src/Generator/Message.php +++ b/src/Generator/Message.php @@ -43,11 +43,25 @@ public function __construct( } else { $this->messageID = $messageID; } + + $this->messageContent = []; } public function setMessageContent($messageContent) { $this->messageContent = $messageContent; + return $this; + } + + public function getMessageContent() + { + return $this->messageContent; + } + + public function addSegment($segment) + { + $this->messageContent[] = $segment; + return $this; } public function getMessageID() @@ -55,6 +69,12 @@ public function getMessageID() return $this->messageID; } + public function setMessageID($messageId) + { + $this->messageID = $messageId; + return $this; + } + /** * Compose. * @throws \EDI\Generator\EdifactException @@ -72,6 +92,9 @@ public function compose() // Segments foreach ($this->messageContent as $i) { + if ($i instanceof \EDI\Generator\Segment) { + $i = $i->compose()->getComposed(); + } $aComposed[] = $i; } @@ -83,139 +106,4 @@ public function compose() return $this; } - /** - * DTM segment - * $type = 7 (actual date time), 132 (estimated date time), 137 (message date time), 798 (weight date time) - * $format = 203 (CCYYMMDDHHII) - * @param $type - * @param $dtmString - * @param int $format - * @return array - */ - public static function dtmSegment($type, $dtmString, $format = 203) - { - return ['DTM', [$type, $dtmString, $format]]; - } - - /** - * RFF segment - * $functionCode = DE 1153 - * $identifier = max 35 alphanumeric chars - * @param $functionCode - * @param $identifier - * @return array - */ - public static function rffSegment($functionCode, $identifier) - { - return ['RFF', [$functionCode, $identifier]]; - } - - /** - * LOC segment - * $qualifier = DE 3227 - * $firstLoc = preferred [locode, 139, 6] - * $secondaryLoc = preferred [locode, 139, 6] (if needed) - * @param $qualifier - * @param $firstLoc - *@param $secondaryLoc - * @return array - */ - public static function locSegment($qualifier, $firstLoc, $secondaryLoc = null) - { - $loc = ['LOC', $qualifier, $firstLoc]; - if ($secondaryLoc !== null) { - $loc[] = $secondaryLoc; - } - - return $loc; - } - - /** - * EQD segment - * $eqpType = DE 8053 (for a container CN) - * $eqpIdentification = for a container [A-Z]{3}U\d{7} - * $dimension = [XXXX, 102, 5] - * $supplier = DE 8077, but usually empty - * $statusCode = DE 8249 - * $fullEmptyIndicatorCode = DE 8169 - * @param $eqpType - * @param $eqpIdentification - * @param $dimension - *@param $supplier - *@param $statusCode - *@param $fullEmptyIndicatorCode - * @return array - */ - public static function eqdSegment($eqpType, $eqpIdentification, $dimension, $supplier = null, $statusCode = null, $fullEmptyIndicatorCode = null) - { - $eqd = ['EQD', $eqpType, $eqpIdentification, $dimension]; - if ($supplier !== null) { - $eqd[] = $supplier; - } - if ($statusCode !== null) { - $eqd[] = $statusCode; - } - if ($fullEmptyIndicatorCode !== null) { - $eqd[] = $fullEmptyIndicatorCode; - } - - return $eqd; - } - - /** - * TDT segment - * $stageQualifier = DE 8051 - * $journeyIdentifier = max 17 alphanumeric chars - * $modeOfTransport = DE 8067 (not used) - * $transportMeans = DE 8179 (not used) - * $carrier - * $transitDirection = DE 8101 (not used) - * $$excessTransportation = DE 8457 (not used) - * $transportationIdentification - * @param $stageQualifier - * @param $journeyIdentifier - * @param $modeOfTransport - * @param $transportMeans - * @param $carrier - * @param $transitDirection - * @param $excessTransportation - * @param $transportationIdentification - * @return array - */ - public static function tdtSegment($stageQualifier, $journeyIdentifier, $modeOfTransport, $transportMeans, $carrier, $transitDirection, $excessTransportation, $transportationIdentification) - { - return ['TDT', $stageQualifier, $journeyIdentifier, $modeOfTransport, $transportMeans, $carrier, $transitDirection, $excessTransportation, $transportationIdentification]; - } - - /** - * @param $stageQualifier - * @param $journeyIdentifier - * @param $modeOfTransport - * @param $transportMeans - * @return array - */ - public static function tdtShortSegment($stageQualifier, $journeyIdentifier, $modeOfTransport, $transportMeans, $carrier = null) - { - $tdt = ['TDT', $stageQualifier, $journeyIdentifier, $modeOfTransport, $transportMeans]; - if ($carrier !== null) { - $tdt[] = $carrier; - } - return $tdt; - } - - /** - * @param string $text - * @param string $qualifier - * @param string $reference - * @return array - */ - public static function addFTXSegment($text, $qualifier, $reference = '') - { - $textLines = str_split($text, 70); - if (count($textLines) > 5) { - $textLines = array_slice($textLines, 0, 5); - } - - return ['FTX', $qualifier, '', [$reference, '89'], $textLines]; - } } diff --git a/src/Generator/Segment.php b/src/Generator/Segment.php index 53f585b..2a6ea9b 100644 --- a/src/Generator/Segment.php +++ b/src/Generator/Segment.php @@ -30,4 +30,14 @@ public function setComposed(array $aComposed): void { $this->aComposed = $aComposed; } + + /** + * Compose. + * + * @return self + */ + public function compose(): self + { + return $this; + } } diff --git a/src/Generator/Segment/BeginningOfMessage.php b/src/Generator/Segment/BeginningOfMessage.php new file mode 100644 index 0000000..a8b9ecc --- /dev/null +++ b/src/Generator/Segment/BeginningOfMessage.php @@ -0,0 +1,188 @@ +aDocument = $aDocument; + + return $this; + } + + /** + * Set Document Identification (C106). + * + * @param string|null $sDocumentIdentifier + * @param string|null $sVersionIdentifier + * @param string|null $sRevisionIdentifier + * + * @return self + */ + public function setDocumentIdentification( + ?string $sDocumentIdentifier = null, + ?string $sVersionIdentifier = null, + ?string $sRevisionIdentifier = null + ): self { + $aDocumentIdentification = []; + + if ($sDocumentIdentifier !== null) { + $aDocumentIdentification[] = $sDocumentIdentifier; + } + + if ($sVersionIdentifier !== null) { + $aDocumentIdentification[] = $sVersionIdentifier; + } + + if ($sRevisionIdentifier !== null) { + $aDocumentIdentification[] = $sRevisionIdentifier; + } + + $this->aDocumentIdentification = $aDocumentIdentification; + + return $this; + } + + /** + * Set Message Function Code. + * + * @param string $sMessageFunctionCode + * + * @return self + */ + public function setMessageFunctionCode(string $sMessageFunctionCode): self + { + $this->sMessageFunctionCode = $sMessageFunctionCode; + + return $this; + } + + /** + * Set Response Type Code. + * + * @param string $sResponseTypeCode + * + * @return self + */ + public function setResponseTypeCode(string $sResponseTypeCode): self + { + $this->sResponseTypeCode = $sResponseTypeCode; + + return $this; + } + + /** + * Set Document Status Code. + * + * @param string $sDocumentStatusCode + * + * @return self + */ + public function setDocumentStatusCode(string $sDocumentStatusCode): self + { + $this->sDocumentStatusCode = $sDocumentStatusCode; + + return $this; + } + + /** + * Set Language Name Code. + * + * @param string $sLanguageNameCode + * + * @return self + */ + public function setLanguageNameCode(string $sLanguageNameCode): self + { + $this->sLanguageNameCode = $sLanguageNameCode; + + return $this; + } + + /** + * Compose. + * + * @return self + */ + public function compose(): self + { + $aComposed = [self::SEGMENT_NAME]; + + if (!empty($this->aDocument)) { + $aComposed[] = $this->aDocument; + } + + if (!empty($this->aDocumentIdentification)) { + $aComposed[] = $this->aDocumentIdentification; + } + + if ($this->sMessageFunctionCode !== null) { + $aComposed[] = $this->sMessageFunctionCode; + } + + if ($this->sResponseTypeCode !== null) { + $aComposed[] = $this->sResponseTypeCode; + } + + if ($this->sDocumentStatusCode !== null) { + $aComposed[] = $this->sDocumentStatusCode; + } + + if ($this->sLanguageNameCode !== null) { + $aComposed[] = $this->sLanguageNameCode; + } + + $this->setComposed($aComposed); + + return $this; + } +} \ No newline at end of file diff --git a/src/Generator/Segment/TransportInformation.php b/src/Generator/Segment/TransportInformation.php new file mode 100644 index 0000000..aacc6d3 --- /dev/null +++ b/src/Generator/Segment/TransportInformation.php @@ -0,0 +1,349 @@ +sTransportStageCodeQualifier = $sTransportStageCodeQualifier; + return $this; + } + + /** + * Set Means of Transport Journey Identifier. + * + * @param string $sMeansOfTransportJourneyIdentifier + * @return self + */ + public function setMeansOfTransportJourneyIdentifier(string $sMeansOfTransportJourneyIdentifier): self + { + $this->sMeansOfTransportJourneyIdentifier = $sMeansOfTransportJourneyIdentifier; + return $this; + } + + /** + * Set Mode of Transport (C220). + * + * @param string|null $sTransportModeNameCode + * @param string|null $sTransportModeName + * @return self + */ + public function setModeOfTransport(?string $sTransportModeNameCode = null, ?string $sTransportModeName = null): self + { + $aModeOfTransport = []; + if ($sTransportModeNameCode !== null) { + $aModeOfTransport[] = $sTransportModeNameCode; + } + if ($sTransportModeName !== null) { + $aModeOfTransport[] = $sTransportModeName; + } + $this->aModeOfTransport = $aModeOfTransport; + return $this; + } + + /** + * Set Transport Means (C001). + * + * @param string|null $sTransportMeansDescriptionCode + * @param string|null $sCodeListIdentificationCode + * @param string|null $sCodeListResponsibleAgencyCode + * @param string|null $sTransportMeansDescription + * @return self + */ + public function setTransportMeans( + ?string $sTransportMeansDescriptionCode = null, + ?string $sCodeListIdentificationCode = null, + ?string $sCodeListResponsibleAgencyCode = null, + ?string $sTransportMeansDescription = null + ): self { + $aTransportMeans = []; + if ($sTransportMeansDescriptionCode !== null) { + $aTransportMeans[] = $sTransportMeansDescriptionCode; + } + if ($sCodeListIdentificationCode !== null) { + $aTransportMeans[] = $sCodeListIdentificationCode; + } + if ($sCodeListResponsibleAgencyCode !== null) { + $aTransportMeans[] = $sCodeListResponsibleAgencyCode; + } + if ($sTransportMeansDescription !== null) { + $aTransportMeans[] = $sTransportMeansDescription; + } + $this->aTransportMeans = $aTransportMeans; + return $this; + } + + /** + * Set Carrier (C040). + * + * @param string|null $sCarrierIdentifier + * @param string|null $sCodeListIdentificationCode + * @param string|null $sCodeListResponsibleAgencyCode + * @param string|null $sCarrierName + * @return self + */ + public function setCarrier( + ?string $sCarrierIdentifier = null, + ?string $sCodeListIdentificationCode = null, + ?string $sCodeListResponsibleAgencyCode = null, + ?string $sCarrierName = null + ): self { + $aCarrier = []; + if ($sCarrierIdentifier !== null) { + $aCarrier[] = $sCarrierIdentifier; + } + if ($sCodeListIdentificationCode !== null) { + $aCarrier[] = $sCodeListIdentificationCode; + } + if ($sCodeListResponsibleAgencyCode !== null) { + $aCarrier[] = $sCodeListResponsibleAgencyCode; + } + if ($sCarrierName !== null) { + $aCarrier[] = $sCarrierName; + } + $this->aCarrier = $aCarrier; + return $this; + } + + /** + * Set Transit Direction Indicator Code. + * + * @param string $sTransitDirectionIndicatorCode + * @return self + */ + public function setTransitDirectionIndicatorCode(string $sTransitDirectionIndicatorCode): self + { + $this->sTransitDirectionIndicatorCode = $sTransitDirectionIndicatorCode; + return $this; + } + + /** + * Set Excess Transportation Information (C401). + * + * @param string $sExcessTransportationReasonCode + * @param string $sExcessTransportationResponsibilityCode + * @param string|null $sCustomerShipmentAuthorisationIdentifier + * @return self + */ + public function setExcessTransportationInformation( + string $sExcessTransportationReasonCode, + ?string $sExcessTransportationResponsibilityCode = null, + ?string $sCustomerShipmentAuthorisationIdentifier = null + ): self { + $aExcessTransportationInformation = [ + $sExcessTransportationReasonCode + ]; + if ($sExcessTransportationResponsibilityCode !== null) { + $aExcessTransportationInformation[] = $sExcessTransportationResponsibilityCode; + } + if ($sCustomerShipmentAuthorisationIdentifier !== null) { + $aExcessTransportationInformation[] = $sCustomerShipmentAuthorisationIdentifier; + } + $this->aExcessTransportationInformation = $aExcessTransportationInformation; + return $this; + } + + /** + * Set Transport Identification (C222). + * + * @param string|null $sTransportMeansIdentificationNameIdentifier + * @param string|null $sCodeListIdentificationCode + * @param string|null $sCodeListResponsibleAgencyCode + * @param string|null $sTransportMeansIdentificationName + * @param string|null $sTransportMeansNationalityCode + * @return self + */ + public function setTransportIdentification( + ?string $sTransportMeansIdentificationNameIdentifier = null, + ?string $sCodeListIdentificationCode = null, + ?string $sCodeListResponsibleAgencyCode = null, + ?string $sTransportMeansIdentificationName = null, + ?string $sTransportMeansNationalityCode = null + ): self { + $aTransportIdentification = []; + if ($sTransportMeansIdentificationNameIdentifier !== null) { + $aTransportIdentification[] = $sTransportMeansIdentificationNameIdentifier; + } + if ($sCodeListIdentificationCode !== null) { + $aTransportIdentification[] = $sCodeListIdentificationCode; + } + if ($sCodeListResponsibleAgencyCode !== null) { + $aTransportIdentification[] = $sCodeListResponsibleAgencyCode; + } + if ($sTransportMeansIdentificationName !== null) { + $aTransportIdentification[] = $sTransportMeansIdentificationName; + } + if ($sTransportMeansNationalityCode !== null) { + $aTransportIdentification[] = $sTransportMeansNationalityCode; + } + $this->aTransportIdentification = $aTransportIdentification; + return $this; + } + + /** + * Set Transport Means Ownership Indicator Code. + * + * @param string $sTransportMeansOwnershipIndicatorCode + * @return self + */ + public function setTransportMeansOwnershipIndicatorCode(string $sTransportMeansOwnershipIndicatorCode): self + { + $this->sTransportMeansOwnershipIndicatorCode = $sTransportMeansOwnershipIndicatorCode; + return $this; + } + + /** + * Set Power Type (C003). + * + * @param string|null $sPowerTypeCode + * @param string|null $sCodeListIdentificationCode + * @param string|null $sCodeListResponsibleAgencyCode + * @param string|null $sPowerTypeDescription + * @return self + */ + public function setPowerType( + ?string $sPowerTypeCode = null, + ?string $sCodeListIdentificationCode = null, + ?string $sCodeListResponsibleAgencyCode = null, + ?string $sPowerTypeDescription = null + ): self { + $aPowerType = []; + if ($sPowerTypeCode !== null) { + $aPowerType[] = $sPowerTypeCode; + } + if ($sCodeListIdentificationCode !== null) { + $aPowerType[] = $sCodeListIdentificationCode; + } + if ($sCodeListResponsibleAgencyCode !== null) { + $aPowerType[] = $sCodeListResponsibleAgencyCode; + } + if ($sPowerTypeDescription !== null) { + $aPowerType[] = $sPowerTypeDescription; + } + $this->aPowerType = $aPowerType; + return $this; + } + + /** + * Set Transport Service (C290). + * + * @param string|null $sTransportServiceIdentificationCode + * @param string|null $sCodeListIdentificationCode + * @param string|null $sCodeListResponsibleAgencyCode + * @param string|null $sTransportServiceName + * @param string|null $sTransportServiceDescription + * @return self + */ + public function setTransportService( + ?string $sTransportServiceIdentificationCode = null, + ?string $sCodeListIdentificationCode = null, + ?string $sCodeListResponsibleAgencyCode = null, + ?string $sTransportServiceName = null, + ?string $sTransportServiceDescription = null + ): self { + $aTransportService = []; + if ($sTransportServiceIdentificationCode !== null) { + $aTransportService[] = $sTransportServiceIdentificationCode; + } + if ($sCodeListIdentificationCode !== null) { + $aTransportService[] = $sCodeListIdentificationCode; + } + if ($sCodeListResponsibleAgencyCode !== null) { + $aTransportService[] = $sCodeListResponsibleAgencyCode; + } + if ($sTransportServiceName !== null) { + $aTransportService[] = $sTransportServiceName; + } + if ($sTransportServiceDescription !== null) { + $aTransportService[] = $sTransportServiceDescription; + } + $this->aTransportService = $aTransportService; + return $this; + } + + /** + * Compose the segment. + * + * @return self + */ + public function compose(): self + { + $aComposed = [self::SEGMENT_NAME]; + + if ($this->sTransportStageCodeQualifier !== null) { + $aComposed[] = $this->sTransportStageCodeQualifier; + } + + if ($this->sMeansOfTransportJourneyIdentifier !== null) { + $aComposed[] = $this->sMeansOfTransportJourneyIdentifier; + } + + if (!empty($this->aModeOfTransport)) { + $aComposed[] = $this->aModeOfTransport; + } + + if (!empty($this->aTransportMeans)) { + $aComposed[] = $this->aTransportMeans; + } + + if (!empty($this->aCarrier)) { + $aComposed[] = $this->aCarrier; + } + + if ($this->sTransitDirectionIndicatorCode !== null) { + $aComposed[] = $this->sTransitDirectionIndicatorCode; + } + + if (!empty($this->aExcessTransportationInformation)) { + $aComposed[] = $this->aExcessTransportationInformation; + } + + if (!empty($this->aTransportIdentification)) { + $aComposed[] = $this->aTransportIdentification; + } + + if ($this->sTransportMeansOwnershipIndicatorCode !== null) { + $aComposed[] = $this->sTransportMeansOwnershipIndicatorCode; + } + + if (!empty($this->aPowerType)) { + $aComposed[] = $this->aPowerType; + } + + if (!empty($this->aTransportService)) { + $aComposed[] = $this->aTransportService; + } + + $this->setComposed($aComposed); + + return $this; + } +} \ No newline at end of file diff --git a/src/Generator/Traits/NameAndAddress.php b/src/Generator/Traits/NameAndAddress.php index ea959a8..3960be6 100755 --- a/src/Generator/Traits/NameAndAddress.php +++ b/src/Generator/Traits/NameAndAddress.php @@ -157,10 +157,6 @@ public function addNameAndAddress( $type, $sender = '' ) { - if ($sender === null) { - $sender = $this->sender; - } - $name = [ self::maxChars($name1), ]; @@ -174,7 +170,7 @@ public function addNameAndAddress( 'NAD', $type, [ - self::maxChars($sender), + self::maxChars($sender ?? ''), '', $managingOrganisation, ], @@ -694,7 +690,8 @@ public function setInvoiceAddress( $zipCode = '', $city = '', $countryCode = 'DE', - $managingOrganisation = 'ZZZ' + $managingOrganisation = 'ZZZ', + $sender = null ) { $this->invoiceAddress = $this->addNameAndAddress( $name1, @@ -705,7 +702,8 @@ public function setInvoiceAddress( $city, $countryCode, $managingOrganisation, - 'IV' + 'IV', + $sender ?? '' ); return $this; } diff --git a/src/Generator/Traits/Segments.php b/src/Generator/Traits/Segments.php new file mode 100644 index 0000000..b9f9892 --- /dev/null +++ b/src/Generator/Traits/Segments.php @@ -0,0 +1,229 @@ + 5) { + $textLines = array_slice($textLines, 0, 5); + } + + return ['FTX', $qualifier, '', [$reference, '89'], $textLines]; + } + + /** + * SEGMENT UTILITIES + */ + + /** + * @param string, $functionCode + * @param $identifier + * + * @return array|bool + */ + protected static function addRFFSegment($functionCode, $identifier) + { + if (empty($identifier)) { + return false; + } + + return [ + 'RFF', + [ + $functionCode, + self::maxChars($identifier, 35), + ], + ]; + } + + /** + * @param string|\DateTime $date + * @param string $type + * @param int $formatQualifier + * + * @return array + * @throws EdifactException + * @see http://www.unece.org/trade/untdid/d96a/trsd/trsddtm.htm + */ + protected static function addDTMSegment($date, $type, $formatQualifier = EdifactDate::DATE) + { + $data = []; + $data[] = (string) $type; + if (!empty($date)) { + $data[] = EdifactDate::get($date, $formatQualifier); + $data[] = (string) $formatQualifier; + } + + return ['DTM', $data]; + } + + /** + * @param $documentNumber + * @param $type + * + * @return array + */ + public static function addBGMSegment($documentNumber, $type) + { + return [ + 'BGM', + [ + $type, + '', + '89', + ], + $documentNumber, + ]; + } + + /** + * @param $qualifier + * @param $value + * + * @return array + */ + public static function addMOASegment($qualifier, $value) + { + return [ + 'MOA', + [ + '', + (string) $qualifier, + EdiFactNumber::convert($value), + ], + ]; + } +} \ No newline at end of file diff --git a/tests/GeneratorTest/DesadvTest.php b/tests/GeneratorTest/DesadvTest.php index 4a27bf0..24176e2 100755 --- a/tests/GeneratorTest/DesadvTest.php +++ b/tests/GeneratorTest/DesadvTest.php @@ -126,8 +126,6 @@ public function testDesadv() try { $desadv = (new Desadv()) - ->setSender('UNB-Identifier-Sender') - ->setReceiver('GC-Gruppe') ->setDeliveryNoteNumber(Desadv::DELIVER_NOTE, 'LS123456789') ->setDeliveryNoteDate($this->getDateTime()) ->setDeliveryDate($this->getDateTime()) diff --git a/tests/GeneratorTest/InvoiceItemTest.php b/tests/GeneratorTest/InvoicItemTest.php old mode 100755 new mode 100644 similarity index 100% rename from tests/GeneratorTest/InvoiceItemTest.php rename to tests/GeneratorTest/InvoicItemTest.php diff --git a/tests/GeneratorTest/SamplesTest.php b/tests/GeneratorTest/SamplesTest.php new file mode 100644 index 0000000..c17703f --- /dev/null +++ b/tests/GeneratorTest/SamplesTest.php @@ -0,0 +1,76 @@ +setMessageSender('IC', '', 'JOHN DOE') + ->setMessageSenderInformation('EM', 'TEST@EXAMPLE.COM'); + + $oContainer = (new \EDI\Generator\Vermas\Container()) + ->setContainer('CBHU1234567', '22G1') + ->setBooking('4001234567', '1') + ->setSeal('45545', 'CA') + ->setMeasures('VGM', '1212') + ->setWeighDate() + ->setWeighMethod('SM1', 'DEFAULT') + ->setShipper('MY COMPANY') + ->setSpcContact('RP', 'JOHN DOE', 'EM', 'JOHN@EXAMPLE.COM'); + + $oVermas = $oVermas->addContainer($oContainer); + + $oVermas = $oVermas->compose(5, 749); + + $aComposed = $oInterchange->addMessage($oVermas)->getComposed(); + + $result = (new \EDI\Encoder($aComposed, false))->get(); + + self::assertStringContainsString('UNT+17', $result); + self::assertStringContainsString('EQD+CN+CBHU1234567+22G1:6346:306', $result); + } + + public function testCustomMessage() { + $oInterchange = (new \EDI\Generator\Interchange('ME', 'YOU')) + ->setCharset('UNOC', 2); + + $oBase = (new \EDI\Generator\Message('IFTMIN', 'D', '04A', 'UN')); + + $bgm = (new \EDI\Generator\Segment\BeginningOfMessage()) + ->setDocument('99') + ->setDocumentIdentification($oBase->getMessageID()) + ->setMessageFunctionCode('9'); + + $oBase->addSegment($bgm); + + $dtm = (new \EDI\Generator\Segment\DateTimePeriod()) + ->setDateOrTimeOrPeriodFunctionCodeQualifier('137') + ->setDateOrTimeOrPeriodText(date('YmdHi')) + ->setDateOrTimeOrPeriodFormatCode('203'); + + $oBase->addSegment($dtm); + + $oBase = $oBase->compose(); + $aComposed = $oInterchange->addMessage($oBase)->getComposed(); + // + $this->assertCount(6, $aComposed); + + $msgs = $oInterchange->getMessages(); + $this->assertCount(1, $msgs); + $this->assertInstanceOf(\EDI\Generator\Message::class, $msgs[0]); + + $result = (new \EDI\Encoder($aComposed, false))->get(); + + self::assertStringContainsString('UNT+4', $result); + self::assertStringContainsString('IFTMIN:D:04A:UN', $result); + } +}