src/Entity/Stock.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Storage;
  4. use App\Repository\StorageRepository;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Symfony\Component\Serializer\Annotation\Groups;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. /**
  11.  * @ORM\Entity(repositoryClass="App\Repository\StockRepository")
  12.  */
  13. class Stock
  14. {
  15.     /**
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue
  18.      * @ORM\Column(type="integer")
  19.      * @Groups({"stock_edit", "installation_edit"})
  20.      */
  21.     private $id;
  22.     /**
  23.      * @ORM\ManyToOne(targetEntity=Brand::class, inversedBy="stocks")
  24.      * @Assert\NotBlank
  25.      * @Groups({"stock_edit"})
  26.      */
  27.     private $brand;
  28.     /**
  29.      * @ORM\Column(type="string", length=4000, nullable=true)
  30.      * @Groups({"stock_edit"})
  31.      */
  32.     private $note;
  33.     /**
  34.      * @ORM\Column(type="string", length=255, nullable=true)
  35.      * @Groups({"stock_edit", "installation_edit"})
  36.      */
  37.     private $status;
  38.     /**
  39.      * @ORM\ManyToOne(targetEntity=Installation::class, inversedBy="products")
  40.      */
  41.     private $installation;
  42.     /**
  43.      * @ORM\ManyToOne(targetEntity=Reference::class, inversedBy="stocks")
  44.      * @Groups({"stock_edit", "installation_edit"})
  45.      */
  46.     private $reference;
  47.     /**
  48.      * @ORM\ManyToOne(targetEntity=Supplier::class, inversedBy="stocks")
  49.      * @Groups({"stock_edit"})
  50.      */
  51.     private $supplier;
  52.     /**
  53.      * @ORM\Column(type="string", length=255, nullable=true)
  54.      * @Groups({"stock_edit"})
  55.      */
  56.     private $rowBg;
  57.     /**
  58.      * @ORM\Column(type="datetime_immutable", nullable=true)
  59.      */
  60.     private $lastCheck;
  61.     /**
  62.      * @ORM\OneToMany(targetEntity=Storage::class, mappedBy="stock",cascade={"persist"})
  63.      * @Groups({"stock_edit"})
  64.      */
  65.     private $storages;
  66.     /**
  67.      * @ORM\Column(type="boolean", nullable=true)
  68.      */
  69.     private $checked;
  70.     /**
  71.      * @Groups({"installation_edit"})
  72.      */
  73.     private $totalQuantity;
  74.     /**
  75.      * @ORM\Column(type="string", length=255, nullable=true)
  76.      */
  77.     private $company;
  78.     public function __construct()
  79.     {
  80.         $this->storages = new ArrayCollection();
  81.         $this->totalQuantity 0;
  82.     }
  83.     public function getId(): ?int
  84.     {
  85.         return $this->id;
  86.     }
  87.     public function setId(?int $id): ?int
  88.     {
  89.         return $this->id $id;
  90.     }
  91.     public function getBrand(): ?Brand
  92.     {
  93.         return $this->brand;
  94.     }
  95.     public function setBrand(?Brand $brand): self
  96.     {
  97.         $this->brand $brand;
  98.         return $this;
  99.     }
  100.     public function getNote(): ?string
  101.     {
  102.         return $this->note;
  103.     }
  104.     public function setNote(?string $note): self
  105.     {
  106.         $this->note $note;
  107.         return $this;
  108.     }
  109.     public function getStatus(): ?string
  110.     {
  111.         return $this->status;
  112.     }
  113.     public function setStatus(?string $status): self
  114.     {
  115.         $this->status $status;
  116.         return $this;
  117.     }
  118.     public function getInstallation(): ?Installation
  119.     {
  120.         return $this->installation;
  121.     }
  122.     public function setInstallation(?Installation $installation): self
  123.     {
  124.         $this->installation $installation;
  125.         return $this;
  126.     }
  127.     public function getReference(): ?Reference
  128.     {
  129.         return $this->reference;
  130.     }
  131.     public function setReference(?Reference $reference): self
  132.     {
  133.         $this->reference $reference;
  134.         return $this;
  135.     }
  136.     public function getSupplier(): ?Supplier
  137.     {
  138.         return $this->supplier;
  139.     }
  140.     public function setSupplier(?Supplier $supplier): self
  141.     {
  142.         $this->supplier $supplier;
  143.         return $this;
  144.     }
  145.     public function getRowBg(): ?string
  146.     {
  147.         return $this->rowBg;
  148.     }
  149.     public function setRowBg(?string $rowBg): self
  150.     {
  151.         $this->rowBg $rowBg;
  152.         return $this;
  153.     }
  154.     public function getLastCheck(): ?\DateTimeImmutable
  155.     {
  156.         return $this->lastCheck;
  157.     }
  158.     public function setLastCheck(?\DateTimeImmutable $lastCheck): self
  159.     {
  160.         $this->lastCheck $lastCheck;
  161.         return $this;
  162.     }
  163.     /**
  164.      * @return Collection<int, Storage>
  165.      */
  166.     public function getStorages(): Collection
  167.     {
  168.         return $this->storages;
  169.     }
  170.     public function addStorage(Storage $storage): self
  171.     {
  172.         if (!$this->storages->contains($storage)) {
  173.             $this->storages[] = $storage;
  174.             $storage->setStock($this);
  175.         }
  176.         return $this;
  177.     }
  178.     public function removeStorage(Storage $storage): self
  179.     {
  180.         if ($this->storages->removeElement($storage)) {
  181.             // set the owning side to null (unless already changed)
  182.             if ($storage->getStock() === $this) {
  183.                 $storage->setStock(null);
  184.             }
  185.         }
  186.         return $this;
  187.     }
  188.     public function isChecked(): ?bool
  189.     {
  190.         return $this->checked;
  191.     }
  192.     public function setChecked(bool $checked): self
  193.     {
  194.         $this->checked $checked;
  195.         return $this;
  196.     }
  197.     public function getTotalQuantity() : int 
  198.     {
  199.         $this->totalQuantity 0;
  200.         foreach ($this->storages as $storage) {
  201.             $this->totalQuantity += $storage->getQuantity();
  202.         }
  203.         return $this->totalQuantity
  204.     }
  205.     
  206.     public function getOrderStorage(): ?Storage 
  207.     {
  208.         foreach ($this->storages as $storage) {
  209.             if($storage->getType() == 'Commande') {
  210.                 return $storage;
  211.             } 
  212.         }
  213.         return null;
  214.     }
  215.     
  216.     public function addQuantityToStorage(Storage $storageStorageRepository $storageRepository): void
  217.     {
  218.         $storageExists false;
  219.         foreach($this->storages as $st) {
  220.             if (
  221.                 $st->getType() == $storage->getType() && 
  222.                 $st->getTruck() == $storage->getTruck()
  223.             ){
  224.                 $st->setQuantity($st->getQuantity() + $storage->getQuantity());
  225.                 $storageRepository->remove($storagetrue);
  226.                 $storageExists true;
  227.             }
  228.         }
  229.         
  230.         if ($storageExists == false) {
  231.             $this->storages[] = $storage;
  232.             $storage->setStock($this);
  233.             $storageRepository->add($storagetrue);
  234.         }
  235.     }
  236.     
  237.     public function removeQuantityToStorage(Storage $storage): void
  238.     {
  239.         $storageExists false;
  240.         foreach($this->storages as $st) {
  241.             if (
  242.                 $st->getType() == $storage->getType() && 
  243.                 $st->getTruck() == $storage->getTruck()
  244.             ){
  245.                 $st->setQuantity($st->getQuantity() - $storage->getQuantity());
  246.                 $storageExists true;
  247.             }
  248.         }
  249.         if (!$storageExists) {
  250.             $this->storages[] = $storage;
  251.             $storage->setStock($this);
  252.         }
  253.     }
  254.     public function getCompany(): ?string
  255.     {
  256.         return $this->company;
  257.     }
  258.     public function setCompany(?string $company): self
  259.     {
  260.         $this->company $company;
  261.         return $this;
  262.     }
  263. }