55 */
66declare (strict_types=1 );
77
8- namespace Magento \Bundle \Helper \ Catalog \ Product ;
8+ namespace Magento \Bundle \Pricing \ Price ;
99
10- use Magento \Catalog \Api \CategoryRepositoryInterface ;
11- use Magento \Catalog \Api \ProductRepositoryInterface ;
1210use 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 ;
1611use Magento \Customer \Api \GroupRepositoryInterface ;
17- use Magento \Customer \Model \Address \AbstractAddress ;
1812use Magento \Customer \Model \Session as CustomerSession ;
19- use Magento \Framework \App \Helper \AbstractHelper ;
20- use Magento \Framework \App \Helper \Context ;
2113use Magento \Framework \Exception \LocalizedException ;
22- use Magento \Framework \Pricing \ PriceCurrencyInterface ;
14+ use Magento \Framework \Exception \ NoSuchEntityException ;
2315use Magento \Store \Api \Data \StoreInterface ;
2416use Magento \Store \Model \Store ;
2517use Magento \Store \Model \StoreManagerInterface ;
3022use Magento \Tax \Api \TaxCalculationInterface ;
3123use Magento \Tax \Model \Config ;
3224
33- class Tax extends AbstractHelper
25+ /**
26+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+ * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
28+ */
29+ class TaxPrice
3430{
3531 /**
3632 * @var StoreManagerInterface
3733 */
38- protected $ storeManager ;
34+ private $ storeManager ;
3935
4036 /**
4137 * @var TaxClassKeyInterfaceFactory
4238 */
43- protected $ taxClassKeyFactory ;
39+ private $ taxClassKeyFactory ;
4440
4541 /**
4642 * @var Config
4743 */
48- protected $ taxConfig ;
44+ private $ taxConfig ;
4945
5046 /**
5147 * @var QuoteDetailsInterfaceFactory
5248 */
53- protected $ quoteDetailsFactory ;
49+ private $ quoteDetailsFactory ;
5450
5551 /**
5652 * @var QuoteDetailsItemInterfaceFactory
5753 */
58- protected $ quoteDetailsItemFactory ;
54+ private $ quoteDetailsItemFactory ;
5955
6056 /**
6157 * @var CustomerSession
6258 */
63- protected $ customerSession ;
59+ private $ customerSession ;
6460
6561 /**
6662 * @var TaxCalculationInterface
6763 */
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 ;
8465
8566 /**
8667 * @var GroupRepositoryInterface
8768 */
88- protected $ customerGroupRepository ;
69+ private $ customerGroupRepository ;
8970
9071 /**
91- * @var AddressInterfaceFactory
92- */
93- protected $ addressFactory ;
94-
95- /**
96- * @var RegionInterfaceFactory
97- */
98- protected $ regionFactory ;
99-
100- /**
101- * @param Context $context
10272 * @param StoreManagerInterface $storeManager
10373 * @param TaxClassKeyInterfaceFactory $taxClassKeyFactory
10474 * @param Config $taxConfig
10575 * @param QuoteDetailsInterfaceFactory $quoteDetailsFactory
10676 * @param QuoteDetailsItemInterfaceFactory $quoteDetailsItemFactory
10777 * @param TaxCalculationInterface $taxCalculationService
10878 * @param CustomerSession $customerSession
109- * @param PriceCurrencyInterface $priceCurrency
11079 * @param GroupRepositoryInterface $customerGroupRepository
111- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
11280 */
11381 public function __construct (
114- Context $ context ,
11582 StoreManagerInterface $ storeManager ,
11683 TaxClassKeyInterfaceFactory $ taxClassKeyFactory ,
11784 Config $ taxConfig ,
11885 QuoteDetailsInterfaceFactory $ quoteDetailsFactory ,
11986 QuoteDetailsItemInterfaceFactory $ quoteDetailsItemFactory ,
12087 TaxCalculationInterface $ taxCalculationService ,
12188 CustomerSession $ customerSession ,
122- PriceCurrencyInterface $ priceCurrency ,
12389 GroupRepositoryInterface $ customerGroupRepository
12490 ) {
12591 $ this ->storeManager = $ storeManager ;
@@ -129,9 +95,7 @@ public function __construct(
12995 $ this ->quoteDetailsItemFactory = $ quoteDetailsItemFactory ;
13096 $ this ->taxCalculationService = $ taxCalculationService ;
13197 $ this ->customerSession = $ customerSession ;
132- $ this ->priceCurrency = $ priceCurrency ;
13398 $ this ->customerGroupRepository = $ customerGroupRepository ;
134- parent ::__construct ($ context );
13599 }
136100
137101 /**
@@ -140,53 +104,36 @@ public function __construct(
140104 * @param Product $product
141105 * @param float $price
142106 * @param bool|null $includingTax
143- * @param AbstractAddress|null $shippingAddress
144- * @param AbstractAddress|null $billingAddress
145107 * @param int|null $ctc
146108 * @param Store|bool|int|string|null $store
147109 * @param bool|null $priceIncludesTax
148- * @param bool $roundPrice
149110 * @return float
150111 * @throws LocalizedException
151- * @throws \Magento\Framework\Exception\ NoSuchEntityException
112+ * @throws NoSuchEntityException
152113 */
153114 public function getTaxPrice (
154115 Product $ product ,
155116 float $ price ,
156117 bool $ includingTax = null ,
157- AbstractAddress $ shippingAddress = null ,
158- AbstractAddress $ billingAddress = null ,
159118 int $ ctc = null ,
160119 Store |bool |int |string $ store = null ,
161- bool $ priceIncludesTax = null ,
162- bool $ roundPrice = true
120+ bool $ priceIncludesTax = null
163121 ): float {
164122 if (!$ price ) {
165123 return $ price ;
166124 }
167125
168126 $ 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+
169133 if ($ priceIncludesTax === null ) {
170134 $ priceIncludesTax = $ this ->taxConfig ->priceIncludesTax ($ store );
171135 }
172136
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 ();
190137 $ taxClassKey ->setType (TaxClassKeyInterface::TYPE_ID )
191138 ->setValue ($ product ->getTaxClassId ());
192139
@@ -195,11 +142,9 @@ public function getTaxPrice(
195142 ->getTaxClassId ();
196143 }
197144
198- $ customerTaxClassKey = $ this ->taxClassKeyFactory ->create ();
199145 $ customerTaxClassKey ->setType (TaxClassKeyInterface::TYPE_ID )
200146 ->setValue ($ ctc );
201147
202- $ item = $ this ->quoteDetailsItemFactory ->create ();
203148 $ item ->setQuantity (1 )
204149 ->setCode ($ product ->getSku ())
205150 ->setShortDescription ($ product ->getShortDescription ())
@@ -208,16 +153,12 @@ public function getTaxPrice(
208153 ->setType ('product ' )
209154 ->setUnitPrice ($ price );
210155
211- $ quoteDetails = $ this ->quoteDetailsFactory ->create ();
212- $ quoteDetails ->setShippingAddress ($ shippingAddressDataObject )
213- ->setBillingAddress ($ billingAddressDataObject )
156+ $ quoteDetails
214157 ->setCustomerTaxClassKey ($ customerTaxClassKey )
215158 ->setItems ([$ item ])
216159 ->setCustomerId ($ this ->customerSession ->getCustomerId ());
217160
218- $ storeId = null ;
219- $ storeId = $ store ?->getId();
220- $ taxDetails = $ this ->taxCalculationService ->calculateTax ($ quoteDetails , $ storeId , $ roundPrice );
161+ $ taxDetails = $ this ->taxCalculationService ->calculateTax ($ quoteDetails , $ storeId );
221162 $ items = $ taxDetails ->getItems ();
222163 $ taxDetailsItem = array_shift ($ items );
223164
@@ -233,11 +174,7 @@ public function getTaxPrice(
233174 $ taxDetailsItem ->getPrice () : $ taxDetailsItem ->getPriceInclTax ();
234175 }
235176
236- if ($ roundPrice ) {
237- return $ this ->priceCurrency ->round ($ price );
238- } else {
239- return $ price ;
240- }
177+ return $ price ;
241178 }
242179
243180 /**
@@ -250,26 +187,4 @@ public function displayCartPricesBoth(StoreInterface $store = null): bool
250187 {
251188 return $ this ->taxConfig ->displayCartPricesBoth ($ store );
252189 }
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- }
275190}
0 commit comments