<?php
namespace App\Entity;
use App\Entity\Storage;
use App\Repository\StorageRepository;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\StockRepository")
*/
class Stock
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"stock_edit", "installation_edit"})
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Brand::class, inversedBy="stocks")
* @Assert\NotBlank
* @Groups({"stock_edit"})
*/
private $brand;
/**
* @ORM\Column(type="string", length=4000, nullable=true)
* @Groups({"stock_edit"})
*/
private $note;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"stock_edit", "installation_edit"})
*/
private $status;
/**
* @ORM\ManyToOne(targetEntity=Installation::class, inversedBy="products")
*/
private $installation;
/**
* @ORM\ManyToOne(targetEntity=Reference::class, inversedBy="stocks")
* @Groups({"stock_edit", "installation_edit"})
*/
private $reference;
/**
* @ORM\ManyToOne(targetEntity=Supplier::class, inversedBy="stocks")
* @Groups({"stock_edit"})
*/
private $supplier;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"stock_edit"})
*/
private $rowBg;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $lastCheck;
/**
* @ORM\OneToMany(targetEntity=Storage::class, mappedBy="stock",cascade={"persist"})
* @Groups({"stock_edit"})
*/
private $storages;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $checked;
/**
* @Groups({"installation_edit"})
*/
private $totalQuantity;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $company;
public function __construct()
{
$this->storages = new ArrayCollection();
$this->totalQuantity = 0;
}
public function getId(): ?int
{
return $this->id;
}
public function setId(?int $id): ?int
{
return $this->id = $id;
}
public function getBrand(): ?Brand
{
return $this->brand;
}
public function setBrand(?Brand $brand): self
{
$this->brand = $brand;
return $this;
}
public function getNote(): ?string
{
return $this->note;
}
public function setNote(?string $note): self
{
$this->note = $note;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(?string $status): self
{
$this->status = $status;
return $this;
}
public function getInstallation(): ?Installation
{
return $this->installation;
}
public function setInstallation(?Installation $installation): self
{
$this->installation = $installation;
return $this;
}
public function getReference(): ?Reference
{
return $this->reference;
}
public function setReference(?Reference $reference): self
{
$this->reference = $reference;
return $this;
}
public function getSupplier(): ?Supplier
{
return $this->supplier;
}
public function setSupplier(?Supplier $supplier): self
{
$this->supplier = $supplier;
return $this;
}
public function getRowBg(): ?string
{
return $this->rowBg;
}
public function setRowBg(?string $rowBg): self
{
$this->rowBg = $rowBg;
return $this;
}
public function getLastCheck(): ?\DateTimeImmutable
{
return $this->lastCheck;
}
public function setLastCheck(?\DateTimeImmutable $lastCheck): self
{
$this->lastCheck = $lastCheck;
return $this;
}
/**
* @return Collection<int, Storage>
*/
public function getStorages(): Collection
{
return $this->storages;
}
public function addStorage(Storage $storage): self
{
if (!$this->storages->contains($storage)) {
$this->storages[] = $storage;
$storage->setStock($this);
}
return $this;
}
public function removeStorage(Storage $storage): self
{
if ($this->storages->removeElement($storage)) {
// set the owning side to null (unless already changed)
if ($storage->getStock() === $this) {
$storage->setStock(null);
}
}
return $this;
}
public function isChecked(): ?bool
{
return $this->checked;
}
public function setChecked(bool $checked): self
{
$this->checked = $checked;
return $this;
}
public function getTotalQuantity() : int
{
$this->totalQuantity = 0;
foreach ($this->storages as $storage) {
$this->totalQuantity += $storage->getQuantity();
}
return $this->totalQuantity;
}
public function getOrderStorage(): ?Storage
{
foreach ($this->storages as $storage) {
if($storage->getType() == 'Commande') {
return $storage;
}
}
return null;
}
public function addQuantityToStorage(Storage $storage, StorageRepository $storageRepository): void
{
$storageExists = false;
foreach($this->storages as $st) {
if (
$st->getType() == $storage->getType() &&
$st->getTruck() == $storage->getTruck()
){
$st->setQuantity($st->getQuantity() + $storage->getQuantity());
$storageRepository->remove($storage, true);
$storageExists = true;
}
}
if ($storageExists == false) {
$this->storages[] = $storage;
$storage->setStock($this);
$storageRepository->add($storage, true);
}
}
public function removeQuantityToStorage(Storage $storage): void
{
$storageExists = false;
foreach($this->storages as $st) {
if (
$st->getType() == $storage->getType() &&
$st->getTruck() == $storage->getTruck()
){
$st->setQuantity($st->getQuantity() - $storage->getQuantity());
$storageExists = true;
}
}
if (!$storageExists) {
$this->storages[] = $storage;
$storage->setStock($this);
}
}
public function getCompany(): ?string
{
return $this->company;
}
public function setCompany(?string $company): self
{
$this->company = $company;
return $this;
}
}