diff --git a/src/Helper/MyParcelCollection.php b/src/Helper/MyParcelCollection.php index 9f47c9a2..eaaeed3f 100644 --- a/src/Helper/MyParcelCollection.php +++ b/src/Helper/MyParcelCollection.php @@ -269,15 +269,24 @@ public function createConcepts(): self $newConsignments = $this->where('consignment_id', '!=', null)->toArray(); $this->addMissingReferenceId(); + $grouped = $this->where('consignment_id', null)->groupBy(function(AbstractConsignment $item) { + return $item->getApiKey() . ($item->hasSender() ? '-sender' : ''); + }); + /* @var MyParcelCollection $consignments */ - foreach ($this->where('consignment_id', null)->groupBy('api_key') as $consignments) { + foreach ($grouped as $consignments) { + $headers = MyParcelRequest::HEADER_CONTENT_TYPE_SHIPMENT; + if ($consignments->first()->hasSender()) { + $headers += MyParcelRequest::HEADER_SET_CUSTOM_SENDER; + } + $data = (new CollectionEncode($consignments))->encode(); $request = (new MyParcelRequest()) ->setUserAgents($this->getUserAgent()) ->setRequestParameters( $consignments->first()->apiKey, $data, - MyParcelRequest::HEADER_CONTENT_TYPE_SHIPMENT + $headers ) ->sendRequest(); diff --git a/src/Model/Consignment/AbstractConsignment.php b/src/Model/Consignment/AbstractConsignment.php index 7db65ed6..81f3be28 100755 --- a/src/Model/Consignment/AbstractConsignment.php +++ b/src/Model/Consignment/AbstractConsignment.php @@ -18,6 +18,7 @@ use MyParcelNL\Sdk\src\Model\Carrier\AbstractCarrier; use MyParcelNL\Sdk\src\Model\Carrier\CarrierFactory; use MyParcelNL\Sdk\src\Model\MyParcelCustomsItem; +use MyParcelNL\Sdk\src\Model\Recipient; use MyParcelNL\Sdk\src\Services\CountryCodes; use MyParcelNL\Sdk\src\Services\CountryService; use MyParcelNL\Sdk\src\Support\Str; @@ -522,6 +523,11 @@ abstract class AbstractConsignment */ private $state; + /** + * @var null|\MyParcelNL\Sdk\src\Model\Recipient + */ + private $sender; + /** * @throws \Exception */ @@ -532,6 +538,34 @@ public function __construct() : null; } + /** + * If you set a sender, a feature header will be added to the request. + * Exporting the consignment to MyParcel will throw an error if your shop (by apikey) does not have this permission. + * + * @param Recipient $sender + * @return $this + */ + public function setSender(Recipient $sender): AbstractConsignment + { + $this->sender = $sender; + + return $this; + } + + public function hasSender(): bool + { + return isset($this->sender); + } + + public function getSender(): ?Recipient + { + if (!$this->hasSender()) { + return null; + } + + return $this->sender; + } + /** * @return null|\MyParcelNL\Sdk\src\Model\Carrier\AbstractCarrier */ diff --git a/src/Model/MyParcelRequest.php b/src/Model/MyParcelRequest.php index 3d4aac28..d6c14eac 100644 --- a/src/Model/MyParcelRequest.php +++ b/src/Model/MyParcelRequest.php @@ -40,6 +40,7 @@ class MyParcelRequest ]; public const HEADER_ACCEPT_APPLICATION_PDF = ['Accept' => 'application/pdf']; public const HEADER_CONTENT_TYPE_RETURN_SHIPMENT = ['Content-Type' => 'application/vnd.return_shipment+json; charset=utf-8']; + public const HEADER_SET_CUSTOM_SENDER = ['x-dmp-set-custom-sender' => 'true']; /* @deprecated use HEADER_CONTENT_TYPE_SHIPMENT, HEADER_ACCEPT_APPLICATION_PDF or HEADER_CONTENT_TYPE_RETURN_SHIPMENT */ public const REQUEST_HEADER_SHIPMENT = 'Content-Type: application/vnd.shipment+json;charset=utf-8;version=1.1'; diff --git a/src/Services/CollectionEncode.php b/src/Services/CollectionEncode.php index 5bb794af..4ca17da5 100644 --- a/src/Services/CollectionEncode.php +++ b/src/Services/CollectionEncode.php @@ -55,10 +55,10 @@ private function groupMultiColloConsignments() { return $this->consignments->groupBy(function (AbstractConsignment $consignment) { if ($consignment->isPartOfMultiCollo()) { - return $consignment->getReferenceId(); + return $consignment->getReferenceIdentifier(); } - return 'random_to_prevent_multi_collo_' . uniqid(); + return 'random_to_prevent_multi_collo_' . uniqid('', true); })->toArray(); } } diff --git a/src/Services/ConsignmentEncode.php b/src/Services/ConsignmentEncode.php index be260021..6b9579ae 100644 --- a/src/Services/ConsignmentEncode.php +++ b/src/Services/ConsignmentEncode.php @@ -205,12 +205,17 @@ private function encodeBase(): self 'carrier' => $consignment->getCarrierId(), ]; - if ($consignment->getReferenceId()) { - $this->consignmentEncoded['reference_identifier'] = $consignment->getReferenceId(); + + if (($sender = $consignment->getSender())) { + $this->consignmentEncoded['sender'] = $sender->toArrayWithoutNull(); + } + + if (($id = $consignment->getReferenceIdentifier())) { + $this->consignmentEncoded['reference_identifier'] = $id; } - if ($consignment->getCompany()) { - $this->consignmentEncoded['recipient']['company'] = $consignment->getCompany(); + if (($company = $consignment->getCompany())) { + $this->consignmentEncoded['recipient']['company'] = $company; } return $this;