<?php
namespace App\Entity;
use App\Repository\CustomProductOrderRepository;
use App\Traits\DateTrait;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\SerializedName;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Commande d'un utilisateur pour un produit personnalisé
*
* @ORM\Entity(repositoryClass=CustomProductOrderRepository::class)
*
* @Serializer\ExclusionPolicy("ALL")
*/
class CustomProductOrder
{
public const PENDING = 0;
public const PROCESSING = 1;
public const DONE = 2;
public const CANCELED = 3;
public const STATUS = [self::PENDING, self::PROCESSING, self::DONE, self::CANCELED];
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @Expose
* @Groups({"customProductOrder:list"})
*/
private ?int $id = NULL;
/**
* Utilisateur qui passe la commande
*
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="customProductOrders")
* @ORM\JoinColumn(nullable=false)
*
* @Expose
* @Groups({"customProductOrder:list"})
*/
private ?User $user = NULL;
/**
* Quantité commandée
*
* @ORM\Column(type="integer")
*
* @Assert\Positive
*
* @Expose
* @Groups({"customProductOrder:list"})
*/
private ?int $quantity = NULL;
/**
* Statut de la commande
*
* @ORM\Column(type="integer")
*
* @Assert\Choice(choices=CustomProductOrder::STATUS, message="Sélectionnez un statut valide")
*
* @Expose
* @Groups({"customProductOrder:list"})
*/
private int $status = 0;
/**
* Donnée du formulaire rempli par l'utilisateur
*
* @ORM\Column(type="json")
*
* @Assert\Json( message = "Vous avez entré un Json invalide" )
*/
private ?string $formData = NULL;
/**
* Produit commandé (figé à l'instant de la commande stocké en JSON)
*
* @ORM\Column(type="json")
*
* @Assert\Json( message = "Vous avez entré un Json invalide" )
*
*/
private ?string $product = NULL;
/**
* Type de produit
*
* @ORM\ManyToOne(targetEntity=CustomProductType::class, inversedBy="templates")
* @ORM\JoinColumn(nullable=false, name="type", referencedColumnName="slug")
*
* @Expose
* @Groups({"customProductOrder:list"})
*/
private ?CustomProductType $type = NULL;
/**
* @ORM\OneToMany(targetEntity=CustomProductField::class, mappedBy="order", cascade={"persist","remove"})
*/
private $fields;
/**
* @ORM\Column(type="text")
*/
private $value;
use DateTrait;
public function getProductToArray(): array
{
return json_decode($this->product, TRUE);
}
public function getFormDataToArray(): array
{
return json_decode($this->formData, TRUE);
}
/**
* Label du type de produit
*
* @Serializer\VirtualProperty()
* @SerializedName ("orderTypeLabel")
* @Groups ({"customProductOrder:list"})
*
* @return string
*/
public function getOrderTypeLabel()
{
return $this->getType()->getName();
}
/**
* Label du produit
*
* @Serializer\VirtualProperty()
* @SerializedName ("productLabel")
* @Groups ({"customProductOrder:list"})
*
* @return string
*/
public function getProductLabel(): string
{
return $this->getProductToArray()[ 'label' ];
}
/**
* Description du produit
*
* @return string|null
*/
public function getProductDescription(): ?string
{
return $this->getProductToArray()[ 'description' ];
}
/**
* Valeur (prix) du produit
*
* @return float
*/
public function getProductValue(): float
{
return $this->getProductToArray()[ 'value' ] ?? 0;
}
/**
* Image du produit
*
* @return string|null
*/
public function getProductImage(): ?string
{
return $this->getProductToArray()[ 'image' ];
}
/**
* Liste des contacts (email) quand il y a une demande pour ce produit
*
* @return array
*/
public function getProductContacts(): array
{
return $this->getProductToArray()[ 'contacts' ];
}
public function getHumanizedStatus(): string
{
$labels = [
'En attente de traitement',
'En cours de traitement',
'Traitée',
'Annulée',
];
return $labels[ $this->status ];
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getQuantity(): ?int
{
return $this->quantity;
}
public function setQuantity(int $quantity): self
{
$this->quantity = $quantity;
return $this;
}
public function getStatus(): int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->status = $status;
return $this;
}
public function getFormData(): ?string
{
return $this->formData;
}
public function setFormData(string $formData): self
{
$this->formData = $formData;
return $this;
}
public function getProduct(): ?string
{
return $this->product;
}
public function setProduct(string $product): self
{
$this->product = $product;
return $this;
}
public function getType(): ?CustomProductType
{
return $this->type;
}
public function setType(?CustomProductType $type): self
{
$this->type = $type;
return $this;
}
public function getValue(): ?string
{
return $this->value;
}
public function setValue(string $value): self
{
$this->value = $value;
return $this;
}
}