Skip to content

Commit 46d43ed

Browse files
committed
MAGETWO-53366: Tax Report does not display any records
- adding indentation and code cleanup based on code review comments
1 parent 8d10356 commit 46d43ed

File tree

3 files changed

+110
-114
lines changed

3 files changed

+110
-114
lines changed

app/code/Magento/Tax/Model/Plugin/OrderSave.php

Lines changed: 86 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -78,105 +78,110 @@ protected function saveOrderTax(\Magento\Sales\Api\Data\OrderInterface $order)
7878
$ratesIdQuoteItemId = [];
7979
foreach ($taxesForItems as $taxesArray) {
8080
foreach ($taxesArray['applied_taxes'] as $rates) {
81-
/** @var \Magento\Tax\Api\Data\AppliedTaxRateInterface $rates */
82-
$taxRates = $rates['extension_attributes']->getRates();
83-
if (count($taxRates) == 1) {
84-
$ratesIdQuoteItemId[$rates['id']][] = [
85-
'id' => $taxesArray['item_id'],
86-
'percent' => $rates['percent'],
87-
'code' => $taxRates[0]['code'],
88-
'associated_item_id' => $taxesArray['associated_item_id'],
89-
'item_type' => $taxesArray['type'],
90-
'amount' => $rates['amount'],
91-
'base_amount' => $rates['base_amount'],
92-
'real_amount' => $rates['amount'],
93-
'real_base_amount' => $rates['base_amount'],
94-
];
95-
} else {
96-
$percentSum = 0;
97-
foreach ($taxRates as $rate) {
98-
$realAmount = $rates['amount'] * $rate['percent'] / $rates['percent'];
99-
$realBaseAmount = $rates['base_amount'] * $rate['percent'] / $rates['percent'];
81+
if ($rates['extension_attributes']) {
82+
$taxRates = $rates['extension_attributes']->getRates();
83+
/** @var \Magento\Tax\Api\Data\AppliedTaxRateInterface[] $taxRates */
84+
if (count($taxRates) == 1) {
10085
$ratesIdQuoteItemId[$rates['id']][] = [
10186
'id' => $taxesArray['item_id'],
102-
'percent' => $rate['percent'],
103-
'code' => $rate['code'],
87+
'percent' => $rates['percent'],
88+
'code' => $taxRates[0]['code'],
10489
'associated_item_id' => $taxesArray['associated_item_id'],
10590
'item_type' => $taxesArray['type'],
10691
'amount' => $rates['amount'],
10792
'base_amount' => $rates['base_amount'],
108-
'real_amount' => $realAmount,
109-
'real_base_amount' => $realBaseAmount,
93+
'real_amount' => $rates['amount'],
94+
'real_base_amount' => $rates['base_amount'],
11095
];
111-
$percentSum += $rate['percent'];
96+
} else {
97+
$percentSum = 0;
98+
foreach ($taxRates as $rate) {
99+
$realAmount = $rates['amount'] * $rate['percent'] / $rates['percent'];
100+
$realBaseAmount = $rates['base_amount'] * $rate['percent'] / $rates['percent'];
101+
$ratesIdQuoteItemId[$rates['id']][] = [
102+
'id' => $taxesArray['item_id'],
103+
'percent' => $rate['percent'],
104+
'code' => $rate['code'],
105+
'associated_item_id' => $taxesArray['associated_item_id'],
106+
'item_type' => $taxesArray['type'],
107+
'amount' => $rates['amount'],
108+
'base_amount' => $rates['base_amount'],
109+
'real_amount' => $realAmount,
110+
'real_base_amount' => $realBaseAmount,
111+
];
112+
$percentSum += $rate['percent'];
113+
}
112114
}
113115
}
114116
}
115117
}
116118

117119
foreach ($taxes as $row) {
118120
$id = $row['id'];
119-
$taxRates = $row['extension_attributes']->getRates();
120-
foreach ($taxRates as $tax) {
121-
if ($row['percent'] == null) {
122-
$baseRealAmount = $row['base_amount'];
123-
} else {
124-
if ($row['percent'] == 0 || $tax['percent'] == 0) {
125-
continue;
121+
if ($row['extension_attributes']) {
122+
/** @var \Magento\Tax\Api\Data\AppliedTaxRateInterface[] $taxRates */
123+
$taxRates = $row['extension_attributes']->getRates();
124+
foreach ($taxRates as $tax) {
125+
if ($row['percent'] == null) {
126+
$baseRealAmount = $row['base_amount'];
127+
} else {
128+
if ($row['percent'] == 0 || $tax['percent'] == 0) {
129+
continue;
130+
}
131+
$baseRealAmount = $row['base_amount'] / $row['percent'] * $tax['percent'];
126132
}
127-
$baseRealAmount = $row['base_amount'] / $row['percent'] * $tax['percent'];
128-
}
129-
$hidden = isset($row['hidden']) ? $row['hidden'] : 0;
130-
$priority = isset($tax['priority']) ? $tax['priority'] : 0;
131-
$position = isset($tax['position']) ? $tax['position'] : 0;
132-
$process = isset($row['process']) ? $row['process'] : 0;
133-
$data = [
134-
'order_id' => $order->getEntityId(),
135-
'code' => $tax['code'],
136-
'title' => $tax['title'],
137-
'hidden' => $hidden,
138-
'percent' => $tax['percent'],
139-
'priority' => $priority,
140-
'position' => $position,
141-
'amount' => $row['amount'],
142-
'base_amount' => $row['base_amount'],
143-
'process' => $process,
144-
'base_real_amount' => $baseRealAmount,
145-
];
133+
$hidden = isset($row['hidden']) ? $row['hidden'] : 0;
134+
$priority = isset($tax['priority']) ? $tax['priority'] : 0;
135+
$position = isset($tax['position']) ? $tax['position'] : 0;
136+
$process = isset($row['process']) ? $row['process'] : 0;
137+
$data = [
138+
'order_id' => $order->getEntityId(),
139+
'code' => $tax['code'],
140+
'title' => $tax['title'],
141+
'hidden' => $hidden,
142+
'percent' => $tax['percent'],
143+
'priority' => $priority,
144+
'position' => $position,
145+
'amount' => $row['amount'],
146+
'base_amount' => $row['base_amount'],
147+
'process' => $process,
148+
'base_real_amount' => $baseRealAmount,
149+
];
146150

147-
/** @var $orderTax \Magento\Tax\Model\Sales\Order\Tax */
148-
$orderTax = $this->orderTaxFactory->create();
149-
$result = $orderTax->setData($data)->save();
151+
/** @var $orderTax \Magento\Tax\Model\Sales\Order\Tax */
152+
$orderTax = $this->orderTaxFactory->create();
153+
$result = $orderTax->setData($data)->save();
150154

151-
if (isset($ratesIdQuoteItemId[$id])) {
152-
foreach ($ratesIdQuoteItemId[$id] as $quoteItemId) {
153-
if ($quoteItemId['code'] == $tax['code']) {
154-
$itemId = null;
155-
$associatedItemId = null;
156-
if (isset($quoteItemId['id'])) {
157-
//This is a product item
158-
$item = $order->getItemByQuoteItemId($quoteItemId['id']);
159-
$itemId = $item->getId();
160-
} elseif (isset($quoteItemId['associated_item_id'])) {
161-
//This item is associated with a product item
162-
$item = $order->getItemByQuoteItemId($quoteItemId['associated_item_id']);
163-
$associatedItemId = $item->getId();
164-
}
155+
if (isset($ratesIdQuoteItemId[$id])) {
156+
foreach ($ratesIdQuoteItemId[$id] as $quoteItemId) {
157+
if ($quoteItemId['code'] == $tax['code']) {
158+
$itemId = null;
159+
$associatedItemId = null;
160+
if (isset($quoteItemId['id'])) {
161+
//This is a product item
162+
$item = $order->getItemByQuoteItemId($quoteItemId['id']);
163+
$itemId = $item->getId();
164+
} elseif (isset($quoteItemId['associated_item_id'])) {
165+
//This item is associated with a product item
166+
$item = $order->getItemByQuoteItemId($quoteItemId['associated_item_id']);
167+
$associatedItemId = $item->getId();
168+
}
165169

166-
$data = [
167-
'item_id' => $itemId,
168-
'tax_id' => $result->getTaxId(),
169-
'tax_percent' => $quoteItemId['percent'],
170-
'associated_item_id' => $associatedItemId,
171-
'amount' => $quoteItemId['amount'],
172-
'base_amount' => $quoteItemId['base_amount'],
173-
'real_amount' => $quoteItemId['real_amount'],
174-
'real_base_amount' => $quoteItemId['real_base_amount'],
175-
'taxable_item_type' => $quoteItemId['item_type'],
176-
];
177-
/** @var $taxItem \Magento\Sales\Model\Order\Tax\Item */
178-
$taxItem = $this->taxItemFactory->create();
179-
$taxItem->setData($data)->save();
170+
$data = [
171+
'item_id' => $itemId,
172+
'tax_id' => $result->getTaxId(),
173+
'tax_percent' => $quoteItemId['percent'],
174+
'associated_item_id' => $associatedItemId,
175+
'amount' => $quoteItemId['amount'],
176+
'base_amount' => $quoteItemId['base_amount'],
177+
'real_amount' => $quoteItemId['real_amount'],
178+
'real_base_amount' => $quoteItemId['real_base_amount'],
179+
'taxable_item_type' => $quoteItemId['item_type'],
180+
];
181+
/** @var $taxItem \Magento\Sales\Model\Order\Tax\Item */
182+
$taxItem = $this->taxItemFactory->create();
183+
$taxItem->setData($data)->save();
184+
}
180185
}
181186
}
182187
}

app/code/Magento/Tax/Test/Unit/Model/Plugin/OrderSaveTest.php

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function verifyItemTaxes($expectedItemTaxes)
132132
$index = 0;
133133
$itemTaxes = [];
134134
foreach ($expectedItemTaxes as $itemTax) {
135-
$itemTaxMock = $this->getMockBuilder('\Magento\Sales\Model\Order\Tax\Item')
135+
$itemTaxMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Tax\Item::class)
136136
->disableOriginalConstructor()
137137
->setMethods(
138138
[
@@ -179,7 +179,7 @@ public function testAfterSave(
179179

180180
$orderItemMock = $this->getMockBuilder('\Magento\Sales\Model\Order\Item')
181181
->disableOriginalConstructor()
182-
->setMethods(['getId', ])
182+
->setMethods(['getId'])
183183
->getMock();
184184
$orderItemMock->expects($this->atLeastOnce())
185185
->method('getId')
@@ -235,7 +235,6 @@ public function afterSaveDataProvider()
235235
]
236236
]
237237
);
238-
239238
$orderTaxDetailsApplied->expects($this->at(1))
240239
->method('getRates')
241240
->willReturn(
@@ -247,8 +246,6 @@ public function afterSaveDataProvider()
247246
],
248247
]
249248
);
250-
251-
252249
$orderTaxDetailsApplied->expects($this->at(2))
253250
->method('getRates')
254251
->willReturn(
@@ -265,7 +262,6 @@ public function afterSaveDataProvider()
265262
],
266263
]
267264
);
268-
269265
$orderTaxDetailsApplied->expects($this->at(3))
270266
->method('getRates')
271267
->willReturn(
@@ -277,7 +273,6 @@ public function afterSaveDataProvider()
277273
],
278274
]
279275
);
280-
281276
$orderTaxDetailsApplied->expects($this->at(4))
282277
->method('getRates')
283278
->willReturn(
@@ -294,9 +289,6 @@ public function afterSaveDataProvider()
294289
],
295290
]
296291
);
297-
298-
299-
300292
$orderTaxDetailsApplied->expects($this->at(5))
301293
->method('getRates')
302294
->willReturn(
@@ -338,22 +330,22 @@ public function afterSaveDataProvider()
338330
'type' => 'product',
339331
'associated_item_id' => null,
340332
'applied_taxes' => [
341-
[
342-
'amount' => 0.11,
343-
'base_amount' => 0.11,
344-
'percent' => 11,
345-
'id' => 'ILUS',
346-
'extension_attributes' => $orderTaxDetailsApplied,
347-
],
348-
//city tax
349-
[
350-
'amount' => 0.03,
351-
'base_amount' => 0.03,
352-
'percent' => 3.33,
353-
'id' => 'CityTax',
354-
'extension_attributes' => $orderTaxDetailsApplied,
355-
],
333+
[
334+
'amount' => 0.11,
335+
'base_amount' => 0.11,
336+
'percent' => 11,
337+
'id' => 'ILUS',
338+
'extension_attributes' => $orderTaxDetailsApplied,
356339
],
340+
//city tax
341+
[
342+
'amount' => 0.03,
343+
'base_amount' => 0.03,
344+
'percent' => 3.33,
345+
'id' => 'CityTax',
346+
'extension_attributes' => $orderTaxDetailsApplied,
347+
],
348+
],
357349
],
358350
//shipping tax
359351
[

app/code/Magento/Tax/Test/Unit/Model/Quote/ToOrderConverterTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,13 @@ public function afterConvertDataProvider()
265265
],
266266
]
267267
],
268-
269268
],
270269
'shipping' => [
271-
'item_id' => 146,
272-
'type' => 'shipping',
273-
'associated_item_id' => null,
274-
'applied_taxes' => [
275-
[
270+
'item_id' => 146,
271+
'type' => 'shipping',
272+
'associated_item_id' => null,
273+
'applied_taxes' => [
274+
[
276275
'amount' => 0.30,
277276
'item_id' => 146,
278277
'item_type' => 'shipping',
@@ -286,8 +285,8 @@ public function afterConvertDataProvider()
286285
]
287286
],
288287
],
289-
]
290-
],
288+
]
289+
],
291290
],
292291
],
293292
],

0 commit comments

Comments
 (0)