5
5
*/
6
6
namespace Magento \Sales \Model \Order \Creditmemo \Total ;
7
7
8
+ use Magento \Tax \Model \Config ;
9
+
8
10
/**
9
11
* Discount total calculator
10
12
*/
11
13
class Discount extends AbstractTotal
12
14
{
15
+ /**
16
+ * @var Config
17
+ */
18
+ private $ taxConfig ;
19
+
20
+ /**
21
+ * @param Config $taxConfig
22
+ * @param array $data
23
+ */
24
+ public function __construct (
25
+ Config $ taxConfig ,
26
+ array $ data = []
27
+ ) {
28
+ $ this ->taxConfig = $ taxConfig ;
29
+
30
+ parent ::__construct ($ data );
31
+ }
32
+
13
33
/**
14
34
* Collect discount
15
35
*
16
36
* @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
17
37
* @return $this
18
38
* @throws \Magento\Framework\Exception\LocalizedException
39
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
19
40
*/
20
41
public function collect (\Magento \Sales \Model \Order \Creditmemo $ creditmemo )
21
42
{
@@ -31,7 +52,7 @@ public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
31
52
* Calculate how much shipping discount should be applied
32
53
* basing on how much shipping should be refunded.
33
54
*/
34
- $ baseShippingAmount = $ this ->getBaseShippingAmount ($ creditmemo );
55
+ $ baseShippingAmount = $ this ->getBaseShippingAmount ($ creditmemo, $ order );
35
56
36
57
/**
37
58
* If credit memo's shipping amount is set and Order's shipping amount is 0,
@@ -43,10 +64,14 @@ public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
43
64
);
44
65
}
45
66
if ($ baseShippingAmount ) {
67
+ $ orderBaseShippingAmount = $ this ->isShippingInclTax ((int )$ order ->getStoreId ()) ?
68
+ $ order ->getBaseShippingInclTax () : $ order ->getBaseShippingAmount ();
69
+ $ orderShippingAmount = $ this ->isShippingInclTax ((int )$ order ->getStoreId ()) ?
70
+ $ order ->getShippingInclTax () : $ order ->getShippingAmount ();
46
71
$ baseShippingDiscount = $ baseShippingAmount *
47
72
$ order ->getBaseShippingDiscountAmount () /
48
- $ order -> getBaseShippingAmount () ;
49
- $ shippingDiscount = $ order -> getShippingAmount () * $ baseShippingDiscount / $ order -> getBaseShippingAmount () ;
73
+ $ orderBaseShippingAmount ;
74
+ $ shippingDiscount = $ orderShippingAmount * $ baseShippingDiscount / $ orderBaseShippingAmount ;
50
75
$ totalDiscountAmount = $ totalDiscountAmount + $ shippingDiscount ;
51
76
$ baseTotalDiscountAmount = $ baseTotalDiscountAmount + $ baseShippingDiscount ;
52
77
}
@@ -104,8 +129,20 @@ private function getBaseShippingAmount(\Magento\Sales\Model\Order\Creditmemo $cr
104
129
if (!$ baseShippingAmount ) {
105
130
$ baseShippingInclTax = (float )$ creditmemo ->getBaseShippingInclTax ();
106
131
$ baseShippingTaxAmount = (float )$ creditmemo ->getBaseShippingTaxAmount ();
107
- $ baseShippingAmount = $ baseShippingInclTax - $ baseShippingTaxAmount ;
132
+ $ baseShippingAmount = $ this ->isShippingInclTax ((int )$ creditmemo ->getStoreId ()) ?
133
+ $ baseShippingInclTax : $ baseShippingInclTax - $ baseShippingTaxAmount ;
108
134
}
109
135
return $ baseShippingAmount ;
110
136
}
137
+
138
+ /**
139
+ * Returns whether the user specified a shipping amount that already includes tax
140
+ *
141
+ * @param int $storeId
142
+ * @return bool
143
+ */
144
+ private function isShippingInclTax (int $ storeId ): bool
145
+ {
146
+ return (bool )$ this ->taxConfig ->displaySalesShippingInclTax ($ storeId );
147
+ }
111
148
}
0 commit comments