Skip to content

Commit 53fe071

Browse files
author
Robert He
committed
Merge branch 'FearlessKiwis-MAGETWO-37948-order-tax-not-saved' of https://github.corp.ebay.com/magento-fearless-kiwis/magento2ce into develop
2 parents 6f34e42 + 830b8b1 commit 53fe071

File tree

10 files changed

+875
-184
lines changed

10 files changed

+875
-184
lines changed

app/code/Magento/Sales/etc/extension_attributes.xml

Lines changed: 0 additions & 14 deletions
This file was deleted.

app/code/Magento/Tax/Model/Observer.php

Lines changed: 0 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@ class Observer
2020
*/
2121
protected $_taxData;
2222

23-
/**
24-
* @var \Magento\Tax\Model\Sales\Order\TaxFactory
25-
*/
26-
protected $_orderTaxFactory;
27-
28-
/**
29-
* @var \Magento\Sales\Model\Order\Tax\ItemFactory
30-
*/
31-
protected $_taxItemFactory;
32-
3323
/**
3424
* @var \Magento\Tax\Model\Calculation
3525
*/
@@ -57,8 +47,6 @@ class Observer
5747

5848
/**
5949
* @param \Magento\Tax\Helper\Data $taxData
60-
* @param \Magento\Tax\Model\Sales\Order\TaxFactory $orderTaxFactory
61-
* @param \Magento\Sales\Model\Order\Tax\ItemFactory $taxItemFactory
6250
* @param \Magento\Tax\Model\Calculation $calculation
6351
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
6452
* @param \Magento\Tax\Model\Resource\Report\TaxFactory $reportTaxFactory
@@ -67,17 +55,13 @@ class Observer
6755
*/
6856
public function __construct(
6957
\Magento\Tax\Helper\Data $taxData,
70-
\Magento\Tax\Model\Sales\Order\TaxFactory $orderTaxFactory,
71-
\Magento\Sales\Model\Order\Tax\ItemFactory $taxItemFactory,
7258
\Magento\Tax\Model\Calculation $calculation,
7359
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
7460
\Magento\Tax\Model\Resource\Report\TaxFactory $reportTaxFactory,
7561
\Magento\Framework\Locale\ResolverInterface $localeResolver,
7662
\Magento\Framework\Registry $registry
7763
) {
7864
$this->_taxData = $taxData;
79-
$this->_orderTaxFactory = $orderTaxFactory;
80-
$this->_taxItemFactory = $taxItemFactory;
8165
$this->_calculation = $calculation;
8266
$this->_localeDate = $localeDate;
8367
$this->_reportTaxFactory = $reportTaxFactory;
@@ -114,145 +98,6 @@ public function salesEventConvertQuoteAddressToOrder(\Magento\Framework\Event\Ob
11498
}
11599
}
116100

