Coupon.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Model\ResourceModel;
  7. use Magento\Framework\Model\AbstractModel;
  8. /**
  9. * SalesRule Resource Coupon
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Coupon extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implements
  14. \Magento\SalesRule\Model\Spi\CouponResourceInterface
  15. {
  16. /**
  17. * Constructor adds unique fields
  18. *
  19. * @return void
  20. */
  21. protected function _construct()
  22. {
  23. $this->_init('salesrule_coupon', 'coupon_id');
  24. $this->addUniqueField(['field' => 'code', 'title' => __('Coupon with the same code')]);
  25. }
  26. /**
  27. * Perform actions before object save
  28. *
  29. * @param AbstractModel $object
  30. * @return $this
  31. */
  32. public function _beforeSave(AbstractModel $object)
  33. {
  34. if (!$object->getExpirationDate()) {
  35. $object->setExpirationDate(null);
  36. } elseif ($object->getExpirationDate() instanceof \DateTimeInterface) {
  37. $object->setExpirationDate(
  38. $object->getExpirationDate()->format('Y-m-d H:i:s')
  39. );
  40. }
  41. // maintain single primary coupon per rule
  42. $object->setIsPrimary($object->getIsPrimary() ? 1 : null);
  43. return parent::_beforeSave($object);
  44. }
  45. /**
  46. * Load primary coupon (is_primary = 1) for specified rule
  47. *
  48. *
  49. * @param \Magento\SalesRule\Model\Coupon $object
  50. * @param \Magento\SalesRule\Model\Rule|int $rule
  51. * @return bool
  52. */
  53. public function loadPrimaryByRule(\Magento\SalesRule\Model\Coupon $object, $rule)
  54. {
  55. $connection = $this->getConnection();
  56. if ($rule instanceof \Magento\SalesRule\Model\Rule) {
  57. $ruleId = $rule->getId();
  58. } else {
  59. $ruleId = (int)$rule;
  60. }
  61. $select = $connection->select()->from(
  62. $this->getMainTable()
  63. )->where(
  64. 'rule_id = :rule_id'
  65. )->where(
  66. 'is_primary = :is_primary'
  67. );
  68. $data = $connection->fetchRow($select, [':rule_id' => $ruleId, ':is_primary' => 1]);
  69. if (!$data) {
  70. return false;
  71. }
  72. $object->setData($data);
  73. $this->_afterLoad($object);
  74. return true;
  75. }
  76. /**
  77. * Check if code exists
  78. *
  79. * @param string $code
  80. * @return bool
  81. */
  82. public function exists($code)
  83. {
  84. $connection = $this->getConnection();
  85. $select = $connection->select();
  86. $select->from($this->getMainTable(), 'code');
  87. $select->where('code = :code');
  88. if ($connection->fetchOne($select, ['code' => $code]) === false) {
  89. return false;
  90. }
  91. return true;
  92. }
  93. /**
  94. * Update auto generated Specific Coupon if its rule changed
  95. *
  96. * @param \Magento\SalesRule\Model\Rule $rule
  97. * @return $this
  98. */
  99. public function updateSpecificCoupons(\Magento\SalesRule\Model\Rule $rule)
  100. {
  101. if (!$rule || !$rule->getId() || !$rule->hasDataChanges()) {
  102. return $this;
  103. }
  104. $updateArray = [];
  105. if ($rule->dataHasChangedFor('uses_per_coupon')) {
  106. $updateArray['usage_limit'] = $rule->getUsesPerCoupon();
  107. }
  108. if ($rule->dataHasChangedFor('uses_per_customer')) {
  109. $updateArray['usage_per_customer'] = $rule->getUsesPerCustomer();
  110. }
  111. $ruleNewDate = new \DateTime($rule->getToDate());
  112. $ruleOldDate = new \DateTime($rule->getOrigData('to_date'));
  113. if ($ruleNewDate != $ruleOldDate) {
  114. $updateArray['expiration_date'] = $rule->getToDate();
  115. }
  116. if (!empty($updateArray)) {
  117. $this->getConnection()->update(
  118. $this->getTable('salesrule_coupon'),
  119. $updateArray,
  120. ['rule_id = ?' => $rule->getId()]
  121. );
  122. }
  123. return $this;
  124. }
  125. }