Skip to content

Commit

Permalink
Merge pull request #741 from mollie/PIPRES-219-shipping-number-replac…
Browse files Browse the repository at this point in the history
…ement

PIPRES-219: Shipping number replacement
  • Loading branch information
mandan2 authored Jun 6, 2023
2 parents b47f931 + e98ec89 commit 91b83fb
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/Service/ShipmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,21 @@ public function __construct(
public function getShipmentInformation($orderReference)
{
$orders = Order::getByReference($orderReference);

/** @var Order $order */
$order = $orders->getFirst();

if (!Validate::isLoadedObject($order)) {
return null;
}

$invoiceAddress = new Address($order->id_address_invoice);
$deliveryAddress = new Address($order->id_address_delivery);

$carrierInformationId = $this->informationRepository->getMollieCarrierInformationIdByCarrierId($order->id_carrier);

$carrierInformation = new MolCarrierInformation($carrierInformationId);

if (!Validate::isLoadedObject($invoiceAddress)
|| !Validate::isLoadedObject($deliveryAddress)
|| !Validate::isLoadedObject($carrierInformation)
Expand All @@ -85,17 +91,21 @@ public function getShipmentInformation($orderReference)
}

$langId = Context::getContext()->language->id;

if (Config::MOLLIE_CARRIER_MODULE === $carrierInformation->url_source) {
$carrier = new Carrier($order->id_carrier);

if (in_array($carrier->external_module_name, ['postnl', 'myparcel'])) {
$table = 'postnl' === $carrier->external_module_name ? 'postnlmod_order' : 'myparcel_order';

try {
$info = $this->orderShipmentRepository->getShipmentInformation($table, $order->id);

if ($info['tracktrace'] && $info['postcode']) {
$postcode = Tools::strtoupper(str_replace(' ', '', $info['postcode']));
$langIso = Tools::strtoupper(Language::getIsoById($langId));
$countryIso = Tools::strtoupper(Country::getIsoById($deliveryAddress->id_country));

$tracktrace = $info['tracktrace'];

return [
Expand All @@ -119,31 +129,27 @@ public function getShipmentInformation($orderReference)

if (Config::MOLLIE_CARRIER_CARRIER === $carrierInformation->url_source) {
$carrier = new Carrier($order->id_carrier);
$shippingNumber = $order->shipping_number;
if (!$shippingNumber && method_exists($order, 'getIdOrderCarrier')) {
$orderCarrier = new OrderCarrier($order->getIdOrderCarrier());
$shippingNumber = $orderCarrier->tracking_number;
}
$orderCarrier = new OrderCarrier($order->getIdOrderCarrier());

if (!$shippingNumber || !$carrier->name) {
if (!$carrier->id || !$orderCarrier->id || !$carrier->name || !$orderCarrier->tracking_number) {
return [];
}

return [
'tracking' => [
'carrier' => $carrier->name,
'code' => $shippingNumber,
'url' => str_replace('@', $shippingNumber, $carrier->url),
'code' => $orderCarrier->tracking_number,
'url' => str_replace('@', $orderCarrier->tracking_number, $carrier->url),
],
];
}

if (Config::MOLLIE_CARRIER_CUSTOM === $carrierInformation->url_source) {
$carrier = new Carrier($order->id_carrier);
$shippingNumber = $order->shipping_number;
if (!$shippingNumber && method_exists($order, 'getIdOrderCarrier')) {
$orderCarrier = new OrderCarrier($order->getIdOrderCarrier());
$shippingNumber = $orderCarrier->tracking_number;
$orderCarrier = new OrderCarrier($order->getIdOrderCarrier());

if (!$carrier->id || !$orderCarrier->id || !$carrier->name || !$orderCarrier->tracking_number) {
return [];
}

$invoicePostcode = Tools::strtoupper(str_replace(' ', '', $invoiceAddress->postcode));
Expand All @@ -153,13 +159,9 @@ public function getShipmentInformation($orderReference)

$langIso = Tools::strtoupper(Language::getIsoById($langId));

if (!$carrier->name || !$shippingNumber) {
return [];
}

$info = [
'@' => $shippingNumber,
'%%shipping_number%%' => $shippingNumber,
'@' => $orderCarrier->tracking_number,
'%%shipping_number%%' => $orderCarrier->tracking_number,
'%%invoice.country_iso%%' => $invoiceCountryIso,
'%%invoice.postcode%%' => $invoicePostcode,
'%%delivery.country_iso%%' => $deliveryCountryIso,
Expand All @@ -170,7 +172,7 @@ public function getShipmentInformation($orderReference)
return [
'tracking' => [
'carrier' => $carrier->name,
'code' => $shippingNumber,
'code' => $orderCarrier->tracking_number,
'url' => str_ireplace(
array_keys($info),
array_values($info),
Expand Down

0 comments on commit 91b83fb

Please sign in to comment.