Skip to content

Commit

Permalink
fix: fix track trace emails not being sent (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine authored Nov 9, 2021
1 parent be1ccc8 commit d94c563
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Entity/OrderStatus/AbstractOrderStatusUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function sendEmail(string $status): void
try {
OrderLabel::sendShippedNotification($this->shipmentId);
} catch (Exception $e) {
Logger::addLog($e, true);
Logger::addLog($e->getMessage(), true);
}
}
}
Expand Down
53 changes: 31 additions & 22 deletions src/Model/OrderLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,12 @@ public static function sendShippedNotification(int $shipmentId): void
return;
}

$address = new Address($order->id_address_delivery);
$deliveryOptions = DeliveryOptions::getFromOrder($order);
$address = new Address($order->id_address_delivery);
$deliveryOptions = DeliveryOptions::getFromOrder($order);

if (! $deliveryOptions) {
throw new Exception('Delivery options are missing');
}

/** @deprecated use $deliveryOptions */
$oldDeliveryOptions = DeliveryOptions::queryByOrder($order);
Expand All @@ -161,28 +165,33 @@ public static function sendShippedNotification(int $shipmentId): void
'{utc_offset}' => date('P'),
];

$tracktraceInfo = self::getTrackTraceInfo($order_label);

$deliveryDate = $tracktraceInfo['delivery_moment']['start']['date'] ?? $tracktraceInfo['options']['delivery_date'] ?? $deliveryOptions->getDate();
$deliveryDateFrom = $tracktraceInfo['delivery_moment']['start']['date'] ?? $deliveryOptions->getDate();
$deliveryDateTo = $tracktraceInfo['delivery_moment']['end']['date'] ?? $deliveryOptions->getDate();
$monthNumber = (int) date('n', strtotime($deliveryDate));
$templateVars['{delivery_street}'] = $tracktraceInfo['recipient']['street'];
$templateVars['{delivery_number}'] = $tracktraceInfo['recipient']['street_additional_info'] . ' ' . $tracktraceInfo['recipient']['number'];
$templateVars['{delivery_postcode}'] = $tracktraceInfo['recipient']['postal_code'];
$templateVars['{delivery_city}'] = $tracktraceInfo['recipient']['city'];
$templateVars['{delivery_cc}'] = $tracktraceInfo['recipient']['cc'];
$templateVars['{pickup_name}'] = $tracktraceInfo['pickup']['location_name'];
$templateVars['{pickup_street}'] = $tracktraceInfo['pickup']['street'];
$templateVars['{pickup_number}'] = $tracktraceInfo['pickup']['number'];
$templateVars['{pickup_postcode}'] = strtoupper(
str_replace(' ', '', $tracktraceInfo['pickup']['postal_code'])
);
$templateVars['{pickup_region}'] = $tracktraceInfo['pickup']['region'] ?: '-';
$templateVars['{pickup_city}'] = $tracktraceInfo['pickup']['city'];
$templateVars['{pickup_cc}'] = $tracktraceInfo['recipient']['cc'];
$trackTraceInfo = self::getTrackTraceInfo($order_label);

$templateVars['{delivery_street}'] = $trackTraceInfo['recipient']['street'];
$templateVars['{delivery_number}'] = $trackTraceInfo['recipient']['street_additional_info'] . ' ' . $trackTraceInfo['recipient']['number'];
$templateVars['{delivery_postcode}'] = $trackTraceInfo['recipient']['postal_code'];
$templateVars['{delivery_city}'] = $trackTraceInfo['recipient']['city'];
$templateVars['{delivery_cc}'] = $trackTraceInfo['recipient']['cc'];

$deliveryDate = $trackTraceInfo['delivery_moment']['start']['date'] ?? $trackTraceInfo['options']['delivery_date'] ?? $deliveryOptions->getDate();
$deliveryDateFrom = $trackTraceInfo['delivery_moment']['start']['date'] ?? $deliveryOptions->getDate();
$deliveryDateTo = $trackTraceInfo['delivery_moment']['end']['date'] ?? $deliveryOptions->getDate();
$monthNumber = (int) date('n', strtotime($deliveryDate));

if ($deliveryOptions->isPickup()) {
if ($trackTraceInfo['pickup']) {
$templateVars['{pickup_name}'] = $trackTraceInfo['pickup']['location_name'];
$templateVars['{pickup_street}'] = $trackTraceInfo['pickup']['street'];
$templateVars['{pickup_number}'] = $trackTraceInfo['pickup']['number'];
$templateVars['{pickup_postcode}'] = strtoupper(
str_replace(' ', '', $trackTraceInfo['pickup']['postal_code'])
);
$templateVars['{pickup_region}'] = $trackTraceInfo['pickup']['region'] ?: '-';
$templateVars['{pickup_city}'] = $trackTraceInfo['pickup']['city'];
}

$templateVars['{pickup_cc}'] = $trackTraceInfo['recipient']['cc'];

if ('nl' === $orderIso) {
$dayNumber = (int) date('w', strtotime($deliveryDateFrom));
$templateVars['{delivery_day_name}'] = Constant::NL_DAYS[$dayNumber];
Expand Down
4 changes: 3 additions & 1 deletion src/Service/Tracktrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Tracktrace extends AbstractEndpoint
* @return mixed|null
* @throws \MyParcelNL\Sdk\src\Exception\ApiException
* @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException
* @throws \MyParcelNL\Sdk\src\Exception\AccountNotActiveException
* @throws \Exception
*/
public function getTrackTrace(int $shipmentId, bool $withDeliveryMoment = false)
{
Expand All @@ -27,7 +29,7 @@ public function getTrackTrace(int $shipmentId, bool $withDeliveryMoment = false)
->setRequestParameters(
$this->apiKey,
null,
MyParcelRequest::REQUEST_HEADER_RETRIEVE_SHIPMENT
MyParcelRequest::HEADER_ACCEPT_APPLICATION_PDF
)
->sendRequest('GET', Request::REQUEST_TYPE_TRACKTRACE . "/{$shipmentId}{$extraInfo}");

Expand Down

0 comments on commit d94c563

Please sign in to comment.