ToOrderConverter.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\Quote;
  7. use Magento\Sales\Api\Data\OrderInterface;
  8. use Magento\Quote\Model\Quote\Address\ToOrder as QuoteAddressToOrder;
  9. use Magento\Quote\Model\Quote\Address as QuoteAddress;
  10. class ToOrderConverter
  11. {
  12. /**
  13. * @var QuoteAddress
  14. */
  15. protected $quoteAddress;
  16. /**
  17. * @var \Magento\Sales\Api\Data\OrderExtensionFactory
  18. */
  19. protected $orderExtensionFactory;
  20. /**
  21. * @param \Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory
  22. */
  23. public function __construct(
  24. \Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory
  25. ) {
  26. $this->orderExtensionFactory = $orderExtensionFactory;
  27. }
  28. /**
  29. * @param QuoteAddressToOrder $subject
  30. * @param QuoteAddress $address
  31. * @param array $additional
  32. * @return array
  33. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  34. */
  35. public function beforeConvert(QuoteAddressToOrder $subject, QuoteAddress $address, $additional = [])
  36. {
  37. $this->quoteAddress = $address;
  38. return [$address, $additional];
  39. }
  40. /**
  41. * @param QuoteAddressToOrder $subject
  42. * @param OrderInterface $order
  43. * @return OrderInterface
  44. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  45. */
  46. public function afterConvert(QuoteAddressToOrder $subject, OrderInterface $order)
  47. {
  48. /** @var \Magento\Sales\Model\Order $order */
  49. $taxes = $this->quoteAddress->getAppliedTaxes();
  50. $extensionAttributes = $order->getExtensionAttributes();
  51. if ($extensionAttributes == null) {
  52. $extensionAttributes = $this->orderExtensionFactory->create();
  53. }
  54. if (!empty($taxes)) {
  55. foreach ($taxes as $key => $tax) {
  56. $tax['extension_attributes']['rates'] = $tax['rates'];
  57. unset($tax['rates']);
  58. $taxes[$key] = $tax;
  59. }
  60. $extensionAttributes->setAppliedTaxes($taxes);
  61. $extensionAttributes->setConvertingFromQuote(true);
  62. }
  63. $itemAppliedTaxes = $this->quoteAddress->getItemsAppliedTaxes();
  64. $itemAppliedTaxesModified = [];
  65. if (!empty($itemAppliedTaxes)) {
  66. foreach ($itemAppliedTaxes as $key => $itemAppliedTaxItem) {
  67. if (is_array($itemAppliedTaxItem) && !empty($itemAppliedTaxItem)) {
  68. foreach ($itemAppliedTaxItem as $itemAppliedTax) {
  69. $itemAppliedTaxesModified[$key]['type'] = $itemAppliedTax['item_type'];
  70. $itemAppliedTaxesModified[$key]['item_id'] = $itemAppliedTax['item_id'];
  71. $itemAppliedTaxesModified[$key]['associated_item_id'] = $itemAppliedTax['associated_item_id'];
  72. $itemAppliedTax['extension_attributes']['rates'] = $itemAppliedTax['rates'];
  73. unset($itemAppliedTax['rates']);
  74. $itemAppliedTaxesModified[$key]['applied_taxes'][] = $itemAppliedTax;
  75. }
  76. }
  77. }
  78. $extensionAttributes->setItemAppliedTaxes($itemAppliedTaxesModified);
  79. }
  80. $order->setExtensionAttributes($extensionAttributes);
  81. return $order;
  82. }
  83. }