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

Implement properly parcels & labels in Woopit delivery request. #4874

Merged
merged 6 commits into from
Jan 30, 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
53 changes: 23 additions & 30 deletions features/woopit.feature
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,35 @@ Feature: Woopit
"""
{
"deliveryId":"yXJPEP30xgja",
"parcels": [
{
"id": @string@
},
{
"id": @string@
}
],
"labels": [
{
"id": "@string@",
"type": "url",
"mode": "pdf",
"value": "@string@"
"value": "@string@",
"parcelId": "@string@"
},
{
"id": "@string@",
"type": "url",
"mode": "pdf",
"value": "@string@"
"value": "@string@",
"parcelId": "@string@"
},
{
"id": "@string@",
"type": "url",
"mode": "pdf",
"value": "@string@",
"parcelId": "@string@"
}
]
}
Expand Down Expand Up @@ -625,20 +642,8 @@ Feature: Woopit
"""
{
"deliveryId":"yXJPEP30xgja",
"labels": [
{
"id": "@string@",
"type": "url",
"mode": "pdf",
"value": "@string@"
},
{
"id": "@string@",
"type": "url",
"mode": "pdf",
"value": "@string@"
}
]
"parcels": @array@,
"labels": @array@
}
"""
When I add "Content-Type" header equal to "application/json"
Expand Down Expand Up @@ -869,20 +874,8 @@ Feature: Woopit
"""
{
"deliveryId":"yXJPEP30xgja",
"labels": [
{
"id": "@string@",
"type": "url",
"mode": "pdf",
"value": "@string@"
},
{
"id": "@string@",
"type": "url",
"mode": "pdf",
"value": "@string@"
}
]
"parcels": @array@,
"labels": @array@
}
"""
When I add "Content-Type" header equal to "application/json"
Expand Down
2 changes: 1 addition & 1 deletion src/Action/Woopit/CreateDeliveryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected function createDelivery(WoopitQuoteRequest $data, WoopitIntegration $i
$pickup = $this->createTask($data->picking, Task::TYPE_PICKUP);
$dropoff = $this->createTask($data->delivery, Task::TYPE_DROPOFF);

$this->parseAndApplyPackages($data, $pickup);
$this->parseAndApplyPackages($data, $dropoff);

$delivery = Delivery::createWithTasks($pickup, $dropoff);

Expand Down
44 changes: 25 additions & 19 deletions src/Action/Woopit/DeliveryRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ class DeliveryRequest
{
use CreateDeliveryTrait;

private $deliveryManager;
private $geocoder;

public function __construct(
DeliveryManager $deliveryManager,
Geocoder $geocoder,
Hashids $hashids12,
EntityManagerInterface $entityManager,
PhoneNumberUtil $phoneNumberUtil,
ValidatorInterface $checkDeliveryValidator,
private DeliveryManager $deliveryManager,
private Geocoder $geocoder,
private Hashids $hashids12,
private EntityManagerInterface $entityManager,
private PhoneNumberUtil $phoneNumberUtil,
private ValidatorInterface $checkDeliveryValidator,
private UrlGeneratorInterface $urlGenerator)
{
$this->deliveryManager = $deliveryManager;
Expand Down Expand Up @@ -97,20 +94,29 @@ public function __invoke(WoopitQuoteRequest $data)
$data->deliveryObject = $delivery;
$data->state = WoopitQuoteRequest::STATE_CONFIRMED;

$pickup = $delivery->getPickup();
$dropoff = $delivery->getDropoff();

// TODO Add label only for pickup
foreach ($delivery->getTasks() as $task) {
foreach ($dropoff->getPackages() as $package) {

$barcode = BarcodeUtils::getRawBarcodeFromTask($task);
$barcodeToken = BarcodeUtils::getToken($barcode);
$parcelId = sprintf('pkg_%s', $this->hashids12->encode($package->getId()));

$data->labels[] = [
'id' => sprintf('lbl_%s', $this->hashids12->encode($task->getId())),
'type' => 'url',
'mode' => 'pdf',
'value' => $this->urlGenerator->generate('task_label_pdf', ['code' => $barcode, 'token' => $barcodeToken], UrlGeneratorInterface::ABSOLUTE_URL)
$data->parcels[] = [
'id' => $parcelId,
];

$barcodes = BarcodeUtils::getBarcodesFromPackage($package);
foreach ($barcodes as $i => $barcode) {

$barcodeToken = BarcodeUtils::getToken($barcode);

$data->labels[] = [
'id' => sprintf('lbl_%s', $this->hashids12->encode($dropoff->getId(), $package->getId(), $i)),
'type' => 'url',
'mode' => 'pdf',
'value' => $this->urlGenerator->generate('task_label_pdf', ['code' => $barcode, 'token' => $barcodeToken], UrlGeneratorInterface::ABSOLUTE_URL),
'parcelId' => $parcelId,
];
}
}

return $data;
Expand Down
68 changes: 60 additions & 8 deletions src/Action/Woopit/PackagesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,79 @@

namespace AppBundle\Action\Woopit;

use AppBundle\Entity\Package;
use AppBundle\Entity\PackageSet;
use AppBundle\Entity\Task;
use AppBundle\Entity\Woopit\QuoteRequest as WoopitQuoteRequest;
use PhpUnitsOfMeasure\PhysicalQuantity\Mass;

trait PackagesTrait
{
protected function parseAndApplyPackages(WoopitQuoteRequest $data, Task $task)
{
$packageSet = $this->entityManager->getRepository(PackageSet::class)->findOneBy(['name' => 'Woopit']);
if (null === $packageSet) {
$packageSet = new PackageSet();
$packageSet->setName('Woopit');

$this->entityManager->persist($packageSet);
$this->entityManager->flush();
}

if ($data->packages) {

$packagesString = '';

foreach($data->packages as $package) {
if (!empty($packagesString)) {
$packagesString .= ', ';
}
$packagesString .= $package['quantity'];
if (isset($package['weight'])) {
$packagesString .= ' x ' . $package['weight']['value'] . ' ' . $package['weight']['unit'];
$qb = $this->entityManager->getRepository(Package::class)
->createQueryBuilder('p')
->andWhere('p.packageSet = :package_set')
->andWhere('p.name = :name')
->setParameter('package_set', $packageSet);

foreach ($data->packages as $package) {

$packageName = $this->generatePackageName($package);

$qb->setParameter('name', $packageName);
$p = $qb->getQuery()->getOneOrNullResult();

if (null === $p) {

$p = new Package();
$p->setPackageSet($packageSet);

$p->setName($packageName);
$p->setDescription($packageName);
$p->setAverageVolumeUnits(1);
$p->setMaxVolumeUnits(1);
$p->setColor('#FFFFFF');
$p->setAverageWeight($this->convertWeight($package['weight']));
$p->setMaxWeight($this->convertWeight($package['weight']));
$p->setShortCode(strtoupper(substr($packageName, 0 ,2)));

$this->entityManager->persist($p);
$this->entityManager->flush();
}

$task->addPackageWithQuantity($p, $package['quantity']);
}
}
}

$task->setComments(sprintf('Packages: %s', $packagesString));
private function generatePackageName(array $data): string
{
$params = [];
foreach (['width', 'height', 'length', 'weight'] as $key) {
$params[] = $data[$key]['value'] . $data[$key]['unit'];
}

return sprintf('%s x %s x %s, %s', ...$params);
}

private function convertWeight(array $data): int|float
{
$mass = new Mass($data['value'], $data['unit']);

return $mass->toUnit('g');
}
}
13 changes: 5 additions & 8 deletions src/Action/Woopit/QuoteRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ class QuoteRequest
{
use CreateDeliveryTrait;

private $deliveryManager;
private $geocoder;

public function __construct(
DeliveryManager $deliveryManager,
Geocoder $geocoder,
PriceHelper $priceHelper,
EntityManagerInterface $entityManager,
ValidatorInterface $checkDeliveryValidator)
private DeliveryManager $deliveryManager,
private Geocoder $geocoder,
private PriceHelper $priceHelper,
private EntityManagerInterface $entityManager,
private ValidatorInterface $checkDeliveryValidator)
{
$this->deliveryManager = $deliveryManager;
$this->geocoder = $geocoder;
Expand Down
5 changes: 5 additions & 0 deletions src/Entity/Woopit/QuoteRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ class QuoteRequest
*/
public $deliveryId;

/**
* @Groups({"woopit_delivery_output"})
*/
public $parcels = [];

/**
* @Groups({"woopit_delivery_output"})
*/
Expand Down
Loading