<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\CouponRepository")
*/
class Coupon implements \Serializable
{
const TYPE_PERCENTAGE = 1;
const TYPE_DISCOUNT = 2;
const TYPE_FREE_PRODUCT = 3;
const STATUS_ACTIVE = 1;
const STATUS_INACTIVE = 0;
//These should match those in the Product class
const PRODUCT_TYPE_DIRECTORY = 1;
const PRODUCT_TYPE_CLASSIFIED = 2;
const PRODUCT_TYPE_MEMBERSHIP = 3;
const PRODUCT_TYPE_SHOP = 4;
const RECURRING_NONE = 0;
const RECURRING_SET_MONTHS = 1;
const RECURRING_UNLIMITED = 2;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="bigint")
*/
private $id;
/**
* @ORM\Version @ORM\Column(type="integer")
*/
private $version;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\Column(type="string", length=32)
*/
private $code;
/**
* @ORM\Column(type="integer")
*/
private $status;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $published_at;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $expires_at;
/**
* @ORM\Column(type="string", length=32)
*/
private $type;
/**
* @ORM\Column(type="string", length=64)
*/
private $amount;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Product", inversedBy="coupons")
*/
private $products;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Purchase", mappedBy="coupons")
*/
private $purchases;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Product")
*/
private $chosen_product;
/**
* @ORM\Column(type="integer")
*/
private $limit_per_user;
/**
* @ORM\Column(type="integer")
*/
private $product_type;
/**
* @ORM\Column(type="integer")
*/
private $recurring_type;
/**
* @ORM\Column(type="integer")
*/
private $months_recurring;
public function __construct ()
{
$this->title = "";
$this->description = "";
$this->code = "";
$this->status = 1;
$this->amount = 0;
$this->type = self::TYPE_PERCENTAGE;
$this->products = new ArrayCollection();
$this->published_at = new \DateTime("now");
$this->purchases = new ArrayCollection();
$this->limit_per_user = 1;
$this->product_type = 0;
$this->recurring_type = 0;
$this->months_recurring = 1;
}
public function getId()
{
return $this->id;
}
public function getVersion() {
return $this->version;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title = null): self
{
$this->title = $title ? $title : "";
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description = null): self
{
$this->description = $description ? $description : "";
return $this;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code = null): self
{
$this->code = $code ? $code : "";
return $this;
}
public function getDiscount(): ?int
{
return $this->discount;
}
public function setDiscount(int $discount): self
{
$this->discount = $discount;
return $this;
}
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->status = $status;
return $this;
}
public function getLimitPerUser(): ?int
{
return $this->limit_per_user;
}
public function setLimitPerUser(int $limit): self
{
$this->limit_per_user = $limit;
return $this;
}
public function getPublishedAt(): ?\DateTimeInterface
{
return $this->published_at;
}
public function setPublishedAt(?\DateTimeInterface $published_at): self
{
$this->published_at = $published_at;
return $this;
}
public function getExpiresAt(): ?\DateTimeInterface
{
return $this->expires_at;
}
public function setExpiresAt(?\DateTimeInterface $expires_at): self
{
$this->expires_at = $expires_at;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type = null): self
{
$this->type = $type ? $type : "";
return $this;
}
public function getAmount(): ?float
{
return $this->amount;
}
public function setAmount(float $amount = null): self
{
$this->amount = $amount ? round($amount, 2) : 0;
return $this;
}
/**
* @return Collection|Product[]
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
$product->addCoupon($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
if ($this->products->contains($product)) {
$this->products->removeElement($product);
$product->removeCoupon($this);
}
return $this;
}
public function serialize ()
{
return serialize(array(
$this->id,
$this->title,
$this->description,
$this->code,
$this->type,
$this->amount,
$this->product_type,
$this->recurring_type,
$this->months_recurring,
));
}
public function unserialize ($serialized)
{
list (
$this->id,
$this->title,
$this->description,
$this->code,
$this->type,
$this->amount,
$this->product_type,
$this->recurring_type,
$this->months_recurring
) = unserialize($serialized, array ("allowed_classes" => false));
}
public function toJSON ()
{
return json_encode([
"id" => $this->id,
"title" => $this->title,
"description" => $this->description,
"code" => $this->code,
"type" => $this->type,
"amount" => $this->amount,
"product_type" => $this->product_type,
"recurring_type" => $this->recurring_type,
"months_recurring" => $this->months_recurring,
]);
}
/**
* @return Collection|Purchase[]
*/
public function getPurchases(): Collection
{
return $this->purchases;
}
public function addPurchase(Purchase $purchase): self
{
if (!$this->purchases->contains($purchase)) {
$this->purchases[] = $purchase;
$purchase->addCoupon($this);
}
return $this;
}
public function removePurchase(Purchase $purchase): self
{
if ($this->purchases->contains($purchase)) {
$this->purchases->removeElement($purchase);
$purchase->removeCoupon($this);
}
return $this;
}
public function getAmountText ()
{
$amount = $this->getAmount();
$recurring_text = "";
//Only membership are recurring for now
if($this->product_type == self::PRODUCT_TYPE_MEMBERSHIP) {
if(($this->recurring_type == self::RECURRING_NONE) || ($this->recurring_type == self::RECURRING_SET_MONTHS && $this->months_recurring < 2)) {
$recurring_text = " for the first payment";
}
elseif($this->recurring_type == self::RECURRING_SET_MONTHS && $this->months_recurring >= 2) {
$recurring_text = " for the first " . $this->months_recurring . " months";
}
elseif($this->recurring_type == self::RECURRING_UNLIMITED) {
$recurring_text = " per month";
}
}
switch ($this->type) {
case self::TYPE_PERCENTAGE:
if (strpos($amount, "%") !== false) {
return "{$amount} Off " . $this->getProductTypeString(true) . $recurring_text;
}
else if (strpos($amount, ".") !== false) {
// convert to percentage
//return ($amount * 100) . "% Off " . $this->getProductTypeString(true) . $recurring_text;
}
return $amount . "% Off " . $this->getProductTypeString(true) . $recurring_text;
case self::TYPE_DISCOUNT:
if (strpos($amount, ".") === false) {
$amount = "{$amount}00";
}
$amount = preg_replace("/[^0-9]*/", "", $amount);
return "$" . number_format(($amount / 100), 2, ".", "") . " Off " . $this->getProductTypeString(true) . $recurring_text;
case self::TYPE_FREE_PRODUCT:
/*$product = $this->chosen_product;
return $product->getPriceUSD();*/
if (strpos($amount, ".") === false) {
$amount = "{$amount}00";
}
$amount = preg_replace("/[^0-9]*/", "", $amount);
return "$" . number_format(($amount / 100), 2, ".", "") . " Off " . $this->getProductTypeString(true) . $recurring_text;
/*
if ($purchase) {
// find the correct
}
else {
// how to find the correct product here
$return = array ();
$products = $this->getProducts();
for ($i = 0; $i < count($products); $i++) {
$return[] = $products[$i]->getTitle();
}
if (1 < count($return)) {
array_unshift($return, "One of the following products: ");
}
return implode("", $return);
}
*/
}
return "";
}
public function getChosenProduct(): ?Product
{
return $this->chosen_product;
}
public function setChosenProduct(?Product $chosen_product): self
{
$this->chosen_product = $chosen_product;
return $this;
}
public function getProductType(): ?int
{
return $this->product_type;
}
public function setProductType(int $product_type): self
{
$this->product_type = $product_type;
return $this;
}
public function getProductTypeString($plural = false): ?string
{
switch ($this->product_type) {
case self::PRODUCT_TYPE_DIRECTORY:
return $plural ? "Directory Listings" : "Directory Listing";
case self::PRODUCT_TYPE_CLASSIFIED:
return $plural ? "Classified Listings" : "Classified Listing";
case self::PRODUCT_TYPE_MEMBERSHIP:
return $plural ? "R-Club Memberships" : "R-Club Membership";
case self::PRODUCT_TYPE_SHOP:
return $plural ? "RCS Shop Items" : "RCS Shop Item";
}
return "";
}
public function getRecurringType(): ?int
{
return $this->recurring_type;
}
public function setRecurringType(int $recurring_type): self
{
$this->recurring_type = $recurring_type;
return $this;
}
public function getRecurringTypeString(): ?string
{
switch ($this->recurring_type) {
case self::RECURRING_NONE:
return "One Time";
case self::RECURRING_SET_MONTHS:
return "X Months";
case self::RECURRING_UNLIMITED:
return "Unlimited";
}
return "";
}
public function getMonthsRecurring(): ?int
{
return $this->months_recurring;
}
public function setMonthsRecurring(int $months_recurring): self
{
$this->months_recurring = $months_recurring;
return $this;
}
public function getAutoApplyURL() {
if($this->getCode()) {
return "https://www.rooferscoffeeshop.com/coupon/".$this->getCode();
}
return "";
}
public function getDevAutoApplyURL() {
if($this->getCode()) {
return "https://updates.rooferscoffeeshop.com/coupon/".$this->getCode();
}
return "";
}
public function __toString ()
{
return $this->code ? $this->code : '';
}
}