Validation.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. use Magento\Bundle\Model\Product\Price as ProductPrice;
  12. use Magento\Catalog\Model\Product\Type as ProductBundle;
  13. use Magento\Quote\Model\Quote;
  14. use Magento\Quote\Model\ResourceModel\Quote\Item as QuoteItem;
  15. use Magento\Sales\Api\Data\OrderItemInterface;
  16. use Magento\Sales\Model\AbstractModel;
  17. use Magento\Sales\Model\Order\Creditmemo\Item as CreditMemoItem;
  18. use Magento\Sales\Model\Order\Invoice\Item as InvoiceItem;
  19. /**
  20. * Class Validation
  21. *
  22. * @package Klarna\Core\Model\Fpt
  23. */
  24. class Validation
  25. {
  26. /**
  27. * Checking if we have a valid order fpt order item
  28. *
  29. * @param InvoiceItem|CreditMemoItem $item
  30. * @param AbstractModel|Quote $object
  31. * @return bool
  32. */
  33. public function isValidOrderItem($item, $object)
  34. {
  35. $orderItem = $item->getOrderItem();
  36. $parentItem = $orderItem->getParentItem() ? $object->getItemById($orderItem->getParentItemId()) : null;
  37. if (!$this->isValidParentOrderItem($parentItem)) {
  38. return false;
  39. }
  40. // Skip if a bundled product with price type dynamic
  41. if ($orderItem->getProductType() == ProductBundle::TYPE_BUNDLE &&
  42. $orderItem->getProduct()->getPriceType() == ProductPrice::PRICE_TYPE_DYNAMIC
  43. ) {
  44. return false;
  45. }
  46. // Skip if parent is a bundle product having price type dynamic
  47. if (null !== $parentItem && $orderItem->getProductType() == ProductBundle::TYPE_BUNDLE &&
  48. $orderItem->getProduct()->getPriceType() == ProductPrice::PRICE_TYPE_DYNAMIC
  49. ) {
  50. return false;
  51. }
  52. return true;
  53. }
  54. /**
  55. * Check if we have a valid parent order item
  56. *
  57. * @param OrderItemInterface|null $parentItem
  58. * @return bool
  59. */
  60. private function isValidParentOrderItem($parentItem)
  61. {
  62. // Skip if child product of a non bundle parent
  63. if (!empty($parentItem)
  64. && $parentItem->getProductType() != ProductBundle::TYPE_BUNDLE
  65. ) {
  66. return false;
  67. }
  68. // Skip if child product of a bundle parent and bundle product price type is fixed
  69. if (!empty($parentItem) && $parentItem->getProductType() == ProductBundle::TYPE_BUNDLE &&
  70. $parentItem->getProduct()->getPriceType() == ProductPrice::PRICE_TYPE_FIXED
  71. ) {
  72. return false;
  73. }
  74. return true;
  75. }
  76. /**
  77. * Checking if we have a valid fpt quote item
  78. *
  79. * @param QuoteItem $item
  80. * @param AbstractModel|Quote $object
  81. * @return bool
  82. */
  83. public function isValidQuoteItem($item, $object)
  84. {
  85. if ($item instanceof \Magento\Quote\Model\Quote\Item) {
  86. // Skip if bundle product with a dynamic price type
  87. if ($item->getProductType() == ProductBundle::TYPE_BUNDLE &&
  88. $item->getProduct()->getPriceType() == ProductPrice::PRICE_TYPE_DYNAMIC
  89. ) {
  90. return false;
  91. }
  92. // Get quantity multiplier for bundle products
  93. if (null !== $item->getParentItemId() && ($parentItem = $object->getItemById($item->getParentItemId()))) {
  94. // Skip if non bundle product or if bundled product with a fixed price type
  95. if ($parentItem->getProductType() != ProductBundle::TYPE_BUNDLE
  96. || $parentItem->getProduct()->getPriceType() == ProductPrice::PRICE_TYPE_FIXED
  97. ) {
  98. return false;
  99. }
  100. }
  101. }
  102. return true;
  103. }
  104. }