Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support creating unrelated returns #523

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Helper/MyParcelCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(
Expand All @@ -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
*
Expand Down
14 changes: 12 additions & 2 deletions src/Model/Consignment/AbstractConsignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
13 changes: 5 additions & 8 deletions src/Model/MyParcelRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
6 changes: 4 additions & 2 deletions src/Services/CollectionEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions src/Services/ConsignmentEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down