10 use Magento\Tax\Api\Data\OrderTaxDetailsAppliedTaxExtension;
45 \
Magento\Sales\Api\OrderRepositoryInterface $subject,
48 $this->saveOrderTax(
$order);
59 protected function saveOrderTax(\
Magento\Sales\Api\Data\OrderInterface
$order)
61 $extensionAttribute =
$order->getExtensionAttributes();
62 if (!$extensionAttribute ||
63 !$extensionAttribute->getConvertingFromQuote() ||
64 $order->getAppliedTaxIsSaved()) {
69 $taxes = $extensionAttribute->getAppliedTaxes();
75 $taxesForItems = $extensionAttribute->getItemAppliedTaxes();
76 if ($taxesForItems ==
null) {
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'],
102 foreach ($taxRates as
$rate) {
103 $percentSum +=
$rate[
'percent'];
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,
127 foreach ($taxes as
$row) {
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'];
138 if (
$row[
'percent'] == 0 ||
$tax[
'percent'] == 0) {
141 $baseRealAmount =
$row[
'base_amount'] /
$row[
'percent'] *
$tax[
'percent'];
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;
148 'order_id' =>
$order->getEntityId(),
149 'code' =>
$tax[
'code'],
150 'title' =>
$tax[
'title'],
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,
162 $orderTax = $this->orderTaxFactory->create();
165 if (isset($ratesIdQuoteItemId[
$id])) {
169 $associatedItemId =
null;
174 $itemId =
$item->getId();
179 $associatedItemId =
$item->getId();
183 'item_id' => $itemId,
184 'tax_id' =>
$result->getTaxId(),
186 'associated_item_id' => $associatedItemId,
194 $taxItem = $this->taxItemFactory->create();
195 $taxItem->setData(
$data)->save();
204 $order->setAppliedTaxIsSaved(
true);
elseif(isset( $params[ 'redirect_parent']))
__construct(\Magento\Tax\Model\Sales\Order\TaxFactory $orderTaxFactory, \Magento\Sales\Model\Order\Tax\ItemFactory $taxItemFactory)
afterSave(\Magento\Sales\Api\OrderRepositoryInterface $subject, \Magento\Sales\Api\Data\OrderInterface $order)