<?php
namespace AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Voucher
*/
#[ORM\Table(name: 'ctlf_voucher')]
#[ORM\Entity(repositoryClass: \AdminBundle\Repository\VoucherRepository::class)]
class Voucher extends BaseEntity
{
const VOUCHER_STATUS_ENABLE = 0;
const VOUCHER_STATUS_USED = 1;
const VOUCHER_DISCOUNT_PERCENTAGE = 0;
const VOUCHER_DISCOUNT_VALUE = 1;
const VOUCHER_TYPE_GENERAL = 0;
const VOUCHER_TYPE_MOBILE_APP = 1;
const VOUCHER_TYPE_REFERRAL = 2;
const VOUCHER_TYPE_EMPLOYEE = 3;
public static $statusTypes = array(
self::VOUCHER_STATUS_ENABLE => 'enable',
self::VOUCHER_STATUS_USED => 'used',
);
public static $discountTypes = array(
self::VOUCHER_DISCOUNT_PERCENTAGE => '%',
self::VOUCHER_DISCOUNT_VALUE => '£',
);
public static $types = array(
self::VOUCHER_TYPE_GENERAL => 'General',
self::VOUCHER_TYPE_MOBILE_APP => 'Mobile App',
self::VOUCHER_TYPE_EMPLOYEE => 'Employee',
);
/**
* @var integer
*/
#[ORM\Column(name: 'id', type: 'integer', nullable: false)]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
protected $id;
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\User::class)]
protected $user;
/**
* @var string
*/
#[ORM\Column(name: 'voucher_code', type: 'string', length: 64, unique: true)]
protected $voucherCode;
/**
* @var integer
*/
#[ORM\Column(name: 'voucher_value', type: 'integer')]
protected $voucherValue;
/**
* @var integer
*/
#[ORM\Column(name: 'voucher_min_journey_value', type: 'integer', nullable: true)]
protected $voucherMinJourneyValue;
/**
* @var string
*/
#[ORM\Column(name: 'voucher_discount_type', type: 'string', length: 2)]
protected $voucherDiscountType;
/**
* @var \DateTime
*/
#[ORM\Column(name: 'voucher_validity', type: 'datetime')]
protected $voucherValidity;
/**
* @var boolean
*/
#[ORM\Column(name: 'multiple_used', type: 'boolean')]
protected $multipleUsed;
/**
* @var integer
*/
#[ORM\Column(name: 'voucher_max_use', type: 'integer', nullable: true)]
protected $maxUse;
/**
* @var integer
*/
#[ORM\Column(name: 'voucher_max_use_per_user', type: 'integer', nullable: true)]
protected $maxUsePerUser;
/**
* @var integer
*/
#[ORM\Column(name: 'voucher_status', type: 'integer')]
protected $status = self::VOUCHER_STATUS_ENABLE;
/**
* @var integer
*/
#[ORM\Column(name: 'type', type: 'integer', options: ['default' => 0])]
protected $type = self::VOUCHER_TYPE_GENERAL;
/**
* @var string
*/
#[ORM\Column(name: 'details', type: 'text', nullable: true)]
protected $details;
/**
* @var string
*/
#[ORM\Column(name: 'employer_type', type: 'integer', length: 255, nullable: true)]
protected $employerType;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getVoucherCode()
{
return $this->voucherCode;
}
/**
* @param string $voucherCode
*
* @return Voucher
*/
public function setVoucherCode($voucherCode)
{
$this->voucherCode = $voucherCode;
return $this;
}
/**
* @return int
*/
public function getVoucherValue()
{
return $this->voucherValue;
}
/**
* @param int $voucherValue
*
* @return Voucher
*/
public function setVoucherValue($voucherValue)
{
$this->voucherValue = $voucherValue;
return $this;
}
/**
* @return int
*/
public function getVoucherMinJourneyValue()
{
return $this->voucherMinJourneyValue;
}
/**
* @param int $voucherMinJourneyValue
*
* @return Voucher
*/
public function setVoucherMinJourneyValue($voucherMinJourneyValue)
{
$this->voucherMinJourneyValue = $voucherMinJourneyValue;
return $this;
}
/**
* @param string $currencySymbol
* @return string
*/
public function getVoucherDiscountTypeDisplay($currencySymbol = '£')
{
return $this->voucherDiscountType ? $currencySymbol : '%';
}
/**
* @return string
*/
public function getVoucherDiscountType()
{
return $this->voucherDiscountType;
}
/**
* @param string $voucherDiscountType
*
* @return Voucher
*/
public function setVoucherDiscountType($voucherDiscountType)
{
$this->voucherDiscountType = $voucherDiscountType;
return $this;
}
/**
* @return \DateTime
*/
public function getVoucherValidity()
{
return $this->voucherValidity;
}
/**
* @param \DateTime $voucherValidity
*
* @return Voucher
*/
public function setVoucherValidity($voucherValidity)
{
$this->voucherValidity = $voucherValidity;
return $this;
}
/**
* @return bool
*/
public function isMultipleUsed()
{
return $this->multipleUsed;
}
/**
* @param bool $multipleUsed
*
* @return Voucher
*/
public function setMultipleUsed($multipleUsed)
{
$this->multipleUsed = $multipleUsed;
return $this;
}
/**
* @return int
*/
public function getStatus()
{
return $this->status;
}
/**
* @param int $status
*
* @return Voucher
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* @return int
*/
public function getMaxUse()
{
return $this->maxUse;
}
public function decrementMaxUse()
{
$this->maxUse--;
if ($this->maxUse == 0) {
$this->status = self::VOUCHER_STATUS_USED;
}
}
/**
* @param int $maxUse
*
* @return Voucher
*/
public function setMaxUse($maxUse)
{
$this->maxUse = $maxUse;
return $this;
}
public function __toString()
{
return $this->getVoucherCode() ? $this->getVoucherCode() : 'n\a';
}
/**
* @return Voucher
*/
public function getType()
{
return $this->type;
}
/**
* @param int $type
*
* @return Voucher
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* @param User $user
*
* return Voucher
*/
public function setUser(User $user = null)
{
$this->user = $user;
return $this;
}
/**
* @return Voucher
*/
public function getDetails()
{
return $this->details;
}
/**
* @param string $details
*
* @return Voucher
*/
public function setDetails($details)
{
$this->details = $details;
return $this;
}
/**
* @param int $employerType
*
* @return Voucher
*/
public function setEmployerType($employerType)
{
$this->employerType = $employerType;
return $this;
}
/**
* @return string
*/
public function getEmployerType()
{
return $this->employerType;
}
/**
* @return int
*/
public function getMaxUsePerUser()
{
return $this->maxUsePerUser;
}
public function decrementMaxUsePerUser()
{
$this->maxUsePerUser--;
if ($this->maxUsePerUser === 0) {
$this->status = self::VOUCHER_STATUS_USED;
}
}
/**
* @param int $maxUsePerUser
*
* @return Voucher
*/
public function setMaxUsePerUser($maxUsePerUser)
{
$this->maxUsePerUser = $maxUsePerUser;
return $this;
}
/**
* @param float|null $price
*
* @return float
*/
public function calculateDiscountValue(?float $price = 0): float
{
$val = 0;
$value = $this->getVoucherValue();
$type = $this->getVoucherDiscountType();
if ($type == self::VOUCHER_DISCOUNT_VALUE) {
$val = $value;
} else if ($type == self::VOUCHER_DISCOUNT_PERCENTAGE) {
$val = floatval($price) > floatval(0) ? $price * ($value / 100) : 0;
}
// Check if the discount value is greater than the price to avoid negative results.
return floatval($val) >= floatval($price) ? $price : $val;
// Old way, unused.
switch ($this->getVoucherDiscountType()) {
case self::VOUCHER_DISCOUNT_VALUE:
return $this->getVoucherValue();
case self::VOUCHER_DISCOUNT_PERCENTAGE:
return floatval($price) > floatval(0)
? $price * ($this->getVoucherValue() / 100)
: 0
;
default:
return 0;
}
}
}