src/Entity/Article.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Cocur\Slugify\Slugify;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use Symfony\Component\Serializer\Annotation\Groups;
  7. use App\Repository\ArticleRepository;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  12. /**
  13.  * @ORM\Entity(repositoryClass=ArticleRepository::class)
  14.  * @ORM\Table(name="article", indexes={@ORM\Index(columns={"title","description"}, flags={"fulltext"})})
  15.  * @ApiResource(
  16.  *  paginationItemsPerPage=6,
  17.  *  normalizationContext={"groups"={"list:article"}},
  18.  *  collectionOperations={"get"},
  19.  *  itemOperations={"get"}
  20.  * )
  21.  * @UniqueEntity(
  22.  *  fields="ref",
  23.  *  message="Cette référence existe ! "
  24.  * )
  25.  */
  26. class Article
  27. {
  28.     const ETATS =[
  29.         'Top'=>'Top',
  30.         'New'=>'Nouveau',
  31.         'Tendance'=>'Tendance',
  32.         'Recommandez'=>'Recommandez',
  33.         'Populaire'=>'Populaire',
  34.         'Meilleurs ventes'=>'Meilleurs ventes'
  35.     ];
  36.     const STATUS =[
  37.         'Neuf'=>'Neuf',
  38.         'Reconditionné'=>'Reconditionné',
  39.         'Occasion'=>'Occasion',
  40.     ];
  41.     const LABEL =[
  42.         'New'=>'New',
  43.         'Top'=>'Top'
  44.     ];
  45.     /**
  46.      * @ORM\Id
  47.      * @ORM\GeneratedValue
  48.      * @ORM\Column(type="integer")
  49.      * @[Groups(['conference:list', 'conference:item'])]
  50.      */
  51.     private $id;
  52.     /**
  53.      * @Assert\Length(
  54.      *     min = 3,
  55.      *     max = 70
  56.      * )
  57.      * @Assert\NotBlank()
  58.      * @ORM\Column(type="string", length=255)
  59.      * @Groups({"list:article"})
  60.      */
  61.     private $title;
  62.     
  63.     /**
  64.      * @Assert\NotBlank()
  65.      * @ORM\Column(type="float")
  66.      * @Groups({"list:article"})
  67.      */
  68.     private $price;
  69.     
  70.     /**
  71.      * @Assert\NotBlank()
  72.      * @ORM\Column(type="text")
  73.      * @Groups({"list:article"})
  74.      */
  75.     private $description;
  76.     
  77.     /**
  78.      * @ORM\Column(type="datetime" ,options={"default"="CURRENT_TIMESTAMP"})
  79.      * @Groups({"list:article"})
  80.      */
  81.     private $created_at;
  82.     
  83.     /**
  84.      * @ORM\Column(type="datetime", nullable=true)
  85.      * @Groups({"list:article"})
  86.      */
  87.     private $updated_at;
  88.     
  89.     /**
  90.      * 
  91.      * @ORM\Column(type="boolean")
  92.      * @Groups({"list:article"})
  93.      */
  94.     private $enabled;
  95.     
  96.     /**
  97.      * @Assert\NotBlank()
  98.      * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="articles")
  99.      * @ORM\JoinColumn(nullable=true)
  100.      */
  101.     private $category;
  102.     
  103.     
  104.     /**
  105.      * @ORM\OneToMany(targetEntity=ArticleOption::class, mappedBy="article", orphanRemoval=true)
  106.      */
  107.     private $options;
  108.     
  109.     /**
  110.      * @ORM\Column(type="integer")
  111.      * @Groups({"list:article"})
  112.      */
  113.     private $quantity;
  114.     
  115.     /**
  116.      * @ORM\Column(type="float", nullable=true)
  117.      * @Assert\NotNull
  118.      * @Groups({"list:article"})
  119.      */
  120.     private $buyingPrice;
  121.     
  122.     /**
  123.      * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="article", orphanRemoval=true)
  124.      */
  125.     private $comments;
  126.     
  127.     /**
  128.      * @ORM\Column(type="string", length=255, nullable=true)
  129.      * @Groups({"list:article"})
  130.      */
  131.     private $etat;
  132.     
  133.     /**
  134.      * @ORM\ManyToMany(targetEntity=User::class, inversedBy="favoris")
  135.      */
  136.     private $favori;
  137.     
  138.     /**
  139.      * @ORM\OneToMany(targetEntity=ArticleBuy::class, mappedBy="article", orphanRemoval=true)
  140.      */
  141.     private $articleBuys;
  142.     
  143.     /**
  144.      * @ORM\Column(type="integer")
  145.      * @Groups({"list:article"})
  146.      */
  147.     private $qty_reel;
  148.     
  149.     
  150.     /**
  151.      * @ORM\Column(type="string", length=255, nullable=true)
  152.      * @Groups({"list:article"})
  153.      */
  154.     private $label;
  155.     
  156.     /**
  157.      * @ORM\Column(type="integer", nullable=true)
  158.      * @Groups({"list:article"})
  159.      */
  160.     private $reduction;
  161.     
  162.     /**
  163.      * @ORM\ManyToOne(targetEntity=Brand::class, inversedBy="articles")
  164.      */
  165.     private $brand;
  166.     
  167.     /**
  168.      * @ORM\Column(type="string", length=255)
  169.      * @Groups({"list:article"})
  170.      */
  171.     private $status;
  172.     
  173.     /**
  174.      * @ORM\Column(type="string", length=255, nullable=true)
  175.      * @Groups({"list:article"})
  176.      */
  177.     private $ref;
  178.     
  179.     /**
  180.      * @ORM\Column(type="string", length=255)
  181.      * @Groups({"list:article"})
  182.      */
  183.     private $detail;
  184.     /**
  185.      * @ORM\ManyToMany(targetEntity=Image::class, inversedBy="articles", cascade={"persist"})
  186.      */
  187.     private $images;
  188.     
  189.     public function __construct()
  190.     {
  191.         $this->options = new ArrayCollection();
  192.         $this->comments = new ArrayCollection();
  193.         $this->favori = new ArrayCollection();
  194.         $this->articleBuys = new ArrayCollection();
  195.         $this->images = new ArrayCollection();
  196.     }
  197.     public function getId(): ?int
  198.     {
  199.         return $this->id;
  200.     }
  201.     public function getSlug()
  202.     {
  203.         return (new Slugify())->slugify($this->title);
  204.     }
  205.     public function getTitle(): ?string
  206.     {
  207.         return $this->title;
  208.     }
  209.     public function setTitle(string $title): self
  210.     {
  211.         $this->title =ucfirst($title);
  212.         return $this;
  213.     }
  214.     public function getPrice(): ?float
  215.     {
  216.         return $this->price;
  217.     }
  218.     public function formatterBuying()
  219.     {
  220.         return number_format($this->buyingPrice,0,''' ');
  221.     }
  222.     public function formatterPrice()
  223.     {
  224.         return number_format($this->price,0,''' ');
  225.     }
  226.     public function setPrice(float $price): self
  227.     {
  228.         $this->price $price;
  229.         return $this;
  230.     }
  231.     public function getDescription(): ?string
  232.     {
  233.         return $this->description;
  234.     }
  235.     public function setDescription(string $description): self
  236.     {
  237.         $this->description ucfirst($description);
  238.         return $this;
  239.     }
  240.     public function getCreatedAt(): ?\DateTime
  241.     {
  242.         return $this->created_at;
  243.     }
  244.     public function setCreatedAt(\DateTime $created_at): self
  245.     {
  246.         $this->created_at $created_at;
  247.         return $this;
  248.     }
  249.     public function getUpdatedAt(): ?\DateTimeInterface
  250.     {
  251.         return $this->updated_at;
  252.     }
  253.     public function setUpdatedAt(?\DateTimeInterface $updated_at): self
  254.     {
  255.         $this->updated_at $updated_at;
  256.         return $this;
  257.     }
  258.     public function getEnabled(): ?bool
  259.     {
  260.         return $this->enabled;
  261.     }
  262.     public function setEnabled(bool $enabled): self
  263.     {
  264.         $this->enabled $enabled;
  265.         return $this;
  266.     }
  267.     public function getCategory(): ?Category
  268.     {
  269.         return $this->category;
  270.     }
  271.     public function setCategory(?Category $category): self
  272.     {
  273.         $this->category $category;
  274.         return $this;
  275.     }
  276.     /**
  277.      * @return Collection|ArticleOption[]
  278.      */
  279.     public function getOptions(): Collection
  280.     {
  281.         return $this->options;
  282.     }
  283.     public function addOption(ArticleOption $option): self
  284.     {
  285.         if (!$this->options->contains($option)) {
  286.             $this->options[] = $option;
  287.             $option->setArticle($this);
  288.         }
  289.         return $this;
  290.     }
  291.     public function removeOption(ArticleOption $option): self
  292.     {
  293.         if ($this->options->removeElement($option)) {
  294.             // set the owning side to null (unless already changed)
  295.             if ($option->getArticle() === $this) {
  296.                 $option->setArticle(null);
  297.             }
  298.         }
  299.         return $this;
  300.     }
  301.     public function getQuantity(): ?int
  302.     {
  303.         return $this->quantity;
  304.     }
  305.     public function setQuantity(int $quantity): self
  306.     {
  307.         $this->quantity $quantity;
  308.         return $this;
  309.     }
  310.     public function getBuyingPrice(): ?float
  311.     {
  312.         return $this->buyingPrice;
  313.     }
  314.     public function setBuyingPrice(float $buyingPrice): self
  315.     {
  316.         $this->buyingPrice $buyingPrice;
  317.         return $this;
  318.     }
  319.     /**
  320.      * @return Collection|Comment[]
  321.      */
  322.     public function getComments(): Collection
  323.     {
  324.         return $this->comments;
  325.     }
  326.     public function addComment(Comment $comment): self
  327.     {
  328.         if (!$this->comments->contains($comment)) {
  329.             $this->comments[] = $comment;
  330.             $comment->setArticle($this);
  331.         }
  332.         return $this;
  333.     }
  334.     public function removeComment(Comment $comment): self
  335.     {
  336.         if ($this->comments->removeElement($comment)) {
  337.             // set the owning side to null (unless already changed)
  338.             if ($comment->getArticle() === $this) {
  339.                 $comment->setArticle(null);
  340.             }
  341.         }
  342.         return $this;
  343.     }
  344.     public function getEtat(): ?string
  345.     {
  346.         return $this->etat;
  347.     }
  348.     public function setEtat(?string $etat): self
  349.     {
  350.         $this->etat $etat;
  351.         return $this;
  352.     }
  353.     /**
  354.      * @return Collection|User[]
  355.      */
  356.     public function getFavori(): Collection
  357.     {
  358.         return $this->favori;
  359.     }
  360.     public function addFavori(User $favori): self
  361.     {
  362.         if (!$this->favori->contains($favori)) {
  363.             $this->favori[] = $favori;
  364.         }
  365.         return $this;
  366.     }
  367.     public function removeFavori(User $favori): self
  368.     {
  369.         $this->favori->removeElement($favori);
  370.         return $this;
  371.     }
  372.     /**
  373.      * @return Collection|ArticleBuy[]
  374.      */
  375.     public function getArticleBuys(): Collection
  376.     {
  377.         return $this->articleBuys;
  378.     }
  379.     public function addArticleBuy(ArticleBuy $articleBuy): self
  380.     {
  381.         if (!$this->articleBuys->contains($articleBuy)) {
  382.             $this->articleBuys[] = $articleBuy;
  383.             $articleBuy->setArticle($this);
  384.         }
  385.         return $this;
  386.     }
  387.     public function removeArticleBuy(ArticleBuy $articleBuy): self
  388.     {
  389.         if ($this->articleBuys->removeElement($articleBuy)) {
  390.             // set the owning side to null (unless already changed)
  391.             if ($articleBuy->getArticle() === $this) {
  392.                 $articleBuy->setArticle(null);
  393.             }
  394.         }
  395.         return $this;
  396.     }
  397.     public function getQtyReel(): ?int
  398.     {
  399.         return $this->qty_reel;
  400.     }
  401.     public function setQtyReel(int $qty_reel): self
  402.     {
  403.         $this->qty_reel $qty_reel;
  404.         return $this;
  405.     }
  406.     public function getLabel(): ?string
  407.     {
  408.         return $this->label;
  409.     }
  410.     public function setLabel(?string $label): self
  411.     {
  412.         $this->label $label;
  413.         return $this;
  414.     }
  415.     public function getReduction(): ?int
  416.     {
  417.         return $this->reduction;
  418.     }
  419.     public function getNewPrice()
  420.     {
  421.         if($this->reduction 0){
  422.            return $this->getPrice() -  (($this->getPrice() * $this->getReduction() )/100) ;
  423.         }
  424.         return $this->getPrice();
  425.     }
  426.     public function setReduction(?int $reduction): self
  427.     {
  428.         $this->reduction $reduction;
  429.         return $this;
  430.     }
  431.     public static function reductions(){
  432.         $data[0] =0  ;
  433.         for ($i=1$i 100 $i++) { 
  434.             $reduction  $i' %';
  435.             $data[$reduction] = $i;
  436.         }
  437.         return $data;
  438.     }
  439.     public function getBrand(): ?Brand
  440.     {
  441.         return $this->brand;
  442.     }
  443.     public function setBrand(?Brand $brand): self
  444.     {
  445.         $this->brand $brand;
  446.         return $this;
  447.     }
  448.     public function getStatus(): ?string
  449.     {
  450.         return $this->status;
  451.     }
  452.     public function setStatus(string $status): self
  453.     {
  454.         $this->status $status;
  455.         return $this;
  456.     }
  457.     public function getRef(): ?string
  458.     {
  459.         return $this->ref;
  460.     }
  461.     public function setRef(string $ref): self
  462.     {
  463.         $ref sprintf("%04s"$ref);
  464.         $this->ref $ref;
  465.         return $this;
  466.     }
  467.     public function getDetail(): ?string
  468.     {
  469.         return $this->detail;
  470.     }
  471.     public function setDetail(string $detail): self
  472.     {
  473.         $this->detail $detail;
  474.         return $this;
  475.     }
  476.     /**
  477.      * @return Collection<int, Image>
  478.      */
  479.     public function getImages(): Collection
  480.     {
  481.         return $this->images;
  482.     }
  483.     public function addImage(Image $image): self
  484.     {
  485.         if (!$this->images->contains($image)) {
  486.             $this->images[] = $image;
  487.         }
  488.         return $this;
  489.     }
  490.     public function removeImage(Image $image): self
  491.     {
  492.         $this->images->removeElement($image);
  493.         return $this;
  494.     }
  495. }