Skip to content

Commit 57c1d79

Browse files
committed
ACP2E-1650:Create Shipment API
- added validation for create shipment API call when order contains bundle products
1 parent f0b01ea commit 57c1d79

File tree

5 files changed

+84
-9
lines changed

5 files changed

+84
-9
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Magento\Bundle\Model\Sales\Order;
4+
5+
use Magento\Bundle\Model\Sales\Order\Shipment\BundleShipmentTypeValidator;
6+
use \Laminas\Validator\ValidatorInterface;
7+
use Magento\Sales\Model\Order\Shipment;
8+
9+
class BundleOrderTypeValidator extends BundleShipmentTypeValidator implements ValidatorInterface
10+
{
11+
/**
12+
* @var array
13+
*/
14+
private array $messages = [];
15+
16+
/**
17+
* @param Shipment $value
18+
* @return bool
19+
* @throws \Magento\Framework\Exception\NoSuchEntityException
20+
* @throws \Magento\Sales\Exception\DocumentValidationException
21+
*/
22+
public function isValid($value): bool
23+
{
24+
foreach ($value->getOrder()->getAllItems() as $orderItem) {
25+
foreach ($value->getItems() as $shipmentItem) {
26+
if ($orderItem->getItemId() == $shipmentItem->getOrderItemId()) {
27+
$this->messages = array_merge($this->messages, $this->validate($orderItem));
28+
}
29+
}
30+
}
31+
32+
return empty($this->messages);
33+
}
34+
35+
/**
36+
* @return array|string[]
37+
*/
38+
public function getMessages(): array
39+
{
40+
return $this->messages;
41+
}
42+
}

app/code/Magento/Bundle/etc/webapi_rest/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@
1717
<plugin name="reindex_after_add_child_by_sku" type="Magento\Bundle\Plugin\Api\ProductLinkManagement\ReindexAfterAddChildBySkuPlugin"/>
1818
<plugin name="reindex_after_remove_child" type="Magento\Bundle\Plugin\Api\ProductLinkManagement\ReindexAfterRemoveChildPlugin"/>
1919
</type>
20+
<type name="Magento\Sales\Model\Order\Shipment">
21+
<arguments>
22+
<argument name="validator" xsi:type="object">Magento\Bundle\Model\Sales\Order\BundleOrderTypeValidator</argument>
23+
</arguments>
24+
</type>
2025
</config>

app/code/Magento/Sales/Model/Order/Shipment.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
*/
66
namespace Magento\Sales\Model\Order;
77

8+
use Magento\Bundle\Model\Sales\Order\BundleOrderTypeValidator;
89
use Magento\Framework\Api\AttributeValueFactory;
910
use Magento\Sales\Api\Data\ShipmentInterface;
1011
use Magento\Sales\Model\AbstractModel;
1112
use Magento\Sales\Model\EntityInterface;
1213
use Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\Collection as CommentsCollection;
14+
use Magento\Sales\Model\ValidatorInterface;
1315

1416
/**
1517
* Sales order shipment model
@@ -104,6 +106,11 @@ class Shipment extends AbstractModel implements EntityInterface, ShipmentInterfa
104106
*/
105107
private $commentsCollection;
106108

109+
/**
110+
* @var ValidatorInterface|null
111+
*/
112+
private $validator;
113+
107114
/**
108115
* @param \Magento\Framework\Model\Context $context
109116
* @param \Magento\Framework\Registry $registry
@@ -117,6 +124,7 @@ class Shipment extends AbstractModel implements EntityInterface, ShipmentInterfa
117124
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
118125
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
119126
* @param array $data
127+
* @param ValidatorInterface|null $validator
120128
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
121129
*/
122130
public function __construct(
@@ -131,13 +139,15 @@ public function __construct(
131139
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
132140
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
133141
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
134-
array $data = []
142+
array $data = [],
143+
?ValidatorInterface $validator = null
135144
) {
136145
$this->_shipmentItemCollectionFactory = $shipmentItemCollectionFactory;
137146
$this->_trackCollectionFactory = $trackCollectionFactory;
138147
$this->_commentFactory = $commentFactory;
139148
$this->_commentCollectionFactory = $commentCollectionFactory;
140149
$this->orderRepository = $orderRepository;
150+
$this->validator = $validator;
141151
parent::__construct(
142152
$context,
143153
$registry,
@@ -159,6 +169,14 @@ protected function _construct()
159169
$this->_init(\Magento\Sales\Model\ResourceModel\Order\Shipment::class);
160170
}
161171

172+
/**
173+
* @inheritDoc
174+
*/
175+
protected function _getValidationRulesBeforeSave(): ?ValidatorInterface
176+
{
177+
return $this->validator;
178+
}
179+
162180
/**
163181
* Load shipment by increment id
164182
*

app/code/Magento/Sales/Model/Order/ShipmentRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ public function save(\Magento\Sales\Api\Data\ShipmentInterface $entity)
146146
try {
147147
$this->metadata->getMapper()->save($entity);
148148
$this->registry[$entity->getEntityId()] = $entity;
149+
} catch (\Magento\Framework\Validator\Exception $exception) {
150+
throw new CouldNotSaveException(__($exception->getMessage()), $exception);
149151
} catch (\Exception $e) {
150152
throw new CouldNotSaveException(__("The shipment couldn't be saved."), $e);
151153
}

lib/internal/Magento/Framework/Model/AbstractModel.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Laminas\Validator\ValidatorChain;
99
use Laminas\Validator\ValidatorInterface;
1010
use Magento\Framework\DataObject;
11+
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\Framework\Phrase;
1213

1314
/**
@@ -754,6 +755,7 @@ protected function _getValidatorBeforeSave()
754755
* Returns FALSE, if no validation rules exist.
755756
*
756757
* @return ValidatorInterface|bool
758+
* @throws LocalizedException
757759
*/
758760
protected function _createValidatorBeforeSave()
759761
{
@@ -763,19 +765,25 @@ protected function _createValidatorBeforeSave()
763765
return false;
764766
}
765767

766-
if ($modelRules && $resourceRules) {
767-
$validator = new ValidatorChain();
768-
$validator->addValidator($modelRules);
769-
$validator->addValidator($resourceRules);
770-
} elseif ($modelRules) {
771-
$validator = $modelRules;
772-
} else {
773-
$validator = $resourceRules;
768+
$validator = $this->getValidator();
769+
if ($modelRules) {
770+
$validator->attach($modelRules);
771+
}
772+
if ($resourceRules) {
773+
$validator->attach($resourceRules);
774774
}
775775

776776
return $validator;
777777
}
778778

779+
/**
780+
* @return ValidatorChain
781+
*/
782+
protected function getValidator(): ValidatorChain
783+
{
784+
return \Magento\Framework\App\ObjectManager::getInstance()->create(ValidatorChain::class);
785+
}
786+
779787
/**
780788
* Template method to return validate rules for the entity
781789
*

0 commit comments

Comments
 (0)