Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OrderSave.php
Go to the documentation of this file.
1 <?php
9 
10 use Magento\Tax\Api\Data\OrderTaxDetailsAppliedTaxExtension;
11 
12 class OrderSave
13 {
17  protected $orderTaxFactory;
18 
22  protected $taxItemFactory;
23 
28  public function __construct(
29  \Magento\Tax\Model\Sales\Order\TaxFactory $orderTaxFactory,
30  \Magento\Sales\Model\Order\Tax\ItemFactory $taxItemFactory
31  ) {
32  $this->orderTaxFactory = $orderTaxFactory;
33  $this->taxItemFactory = $taxItemFactory;
34  }
35 
44  public function afterSave(
45  \Magento\Sales\Api\OrderRepositoryInterface $subject,
46  \Magento\Sales\Api\Data\OrderInterface $order
47  ) {
48  $this->saveOrderTax($order);
49  return $order;
50  }
51 
59  protected function saveOrderTax(\Magento\Sales\Api\Data\OrderInterface $order)
60  {
61  $extensionAttribute = $order->getExtensionAttributes();
62  if (!$extensionAttribute ||
63  !$extensionAttribute->getConvertingFromQuote() ||
64  $order->getAppliedTaxIsSaved()) {
65  return;
66  }
67 
69  $taxes = $extensionAttribute->getAppliedTaxes();
70  if ($taxes == null) {
71  $taxes = [];
72  }
73 
75  $taxesForItems = $extensionAttribute->getItemAppliedTaxes();
76  if ($taxesForItems == null) {
77  $taxesForItems = [];
78  }
79 
80  $ratesIdQuoteItemId = [];
81  foreach ($taxesForItems as $taxesArray) {
82  foreach ($taxesArray['applied_taxes'] as $rates) {
83  if (isset($rates['extension_attributes'])) {
84  $taxRates = $rates['extension_attributes'] instanceof OrderTaxDetailsAppliedTaxExtension
85  ? $rates['extension_attributes']->getRates()
86  : $rates['extension_attributes']['rates'];
87  if (is_array($taxRates)) {
88  if (count($taxRates) == 1) {
89  $ratesIdQuoteItemId[$rates['id']][] = [
90  'id' => $taxesArray['item_id'],
91  'percent' => $rates['percent'],
92  'code' => $taxRates[0]['code'],
93  'associated_item_id' => $taxesArray['associated_item_id'],
94  'item_type' => $taxesArray['type'],
95  'amount' => $rates['amount'],
96  'base_amount' => $rates['base_amount'],
97  'real_amount' => $rates['amount'],
98  'real_base_amount' => $rates['base_amount'],
99  ];
100  } else {
101  $percentSum = 0;
102  foreach ($taxRates as $rate) {
103  $percentSum += $rate['percent'];
104  }
105 
106  foreach ($taxRates as $rate) {
107  $realAmount = $rates['amount'] * $rate['percent'] / $percentSum;
108  $realBaseAmount = $rates['base_amount'] * $rate['percent'] / $percentSum;
109  $ratesIdQuoteItemId[$rates['id']][] = [
110  'id' => $taxesArray['item_id'],
111  'percent' => $rate['percent'],
112  'code' => $rate['code'],
113  'associated_item_id' => $taxesArray['associated_item_id'],
114  'item_type' => $taxesArray['type'],
115  'amount' => $rates['amount'],
116  'base_amount' => $rates['base_amount'],
117  'real_amount' => $realAmount,
118  'real_base_amount' => $realBaseAmount,
119  ];
120  }
121  }
122  }
123  }
124  }
125  }
126 
127  foreach ($taxes as $row) {
128  $id = $row['id'];
129  if (isset($row['extension_attributes'])) {
130  $taxRates = $row['extension_attributes'] instanceof OrderTaxDetailsAppliedTaxExtension
131  ? $row['extension_attributes']->getRates()
132  : $row['extension_attributes']['rates'];
133  if (is_array($taxRates)) {
134  foreach ($taxRates as $tax) {
135  if ($row['percent'] == null) {
136  $baseRealAmount = $row['base_amount'];
137  } else {
138  if ($row['percent'] == 0 || $tax['percent'] == 0) {
139  continue;
140  }
141  $baseRealAmount = $row['base_amount'] / $row['percent'] * $tax['percent'];
142  }
143  $hidden = isset($row['hidden']) ? $row['hidden'] : 0;
144  $priority = isset($tax['priority']) ? $tax['priority'] : 0;
145  $position = isset($tax['position']) ? $tax['position'] : 0;
146  $process = isset($row['process']) ? $row['process'] : 0;
147  $data = [
148  'order_id' => $order->getEntityId(),
149  'code' => $tax['code'],
150  'title' => $tax['title'],
151  'hidden' => $hidden,
152  'percent' => $tax['percent'],
153  'priority' => $priority,
154  'position' => $position,
155  'amount' => $row['amount'],
156  'base_amount' => $row['base_amount'],
157  'process' => $process,
158  'base_real_amount' => $baseRealAmount,
159  ];
160 
162  $orderTax = $this->orderTaxFactory->create();
163  $result = $orderTax->setData($data)->save();
164 
165  if (isset($ratesIdQuoteItemId[$id])) {
166  foreach ($ratesIdQuoteItemId[$id] as $quoteItemId) {
167  if ($quoteItemId['code'] === $tax['code']) {
168  $itemId = null;
169  $associatedItemId = null;
170  if (isset($quoteItemId['id'])) {
171  //This is a product item
172  $item = $order->getItemByQuoteItemId($quoteItemId['id']);
173  if ($item !== null && $item->getId()) {
174  $itemId = $item->getId();
175  }
176  } elseif (isset($quoteItemId['associated_item_id'])) {
177  //This item is associated with a product item
178  $item = $order->getItemByQuoteItemId($quoteItemId['associated_item_id']);
179  $associatedItemId = $item->getId();
180  }
181 
182  $data = [
183  'item_id' => $itemId,
184  'tax_id' => $result->getTaxId(),
185  'tax_percent' => $quoteItemId['percent'],
186  'associated_item_id' => $associatedItemId,
187  'amount' => $quoteItemId['amount'],
188  'base_amount' => $quoteItemId['base_amount'],
189  'real_amount' => $quoteItemId['real_amount'],
190  'real_base_amount' => $quoteItemId['real_base_amount'],
191  'taxable_item_type' => $quoteItemId['item_type'],
192  ];
194  $taxItem = $this->taxItemFactory->create();
195  $taxItem->setData($data)->save();
196  }
197  }
198  }
199  }
200  }
201  }
202  }
203 
204  $order->setAppliedTaxIsSaved(true);
205  return $this;
206  }
207 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
$order
Definition: order.php:55
$rates
Definition: tax.phtml:35
$quoteItemId
Definition: cart.php:17
__construct(\Magento\Tax\Model\Sales\Order\TaxFactory $orderTaxFactory, \Magento\Sales\Model\Order\Tax\ItemFactory $taxItemFactory)
Definition: OrderSave.php:28
afterSave(\Magento\Sales\Api\OrderRepositoryInterface $subject, \Magento\Sales\Api\Data\OrderInterface $order)
Definition: OrderSave.php:44