Skip to content

Commit

Permalink
Merge pull request #4 from logeecom/dev
Browse files Browse the repository at this point in the history
Added "Send with Packlink" button on order overview page
  • Loading branch information
afidalgomoran82 authored Jun 15, 2020
2 parents 36cd69e + 67d6608 commit 2f096ef
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 108 deletions.
21 changes: 19 additions & 2 deletions Packlink/Controllers/Backend/PacklinkDraftTaskStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use Logeecom\Infrastructure\ServiceRegister;
use Logeecom\Infrastructure\TaskExecution\QueueItem;
use Packlink\BusinessLogic\OrderShipmentDetails\Exceptions\OrderShipmentDetailsNotFound;
use Packlink\BusinessLogic\OrderShipmentDetails\OrderShipmentDetailsService;
use Packlink\BusinessLogic\ShipmentDraft\ShipmentDraftService;
use Packlink\Controllers\Backend\PacklinkOrderDetailsController;
use Packlink\Utilities\Response;
Expand All @@ -12,6 +14,8 @@ class Shopware_Controllers_Backend_PacklinkDraftTaskStatusController extends Pac

/**
* Retrieves send draft task status for particular order.
*
* @throws \Packlink\BusinessLogic\OrderShipmentDetails\Exceptions\OrderShipmentDetailsNotFound
*/
public function indexAction()
{
Expand All @@ -27,12 +31,25 @@ public function indexAction()
/** @var ShipmentDraftService $shipmentDraftService */
$shipmentDraftService = ServiceRegister::getService(ShipmentDraftService::CLASS_NAME);
$draftStatus = $shipmentDraftService->getDraftStatus($orderId);

if ($draftStatus->status === QueueItem::QUEUED) {
Response::json(['status' => QueueItem::IN_PROGRESS]);
}

Response::json($draftStatus->toArray());
$response = $draftStatus->toArray();

if ($draftStatus->status === QueueItem::COMPLETED) {
/** @var OrderShipmentDetailsService $orderShipmentDetailsService */
$orderShipmentDetailsService = ServiceRegister::getService(OrderShipmentDetailsService::CLASS_NAME);
$shipmentDetails = $orderShipmentDetailsService->getDetailsByOrderId($orderId);

if ($shipmentDetails === null) {
throw new OrderShipmentDetailsNotFound('Order details not found.');
}

$response['shipmentUrl'] = $shipmentDetails->getShipmentUrl();
}

Response::json($response);
}

