<?php
/**
* Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
* See https://www.x-cart.com/license-agreement.html for license details.
*/
namespace XC\AutoImportBase\Model;
use Doctrine\ORM\Mapping as ORM;
use XC\AutoImportBase\Core\Configuration\AutoImportBaseConfiguration;
use XC\AutoImportBase\Core\Data\Helper\ProductPrice\ProductPrice;
use XC\AutoImportBase\Core\Data\Helper\ProductPrice\ProductPriceCalculator;
use XC\AutoImportBase\Core\Traits\IntegrationSettingsTrait;
use XCart\Container;
/**
* Product price source
*
* @ORM\Entity
* @ORM\Table (name="auto_product_price_source")
*/
class ProductPriceSource extends \XLite\Model\AEntity
{
use IntegrationSettingsTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue (strategy="AUTO")
* @ORM\Column (type="integer", options={"unsigned": true})
*/
protected ?int $id = null;
/**
* @ORM\ManyToOne (targetEntity="XLite\Model\Product", inversedBy="priceSources")
* @ORM\JoinColumn (name="product_id", referencedColumnName="product_id", onDelete="CASCADE")
*/
protected \XLite\Model\Product $product;
/**
* @ORM\Column (type="string", length=64)
*/
protected string $sourceName;
/**
* @ORM\Column (type="string", length=6)
*/
protected string $type;
/**
* @ORM\Column (type="string", length=3)
*/
protected string $currency;
/**
* Price adjustment - percent format
*
* @ORM\Column (type="decimal", precision=14, scale=2, nullable=true)
*/
protected ?float $adjustment = null;
/**
* Price value
*
* @ORM\Column (type="decimal", precision=14, scale=2, nullable=true)
*/
protected float $value;
/**
* @ORM\Column (type="boolean", options={"default":"0"})
*/
protected bool $selected = false;
/**
* Update datetime. Consider using internal datetime types
*
* @ORM\Column (type="integer")
*/
protected int $updateDatetime;
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
public function getProduct(): \XLite\Model\Product
{
return $this->product;
}
public function setProduct(\XLite\Model\Product $product): void
{
$this->product = $product;
}
public function getSourceName(): string
{
return $this->sourceName;
}
public function setSourceName(string $sourceName): void
{
$this->sourceName = $sourceName;
}
public function getType(): string
{
return $this->type;
}
public function setType(string $type): void
{
$this->type = $type;
}
public function getAdjustment(): ?float
{
return $this->adjustment;
}
public function setAdjustment(?float $adjustment): void
{
$this->adjustment = $adjustment;
}
public function getCurrency(): string
{
return $this->currency;
}
public function setCurrency(string $currency): void
{
$this->currency = $currency;
}
public function getLimitedValue(): float
{
// TODO ECOM-6227 Leaking abstraction: ProductPrice from another layer
$calculator = $this->getPriceCalculator();
$newProductPrice = $calculator->limitPrice(
new ProductPrice(
$this->getValue(),
$this->getCurrency(),
$this->getType(),
)
);
return $newProductPrice->getValue();
}
public function getValue(): float
{
return $this->value;
}
public function setValue(float $value): void
{
$this->value = $value;
}
public function getUpdateDatetime(): int
{
return $this->updateDatetime;
}
public function setUpdateDatetime(int $updateDatetime): void
{
$this->updateDatetime = $updateDatetime;
}
public function updateValuesFromAnother(ProductPriceSource $priceSourceToUpdate): void
{
$this->setValue($priceSourceToUpdate->getValue());
$this->setType($priceSourceToUpdate->getType());
$this->setCurrency($priceSourceToUpdate->getCurrency());
$this->setAdjustment($priceSourceToUpdate->getAdjustment());
$this->setUpdateDatetime($priceSourceToUpdate->getUpdateDatetime());
$this->setSelected($priceSourceToUpdate->getSelected());
}
/**
* @return bool
*/
public function isSelected(): bool
{
return $this->getSelected();
}
/**
* @return bool
*/
public function getSelected(): bool
{
return $this->selected;
}
/**
* @param bool $selected
*/
public function setSelected(bool $selected): void
{
$this->selected = $selected;
}
protected function getPriceCalculator(): ProductPriceCalculator
{
$settings = $this->getIntegrationSettingsByName(
$this->getSourceName()
);
return new ProductPriceCalculator(
$this->getProduct(),
$settings->getPriceConfiguration()->getCurrencyConfigProvider(),
$this->getAutoImportBaseConfiguration()
);
}
public function getAutoImportBaseConfiguration(): AutoImportBaseConfiguration
{
/** @noinspection PhpIncompatibleReturnTypeInspection */
return Container::getContainer()?->get(AutoImportBaseConfiguration::class);
}
}