OrderRepository.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model;
  7. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  8. use Magento\Framework\Exception\InputException;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\Sales\Api\Data\OrderExtensionFactory;
  11. use Magento\Sales\Api\Data\OrderExtensionInterface;
  12. use Magento\Sales\Api\Data\OrderInterface;
  13. use Magento\Sales\Api\Data\OrderSearchResultInterfaceFactory as SearchResultFactory;
  14. use Magento\Sales\Api\Data\ShippingAssignmentInterface;
  15. use Magento\Sales\Model\Order\ShippingAssignmentBuilder;
  16. use Magento\Sales\Model\ResourceModel\Metadata;
  17. use Magento\Framework\App\ObjectManager;
  18. use Magento\Tax\Api\OrderTaxManagementInterface;
  19. use Magento\Payment\Api\Data\PaymentAdditionalInfoInterface;
  20. use Magento\Payment\Api\Data\PaymentAdditionalInfoInterfaceFactory;
  21. use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
  22. /**
  23. * Repository class
  24. *
  25. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  26. */
  27. class OrderRepository implements \Magento\Sales\Api\OrderRepositoryInterface
  28. {
  29. /**
  30. * @var Metadata
  31. */
  32. protected $metadata;
  33. /**
  34. * @var SearchResultFactory
  35. */
  36. protected $searchResultFactory = null;
  37. /**
  38. * @var OrderExtensionFactory
  39. */
  40. private $orderExtensionFactory;
  41. /**
  42. * @var ShippingAssignmentBuilder
  43. */
  44. private $shippingAssignmentBuilder;
  45. /**
  46. * @var CollectionProcessorInterface
  47. */
  48. private $collectionProcessor;
  49. /**
  50. * @var OrderInterface[]
  51. */
  52. protected $registry = [];
  53. /**
  54. * @var OrderTaxManagementInterface
  55. */
  56. private $orderTaxManagement;
  57. /**
  58. * @var PaymentAdditionalInfoFactory
  59. */
  60. private $paymentAdditionalInfoFactory;
  61. /**
  62. * @var JsonSerializer
  63. */
  64. private $serializer;
  65. /**
  66. * Constructor
  67. *
  68. * @param Metadata $metadata
  69. * @param SearchResultFactory $searchResultFactory
  70. * @param CollectionProcessorInterface|null $collectionProcessor
  71. * @param \Magento\Sales\Api\Data\OrderExtensionFactory|null $orderExtensionFactory
  72. * @param OrderTaxManagementInterface|null $orderTaxManagement
  73. * @param PaymentAdditionalInfoInterfaceFactory|null $paymentAdditionalInfoFactory
  74. * @param JsonSerializer|null $serializer
  75. */
  76. public function __construct(
  77. Metadata $metadata,
  78. SearchResultFactory $searchResultFactory,
  79. CollectionProcessorInterface $collectionProcessor = null,
  80. \Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory = null,
  81. OrderTaxManagementInterface $orderTaxManagement = null,
  82. PaymentAdditionalInfoInterfaceFactory $paymentAdditionalInfoFactory = null,
  83. JsonSerializer $serializer = null
  84. ) {
  85. $this->metadata = $metadata;
  86. $this->searchResultFactory = $searchResultFactory;
  87. $this->collectionProcessor = $collectionProcessor ?: ObjectManager::getInstance()
  88. ->get(\Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class);
  89. $this->orderExtensionFactory = $orderExtensionFactory ?: ObjectManager::getInstance()
  90. ->get(\Magento\Sales\Api\Data\OrderExtensionFactory::class);
  91. $this->orderTaxManagement = $orderTaxManagement ?: ObjectManager::getInstance()
  92. ->get(OrderTaxManagementInterface::class);
  93. $this->paymentAdditionalInfoFactory = $paymentAdditionalInfoFactory ?: ObjectManager::getInstance()
  94. ->get(PaymentAdditionalInfoInterfaceFactory::class);
  95. $this->serializer = $serializer ?: ObjectManager::getInstance()
  96. ->get(JsonSerializer::class);
  97. }
  98. /**
  99. * Load entity
  100. *
  101. * @param int $id
  102. * @return \Magento\Sales\Api\Data\OrderInterface
  103. * @throws \Magento\Framework\Exception\InputException
  104. * @throws \Magento\Framework\Exception\NoSuchEntityException
  105. */
  106. public function get($id)
  107. {
  108. if (!$id) {
  109. throw new InputException(__('An ID is needed. Set the ID and try again.'));
  110. }
  111. if (!isset($this->registry[$id])) {
  112. /** @var OrderInterface $entity */
  113. $entity = $this->metadata->getNewInstance()->load($id);
  114. if (!$entity->getEntityId()) {
  115. throw new NoSuchEntityException(
  116. __("The entity that was requested doesn't exist. Verify the entity and try again.")
  117. );
  118. }
  119. $this->setOrderTaxDetails($entity);
  120. $this->setShippingAssignments($entity);
  121. $this->setPaymentAdditionalInfo($entity);
  122. $this->registry[$id] = $entity;
  123. }
  124. return $this->registry[$id];
  125. }
  126. /**
  127. * Set order tax details to extension attributes.
  128. *
  129. * @param OrderInterface $order
  130. * @return void
  131. */
  132. private function setOrderTaxDetails(OrderInterface $order)
  133. {
  134. $extensionAttributes = $order->getExtensionAttributes();
  135. $orderTaxDetails = $this->orderTaxManagement->getOrderTaxDetails($order->getEntityId());
  136. $appliedTaxes = $orderTaxDetails->getAppliedTaxes();
  137. $extensionAttributes->setAppliedTaxes($appliedTaxes);
  138. if (!empty($appliedTaxes)) {
  139. $extensionAttributes->setConvertingFromQuote(true);
  140. }
  141. $items = $orderTaxDetails->getItems();
  142. $extensionAttributes->setItemAppliedTaxes($items);
  143. $order->setExtensionAttributes($extensionAttributes);
  144. }
  145. /**
  146. * Set additional info to the order.
  147. *
  148. * @param OrderInterface $order
  149. * @return void
  150. */
  151. private function setPaymentAdditionalInfo(OrderInterface $order): void
  152. {
  153. $extensionAttributes = $order->getExtensionAttributes();
  154. $paymentAdditionalInformation = $order->getPayment()->getAdditionalInformation();
  155. $objects = [];
  156. foreach ($paymentAdditionalInformation as $key => $value) {
  157. /** @var PaymentAdditionalInfoInterface $additionalInformationObject */
  158. $additionalInformationObject = $this->paymentAdditionalInfoFactory->create();
  159. $additionalInformationObject->setKey($key);
  160. if (!is_string($value)) {
  161. $value = $this->serializer->serialize($value);
  162. }
  163. $additionalInformationObject->setValue($value);
  164. $objects[] = $additionalInformationObject;
  165. }
  166. $extensionAttributes->setPaymentAdditionalInfo($objects);
  167. $order->setExtensionAttributes($extensionAttributes);
  168. }
  169. /**
  170. * Find entities by criteria
  171. *
  172. * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
  173. * @return \Magento\Sales\Api\Data\OrderSearchResultInterface
  174. */
  175. public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  176. {
  177. /** @var \Magento\Sales\Api\Data\OrderSearchResultInterface $searchResult */
  178. $searchResult = $this->searchResultFactory->create();
  179. $this->collectionProcessor->process($searchCriteria, $searchResult);
  180. $searchResult->setSearchCriteria($searchCriteria);
  181. foreach ($searchResult->getItems() as $order) {
  182. $this->setShippingAssignments($order);
  183. $this->setOrderTaxDetails($order);
  184. $this->setPaymentAdditionalInfo($order);
  185. }
  186. return $searchResult;
  187. }
  188. /**
  189. * Register entity to delete
  190. *
  191. * @param \Magento\Sales\Api\Data\OrderInterface $entity
  192. * @return bool
  193. */
  194. public function delete(\Magento\Sales\Api\Data\OrderInterface $entity)
  195. {
  196. $this->metadata->getMapper()->delete($entity);
  197. unset($this->registry[$entity->getEntityId()]);
  198. return true;
  199. }
  200. /**
  201. * Delete entity by Id
  202. *
  203. * @param int $id
  204. * @return bool
  205. */
  206. public function deleteById($id)
  207. {
  208. $entity = $this->get($id);
  209. return $this->delete($entity);
  210. }
  211. /**
  212. * Perform persist operations for one entity
  213. *
  214. * @param \Magento\Sales\Api\Data\OrderInterface $entity
  215. * @return \Magento\Sales\Api\Data\OrderInterface
  216. */
  217. public function save(\Magento\Sales\Api\Data\OrderInterface $entity)
  218. {
  219. /** @var \Magento\Sales\Api\Data\OrderExtensionInterface $extensionAttributes */
  220. $extensionAttributes = $entity->getExtensionAttributes();
  221. if ($entity->getIsNotVirtual() && $extensionAttributes && $extensionAttributes->getShippingAssignments()) {
  222. $shippingAssignments = $extensionAttributes->getShippingAssignments();
  223. if (!empty($shippingAssignments)) {
  224. $shipping = array_shift($shippingAssignments)->getShipping();
  225. $entity->setShippingAddress($shipping->getAddress());
  226. $entity->setShippingMethod($shipping->getMethod());
  227. }
  228. }
  229. $this->metadata->getMapper()->save($entity);
  230. $this->registry[$entity->getEntityId()] = $entity;
  231. return $this->registry[$entity->getEntityId()];
  232. }
  233. /**
  234. * Set shipping assignments to extension attributes.
  235. *
  236. * @param OrderInterface $order
  237. * @return void
  238. */
  239. private function setShippingAssignments(OrderInterface $order)
  240. {
  241. /** @var OrderExtensionInterface $extensionAttributes */
  242. $extensionAttributes = $order->getExtensionAttributes();
  243. if ($extensionAttributes === null) {
  244. $extensionAttributes = $this->orderExtensionFactory->create();
  245. } elseif ($extensionAttributes->getShippingAssignments() !== null) {
  246. return;
  247. }
  248. /** @var ShippingAssignmentInterface $shippingAssignment */
  249. $shippingAssignments = $this->getShippingAssignmentBuilderDependency();
  250. $shippingAssignments->setOrderId($order->getEntityId());
  251. $extensionAttributes->setShippingAssignments($shippingAssignments->create());
  252. $order->setExtensionAttributes($extensionAttributes);
  253. }
  254. /**
  255. * Get the new ShippingAssignmentBuilder dependency for application code
  256. *
  257. * @return ShippingAssignmentBuilder
  258. * @deprecated 100.0.4
  259. */
  260. private function getShippingAssignmentBuilderDependency()
  261. {
  262. if (!$this->shippingAssignmentBuilder instanceof ShippingAssignmentBuilder) {
  263. $this->shippingAssignmentBuilder = \Magento\Framework\App\ObjectManager::getInstance()->get(
  264. \Magento\Sales\Model\Order\ShippingAssignmentBuilder::class
  265. );
  266. }
  267. return $this->shippingAssignmentBuilder;
  268. }
  269. /**
  270. * Helper function that adds a FilterGroup to the collection.
  271. *
  272. * @param \Magento\Framework\Api\Search\FilterGroup $filterGroup
  273. * @param \Magento\Sales\Api\Data\OrderSearchResultInterface $searchResult
  274. * @return void
  275. * @deprecated 101.0.0
  276. * @throws \Magento\Framework\Exception\InputException
  277. */
  278. protected function addFilterGroupToCollection(
  279. \Magento\Framework\Api\Search\FilterGroup $filterGroup,
  280. \Magento\Sales\Api\Data\OrderSearchResultInterface $searchResult
  281. ) {
  282. $fields = [];
  283. $conditions = [];
  284. foreach ($filterGroup->getFilters() as $filter) {
  285. $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
  286. $conditions[] = [$condition => $filter->getValue()];
  287. $fields[] = $filter->getField();
  288. }
  289. if ($fields) {
  290. $searchResult->addFieldToFilter($fields, $conditions);
  291. }
  292. }
  293. }