Skip to content

Commit

Permalink
Using label type from database instead of config value
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck Allimant committed Jun 30, 2024
1 parent 09ccdaa commit 6afa8d3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
18 changes: 15 additions & 3 deletions ColissimoLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>2.1.4</version>
<version>2.1.5</version>
<authors>
<author>
<name>Gilles Bourgeat</name>
Expand Down
38 changes: 20 additions & 18 deletions Service/LabelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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());
}

/**
Expand Down

0 comments on commit 6afa8d3

Please sign in to comment.