Coupon.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Model;
  7. /**
  8. * SalesRule Coupon Model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Coupon extends \Magento\Framework\Model\AbstractExtensibleModel implements
  14. \Magento\SalesRule\Api\Data\CouponInterface
  15. {
  16. const KEY_COUPON_ID = 'coupon_id';
  17. const KEY_RULE_ID = 'rule_id';
  18. const KEY_CODE = 'code';
  19. const KEY_USAGE_LIMIT = 'usage_limit';
  20. const KEY_USAGE_PER_CUSTOMER = 'usage_per_customer';
  21. const KEY_TIMES_USED = 'times_used';
  22. const KEY_EXPIRATION_DATE = 'expiration_date';
  23. const KEY_IS_PRIMARY = 'is_primary';
  24. const KEY_CREATED_AT = 'created_at';
  25. const KEY_TYPE = 'type';
  26. /**
  27. * Constructor
  28. *
  29. * @return void
  30. */
  31. protected function _construct()
  32. {
  33. parent::_construct();
  34. $this->_init(\Magento\SalesRule\Model\ResourceModel\Coupon::class);
  35. }
  36. /**
  37. * Set rule instance
  38. *
  39. * @param Rule $rule
  40. * @return $this
  41. */
  42. public function setRule(Rule $rule)
  43. {
  44. $this->setRuleId($rule->getId());
  45. return $this;
  46. }
  47. /**
  48. * Load primary coupon for specified rule
  49. *
  50. * @param Rule|int $rule
  51. * @return $this
  52. */
  53. public function loadPrimaryByRule($rule)
  54. {
  55. $this->getResource()->loadPrimaryByRule($this, $rule);
  56. return $this;
  57. }
  58. /**
  59. * Load Cart Price Rule by coupon code
  60. *
  61. * @param string $couponCode
  62. * @return $this
  63. */
  64. public function loadByCode($couponCode)
  65. {
  66. $this->load($couponCode, 'code');
  67. return $this;
  68. }
  69. //@codeCoverageIgnoreStart
  70. /**
  71. * Get coupon id
  72. *
  73. * @return int|null
  74. */
  75. public function getCouponId()
  76. {
  77. return $this->getData(self::KEY_COUPON_ID);
  78. }
  79. /**
  80. * Set coupon id
  81. *
  82. * @param int $couponId
  83. * @return $this
  84. */
  85. public function setCouponId($couponId)
  86. {
  87. return $this->setData(self::KEY_COUPON_ID, $couponId);
  88. }
  89. /**
  90. * Get the id of the rule associated with the coupon
  91. *
  92. * @return int
  93. */
  94. public function getRuleId()
  95. {
  96. return $this->getData(self::KEY_RULE_ID);
  97. }
  98. /**
  99. * Set rule id
  100. *
  101. * @param int $ruleId
  102. * @return $this
  103. */
  104. public function setRuleId($ruleId)
  105. {
  106. return $this->setData(self::KEY_RULE_ID, $ruleId);
  107. }
  108. /**
  109. * Get coupon code
  110. *
  111. * @return string|null
  112. */
  113. public function getCode()
  114. {
  115. return $this->getData(self::KEY_CODE);
  116. }
  117. /**
  118. * Set coupon code
  119. *
  120. * @param string $code
  121. * @return $this
  122. */
  123. public function setCode($code)
  124. {
  125. return $this->setData(self::KEY_CODE, $code);
  126. }
  127. /**
  128. * Get usage limit
  129. *
  130. * @return int|null
  131. */
  132. public function getUsageLimit()
  133. {
  134. return $this->getData(self::KEY_USAGE_LIMIT);
  135. }
  136. /**
  137. * Set usage limit
  138. *
  139. * @param int $usageLimit
  140. * @return $this
  141. */
  142. public function setUsageLimit($usageLimit)
  143. {
  144. return $this->setData(self::KEY_USAGE_LIMIT, $usageLimit);
  145. }
  146. /**
  147. * Get usage limit per customer
  148. *
  149. * @return int|null
  150. */
  151. public function getUsagePerCustomer()
  152. {
  153. return $this->getData(self::KEY_USAGE_PER_CUSTOMER);
  154. }
  155. /**
  156. * Set usage limit per customer
  157. *
  158. * @param int $usagePerCustomer
  159. * @return $this
  160. */
  161. public function setUsagePerCustomer($usagePerCustomer)
  162. {
  163. return $this->setData(self::KEY_USAGE_PER_CUSTOMER, $usagePerCustomer);
  164. }
  165. /**
  166. * Get the number of times the coupon has been used
  167. *
  168. * @return int
  169. */
  170. public function getTimesUsed()
  171. {
  172. return $this->getData(self::KEY_TIMES_USED);
  173. }
  174. /**
  175. * Set Times Used
  176. *
  177. * @param int $timesUsed
  178. * @return $this
  179. */
  180. public function setTimesUsed($timesUsed)
  181. {
  182. return $this->setData(self::KEY_TIMES_USED, $timesUsed);
  183. }
  184. /**
  185. * Get expiration date
  186. *
  187. * @return string|null
  188. */
  189. public function getExpirationDate()
  190. {
  191. return $this->getData(self::KEY_EXPIRATION_DATE);
  192. }
  193. /**
  194. * Set expiration date
  195. *
  196. * @param string $expirationDate
  197. * @return $this
  198. */
  199. public function setExpirationDate($expirationDate)
  200. {
  201. return $this->setData(self::KEY_EXPIRATION_DATE, $expirationDate);
  202. }
  203. /**
  204. * Whether the coupon is primary coupon for the rule that it's associated with
  205. *
  206. * @return bool
  207. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  208. */
  209. public function getIsPrimary()
  210. {
  211. return $this->getData(self::KEY_IS_PRIMARY);
  212. }
  213. /**
  214. * Set whether the coupon is the primary coupon for the rule that it's associated with
  215. *
  216. * @param bool $isPrimary
  217. * @return $this
  218. */
  219. public function setIsPrimary($isPrimary)
  220. {
  221. return $this->setData(self::KEY_IS_PRIMARY, $isPrimary);
  222. }
  223. /**
  224. * Date when the coupon is created
  225. *
  226. * @return string|null
  227. */
  228. public function getCreatedAt()
  229. {
  230. return $this->getData(self::KEY_CREATED_AT);
  231. }
  232. /**
  233. * Set the date the coupon is created
  234. *
  235. * @param string $createdAt
  236. * @return $this
  237. */
  238. public function setCreatedAt($createdAt)
  239. {
  240. return $this->setData(self::KEY_CREATED_AT, $createdAt);
  241. }
  242. /**
  243. * Type of coupon
  244. *
  245. * @return int|null
  246. */
  247. public function getType()
  248. {
  249. return $this->getData(self::KEY_TYPE);
  250. }
  251. /**
  252. * Set type
  253. *
  254. * @param int $type
  255. * @return $this
  256. */
  257. public function setType($type)
  258. {
  259. return $this->setData(self::KEY_TYPE, $type);
  260. }
  261. /**
  262. * Retrieve existing extension attributes object or create a new one.
  263. *
  264. * @return \Magento\SalesRule\Api\Data\CouponExtensionInterface|null
  265. */
  266. public function getExtensionAttributes()
  267. {
  268. return $this->_getExtensionAttributes();
  269. }
  270. /**
  271. * Set an extension attributes object.
  272. *
  273. * @param \Magento\SalesRule\Api\Data\CouponExtensionInterface $extensionAttributes
  274. * @return $this
  275. */
  276. public function setExtensionAttributes(
  277. \Magento\SalesRule\Api\Data\CouponExtensionInterface $extensionAttributes
  278. ) {
  279. return $this->_setExtensionAttributes($extensionAttributes);
  280. }
  281. //@codeCoverageIgnoreEnd
  282. }