Utility.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Model;
  7. use Magento\Framework\Pricing\PriceCurrencyInterface;
  8. /**
  9. * Class Utility
  10. *
  11. * @package Magento\SalesRule\Model
  12. */
  13. class Utility
  14. {
  15. /**
  16. * @var array
  17. */
  18. protected $_roundingDeltas = [];
  19. /**
  20. * @var array
  21. */
  22. protected $_baseRoundingDeltas = [];
  23. /**
  24. * @var \Magento\SalesRule\Model\ResourceModel\Coupon\UsageFactory
  25. */
  26. protected $usageFactory;
  27. /**
  28. * @var \Magento\SalesRule\Model\CouponFactory
  29. */
  30. protected $couponFactory;
  31. /**
  32. * @var \Magento\SalesRule\Model\Rule\CustomerFactory
  33. */
  34. protected $customerFactory;
  35. /**
  36. * @var \Magento\Framework\DataObjectFactory
  37. */
  38. protected $objectFactory;
  39. /**
  40. * @var PriceCurrencyInterface
  41. */
  42. protected $priceCurrency;
  43. /**
  44. * @param \Magento\SalesRule\Model\ResourceModel\Coupon\UsageFactory $usageFactory
  45. * @param CouponFactory $couponFactory
  46. * @param Rule\CustomerFactory $customerFactory
  47. * @param \Magento\Framework\DataObjectFactory $objectFactory
  48. * @param PriceCurrencyInterface $priceCurrency
  49. */
  50. public function __construct(
  51. \Magento\SalesRule\Model\ResourceModel\Coupon\UsageFactory $usageFactory,
  52. \Magento\SalesRule\Model\CouponFactory $couponFactory,
  53. \Magento\SalesRule\Model\Rule\CustomerFactory $customerFactory,
  54. \Magento\Framework\DataObjectFactory $objectFactory,
  55. PriceCurrencyInterface $priceCurrency
  56. ) {
  57. $this->couponFactory = $couponFactory;
  58. $this->customerFactory = $customerFactory;
  59. $this->usageFactory = $usageFactory;
  60. $this->objectFactory = $objectFactory;
  61. $this->priceCurrency = $priceCurrency;
  62. }
  63. /**
  64. * Check if rule can be applied for specific address/quote/customer
  65. *
  66. * @param \Magento\SalesRule\Model\Rule $rule
  67. * @param \Magento\Quote\Model\Quote\Address $address
  68. * @return bool
  69. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  70. * @SuppressWarnings(PHPMD.NPathComplexity)
  71. */
  72. public function canProcessRule($rule, $address)
  73. {
  74. if ($rule->hasIsValidForAddress($address) && !$address->isObjectNew()) {
  75. return $rule->getIsValidForAddress($address);
  76. }
  77. /**
  78. * check per coupon usage limit
  79. */
  80. if ($rule->getCouponType() != \Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON) {
  81. $couponCode = $address->getQuote()->getCouponCode();
  82. if (strlen($couponCode)) {
  83. /** @var \Magento\SalesRule\Model\Coupon $coupon */
  84. $coupon = $this->couponFactory->create();
  85. $coupon->load($couponCode, 'code');
  86. if ($coupon->getId()) {
  87. // check entire usage limit
  88. if ($coupon->getUsageLimit() && $coupon->getTimesUsed() >= $coupon->getUsageLimit()) {
  89. $rule->setIsValidForAddress($address, false);
  90. return false;
  91. }
  92. // check per customer usage limit
  93. $customerId = $address->getQuote()->getCustomerId();
  94. if ($customerId && $coupon->getUsagePerCustomer()) {
  95. $couponUsage = $this->objectFactory->create();
  96. $this->usageFactory->create()->loadByCustomerCoupon(
  97. $couponUsage,
  98. $customerId,
  99. $coupon->getId()
  100. );
  101. if ($couponUsage->getCouponId() &&
  102. $couponUsage->getTimesUsed() >= $coupon->getUsagePerCustomer()
  103. ) {
  104. $rule->setIsValidForAddress($address, false);
  105. return false;
  106. }
  107. }
  108. }
  109. }
  110. }
  111. /**
  112. * check per rule usage limit
  113. */
  114. $ruleId = $rule->getId();
  115. if ($ruleId && $rule->getUsesPerCustomer()) {
  116. $customerId = $address->getQuote()->getCustomerId();
  117. /** @var \Magento\SalesRule\Model\Rule\Customer $ruleCustomer */
  118. $ruleCustomer = $this->customerFactory->create();
  119. $ruleCustomer->loadByCustomerRule($customerId, $ruleId);
  120. if ($ruleCustomer->getId()) {
  121. if ($ruleCustomer->getTimesUsed() >= $rule->getUsesPerCustomer()) {
  122. $rule->setIsValidForAddress($address, false);
  123. return false;
  124. }
  125. }
  126. }
  127. $rule->afterLoad();
  128. /**
  129. * quote does not meet rule's conditions
  130. */
  131. if (!$rule->validate($address)) {
  132. $rule->setIsValidForAddress($address, false);
  133. return false;
  134. }
  135. /**
  136. * passed all validations, remember to be valid
  137. */
  138. $rule->setIsValidForAddress($address, true);
  139. return true;
  140. }
  141. /**
  142. * @param \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData
  143. * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
  144. * @param float $qty
  145. * @return void
  146. */
  147. public function minFix(
  148. \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData,
  149. \Magento\Quote\Model\Quote\Item\AbstractItem $item,
  150. $qty
  151. ) {
  152. $itemPrice = $this->getItemPrice($item);
  153. $baseItemPrice = $this->getItemBasePrice($item);
  154. $itemDiscountAmount = $item->getDiscountAmount();
  155. $itemBaseDiscountAmount = $item->getBaseDiscountAmount();
  156. $discountAmount = min($itemDiscountAmount + $discountData->getAmount(), $itemPrice * $qty);
  157. $baseDiscountAmount = min($itemBaseDiscountAmount + $discountData->getBaseAmount(), $baseItemPrice * $qty);
  158. $discountData->setAmount($discountAmount);
  159. $discountData->setBaseAmount($baseDiscountAmount);
  160. }
  161. /**
  162. * Process "delta" rounding
  163. *
  164. * @param \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData
  165. * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
  166. * @return $this
  167. */
  168. public function deltaRoundingFix(
  169. \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData,
  170. \Magento\Quote\Model\Quote\Item\AbstractItem $item
  171. ) {
  172. $discountAmount = $discountData->getAmount();
  173. $baseDiscountAmount = $discountData->getBaseAmount();
  174. $rowTotalInclTax = $item->getRowTotalInclTax();
  175. $baseRowTotalInclTax = $item->getBaseRowTotalInclTax();
  176. //TODO Seems \Magento\Quote\Model\Quote\Item\AbstractItem::getDiscountPercent() returns float value
  177. //that can not be used as array index
  178. $percentKey = $item->getDiscountPercent();
  179. if ($percentKey) {
  180. $delta = isset($this->_roundingDeltas[$percentKey]) ? $this->_roundingDeltas[$percentKey] : 0;
  181. $baseDelta = isset($this->_baseRoundingDeltas[$percentKey]) ? $this->_baseRoundingDeltas[$percentKey] : 0;
  182. $discountAmount += $delta;
  183. $baseDiscountAmount += $baseDelta;
  184. $this->_roundingDeltas[$percentKey] = $discountAmount - $this->priceCurrency->round($discountAmount);
  185. $this->_baseRoundingDeltas[$percentKey] = $baseDiscountAmount
  186. - $this->priceCurrency->round($baseDiscountAmount);
  187. }
  188. /**
  189. * When we have 100% discount check if totals will not be negative
  190. */
  191. if ($percentKey == 100) {
  192. $discountDelta = $rowTotalInclTax - $discountAmount;
  193. $baseDiscountDelta = $baseRowTotalInclTax - $baseDiscountAmount;
  194. if ($discountDelta < 0) {
  195. $discountAmount += $discountDelta;
  196. }
  197. if ($baseDiscountDelta < 0) {
  198. $baseDiscountAmount += $baseDiscountDelta;
  199. }
  200. }
  201. $discountData->setAmount($this->priceCurrency->round($discountAmount));
  202. $discountData->setBaseAmount($this->priceCurrency->round($baseDiscountAmount));
  203. return $this;
  204. }
  205. /**
  206. * Return item price
  207. *
  208. * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
  209. * @return float
  210. */
  211. public function getItemPrice($item)
  212. {
  213. $price = $item->getDiscountCalculationPrice();
  214. $calcPrice = $item->getCalculationPrice();
  215. return $price === null ? $calcPrice : $price;
  216. }
  217. /**
  218. * Return item base price
  219. *
  220. * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
  221. * @return float
  222. */
  223. public function getItemBasePrice($item)
  224. {
  225. $price = $item->getDiscountCalculationPrice();
  226. return $price !== null ? $item->getBaseDiscountCalculationPrice() : $item->getBaseCalculationPrice();
  227. }
  228. /**
  229. * Return discount item qty
  230. *
  231. * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
  232. * @param \Magento\SalesRule\Model\Rule $rule
  233. * @return int
  234. */
  235. public function getItemQty($item, $rule)
  236. {
  237. $qty = $item->getTotalQty();
  238. $discountQty = $rule->getDiscountQty();
  239. return $discountQty ? min($qty, $discountQty) : $qty;
  240. }
  241. /**
  242. * Merge two sets of ids
  243. *
  244. * @param array|string $a1
  245. * @param array|string $a2
  246. * @param bool $asString
  247. * @return array|string
  248. */
  249. public function mergeIds($a1, $a2, $asString = true)
  250. {
  251. if (!is_array($a1)) {
  252. $a1 = empty($a1) ? [] : explode(',', $a1);
  253. }
  254. if (!is_array($a2)) {
  255. $a2 = empty($a2) ? [] : explode(',', $a2);
  256. }
  257. $a = array_unique(array_merge($a1, $a2));
  258. if ($asString) {
  259. $a = implode(',', $a);
  260. }
  261. return $a;
  262. }
  263. /**
  264. * @return void
  265. */
  266. public function resetRoundingDeltas()
  267. {
  268. $this->_roundingDeltas = [];
  269. $this->_baseRoundingDeltas = [];
  270. }
  271. }