<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="suppliers__suppliers")
* @ORM\Entity(repositoryClass="App\Repository\SupplierRepository")
*/
class Supplier
{
use IdTrait;
/**
* @ORM\Column(name="name", type="string")
*/
private string $name;
/**
* @ORM\Column(name="alias", type="string", unique=true)
*/
private string $alias;
/**
* @ORM\OneToOne(targetEntity="SupplierMarkup", mappedBy="supplier")
*/
private ?SupplierMarkup $supplierMarkup = null;
/**
* @var SupplierWarehouse[]|Collection
* @ORM\OneToMany(targetEntity="SupplierWarehouse", mappedBy="supplier")
*/
private $supplierWarehouses;
/**
* @var SupplierBrandDiscount[]|Collection
* @ORM\OneToMany(targetEntity="SupplierBrandDiscount", mappedBy="supplier")
*/
private $supplierBrandsDiscount;
/**
* @var SupplierMarkupRange[]|Collection
* @ORM\OneToMany(targetEntity="SupplierMarkupRange", mappedBy="supplier")
*/
private $supplierMarkupRanges;
public function __construct(string $name, string $alias)
{
$this->name = $name;
$this->alias = $alias;
$this->supplierBrandsDiscount = new ArrayCollection();
$this->supplierWarehouses = new ArrayCollection();
}
public function getName(): string
{
return $this->name;
}
public function getAlias(): string
{
return $this->alias;
}
public function getSupplierMarkup(): ?SupplierMarkup
{
return $this->supplierMarkup;
}
public function getSupplierBrandDiscount(Brand $brand): ?SupplierBrandDiscount
{
$supplierBrandsDiscount = $this->supplierBrandsDiscount->matching(Criteria::create()->where(Criteria::expr()->eq('brand', $brand)));
return $supplierBrandsDiscount->count() ? $supplierBrandsDiscount->last() : null;
}
/**
* @return SupplierWarehouse[]
*/
public function getWarehouses()
{
return $this->supplierWarehouses;
}
public function getSupplierMarkupRange(int $price): ?SupplierMarkupRange
{
$supplierMarkupRanges = $this->supplierMarkupRanges
->matching(
Criteria::create()->where(
Criteria::expr()->orX(
Criteria::expr()->andX(
Criteria::expr()->gte('rangeFrom', $price),
Criteria::expr()->lt('rangeTo', $price)
),
Criteria::expr()->andX(
Criteria::expr()->gt('rangeFrom', $price),
Criteria::expr()->lte('rangeTo', $price)
)
)
)
);
return $supplierMarkupRanges->count() ? $supplierMarkupRanges->last() : null;
}
}