From f94d0b938c93a19b78b75ac4c89bf6abb6411e7f Mon Sep 17 00:00:00 2001 From: Markus Thielen Date: Fri, 20 Sep 2024 08:46:42 +0200 Subject: [PATCH] +BusinessPartner in OrderRequest +requestedDeliveryDate in OrderRequestHeader +TransportInformation -> +Route --- rector.php | 3 ++ src/CXml/Builder/OrderRequestBuilder.php | 53 ++++++++++++++++--- .../Builder/PunchOutOrderMessageBuilder.php | 8 ++- src/CXml/Model/BusinessPartner.php | 28 ++++++++++ src/CXml/Model/CarrierIdentifier.php | 17 +++--- src/CXml/Model/Request/OrderRequestHeader.php | 17 +++++- src/CXml/Model/Route.php | 20 +++++++ src/CXml/Model/TransportInformation.php | 11 ++-- .../Builder/OrderRequestBuilderTest.php | 8 +-- .../CXmlTest/Model/ShipNoticeRequestTest.php | 2 +- 10 files changed, 142 insertions(+), 25 deletions(-) create mode 100644 src/CXml/Model/BusinessPartner.php create mode 100644 src/CXml/Model/Route.php diff --git a/rector.php b/rector.php index 7d5c62e..5c39263 100644 --- a/rector.php +++ b/rector.php @@ -33,6 +33,9 @@ \Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector::class, //allow to use promoted properties that only purpose is to get serialized \Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector::class, + //this is stupid and makes code unreadable + \Rector\Strict\Rector\Ternary\BooleanInTernaryOperatorRuleFixerRector::class, + \Rector\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector::class ]) ->withFileExtensions(['php']) ->withCache( diff --git a/src/CXml/Builder/OrderRequestBuilder.php b/src/CXml/Builder/OrderRequestBuilder.php index 35db0aa..dc73d9d 100644 --- a/src/CXml/Builder/OrderRequestBuilder.php +++ b/src/CXml/Builder/OrderRequestBuilder.php @@ -6,6 +6,7 @@ use CXml\Model\Address; use CXml\Model\BillTo; +use CXml\Model\BusinessPartner; use CXml\Model\Classification; use CXml\Model\Comment; use CXml\Model\Contact; @@ -54,13 +55,25 @@ class OrderRequestBuilder private array $extrinsics = []; - private function __construct(private readonly string $orderId, private readonly DateTimeInterface $orderDate, private readonly string $currency, private readonly string $language = 'en') - { + private array $businessPartners = []; + + private function __construct( + private readonly string $orderId, + private readonly DateTimeInterface $orderDate, + private readonly string $currency, + private readonly string $language = 'en', + private readonly ?DateTimeInterface $requestedDeliveryDate = null, + ) { } - public static function create(string $orderId, DateTimeInterface $orderDate, string $currency, string $language = 'en'): self - { - return new self($orderId, $orderDate, $currency, $language); + public static function create( + string $orderId, + DateTimeInterface $orderDate, + string $currency, + string $language = 'en', + ?DateTimeInterface $requestedDeliveryDate = null, + ): self { + return new self($orderId, $orderDate, $currency, $language, $requestedDeliveryDate); } public static function fromPunchOutOrderMessage( @@ -90,6 +103,7 @@ public static function fromPunchOutOrderMessage( $orderDate, $currency, $language, + null, ); $orb->setShipTo($punchOutOrderMessage->getPunchOutOrderMessageHeader()->getShipTo()); @@ -141,13 +155,19 @@ public function shipTo( PostalAddress $postalAddress, array $carrierIdentifiers = [], string $carrierAccountNo = null, + string $carrierShippingMethod = null, ): self { + $transportInformation = null; + if (null !== $carrierAccountNo || null != $carrierShippingMethod) { + $transportInformation = TransportInformation::create($carrierAccountNo, $carrierShippingMethod); + } + $this->shipTo = new ShipTo( new Address( new MultilanguageString($name, null, $this->language), $postalAddress, ), - null !== $carrierAccountNo && '' !== $carrierAccountNo && '0' !== $carrierAccountNo ? TransportInformation::fromContractAccountNumber($carrierAccountNo) : null, + $transportInformation, ); foreach ($carrierIdentifiers as $domain => $identifier) { @@ -284,6 +304,7 @@ private function buildOrderRequestHeader(): OrderRequestHeader $this->billTo, new MoneyWrapper($this->currency, $this->total), OrderRequestHeader::TYPE_NEW, + $this->requestedDeliveryDate, $this->contacts, ) ->setShipping($this->shipping) @@ -297,6 +318,10 @@ private function buildOrderRequestHeader(): OrderRequestHeader $orh->addExtrinsic($extrinsic); } + foreach ($this->businessPartners as $businessPartner) { + $orh->addBusinessPartner($businessPartner); + } + return $orh; } @@ -318,4 +343,20 @@ public function getItems(): array { return $this->items; } + + public function addBusinessPartner(string $role, string $name, array $idReferences = []): void + { + $bp = new BusinessPartner( + $role, + new Address( + new MultilanguageString($name, null, $this->language), + ), + ); + + foreach ($idReferences as $domain => $identifier) { + $bp->addIdReference($domain, $identifier); + } + + $this->businessPartners[] = $bp; + } } diff --git a/src/CXml/Builder/PunchOutOrderMessageBuilder.php b/src/CXml/Builder/PunchOutOrderMessageBuilder.php index 33ac62f..eb2316d 100644 --- a/src/CXml/Builder/PunchOutOrderMessageBuilder.php +++ b/src/CXml/Builder/PunchOutOrderMessageBuilder.php @@ -65,13 +65,19 @@ public function shipTo( PostalAddress $postalAddress, array $carrierIdentifiers = [], string $carrierAccountNo = null, + string $carrierShippingMethod = null, ): self { + $transportInformation = null; + if (null !== $carrierAccountNo || null != $carrierShippingMethod) { + $transportInformation = TransportInformation::create($carrierAccountNo, $carrierShippingMethod); + } + $this->shipTo = new ShipTo( new Address( new MultilanguageString($name, null, $this->language), $postalAddress, ), - null !== $carrierAccountNo && '' !== $carrierAccountNo && '0' !== $carrierAccountNo ? TransportInformation::fromContractAccountNumber($carrierAccountNo) : null, + $transportInformation, ); foreach ($carrierIdentifiers as $domain => $identifier) { diff --git a/src/CXml/Model/BusinessPartner.php b/src/CXml/Model/BusinessPartner.php new file mode 100644 index 0000000..1e25479 --- /dev/null +++ b/src/CXml/Model/BusinessPartner.php @@ -0,0 +1,28 @@ +domain; diff --git a/src/CXml/Model/Request/OrderRequestHeader.php b/src/CXml/Model/Request/OrderRequestHeader.php index c692281..3eaf84d 100644 --- a/src/CXml/Model/Request/OrderRequestHeader.php +++ b/src/CXml/Model/Request/OrderRequestHeader.php @@ -6,6 +6,7 @@ use Assert\Assertion; use CXml\Model\BillTo; +use CXml\Model\BusinessPartner; use CXml\Model\CommentsTrait; use CXml\Model\Contact; use CXml\Model\ExtrinsicsTrait; @@ -18,7 +19,7 @@ use DateTimeInterface; use JMS\Serializer\Annotation as Serializer; -#[Serializer\AccessorOrder(order: 'custom', custom: ['total', 'shipTo', 'billTo', 'shipping', 'tax', 'contacts', 'comments', 'supplierOrderInfo', 'idReferences', 'extrinsics'])] +#[Serializer\AccessorOrder(order: 'custom', custom: ['total', 'shipTo', 'billTo', 'businessPartners', 'shipping', 'tax', 'contacts', 'comments', 'supplierOrderInfo', 'idReferences', 'extrinsics'])] class OrderRequestHeader { use CommentsTrait; @@ -38,6 +39,10 @@ class OrderRequestHeader #[Serializer\SerializedName('SupplierOrderInfo')] private ?SupplierOrderInfo $supplierOrderInfo = null; + #[Serializer\XmlList(entry: 'BusinessPartner', inline: true)] + #[Serializer\Type('array')] + private array $businessPartners; + protected function __construct( #[Serializer\XmlAttribute] #[Serializer\SerializedName('orderID')] @@ -56,6 +61,8 @@ protected function __construct( private readonly MoneyWrapper $total, #[Serializer\XmlAttribute] private readonly string $type = self::TYPE_NEW, + #[Serializer\XmlAttribute] + private readonly ?DateTimeInterface $requestedDeliveryDate = null, #[Serializer\Type('array')] #[Serializer\XmlList(entry: 'Contact', inline: true)] private ?array $contacts = null, @@ -78,9 +85,10 @@ public static function create( BillTo $billTo, MoneyWrapper $total, string $type = self::TYPE_NEW, + ?DateTimeInterface $requestedDeliveryDate = null, array $contacts = null, ): self { - return new self($orderId, $orderDate, $shipTo, $billTo, $total, $type, $contacts); + return new self($orderId, $orderDate, $shipTo, $billTo, $total, $type, $requestedDeliveryDate, $contacts); } public function getShipping(): ?Shipping @@ -157,4 +165,9 @@ public function getSupplierOrderInfo(): ?SupplierOrderInfo { return $this->supplierOrderInfo; } + + public function addBusinessPartner(BusinessPartner $businessPartner): void + { + $this->businessPartners[] = $businessPartner; + } } diff --git a/src/CXml/Model/Route.php b/src/CXml/Model/Route.php new file mode 100644 index 0000000..7bf7a18 --- /dev/null +++ b/src/CXml/Model/Route.php @@ -0,0 +1,20 @@ +deserialize($poomXml); - $orb = OrderRequestBuilder::fromPunchOutOrderMessage($poom->getMessage()->getPayload()); - $actualOrderRequest = $orb - ->billTo('name') - ->build(); + $actualOrderRequest = + OrderRequestBuilder::fromPunchOutOrderMessage($poom->getMessage()->getPayload()) + ->billTo('name') + ->build(); $actualOrderRequest = Builder::create('cxml-php UserAgent', null, $this) ->payload($actualOrderRequest) diff --git a/tests/CXmlTest/Model/ShipNoticeRequestTest.php b/tests/CXmlTest/Model/ShipNoticeRequestTest.php index 160f04a..8e44a45 100644 --- a/tests/CXmlTest/Model/ShipNoticeRequestTest.php +++ b/tests/CXmlTest/Model/ShipNoticeRequestTest.php @@ -59,7 +59,7 @@ public function testMinimumExample(): void ->addCommentAsString('Got it all into one shipment.', null, 'en-CA'), ) ->addShipControl( - ShipControl::create(CarrierIdentifier::fromScacCode('FDE'), new ShipmentIdentifier('8202 8261 1194')) + ShipControl::create(new CarrierIdentifier(CarrierIdentifier::DOMAIN_SCAC, 'FDE'), new ShipmentIdentifier('8202 8261 1194')) ->addCarrierIdentifier('companyName', 'Federal Express'), ) ->addShipNoticePortion(