@@ -63,117 +63,128 @@ protected function saveOrderTax(\Magento\Sales\Api\Data\OrderInterface $order)
63
63
return ;
64
64
}
65
65
66
+ /** @var \Magento\Tax\Api\Data\OrderTaxDetailsAppliedTaxInterface[]|null $taxes */
66
67
$ taxes = $ extensionAttribute ->getAppliedTaxes ();
67
68
if ($ taxes == null ) {
68
69
$ taxes = [];
69
70
}
70
71
72
+ /** @var \Magento\Tax\Api\Data\OrderTaxDetailsItemInterface[]|null $taxesForItems */
71
73
$ taxesForItems = $ extensionAttribute ->getItemAppliedTaxes ();
72
74
if ($ taxesForItems == null ) {
73
75
$ taxesForItems = [];
74
76
}
75
77
76
78
$ ratesIdQuoteItemId = [];
77
79
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 ' ];
80
+ foreach ($ taxesArray ['applied_taxes ' ] as $ rates ) {
81
+ if (isset ($ rates ['extension_attributes ' ])) {
82
+ /** @var \Magento\Tax\Api\Data\AppliedTaxRateInterface[] $taxRates */
83
+ $ taxRates = $ rates ['extension_attributes ' ]->getRates ();
84
+ if (is_array ($ taxRates )) {
85
+ if (count ($ taxRates ) == 1 ) {
86
+ $ ratesIdQuoteItemId [$ rates ['id ' ]][] = [
87
+ 'id ' => $ taxesArray ['item_id ' ],
88
+ 'percent ' => $ rates ['percent ' ],
89
+ 'code ' => $ taxRates [0 ]['code ' ],
90
+ 'associated_item_id ' => $ taxesArray ['associated_item_id ' ],
91
+ 'item_type ' => $ taxesArray ['type ' ],
92
+ 'amount ' => $ rates ['amount ' ],
93
+ 'base_amount ' => $ rates ['base_amount ' ],
94
+ 'real_amount ' => $ rates ['amount ' ],
95
+ 'real_base_amount ' => $ rates ['base_amount ' ],
96
+ ];
97
+ } else {
98
+ $ percentSum = 0 ;
99
+ foreach ($ taxRates as $ rate ) {
100
+ $ realAmount = $ rates ['amount ' ] * $ rate ['percent ' ] / $ rates ['percent ' ];
101
+ $ realBaseAmount = $ rates ['base_amount ' ] * $ rate ['percent ' ] / $ rates ['percent ' ];
102
+ $ ratesIdQuoteItemId [$ rates ['id ' ]][] = [
103
+ 'id ' => $ taxesArray ['item_id ' ],
104
+ 'percent ' => $ rate ['percent ' ],
105
+ 'code ' => $ rate ['code ' ],
106
+ 'associated_item_id ' => $ taxesArray ['associated_item_id ' ],
107
+ 'item_type ' => $ taxesArray ['type ' ],
108
+ 'amount ' => $ rates ['amount ' ],
109
+ 'base_amount ' => $ rates ['base_amount ' ],
110
+ 'real_amount ' => $ realAmount ,
111
+ 'real_base_amount ' => $ realBaseAmount ,
112
+ ];
113
+ $ percentSum += $ rate ['percent ' ];
114
+ }
115
+ }
108
116
}
109
117
}
110
118
}
111
119
}
112
120
113
121
foreach ($ taxes as $ row ) {
114
122
$ id = $ row ['id ' ];
115
- // @todo: should be refactored as part of MAGETWO-53366
116
- if (isset ($ row ['rates ' ])) {
117
- foreach ($ row ['rates ' ] as $ tax ) {
118
- if ($ row ['percent ' ] == null ) {
119
- $ baseRealAmount = $ row ['base_amount ' ];
120
- } else {
121
- if ($ row ['percent ' ] == 0 || $ tax ['percent ' ] == 0 ) {
122
- continue ;
123
+ if (isset ($ row ['extension_attributes ' ])) {
124
+ /** @var \Magento\Tax\Api\Data\AppliedTaxRateInterface[] $taxRates */
125
+ $ taxRates = $ row ['extension_attributes ' ]->getRates ();
126
+ if (is_array ($ taxRates )) {
127
+ foreach ($ taxRates as $ tax ) {
128
+ if ($ row ['percent ' ] == null ) {
129
+ $ baseRealAmount = $ row ['base_amount ' ];
130
+ } else {
131
+ if ($ row ['percent ' ] == 0 || $ tax ['percent ' ] == 0 ) {
132
+ continue ;
133
+ }
134
+ $ baseRealAmount = $ row ['base_amount ' ] / $ row ['percent ' ] * $ tax ['percent ' ];
123
135
}
124
- $ baseRealAmount = $ row ['base_amount ' ] / $ row ['percent ' ] * $ tax ['percent ' ];
125
- }
126
- $ hidden = isset ($ row ['hidden ' ]) ? $ row ['hidden ' ] : 0 ;
127
- $ priority = isset ($ tax ['priority ' ]) ? $ tax ['priority ' ] : 0 ;
128
- $ position = isset ($ tax ['position ' ]) ? $ tax ['position ' ] : 0 ;
129
- $ process = isset ($ row ['process ' ]) ? $ row ['process ' ] : 0 ;
130
- $ data = [
131
- 'order_id ' => $ order ->getEntityId (),
132
- 'code ' => $ tax ['code ' ],
133
- 'title ' => $ tax ['title ' ],
134
- 'hidden ' => $ hidden ,
135
- 'percent ' => $ tax ['percent ' ],
136
- 'priority ' => $ priority ,
137
- 'position ' => $ position ,
138
- 'amount ' => $ row ['amount ' ],
139
- 'base_amount ' => $ row ['base_amount ' ],
140
- 'process ' => $ process ,
141
- 'base_real_amount ' => $ baseRealAmount ,
142
- ];
136
+ $ hidden = isset ($ row ['hidden ' ]) ? $ row ['hidden ' ] : 0 ;
137
+ $ priority = isset ($ tax ['priority ' ]) ? $ tax ['priority ' ] : 0 ;
138
+ $ position = isset ($ tax ['position ' ]) ? $ tax ['position ' ] : 0 ;
139
+ $ process = isset ($ row ['process ' ]) ? $ row ['process ' ] : 0 ;
140
+ $ data = [
141
+ 'order_id ' => $ order ->getEntityId (),
142
+ 'code ' => $ tax ['code ' ],
143
+ 'title ' => $ tax ['title ' ],
144
+ 'hidden ' => $ hidden ,
145
+ 'percent ' => $ tax ['percent ' ],
146
+ 'priority ' => $ priority ,
147
+ 'position ' => $ position ,
148
+ 'amount ' => $ row ['amount ' ],
149
+ 'base_amount ' => $ row ['base_amount ' ],
150
+ 'process ' => $ process ,
151
+ 'base_real_amount ' => $ baseRealAmount ,
152
+ ];
143
153
144
- /** @var $orderTax \Magento\Tax\Model\Sales\Order\Tax */
145
- $ orderTax = $ this ->orderTaxFactory ->create ();
146
- $ result = $ orderTax ->setData ($ data )->save ();
154
+ /** @var $orderTax \Magento\Tax\Model\Sales\Order\Tax */
155
+ $ orderTax = $ this ->orderTaxFactory ->create ();
156
+ $ result = $ orderTax ->setData ($ data )->save ();
147
157
148
- if (isset ($ ratesIdQuoteItemId [$ id ])) {
149
- foreach ($ ratesIdQuoteItemId [$ id ] as $ quoteItemId ) {
150
- if ($ quoteItemId ['code ' ] == $ tax ['code ' ]) {
151
- $ itemId = null ;
152
- $ associatedItemId = null ;
153
- if (isset ($ quoteItemId ['id ' ])) {
154
- //This is a product item
155
- $ item = $ order ->getItemByQuoteItemId ($ quoteItemId ['id ' ]);
156
- $ itemId = $ item ->getId ();
157
- } elseif (isset ($ quoteItemId ['associated_item_id ' ])) {
158
- //This item is associated with a product item
159
- $ item = $ order ->getItemByQuoteItemId ($ quoteItemId ['associated_item_id ' ]);
160
- $ associatedItemId = $ item ->getId ();
161
- }
158
+ if (isset ($ ratesIdQuoteItemId [$ id ])) {
159
+ foreach ($ ratesIdQuoteItemId [$ id ] as $ quoteItemId ) {
160
+ if ($ quoteItemId ['code ' ] == $ tax ['code ' ]) {
161
+ $ itemId = null ;
162
+ $ associatedItemId = null ;
163
+ if (isset ($ quoteItemId ['id ' ])) {
164
+ //This is a product item
165
+ $ item = $ order ->getItemByQuoteItemId ($ quoteItemId ['id ' ]);
166
+ $ itemId = $ item ->getId ();
167
+ } elseif (isset ($ quoteItemId ['associated_item_id ' ])) {
168
+ //This item is associated with a product item
169
+ $ item = $ order ->getItemByQuoteItemId ($ quoteItemId ['associated_item_id ' ]);
170
+ $ associatedItemId = $ item ->getId ();
171
+ }
162
172
163
- $ data = [
164
- 'item_id ' => $ itemId ,
165
- 'tax_id ' => $ result ->getTaxId (),
166
- 'tax_percent ' => $ quoteItemId ['percent ' ],
167
- 'associated_item_id ' => $ associatedItemId ,
168
- 'amount ' => $ quoteItemId ['amount ' ],
169
- 'base_amount ' => $ quoteItemId ['base_amount ' ],
170
- 'real_amount ' => $ quoteItemId ['real_amount ' ],
171
- 'real_base_amount ' => $ quoteItemId ['real_base_amount ' ],
172
- 'taxable_item_type ' => $ quoteItemId ['item_type ' ],
173
- ];
174
- /** @var $taxItem \Magento\Sales\Model\Order\Tax\Item */
175
- $ taxItem = $ this ->taxItemFactory ->create ();
176
- $ taxItem ->setData ($ data )->save ();
173
+ $ data = [
174
+ 'item_id ' => $ itemId ,
175
+ 'tax_id ' => $ result ->getTaxId (),
176
+ 'tax_percent ' => $ quoteItemId ['percent ' ],
177
+ 'associated_item_id ' => $ associatedItemId ,
178
+ 'amount ' => $ quoteItemId ['amount ' ],
179
+ 'base_amount ' => $ quoteItemId ['base_amount ' ],
180
+ 'real_amount ' => $ quoteItemId ['real_amount ' ],
181
+ 'real_base_amount ' => $ quoteItemId ['real_base_amount ' ],
182
+ 'taxable_item_type ' => $ quoteItemId ['item_type ' ],
183
+ ];
184
+ /** @var $taxItem \Magento\Sales\Model\Order\Tax\Item */
185
+ $ taxItem = $ this ->taxItemFactory ->create ();
186
+ $ taxItem ->setData ($ data )->save ();
187
+ }
177
188
}
178
189
}
179
190
}
0 commit comments