Skip to content

Commit

Permalink
fix(orders): fix automated order status settings not saving
Browse files Browse the repository at this point in the history
Resolves #237
  • Loading branch information
EdieLemoine committed Nov 27, 2024
1 parent d519ed2 commit cf54af6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
14 changes: 11 additions & 3 deletions src/Migration/Pdk/PdkSettingsMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,22 +337,30 @@ private function getSettingsTransformationMap(): Generator
new CastValue(CastValue::CAST_BOOL)
);

$transformStatus = new TransformValue(function ($value) {
if ($value) {
return "status_$value";
}

return (new CastValue(CastValue::CAST_TRI_STATE))->modify($value);
});

yield new MigratableValue(
'MYPARCELNL_LABEL_CREATED_ORDER_STATUS',
implode('.', [OrderSettings::ID, OrderSettings::STATUS_ON_LABEL_CREATE]),
new CastValue(CastValue::CAST_TRI_STATE)
$transformStatus
);

yield new MigratableValue(
'MYPARCELNL_LABEL_SCANNED_ORDER_STATUS',
implode('.', [OrderSettings::ID, OrderSettings::STATUS_WHEN_LABEL_SCANNED]),
new CastValue(CastValue::CAST_TRI_STATE)
$transformStatus
);

yield new MigratableValue(
'MYPARCELNL_DELIVERED_ORDER_STATUS',
implode('.', [OrderSettings::ID, OrderSettings::STATUS_WHEN_DELIVERED]),
new CastValue(CastValue::CAST_TRI_STATE)
$transformStatus
);

/*
Expand Down
20 changes: 13 additions & 7 deletions src/Pdk/Order/Service/PsOrderStatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Context;
use MyParcelNL\Pdk\App\Order\Contract\OrderStatusServiceInterface;
use MyParcelNL\Pdk\Facade\Logger;
use MyParcelNL\PrestaShop\Contract\PsOrderServiceInterface;
use OrderState;

Expand All @@ -31,13 +32,12 @@ public function all(): array
{
$orderStates = OrderState::getOrderStates(Context::getContext()->language->id);

$array = [];
return array_reduce($orderStates, static function ($carry, $orderState) {
$key = sprintf('status_%s', $orderState['id_order_state']);
$carry[$key] = $orderState['name'];

foreach ($orderStates as $orderState) {
$array[$orderState['id_order_state']] = $orderState['name'];
}

return $array;
return $carry;
}, []);
}

/**
Expand All @@ -49,9 +49,15 @@ public function all(): array
public function updateStatus(array $orderIds, string $status): void
{
foreach ($orderIds as $orderId) {
/** @var \Order|null $psOrder */
$psOrder = $this->psOrderService->get($orderId);

$psOrder->setCurrentState((int) $status);
if (! $psOrder) {
Logger::error(sprintf('Order with id %s not found', $orderId));
continue;

Check warning on line 57 in src/Pdk/Order/Service/PsOrderStatusService.php

View check run for this annotation

Codecov / codecov/patch

src/Pdk/Order/Service/PsOrderStatusService.php#L56-L57

Added lines #L56 - L57 were not covered by tests
}

$psOrder->setCurrentState((int) str_replace('status_', '', $status));
}
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Migration/Pdk/PdkSettingsMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
['name' => 'MYPARCELNL_SHARE_CUSTOMER_PHONE', 'value' => '1'],
['name' => 'MYPARCELNL_LABEL_OPEN_DOWNLOAD', 'value' => 'false'],
['name' => 'MYPARCELNL_LABEL_CREATED_ORDER_STATUS', 'value' => '3'],
['name' => 'MYPARCELNL_LABEL_SCANNED_ORDER_STATUS', 'value' => '4'],
['name' => 'MYPARCELNL_LABEL_SCANNED_ORDER_STATUS', 'value' => null],
['name' => 'MYPARCELNL_DELIVERED_ORDER_STATUS', 'value' => '5'],
['name' => 'MYPARCELNL_IGNORE_ORDER_STATUS', 'value' => '8,6'],
['name' => 'MYPARCELNL_STATUS_CHANGE_MAIL', 'value' => '1'],
Expand Down
29 changes: 15 additions & 14 deletions tests/Unit/Pdk/Order/Service/PsOrderStatusServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@

/** @see UsesMockPsPdkInstance::createOrderStates() */
expect($service->all())->toBe([
1 => 'Awaiting check payment',
2 => 'Payment accepted',
3 => 'Processing in progress',
4 => 'Shipped',
5 => 'Delivered',
6 => 'Canceled',
7 => 'Refunded',
8 => 'Payment error',
9 => 'On backorder (paid)',
10 => 'Awaiting bank wire payment',
11 => 'Remote payment accepted',
12 => 'On backorder (not paid)',
13 => 'Awaiting Cash On Delivery validation',
'status_1' => 'Awaiting check payment',
'status_2' => 'Payment accepted',
'status_3' => 'Processing in progress',
'status_4' => 'Shipped',
'status_5' => 'Delivered',
'status_6' => 'Canceled',
'status_7' => 'Refunded',
'status_8' => 'Payment error',
'status_9' => 'On backorder (paid)',
'status_10' => 'Awaiting bank wire payment',
'status_11' => 'Remote payment accepted',
'status_12' => 'On backorder (not paid)',
'status_13' => 'Awaiting Cash On Delivery validation',
]);
});

Expand All @@ -47,8 +47,9 @@
/** @var OrderStatusServiceInterface $service */
$service = Pdk::get(OrderStatusServiceInterface::class);

$service->updateStatus([14, 16], '4');
$service->updateStatus([14, 16], 'status_4');

// Retrieve the orders again to check if the status has been updated
$order14 = new PsOrder(14);
$order16 = new PsOrder(16);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"sendNotificationAfter": -1,
"sendReturnEmail": false,
"shareCustomerInformation": true,
"statusOnLabelCreate": "3",
"statusWhenDelivered": "5",
"statusWhenLabelScanned": "4",
"statusOnLabelCreate": "status_3",
"statusWhenDelivered": "status_5",
"statusWhenLabelScanned": -1,
"trackTraceInAccount": false,
"trackTraceInEmail": false
},
Expand Down

0 comments on commit cf54af6

Please sign in to comment.