Total Complexity | 89 |
Total Lines | 467 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like Invoice often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Invoice, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class Invoice |
||
45 | { |
||
46 | /** |
||
47 | * Index of added invoice items |
||
48 | * |
||
49 | * @var integer |
||
50 | */ |
||
51 | protected $iIndex = 1; |
||
52 | |||
53 | /** |
||
54 | * Invoice amount |
||
55 | * |
||
56 | * @var integer |
||
57 | */ |
||
58 | protected $dAmount = 0; |
||
59 | |||
60 | /** |
||
61 | * Vat rate for following entities which may not have the vat attached to it |
||
62 | * |
||
63 | * @var double |
||
64 | */ |
||
65 | protected $dTax = false; |
||
66 | |||
67 | /** |
||
68 | * PAYONE toolkit helper |
||
69 | * |
||
70 | * @var \Payone\Core\Helper\Toolkit |
||
71 | */ |
||
72 | protected $toolkitHelper; |
||
73 | |||
74 | /** |
||
75 | * PAYONE amasty helper |
||
76 | * |
||
77 | * @var \Payone\Core\Helper\AmastyGiftcard |
||
78 | */ |
||
79 | protected $amastyHelper; |
||
80 | |||
81 | /** |
||
82 | * @var \Magento\Framework\Pricing\PriceCurrencyInterface |
||
83 | */ |
||
84 | protected $priceCurrency; |
||
85 | |||
86 | /** |
||
87 | * Request object |
||
88 | * |
||
89 | * @var Base |
||
90 | */ |
||
91 | protected $oRequest; |
||
92 | |||
93 | /** |
||
94 | * Current store code |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $sStoreCode; |
||
99 | |||
100 | /** |
||
101 | * Determines if price has to be negated |
||
102 | * |
||
103 | * @var bool |
||
104 | */ |
||
105 | protected $blNegatePrice = false; |
||
106 | |||
107 | /** |
||
108 | * Determines if product category url has to be send |
||
109 | * |
||
110 | * @var bool |
||
111 | */ |
||
112 | protected $blSendCategoryUrl = false; |
||
113 | |||
114 | /** |
||
115 | * Constructor |
||
116 | * |
||
117 | * @param \Payone\Core\Helper\Toolkit $toolkitHelper |
||
118 | * @param \Payone\Core\Helper\AmastyGiftcard $amastyHelper |
||
119 | * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency |
||
120 | */ |
||
121 | public function __construct( |
||
122 | \Payone\Core\Helper\Toolkit $toolkitHelper, |
||
123 | \Payone\Core\Helper\AmastyGiftcard $amastyHelper, |
||
124 | \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency |
||
125 | ) { |
||
126 | $this->toolkitHelper = $toolkitHelper; |
||
127 | $this->amastyHelper = $amastyHelper; |
||
128 | $this->priceCurrency = $priceCurrency; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param bool $blNegatePrice |
||
133 | */ |
||
134 | public function setNegatePrice($blNegatePrice) |
||
135 | { |
||
136 | $this->blNegatePrice = $blNegatePrice; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param bool $blSendCategoryUrl |
||
141 | */ |
||
142 | public function setSendCategoryUrl($blSendCategoryUrl) |
||
143 | { |
||
144 | $this->blSendCategoryUrl = $blSendCategoryUrl; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Add parameters for a invoice position |
||
149 | * |
||
150 | * @param string $sId item identification |
||
151 | * @param double $dPrice item price |
||
152 | * @param string $sItemType item type |
||
153 | * @param int $iAmount item amount |
||
154 | * @param string $sDesc item description |
||
155 | * @param double $dVat item tax rate |
||
156 | * @param string $sCategoryUrl category url |
||
157 | * @return void |
||
158 | */ |
||
159 | protected function addInvoicePosition($sId, $dPrice, $sItemType, $iAmount, $sDesc, $dVat, $sCategoryUrl = false) |
||
160 | { |
||
161 | $iMultiplier = 1; |
||
162 | if ($this->blNegatePrice === true) { |
||
163 | $iMultiplier = -1; |
||
164 | } |
||
165 | $this->oRequest->addParameter('id['.$this->iIndex.']', $this->formatSku($sId)); // add invoice item id |
||
166 | $this->oRequest->addParameter('pr['.$this->iIndex.']', $this->toolkitHelper->formatNumber($dPrice) * 100 * $iMultiplier); // expected in smallest unit of currency |
||
167 | $this->oRequest->addParameter('it['.$this->iIndex.']', $sItemType); // add invoice item type |
||
168 | $this->oRequest->addParameter('no['.$this->iIndex.']', $iAmount); // add invoice item amount |
||
169 | $this->oRequest->addParameter('de['.$this->iIndex.']', $sDesc); // add invoice item description |
||
170 | $this->oRequest->addParameter('va['.$this->iIndex.']', $this->toolkitHelper->formatNumber($dVat * 100, 0)); // expected * 100 to also handle vats with decimals |
||
171 | if ($sCategoryUrl !== false) { |
||
172 | $this->oRequest->addParameter('add_paydata[category_path_'.$sId.']', $sCategoryUrl); // add category url of a product, needed for BNPL payment methods |
||
173 | } |
||
174 | $this->dAmount += $dPrice * $iAmount; // needed for return of the main method |
||
175 | $this->iIndex++; // increase index for next item |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Add invoicing data to the request and return the summed invoicing amount |
||
180 | * |
||
181 | * @param Base $oRequest Request object |
||
182 | * @param object $oOrder Order object |
||
183 | * @param array $aPositions Is given with non-complete captures or debits |
||
184 | * @param bool $blDebit Is the call coming from a debit request |
||
185 | * @param double $dShippingCosts Shipping costs - needed for Klarna start_session |
||
186 | * @return integer |
||
187 | */ |
||
188 | public function addProductInfo(Base $oRequest, $oOrder, $aPositions = false, $blDebit = false, $dShippingCosts = false) |
||
189 | { |
||
190 | $this->oRequest = $oRequest; // write request to property for manipulation of the object |
||
191 | $this->setStoreCode($oOrder->getStore()->getCode()); |
||
192 | if ($oOrder instanceof Order) { |
||
193 | $sInvoiceAppendix = $this->toolkitHelper->getInvoiceAppendix($oOrder); // get invoice appendix |
||
194 | if (!empty($sInvoiceAppendix)) { // invoice appendix existing? |
||
195 | $this->oRequest->addParameter('invoiceappendix', $sInvoiceAppendix); // add appendix to request |
||
196 | } |
||
197 | } |
||
198 | |||
199 | $iQtyInvoiced = 0; |
||
200 | foreach ($oOrder->getAllItems() as $oItem) { // add invoice items for all order items |
||
201 | if (($oOrder instanceof Order && $oItem->isDummy() === false) || ($oOrder instanceof Quote && $oItem->getParentItemId() === null)) { // prevent variant-products of adding 2 items |
||
202 | $this->addProductItem($oItem, $aPositions); // add product invoice params to request |
||
203 | } |
||
204 | $iQtyInvoiced += $oItem->getOrigData('qty_invoiced'); // get data pre-capture |
||
205 | } |
||
206 | |||
207 | $blFirstCapture = true; // Is first capture? |
||
208 | if ($iQtyInvoiced > 0) { |
||
209 | $blFirstCapture = false; |
||
210 | } |
||
211 | |||
212 | if ($aPositions === false || $blFirstCapture === true || $blDebit === true) { |
||
213 | $this->addShippingItem($oOrder, $aPositions, $blDebit, $dShippingCosts); // add shipping invoice params to request |
||
214 | $this->addGiftCardItem($oOrder); // add gift card invoice params to request |
||
215 | $this->addAmastyGiftcards($oOrder, $aPositions, $blDebit); // add amasty giftcard invoice params to request |
||
216 | } |
||
217 | $this->addDiscountItem($oOrder, $aPositions, $blDebit); // add discount invoice params to request |
||
218 | |||
219 | return $this->dAmount; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Add invoicing item for a product |
||
224 | * |
||
225 | * @param \Magento\Sales\Model\Order\Item $oItem |
||
226 | * @param array $aPositions |
||
227 | * @return void |
||
228 | */ |
||
229 | protected function addProductItem($oItem, $aPositions) |
||
230 | { |
||
231 | $sPositionKey = $oItem->getProductId().$oItem->getSku(); |
||
232 | if ($aPositions === false || array_key_exists($sPositionKey, $aPositions) !== false) { // full or single-invoice? |
||
233 | $dItemAmount = $oItem->getQtyOrdered(); // get ordered item amount |
||
234 | if ($oItem instanceof QuoteItem) { |
||
235 | $dItemAmount = $oItem->getQty(); |
||
236 | } |
||
237 | if ($aPositions !== false && array_key_exists($sPositionKey, $aPositions) !== false) { // product existing in single-invoice? |
||
238 | $dItemAmount = $aPositions[$sPositionKey]; // use amount from single-invoice |
||
239 | } |
||
240 | $iAmount = $this->convertItemAmount($dItemAmount); |
||
241 | $dPrice = $oItem->getBasePriceInclTax(); |
||
242 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
243 | $dPrice = $oItem->getPriceInclTax(); |
||
244 | } |
||
245 | |||
246 | $sCategoryUrl = false; |
||
247 | if ($this->blSendCategoryUrl === true) { |
||
248 | $oCategory = $this->getProductCategory($oItem); |
||
249 | if (!empty($oCategory) && !empty($oCategory->getUrl())) { |
||
250 | $sCategoryUrl = $oCategory->getUrl(); |
||
251 | } |
||
252 | } |
||
253 | |||
254 | $this->addInvoicePosition($oItem->getSku(), $dPrice, 'goods', $iAmount, $oItem->getName(), $oItem->getTaxPercent(), $sCategoryUrl); // add invoice params to request |
||
255 | if ($this->dTax === false) { // is dTax not set yet? |
||
256 | $this->dTax = $oItem->getTaxPercent(); // set the tax for following entities which dont have the vat attached to it |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Try to get a category from given order item |
||
263 | * |
||
264 | * @param \Magento\Sales\Model\Order\Item $oItem |
||
265 | * @return \Magento\Catalog\Model\Category|false |
||
266 | */ |
||
267 | protected function getProductCategory($oItem) |
||
268 | { |
||
269 | $oProduct = $oItem->getProduct(); |
||
270 | if ($oProduct) { |
||
271 | $oCategoryCollection = $oProduct->getCategoryCollection(); |
||
272 | if (count($oCategoryCollection) > 0) { |
||
273 | $oCategory = $oCategoryCollection->getFirstItem(); |
||
274 | if ($oCategory) { |
||
275 | return $oCategory; |
||
276 | } |
||
277 | } |
||
278 | } |
||
279 | return false; |
||
280 | } |
||
281 | |||
282 | protected function addGiftCardItem($oOrder) |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * return giftcard-amount based on magento version |
||
298 | * |
||
299 | * @param $aGiftCard |
||
300 | * @return |
||
301 | */ |
||
302 | private function getGiftCardAmount($aGiftCard) |
||
303 | { |
||
304 | // up to Magento 2.3.4 giftcard-amount is saved in 'authorized', again in 2.4 |
||
305 | if (array_key_exists('authorized', $aGiftCard)) { |
||
306 | return -$aGiftCard['authorized']; |
||
307 | } |
||
308 | // in Magento 2.3.5 the array has slightly changed, giftcard-amount is only saved in 'ba' |
||
309 | if (array_key_exists('ba', $aGiftCard)) { |
||
310 | return -$aGiftCard['ba']; |
||
311 | } |
||
312 | return 0; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Add invoicing item for shipping |
||
317 | * |
||
318 | * @param Order $oOrder |
||
319 | * @param array $aPositions |
||
320 | * @param bool $blDebit |
||
321 | * @param double $dShippingCosts |
||
322 | * @return void |
||
323 | */ |
||
324 | protected function addShippingItem($oOrder, $aPositions, $blDebit, $dShippingCosts = false) |
||
365 | } |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Add invoicing item for discounts |
||
370 | * |
||
371 | * @param Order $oOrder |
||
372 | * @param array $aPositions |
||
373 | * @param bool $blDebit |
||
374 | * @return void |
||
375 | */ |
||
376 | protected function addDiscountItem($oOrder, $aPositions, $blDebit) |
||
377 | { |
||
378 | // discount costs existing or given for partial captures/debit? |
||
379 | $dTransmitDiscount = $oOrder->getBaseDiscountAmount(); |
||
380 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
381 | $dTransmitDiscount = $oOrder->getDiscountAmount(); |
||
382 | } |
||
383 | |||
384 | if ($oOrder instanceof Quote) { |
||
385 | $dTransmitDiscount = $oOrder->getBaseSubtotal() - $oOrder->getBaseSubtotalWithDiscount(); |
||
386 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
387 | $dTransmitDiscount = $oOrder->getSubtotal() - $oOrder->getSubtotalWithDiscount(); |
||
388 | } |
||
389 | } |
||
390 | |||
391 | if ($dTransmitDiscount != 0 && ($aPositions === false || ($blDebit === false || array_key_exists('discount', $aPositions) !== false))) { |
||
392 | if ($aPositions !== false && array_key_exists('discount', $aPositions) !== false) { |
||
393 | $dTransmitDiscount = $aPositions['discount']; |
||
394 | } |
||
395 | $dDiscount = $this->toolkitHelper->formatNumber($dTransmitDiscount); // format discount |
||
396 | if ($aPositions === false && $this->amastyHelper->hasAmastyGiftcards($oOrder->getQuoteId(), $oOrder) === false) { |
||
397 | // The calculations broken down to single items of Magento2 are unprecise and the Payone API will send an error if |
||
398 | // the calculated positions don't match, so we compensate for rounding-problems here |
||
399 | $dTotal = $oOrder->getBaseGrandTotal(); |
||
400 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
401 | $dTotal = $oOrder->getGrandTotal(); |
||
402 | } |
||
403 | $dDiff = ($this->dAmount + $dTransmitDiscount - $dTotal); // calc rounding discrepancy |
||
404 | $dDiscount -= $dDiff; // subtract difference from discount |
||
405 | } |
||
406 | $sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'discount', 'payone_misc', $this->getStoreCode()); // get configured discount SKU |
||
407 | $sDesc = (string)__('Discount'); // default description |
||
408 | if ($oOrder->getCouponCode()) {// was a coupon code used? |
||
409 | $sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'voucher', 'payone_misc', $this->getStoreCode()); // get configured voucher SKU |
||
410 | $sDesc = (string)__('Coupon').' - '.$oOrder->getCouponCode(); // add counpon code to description |
||
411 | } |
||
412 | $this->addInvoicePosition($sDiscountSku, $dDiscount, 'voucher', 1, $sDesc, $this->dTax); // add invoice params to request |
||
413 | } |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * Adding amasty giftcards to request |
||
418 | * |
||
419 | * @param Order $oOrder |
||
420 | * @param array $aPositions |
||
421 | * @param bool $blDebit |
||
422 | * @return void |
||
423 | */ |
||
424 | protected function addAmastyGiftcards($oOrder, $aPositions, $blDebit) |
||
425 | { |
||
426 | $aGiftCards = $this->amastyHelper->getAmastyGiftCards($oOrder->getQuoteId(), $oOrder); |
||
427 | for ($i = 0; $i < count($aGiftCards); $i++) { |
||
428 | $aGiftCard = $aGiftCards[$i]; |
||
429 | $blIsLastGiftcard = false; |
||
430 | if ($i + 1 == count($aGiftCards)) { |
||
431 | $blIsLastGiftcard = true; |
||
432 | } |
||
433 | |||
434 | $dTransmitDiscount = $aGiftCard['base_gift_amount']; |
||
435 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
436 | $dTransmitDiscount = $aGiftCard['gift_amount']; |
||
437 | } |
||
438 | if ($dTransmitDiscount != 0 && ($aPositions === false || ($blDebit === false || array_key_exists('discount', $aPositions) !== false))) { |
||
439 | $dTransmitDiscount = $dTransmitDiscount * -1; |
||
440 | $dDiscount = $this->toolkitHelper->formatNumber($dTransmitDiscount); // format discount |
||
441 | if ($aPositions === false && $blIsLastGiftcard === true) { |
||
442 | // The calculations broken down to single items of Magento2 are unprecise and the Payone API will send an error if |
||
443 | // the calculated positions don't match, so we compensate for rounding-problems here |
||
444 | $dTotal = $oOrder->getBaseGrandTotal(); |
||
445 | if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') { |
||
446 | $dTotal = $oOrder->getGrandTotal(); |
||
447 | } |
||
448 | $dDiff = ($this->dAmount + $dTransmitDiscount - $dTotal); // calc rounding discrepancy |
||
449 | $dDiscount -= $dDiff; // subtract difference from discount |
||
450 | } |
||
451 | |||
452 | if ($dDiscount != 0) { |
||
453 | $sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'voucher', 'payone_misc', $this->getStoreCode()); // get configured voucher SKU |
||
454 | $sDesc = (string)__('Amasty Coupon'); |
||
455 | $this->addInvoicePosition($sDiscountSku, $dDiscount, 'voucher', 1, $sDesc, $this->dTax); // add invoice params to request |
||
456 | } |
||
457 | } |
||
458 | } |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * Check if item amount has decimal places |
||
463 | * Throw exception if given amount is no integer |
||
464 | * |
||
465 | * @param double $dItemAmount |
||
466 | * @throws \InvalidArgumentException |
||
467 | * @return int |
||
468 | */ |
||
469 | protected function convertItemAmount($dItemAmount) |
||
470 | { |
||
471 | if (fmod(floatval($dItemAmount), 1.0) > 0) { // input does not represent an integer |
||
472 | $sErrorMessage = "Unable to use floating point values for item amounts! Parameter was: "; |
||
473 | throw new \InvalidArgumentException($sErrorMessage . strval($dItemAmount), 1); |
||
474 | } else { // return the integer value |
||
475 | return intval($dItemAmount); |
||
476 | } |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Set store code |
||
481 | * |
||
482 | * @param $sStoreCode |
||
483 | * @return void |
||
484 | */ |
||
485 | protected function setStoreCode($sStoreCode) |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Returns store code |
||
492 | * |
||
493 | * @return string |
||
494 | */ |
||
495 | protected function getStoreCode() |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Returns formatted sku |
||
502 | * |
||
503 | * @param string $sSku |
||
504 | * @return string |
||
505 | */ |
||
506 | protected function formatSku($sSku) |
||
513 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths