Rate.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\Model\Fpt;
  11. /**
  12. * Class Rate
  13. * @package Klarna\Core\Model\Fpt
  14. */
  15. class Rate
  16. {
  17. /** @var Validation $validation */
  18. private $validation;
  19. /**
  20. * Rate Constructor
  21. *
  22. * @param Validation $validation
  23. */
  24. public function __construct(Validation $validation)
  25. {
  26. $this->validation = $validation;
  27. }
  28. /**
  29. * Get total FPT tax for all items on order/quote/invoice/creditmemo
  30. *
  31. * @param \Magento\Sales\Model\AbstractModel|\Magento\Quote\Model\Quote $object $object
  32. * @return array
  33. */
  34. public function getFptTax($object)
  35. {
  36. $totalTax = 0;
  37. $name = [];
  38. $reference = [];
  39. foreach ($object->getAllItems() as $item) {
  40. if (($item instanceof \Magento\Sales\Model\Order\Invoice\Item
  41. || $item instanceof \Magento\Sales\Model\Order\Creditmemo\Item)
  42. && !$this->validation->isValidOrderItem($item, $object)
  43. ) {
  44. continue;
  45. }
  46. if (!$this->validation->isValidQuoteItem($item, $object)) {
  47. continue;
  48. }
  49. $totalTax += $item->getWeeeTaxAppliedRowAmount();
  50. $weee = json_decode($item->getWeeeTaxApplied(), true);
  51. foreach ($weee as $tax) {
  52. $name[] = $tax['title'];
  53. $reference[] = $tax['title'];
  54. }
  55. }
  56. return [
  57. 'tax' => $totalTax,
  58. 'name' => array_unique($name),
  59. 'reference' => array_unique($reference)
  60. ];
  61. }
  62. }