Skip to content

Commit

Permalink
Merge pull request #229 from MyParcelCOM/feature/populate-maste-with-…
Browse files Browse the repository at this point in the history
…weight-from-colli

♿️ Populate the master shipment with weight from the colli
  • Loading branch information
M4tini authored Dec 9, 2024
2 parents 0f12215 + 548b866 commit a1e130b
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/MyParcelComApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use MyParcelCom\ApiSdk\Resources\Interfaces\ShipmentInterface;
use MyParcelCom\ApiSdk\Resources\Interfaces\ShipmentSurchargeInterface;
use MyParcelCom\ApiSdk\Resources\Interfaces\ShopInterface;
use MyParcelCom\ApiSdk\Resources\PhysicalProperties;
use MyParcelCom\ApiSdk\Resources\ResourceFactory;
use MyParcelCom\ApiSdk\Resources\Service;
use MyParcelCom\ApiSdk\Resources\ServiceRate;
Expand Down Expand Up @@ -504,6 +505,20 @@ protected function populateShipmentWithDefaultsFromShop(ShipmentInterface $shipm
}
}

protected function populateShipmentWithWeightFromColli(ShipmentInterface $shipment): void
{
// If no weight is set, use the sum of the weight of all colli.
if ($shipment->getPhysicalProperties()?->getWeight() === null) {
$physicalProperties = $shipment->getPhysicalProperties() ?? new PhysicalProperties();
$physicalProperties->setWeight(
array_reduce($shipment->getColli(), function (mixed $carry, Shipment $item) {
return $item->getWeight() ? $carry + $item->getWeight() : $carry;
}, 0),
);
$shipment->setPhysicalProperties($physicalProperties);
}
}

public function validateShipment(ShipmentInterface $shipment): void
{
$validator = new ShipmentValidator($shipment);
Expand Down Expand Up @@ -609,6 +624,7 @@ public function createAndRegisterMultiColliShipment(
?string $idempotencyKey = null,
): ShipmentInterface {
$this->populateShipmentWithDefaultsFromShop($shipment);
$this->populateShipmentWithWeightFromColli($shipment);
$this->validateShipment($shipment);

if (count($shipment->getColli()) < 1) {
Expand Down

0 comments on commit a1e130b

Please sign in to comment.