5
5
*/
6
6
declare (strict_types=1 );
7
7
8
- namespace Magento \Bundle \Helper \ Catalog \ Product ;
8
+ namespace Magento \Bundle \Pricing \ Price ;
9
9
10
- use Magento \Catalog \Api \CategoryRepositoryInterface ;
11
- use Magento \Catalog \Api \ProductRepositoryInterface ;
12
10
use Magento \Catalog \Model \Product ;
13
- use Magento \Customer \Api \Data \AddressInterface ;
14
- use Magento \Customer \Api \Data \AddressInterfaceFactory ;
15
- use Magento \Customer \Api \Data \RegionInterfaceFactory ;
16
11
use Magento \Customer \Api \GroupRepositoryInterface ;
17
- use Magento \Customer \Model \Address \AbstractAddress ;
18
12
use Magento \Customer \Model \Session as CustomerSession ;
19
- use Magento \Framework \App \Helper \AbstractHelper ;
20
- use Magento \Framework \App \Helper \Context ;
21
13
use Magento \Framework \Exception \LocalizedException ;
22
- use Magento \Framework \Pricing \ PriceCurrencyInterface ;
14
+ use Magento \Framework \Exception \ NoSuchEntityException ;
23
15
use Magento \Store \Api \Data \StoreInterface ;
24
16
use Magento \Store \Model \Store ;
25
17
use Magento \Store \Model \StoreManagerInterface ;
30
22
use Magento \Tax \Api \TaxCalculationInterface ;
31
23
use Magento \Tax \Model \Config ;
32
24
33
- class Tax extends AbstractHelper
25
+ /**
26
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27
+ * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
28
+ */
29
+ class TaxPrice
34
30
{
35
31
/**
36
32
* @var StoreManagerInterface
37
33
*/
38
- protected $ storeManager ;
34
+ private $ storeManager ;
39
35
40
36
/**
41
37
* @var TaxClassKeyInterfaceFactory
42
38
*/
43
- protected $ taxClassKeyFactory ;
39
+ private $ taxClassKeyFactory ;
44
40
45
41
/**
46
42
* @var Config
47
43
*/
48
- protected $ taxConfig ;
44
+ private $ taxConfig ;
49
45
50
46
/**
51
47
* @var QuoteDetailsInterfaceFactory
52
48
*/
53
- protected $ quoteDetailsFactory ;
49
+ private $ quoteDetailsFactory ;
54
50
55
51
/**
56
52
* @var QuoteDetailsItemInterfaceFactory
57
53
*/
58
- protected $ quoteDetailsItemFactory ;
54
+ private $ quoteDetailsItemFactory ;
59
55
60
56
/**
61
57
* @var CustomerSession
62
58
*/
63
- protected $ customerSession ;
59
+ private $ customerSession ;
64
60
65
61
/**
66
62
* @var TaxCalculationInterface
67
63
*/
68
- protected $ taxCalculationService ;
69
-
70
- /**
71
- * @var PriceCurrencyInterface
72
- */
73
- protected $ priceCurrency ;
74
-
75
- /**
76
- * @var ProductRepositoryInterface
77
- */
78
- protected $ productRepository ;
79
-
80
- /**
81
- * @var CategoryRepositoryInterface
82
- */
83
- protected $ categoryRepository ;
64
+ private $ taxCalculationService ;
84
65
85
66
/**
86
67
* @var GroupRepositoryInterface
87
68
*/
88
- protected $ customerGroupRepository ;
69
+ private $ customerGroupRepository ;
89
70
90
71
/**
91
- * @var AddressInterfaceFactory
92
- */
93
- protected $ addressFactory ;
94
-
95
- /**
96
- * @var RegionInterfaceFactory
97
- */
98
- protected $ regionFactory ;
99
-
100
- /**
101
- * @param Context $context
102
72
* @param StoreManagerInterface $storeManager
103
73
* @param TaxClassKeyInterfaceFactory $taxClassKeyFactory
104
74
* @param Config $taxConfig
105
75
* @param QuoteDetailsInterfaceFactory $quoteDetailsFactory
106
76
* @param QuoteDetailsItemInterfaceFactory $quoteDetailsItemFactory
107
77
* @param TaxCalculationInterface $taxCalculationService
108
78
* @param CustomerSession $customerSession
109
- * @param PriceCurrencyInterface $priceCurrency
110
79
* @param GroupRepositoryInterface $customerGroupRepository
111
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
112
80
*/
113
81
public function __construct (
114
- Context $ context ,
115
82
StoreManagerInterface $ storeManager ,
116
83
TaxClassKeyInterfaceFactory $ taxClassKeyFactory ,
117
84
Config $ taxConfig ,
118
85
QuoteDetailsInterfaceFactory $ quoteDetailsFactory ,
119
86
QuoteDetailsItemInterfaceFactory $ quoteDetailsItemFactory ,
120
87
TaxCalculationInterface $ taxCalculationService ,
121
88
CustomerSession $ customerSession ,
122
- PriceCurrencyInterface $ priceCurrency ,
123
89
GroupRepositoryInterface $ customerGroupRepository
124
90
) {
125
91
$ this ->storeManager = $ storeManager ;
@@ -129,9 +95,7 @@ public function __construct(
129
95
$ this ->quoteDetailsItemFactory = $ quoteDetailsItemFactory ;
130
96
$ this ->taxCalculationService = $ taxCalculationService ;
131
97
$ this ->customerSession = $ customerSession ;
132
- $ this ->priceCurrency = $ priceCurrency ;
133
98
$ this ->customerGroupRepository = $ customerGroupRepository ;
134
- parent ::__construct ($ context );
135
99
}
136
100
137
101
/**
@@ -140,53 +104,36 @@ public function __construct(
140
104
* @param Product $product
141
105
* @param float $price
142
106
* @param bool|null $includingTax
143
- * @param AbstractAddress|null $shippingAddress
144
- * @param AbstractAddress|null $billingAddress
145
107
* @param int|null $ctc
146
108
* @param Store|bool|int|string|null $store
147
109
* @param bool|null $priceIncludesTax
148
- * @param bool $roundPrice
149
110
* @return float
150
111
* @throws LocalizedException
151
- * @throws \Magento\Framework\Exception\ NoSuchEntityException
112
+ * @throws NoSuchEntityException
152
113
*/
153
114
public function getTaxPrice (
154
115
Product $ product ,
155
116
float $ price ,
156
117
bool $ includingTax = null ,
157
- AbstractAddress $ shippingAddress = null ,
158
- AbstractAddress $ billingAddress = null ,
159
118
int $ ctc = null ,
160
119
Store |bool |int |string $ store = null ,
161
- bool $ priceIncludesTax = null ,
162
- bool $ roundPrice = true
120
+ bool $ priceIncludesTax = null
163
121
): float {
164
122
if (!$ price ) {
165
123
return $ price ;
166
124
}
167
125
168
126
$ store = $ this ->storeManager ->getStore ($ store );
127
+ $ storeId = $ store ?->getId();
128
+ $ taxClassKey = $ this ->taxClassKeyFactory ->create ();
129
+ $ customerTaxClassKey = $ this ->taxClassKeyFactory ->create ();
130
+ $ item = $ this ->quoteDetailsItemFactory ->create ();
131
+ $ quoteDetails = $ this ->quoteDetailsFactory ->create ();
132
+
169
133
if ($ priceIncludesTax === null ) {
170
134
$ priceIncludesTax = $ this ->taxConfig ->priceIncludesTax ($ store );
171
135
}
172
136
173
- $ shippingAddressDataObject = null ;
174
- if ($ shippingAddress === null ) {
175
- $ shippingAddressDataObject =
176
- $ this ->convertDefaultTaxAddress ($ this ->customerSession ->getDefaultTaxShippingAddress ());
177
- } elseif ($ shippingAddress instanceof AbstractAddress) {
178
- $ shippingAddressDataObject = $ shippingAddress ->getDataModel ();
179
- }
180
-
181
- $ billingAddressDataObject = null ;
182
- if ($ billingAddress === null ) {
183
- $ billingAddressDataObject =
184
- $ this ->convertDefaultTaxAddress ($ this ->customerSession ->getDefaultTaxBillingAddress ());
185
- } elseif ($ billingAddress instanceof AbstractAddress) {
186
- $ billingAddressDataObject = $ billingAddress ->getDataModel ();
187
- }
188
-
189
- $ taxClassKey = $ this ->taxClassKeyFactory ->create ();
190
137
$ taxClassKey ->setType (TaxClassKeyInterface::TYPE_ID )
191
138
->setValue ($ product ->getTaxClassId ());
192
139
@@ -195,11 +142,9 @@ public function getTaxPrice(
195
142
->getTaxClassId ();
196
143
}
197
144
198
- $ customerTaxClassKey = $ this ->taxClassKeyFactory ->create ();
199
145
$ customerTaxClassKey ->setType (TaxClassKeyInterface::TYPE_ID )
200
146
->setValue ($ ctc );
201
147
202
- $ item = $ this ->quoteDetailsItemFactory ->create ();
203
148
$ item ->setQuantity (1 )
204
149
->setCode ($ product ->getSku ())
205
150
->setShortDescription ($ product ->getShortDescription ())
@@ -208,16 +153,12 @@ public function getTaxPrice(
208
153
->setType ('product ' )
209
154
->setUnitPrice ($ price );
210
155
211
- $ quoteDetails = $ this ->quoteDetailsFactory ->create ();
212
- $ quoteDetails ->setShippingAddress ($ shippingAddressDataObject )
213
- ->setBillingAddress ($ billingAddressDataObject )
156
+ $ quoteDetails
214
157
->setCustomerTaxClassKey ($ customerTaxClassKey )
215
158
->setItems ([$ item ])
216
159
->setCustomerId ($ this ->customerSession ->getCustomerId ());
217
160
218
- $ storeId = null ;
219
- $ storeId = $ store ?->getId();
220
- $ taxDetails = $ this ->taxCalculationService ->calculateTax ($ quoteDetails , $ storeId , $ roundPrice );
161
+ $ taxDetails = $ this ->taxCalculationService ->calculateTax ($ quoteDetails , $ storeId );
221
162
$ items = $ taxDetails ->getItems ();
222
163
$ taxDetailsItem = array_shift ($ items );
223
164
@@ -233,11 +174,7 @@ public function getTaxPrice(
233
174
$ taxDetailsItem ->getPrice () : $ taxDetailsItem ->getPriceInclTax ();
234
175
}
235
176
236
- if ($ roundPrice ) {
237
- return $ this ->priceCurrency ->round ($ price );
238
- } else {
239
- return $ price ;
240
- }
177
+ return $ price ;
241
178
}
242
179
243
180
/**
@@ -250,26 +187,4 @@ public function displayCartPricesBoth(StoreInterface $store = null): bool
250
187
{
251
188
return $ this ->taxConfig ->displayCartPricesBoth ($ store );
252
189
}
253
-
254
- /**
255
- * Convert tax address array to address data object with country id and postcode
256
- *
257
- * @param array|null $taxAddress
258
- * @return AddressInterface|null
259
- */
260
- private function convertDefaultTaxAddress (array $ taxAddress = null )
261
- {
262
- if (empty ($ taxAddress )) {
263
- return null ;
264
- }
265
- /** @var AddressInterface $addressDataObject */
266
- $ addressDataObject = $ this ->addressFactory ->create ()
267
- ->setCountryId ($ taxAddress ['country_id ' ])
268
- ->setPostcode ($ taxAddress ['postcode ' ]);
269
-
270
- if (isset ($ taxAddress ['region_id ' ])) {
271
- $ addressDataObject ->setRegion ($ this ->regionFactory ->create ()->setRegionId ($ taxAddress ['region_id ' ]));
272
- }
273
- return $ addressDataObject ;
274
- }
275
190
}
0 commit comments