<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="suppliers__items",
* uniqueConstraints={
* @ORM\UniqueConstraint(columns={"supplier_id", "product_id"}),
* })
* @ORM\Entity(repositoryClass="App\Repository\SupplierItemRepository")
*/
class SupplierItem
{
use IdTrait;
use UpdatedAtTrait;
/**
* @ORM\ManyToOne(targetEntity="Product")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private Product $product;
/**
* @ORM\ManyToOne(targetEntity="Supplier")
* @ORM\JoinColumn(name="supplier_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private Supplier $supplier;
/**
* @ORM\Column(name="price", type="integer", options={"default":0})
*/
private int $price = 0;
/**
* @ORM\Column(name="multiplicity", type="integer", options={"default":1})
*/
private int $multiplicity = 1;
/**
* @ORM\Column(name="vendor_id", type="string", nullable=true)
*/
private ?string $vendorId = null;
/**
* @var SupplierWarehouseItem[]|Collection
* @ORM\OneToMany(targetEntity="SupplierWarehouseItem", mappedBy="item")
*/
private $supplierWarehouseItems;
public function __construct(Product $product, Supplier $supplier)
{
$this->product = $product;
$this->supplier = $supplier;
$this->supplierWarehouseItems = new ArrayCollection();
}
public function getSupplier(): Supplier
{
return $this->supplier;
}
public function getProduct(): Product
{
return $this->product;
}
public function getPrice(): int
{
return $this->price;
}
public function getVendorId(): ?string
{
return $this->vendorId;
}
/**
* @return Collection|SupplierWarehouseItem[]
*/
public function getSupplierWarehouseItems()
{
return $this->supplierWarehouseItems;
}
public function calculatePrice(): int
{
$supplier = $this->getSupplier();
$supplierMarkup = $supplier->getSupplierMarkup();
$supplierBrandDiscount = $supplier->getSupplierBrandDiscount($this->getProduct()->getBrand());
$price = $this->price;
$price = $price * (1 - ($supplierBrandDiscount ? $supplierBrandDiscount->getDiscount() : 0) / 10000);
$price = $price * (1 + ($supplierMarkup ? $supplierMarkup->getMarkup() : 0) / 10000);
$supplierMarkupRangeValue = $supplier->getSupplierMarkupRange($price);
return (int)round($price * (1 + ($supplierMarkupRangeValue ? $supplierMarkupRangeValue->getMarkup() : 0) / 10000));
}
}