<?php
declare(strict_types=1);
namespace App\Entity\Product;
use App\Entity\Customer\CustomerInterface;
use App\Entity\Trait\TierPriceableTrait;
use Brille24\SyliusTierPricePlugin\Entity\ProductVariantInterface;
use Sylius\Component\Core\Model\ProductVariant as BaseProductVariant;
use Brille24\SyliusTierPricePlugin\Entity\TierPriceInterface;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Product\Model\ProductVariantTranslationInterface;
/**
* @ORM\Entity
* @ORM\Table(name="sylius_product_variant")
*/
class ProductVariant extends BaseProductVariant implements ProductVarinatInterface, ProductVariantInterface
{
use TierPriceableTrait;
/**
* @ORM\Column(name="pack_units", type="integer", nullable=true)
*/
private ?int $packUnits = null;
/**
* @ORM\Column(name="pack_length", type="decimal", precision=10, scale=3, nullable=true)
*/
private ?string $packLength = null;
/**
* @ORM\Column(name="pack_width", type="decimal", precision=10, scale=3, nullable=true)
*/
private ?string $packWidth = null;
/**
* @ORM\Column(name="pack_height", type="decimal", precision=10, scale=3, nullable=true)
*/
private ?string $packHeight = null;
/**
* @ORM\Column(name="pack_weight", type="decimal", precision=10, scale=3, nullable=true)
*/
private ?string $packWeight = null;
public function __construct()
{
parent::__construct();
$this->initTierPriceableTrait();
}
protected function createTranslation(): ProductVariantTranslationInterface
{
return new ProductVariantTranslation();
}
public function getMainImage()
{
foreach ($this->images as $key => $value) {
if ($value->getType === 'main') {
return $value;
}
}
return null;
}
public function getTierPriceByQty(int $qty): ?TierPriceInterface
{
foreach ($this->getTierPrices() as $item) {
if ($item->getQty() === $qty) {
return $item;
}
}
return null;
}
public function getMinQtyFromTierprice($channel): ?int
{
$min = null;
foreach ($this->getTierPricesForChannel($channel) as $tierprice) {
if ($tierprice->getQty() < $min || $min === null) {
$min = $tierprice->getQty();
}
}
return $min;
}
public function getBreakpoints(ChannelInterface $channel, ?CustomerInterface $customer = null): array
{
$out = [];
$tierPrices = $this->getTierPricesForChannel($channel, $customer = null);
$ovs = $this->getOptionValues();
// if (count($ovs) === 1) {
foreach ($tierPrices as $tierPrice) {
$qty = $tierPrice->getQty();
$out[$qty] = $qty;
}
// }
if (is_array($out)) {
ksort($out);
}
return $out;
}
public function getOptionValueByCode(string $code)
{
foreach ($this->getOptionValues() as $optionValue) {
if ($optionValue->getCode() === $code) {
return $optionValue;
}
}
return null;
}
public function getOptionValueByOptionCode(string $code)
{
foreach ($this->getOptionValues() as $optionValue) {
if ($optionValue->getOption()->getCode() === $code) {
return $optionValue;
}
}
return null;
}
public function __toString(): string
{
return $this->name ?? 'Variant #' . $this->getId(); // Provide a fallback
}
public function getPackUnits(): ?int
{
return $this->packUnits;
}
public function setPackUnits(?int $packUnits): void
{
$this->packUnits = $packUnits;
}
/** Dimensions in IN */
public function getPackLength(): ?float
{
return null === $this->packLength ? null : (float) $this->packLength;
}
public function setPackLength(?float $packLength): void
{
$this->packLength = null === $packLength ? null : number_format($packLength, 3, '.', '');
}
public function getPackWidth(): ?float
{
return null === $this->packWidth ? null : (float) $this->packWidth;
}
public function setPackWidth(?float $packWidth): void
{
$this->packWidth = null === $packWidth ? null : number_format($packWidth, 3, '.', '');
}
public function getPackHeight(): ?float
{
return null === $this->packHeight ? null : (float) $this->packHeight;
}
public function setPackHeight(?float $packHeight): void
{
$this->packHeight = null === $packHeight ? null : number_format($packHeight, 3, '.', '');
}
/** Weight in LBS */
public function getPackWeight(): ?float
{
return null === $this->packWeight ? null : (float) $this->packWeight;
}
public function setPackWeight(?float $packWeight): void
{
$this->packWeight = null === $packWeight ? null : number_format($packWeight, 3, '.', '');
}
}