Skip to content

Commit a6e485f

Browse files
feat(msi): support multiple sources (#633)
* feat: support multiple msi sources
1 parent ff7ffd4 commit a6e485f

File tree

5 files changed

+46
-54
lines changed

5 files changed

+46
-54
lines changed

Controller/Adminhtml/Order/CreateAndPrintMyParcelTrack.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ private function massAction()
112112

113113
if (! $this->orderCollection->hasShipment()) {
114114
$this->messageManager->addErrorMessage(__(MagentoOrderCollection::ERROR_ORDER_HAS_NO_SHIPMENT));
115+
}
116+
117+
if ($this->messageManager->getMessages()->getErrors()) {
118+
$this->messageManager->getMessages();
115119

116120
return $this;
117121
}

Model/Sales/MagentoCollection.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
*/
3131
abstract class MagentoCollection implements MagentoCollectionInterface
3232
{
33-
public const PATH_HELPER_DATA = '\MyParcelNL\Magento\Helper\Data';
34-
public const PATH_MODEL_ORDER = '\Magento\Sales\Model\ResourceModel\Order\Collection';
35-
public const PATH_MODEL_SHIPMENT = '\Magento\Sales\Model\ResourceModel\Order\Shipment\Collection';
36-
public const ERROR_ORDER_HAS_NO_SHIPMENT = 'No shipment can be made with this order. Shipments can not be created if the status is On Hold or if the product is digital.';
33+
public const PATH_HELPER_DATA = '\MyParcelNL\Magento\Helper\Data';
34+
public const PATH_MODEL_ORDER = '\Magento\Sales\Model\ResourceModel\Order\Collection';
35+
public const PATH_MODEL_SHIPMENT = '\Magento\Sales\Model\ResourceModel\Order\Shipment\Collection';
36+
public const ERROR_ORDER_HAS_NO_SHIPMENT = 'No shipment can be made with this order. Shipments can not be created if the status is On Hold or if the product is digital.';
37+
public const ERROR_ORDER_HAS_NO_SOURCE = 'Creating shipments via bulk actions is not possible for orders without a source. Go to the details of the order and process the shipment manually.';
38+
public const DEFAULT_ERROR_ORDER_HAS_NO_SOURCE = 'Source item not found by source code';
3739

3840
private const PATH_ORDER_TRACK = '\Magento\Sales\Model\Order\Shipment\Track';
3941
private const PATH_MANAGER_INTERFACE = '\Magento\Framework\Message\ManagerInterface';

Model/Sales/MagentoOrderCollection.php

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use MyParcelNL\Sdk\src\Support\Collection;
2929
use Magento\Framework\App\Config\ScopeConfigInterface;
3030

31+
3132
/**
3233
* Class MagentoOrderCollection
3334
*
@@ -63,9 +64,9 @@ class MagentoOrderCollection extends MagentoCollection
6364
private $shippingRecipient;
6465

6566
/**
66-
* @param ObjectManagerInterface $objectManager
67-
* @param \Magento\Framework\App\RequestInterface $request
68-
* @param null $areaList
67+
* @param ObjectManagerInterface $objectManager
68+
* @param \Magento\Framework\App\RequestInterface $request
69+
* @param null $areaList
6970
*/
7071
public function __construct(ObjectManagerInterface $objectManager, $request = null, $areaList = null)
7172
{
@@ -510,6 +511,13 @@ private function createShipment(Order $order)
510511
$convertOrder = $this->objectManager->create('Magento\Sales\Model\Convert\Order');
511512
$shipment = $convertOrder->toShipment($order);
512513

514+
$shipmentAttributes = $shipment->getExtensionAttributes();
515+
516+
if (method_exists($shipmentAttributes, 'setSourceCode')) {
517+
$shipmentAttributes->setSourceCode($this->sourceItem->getSource($order, $order->getAllItems()));
518+
$shipment->setExtensionAttributes($shipmentAttributes);
519+
}
520+
513521
// Loop through order items
514522
foreach ($order->getAllItems() as $orderItem) {
515523
// Check if order item has qty to ship or is virtual
@@ -524,11 +532,6 @@ private function createShipment(Order $order)
524532

525533
// Add shipment item to shipment
526534
$shipment->addItem($shipmentItem);
527-
528-
if ($this->sourceItem) {
529-
$source = $this->getMultiStockInventory($orderItem);
530-
$shipment->getExtensionAttributes()->setSourceCode($source);
531-
}
532535
}
533536

534537
// Register shipment
@@ -544,29 +547,15 @@ private function createShipment(Order $order)
544547
$this->objectManager->create('Magento\Shipping\Model\ShipmentNotifier')
545548
->notify($shipment);
546549
} catch (\Exception $e) {
547-
throw new LocalizedException(
548-
__($e->getMessage())
549-
);
550-
}
551-
}
552550

553-
/**
554-
* @param \Magento\Sales\Model\Order\Item $orderItem
555-
*
556-
* @return string
557-
*/
558-
private function getMultiStockInventory(\Magento\Sales\Model\Order\Item $orderItem): string
559-
{
560-
$sku = $orderItem->getSku();
561-
$result = $this->sourceItem->getSourceItemDetailBySKU($sku);
562-
563-
foreach ($result as $item) {
564-
if ($item->getSourceCode() !== 'default') {
565-
return $item->getSourceCode();
551+
if (preg_match('/' . MagentoOrderCollection::DEFAULT_ERROR_ORDER_HAS_NO_SOURCE . '/', $e->getMessage())) {
552+
$this->messageManager->addErrorMessage(__(MagentoOrderCollection::ERROR_ORDER_HAS_NO_SOURCE));
553+
} else {
554+
$this->messageManager->addErrorMessage(__($e->getMessage()));
566555
}
567-
}
568556

569-
return 'default';
557+
$this->objectManager->get('Psr\Log\LoggerInterface')->critical($e);
558+
}
570559
}
571560

572561
/**

Model/Source/SourceItem.php

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,38 @@
22

33
namespace MyParcelNL\Magento\Model\Source;
44

5-
use Magento\Framework\Api\SearchCriteriaBuilder;
6-
use Magento\InventoryApi\Api\Data\SourceItemInterface;
7-
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
5+
use Magento\Sales\Model\Order\ShipmentFactory;
86

97
class SourceItem
108
{
119
/**
12-
* @var SearchCriteriaBuilder
10+
* @var ShipmentFactory
1311
*/
14-
private $searchCriteriaBuilder;
15-
16-
/**
17-
* @var SourceItemRepositoryInterface
18-
*/
19-
private $sourceItemRepository;
12+
private $shipmentFactory;
2013

2114
public function __construct(
22-
SearchCriteriaBuilder $searchCriteriaBuilder,
23-
SourceItemRepositoryInterface $sourceItemRepository
15+
ShipmentFactory $shipmentFactory
2416
) {
25-
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
26-
$this->sourceItemRepository = $sourceItemRepository;
17+
$this->shipmentFactory = $shipmentFactory;
2718
}
2819

2920
/**
30-
* Retrieves links that are assigned to $stockId
21+
* Magento uses an afterCreate plugin on the shipmentFactory to set the SourceCode. In the default flow Magento
22+
* runs this code when you open the Create Shipment page. This behaviour doesn't occur in this flow, so we force
23+
* that flow to happen here.
24+
*
25+
* @param $order
26+
* @param $shipmentItems
3127
*
32-
* @param string $sku
33-
* @return SourceItemInterface[]
3428
*/
35-
public function getSourceItemDetailBySKU(string $sku): array
29+
public function getSource($order, $shipmentItems)
3630
{
37-
$searchCriteria = $this->searchCriteriaBuilder
38-
->addFilter(SourceItemInterface::SKU, $sku)
39-
->create();
31+
$shipment = $this->shipmentFactory->create(
32+
$order,
33+
$shipmentItems
34+
);
4035

41-
return $this->sourceItemRepository->getList($searchCriteria)->getItems();
36+
$extensionAttributes = $shipment->getExtensionAttributes();
37+
return $extensionAttributes->getSourceCode();
4238
}
4339
}

i18n/nl_NL.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Please select an item from the list,Selecteer eerst een order uit de lijst
114114
Please check address,Controleer het adres
115115
No key found. Go to Configuration and then to MyParcel to enter the key.,Geen API key gevonden. Ga naar Configuratie en vervolgens naar MyParcel om de API key in te voeren.
116116
"No shipment can be made with this order. Shipments can not be created if the status is On Hold or if the product is digital.","Er kan geen zending met deze bestelling gemaakt worden. Zendingen kunnen niet worden aangemaakt als de status 'Wordt vastgehouden' is of als het product digitaal is."
117+
"Creating shipments via bulk actions is not possible for orders without a source. Go to the details of the order and process the shipment manually.","Een zending aanmaken via een bulkactie is niet mogelijk voor orders zonder source. Ga naar de details van de order en boek de zending handmatig in."
117118
MyParcel Track,MyParcel barcode
118119
Use Shipment template,Gebruik Verzending template
119120
Enable this option to add the MyParcel barcode in the email.,Activeer deze optie om de MyParcel barcode toe te voegen in de email.

0 commit comments

Comments
 (0)