diff --git a/src/Generator/Base.php b/src/Generator/Base.php index 0b9f7bc..f5467a2 100755 --- a/src/Generator/Base.php +++ b/src/Generator/Base.php @@ -20,10 +20,9 @@ class Base /** @var string */ protected $receiver; - protected $composeKeys; /** @var string */ -// protected $managingOrganisation = '89'; + // protected $managingOrganisation = '89'; /** * @param $keyName @@ -115,6 +114,46 @@ public function setReceiver($receiver) return $this; } + /** + * Crop String to max char length + * + * @param string $string + * @param int $length + * + * @return string + */ + protected static function maxChars($string, $length = 35) + { + if (empty($string)) { + return ''; + } + + return mb_substr($string, 0, $length); + } + + /** + * + * @param $value + * @param $array + * @param $errorMessage + * + * @throws EdifactException + */ + protected function isAllowed($value, $array, $errorMessage = null) + { + if ($errorMessage === null) { + $errorMessage = 'value: ' . $value . ' is not in allowed values: ' . + ' [' . implode(', ', $array) . '] in ' . get_class($this) . '->' . + debug_backtrace()[1]['function']; + } + if (!in_array($value, $array, true)) { + throw new EdifactException($errorMessage); + } + } + + /** + * SEGMENT UTILITIES + */ /** * @param string, $functionCode @@ -149,10 +188,10 @@ protected function addRFFSegment($functionCode, $identifier) protected function addDTMSegment($date, $type, $formatQualifier = EdifactDate::DATE) { $data = []; - $data[] = (string)$type; + $data[] = (string) $type; if (!empty($date)) { $data[] = EdifactDate::get($date, $formatQualifier); - $data[] = (string)$formatQualifier; + $data[] = (string) $formatQualifier; } return ['DTM', $data]; @@ -177,44 +216,6 @@ public static function addBGMSegment($documentNumber, $type) ]; } - /** - * Crop String to max char length - * - * @param string $string - * @param int $length - * - * @return string - */ - protected static function maxChars($string, $length = 35) - { - if (empty($string)) { - return ''; - } - - return mb_substr($string, 0, $length); - } - - /** - * - * @param $value - * @param $array - * @param $errorMessage - * - * @throws EdifactException - */ - protected function isAllowed($value, $array, $errorMessage = null) - { - if ($errorMessage === null) { - $errorMessage = 'value: ' . $value . ' is not in allowed values: ' . - ' [' . implode(', ', $array) . '] in ' . get_class($this) . '->' . - debug_backtrace()[1]['function']; - } - if (!in_array($value, $array, true)) { - throw new EdifactException($errorMessage); - } - } - - /** * @param $qualifier * @param $value @@ -227,7 +228,7 @@ public static function addMOASegment($qualifier, $value) 'MOA', [ '', - (string)$qualifier, + (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..24506c2 100644 --- a/src/Generator/Interchange.php +++ b/src/Generator/Interchange.php @@ -96,7 +96,7 @@ public function compose() if ($this->appref !== null) { $unb[] = $this->appref; } - d($this->appref); + $temp[] = $unb; foreach ($this->messages as $msg) { foreach ($msg->getComposed() as $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..7af76bf 100644 --- a/src/Generator/Message.php +++ b/src/Generator/Message.php @@ -43,11 +43,23 @@ public function __construct( } else { $this->messageID = $messageID; } + + $this->messageContent = []; } public function setMessageContent($messageContent) { $this->messageContent = $messageContent; + return $this; + } + + public function addSegment($segment) + { + if ($segment instanceof \EDI\Generator\Segment) { + $segment = $segment->compose()->getComposed(); + } + $this->messageContent[] = $segment; + return $this; } public function getMessageID() @@ -94,7 +106,7 @@ public function compose() */ public static function dtmSegment($type, $dtmString, $format = 203) { - return ['DTM', [$type, $dtmString, $format]]; + return self::addDTMSegment($dtmString, $type, $format); } /** @@ -107,7 +119,7 @@ public static function dtmSegment($type, $dtmString, $format = 203) */ public static function rffSegment($functionCode, $identifier) { - return ['RFF', [$functionCode, $identifier]]; + return self::addRFFSegment($functionCode, $identifier); } /** @@ -117,7 +129,7 @@ public static function rffSegment($functionCode, $identifier) * $secondaryLoc = preferred [locode, 139, 6] (if needed) * @param $qualifier * @param $firstLoc - *@param $secondaryLoc + * @param $secondaryLoc * @return array */ public static function locSegment($qualifier, $firstLoc, $secondaryLoc = null) 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..19cf751 100755 --- a/src/Generator/Traits/NameAndAddress.php +++ b/src/Generator/Traits/NameAndAddress.php @@ -694,7 +694,8 @@ public function setInvoiceAddress( $zipCode = '', $city = '', $countryCode = 'DE', - $managingOrganisation = 'ZZZ' + $managingOrganisation = 'ZZZ', + $sender = null ) { $this->invoiceAddress = $this->addNameAndAddress( $name1, @@ -705,7 +706,8 @@ public function setInvoiceAddress( $city, $countryCode, $managingOrganisation, - 'IV' + 'IV', + $sender ?? '' ); return $this; }