GiftWrapping.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * This file is part of the Klarna Core module
  4. *
  5. * (c) Klarna Bank AB (publ)
  6. *
  7. * For the full copyright and license information, please view the NOTICE
  8. * and LICENSE files that were distributed with this source code.
  9. */
  10. namespace Klarna\Core\Helper;
  11. use Magento\Framework\App\Helper\AbstractHelper;
  12. use Magento\Framework\App\Helper\Context;
  13. use Magento\Store\Model\ScopeInterface;
  14. use Magento\Tax\Model\Calculation;
  15. use Magento\Store\Api\Data\StoreInterface;
  16. use Klarna\Core\Helper\DataConverter;
  17. use Magento\Quote\Model\Quote;
  18. use Magento\Sales\Model\Order;
  19. use Magento\Framework\DataObject;
  20. /**
  21. * Class GiftWrapping
  22. *
  23. * @package Klarna\Core\Helper
  24. */
  25. class GiftWrapping extends AbstractHelper
  26. {
  27. /**
  28. * @var Calculation
  29. */
  30. private $calculator;
  31. /**
  32. * @var DataConverter
  33. */
  34. private $dataConverter;
  35. /**
  36. * Gift wrapping tax class
  37. */
  38. const XML_PATH_TAX_CLASS_GW = 'tax/classes/wrapping_tax_class';
  39. /**
  40. * GiftWrapping Helper.
  41. *
  42. * @param Context $context
  43. * @param Calculation $calculator
  44. */
  45. public function __construct(
  46. Context $context,
  47. Calculation $calculator,
  48. DataConverter $dataConverter
  49. ) {
  50. parent::__construct($context);
  51. $this->calculator = $calculator;
  52. $this->dataConverter = $dataConverter;
  53. }
  54. /**
  55. * Calculate gift wrapping tax rate for an object
  56. *
  57. * @param Quote|Order $object
  58. * @param StoreInterface $store
  59. * @return float
  60. */
  61. public function getGiftWrappingTaxRate($object, StoreInterface $store)
  62. {
  63. //if address object doesn't contain valid data tax rate will be mis-calculated to 0
  64. //in this case use null instead
  65. $billingAddress = $this->isAddressValidForTaxCalculation($object->getBillingAddress())
  66. ? $object->getBillingAddress() : null;
  67. $shippingAddress = $this->isAddressValidForTaxCalculation($object->getBillingAddress())
  68. ? $object->getBillingAddress() : null;
  69. $request = $this->calculator->getRateRequest(
  70. $shippingAddress,
  71. $billingAddress,
  72. null,
  73. $store
  74. );
  75. $taxRateId = $this->scopeConfig->getValue(
  76. self::XML_PATH_TAX_CLASS_GW,
  77. ScopeInterface::SCOPE_STORES,
  78. $store
  79. );
  80. return $this->calculator->getRate($request->setProductClassId($taxRateId));
  81. }
  82. /**
  83. * check if address is ready to send for calculating tax
  84. *
  85. * @param \Magento\Sales\Api\Data\OrderAddressInterface|\Magento\Quote\Api\Data\AddressInterface $address
  86. * @return bool
  87. */
  88. private function isAddressValidForTaxCalculation($address)
  89. {
  90. return (bool)$address->getCountryId();
  91. }
  92. /**
  93. * calculate missing gift wrapping tax
  94. *
  95. * @param DataObject $checkout
  96. *
  97. * @param Quote $quote
  98. *
  99. * @return float|int
  100. */
  101. public function getAdditionalGwTax(DataObject $checkout, Quote $quote)
  102. {
  103. $klarnaTotal = (int)($checkout->getOrderAmount() ?: $checkout->getData('cart/total_price_including_tax'));
  104. $quoteTotal = (int)$this->dataConverter->toApiFloat($quote->getGrandTotal());
  105. if ($klarnaTotal > $quoteTotal) {
  106. $store = $quote->getStore();
  107. $taxRate = $this->getGiftWrappingTaxRate($quote, $store);
  108. if ($quote->getGwId()
  109. && $quote->getGwBasePrice() > 0
  110. && $quote->getGwBaseTaxAmount() == 0
  111. && $taxRate > 0) {
  112. $gwTotalAmount = $quote->getGwBasePrice() * ((100 + $taxRate) / 100);
  113. $taxAmount = $gwTotalAmount * ($taxRate / (100 + $taxRate));
  114. $taxAmount = (int)$this->dataConverter->toApiFloat($taxAmount);
  115. //additional validation to ensure only gift wrapping tax is missing from quote total
  116. if ($klarnaTotal == ($quoteTotal + $taxAmount)) {
  117. return $taxAmount;
  118. }
  119. }
  120. }
  121. return 0;
  122. }
  123. }