<?php
/*
* This file is part of the CouponPro42 Plugin
*
* Copyright (C) 2022 Diezon.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\CouponPro42\Entity;
use Eccube\Entity\AbstractEntity;
use Eccube\Entity\Master\OrderStatus;
use Plugin\CouponPro42\Entity\Master\CouponKind;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Coupon
* plg_couponはECCUBE標準のクーポンプラグインで使用されているため、命名規則に応じてベンダー名を追加
* https://doc4.ec-cube.net/plugin_naming_conventions
*
* @ORM\Table(name="plg_diezon_coupon")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
* @ORM\HasLifecycleCallbacks()
* @ORM\Entity(repositoryClass="Plugin\CouponPro42\Repository\CouponRepository")
*/
class Coupon extends AbstractEntity
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer", options={"unsigned":true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="coupon_code", type="string", length=255)
*/
private $coupon_code;
/**
* @var string
*
* @ORM\Column(name="coupon_name", type="string", length=255)
*/
private $coupon_name;
/**
* @var DateTime
*
* @ORM\Column(name="available_from", type="datetimetz")
*/
private $available_from;
/**
* @var DateTime
*
* @ORM\Column(name="available_to", type="datetimetz")
*/
private $available_to;
/**
* @var CouponKind
*
* @ORM\ManyToOne(targetEntity="Plugin\CouponPro42\Entity\Master\CouponKind")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="coupon_kind_id", referencedColumnName="id")
* })
*/
private $CouponKind;
/**
* @var string|null
*
* @ORM\Column(name="discount_price", type="decimal", precision=10, scale=0, options={"unsigned":true}, nullable=true)
*/
private $discount_price;
/**
* @var string|null
*
* @ORM\Column(name="discount_rate", type="decimal", precision=10, scale=0, options={"unsigned":true}, nullable=true)
*/
private $discount_rate;
/**
* @var string|null
*
* @ORM\Column(name="issued", type="decimal", precision=10, scale=0, options={"unsigned":true}, nullable=true)
*/
private $issued;
/**
* @var boolean
*
* @ORM\Column(name="issued_unlimited", type="boolean", options={"default":false})
*/
private $issued_unlimited;
/**
* @var boolean
*
* @ORM\Column(name="is_display_mypage", type="boolean", options={"default":false})
*/
private $is_display_mypage = false;
/**
* @var boolean
*
* @ORM\Column(name="is_display_guest", type="boolean", options={"default":false})
*/
private $is_display_guest = false;
/**
* @var string|null
*
* @ORM\Column(name="trans_url", type="string", length=255, nullable=true)
*/
private $trans_url;
/**
* @var boolean
*
* @ORM\Column(name="first_time_limit", type="boolean", options={"default":false})
*/
private $first_time_limit;
/**
* @var string|null
*
* @ORM\Column(name="min_price", type="decimal", precision=10, scale=0, options={"unsigned":true}, nullable=true)
*/
private $min_price;
/**
* @var string|null
*
* @ORM\Column(name="customer_available", type="decimal", precision=10, scale=0, options={"unsigned":true}, nullable=true)
*/
private $customer_available;
/**
* @var boolean
*
* @ORM\Column(name="enable", type="boolean", options={"default":false})
*/
private $enable;
/**
* @var boolean
*
* @ORM\Column(name="deleted", type="boolean", options={"default":false})
*/
private $deleted;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Plugin\CouponPro42\Entity\OrderCoupon", mappedBy="Coupon")
*/
private $OrderCoupons;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Plugin\CouponPro42\Entity\CouponCategory", mappedBy="Coupon")
*/
private $CouponCategories;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Plugin\CouponPro42\Entity\CouponCustomer", mappedBy="Coupon")
*/
private $CouponCustomers;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Plugin\CouponPro42\Entity\CouponProduct", mappedBy="Coupon")
*/
private $CouponProducts;
/**
* @var DateTime
*
* @ORM\Column(name="create_date", type="datetimetz")
*/
private $create_date;
/**
* @var DateTime
*
* @ORM\Column(name="update_date", type="datetimetz")
*/
private $update_date;
public function __construct()
{
$this->OrderCoupons = new ArrayCollection();
$this->CouponCategories = new ArrayCollection();
$this->CouponCustomers = new ArrayCollection();
$this->CouponProducts = new ArrayCollection();
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @param string $couponCode
* @return $this
*/
public function setCouponCode(string $couponCode): self
{
$this->coupon_code = $couponCode;
return $this;
}
/**
* @return string
*/
public function getCouponCode(): string
{
return $this->coupon_code;
}
/**
* @param string $couponName
* @return $this
*/
public function setCouponName(string $couponName): self
{
$this->coupon_name = $couponName;
return $this;
}
/**
* @return string
*/
public function getCouponName(): string
{
return $this->coupon_name;
}
/**
* @param CouponKind $CouponKind
* @return $this
*/
public function setCouponKind(CouponKind $CouponKind): self
{
$this->CouponKind = $CouponKind;
return $this;
}
/**
* @return CouponKind
*/
public function getCouponKind(): CouponKind
{
return $this->CouponKind;
}
/**
* @param string|null $discountPrice
* @return $this
*/
public function setDiscountPrice(?string $discountPrice): self
{
$this->discount_price = $discountPrice;
return $this;
}
/**
* @return string|null
*/
public function getDiscountPrice(): ?string
{
return $this->discount_price;
}
/**
* @param string|null $discountRate
* @return $this
*/
public function setDiscountRate(?string $discountRate)
{
$this->discount_rate = $discountRate;
return $this;
}
/**
* @return string|null
*/
public function getDiscountRate(): ?string
{
return $this->discount_rate;
}
/**
* @param string|null $issued
* @return $this
*/
public function setIssued(?string $issued): self
{
$this->issued = $issued;
return $this;
}
/**
* @return string|null
*/
public function getIssued(): ?string
{
return $this->issued;
}
/**
* @param bool|null $issuedUnlimited
* @return $this
*/
public function setIssuedUnlimited(?bool $issuedUnlimited = false): self
{
$this->issued_unlimited = $issuedUnlimited;
return $this;
}
/**
* @return bool
*/
public function isIssuedUnlimited(): bool
{
return $this->issued_unlimited;
}
/**
* @param string|null $transUrl
* @return $this
*/
public function setTransUrl(?string $transUrl): self
{
$this->trans_url = $transUrl;
return $this;
}
/**
* @return string|null
*/
public function getTransUrl(): ?string
{
return $this->trans_url;
}
/**
* @param bool|null $firstTimeLimit
* @return $this
*/
public function setFirstTimeLimit(?bool $firstTimeLimit = false): self
{
$this->first_time_limit = $firstTimeLimit;
return $this;
}
/**
* @return bool
*/
public function isFirstTimeLimit(): bool
{
return $this->first_time_limit;
}
/**
* @param string|null $minPrice
* @return $this
*/
public function setMinPrice(?string $minPrice): self
{
$this->min_price = $minPrice;
return $this;
}
/**
* @return string|null
*/
public function getMinPrice(): ?string
{
return $this->min_price;
}
/**
* @param string|null $customerAvailable
* @return $this
*/
public function setCustomerAvailable(?string $customerAvailable): self
{
$this->customer_available = $customerAvailable;
return $this;
}
/**
* @return string|null
*/
public function getCustomerAvailable(): ?string
{
return $this->customer_available;
}
/**
* @param \DateTime|null $availableFrom
* @return $this
*/
public function setAvailableFrom(?\DateTime $availableFrom): self
{
$this->available_from = $availableFrom;
return $this;
}
/**
* @return \DateTime
*/
public function getAvailableFrom()
{
return $this->available_from;
}
/**
* @param \DateTime|null $availableTo
* @return $this
*/
public function setAvailableTo(?\DateTime $availableTo): self
{
$this->available_to = $availableTo;
return $this;
}
/**
* @return \DateTime
*/
public function getAvailableTo()
{
return $this->available_to;
}
/**
* @param bool|null $enable
* @return $this
*/
public function setEnable(?bool $enable = false)
{
$this->enable = $enable;
return $this;
}
/**
* @return bool
*/
public function isEnable(): bool
{
return $this->enable;
}
/**
* @param bool|null $deleted
* @return $this
*/
public function setDeleted(?bool $deleted = false): self
{
$this->deleted = $deleted;
return $this;
}
/**
* @return bool
*/
public function isDeleted(): bool
{
return $this->deleted;
}
/**
* @param \DateTime $createDate
* @return $this
*/
public function setCreateDate(?\DateTime $createDate): self
{
$this->create_date = $createDate;
return $this;
}
/**
* @return \DateTime
*/
public function getCreateDate()
{
return $this->create_date;
}
/**
* @param \DateTime $updateDate
* @return $this
*/
public function setUpdateDate(?\DateTime $updateDate): self
{
$this->update_date = $updateDate;
return $this;
}
/**
* @return \DateTime
*/
public function getUpdateDate()
{
return $this->update_date;
}
/**
* @param bool $isDisplayMypage
* @return $this
*/
public function setIsDisplayMypage(bool $isDisplayMypage): self
{
$this->is_display_mypage = $isDisplayMypage;
return $this;
}
/**
* @return boolean
*/
public function isDisplayMypage()
{
return $this->is_display_mypage;
}
/**
* @param bool|null $isDisplayGuest
* @return $this
*/
public function setIsDisplayGuest(?bool $isDisplayGuest = false): self
{
$this->is_display_guest = $isDisplayGuest;
return $this;
}
/**
* @return boolean
*/
public function isDisplayGuest()
{
return $this->is_display_guest;
}
/**
* @param OrderCoupon $OrderCoupon
* @return $this
*/
public function addOrderCoupon(OrderCoupon $OrderCoupon)
{
$this->OrderCoupons[] = $OrderCoupon;
return $this;
}
/**
* @param OrderCoupon $OrderCoupon
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeOrderCoupon(OrderCoupon $OrderCoupon)
{
return $this->OrderCoupons->removeElement($OrderCoupon);
}
/**
* @return ArrayCollection
*/
public function getOrderCoupons()
{
return $this->OrderCoupons;
}
/**
* @param CouponCategory $CouponCategory
*/
public function addCouponCategory(CouponCategory $CouponCategory)
{
$this->CouponCategories[] = $CouponCategory;
return $this;
}
/**
* @param CouponCategory $CouponCategory
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeCouponCategory(CouponCategory $CouponCategory)
{
return $this->CouponCategories->removeElement($CouponCategory);
}
/**
* @return ArrayCollection
*/
public function getCouponCategories()
{
return $this->CouponCategories;
}
/**
* @param CouponCustomer $CouponCustomer
*/
public function addCouponCustomer(CouponCustomer $CouponCustomer)
{
$this->CouponCustomers[] = $CouponCustomer;
return $this;
}
/**
* @param CouponCustomer $CouponCustomer
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeCouponCustomer(CouponCustomer $CouponCustomer)
{
return $this->CouponCustomers->removeElement($CouponCustomer);
}
/**
* @return ArrayCollection
*/
public function getCouponCustomers()
{
return $this->CouponCustomers;
}
/**
* @param CouponProduct $CouponProduct
*/
public function addCouponProduct(CouponProduct $CouponProduct)
{
$this->CouponProducts[] = $CouponProduct;
return $this;
}
/**
* @param CouponProduct $CouponProduct
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeCouponProduct(CouponProduct $CouponProduct)
{
return $this->CouponProducts->removeElement($CouponProduct);
}
/**
* Get CouponProducts.
* @return ArrayCollection
*/
public function getCouponProducts()
{
return $this->CouponProducts;
}
/**
* @return string
*/
public function getOrderCouponCount()
{
$OrderCoupons = $this->getOrderCoupons();
$count = 0;
foreach ($OrderCoupons as $OrderCoupon) {
$OrderStatus = $OrderCoupon->getOrder()->getOrderStatus();
if (!in_array($OrderStatus->getId(), [
OrderStatus::PENDING,
OrderStatus::PROCESSING,
OrderStatus::CANCEL,
])) {
$count++;
}
}
return $count;
}
/**
* @return string
*/
public function getDiscountLabel()
{
if ($this->getCouponKind()->getId() == CouponKind::DISCOUNT_PRICE):
$discount = '¥' . $this->getDiscountPrice();
elseif ($this->getCouponKind()->getId() == CouponKind::DISCOUNT_RATE):
$discount = $this->getDiscountRate() . '%';
else:
$discount = trans('common.coupon.free_shipping');
endif;
return $discount;
}
/**
* @return array
*/
public function getTargetProducts()
{
$products = [];
foreach ($this->getCouponProducts() as $CouponProduct) {
if ($CouponProduct->getProduct()->isEnable()) {
$products[$CouponProduct->getProduct()->getId()] = $CouponProduct->getProduct()->getName();
}
}
ksort($products);
return $products;
}
/**
* @return array
*/
public function getTargetCategories()
{
$categories = [];
foreach ($this->getCouponCategories() as $CouponCategory) {
$categories[$CouponCategory->getCategory()->getId()] = $CouponCategory->getCategory()->getName();
}
ksort($categories);
return $categories;
}
}