/**
Expand Down
31 changes: 21 additions & 10 deletions Packlink/Resources/snippets/backend/packlink/configuration.ini
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,14 @@ shipping/cost = "Shipping Cost"
shipment/details/tab/title = "Shipment details"
shipment/create/draft/label = "Create shipment draft on Packlink PRO"
shipment/details/title = "Packlink Shipping"
shipment/inprogress/label = "Draft is currently being created in Packlink PRO"
shipment/inprogress/label = "Draft is currently being created."
shipment/failed_no_error/label = "Previous attempt to create a draft failed."
shipment/failed/label = "Previous attempt to create a draft failed. Error: "
shipment/total/charges = "Total shipping charges(EUR):"
shipment/reference/number = "Packlink reference number:"
shipment/packlink/price = "Packlink shipping price:"
shipment/view = "View on Packlink PRO"
shipment/view = "View on Packlink"
shipment/send = "Send with Packlink"
shipment/print = "Print shipment labels"
shipment/printed = "Printed"
shipment/trackit = "Track it"
Expand Down Expand Up @@ -311,12 +313,14 @@ shipping/cost = "Shipping Cost"
shipment/details/tab/title = "Versanddetails"
shipment/create/draft/label = "Entwurf der Bestellung in Packlink PRO erstellen"
shipment/details/title = "Packlink Shipping"
shipment/inprogress/label = "Der Entwurf wird gerade in Packlink PRO erstellt"
shipment/inprogress/label = "Der Entwurf wird gerade erstellt"
shipment/failed_no_error/label = "Der Versuch der Entwurfserstellung ist fehlgeschlagen."
shipment/failed/label = "Der Versuch der Entwurfserstellung ist fehlgeschlagen. Fehler: "
shipment/total/charges = "Gesamtversandkosten (EUR):"
shipment/reference/number = "Packlink Referenznummer:"
shipment/packlink/price = "Packlink Versandpreis:"
shipment/view = "Auf Packlink PRO ansehen"
shipment/view = "Auf Packlink ansehen"
shipment/send = "Mit Packlink versenden"
shipment/print = "Versandetiketten drucken"
shipment/printed = "Ausgedruckt"
shipment/trackit = "Versand verfolgen"
Expand Down Expand Up @@ -490,12 +494,14 @@ shipping/cost = "Shipping Cost"
shipment/details/tab/title = "Détails de l\'envoi"
shipment/create/draft/label = "Créer un brouillon de commande sur Packlink PRO"
shipment/details/title = "Packlink Shipping"
shipment/inprogress/label = "Le brouillon est en cours de création sur Packlink PRO"
shipment/inprogress/label = "Le brouillon est en cours de création."
shipment/failed_no_error/label = "La tentative précédente de création d'un brouillon a échoué."
shipment/failed/label = "La tentative précédente de création d'un brouillon a échoué. Erreur: "
shipment/total/charges = "Total des frais d’envoi (EUR) :"
shipment/reference/number = "Numéro de référence Packlink:"
shipment/packlink/price = "Prix de livraison Packlink:"
shipment/view = "Voir sur Packlink PRO"
shipment/view = "Voir sur Packlink"
shipment/send = "Envoyer avec Packlink"
shipment/print = "Imprimer les étiquettes d’envoi"
shipment/printed = "Imprimé"
shipment/trackit = "Suivez le!"
Expand Down Expand Up @@ -669,12 +675,14 @@ shipping/cost = "Shipping Cost"
shipment/details/tab/title = "Dettagli della spedizione"
shipment/create/draft/label = "Crea una bozza di ordine su Packlink PRO"
shipment/details/title = "Packlink Shipping"
shipment/inprogress/label = "La bozza è attualmente in fase di creazione su Packlink PRO"
shipment/inprogress/label = "La bozza è attualmente in fase di creazione."
shipment/failed_no_error/label = "Il precedente tentativo di creare una bozza è fallito."
shipment/failed/label = "Il precedente tentativo di creare una bozza è fallito. Errore: "
shipment/total/charges = "Spese di spedizione totali (EUR):"
shipment/reference/number = "Packlink reference number:"
shipment/packlink/price = "Prezzo di spedizione Packlink:"
shipment/view = "Vedi su Packlink PRO"
shipment/view = "Vedi su Packlink"
shipment/send = "Spedisci con Packlink"
shipment/print = "Stampa le etichette di spedizione"
shipment/printed = "Stampato"
shipment/trackit = "Traccialo!"
Expand Down Expand Up @@ -848,11 +856,14 @@ shipping/cost = "Shipping Cost"
shipment/details/tab/title = "Detalles del envío"
shipment/create/draft/label = "Crear borrador en Packlink PRO"
shipment/details/title = "Packlink Shipping"
shipment/inprogress/label = "El intento anterior de crear un borrador ha fallado. Error: "
shipment/inprogress/label = "El borrador se está creando actualmente."
shipment/failed_no_error/label = "El intento anterior de crear un borrador ha fallado."
shipment/failed/label = "El intento anterior de crear un borrador ha fallado. Error: "
shipment/total/charges = "Gastos de envío totales (EUR):"
shipment/reference/number = "Número de referencia Packlink:"
shipment/packlink/price = "Precio de envío de Packlink:"
shipment/view = "Ver en Packlink PRO"
shipment/view = "Ver en Packlink"
shipment/send = "Enviar con Packlink"
shipment/print = "Imprimir etiquetas de envío"
shipment/printed = "Impreso"
shipment/trackit = "Seguimiento"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ Ext.define('Shopware.apps.Packlink.controller.OrderDetailsController', {
return [
{
xtype: 'displayfield',
value: '{s name="shipment/inprogress/label"}Draft is currently being created in Packlink PRO{/s}',
value: '{s name="shipment/inprogress/label"}Draft is currently being created.{/s}',
style: {
margin: '10px'
},
Expand Down Expand Up @@ -389,7 +389,7 @@ Ext.define('Shopware.apps.Packlink.controller.OrderDetailsController', {
*/
function getViewOnPacklinkButton() {
return Ext.create('Ext.Button', {
text: '{s name="shipment/view"}View on Packlink PRO{/s}',
text: '{s name="shipment/view"}View on Packlink{/s}',
cls: 'large primary',
disabled: !data.referenceUrl || data.isDeleted,
border: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,47 +27,185 @@ Ext.define('Shopware.apps.Packlink.controller.OrderListController', {
event.target.classList.remove('sprite-tag');
event.target.classList.add('sprite-tag-label-black');
}

if (event.target && event.target.hasAttribute('data-pl-order-id')) {
if (event.target.classList.contains('pl-create-draft-button')) {
me.createDraft(event.target);
} else {
me.createDraft(event.target.parentElement);
}
}
},
afterrender: function (grid) {
let store = grid.getStore();
store.on('load', function () {
let draftsInProgress = document.getElementsByClassName('pl-draft-in-progress');
for (let draftInProgress of draftsInProgress) {
if (draftInProgress.hasAttribute('data-pl-order-id')) {
let orderId = draftInProgress.getAttribute('data-pl-order-id'),
parent = draftInProgress.parentElement;

me.checkDraftStatus(parent, orderId);
}
}
});
},
plPrintLabels: function (grid) {
if (grid.plOrderCheckboxes) {
let selected = grid.plOrderCheckboxes.getSelection();

selected = selected.filter(function (order) {
return order.get('plHasLabel');
});

let ids = selected.map(function (order) {
return order.get('id');
});

if (ids.length !== 0) {
printLabels(ids);
setPrinted(ids);
}
}

function printLabels(ids) {
let url = '{url controller=PacklinkPrintLabelsController action="print"}';
url += '/__csrf_token/' + Ext.CSRFService.getToken();
url += '?orderIds=' + ids.join(',');
let newWindow = window.open(url, '_blank');
newWindow.focus();
}

function setPrinted(ids) {
let element = grid.getEl();
let icons = element.query('[data-pl-label]');

for (let icon of icons) {
if (ids.indexOf(parseInt(icon.getAttribute('data-pl-label'))) !== -1) {
icon.classList.remove('sprite-tag');
icon.classList.add('sprite-tag-label-black');
}
}
}
}
if (grid.plOrderCheckboxes) {
let selected = grid.plOrderCheckboxes.getSelection();

selected = selected.filter(function (order) {
return order.get('plHasLabel');
});

let ids = selected.map(function (order) {
return order.get('id');
});

if (ids.length !== 0) {
printLabels(ids);
setPrinted(ids);
}
}

function printLabels(ids) {
let url = '{url controller=PacklinkPrintLabelsController action="print"}';
url += '/__csrf_token/' + Ext.CSRFService.getToken();
url += '?orderIds=' + ids.join(',');
let newWindow = window.open(url, '_blank');
newWindow.focus();
}

function setPrinted(ids) {
let element = grid.getEl();
let icons = element.query('[data-pl-label]');

for (let icon of icons) {
if (ids.indexOf(parseInt(icon.getAttribute('data-pl-label'))) !== -1) {
icon.classList.remove('sprite-tag');
icon.classList.add('sprite-tag-label-black');
}
}
}
},
}
}
)
},

