<?php
namespace App\Entity;
use App\Repository\ProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=ProductRepository::class)
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity("code",message="Le code produit existe déjà.")
* @Vich\Uploadable
*/
class Product
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\NotBlank(message="Le code produit est obligatoire")
*/
private $code;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\NotBlank(message="Le nom est obligatoire")
*/
private $name;
/**
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
* @Assert\NotBlank(message="Le prix de vente est obligatoire")
*/
private $sellHT;
/**
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
* @Assert\NotBlank(message="Le prix d'achat est obligatoire")
*/
private $buyHT;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $stock;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @ORM\OneToMany(targetEntity=PieceLine::class, mappedBy="product")
*/
private $pieceLines;
private $codeName;
private $nomComplet;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $photo;
/**
* @var File
* @Vich\UploadableField(mapping="photos", fileNameProperty="photo")
*/
private $photoFile;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $taillePoids;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $marque;
/**
* @ORM\Column(type="date", nullable=true)
*/
private $dateExpiration;
/**
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
*/
private $marge;
/**
* @ORM\OneToMany(targetEntity=Lot::class, mappedBy="product", orphanRemoval=true, cascade={"persist"})
*/
private $lots;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $barecode;
/**
* @ORM\PrePersist
*/
public function createDate()
{
$this->marge = floatval($this->sellHT) - floatval($this->buyHT);
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
/**
* @ORM\PreUpdate
*/
public function updateDate()
{
$this->marge = $this->sellHT - $this->getBuyHT();
$this->updatedAt = new \DateTime();
}
public function __construct()
{
$this->pieceLines = new ArrayCollection();
$this->lots = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(?string $code): self
{
$this->code = $code;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getSellHT(): ?string
{
return $this->sellHT;
}
public function setSellHT(?string $sellHT): self
{
$this->sellHT = $sellHT;
return $this;
}
public function getBuyHT(): ?string
{
return $this->buyHT;
}
public function setBuyHT(?string $buyHT): self
{
$this->buyHT = $buyHT;
return $this;
}
public function getStock(): ?float
{
return $this->stock;
}
public function setStock(?float $stock): self
{
$this->stock = $stock;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return Collection|PieceLine[]
*/
public function getPieceLines(): Collection
{
return $this->pieceLines;
}
public function addPieceLine(PieceLine $pieceLine): self
{
if (!$this->pieceLines->contains($pieceLine)) {
$this->pieceLines[] = $pieceLine;
$pieceLine->setProduct($this);
}
return $this;
}
public function removePieceLine(PieceLine $pieceLine): self
{
if ($this->pieceLines->contains($pieceLine)) {
$this->pieceLines->removeElement($pieceLine);
// set the owning side to null (unless already changed)
if ($pieceLine->getProduct() === $this) {
$pieceLine->setProduct(null);
}
}
return $this;
}
public function getCodeName(): ?string
{
$nom = '';
if ($this->barecode != '') {
$nom = $this->barecode . ' - ';
}
if ($this->marque != '') {
$nom .= $this->marque . ' ';
}
$nom .= $this->code . " - " . $this->name . " - " . $this->getTaillePoids().' - (Stock : '.$this->getStockProduct().')';
return $nom;
}
public function getNomComplet(): ?string
{
$nom_complet = $this->name;
if ($this->marque != '') {
$nom_complet .= ' ' . $this->marque;
}
if ($this->taillePoids != '') {
$nom_complet .= ' ' . $this->taillePoids;
}
return $nom_complet;
}
public function getPhoto(): ?string
{
return $this->photo;
}
public function setPhoto(?string $photo): self
{
$this->photo = $photo;
return $this;
}
/**
* Set photoFile.
*
* @param string $photoFile
*
* @return PhotoFile
*/
public function setPhotoFile(File $photoFile = null)
{
$this->photoFile = $photoFile;
if ($photoFile)
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
/**
* Get photoFile.
*
* @return string
*/
public function getPhotoFile()
{
return $this->photoFile;
}
public function getTaillePoids(): ?string
{
return $this->taillePoids;
}
public function setTaillePoids(?string $taillePoids): self
{
$this->taillePoids = $taillePoids;
return $this;
}
public function getMarque(): ?string
{
return $this->marque;
}
public function setMarque(?string $marque): self
{
$this->marque = $marque;
return $this;
}
public function getDateExpiration(): ?\DateTimeInterface
{
return $this->dateExpiration;
}
public function setDateExpiration(?\DateTimeInterface $dateExpiration): self
{
$this->dateExpiration = $dateExpiration;
return $this;
}
public function getMarge(): ?string
{
return $this->marge;
}
public function setMarge(?string $marge): self
{
$this->marge = $marge;
return $this;
}
/**
* @return Collection|Lot[]
*/
public function getLots(): Collection
{
return $this->lots;
}
public function addLot(Lot $lot): self
{
if (!$this->lots->contains($lot)) {
$this->lots[] = $lot;
$lot->setProduct($this);
}
return $this;
}
public function removeLot(Lot $lot): self
{
if ($this->lots->contains($lot)) {
$this->lots->removeElement($lot);
// set the owning side to null (unless already changed)
if ($lot->getProduct() === $this) {
$lot->setProduct(null);
}
}
return $this;
}
public function getStockProduct(): ?string
{
$stock_product = 0;
foreach ($this->getLots() as $lot) {
$stock_product += $lot->getStock();
}
return $stock_product;
}
public function getBarecode(): ?string
{
return $this->barecode;
}
public function setBarecode(?string $barecode): self
{
$this->barecode = $barecode;
return $this;
}
public function hasStock(){
foreach($this->getLots() as $lot){
if($lot->getStock() > 0){
return true;
}
}
return false;
}
}