src/AdminBundle/Entity/Voucher.php line 356

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. /**
  5. * Voucher
  6. */
  7. #[ORM\Table(name: 'ctlf_voucher')]
  8. #[ORM\Entity(repositoryClass: \AdminBundle\Repository\VoucherRepository::class)]
  9. class Voucher extends BaseEntity
  10. {
  11. const VOUCHER_STATUS_ENABLE = 0;
  12. const VOUCHER_STATUS_USED = 1;
  13. const VOUCHER_DISCOUNT_PERCENTAGE = 0;
  14. const VOUCHER_DISCOUNT_VALUE = 1;
  15. const VOUCHER_TYPE_GENERAL = 0;
  16. const VOUCHER_TYPE_MOBILE_APP = 1;
  17. const VOUCHER_TYPE_REFERRAL = 2;
  18. const VOUCHER_TYPE_EMPLOYEE = 3;
  19. public static $statusTypes = array(
  20. self::VOUCHER_STATUS_ENABLE => 'enable',
  21. self::VOUCHER_STATUS_USED => 'used',
  22. );
  23. public static $discountTypes = array(
  24. self::VOUCHER_DISCOUNT_PERCENTAGE => '%',
  25. self::VOUCHER_DISCOUNT_VALUE => '£',
  26. );
  27. public static $types = array(
  28. self::VOUCHER_TYPE_GENERAL => 'General',
  29. self::VOUCHER_TYPE_MOBILE_APP => 'Mobile App',
  30. self::VOUCHER_TYPE_EMPLOYEE => 'Employee',
  31. );
  32. /**
  33. * @var integer
  34. */
  35. #[ORM\Column(name: 'id', type: 'integer', nullable: false)]
  36. #[ORM\Id]
  37. #[ORM\GeneratedValue(strategy: 'IDENTITY')]
  38. protected $id;
  39. #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id')]
  40. #[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\User::class)]
  41. protected $user;
  42. /**
  43. * @var string
  44. */
  45. #[ORM\Column(name: 'voucher_code', type: 'string', length: 64, unique: true)]
  46. protected $voucherCode;
  47. /**
  48. * @var integer
  49. */
  50. #[ORM\Column(name: 'voucher_value', type: 'integer')]
  51. protected $voucherValue;
  52. /**
  53. * @var integer
  54. */
  55. #[ORM\Column(name: 'voucher_min_journey_value', type: 'integer', nullable: true)]
  56. protected $voucherMinJourneyValue;
  57. /**
  58. * @var string
  59. */
  60. #[ORM\Column(name: 'voucher_discount_type', type: 'string', length: 2)]
  61. protected $voucherDiscountType;
  62. /**
  63. * @var \DateTime
  64. */
  65. #[ORM\Column(name: 'voucher_validity', type: 'datetime')]
  66. protected $voucherValidity;
  67. /**
  68. * @var boolean
  69. */
  70. #[ORM\Column(name: 'multiple_used', type: 'boolean')]
  71. protected $multipleUsed;
  72. /**
  73. * @var integer
  74. */
  75. #[ORM\Column(name: 'voucher_max_use', type: 'integer', nullable: true)]
  76. protected $maxUse;
  77. /**
  78. * @var integer
  79. */
  80. #[ORM\Column(name: 'voucher_max_use_per_user', type: 'integer', nullable: true)]
  81. protected $maxUsePerUser;
  82. /**
  83. * @var integer
  84. */
  85. #[ORM\Column(name: 'voucher_status', type: 'integer')]
  86. protected $status = self::VOUCHER_STATUS_ENABLE;
  87. /**
  88. * @var integer
  89. */
  90. #[ORM\Column(name: 'type', type: 'integer', options: ['default' => 0])]
  91. protected $type = self::VOUCHER_TYPE_GENERAL;
  92. /**
  93. * @var string
  94. */
  95. #[ORM\Column(name: 'details', type: 'text', nullable: true)]
  96. protected $details;
  97. /**
  98. * @var string
  99. */
  100. #[ORM\Column(name: 'employer_type', type: 'integer', length: 255, nullable: true)]
  101. protected $employerType;
  102. /**
  103. * @return int
  104. */
  105. public function getId()
  106. {
  107. return $this->id;
  108. }
  109. /**
  110. * @param int $id
  111. */
  112. public function setId($id)
  113. {
  114. $this->id = $id;
  115. }
  116. /**
  117. * @return string
  118. */
  119. public function getVoucherCode()
  120. {
  121. return $this->voucherCode;
  122. }
  123. /**
  124. * @param string $voucherCode
  125. *
  126. * @return Voucher
  127. */
  128. public function setVoucherCode($voucherCode)
  129. {
  130. $this->voucherCode = $voucherCode;
  131. return $this;
  132. }
  133. /**
  134. * @return int
  135. */
  136. public function getVoucherValue()
  137. {
  138. return $this->voucherValue;
  139. }
  140. /**
  141. * @param int $voucherValue
  142. *
  143. * @return Voucher
  144. */
  145. public function setVoucherValue($voucherValue)
  146. {
  147. $this->voucherValue = $voucherValue;
  148. return $this;
  149. }
  150. /**
  151. * @return int
  152. */
  153. public function getVoucherMinJourneyValue()
  154. {
  155. return $this->voucherMinJourneyValue;
  156. }
  157. /**
  158. * @param int $voucherMinJourneyValue
  159. *
  160. * @return Voucher
  161. */
  162. public function setVoucherMinJourneyValue($voucherMinJourneyValue)
  163. {
  164. $this->voucherMinJourneyValue = $voucherMinJourneyValue;
  165. return $this;
  166. }
  167. /**
  168. * @param string $currencySymbol
  169. * @return string
  170. */
  171. public function getVoucherDiscountTypeDisplay($currencySymbol = '£')
  172. {
  173. return $this->voucherDiscountType ? $currencySymbol : '%';
  174. }
  175. /**
  176. * @return string
  177. */
  178. public function getVoucherDiscountType()
  179. {
  180. return $this->voucherDiscountType;
  181. }
  182. /**
  183. * @param string $voucherDiscountType
  184. *
  185. * @return Voucher
  186. */
  187. public function setVoucherDiscountType($voucherDiscountType)
  188. {
  189. $this->voucherDiscountType = $voucherDiscountType;
  190. return $this;
  191. }
  192. /**
  193. * @return \DateTime
  194. */
  195. public function getVoucherValidity()
  196. {
  197. return $this->voucherValidity;
  198. }
  199. /**
  200. * @param \DateTime $voucherValidity
  201. *
  202. * @return Voucher
  203. */
  204. public function setVoucherValidity($voucherValidity)
  205. {
  206. $this->voucherValidity = $voucherValidity;
  207. return $this;
  208. }
  209. /**
  210. * @return bool
  211. */
  212. public function isMultipleUsed()
  213. {
  214. return $this->multipleUsed;
  215. }
  216. /**
  217. * @param bool $multipleUsed
  218. *
  219. * @return Voucher
  220. */
  221. public function setMultipleUsed($multipleUsed)
  222. {
  223. $this->multipleUsed = $multipleUsed;
  224. return $this;
  225. }
  226. /**
  227. * @return int
  228. */
  229. public function getStatus()
  230. {
  231. return $this->status;
  232. }
  233. /**
  234. * @param int $status
  235. *
  236. * @return Voucher
  237. */
  238. public function setStatus($status)
  239. {
  240. $this->status = $status;
  241. return $this;
  242. }
  243. /**
  244. * @return int
  245. */
  246. public function getMaxUse()
  247. {
  248. return $this->maxUse;
  249. }
  250. public function decrementMaxUse()
  251. {
  252. $this->maxUse--;
  253. if ($this->maxUse == 0) {
  254. $this->status = self::VOUCHER_STATUS_USED;
  255. }
  256. }
  257. /**
  258. * @param int $maxUse
  259. *
  260. * @return Voucher
  261. */
  262. public function setMaxUse($maxUse)
  263. {
  264. $this->maxUse = $maxUse;
  265. return $this;
  266. }
  267. public function __toString()
  268. {
  269. return $this->getVoucherCode() ? $this->getVoucherCode() : 'n\a';
  270. }
  271. /**
  272. * @return Voucher
  273. */
  274. public function getType()
  275. {
  276. return $this->type;
  277. }
  278. /**
  279. * @param int $type
  280. *
  281. * @return Voucher
  282. */
  283. public function setType($type)
  284. {
  285. $this->type = $type;
  286. return $this;
  287. }
  288. /**
  289. * @return User
  290. */
  291. public function getUser()
  292. {
  293. return $this->user;
  294. }
  295. /**
  296. * @param User $user
  297. *
  298. * return Voucher
  299. */
  300. public function setUser(User $user = null)
  301. {
  302. $this->user = $user;
  303. return $this;
  304. }
  305. /**
  306. * @return Voucher
  307. */
  308. public function getDetails()
  309. {
  310. return $this->details;
  311. }
  312. /**
  313. * @param string $details
  314. *
  315. * @return Voucher
  316. */
  317. public function setDetails($details)
  318. {
  319. $this->details = $details;
  320. return $this;
  321. }
  322. /**
  323. * @param int $employerType
  324. *
  325. * @return Voucher
  326. */
  327. public function setEmployerType($employerType)
  328. {
  329. $this->employerType = $employerType;
  330. return $this;
  331. }
  332. /**
  333. * @return string
  334. */
  335. public function getEmployerType()
  336. {
  337. return $this->employerType;
  338. }
  339. /**
  340. * @return int
  341. */
  342. public function getMaxUsePerUser()
  343. {
  344. return $this->maxUsePerUser;
  345. }
  346. public function decrementMaxUsePerUser()
  347. {
  348. $this->maxUsePerUser--;
  349. if ($this->maxUsePerUser === 0) {
  350. $this->status = self::VOUCHER_STATUS_USED;
  351. }
  352. }
  353. /**
  354. * @param int $maxUsePerUser
  355. *
  356. * @return Voucher
  357. */
  358. public function setMaxUsePerUser($maxUsePerUser)
  359. {
  360. $this->maxUsePerUser = $maxUsePerUser;
  361. return $this;
  362. }
  363. /**
  364. * @param float|null $price
  365. *
  366. * @return float
  367. */
  368. public function calculateDiscountValue(?float $price = 0): float
  369. {
  370. $val = 0;
  371. $value = $this->getVoucherValue();
  372. $type = $this->getVoucherDiscountType();
  373. if ($type == self::VOUCHER_DISCOUNT_VALUE) {
  374. $val = $value;
  375. } else if ($type == self::VOUCHER_DISCOUNT_PERCENTAGE) {
  376. $val = floatval($price) > floatval(0) ? $price * ($value / 100) : 0;
  377. }
  378. // Check if the discount value is greater than the price to avoid negative results.
  379. return floatval($val) >= floatval($price) ? $price : $val;
  380. // Old way, unused.
  381. switch ($this->getVoucherDiscountType()) {
  382. case self::VOUCHER_DISCOUNT_VALUE:
  383. return $this->getVoucherValue();
  384. case self::VOUCHER_DISCOUNT_PERCENTAGE:
  385. return floatval($price) > floatval(0)
  386. ? $price * ($this->getVoucherValue() / 100)
  387. : 0
  388. ;
  389. default:
  390. return 0;
  391. }
  392. }
  393. }