app/Plugin/CouponPro42/Entity/Coupon.php line 31

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the CouponPro42 Plugin
  4. *
  5. * Copyright (C) 2022 Diezon.
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Plugin\CouponPro42\Entity;
  11. use Eccube\Entity\AbstractEntity;
  12. use Eccube\Entity\Master\OrderStatus;
  13. use Plugin\CouponPro42\Entity\Master\CouponKind;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Doctrine\ORM\Mapping as ORM;
  16. /**
  17. * Coupon
  18. * plg_couponはECCUBE標準のクーポンプラグインで使用されているため、命名規則に応じてベンダー名を追加
  19. * https://doc4.ec-cube.net/plugin_naming_conventions
  20. *
  21. * @ORM\Table(name="plg_diezon_coupon")
  22. * @ORM\InheritanceType("SINGLE_TABLE")
  23. * @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
  24. * @ORM\HasLifecycleCallbacks()
  25. * @ORM\Entity(repositoryClass="Plugin\CouponPro42\Repository\CouponRepository")
  26. */
  27. class Coupon extends AbstractEntity
  28. {
  29. /**
  30. * @var int
  31. *
  32. * @ORM\Column(name="id", type="integer", options={"unsigned":true})
  33. * @ORM\Id
  34. * @ORM\GeneratedValue(strategy="IDENTITY")
  35. */
  36. private $id;
  37. /**
  38. * @var string
  39. *
  40. * @ORM\Column(name="coupon_code", type="string", length=255)
  41. */
  42. private $coupon_code;
  43. /**
  44. * @var string
  45. *
  46. * @ORM\Column(name="coupon_name", type="string", length=255)
  47. */
  48. private $coupon_name;
  49. /**
  50. * @var DateTime
  51. *
  52. * @ORM\Column(name="available_from", type="datetimetz")
  53. */
  54. private $available_from;
  55. /**
  56. * @var DateTime
  57. *
  58. * @ORM\Column(name="available_to", type="datetimetz")
  59. */
  60. private $available_to;
  61. /**
  62. * @var CouponKind
  63. *
  64. * @ORM\ManyToOne(targetEntity="Plugin\CouponPro42\Entity\Master\CouponKind")
  65. * @ORM\JoinColumns({
  66. * @ORM\JoinColumn(name="coupon_kind_id", referencedColumnName="id")
  67. * })
  68. */
  69. private $CouponKind;
  70. /**
  71. * @var string|null
  72. *
  73. * @ORM\Column(name="discount_price", type="decimal", precision=10, scale=0, options={"unsigned":true}, nullable=true)
  74. */
  75. private $discount_price;
  76. /**
  77. * @var string|null
  78. *
  79. * @ORM\Column(name="discount_rate", type="decimal", precision=10, scale=0, options={"unsigned":true}, nullable=true)
  80. */
  81. private $discount_rate;
  82. /**
  83. * @var string|null
  84. *
  85. * @ORM\Column(name="issued", type="decimal", precision=10, scale=0, options={"unsigned":true}, nullable=true)
  86. */
  87. private $issued;
  88. /**
  89. * @var boolean
  90. *
  91. * @ORM\Column(name="issued_unlimited", type="boolean", options={"default":false})
  92. */
  93. private $issued_unlimited;
  94. /**
  95. * @var boolean
  96. *
  97. * @ORM\Column(name="is_display_mypage", type="boolean", options={"default":false})
  98. */
  99. private $is_display_mypage = false;
  100. /**
  101. * @var boolean
  102. *
  103. * @ORM\Column(name="is_display_guest", type="boolean", options={"default":false})
  104. */
  105. private $is_display_guest = false;
  106. /**
  107. * @var string|null
  108. *
  109. * @ORM\Column(name="trans_url", type="string", length=255, nullable=true)
  110. */
  111. private $trans_url;
  112. /**
  113. * @var boolean
  114. *
  115. * @ORM\Column(name="first_time_limit", type="boolean", options={"default":false})
  116. */
  117. private $first_time_limit;
  118. /**
  119. * @var string|null
  120. *
  121. * @ORM\Column(name="min_price", type="decimal", precision=10, scale=0, options={"unsigned":true}, nullable=true)
  122. */
  123. private $min_price;
  124. /**
  125. * @var string|null
  126. *
  127. * @ORM\Column(name="customer_available", type="decimal", precision=10, scale=0, options={"unsigned":true}, nullable=true)
  128. */
  129. private $customer_available;
  130. /**
  131. * @var boolean
  132. *
  133. * @ORM\Column(name="enable", type="boolean", options={"default":false})
  134. */
  135. private $enable;
  136. /**
  137. * @var boolean
  138. *
  139. * @ORM\Column(name="deleted", type="boolean", options={"default":false})
  140. */
  141. private $deleted;
  142. /**
  143. * @var ArrayCollection
  144. *
  145. * @ORM\OneToMany(targetEntity="Plugin\CouponPro42\Entity\OrderCoupon", mappedBy="Coupon")
  146. */
  147. private $OrderCoupons;
  148. /**
  149. * @var ArrayCollection
  150. *
  151. * @ORM\OneToMany(targetEntity="Plugin\CouponPro42\Entity\CouponCategory", mappedBy="Coupon")
  152. */
  153. private $CouponCategories;
  154. /**
  155. * @var ArrayCollection
  156. *
  157. * @ORM\OneToMany(targetEntity="Plugin\CouponPro42\Entity\CouponCustomer", mappedBy="Coupon")
  158. */
  159. private $CouponCustomers;
  160. /**
  161. * @var ArrayCollection
  162. *
  163. * @ORM\OneToMany(targetEntity="Plugin\CouponPro42\Entity\CouponProduct", mappedBy="Coupon")
  164. */
  165. private $CouponProducts;
  166. /**
  167. * @var DateTime
  168. *
  169. * @ORM\Column(name="create_date", type="datetimetz")
  170. */
  171. private $create_date;
  172. /**
  173. * @var DateTime
  174. *
  175. * @ORM\Column(name="update_date", type="datetimetz")
  176. */
  177. private $update_date;
  178. public function __construct()
  179. {
  180. $this->OrderCoupons = new ArrayCollection();
  181. $this->CouponCategories = new ArrayCollection();
  182. $this->CouponCustomers = new ArrayCollection();
  183. $this->CouponProducts = new ArrayCollection();
  184. }
  185. /**
  186. * @return int|null
  187. */
  188. public function getId(): ?int
  189. {
  190. return $this->id;
  191. }
  192. /**
  193. * @param string $couponCode
  194. * @return $this
  195. */
  196. public function setCouponCode(string $couponCode): self
  197. {
  198. $this->coupon_code = $couponCode;
  199. return $this;
  200. }
  201. /**
  202. * @return string
  203. */
  204. public function getCouponCode(): string
  205. {
  206. return $this->coupon_code;
  207. }
  208. /**
  209. * @param string $couponName
  210. * @return $this
  211. */
  212. public function setCouponName(string $couponName): self
  213. {
  214. $this->coupon_name = $couponName;
  215. return $this;
  216. }
  217. /**
  218. * @return string
  219. */
  220. public function getCouponName(): string
  221. {
  222. return $this->coupon_name;
  223. }
  224. /**
  225. * @param CouponKind $CouponKind
  226. * @return $this
  227. */
  228. public function setCouponKind(CouponKind $CouponKind): self
  229. {
  230. $this->CouponKind = $CouponKind;
  231. return $this;
  232. }
  233. /**
  234. * @return CouponKind
  235. */
  236. public function getCouponKind(): CouponKind
  237. {
  238. return $this->CouponKind;
  239. }
  240. /**
  241. * @param string|null $discountPrice
  242. * @return $this
  243. */
  244. public function setDiscountPrice(?string $discountPrice): self
  245. {
  246. $this->discount_price = $discountPrice;
  247. return $this;
  248. }
  249. /**
  250. * @return string|null
  251. */
  252. public function getDiscountPrice(): ?string
  253. {
  254. return $this->discount_price;
  255. }
  256. /**
  257. * @param string|null $discountRate
  258. * @return $this
  259. */
  260. public function setDiscountRate(?string $discountRate)
  261. {
  262. $this->discount_rate = $discountRate;
  263. return $this;
  264. }
  265. /**
  266. * @return string|null
  267. */
  268. public function getDiscountRate(): ?string
  269. {
  270. return $this->discount_rate;
  271. }
  272. /**
  273. * @param string|null $issued
  274. * @return $this
  275. */
  276. public function setIssued(?string $issued): self
  277. {
  278. $this->issued = $issued;
  279. return $this;
  280. }
  281. /**
  282. * @return string|null
  283. */
  284. public function getIssued(): ?string
  285. {
  286. return $this->issued;
  287. }
  288. /**
  289. * @param bool|null $issuedUnlimited
  290. * @return $this
  291. */
  292. public function setIssuedUnlimited(?bool $issuedUnlimited = false): self
  293. {
  294. $this->issued_unlimited = $issuedUnlimited;
  295. return $this;
  296. }
  297. /**
  298. * @return bool
  299. */
  300. public function isIssuedUnlimited(): bool
  301. {
  302. return $this->issued_unlimited;
  303. }
  304. /**
  305. * @param string|null $transUrl
  306. * @return $this
  307. */
  308. public function setTransUrl(?string $transUrl): self
  309. {
  310. $this->trans_url = $transUrl;
  311. return $this;
  312. }
  313. /**
  314. * @return string|null
  315. */
  316. public function getTransUrl(): ?string
  317. {
  318. return $this->trans_url;
  319. }
  320. /**
  321. * @param bool|null $firstTimeLimit
  322. * @return $this
  323. */
  324. public function setFirstTimeLimit(?bool $firstTimeLimit = false): self
  325. {
  326. $this->first_time_limit = $firstTimeLimit;
  327. return $this;
  328. }
  329. /**
  330. * @return bool
  331. */
  332. public function isFirstTimeLimit(): bool
  333. {
  334. return $this->first_time_limit;
  335. }
  336. /**
  337. * @param string|null $minPrice
  338. * @return $this
  339. */
  340. public function setMinPrice(?string $minPrice): self
  341. {
  342. $this->min_price = $minPrice;
  343. return $this;
  344. }
  345. /**
  346. * @return string|null
  347. */
  348. public function getMinPrice(): ?string
  349. {
  350. return $this->min_price;
  351. }
  352. /**
  353. * @param string|null $customerAvailable
  354. * @return $this
  355. */
  356. public function setCustomerAvailable(?string $customerAvailable): self
  357. {
  358. $this->customer_available = $customerAvailable;
  359. return $this;
  360. }
  361. /**
  362. * @return string|null
  363. */
  364. public function getCustomerAvailable(): ?string
  365. {
  366. return $this->customer_available;
  367. }
  368. /**
  369. * @param \DateTime|null $availableFrom
  370. * @return $this
  371. */
  372. public function setAvailableFrom(?\DateTime $availableFrom): self
  373. {
  374. $this->available_from = $availableFrom;
  375. return $this;
  376. }
  377. /**
  378. * @return \DateTime
  379. */
  380. public function getAvailableFrom()
  381. {
  382. return $this->available_from;
  383. }
  384. /**
  385. * @param \DateTime|null $availableTo
  386. * @return $this
  387. */
  388. public function setAvailableTo(?\DateTime $availableTo): self
  389. {
  390. $this->available_to = $availableTo;
  391. return $this;
  392. }
  393. /**
  394. * @return \DateTime
  395. */
  396. public function getAvailableTo()
  397. {
  398. return $this->available_to;
  399. }
  400. /**
  401. * @param bool|null $enable
  402. * @return $this
  403. */
  404. public function setEnable(?bool $enable = false)
  405. {
  406. $this->enable = $enable;
  407. return $this;
  408. }
  409. /**
  410. * @return bool
  411. */
  412. public function isEnable(): bool
  413. {
  414. return $this->enable;
  415. }
  416. /**
  417. * @param bool|null $deleted
  418. * @return $this
  419. */
  420. public function setDeleted(?bool $deleted = false): self
  421. {
  422. $this->deleted = $deleted;
  423. return $this;
  424. }
  425. /**
  426. * @return bool
  427. */
  428. public function isDeleted(): bool
  429. {
  430. return $this->deleted;
  431. }
  432. /**
  433. * @param \DateTime $createDate
  434. * @return $this
  435. */
  436. public function setCreateDate(?\DateTime $createDate): self
  437. {
  438. $this->create_date = $createDate;
  439. return $this;
  440. }
  441. /**
  442. * @return \DateTime
  443. */
  444. public function getCreateDate()
  445. {
  446. return $this->create_date;
  447. }
  448. /**
  449. * @param \DateTime $updateDate
  450. * @return $this
  451. */
  452. public function setUpdateDate(?\DateTime $updateDate): self
  453. {
  454. $this->update_date = $updateDate;
  455. return $this;
  456. }
  457. /**
  458. * @return \DateTime
  459. */
  460. public function getUpdateDate()
  461. {
  462. return $this->update_date;
  463. }
  464. /**
  465. * @param bool $isDisplayMypage
  466. * @return $this
  467. */
  468. public function setIsDisplayMypage(bool $isDisplayMypage): self
  469. {
  470. $this->is_display_mypage = $isDisplayMypage;
  471. return $this;
  472. }
  473. /**
  474. * @return boolean
  475. */
  476. public function isDisplayMypage()
  477. {
  478. return $this->is_display_mypage;
  479. }
  480. /**
  481. * @param bool|null $isDisplayGuest
  482. * @return $this
  483. */
  484. public function setIsDisplayGuest(?bool $isDisplayGuest = false): self
  485. {
  486. $this->is_display_guest = $isDisplayGuest;
  487. return $this;
  488. }
  489. /**
  490. * @return boolean
  491. */
  492. public function isDisplayGuest()
  493. {
  494. return $this->is_display_guest;
  495. }
  496. /**
  497. * @param OrderCoupon $OrderCoupon
  498. * @return $this
  499. */
  500. public function addOrderCoupon(OrderCoupon $OrderCoupon)
  501. {
  502. $this->OrderCoupons[] = $OrderCoupon;
  503. return $this;
  504. }
  505. /**
  506. * @param OrderCoupon $OrderCoupon
  507. * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  508. */
  509. public function removeOrderCoupon(OrderCoupon $OrderCoupon)
  510. {
  511. return $this->OrderCoupons->removeElement($OrderCoupon);
  512. }
  513. /**
  514. * @return ArrayCollection
  515. */
  516. public function getOrderCoupons()
  517. {
  518. return $this->OrderCoupons;
  519. }
  520. /**
  521. * @param CouponCategory $CouponCategory
  522. */
  523. public function addCouponCategory(CouponCategory $CouponCategory)
  524. {
  525. $this->CouponCategories[] = $CouponCategory;
  526. return $this;
  527. }
  528. /**
  529. * @param CouponCategory $CouponCategory
  530. * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  531. */
  532. public function removeCouponCategory(CouponCategory $CouponCategory)
  533. {
  534. return $this->CouponCategories->removeElement($CouponCategory);
  535. }
  536. /**
  537. * @return ArrayCollection
  538. */
  539. public function getCouponCategories()
  540. {
  541. return $this->CouponCategories;
  542. }
  543. /**
  544. * @param CouponCustomer $CouponCustomer
  545. */
  546. public function addCouponCustomer(CouponCustomer $CouponCustomer)
  547. {
  548. $this->CouponCustomers[] = $CouponCustomer;
  549. return $this;
  550. }
  551. /**
  552. * @param CouponCustomer $CouponCustomer
  553. * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  554. */
  555. public function removeCouponCustomer(CouponCustomer $CouponCustomer)
  556. {
  557. return $this->CouponCustomers->removeElement($CouponCustomer);
  558. }
  559. /**
  560. * @return ArrayCollection
  561. */
  562. public function getCouponCustomers()
  563. {
  564. return $this->CouponCustomers;
  565. }
  566. /**
  567. * @param CouponProduct $CouponProduct
  568. */
  569. public function addCouponProduct(CouponProduct $CouponProduct)
  570. {
  571. $this->CouponProducts[] = $CouponProduct;
  572. return $this;
  573. }
  574. /**
  575. * @param CouponProduct $CouponProduct
  576. * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
  577. */
  578. public function removeCouponProduct(CouponProduct $CouponProduct)
  579. {
  580. return $this->CouponProducts->removeElement($CouponProduct);
  581. }
  582. /**
  583. * Get CouponProducts.
  584. * @return ArrayCollection
  585. */
  586. public function getCouponProducts()
  587. {
  588. return $this->CouponProducts;
  589. }
  590. /**
  591. * @return string
  592. */
  593. public function getOrderCouponCount()
  594. {
  595. $OrderCoupons = $this->getOrderCoupons();
  596. $count = 0;
  597. foreach ($OrderCoupons as $OrderCoupon) {
  598. $OrderStatus = $OrderCoupon->getOrder()->getOrderStatus();
  599. if (!in_array($OrderStatus->getId(), [
  600. OrderStatus::PENDING,
  601. OrderStatus::PROCESSING,
  602. OrderStatus::CANCEL,
  603. ])) {
  604. $count++;
  605. }
  606. }
  607. return $count;
  608. }
  609. /**
  610. * @return string
  611. */
  612. public function getDiscountLabel()
  613. {
  614. if ($this->getCouponKind()->getId() == CouponKind::DISCOUNT_PRICE):
  615. $discount = '¥' . $this->getDiscountPrice();
  616. elseif ($this->getCouponKind()->getId() == CouponKind::DISCOUNT_RATE):
  617. $discount = $this->getDiscountRate() . '%';
  618. else:
  619. $discount = trans('common.coupon.free_shipping');
  620. endif;
  621. return $discount;
  622. }
  623. /**
  624. * @return array
  625. */
  626. public function getTargetProducts()
  627. {
  628. $products = [];
  629. foreach ($this->getCouponProducts() as $CouponProduct) {
  630. if ($CouponProduct->getProduct()->isEnable()) {
  631. $products[$CouponProduct->getProduct()->getId()] = $CouponProduct->getProduct()->getName();
  632. }
  633. }
  634. ksort($products);
  635. return $products;
  636. }
  637. /**
  638. * @return array
  639. */
  640. public function getTargetCategories()
  641. {
  642. $categories = [];
  643. foreach ($this->getCouponCategories() as $CouponCategory) {
  644. $categories[$CouponCategory->getCategory()->getId()] = $CouponCategory->getCategory()->getName();
  645. }
  646. ksort($categories);
  647. return $categories;
  648. }
  649. }