modules/XC/AutoImportBase/src/Model/ProductPriceSource.php line 23

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XC\AutoImportBase\Model;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use XC\AutoImportBase\Core\Configuration\AutoImportBaseConfiguration;
  9. use XC\AutoImportBase\Core\Data\Helper\ProductPrice\ProductPrice;
  10. use XC\AutoImportBase\Core\Data\Helper\ProductPrice\ProductPriceCalculator;
  11. use XC\AutoImportBase\Core\Traits\IntegrationSettingsTrait;
  12. use XCart\Container;
  13. /**
  14.  * Product price source
  15.  *
  16.  * @ORM\Entity
  17.  * @ORM\Table (name="auto_product_price_source")
  18.  */
  19. class ProductPriceSource extends \XLite\Model\AEntity
  20. {
  21.     use IntegrationSettingsTrait;
  22.     /**
  23.      * @ORM\Id
  24.      * @ORM\GeneratedValue (strategy="AUTO")
  25.      * @ORM\Column         (type="integer", options={"unsigned": true})
  26.      */
  27.     protected ?int $id null;
  28.     /**
  29.      * @ORM\ManyToOne  (targetEntity="XLite\Model\Product", inversedBy="priceSources")
  30.      * @ORM\JoinColumn (name="product_id", referencedColumnName="product_id", onDelete="CASCADE")
  31.      */
  32.     protected \XLite\Model\Product $product;
  33.     /**
  34.      * @ORM\Column (type="string", length=64)
  35.      */
  36.     protected string $sourceName;
  37.     /**
  38.      * @ORM\Column (type="string", length=6)
  39.      */
  40.     protected string $type;
  41.     /**
  42.      * @ORM\Column (type="string", length=3)
  43.      */
  44.     protected string $currency;
  45.     /**
  46.      * Price adjustment - percent format
  47.      *
  48.      * @ORM\Column (type="decimal", precision=14, scale=2, nullable=true)
  49.      */
  50.     protected ?float $adjustment null;
  51.     /**
  52.      * Price value
  53.      *
  54.      * @ORM\Column (type="decimal", precision=14, scale=2, nullable=true)
  55.      */
  56.     protected float $value;
  57.     /**
  58.      * @ORM\Column (type="boolean", options={"default":"0"})
  59.      */
  60.     protected bool $selected false;
  61.     /**
  62.      * Update datetime. Consider using internal datetime types
  63.      *
  64.      * @ORM\Column (type="integer")
  65.      */
  66.     protected int $updateDatetime;
  67.     /**
  68.      * @return int|null
  69.      */
  70.     public function getId(): ?int
  71.     {
  72.         return $this->id;
  73.     }
  74.     public function getProduct(): \XLite\Model\Product
  75.     {
  76.         return $this->product;
  77.     }
  78.     public function setProduct(\XLite\Model\Product $product): void
  79.     {
  80.         $this->product $product;
  81.     }
  82.     public function getSourceName(): string
  83.     {
  84.         return $this->sourceName;
  85.     }
  86.     public function setSourceName(string $sourceName): void
  87.     {
  88.         $this->sourceName $sourceName;
  89.     }
  90.     public function getType(): string
  91.     {
  92.         return $this->type;
  93.     }
  94.     public function setType(string $type): void
  95.     {
  96.         $this->type $type;
  97.     }
  98.     public function getAdjustment(): ?float
  99.     {
  100.         return $this->adjustment;
  101.     }
  102.     public function setAdjustment(?float $adjustment): void
  103.     {
  104.         $this->adjustment $adjustment;
  105.     }
  106.     public function getCurrency(): string
  107.     {
  108.         return $this->currency;
  109.     }
  110.     public function setCurrency(string $currency): void
  111.     {
  112.         $this->currency $currency;
  113.     }
  114.     public function getLimitedValue(): float
  115.     {
  116.         // TODO ECOM-6227 Leaking abstraction: ProductPrice from another layer
  117.         $calculator $this->getPriceCalculator();
  118.         $newProductPrice $calculator->limitPrice(
  119.             new ProductPrice(
  120.                 $this->getValue(),
  121.                 $this->getCurrency(),
  122.                 $this->getType(),
  123.             )
  124.         );
  125.         return $newProductPrice->getValue();
  126.     }
  127.     public function getValue(): float
  128.     {
  129.         return $this->value;
  130.     }
  131.     public function setValue(float $value): void
  132.     {
  133.         $this->value $value;
  134.     }
  135.     public function getUpdateDatetime(): int
  136.     {
  137.         return $this->updateDatetime;
  138.     }
  139.     public function setUpdateDatetime(int $updateDatetime): void
  140.     {
  141.         $this->updateDatetime $updateDatetime;
  142.     }
  143.     public function updateValuesFromAnother(ProductPriceSource $priceSourceToUpdate): void
  144.     {
  145.         $this->setValue($priceSourceToUpdate->getValue());
  146.         $this->setType($priceSourceToUpdate->getType());
  147.         $this->setCurrency($priceSourceToUpdate->getCurrency());
  148.         $this->setAdjustment($priceSourceToUpdate->getAdjustment());
  149.         $this->setUpdateDatetime($priceSourceToUpdate->getUpdateDatetime());
  150.         $this->setSelected($priceSourceToUpdate->getSelected());
  151.     }
  152.     /**
  153.      * @return bool
  154.      */
  155.     public function isSelected(): bool
  156.     {
  157.         return $this->getSelected();
  158.     }
  159.     /**
  160.      * @return bool
  161.      */
  162.     public function getSelected(): bool
  163.     {
  164.         return $this->selected;
  165.     }
  166.     /**
  167.      * @param bool $selected
  168.      */
  169.     public function setSelected(bool $selected): void
  170.     {
  171.         $this->selected $selected;
  172.     }
  173.     protected function getPriceCalculator(): ProductPriceCalculator
  174.     {
  175.         $settings $this->getIntegrationSettingsByName(
  176.             $this->getSourceName()
  177.         );
  178.         return new ProductPriceCalculator(
  179.             $this->getProduct(),
  180.             $settings->getPriceConfiguration()->getCurrencyConfigProvider(),
  181.             $this->getAutoImportBaseConfiguration()
  182.         );
  183.     }
  184.     public function getAutoImportBaseConfiguration(): AutoImportBaseConfiguration
  185.     {
  186.         /** @noinspection PhpIncompatibleReturnTypeInspection */
  187.         return Container::getContainer()?->get(AutoImportBaseConfiguration::class);
  188.     }
  189. }