Skip to content

Commit

Permalink
+BusinessPartner in OrderRequest
Browse files Browse the repository at this point in the history
+requestedDeliveryDate in OrderRequestHeader
+TransportInformation -> +Route
  • Loading branch information
mathielen committed Sep 20, 2024
1 parent 8fc42d0 commit f94d0b9
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 25 deletions.
3 changes: 3 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
53 changes: 47 additions & 6 deletions src/CXml/Builder/OrderRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -90,6 +103,7 @@ public static function fromPunchOutOrderMessage(
$orderDate,
$currency,
$language,
null,
);

$orb->setShipTo($punchOutOrderMessage->getPunchOutOrderMessageHeader()->getShipTo());
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -297,6 +318,10 @@ private function buildOrderRequestHeader(): OrderRequestHeader
$orh->addExtrinsic($extrinsic);
}

foreach ($this->businessPartners as $businessPartner) {
$orh->addBusinessPartner($businessPartner);
}

return $orh;
}

Expand All @@ -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;
}
}
8 changes: 7 additions & 1 deletion src/CXml/Builder/PunchOutOrderMessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 28 additions & 0 deletions src/CXml/Model/BusinessPartner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace CXml\Model;

use JMS\Serializer\Annotation as Serializer;

#[Serializer\AccessorOrder(order: 'custom', custom: ['type', 'role', 'address', 'idReferences'])]
class BusinessPartner
{
use IdReferencesTrait;
final public const ROLE_SOLD_TO = 'soldTo';

final public const ROLE_SHIP_FROM = 'shipFrom';

final public const ROLE_ORDERING_ADDRESS = 'orderingAddress';

public function __construct(
#[Serializer\XmlAttribute]
private string $role,
#[Serializer\SerializedName('Address')]
private Address $address,
#[Serializer\XmlAttribute]
private string $type = 'organization',
) {
}
}
17 changes: 9 additions & 8 deletions src/CXml/Model/CarrierIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@
#[Serializer\AccessorOrder(order: 'custom', custom: ['value'])]
class CarrierIdentifier
{
final public const DOMAIN_COMPANYNAME = 'companyName';

final public const DOMAIN_SCAC = 'SCAC';

final public const DOMAIN_COMPANYNAME = 'companyName';
final public const DOMAIN_IATA = 'IATA';

final public const DOMAIN_AAR = 'AAR';

final public const DOMAIN_SKU = 'sku';
final public const DOMAIN_UIC = 'UIC';

final public const DOMAIN_CARRIER_METHOD = 'carrierMethod';
final public const DOMAIN_EAN = 'EAN';

final public const DOMAIN_DUNS = 'DUNS';

public function __construct(
#[Serializer\XmlAttribute]
Expand All @@ -25,11 +31,6 @@ public function __construct(
) {
}

public static function fromScacCode(string $scacCarrierCode): self
{
return new self(self::DOMAIN_SCAC, $scacCarrierCode);
}

public function getDomain(): string
{
return $this->domain;
Expand Down
17 changes: 15 additions & 2 deletions src/CXml/Model/Request/OrderRequestHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -38,6 +39,10 @@ class OrderRequestHeader
#[Serializer\SerializedName('SupplierOrderInfo')]
private ?SupplierOrderInfo $supplierOrderInfo = null;

#[Serializer\XmlList(entry: 'BusinessPartner', inline: true)]
#[Serializer\Type('array<CXml\Model\BusinessPartner>')]
private array $businessPartners;

protected function __construct(
#[Serializer\XmlAttribute]
#[Serializer\SerializedName('orderID')]
Expand All @@ -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<CXml\Model\Contact>')]
#[Serializer\XmlList(entry: 'Contact', inline: true)]
private ?array $contacts = null,
Expand All @@ -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
Expand Down Expand Up @@ -157,4 +165,9 @@ public function getSupplierOrderInfo(): ?SupplierOrderInfo
{
return $this->supplierOrderInfo;
}

public function addBusinessPartner(BusinessPartner $businessPartner): void
{
$this->businessPartners[] = $businessPartner;
}
}
20 changes: 20 additions & 0 deletions src/CXml/Model/Route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace CXml\Model;

use JMS\Serializer\Annotation as Serializer;

readonly class Route
{
final public const METHOD_UNKNOWN = 'unknown';

public function __construct(
#[Serializer\XmlValue(cdata: false)]
private string $value,
#[Serializer\XmlAttribute]
private string $method = self::METHOD_UNKNOWN,
) {
}
}
11 changes: 8 additions & 3 deletions src/CXml/Model/TransportInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@

use JMS\Serializer\Annotation as Serializer;

#[Serializer\AccessorOrder(order: 'custom', custom: ['shippingContractNumber'])]
#[Serializer\AccessorOrder(order: 'custom', custom: ['route', 'shippingContractNumber'])]
readonly class TransportInformation
{
public function __construct(
#[Serializer\SerializedName('Route')]
private ?Route $route,
#[Serializer\SerializedName('ShippingContractNumber')]
private ?ShippingContractNumber $shippingContractNumber,
) {
}

public static function fromContractAccountNumber(string $carrierAccountNo): self
public static function create(?string $carrierAccountNo = null, ?string $carrierShippingMethod = null): self
{
return new self(new ShippingContractNumber($carrierAccountNo));
return new self(
null === $carrierShippingMethod ? null : new Route($carrierShippingMethod),
null === $carrierAccountNo ? null : new ShippingContractNumber($carrierAccountNo),
);
}
}
8 changes: 4 additions & 4 deletions tests/CXmlTest/Builder/OrderRequestBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public function testFromPunchOutOrderMessage(): void
$poomXml = file_get_contents(__DIR__ . '/fixtures/poom.xml');
$poom = $serializer->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)
Expand Down
2 changes: 1 addition & 1 deletion tests/CXmlTest/Model/ShipNoticeRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit f94d0b9

Please sign in to comment.