diff --git a/src/Helper/MyParcelCollection.php b/src/Helper/MyParcelCollection.php index cfdefe09..fb3ddc60 100644 --- a/src/Helper/MyParcelCollection.php +++ b/src/Helper/MyParcelCollection.php @@ -264,7 +264,7 @@ public function addMultiCollo(AbstractConsignment $consignment, $amount): self * @throws \MyParcelNL\Sdk\src\Exception\ApiException * @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException */ - public function createConcepts(): self + public function createConcepts(bool $asUnrelatedReturn = false): self { $newConsignments = $this->where('consignment_id', '!=', null)->toArray(); $this->addMissingReferenceId(); @@ -275,12 +275,12 @@ public function createConcepts(): self /* @var MyParcelCollection $consignments */ foreach ($grouped as $consignments) { - $headers = MyParcelRequest::HEADER_CONTENT_TYPE_SHIPMENT; + $headers = $asUnrelatedReturn ? MyParcelRequest::HEADER_CONTENT_TYPE_UNRELATED_RETURN_SHIPMENT : MyParcelRequest::HEADER_CONTENT_TYPE_SHIPMENT; if ($consignments->first()->hasSender()) { $headers += MyParcelRequest::HEADER_SET_CUSTOM_SENDER; } - $data = (new CollectionEncode($consignments))->encode(); + $data = (new CollectionEncode($consignments))->encode($asUnrelatedReturn ? 'return_shipments' : 'shipments'); $request = (new MyParcelRequest()) ->setUserAgents($this->getUserAgent()) ->setRequestParameters( @@ -305,6 +305,16 @@ public function createConcepts(): self return $this; } + /** + * @throws \MyParcelNL\Sdk\src\Exception\ApiException + * @throws \MyParcelNL\Sdk\src\Exception\AccountNotActiveException + * @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException + */ + public function createUnrelatedReturns(): self + { + return $this->createConcepts(true); + } + /** * Label prepare wil be active from x number of orders * diff --git a/src/Model/Consignment/AbstractConsignment.php b/src/Model/Consignment/AbstractConsignment.php index 81f3be28..b71a11fc 100755 --- a/src/Model/Consignment/AbstractConsignment.php +++ b/src/Model/Consignment/AbstractConsignment.php @@ -1224,13 +1224,23 @@ public function setBoxNumber(?string $boxNumber): self } /** - * @param array $consignmentEncoded + * @param array $consignmentEncoded + * @param bool $splitStreet todo remove once the API accepts full street for unrelated returns * * @return array * @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException */ - public function encodeStreet(array $consignmentEncoded): array + public function encodeStreet(array $consignmentEncoded, bool $splitStreet = false): array { + if ($splitStreet) { + $consignmentEncoded['recipient']['street'] = $this->getStreet(true); + $consignmentEncoded['recipient']['number'] = $this->getNumber(); + $consignmentEncoded['recipient']['number_suffix'] = $this->getNumberSuffix(); + $consignmentEncoded['recipient']['box_number'] = $this->getBoxNumber(); + $consignmentEncoded['recipient']['street_additional_info'] = $this->getStreetAdditionalInfo(); + + return $consignmentEncoded; + } $consignmentEncoded['recipient']['street'] = $this->getFullStreet(true); $consignmentEncoded['recipient']['street_additional_info'] = $this->getStreetAdditionalInfo(); diff --git a/src/Model/MyParcelRequest.php b/src/Model/MyParcelRequest.php index db05a674..8ebdd1a6 100644 --- a/src/Model/MyParcelRequest.php +++ b/src/Model/MyParcelRequest.php @@ -34,14 +34,11 @@ class MyParcelRequest /** * API headers. */ - public const HEADER_CONTENT_TYPE_SHIPMENT - = [ - 'Content-Type' => 'application/vnd.shipment+json;charset=utf-8;version=1.1', - ]; - 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']; - + public const HEADER_ACCEPT_APPLICATION_PDF = ['Accept' => 'application/pdf']; + public const HEADER_CONTENT_TYPE_SHIPMENT = ['Content-Type' => 'application/vnd.shipment+json;charset=utf-8;version=1.1']; + public const HEADER_CONTENT_TYPE_RETURN_SHIPMENT = ['Content-Type' => 'application/vnd.return_shipment+json; charset=utf-8']; + public const HEADER_CONTENT_TYPE_UNRELATED_RETURN_SHIPMENT = ['Content-Type' => 'application/vnd.unrelated_return_shipment+json;version=1.1; 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'; /* @deprecated use HEADER_CONTENT_TYPE_SHIPMENT, HEADER_ACCEPT_APPLICATION_PDF or HEADER_CONTENT_TYPE_RETURN_SHIPMENT */ diff --git a/src/Services/CollectionEncode.php b/src/Services/CollectionEncode.php index 4ca17da5..b63c6844 100644 --- a/src/Services/CollectionEncode.php +++ b/src/Services/CollectionEncode.php @@ -31,17 +31,19 @@ public function __construct($consignments) /** * Encode multiple shipments so that the data can be sent to MyParcel. * + * @param string $key default 'shipments', the key under 'data' in which the shipments will be returned + * * @return string * @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException */ - public function encode() + public function encode(string $key = 'shipments'): string { $data = []; $groupedConsignments = $this->groupMultiColloConsignments(); foreach ($groupedConsignments as $consignments) { - $data['data']['shipments'][] = (new ConsignmentEncode($consignments))->apiEncode(); + $data['data'][$key][] = (new ConsignmentEncode($consignments))->apiEncode('return_shipments' === $key); } // Remove \\n because json_encode encode \\n for \s diff --git a/src/Services/ConsignmentEncode.php b/src/Services/ConsignmentEncode.php index 6b9579ae..136bfc7e 100644 --- a/src/Services/ConsignmentEncode.php +++ b/src/Services/ConsignmentEncode.php @@ -50,14 +50,15 @@ public function __construct($consignments) /** * Encode all the data before sending it to MyParcel * + * @param bool $splitStreet todo remove this once the API accepts the street as a single field + * * @return array * @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException - * @throws \Exception */ - public function apiEncode(): array + public function apiEncode(bool $splitStreet = false): array { $this->encodeBase() - ->encodeStreet(); + ->encodeStreet($splitStreet); $this->consignmentEncoded = self::encodeExtraOptions( $this->consignmentEncoded, @@ -222,12 +223,14 @@ private function encodeBase(): self } /** + * @param bool $splitStreet todo remove once the API accepts the street as a single field + * * @return self */ - private function encodeStreet(): self + private function encodeStreet(bool $splitStreet): self { $consignment = Arr::first($this->consignments); - $this->consignmentEncoded = $consignment->encodeStreet($this->consignmentEncoded); + $this->consignmentEncoded = $consignment->encodeStreet($this->consignmentEncoded, $splitStreet); return $this; }