<?php
namespace App\Entity;
use App\Repository\ShippingRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Blameable\Traits\BlameableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: ShippingRepository::class)]
class Shipping
{
use BlameableEntity; //Hook blameable behaviour. Updates createdBy, updatedBy fields
use TimestampableEntity; //Hook timestampable behaviour. Updates createdAt, updatedAt fields
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "IDENTITY")]
#[ORM\Column]
#[Groups("serial")]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Assert\NotNull()]
#[Assert\Length(max: 255)]
#[Groups("serial")]
private ?string $name = null;
/**
* Cost of each increment ('repeatKgsNumber') after last scaleShipping
*/
#[ORM\Column]
#[Assert\NotNull()]
#[Assert\PositiveOrZero()]
private ?float $cost = null;
/**
* Size in Kgs. of each increment ('repeatKgsNumber') after last scaleShipping
*/
#[ORM\Column()]
#[Assert\PositiveOrZero()]
private ?int $repeatKgsNumber = null;
#[ORM\OneToMany(mappedBy: 'shipping', targetEntity: ShippingScale::class, cascade: ["persist", "remove"], orphanRemoval: true)]
#[ORM\OrderBy(["minWeight" => "ASC"])]
#[Assert\Count(min: 1)]
#[Assert\Valid]
private Collection $shippingScales;
#[ORM\OneToMany(mappedBy: 'shipping', targetEntity: ProductUnique::class)]
private Collection $productUniques;
#[ORM\OneToMany(mappedBy: 'shipping', targetEntity: Product::class)]
private Collection $products;
public function __construct()
{
$this->shippingScales = new ArrayCollection();
$this->productUniques = new ArrayCollection();
$this->products = new ArrayCollection();
}
/**
* Calculate Cost depending of the kgs.
*
* @param float $kgs
* @return float|null
*/
public function getCostByKgs(float $kgs): ?float
{
$cost = null;
$shippingScale = null;
//Search for the shippingScale
/** @var ShippingScale */
foreach ($this->shippingScales as $auxShippingScale) {
$shippingScale = $auxShippingScale;
if (
($kgs >= $auxShippingScale->getMinWeight()) &&
($kgs <= $auxShippingScale->getMaxWeight())
) {
$cost = $auxShippingScale->getCost();
break;
}
}
if (!$cost) {
// If shippingScale wasn't found, apply the extra Cost for each repeatKgsNumber
$differenceKgs = ceil($kgs - $shippingScale->getMaxWeight());
$cost = $shippingScale->getCost() + ($differenceKgs / $this->repeatKgsNumber * $this->cost);
}
return $cost;
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getCost(): ?float
{
return $this->cost;
}
public function setCost(?float $cost): self
{
$this->cost = $cost;
return $this;
}
public function getRepeatKgsNumber(): ?int
{
return $this->repeatKgsNumber;
}
public function setRepeatKgsNumber(?int $repeatKgsNumber): self
{
$this->repeatKgsNumber = $repeatKgsNumber;
return $this;
}
/**
* @return Collection<int, ShippingScale>
*/
public function getShippingScales(): Collection
{
return $this->shippingScales;
}
public function addShippingScale(ShippingScale $shippingScale): self
{
if (!$this->shippingScales->contains($shippingScale)) {
$this->shippingScales->add($shippingScale);
$shippingScale->setShipping($this);
}
return $this;
}
public function removeShippingScale(ShippingScale $shippingScale): self
{
if ($this->shippingScales->removeElement($shippingScale)) {
// set the owning side to null (unless already changed)
if ($shippingScale->getShipping() === $this) {
$shippingScale->setShipping(null);
}
}
return $this;
}
/**
* @return Collection<int, ProductUnique>
*/
public function getProductUniques(): Collection
{
return $this->productUniques;
}
public function addProductUnique(ProductUnique $productUnique): self
{
if (!$this->productUniques->contains($productUnique)) {
$this->productUniques->add($productUnique);
$productUnique->setShipping($this);
}
return $this;
}
public function removeProductUnique(ProductUnique $productUnique): self
{
if ($this->productUniques->removeElement($productUnique)) {
// set the owning side to null (unless already changed)
if ($productUnique->getShipping() === $this) {
$productUnique->setShipping(null);
}
}
return $this;
}
/**
* @return Collection<int, Product>
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products->add($product);
$product->setShipping($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
if ($this->products->removeElement($product)) {
// set the owning side to null (unless already changed)
if ($product->getShipping() === $this) {
$product->setShipping(null);
}
}
return $this;
}
}