117-
/**
118-
* Save order tax information
119-
*
120-
* @param \Magento\Framework\Event\Observer $observer
121-
* @return void
122-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
123-
* @SuppressWarnings(PHPMD.NPathComplexity)
124-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
125-
*/
126-
public function salesEventOrderAfterSave(\Magento\Framework\Event\Observer $observer)
127-
{
128-
$order = $observer->getEvent()->getOrder();
129-
130-
if (!$order->getConvertingFromQuote() || $order->getAppliedTaxIsSaved()) {
131-
return;
132-
}
133-
134-
$taxesAttr = $order->getCustomAttribute('applied_taxes');
135-
if (is_null($taxesAttr) || !is_array($taxesAttr->getValue())) {
136-
$taxes = [];
137-
} else {
138-
$taxes = $taxesAttr->getValue();
139-
}
140-
141-
$getTaxesForItemsAttr = $order->getCustomAttribute('item_applied_taxes');
142-
if (is_null($getTaxesForItemsAttr) || !is_array($getTaxesForItemsAttr->getValue())) {
143-
$getTaxesForItems = [];
144-
} else {
145-
$getTaxesForItems = $getTaxesForItemsAttr->getValue();
146-
}
147-
148-
$ratesIdQuoteItemId = [];
149-
foreach ($getTaxesForItems as $taxesArray) {
150-
foreach ($taxesArray as $rates) {
151-
if (count($rates['rates']) == 1) {
152-
$ratesIdQuoteItemId[$rates['id']][] = [
153-
'id' => $rates['item_id'],
154-
'percent' => $rates['percent'],
155-
'code' => $rates['rates'][0]['code'],
156-
'associated_item_id' => $rates['associated_item_id'],
157-
'item_type' => $rates['item_type'],
158-
'amount' => $rates['amount'],
159-
'base_amount' => $rates['base_amount'],
160-
'real_amount' => $rates['amount'],
161-
'real_base_amount' => $rates['base_amount'],
162-
];
163-
} else {
164-
$percentSum = 0;
165-
foreach ($rates['rates'] as $rate) {
166-
$real_amount = $rates['amount'] * $rate['percent'] / $rates['percent'];
167-
$real_base_amount = $rates['base_amount'] * $rate['percent'] / $rates['percent'];
168-
$ratesIdQuoteItemId[$rates['id']][] = [
169-
'id' => $rates['item_id'],
170-
'percent' => $rate['percent'],
171-
'code' => $rate['code'],
172-
'associated_item_id' => $rates['associated_item_id'],
173-
'item_type' => $rates['item_type'],
174-
'amount' => $rates['amount'],
175-
'base_amount' => $rates['base_amount'],
176-
'real_amount' => $real_amount,
177-
'real_base_amount' => $real_base_amount,
178-
];
179-
$percentSum += $rate['percent'];
180-
}
181-
}
182-
}
183-
}
184-
185-
foreach ($taxes as $row) {
186-
$id = $row['id'];
187-
foreach ($row['rates'] as $tax) {
188-
if (is_null($row['percent'])) {
189-
$baseRealAmount = $row['base_amount'];
190-
} else {
191-
if ($row['percent'] == 0 || $tax['percent'] == 0) {
192-
continue;
193-
}
194-
$baseRealAmount = $row['base_amount'] / $row['percent'] * $tax['percent'];
195-
}
196-
$hidden = isset($row['hidden']) ? $row['hidden'] : 0;
197-
$priority = isset($tax['priority']) ? $tax['priority'] : 0;
198-
$position = isset($tax['position']) ? $tax['position'] : 0;
199-
$process = isset($row['process']) ? $row['process'] : 0;
200-
$data = [
201-
'order_id' => $order->getId(),
202-
'code' => $tax['code'],
203-
'title' => $tax['title'],
204-
'hidden' => $hidden,
205-
'percent' => $tax['percent'],
206-
'priority' => $priority,
207-
'position' => $position,
208-
'amount' => $row['amount'],
209-
'base_amount' => $row['base_amount'],
210-
'process' => $process,
211-
'base_real_amount' => $baseRealAmount,
212-
];
213-
214-
/** @var $orderTax \Magento\Tax\Model\Sales\Order\Tax */
215-
$orderTax = $this->_orderTaxFactory->create();
216-
$result = $orderTax->setData($data)->save();
217-
218-
if (isset($ratesIdQuoteItemId[$id])) {
219-
foreach ($ratesIdQuoteItemId[$id] as $quoteItemId) {
220-
if ($quoteItemId['code'] == $tax['code']) {
221-
$itemId = null;
222-
$associatedItemId = null;
223-
if (isset($quoteItemId['id'])) {
224-
//This is a product item
225-
$item = $order->getItemByQuoteItemId($quoteItemId['id']);
226-
$itemId = $item->getId();
227-
} elseif (isset($quoteItemId['associated_item_id'])) {
228-
//This item is associated with a product item
229-
$item = $order->getItemByQuoteItemId($quoteItemId['associated_item_id']);
230-
$associatedItemId = $item->getId();
231-
}
232-
233-
$data = [
234-
'item_id' => $itemId,
235-
'tax_id' => $result->getTaxId(),
236-
'tax_percent' => $quoteItemId['percent'],
237-
'associated_item_id' => $associatedItemId,
238-
'amount' => $quoteItemId['amount'],
239-
'base_amount' => $quoteItemId['base_amount'],
240-
'real_amount' => $quoteItemId['real_amount'],
241-
'real_base_amount' => $quoteItemId['real_base_amount'],
242-
'taxable_item_type' => $quoteItemId['item_type'],
243-
];
244-
/** @var $taxItem \Magento\Sales\Model\Order\Tax\Item */
245-
$taxItem = $this->_taxItemFactory->create();
246-
$taxItem->setData($data)->save();
247-
}
248-
}
249-
}
250-
}
251-
}
252-
253-
$order->setAppliedTaxIsSaved(true);
254-
}
255-
256101
/**
257102
* Refresh sales tax report statistics for last day
258103
*
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
namespace Magento\Tax\Model\Plugin;
9+
10+
class OrderSave
11+
{
12+
/**
13+
* @var \Magento\Tax\Model\Sales\Order\TaxFactory
14+
*/
15+
protected $orderTaxFactory;
16+
17+
/**
18+
* @var \Magento\Sales\Model\Order\Tax\ItemFactory
19+
*/
20+
protected $taxItemFactory;
21+
22+
/**
23+
* @param \Magento\Tax\Model\Sales\Order\TaxFactory $orderTaxFactory
24+
* @param \Magento\Sales\Model\Order\Tax\ItemFactory $taxItemFactory
25+
*/
26+
public function __construct(
27+
\Magento\Tax\Model\Sales\Order\TaxFactory $orderTaxFactory,
28+
\Magento\Sales\Model\Order\Tax\ItemFactory $taxItemFactory
29+
) {
30+
$this->orderTaxFactory = $orderTaxFactory;
31+
$this->taxItemFactory = $taxItemFactory;
32+
}
33+
34+
/**
35+
* Save order tax
36+
*
37+
* @param \Magento\Sales\Api\OrderRepositoryInterface $subject
38+
* @param \Magento\Sales\Api\Data\OrderInterface $order
39+
* @return \Magento\Sales\Api\Data\OrderInterface
40+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
41+
*/
42+
public function afterSave(
43+
\Magento\Sales\Api\OrderRepositoryInterface $subject,
44+
\Magento\Sales\Api\Data\OrderInterface $order
45+
) {
46+
$this->saveOrderTax($order);
47+
return $order;
48+
}
49+
50+
/**
51+
* @param \Magento\Sales\Api\Data\OrderInterface $order
52+
* @return $this
53+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
54+
* @SuppressWarnings(PHPMD.NPathComplexity)
55+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
56+
*/
57+
protected function saveOrderTax(\Magento\Sales\Api\Data\OrderInterface $order)
58+
{
59+
$extensionAttribute = $order->getExtensionAttributes();
60+
if (!$extensionAttribute ||
61+
!$extensionAttribute->getConvertingFromQuote() ||
62+
$order->getAppliedTaxIsSaved()) {
63+
return;
64+
}
65+
66+
$taxes = $extensionAttribute->getAppliedTaxes();
67+
if ($taxes == null) {
68+
$taxes = [];
69+
}
70+
71+
$taxesForItems = $extensionAttribute->getItemAppliedTaxes();
72+
if ($taxesForItems == null) {
73+
$taxesForItems = [];
74+
}
75+
76+
$ratesIdQuoteItemId = [];
77+
foreach ($taxesForItems as $taxesArray) {
78+
foreach ($taxesArray as $rates) {
79+
if (count($rates['rates']) == 1) {
80+
$ratesIdQuoteItemId[$rates['id']][] = [
81+
'id' => $rates['item_id'],
82+
'percent' => $rates['percent'],
83+
'code' => $rates['rates'][0]['code'],
84+
'associated_item_id' => $rates['associated_item_id'],
85+
'item_type' => $rates['item_type'],
86+
'amount' => $rates['amount'],
87+
'base_amount' => $rates['base_amount'],
88+
'real_amount' => $rates['amount'],
89+
'real_base_amount' => $rates['base_amount'],
90+
];
91+
} else {
92+
$percentSum = 0;
93+
foreach ($rates['rates'] as $rate) {
94+
$realAmount = $rates['amount'] * $rate['percent'] / $rates['percent'];
95+
$realBaseAmount = $rates['base_amount'] * $rate['percent'] / $rates['percent'];
96+
$ratesIdQuoteItemId[$rates['id']][] = [
97+
'id' => $rates['item_id'],
98+
'percent' => $rate['percent'],
99+
'code' => $rate['code'],
100+
'associated_item_id' => $rates['associated_item_id'],
101+
'item_type' => $rates['item_type'],
102+
'amount' => $rates['amount'],
103+
'base_amount' => $rates['base_amount'],
104+
'real_amount' => $realAmount,
105+
'real_base_amount' => $realBaseAmount,
106+
];
107+
$percentSum += $rate['percent'];
108+
}
109+
}
110+
}
111+
}
112+
113+
foreach ($taxes as $row) {
114+
$id = $row['id'];
115+
foreach ($row['rates'] as $tax) {
116+
if ($row['percent'] == null) {
117+
$baseRealAmount = $row['base_amount'];
118+
} else {
119+
if ($row['percent'] == 0 || $tax['percent'] == 0) {
120+
continue;
121+
}
122+
$baseRealAmount = $row['base_amount'] / $row['percent'] * $tax['percent'];
123+
}
124+
$hidden = isset($row['hidden']) ? $row['hidden'] : 0;
125+
$priority = isset($tax['priority']) ? $tax['priority'] : 0;
126+
$position = isset($tax['position']) ? $tax['position'] : 0;
127+
$process = isset($row['process']) ? $row['process'] : 0;
128+
$data = [
129+
'order_id' => $order->getEntityId(),
130+
'code' => $tax['code'],
131+
'title' => $tax['title'],
132+
'hidden' => $hidden,
133+
'percent' => $tax['percent'],
134+
'priority' => $priority,
135+
'position' => $position,
136+
'amount' => $row['amount'],
137+
'base_amount' => $row['base_amount'],
138+
'process' => $process,
139+
'base_real_amount' => $baseRealAmount,
140+
];
141+
142+
/** @var $orderTax \Magento\Tax\Model\Sales\Order\Tax */
143+
$orderTax = $this->orderTaxFactory->create();
144+
$result = $orderTax->setData($data)->save();
145+
146+
if (isset($ratesIdQuoteItemId[$id])) {
147+
foreach ($ratesIdQuoteItemId[$id] as $quoteItemId) {
148+
if ($quoteItemId['code'] == $tax['code']) {
149+
$itemId = null;
150+
$associatedItemId = null;
151+
if (isset($quoteItemId['id'])) {
152+
//This is a product item
153+
$item = $order->getItemByQuoteItemId($quoteItemId['id']);
154+
$itemId = $item->getId();
155+
} elseif (isset($quoteItemId['associated_item_id'])) {
156+
//This item is associated with a product item
157+
$item = $order->getItemByQuoteItemId($quoteItemId['associated_item_id']);
158+
$associatedItemId = $item->getId();
159+
}
160+
161+
$data = [
162+
'item_id' => $itemId,
163+
'tax_id' => $result->getTaxId(),
164+
'tax_percent' => $quoteItemId['percent'],
165+
'associated_item_id' => $associatedItemId,
166+
'amount' => $quoteItemId['amount'],
167+
'base_amount' => $quoteItemId['base_amount'],
168+
'real_amount' => $quoteItemId['real_amount'],
169+
'real_base_amount' => $quoteItemId['real_base_amount'],
170+
'taxable_item_type' => $quoteItemId['item_type'],
171+
];
172+
/** @var $taxItem \Magento\Sales\Model\Order\Tax\Item */
173+
$taxItem = $this->taxItemFactory->create();
174+
$taxItem->setData($data)->save();
175+
}
176+
}
177+
}
178+
}
179+
}
180+
181+
$order->setAppliedTaxIsSaved(true);
182+
return $this;
183+
}
184+
}

0 commit comments

Comments
 (0)