<?php
namespace App\Entity;
use App\Repository\PurchaseProductRepository;
use App\Traits\DateTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups;
/**
* @ORM\Entity(repositoryClass=PurchaseProductRepository::class)
* @ORM\HasLifecycleCallbacks()
*
* @Serializer\ExclusionPolicy("ALL")
*/
class PurchaseProduct
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @Expose
* @Groups({"purchaseProducts", "purchase_product_list", "export_purchase_product_datatable"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*
* @Expose
* @Groups({"purchaseProducts", "purchase_product_list", "export_purchase_product_datatable"})
*/
private $reference;
/**
* Désignation
*
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Expose
* @Groups({"purchaseProducts", "purchase_product_list", "export_purchase_product_datatable"})
*/
private $name;
/**
* "Catégorie" de produit (chaudière murale, chaudière sol etc)
*
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Expose
* @Groups({"purchase_product_list", "export_purchase_product_datatable"})
*/
private $type;
/**
* Valeur en points
*
* @ORM\Column(type="float", nullable=true)
*
* @Expose
* @Groups({"purchaseProducts", "purchase_product_list", "export_purchase_product_datatable"})
*/
private $value;
/**
* "Hiérarchie" de produit (chaudière murale, chaudière sol etc)
*
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Expose
* @Groups({"purchase_product_list", "export_purchase_product_datatable"})
*/
private $hierarchie;
/**
* @ORM\OneToMany(targetEntity=PurchaseProductItem::class, mappedBy="product")
*
* @Expose
* @Groups({"purchase_product_list", "export_purchase_product_datatable"})
*/
private $purchaseItems;
/**
* Activation référence produit
* @ORM\Column(type="boolean", nullable=true, options={"default":"0"})
*
* @Expose
* @Groups({"purchase_product_list", "export_purchase_product_datatable"})
*/
private $enabled;
/**
* @ORM\ManyToMany(targetEntity=PointTransactionBooster::class, mappedBy="purchaseProducts")
*/
private $pointTransactionBoosters;
/**
* @ORM\Column(type="text", nullable=true)
* @Expose
* @Groups({"purchase_product_list", "export_purchase_product_datatable"})
*/
private ?string $categoryValues;
/**
* Indique la catégorie de point par defaut
*
* @ORM\Column(type="string", nullable=true)
* @Expose
* @Groups({"purchase_product_list", "export_purchase_product_datatable"})
*/
private ?string $defaultPointCategory = NULL;
use DateTrait;
public function __construct()
{
$this->purchaseItems = new ArrayCollection();
$this->pointTransactionBoosters = new ArrayCollection();
}
public function __toString()
{
return $this->name;
}
/**
* @return string
* @Serializer\VirtualProperty()
* @Serializer\SerializedName("ref_name")
*
* @Expose()
* @Groups({"export_purchase_declaration_datatable"})
*/
public function getRefName(): string
{
return $this->reference . ' - ' . $this->name;
}
/**
* @Serializer\VirtualProperty()
* @Serializer\SerializedName("array_category_values")
*
* @Expose()
* @Groups({"purchase_product_list"})
* @return mixed
*/
public function getArrayCategoryValues()
{
return json_decode( $this->categoryValues, TRUE ) ?? [];
}
public function modifyValueToCategory( string $slug, int $value ): self
{
$newArray = $this->getArrayCategoryValues();
$newArray[ $slug ] = $value;
$this->categoryValues = json_encode( $newArray );
return $this;
}
/**
* @Serializer\VirtualProperty()
* @Serializer\SerializedName("count_purchase_items")
*
* @Expose()
* @Groups({"purchase_product_list", "export_purchase_product_datatable"})
*
* @return int
*/
public function countPurchaseItems()
{
return count( $this->purchaseItems );
}
public function getId(): ?int
{
return $this->id;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference( string $reference ): self
{
$this->reference = $reference;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName( ?string $name ): self
{
$this->name = $name;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType( ?string $type ): self
{
$this->type = $type;
return $this;
}
public function getValue(): ?float
{
return $this->value;
}
public function setValue( ?float $value ): self
{
$this->value = $value;
return $this;
}
public function getHierarchie(): ?string
{
return $this->hierarchie;
}
public function setHierarchie( ?string $hierarchie ): self
{
$this->hierarchie = $hierarchie;
return $this;
}
public function getEnabled(): ?string
{
return $this->enabled;
}
public function setEnabled( string $enabled ): self
{
$this->enabled = $enabled;
return $this;
}
/**
* @return Collection|PurchaseProductItem[]
*/
public function getPurchaseItems(): Collection
{
return $this->purchaseItems;
}
public function addPurchaseItem( PurchaseProductItem $purchaseItem ): self
{
if ( !$this->purchaseItems->contains( $purchaseItem ) ) {
$this->purchaseItems[] = $purchaseItem;
$purchaseItem->setProduct( $this );
}
return $this;
}
public function removePurchaseItem( PurchaseProductItem $purchaseItem ): self
{
if ( $this->purchaseItems->removeElement( $purchaseItem ) ) {
// set the owning side to null (unless already changed)
if ( $purchaseItem->getProduct() === $this ) {
$purchaseItem->setProduct( NULL );
}
}
return $this;
}
/**
* @return Collection<int, PointTransactionBooster>
*/
public function getPointTransactionBoosters(): Collection
{
return $this->pointTransactionBoosters;
}
public function addPointTransactionBooster( PointTransactionBooster $pointTransactionBooster ): self
{
if ( !$this->pointTransactionBoosters->contains( $pointTransactionBooster ) ) {
$this->pointTransactionBoosters[] = $pointTransactionBooster;
$pointTransactionBooster->addPurchaseProduct( $this );
}
return $this;
}
public function removePointTransactionBooster( PointTransactionBooster $pointTransactionBooster ): self
{
if ( $this->pointTransactionBoosters->removeElement( $pointTransactionBooster ) ) {
$pointTransactionBooster->removePurchaseProduct( $this );
}
return $this;
}
public function getCategoryValues(): ?string
{
return $this->categoryValues;
}
public function setCategoryValues( ?string $categoryValues ): self
{
$this->categoryValues = $categoryValues;
return $this;
}
public function getDefaultPointCategory(): ?string
{
return $this->defaultPointCategory;
}
public function setDefaultPointCategory(?string $defaultPointCategory): self
{
$this->defaultPointCategory = $defaultPointCategory;
return $this;
}
}