Skip to content

Commit

Permalink
Merge pull request #29 from thelia-modules/fix/label-file-name
Browse files Browse the repository at this point in the history
PDF label names are now the tracking number instead of the order ref.
  • Loading branch information
roadster31 authored Mar 8, 2024
2 parents 6020345 + b157ca6 commit b98f705
Show file tree
Hide file tree
Showing 10 changed files with 406 additions and 406 deletions.
6 changes: 5 additions & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>2.0.6</version>
<version>2.1.1</version>
<authors>
<author>
<name>Gilles Bourgeat</name>
<email>[email protected]</email>
</author>
<author>
<name>Franck Allimant</name>
<email>[email protected]</email>
</author>
</authors>
<type>classic</type>
<thelia>2.5.0</thelia>
Expand Down
64 changes: 41 additions & 23 deletions Controller/Admin/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Core\Translation\Translator;
use Thelia\Exception\TheliaProcessException;
use Thelia\Log\Tlog;
use Thelia\Model\ConfigQuery;
use Thelia\Model\OrderQuery;
use Thelia\Tools\URL;
Expand Down Expand Up @@ -92,8 +93,9 @@ public function generateLabelAction(Request $request, LabelService $labelService
if (count($files) > 0) {
$bordereau = null;
if (ColissimoLabel::getConfigValue(ColissimoLabel::CONFIG_KEY_GENERATE_BORDEREAU)) {
$bordereau = $this->addBordereau($parcelNumbers);
$files[] = $bordereau;
if (null !== $bordereau = $this->addBordereau($parcelNumbers)) {
$files[] = $bordereau;
}
}

$zip = new ZipArchive();
Expand Down Expand Up @@ -126,35 +128,51 @@ public function generateLabelAction(Request $request, LabelService $labelService
*
* @param $parcelNumbers
*
* @return string
* @return string|null
*
* @throws Exception
*/
protected function addBordereau($parcelNumbers): string
protected function addBordereau($parcelNumbers): ?string
{
$service = new SOAPService();
$APIConfiguration = new BordereauRequestAPIConfiguration();
$APIConfiguration->setContractNumber(ColissimoLabel::getConfigValue(ColissimoLabel::CONFIG_KEY_CONTRACT_NUMBER));
$APIConfiguration->setPassword(ColissimoLabel::getConfigValue(ColissimoLabel::CONFIG_KEY_PASSWORD));

$parseResponse = $service->callGenerateBordereauByParcelsNumbersAPI($APIConfiguration, $parcelNumbers);
$resultAttachment = $parseResponse->attachments;
$bordereauContent = $resultAttachment[0];
$fileContent = $bordereauContent['data'];
try {
$parseResponse = $service->callGenerateBordereauByParcelsNumbersAPI($APIConfiguration, $parcelNumbers);
$resultAttachment = $parseResponse->attachments;

if ('' == $fileContent) {
throw new Exception('File is empty');
}
if (! isset($resultAttachment[0])) {
throw new TheliaProcessException('Bordereau request : no attached data.');
}

$filePath = ColissimoLabel::getLabelPath('bordereau', 'pdf');
$bordereauContent = $resultAttachment[0];

$fileSystem = new Filesystem();
$fileSystem->dumpFile(
$filePath,
$fileContent
);
if (! isset($bordereauContent['data'])) {
throw new TheliaProcessException('Bordereau request : attachment is empty.');
}

$fileContent = $bordereauContent['data'];

if (empty($fileContent)) {
throw new TheliaProcessException('Bordereau request : Bordereau file is empty');
}

$filePath = ColissimoLabel::getLabelPath('bordereau', 'pdf');

$fileSystem = new Filesystem();
$fileSystem->dumpFile(
$filePath,
$fileContent
);

return $filePath;
} catch (Exception $ex) {
Tlog::getInstance()->error("Failed to get Bordereau : " . $ex->getMessage());
}

return $filePath;
return null;
}

/**
Expand Down Expand Up @@ -209,8 +227,8 @@ public function deleteLabelAction(Request $request, string $number): Response
/* We check if the label is from this module */
if ($label) {
/* We check if the label is from this version of the module -- Compatibility with ColissimoLabel < 1.0.0 */
if ('' !== $orderRef = $label->getOrderRef()) {
$this->deleteLabelFile($orderRef);
if ('' !== $trackNo = $label->getTrackingNumber()) {
$this->deleteLabelFile($trackNo);
$label->delete();

/* Handle the return when called from order edit */
Expand Down Expand Up @@ -252,7 +270,7 @@ public function getCustomsInvoiceAction($orderId): Response
if (null !== OrderQuery::create()->findOneById($orderId))
{
if ($label = ColissimoLabelQuery::create()->findOneByOrderId($orderId)) {
$fileName = ColissimoLabel::getLabelCN23Path($label->getOrderRef().'CN23', 'pdf');
$fileName = ColissimoLabel::getLabelCN23Path($label->getTrackingNumber().'-CN23', 'pdf');

return new Response(
file_get_contents($fileName),
Expand Down Expand Up @@ -299,8 +317,8 @@ public function getLabelAction(Request $request, string $number): mixed

/* The correct way to find the file for ColissimoLabel >= 1.0.0 */
if ($orderRef && '' !== $orderRef) {
$file = ColissimoLabel::getLabelPath($label->getOrderRef(), ColissimoLabel::getFileExtension());
$fileName = $label->getOrderRef();
$file = ColissimoLabel::getLabelPath($label->getTrackingNumber(), ColissimoLabel::getFileExtension());
$fileName = $label->getOrderRef() . '-' . $label->getTrackingNumber();
}

$response = new BinaryFileResponse($file);
Expand Down
2 changes: 1 addition & 1 deletion I18n/backOffice/default/en_US.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
'Label cannot be created. Error is: ' => 'Label cannot be created. Error is: ',
'Labels' => 'Labels',
'Labels Colissimo' => 'Labels Colissimo',
'Livraison avec signature :' => 'Delivery w/ signature',
'Livraison avec signature' => 'Delivery with signature',
'Module' => 'Module',
'No existing label for this order' => 'No existing label for this order',
'Non disponible' => 'Non disponible',
Expand Down
2 changes: 1 addition & 1 deletion I18n/backOffice/default/fr_FR.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'Label cannot be created. Error is: ' => 'L\'étiquette n\'a pas pu être créée. Erreur: ',
'Labels' => 'Etiquettes',
'Labels Colissimo' => 'Étiquette Colissimo',
'Livraison avec signature :' => 'Livraison avec signature :',
'Livraison avec signature' => 'Livraison avec signature',
'Module' => 'Module',
'No existing label for this order' => 'Aucune étiquette pour cette commande',
'Non disponible' => 'Non disponible',
Expand Down
13 changes: 8 additions & 5 deletions Service/LabelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function generateLabel($data, $isEditPage, EventDispatcherInterface $disp
foreach ($data['order_id'] as $orderId) {
if (null !== $order = OrderQuery::create()->findOneById($orderId)) {
/* DO NOT use strict comparison here */
if (!isset($weightArray[$orderId]) || 0 == (float) $weightArray[$orderId]) {
if (!isset($weightArray[$orderId]) || (float) $weightArray[$orderId] === 0.0) {
$weight = $order->getWeight();
} else {
$weight = (float) $weightArray[$orderId];
Expand Down Expand Up @@ -89,7 +89,10 @@ public function generateLabel($data, $isEditPage, EventDispatcherInterface $disp
$colissimoRequest = new LabelRequest($order, null, null, $signedDelivery);
}

$colissimoRequest->getLetter()->getParcel()->setWeight($weight);
/* We set the weight as the one indicated from the form */
if (null !== $weight) {
$colissimoRequest->getLetter()->getParcel()->setWeight($weight);
}

/* We set whether the delivery is a signed one or not thanks to the 'signed' checkbox in the form */
if (null !== $signedDelivery) {
Expand All @@ -111,7 +114,7 @@ public function generateLabel($data, $isEditPage, EventDispatcherInterface $disp

/* We dump / save the label on the server */
$fileSystem->dumpFile(
$labelName = ColissimoLabel::getLabelPath($order->getRef(), ColissimoLabel::getFileExtension()),
$labelName = ColissimoLabel::getLabelPath($response->getParcelNumber(), ColissimoLabel::getFileExtension()),
$response->getFile()
);

Expand All @@ -121,7 +124,7 @@ public function generateLabel($data, $isEditPage, EventDispatcherInterface $disp
/* Dump the CN23 customs file if there is one */
if ($response->hasFileCN23()) {
$fileSystem->dumpFile(
$customsFileName = ColissimoLabel::getLabelCN23Path($order->getRef().'CN23', 'pdf'),
$customsFileName = ColissimoLabel::getLabelCN23Path($response->getParcelNumber().'-CN23', 'pdf'),
$response->getFileCN23()
);
$files[] = $customsFileName;
Expand Down Expand Up @@ -172,7 +175,7 @@ public function generateLabel($data, $isEditPage, EventDispatcherInterface $disp
if ($isEditPage) {
return new JsonResponse([
'id' => $colissimoLabelModel->getId(),
'url' => URL::getInstance()->absoluteUrl('/admin/module/ColissimoLabel/label/'.$response->getParcelNumber()),
'url' => URL::getInstance()?->absoluteUrl('/admin/module/ColissimoLabel/label/'.$response->getParcelNumber()),
'number' => $response->getParcelNumber(),
'order' => [
'id' => $order->getId(),
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"license": "LGPL-3.0+",
"type": "thelia-module",
"require": {
"thelia/installer": "~1.1"
"thelia/installer": "~1.1",
"ext-zip": "*"
},
"extra": {
"installer-name": "ColissimoLabel"
}
}
}
110 changes: 51 additions & 59 deletions templates/backOffice/default/colissimo-label/bordereau-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,73 +17,65 @@
<div class="row">
<div class="col-md-12">
<div class="general-block-decorator">
<div class="row">
<div class="col-md-12">
<ul id="tabbed-menu" class="nav nav-tabs">
<li class="{if $tab eq "bordereaux"}active{/if}"><a href="{if $tab eq 'bordereaux'}#{else}{url path='/admin/module/ColissimoLabel/bordereaux'}{/if}">{intl l="Bordereaux" d='colissimolabel.bo.default'}</a> </li>
<li class="{if $tab eq "labels"}active{/if}"><a href="{if $tab eq 'labels'}#{else}{url path='/admin/module/ColissimoLabel/labels'}{/if}">{intl l="Labels" d='colissimolabel.bo.default'}</a> </li>
<li class="{if $tab eq "config"}active{/if}"><a href="{if $tab eq 'config'}#{else}{url path='/admin/module/ColissimoLabel/configuration'}{/if}">{intl l="Configuration" d='colissimolabel.bo.default'}</a> </li>
</ul>
<ul id="tabbed-menu" class="nav nav-tabs">
<li class="{if $tab eq "bordereaux"}active{/if}"><a href="{if $tab eq 'bordereaux'}#{else}{url path='/admin/module/ColissimoLabel/bordereaux'}{/if}">{intl l="Bordereaux" d='colissimolabel.bo.default'}</a> </li>
<li class="{if $tab eq "labels"}active{/if}"><a href="{if $tab eq 'labels'}#{else}{url path='/admin/module/ColissimoLabel/labels'}{/if}">{intl l="Labels" d='colissimolabel.bo.default'}</a> </li>
<li class="{if $tab eq "config"}active{/if}"><a href="{if $tab eq 'config'}#{else}{url path='/admin/module/ColissimoLabel/configuration'}{/if}">{intl l="Configuration" d='colissimolabel.bo.default'}</a> </li>
</ul>

<div class="row">
<div class="col-md-12 general-block-decorator">
{if $error}
<div class="row">
<div class="alert alert-danger" id="error-bordereau">
<span class="glyphicon glyphicon-warning-sign"></span>
<span id="error-bordereau-message">{$error}</span>
</div>
</div>
{/if}
<div class="tab-content">
<br>
{if $error}
<div class="alert alert-danger" id="error-bordereau">
<span class="glyphicon glyphicon-warning-sign"></span>
<span id="error-bordereau-message">{$error}</span>
</div>
{/if}

<div class="row">
<a href="{url path="/admin/module/ColissimoLabel/bordereau/generate"}" class="btn btn-block btn-info">
{intl l="Generate bordereau for label since : %date" date="{format_date date=$lastBordereauDate}" d="colissimolabel.bo.default"}
</a>
</div>
</div>
<p>
<a href="{url path="/admin/module/ColissimoLabel/bordereau/generate"}" class="btn btn-info">
{intl l="Generate bordereau for label since : %date" date="{format_date date=$lastBordereauDate}" d="colissimolabel.bo.default"}
</a>
</p>

{if $bordereaux}
<table class="table table-condensed" style="width:100%">
<colgroup>
<col style="width: 95%;">
<col style="width: 5%;">
</colgroup>
<thead>
<tr class="active">
<th class="text-left">{intl d='colissimolabel.bo.default' l="Bordereau"}</th>
<th class="text-center">{intl d='colissimolabel.bo.default' l="Action"}</th>
</tr>
</thead>
<tbody>
{foreach from=$bordereaux item=bordereau}
<tr>
<td class="text-left">
<a href="{url path="/admin/module/ColissimoLabel/bordereau/download" filePath="{$bordereau['path']}"}" target="_blank">
{$bordereau['name']}
</a>
</td>
<td class="text-center">
<a class="btn btn-default btn-xs" href="{url path="/admin/module/ColissimoLabel/bordereau/download" stay="1" filePath="{$bordereau['path']}"}" title="{intl d='colissimolabel.bo.default' l="Download bordereau"}">
<i class="glyphicon glyphicon-download"></i>
</a>
<a class="btn btn-default btn-xs" href="{url path="/admin/module/ColissimoLabel/bordereau/delete" filePath="{$bordereau['path']}"}"" title="{intl d='colissimolabel.bo.default' l="Delete bordereau"}">
<i class="glyphicon glyphicon-trash"></i>
</a>
</td>
{if $bordereaux}
<table class="table table-condensed" style="width:100%">
<colgroup>
<col style="width: 95%;">
<col style="width: 5%;">
</colgroup>
<thead>
<tr class="active">
<th class="text-left">{intl d='colissimolabel.bo.default' l="Bordereau"}</th>
<th class="text-center">{intl d='colissimolabel.bo.default' l="Action"}</th>
</tr>
{/foreach}
</tbody>
</table>
</div>
{/if}
</div>
</div>
</thead>
<tbody>
{foreach from=$bordereaux item=bordereau}
<tr>
<td class="text-left">
<a href="{url path="/admin/module/ColissimoLabel/bordereau/download" filePath="{$bordereau['path']}"}" target="_blank">
{$bordereau['name']}
</a>
</td>
<td class="text-center">
<a class="btn btn-default btn-xs" href="{url path="/admin/module/ColissimoLabel/bordereau/download" stay="1" filePath="{$bordereau['path']}"}" title="{intl d='colissimolabel.bo.default' l="Download bordereau"}">
<i class="glyphicon glyphicon-download"></i>
</a>
<a class="btn btn-default btn-xs" href="{url path="/admin/module/ColissimoLabel/bordereau/delete" filePath="{$bordereau['path']}"}"" title="{intl d='colissimolabel.bo.default' l="Delete bordereau"}">
<i class="glyphicon glyphicon-trash"></i>
</a>
</td>
</tr>
{/foreach}
</tbody>
</table>
{/if}
</div>
</div>
</div>
</div>
{/block}

{block name="javascript-last-call"}
{/block}
{/block}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ <h4 class="modal-title" id="exampleModalLabel">{intl l="Colissimo Labels" d="col

<td class="text-center">
{form_field field="signed" value_key=$ORDER_ID}
<label for="colissimolabel-input-signedDelivery">{intl l="Livraison avec signature :" d="colissimolabel.bo.default"}</label>
<input id="colissimolabel-input-signedDelivery" class="form-control order_checkbox" type="checkbox" name="{$name}" {if $SIGNED} checked {/if} {if !$CAN_BE_NOT_SIGNED} disabled {/if}>
<div class="checkbox">
<label for="colissimolabel-input-signedDelivery">
<input id="colissimolabel-input-signedDelivery" class="order_checkbox" type="checkbox" name="{$name}" {if $SIGNED} checked {/if} {if !$CAN_BE_NOT_SIGNED} disabled {/if}>
{intl l="Livraison avec signature" d="colissimolabel.bo.default"}
</label>
</div>
{/form_field}
</td>

Expand Down
Loading

0 comments on commit b98f705

Please sign in to comment.