src/Entity/Coupon.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. /**
  7.  * @ORM\Entity(repositoryClass="App\Repository\CouponRepository")
  8.  */
  9. class Coupon implements \Serializable
  10. {
  11.     const TYPE_PERCENTAGE 1;
  12.     const TYPE_DISCOUNT 2;
  13.     const TYPE_FREE_PRODUCT 3;
  14.     
  15.     const STATUS_ACTIVE 1;
  16.     const STATUS_INACTIVE 0;
  17.     
  18.     //These should match those in the Product class
  19.     const PRODUCT_TYPE_DIRECTORY 1;
  20.     const PRODUCT_TYPE_CLASSIFIED 2;
  21.     const PRODUCT_TYPE_MEMBERSHIP 3;
  22.     const PRODUCT_TYPE_SHOP 4;
  23.     
  24.     const RECURRING_NONE 0;
  25.     const RECURRING_SET_MONTHS 1;
  26.     const RECURRING_UNLIMITED 2;
  27.     
  28.     /**
  29.      * @ORM\Id()
  30.      * @ORM\GeneratedValue()
  31.      * @ORM\Column(type="bigint")
  32.      */
  33.     private $id;
  34.     
  35.     /**
  36.      * @ORM\Version @ORM\Column(type="integer")
  37.      */
  38.     private $version;
  39.     /**
  40.      * @ORM\Column(type="string", length=255)
  41.      */
  42.     private $title;
  43.     /**
  44.      * @ORM\Column(type="text")
  45.      */
  46.     private $description;
  47.     /**
  48.      * @ORM\Column(type="string", length=32)
  49.      */
  50.     private $code;
  51.     /**
  52.      * @ORM\Column(type="integer")
  53.      */
  54.     private $status;
  55.     /**
  56.      * @ORM\Column(type="datetime", nullable=true)
  57.      */
  58.     private $published_at;
  59.     /**
  60.      * @ORM\Column(type="datetime", nullable=true)
  61.      */
  62.     private $expires_at;
  63.     /**
  64.      * @ORM\Column(type="string", length=32)
  65.      */
  66.     private $type;
  67.     /**
  68.      * @ORM\Column(type="string", length=64)
  69.      */
  70.     private $amount;
  71.     /**
  72.      * @ORM\ManyToMany(targetEntity="App\Entity\Product", inversedBy="coupons")
  73.      */
  74.     private $products;
  75.     /**
  76.      * @ORM\ManyToMany(targetEntity="App\Entity\Purchase", mappedBy="coupons")
  77.      */
  78.     private $purchases;
  79.     /**
  80.      * @ORM\ManyToOne(targetEntity="App\Entity\Product")
  81.      */
  82.     private $chosen_product;
  83.     
  84.     /**
  85.      * @ORM\Column(type="integer")
  86.      */
  87.     private $limit_per_user;
  88.     
  89.     /**
  90.      * @ORM\Column(type="integer")
  91.      */
  92.     private $product_type;
  93.     
  94.     /**
  95.      * @ORM\Column(type="integer")
  96.      */
  97.     private $recurring_type;
  98.     
  99.     /**
  100.      * @ORM\Column(type="integer")
  101.      */
  102.     private $months_recurring;
  103.     
  104.     
  105.     public function __construct ()
  106.     {
  107.         $this->title "";
  108.         $this->description "";
  109.         $this->code "";
  110.         $this->status 1;
  111.         $this->amount 0;
  112.         $this->type self::TYPE_PERCENTAGE;
  113.         $this->products = new ArrayCollection();
  114.         $this->published_at = new \DateTime("now");
  115.         $this->purchases = new ArrayCollection();
  116.         $this->limit_per_user 1;
  117.         $this->product_type 0;
  118.         $this->recurring_type 0;
  119.         $this->months_recurring 1;
  120.     }
  121.     public function getId()
  122.     {
  123.         return $this->id;
  124.     }
  125.     
  126.     public function getVersion() {
  127.         return $this->version;
  128.     }
  129.     
  130.     public function getTitle(): ?string
  131.     {
  132.         return $this->title;
  133.     }
  134.     public function setTitle(string $title null): self
  135.     {
  136.         $this->title $title $title "";
  137.         return $this;
  138.     }
  139.     public function getDescription(): ?string
  140.     {
  141.         return $this->description;
  142.     }
  143.     public function setDescription(string $description null): self
  144.     {
  145.         $this->description $description $description "";
  146.         return $this;
  147.     }
  148.     public function getCode(): ?string
  149.     {
  150.         return $this->code;
  151.     }
  152.     public function setCode(string $code null): self
  153.     {
  154.         $this->code $code $code "";
  155.         return $this;
  156.     }
  157.     public function getDiscount(): ?int
  158.     {
  159.         return $this->discount;
  160.     }
  161.     public function setDiscount(int $discount): self
  162.     {
  163.         $this->discount $discount;
  164.         return $this;
  165.     }
  166.     public function getStatus(): ?int
  167.     {
  168.         return $this->status;
  169.     }
  170.     public function setStatus(int $status): self
  171.     {
  172.         $this->status $status;
  173.         return $this;
  174.     }
  175.     
  176.     public function getLimitPerUser(): ?int
  177.     {
  178.         return $this->limit_per_user;
  179.     }
  180.     public function setLimitPerUser(int $limit): self
  181.     {
  182.         $this->limit_per_user $limit;
  183.         return $this;
  184.     }
  185.     public function getPublishedAt(): ?\DateTimeInterface
  186.     {
  187.         return $this->published_at;
  188.     }
  189.     public function setPublishedAt(?\DateTimeInterface $published_at): self
  190.     {
  191.         $this->published_at $published_at;
  192.         return $this;
  193.     }
  194.     public function getExpiresAt(): ?\DateTimeInterface
  195.     {
  196.         return $this->expires_at;
  197.     }
  198.     public function setExpiresAt(?\DateTimeInterface $expires_at): self
  199.     {
  200.         $this->expires_at $expires_at;
  201.         return $this;
  202.     }
  203.     public function getType(): ?string
  204.     {
  205.         return $this->type;
  206.     }
  207.     public function setType(string $type null): self
  208.     {
  209.         $this->type $type $type "";
  210.         return $this;
  211.     }
  212.     public function getAmount(): ?float
  213.     {
  214.         return $this->amount;
  215.     }
  216.     public function setAmount(float $amount null): self
  217.     {
  218.         $this->amount $amount round($amount2) : 0;
  219.         return $this;
  220.     }
  221.     /**
  222.      * @return Collection|Product[]
  223.      */
  224.     public function getProducts(): Collection
  225.     {
  226.         return $this->products;
  227.     }
  228.     public function addProduct(Product $product): self
  229.     {
  230.         if (!$this->products->contains($product)) {
  231.             $this->products[] = $product;
  232.             $product->addCoupon($this);
  233.         }
  234.         return $this;
  235.     }
  236.     public function removeProduct(Product $product): self
  237.     {
  238.         if ($this->products->contains($product)) {
  239.             $this->products->removeElement($product);
  240.             $product->removeCoupon($this);
  241.         }
  242.         return $this;
  243.     }
  244.     
  245.     public function serialize ()
  246.     {
  247.         return serialize(array(
  248.             $this->id,
  249.             $this->title,
  250.             $this->description,
  251.             $this->code,
  252.             $this->type,
  253.             $this->amount,
  254.             $this->product_type,
  255.             $this->recurring_type,
  256.             $this->months_recurring,
  257.         ));
  258.     }
  259.     
  260.     public function unserialize ($serialized)
  261.     {
  262.         list (
  263.             $this->id,
  264.             $this->title,
  265.             $this->description,
  266.             $this->code,
  267.             $this->type,
  268.             $this->amount,
  269.             $this->product_type,
  270.             $this->recurring_type,
  271.             $this->months_recurring
  272.         ) = unserialize($serialized, array ("allowed_classes" => false));
  273.     }
  274.     
  275.     public function toJSON ()
  276.     {
  277.         return json_encode([
  278.             "id" => $this->id,
  279.             "title" => $this->title,
  280.             "description" => $this->description,
  281.             "code" => $this->code,
  282.             "type" => $this->type,
  283.             "amount" => $this->amount,
  284.             "product_type" => $this->product_type,
  285.             "recurring_type" => $this->recurring_type,
  286.             "months_recurring" => $this->months_recurring,
  287.         ]);
  288.     }
  289.     /**
  290.      * @return Collection|Purchase[]
  291.      */
  292.     public function getPurchases(): Collection
  293.     {
  294.         return $this->purchases;
  295.     }
  296.     public function addPurchase(Purchase $purchase): self
  297.     {
  298.         if (!$this->purchases->contains($purchase)) {
  299.             $this->purchases[] = $purchase;
  300.             $purchase->addCoupon($this);
  301.         }
  302.         return $this;
  303.     }
  304.     public function removePurchase(Purchase $purchase): self
  305.     {
  306.         if ($this->purchases->contains($purchase)) {
  307.             $this->purchases->removeElement($purchase);
  308.             $purchase->removeCoupon($this);
  309.         }
  310.         return $this;
  311.     }
  312.         
  313.     public function getAmountText ()
  314.     {
  315.         $amount $this->getAmount();
  316.         $recurring_text "";
  317.         
  318.         //Only membership are recurring for now
  319.         if($this->product_type == self::PRODUCT_TYPE_MEMBERSHIP) {
  320.             if(($this->recurring_type == self::RECURRING_NONE) || ($this->recurring_type == self::RECURRING_SET_MONTHS && $this->months_recurring 2)) {
  321.                 $recurring_text " for the first payment";
  322.             }
  323.             elseif($this->recurring_type == self::RECURRING_SET_MONTHS && $this->months_recurring >= 2) {
  324.                 $recurring_text " for the first " $this->months_recurring " months";
  325.             }
  326.             elseif($this->recurring_type == self::RECURRING_UNLIMITED) {
  327.                 $recurring_text " per month";
  328.             }
  329.         }
  330.         
  331.         switch ($this->type) {
  332.             
  333.             case self::TYPE_PERCENTAGE:
  334.                 
  335.                 if (strpos($amount"%") !== false) {
  336.                     return "{$amount} Off " $this->getProductTypeString(true) . $recurring_text;
  337.                 }
  338.                 
  339.                 else if (strpos($amount".") !== false) {
  340.                     // convert to percentage
  341.                     //return ($amount * 100) . "% Off " . $this->getProductTypeString(true) . $recurring_text;
  342.                 }
  343.                 
  344.                 return $amount "% Off " $this->getProductTypeString(true) . $recurring_text;
  345.                 
  346.             case self::TYPE_DISCOUNT:
  347.                 
  348.                 if (strpos($amount".") === false) {
  349.                     $amount "{$amount}00";
  350.                 }
  351.                 
  352.                 $amount preg_replace("/[^0-9]*/"""$amount);
  353.                 return "$" number_format(($amount 100), 2".""") . " Off " $this->getProductTypeString(true) . $recurring_text;
  354.                 
  355.             case self::TYPE_FREE_PRODUCT:
  356.                 
  357.                 /*$product = $this->chosen_product;
  358.                 return $product->getPriceUSD();*/
  359.                 
  360.                 if (strpos($amount".") === false) {
  361.                     $amount "{$amount}00";
  362.                 }
  363.                 
  364.                 $amount preg_replace("/[^0-9]*/"""$amount);
  365.                 return "$" number_format(($amount 100), 2".""") . " Off " $this->getProductTypeString(true) . $recurring_text;
  366.                 
  367.                 
  368.                 /*
  369.                 if ($purchase) {
  370.                     // find the correct 
  371.                 }
  372.                 
  373.                 else {
  374.                     // how to find the correct product here
  375.                     $return = array ();
  376.                     $products = $this->getProducts();
  377.                     for ($i = 0; $i < count($products); $i++) {
  378.                         $return[] = $products[$i]->getTitle();
  379.                     }
  380.                     
  381.                     if (1 < count($return)) {
  382.                         array_unshift($return, "One of the following products: ");
  383.                     }
  384.                     
  385.                     return implode("", $return);
  386.                 }
  387.                 */
  388.         }
  389.         return "";
  390.     }
  391.     public function getChosenProduct(): ?Product
  392.     {
  393.         return $this->chosen_product;
  394.     }
  395.     public function setChosenProduct(?Product $chosen_product): self
  396.     {
  397.         $this->chosen_product $chosen_product;
  398.         return $this;
  399.     }
  400.     
  401.     public function getProductType(): ?int
  402.     {
  403.         return $this->product_type;
  404.     }
  405.     public function setProductType(int $product_type): self
  406.     {
  407.         $this->product_type $product_type;
  408.         
  409.         return $this;
  410.     }
  411.     
  412.     public function getProductTypeString($plural false): ?string
  413.     {
  414.         
  415.         switch ($this->product_type) {
  416.             case self::PRODUCT_TYPE_DIRECTORY:
  417.                 return $plural "Directory Listings" "Directory Listing";
  418.             
  419.             case self::PRODUCT_TYPE_CLASSIFIED:
  420.                 return $plural "Classified Listings" "Classified Listing";
  421.                 
  422.             case self::PRODUCT_TYPE_MEMBERSHIP:
  423.                 return $plural "R-Club Memberships" "R-Club Membership";    
  424.                 
  425.             case self::PRODUCT_TYPE_SHOP:
  426.                 return $plural "RCS Shop Items" "RCS Shop Item";
  427.         }
  428.         
  429.         return "";
  430.     }
  431.     
  432.     public function getRecurringType(): ?int
  433.     {
  434.         return $this->recurring_type;
  435.     }
  436.     public function setRecurringType(int $recurring_type): self
  437.     {
  438.         $this->recurring_type $recurring_type;
  439.         
  440.         return $this;
  441.     }
  442.     
  443.     public function getRecurringTypeString(): ?string
  444.     {
  445.         
  446.         switch ($this->recurring_type) {
  447.             case self::RECURRING_NONE:
  448.                 return "One Time";
  449.             
  450.             case self::RECURRING_SET_MONTHS:
  451.                 return "X Months";
  452.                 
  453.             case self::RECURRING_UNLIMITED:
  454.                 return "Unlimited";
  455.         }
  456.         
  457.         return "";
  458.     }
  459.     
  460.     public function getMonthsRecurring(): ?int
  461.     {
  462.         return $this->months_recurring;
  463.     }
  464.     
  465.     public function setMonthsRecurring(int $months_recurring): self
  466.     {
  467.         $this->months_recurring $months_recurring;
  468.         
  469.         return $this;
  470.     }
  471.     
  472.     public function getAutoApplyURL() {
  473.         if($this->getCode()) {
  474.             return "https://www.rooferscoffeeshop.com/coupon/".$this->getCode();
  475.         }
  476.         return "";
  477.     }
  478.     
  479.     public function getDevAutoApplyURL() {
  480.         if($this->getCode()) {
  481.             return "https://updates.rooferscoffeeshop.com/coupon/".$this->getCode();
  482.         }
  483.         return "";
  484.     }
  485.     
  486.     public function __toString ()
  487.     {
  488.         return $this->code $this->code '';
  489.     }
  490. }