<?php
namespace JF\JuridicusBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Criteria;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Gutschein
*
* @ORM\Entity
* @UniqueEntity("code")
*/
class Gutschein
{
const CLASSNAME = __CLASS__;
const STATUS_UNUSED = NULL;
const STATUS_USED = 'USED';
const STATUS_BLOCKED = 'BLOCKED';
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $code;
/**
*
* @var boolean
*/
private $multiple = false;
/**
* @var \DateTime
*/
private $created_at;
/**
* @var float
*/
private $betrag;
/**
* @var \DateTime
*/
private $expires_at;
/**
* @var integer
*/
private $valid_abo_typ;
/**
* @var string
*/
private $valid_zeitschrift;
/**
* @var boolean
*/
private $valid_online;
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $buchungen;
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $abos;
/**
*
* @var string
*/
private static $code_chars = 'ABCDEFGHJKMNPQRSTUXYZ23456789';
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @Assert\IsTrue(groups={"gutschein"}, message = "Ungültiger Gutscheincode.")
* changedFrom Assert\True
*/
public function isValid()
{
return $this->id !== null;
}
/**
* isUsed
*
* @return boolean
*
* @Assert\IsFalse(groups={"gutschein"}, message = "Der Gutscheincode wurde bereits verwendet.")
* changedFrom Assert\False
*/
public function isUsed()
{
if ($this->multiple) {
return false;
}
return $this->hasBuchung() || $this->hasAbo();
}
/**
* isExpired
*
* @return boolean
*
* @Assert\IsFalse(groups={"gutschein"}, message = "Der Gutscheincode ist nicht mehr gültig.")
* changedFrom Assert\False
*/
public function isExpired()
{
return $this->expires_at && $this->expires_at < new \DateTime('now');
}
/**
* isBlocked
*
* @return boolean
*
* @Assert\IsFalse(groups={"gutschein"}, message = "Der Gutscheincode ist ungültig.")
* changedFrom Assert\False
*/
public function isBlocked()
{
if ($this->status == self::STATUS_BLOCKED ) {
return true;
}
return false;
}
/**
* Set code
*
* @param string $code
* @return Gutschein
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code
*
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* Erzeugt einen zufälligen Code der Form 'xxxx-xxxx'
*
* @param string $postfix
* @return \JF\JuridicusBundle\Entity\Gutschein
*/
public function createCode( $postfix = false )
{
$max = strlen(self::$code_chars)-1;
$this->code='';
for ($i=0; $i<4; $i++) {
$this->code .= substr(self::$code_chars,rand(0,$max),1);
}
$this->code .= '-';
for ($i=0; $i<4; $i++) {
$this->code .= substr(self::$code_chars,rand(0,$max),1);
}
if ( $postfix ) {
$this->code .= '-'.$postfix;
}
return $this;
}
/**
* Set created_at
*
* @param \DateTime $createdAt
* @return Gutschein
*/
public function setCreatedAt(\DateTime $createdAt = null)
{
$this->created_at = $createdAt;
return $this;
}
/**
* Get created_at
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->created_at;
}
/**
* @ORM\PrePersist
*/
public function setCreatedAtValue()
{
$this->created_at = new \DateTime();
}
/**
* Set betrag
*
* @param float $betrag
* @return Gutschein
*/
public function setBetrag($betrag)
{
$this->betrag = $betrag;
return $this;
}
/**
* Get betrag
*
* @return float
*/
public function getBetrag()
{
return $this->betrag;
}
/**
*
* @return string
*/
public function __toString()
{
return ($this->code) ? $this->code : '';
}
/**
* Constructor
*/
public function __construct()
{
$this->buchungen = new \Doctrine\Common\Collections\ArrayCollection();
$this->abos = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set expires_at
*
* @param \DateTime $expiresAt
* @return Gutschein
*/
public function setExpiresAt(\DateTime $expiresAt = null)
{
$this->expires_at = $expiresAt;
return $this;
}
/**
* Get expires_at
*
* @return \DateTime
*/
public function getExpiresAt()
{
return $this->expires_at;
}
/**
* Set valid_zeitschrift
*
* @param string $validZeitschrift
* @return Gutschein
*/
public function setValidZeitschrift($validZeitschrift)
{
$this->valid_zeitschrift = $validZeitschrift;
return $this;
}
/**
* Get valid_zeitschrift
*
* @return string
*/
public function getValidZeitschrift()
{
return $this->valid_zeitschrift;
}
/**
* Set valid_online
*
* @param boolean $validOnline
* @return Gutschein
*/
public function setValidOnline($validOnline)
{
$this->valid_online = $validOnline;
return $this;
}
/**
* Get valid_online
*
* @return boolean
*/
public function getValidOnline()
{
return $this->valid_online;
}
/**
* Add buchungen
*
* @param \JF\JuridicusBundle\Entity\KundePruefung $buchung
* @return Gutschein
*/
public function addBuchungen(KundePruefung $buchung)
{
$this->buchungen[] = $buchung;
$buchung->setGutschein($this);
return $this;
}
/**
* Remove buchungen
*
* @param \JF\JuridicusBundle\Entity\KundePruefung $buchung
*/
public function removeBuchungen(KundePruefung $buchung)
{
$this->buchungen->removeElement($buchung);
}
/**
* Get buchungen
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getBuchungen()
{
return $this->buchungen;
}
/**
*
* @return boolean
*/
public function hasBuchung()
{
return ! $this->buchungen->isEmpty();
}
/**
* Add abos
*
* @param \JF\JuridicusBundle\Entity\Abo $abo
* @return Gutschein
*/
public function addAbo(Abo $abo)
{
$this->abos[] = $abo;
$abo->setGutschein($this);
return $this;
}
/**
* Remove abos
*
* @param \JF\JuridicusBundle\Entity\Abo $abo
*/
public function removeAbo(Abo $abo)
{
$this->abos->removeElement($abo);
}
/**
* Get abos
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAbos()
{
return $this->abos;
}
/**
*
* @return boolean
*/
public function hasAbo()
{
return ! $this->abos->isEmpty();
}
/**
* Set valid_abo_typ
*
* @param integer $validAboTyp
* @return Gutschein
*/
public function setValidAboTyp($validAboTyp)
{
$this->valid_abo_typ = $validAboTyp;
return $this;
}
/**
* Get valid_abo_typ
*
* @return integer
*/
public function getValidAboTyp()
{
return $this->valid_abo_typ;
}
/**
* Set multiple
*
* @param boolean $multiple
* @return Gutschein
*/
public function setMultiple($multiple = true)
{
$this->multiple = $multiple;
return $this;
}
/**
* is multiple
*
* @return boolean
*/
public function isMultiple()
{
return $this->multiple;
}
/**
* @var string
*/
private $bulk_slug;
/**
* @var string
*/
private $status;
/**
* Get multiple
*
* @return boolean
*/
public function getMultiple()
{
return $this->multiple;
}
/**
* Set status
*
* @param string $status
* @return Gutschein
*/
public function setStatus($status)
{
$this->status = $status;
$this->status_updated_at = new \DateTime();
return $this;
}
/**
* Get status
*
* @return string
*/
public function getStatus()
{
return $this->status;
}
/**
* @var \DateTime
*/
private $status_updated_at;
/**
* Set status_updated_at
*
* @param \DateTime $statusUpdatedAt
* @return Gutschein
*/
public function setStatusUpdatedAt($statusUpdatedAt)
{
$this->status_updated_at = $statusUpdatedAt;
return $this;
}
/**
* Get status_updated_at
*
* @return \DateTime
*/
public function getStatusUpdatedAt()
{
return $this->status_updated_at;
}
/**
* @var \JF\JuridicusBundle\Entity\GutscheinMetadata
*/
private $gutschein_metadata;
/**
* Set gutschein_metadata
*
* @param \JF\JuridicusBundle\Entity\GutscheinMetadata $gutscheinMetadata
* @return Gutschein
*/
public function setGutscheinMetadata(\JF\JuridicusBundle\Entity\GutscheinMetadata $gutscheinMetadata = null)
{
$this->gutschein_metadata = $gutscheinMetadata;
return $this;
}
/**
* Get gutschein_metadata
*
* @return \JF\JuridicusBundle\Entity\GutscheinMetadata
*/
public function getGutscheinMetadata()
{
return $this->gutschein_metadata;
}
}