<?php
namespace App\Entity;
use App\Repository\StockRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=StockRepository::class)
*/
class Stock
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="integer", options={"default": 0, "comment": "Quantité physique en stock. La quantité disponible à la vente est qt_stock - qt_reserved"})
*/
private $qtStock;
/**
* @ORM\Column(type="integer")
*/
private $qtReserved;
/**
* @ORM\Column(name="incoming_quantity", type="integer", options={"default": 0, "comment": "Quantite en attente d'arrivage issue des bons d'achat fournisseur"})
*/
private int $incomingQuantity = 0;
/**
* @ORM\Column(name="incoming_date_estimated", type="datetime", nullable=true, options={"comment": "Date estimee du prochain arrivage"})
*/
private ?\DateTimeInterface $incomingDateEstimated = null;
/**
* @ORM\Column(name="exchange_incoming_quantity", type="integer", options={"default": 0, "comment": "Quantite attendue depuis des bons d echange client acceptes"})
*/
private int $exchangeIncomingQuantity = 0;
/**
* @ORM\ManyToOne(targetEntity=Produit::class, inversedBy="stocks")
*/
private $produit;
/**
* @ORM\ManyToOne(targetEntity=ProduitDeclinationValue::class, inversedBy="stocks")
*/
private $declinationProduit;
/**
* @ORM\ManyToOne(targetEntity=Warehouse::class, inversedBy="stocks")
* @ORM\JoinColumn(name="warehouse_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private $warehouse;
/**
* @ORM\Column(name="average_unit_cost", type="decimal", precision=12, scale=3, options={"default":"0.000"})
*/
private string $averageUnitCost = '0.000';
/**
* @ORM\Column(name="last_purchase_price", type="decimal", precision=12, scale=3, nullable=true)
*/
private ?string $lastPurchasePrice = null;
/**
* @ORM\Column(name="valuation_updated_at", type="datetime", nullable=true)
*/
private ?\DateTimeInterface $valuationUpdatedAt = null;
/**
* @ORM\OneToMany(targetEntity=Activity::class, mappedBy="stock")
*/
private $activities;
public function __construct()
{
$this->activities = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getQtStock(): ?int
{
return $this->qtStock;
}
public function setQtStock(int $qtStock): self
{
$this->qtStock = $qtStock;
return $this;
}
public function getQtReserved(): ?int
{
return $this->qtReserved;
}
public function setQtReserved(int $qtReserved): self
{
$this->qtReserved = $qtReserved;
return $this;
}
public function getIncomingQuantity(): int
{
return (int) $this->incomingQuantity;
}
public function setIncomingQuantity(int $incomingQuantity): self
{
$this->incomingQuantity = max(0, (int) $incomingQuantity);
return $this;
}
public function getIncomingDateEstimated(): ?\DateTimeInterface
{
return $this->incomingDateEstimated;
}
public function setIncomingDateEstimated(?\DateTimeInterface $incomingDateEstimated): self
{
$this->incomingDateEstimated = $incomingDateEstimated;
return $this;
}
public function getExchangeIncomingQuantity(): int
{
return (int) $this->exchangeIncomingQuantity;
}
public function setExchangeIncomingQuantity(int $exchangeIncomingQuantity): self
{
$this->exchangeIncomingQuantity = max(0, (int) $exchangeIncomingQuantity);
return $this;
}
public function getIncomingRemainingQuantity(): int
{
$incoming = (int) $this->getIncomingQuantity();
if ($incoming <= 0) {
return 0;
}
$realAvailable = (int) $this->getQtStock() - (int) $this->getQtReserved();
if ($realAvailable > 0) {
return $incoming;
}
$absorbedByShortage = max(0, 0 - $realAvailable);
return max(0, $incoming - $absorbedByShortage);
}
public function getAvailableQuantityForWebsite(): int
{
return max(0, ((int) $this->getQtStock() - (int) $this->getQtReserved()) + (int) $this->getIncomingQuantity());
}
public function getProduit(): ?Produit
{
return $this->produit;
}
public function setProduit(?Produit $produit): self
{
$this->produit = $produit;
return $this;
}
public function getDeclinationProduit(): ?ProduitDeclinationValue
{
return $this->declinationProduit;
}
public function setDeclinationProduit(?ProduitDeclinationValue $declinationProduit): self
{
$this->declinationProduit = $declinationProduit;
return $this;
}
public function getResolvedStorehouseName(): ?string
{
return $this->warehouse instanceof Warehouse && $this->warehouse->getName()
? (string) $this->warehouse->getName()
: null;
}
public function getWarehouse(): ?Warehouse
{
return $this->warehouse;
}
public function setWarehouse(?Warehouse $warehouse): self
{
$this->warehouse = $warehouse;
return $this;
}
public function getAverageUnitCost(): ?float
{
return (float) $this->averageUnitCost;
}
public function setAverageUnitCost(?float $averageUnitCost): self
{
$this->averageUnitCost = number_format((float) ($averageUnitCost ?? 0), 3, '.', '');
return $this;
}
public function getLastPurchasePrice(): ?float
{
return $this->lastPurchasePrice !== null ? (float) $this->lastPurchasePrice : null;
}
public function setLastPurchasePrice(?float $lastPurchasePrice): self
{
$this->lastPurchasePrice = $lastPurchasePrice !== null
? number_format((float) $lastPurchasePrice, 3, '.', '')
: null;
return $this;
}
public function getValuationUpdatedAt(): ?\DateTimeInterface
{
return $this->valuationUpdatedAt;
}
public function setValuationUpdatedAt(?\DateTimeInterface $valuationUpdatedAt): self
{
$this->valuationUpdatedAt = $valuationUpdatedAt;
return $this;
}
/**
* @return Collection|Activity[]
*/
public function getActivities(): Collection
{
return $this->activities;
}
public function addActivity(Activity $activity): self
{
if( !$this->activities->contains($activity)) {
$this->activities[] = $activity;
$activity->setStock($this);
}
return $this;
}
public function removeActivity(Activity $activity): self
{
if( $this->activities->removeElement($activity)) {
// set the owning side to null (unless already changed)
if( $activity->getStock() === $this) {
$activity->setStock(null);
}
}
return $this;
}
public function __toString() {
return $this->id;
}
}