<?php
namespace App\Entity;
use App\Validator\Constraints as AppAssert;
use App\Entity\Product\ConfirmedProduct;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class CustomerGroup
*
* @ORM\Entity(repositoryClass="App\Repository\CustomerGroupRepository")
* @ORM\Table(name="malys_customer_group")
* @ORM\HasLifecycleCallbacks()
* @AppAssert\CustomerGroup
* @ExclusionPolicy("all")
*/
class CustomerGroup
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=false)
* @Assert\NotBlank
* @Expose
*/
protected $name;
/**
* @var DateTime
*
* @ORM\Column(name="createdAt", type="datetime", nullable=false)
*/
protected $createdAt;
/**
* @var DateTime
*
* @ORM\Column(name="updatedAt", type="datetime", nullable=true)
*/
protected $updatedAt;
/**
* @var ArrayCollection
* One CustomerGroup has Many Customers.
* @ORM\OneToMany(targetEntity="Customer", mappedBy="customerGroup", cascade={"refresh"})
*/
protected $customers;
/**
* @var boolean
*
* @ORM\Column(name="useProdControl", type="boolean", options={"default" : false})
*/
protected $useProdControl = false;
/**
* @ORM\ManyToOne (targetEntity="User")
*/
/**
* @ORM\ManyToMany (targetEntity="App\Entity\User", inversedBy="managedGroups")
* @ORM\JoinTable (name="malys_admin_customer_group")
* @Assert\Count (min=0, minMessage="Vous devez choisir au moins {{min}} administrateur")
*/
protected $admins;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Product\ConfirmedProduct", inversedBy="mercuCustomerGroups")
* @ORM\JoinTable(
* name="malys_customer_groups_products",
* joinColumns={@ORM\JoinColumn(name="customer_group_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id")}
* )
*/
protected $mercuProducts;
/**
* @var ArrayCollection
* One CustomerGroup has Many GuestProducts.
* @ORM\OneToMany(targetEntity="App\Entity\Product\GuestProduct", mappedBy="byCustomerGroup", cascade={"remove"})
*/
protected $guestProducts;
/**
* @var string
*
* @ORM\Column(name="source_system", type="string", nullable=true)
* @Expose
*/
protected $sourceSystem;
public function __construct()
{
$this->mercuProducts = new ArrayCollection();
$this->admins = new ArrayCollection();
$this->customers = new ArrayCollection();
}
public function __toString()
{
return $this->name;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set createdAt
*
* @return CustomerGroup
* @throws Exception
* @ORM\PrePersist
*/
public function setCreatedAt()
{
$this->createdAt = new DateTime();
$this->updatedAt = new DateTime();
return $this;
}
/**
* @return DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set updatedAt
*
* @return CustomerGroup
* @throws Exception
* @ORM\PreUpdate
*/
public function setUpdatedAt()
{
$this->updatedAt = new DateTime();
return $this;
}
/**
* @return ArrayCollection
*/
public function getCustomers()
{
return $this->customers;
}
/**
* @param ArrayCollection $customers
*/
public function setCustomers(ArrayCollection $customers)
{
$this->customers = $customers;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name)
{
$this->name = $name;
}
/**
* @return bool
*/
public function getUseProdControl(): bool
{
return $this->useProdControl;
}
/**
* @param bool $useProdControl
*/
public function setUseProdControl(bool $useProdControl): void
{
$this->useProdControl = $useProdControl;
}
/**
* @return mixed
*/
public function getMercuProducts()
{
return $this->mercuProducts;
}
/**
* @param mixed $mercuProducts
*/
public function setMercuProducts($mercuProducts)
{
$this->mercuProducts = $mercuProducts;
}
public function addMercuProduct(ConfirmedProduct $product)
{
if (! $this->mercuProducts->contains($product)) {
$this->mercuProducts->add($product);
$product->forceUpdateAt();
}
return $this;
}
public function removeMercuProduct(ConfirmedProduct $product)
{
if ($this->mercuProducts->contains($product)) {
$this->mercuProducts->removeElement($product);
}
return $this;
}
/**
* @return mixed
*/
public function getAdmins()
{
return $this->admins;
}
/**
* @param mixed $admins
*/
public function setAdmins($admins): void
{
$this->admins = $admins;
}
public function addAdmin(User $user)
{
if (! $this->admins->contains($user)) {
$this->admins->add($user);
}
}
public function removeAdmin(User $user)
{
if ($this->admins->contains($user)) {
$this->admins->removeElement($user);
}
}
/**
* @return string
*/
public function getSourceSystem(): ?string
{
return $this->sourceSystem;
}
/**
* @param string $sourceSystem
*/
public function setSourceSystem(?string $sourceSystem): void
{
$this->sourceSystem = $sourceSystem;
}
}