From 6afa8d3ba9b04bbe79be47c4df1a53350d675900 Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Sun, 30 Jun 2024 21:22:44 +0200 Subject: [PATCH] Using label type from database instead of config value --- ColissimoLabel.php | 18 +++++++++++++++--- Config/module.xml | 2 +- Service/LabelService.php | 38 ++++++++++++++++++++------------------ 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/ColissimoLabel.php b/ColissimoLabel.php index 8253894..c033cfa 100644 --- a/ColissimoLabel.php +++ b/ColissimoLabel.php @@ -353,10 +353,22 @@ public static function getBordereauPath($date): string return self::BORDEREAU_FOLDER.DS.$date.'.pdf'; } - /** Get the label files extension according to the file type indicated in the module config */ - public static function getFileExtension(): string + /** + * Get the label files extension according to the file type indicated in the module config, or the requested file type + * + * @param string|null $labelType + * @return string + */ + public static function getFileExtension(?string $labelType = null): string { - return strtolower(substr(OutputFormat::OUTPUT_PRINTING_TYPE[self::getConfigValue(self::CONFIG_KEY_DEFAULT_LABEL_FORMAT)], 0, 3)); + return + strtolower( + substr( + OutputFormat::OUTPUT_PRINTING_TYPE[$labelType ?? self::getConfigValue(self::CONFIG_KEY_DEFAULT_LABEL_FORMAT)], + 0, + 3 + ) + ); } /** diff --git a/Config/module.xml b/Config/module.xml index dc147cc..2e6de93 100644 --- a/Config/module.xml +++ b/Config/module.xml @@ -13,7 +13,7 @@ en_US fr_FR - 2.1.4 + 2.1.5 Gilles Bourgeat diff --git a/Service/LabelService.php b/Service/LabelService.php index bb33352..b5df88a 100644 --- a/Service/LabelService.php +++ b/Service/LabelService.php @@ -20,6 +20,7 @@ use ColissimoLabel\Request\Helper\LabelRequestAPIConfiguration; use ColissimoLabel\Request\LabelRequest; use ColissimoPickupPoint\Model\OrderAddressColissimoPickupPointQuery; +use Propel\Runtime\ActiveQuery\Criteria; use Propel\Runtime\Exception\PropelException; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Filesystem\Filesystem; @@ -223,15 +224,24 @@ public function generateLabel($data, EventDispatcherInterface $dispatcher): arra return $results; } + /** + * @param int $orderId + * @return string|null + */ public function getCustomsInvoicePath(int $orderId): ?string { - if (null === $label = ColissimoLabelQuery::create()->findOneByOrderId($orderId)) { + if (null === $label = ColissimoLabelQuery::create()->orderByCreatedAt(Criteria::DESC)->findOneByOrderId($orderId)) { return null; } return ColissimoLabel::getLabelCN23Path($label->getTrackingNumber().'-CN23', 'pdf'); } + /** + * @param int $orderId + * @param $prettyFileName + * @return string|null + */ public function getLabelPathByOrderId(int $orderId, &$prettyFileName = ''): ?string { if (null === $label = ColissimoLabelQuery::create()->findOneByOrderId($orderId)) { @@ -241,28 +251,20 @@ public function getLabelPathByOrderId(int $orderId, &$prettyFileName = ''): ?str return $this->getLabelPathByTrackingNumber($label->getTrackingNumber(), $prettyFileName); } + /** + * @param string $trackingNumber + * @param $prettyFileName + * @return string|null + */ public function getLabelPathByTrackingNumber(string $trackingNumber, &$prettyFileName = ''): ?string { - $label = ColissimoLabelQuery::create()->findOneByTrackingNumber($trackingNumber); - - $orderRef = null; - $file = null; - - /* Compatibility for ColissimoLabel < 1.0.0 */ - if ($label) { - $file = ColissimoLabel::getLabelPath($trackingNumber, ColissimoLabel::getFileExtension()); - $prettyFileName = $trackingNumber; - - $orderRef = $label->getOrderRef(); + if (null === $label = ColissimoLabelQuery::create()->findOneByTrackingNumber($trackingNumber)) { + throw new TheliaProcessException("No label information for tracking number $trackingNumber"); } - /* The correct way to find the file for ColissimoLabel >= 1.0.0 */ - if ($orderRef && '' !== $orderRef) { - $file = ColissimoLabel::getLabelPath($label->getTrackingNumber(), ColissimoLabel::getFileExtension()); - $prettyFileName = $label->getOrderRef().'-'.$label->getTrackingNumber(); - } + $prettyFileName = $label->getOrderRef().'-'.$label->getTrackingNumber(); - return $file; + return ColissimoLabel::getLabelPath($label->getTrackingNumber(), $label->getLabelType() ?? ColissimoLabel::getFileExtension()); } /**