createDraft: function (createDraftButton) {
let me = this,
parent = createDraftButton.parentElement,
orderId = createDraftButton.getAttribute('data-pl-order-id');

parent.removeChild(createDraftButton);
parent.innerHTML = '<span class="pl-draft-in-progress">'
+ '{s name="shipment/inprogress/label"}Draft is currently being created.{/s}'
+ '</span>';

Packlink.ajaxService.post(
me.getCreateDraftUrl(),
{
orderId: orderId
},
function () {
me.checkDraftStatus(parent, orderId);
}
);
},

checkDraftStatus: function (parent, orderId) {
let me = this;

clearTimeout(function () {
me.checkDraftStatus(parent, orderId);
});

Packlink.ajaxService.get(me.getDraftTaskStatusUrl(orderId), function (response) {
if (response.status === 'completed') {
me.displayViewButton(parent, response.shipmentUrl);
} else if (['failed', 'aborted'].includes(response.status)) {
parent.innerText = '{s name="shipment/failed_no_error/label"}Previous attempt to create a draft failed.{/s}';
setTimeout(function () {
me.displayCreateDraftButton(parent, orderId)
}, 5000);
} else {
setTimeout(function () {
me.checkDraftStatus(parent, orderId)
}, 1000);
}
});
},

displayViewButton: function (parent, shipmentUrl) {
let me = this;
viewDraftButton = document.createElement('a');

viewDraftButton.href = shipmentUrl;
viewDraftButton.target = '_blank';
viewDraftButton.classList.add('pl-draft-button');
viewDraftButton.style.display = 'flex';
viewDraftButton.style.lineHeight = '16px';

let viewOnPacklink = document.createElement('span');
viewOnPacklink.innerText = '{s name="shipment/view"}View on Packlink{/s}';

viewDraftButton.appendChild(me.getImageElement());
viewDraftButton.appendChild(viewOnPacklink);

parent.innerHTML = '';
parent.appendChild(viewDraftButton);
},

displayCreateDraftButton: function (parent, orderId) {
let me = this;

clearTimeout(function () {
displayCreateDraftButton(parent, orderId)
});

let createDraftButton = document.createElement('a');

createDraftButton.classList.add('pl-create-draft-button');
createDraftButton.setAttribute('data-order-id', orderId);
createDraftButton.style.display = 'flex';
createDraftButton.style.lineHeight = '16px';
createDraftButton.style.cursor = 'pointer';

let sendWithPacklink = document.createElement('span');
sendWithPacklink.innerText = '{s name="shipment/send"}Send with Packlink{/s}';

createDraftButton.appendChild(me.getImageElement());
createDraftButton.appendChild(sendWithPacklink);

createDraftButton.addEventListener('click', function () {
me.createDraft(createDraftButton);
});

parent.innerHTML = '';
parent.appendChild(createDraftButton);
},

getImageElement: function () {
let img = document.createElement('img');

img.src = '{link file="backend/_resources/images/logo.png"}';
img.classList.add('pl-image');
img.style.width = '16px';
img.style.height = '16px';

return img;
},

getDraftTaskStatusUrl: function (orderId) {
let url = '{url controller=PacklinkDraftTaskStatusController action="index"}';
url += '/index/__csrf_token/' + Ext.CSRFService.getToken();

return url + '?orderId=' + orderId;
},

getCreateDraftUrl: function () {
return '{url controller=PacklinkDraftTaskCreateController action="create"}' +
'/__csrf_token/' + Ext.CSRFService.getToken();
}
});
Loading

0 comments on commit 2f096ef

Please sign in to comment.