src/App/Entity/Core/Product.php line 23

Open in your IDE?
  1. <?php
  2. declare (strict_types 1);
  3. namespace App\Entity\Core;
  4. use App\Entity\ProductAttribute;
  5. use App\Entity\ProductFile;
  6. use App\Entity\Tag;
  7. use App\Entity\RelatedProduct;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Sylius\Component\Core\Model\Product as BaseProduct;
  11. use Sylius\Component\Core\Model\ProductInterface;
  12. use Sylius\Component\Core\Model\ProductTaxon;
  13. use Sylius\Component\Core\Model\TaxonInterface;
  14. use Sylius\Component\Product\Model\ProductTranslationInterface as BaseProductTranslationInterface;
  15. /**
  16.  * @ORM\MappedSuperclass
  17.  * @ORM\Table(name="sylius_product")
  18.  */
  19. class Product extends BaseProduct implements ProductInterface
  20. {
  21.     public const SIMPLE   1;
  22.     public const BY_METRE 2;
  23.     public const BY_BATCH 3;
  24.     public const CUSTOM   4;
  25.     public const SESSION_IDS 'productIds';
  26.     
  27.     public const DEFAULT_TAX_CATEGORY_CODE      'iva';
  28.     public const DEFAULT_SHIPPING_CATEGORY_CODE 'shippable';
  29.     public const DEFAULT_CHANNEL_CODE           'ES_CL_WEB';
  30.     public const DEFAULT_VARIANT_SELECTION      'choice';
  31.     /**
  32.      * @var bool
  33.      *
  34.      * @ORM\Column(name="can_be_estimated", type="boolean", nullable=false)
  35.      */
  36.     private $canBeEstimated false;
  37.     /**
  38.      * @var bool
  39.      *
  40.      * @ORM\Column(name="can_be_sold", type="boolean", nullable=false)
  41.      */
  42.     private $canBeSold false;
  43.     /**
  44.      * @var string
  45.      *
  46.      * @ORM\Column(name="company", type="string", nullable=false)
  47.      */
  48.     private $company;
  49.     /**
  50.      * @ORM\ManyToMany(targetEntity="App\Entity\Tag")
  51.      */
  52.     private $tags;
  53.     /**
  54.      * @var \Doctrine\Common\Collections\Collection
  55.      *
  56.      * @ORM\OneToMany(targetEntity="App\Entity\ProductFile", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
  57.      */
  58.     private $productFiles;
  59.     /**
  60.      * @var int
  61.      *
  62.      * @ORM\Column(name="type", type="integer", nullable=false)
  63.      */
  64.     private $type;
  65.     /**
  66.      * @var \Doctrine\Common\Collections\Collection
  67.      *
  68.      * @ORM\OneToMany(targetEntity="App\Entity\ProductAttribute", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
  69.      */
  70.     private $productAttributes;
  71.     /**
  72.      * @var bool
  73.      *
  74.      * @ORM\Column(name="admin_enabled", type="boolean", nullable=false)
  75.      */
  76.     private $adminEnabled true;
  77.     /**
  78.      * @var bool
  79.      *
  80.      * @ORM\Column(name="stock_needed", type="boolean", nullable=false)
  81.      */
  82.     private $stockNeeded true;
  83.     /**
  84.      * @var bool
  85.      *
  86.      * @ORM\Column(name="by_metre", type="boolean", nullable=false)
  87.      */
  88.     private $byMetre false;
  89.     /**
  90.      * @var \Doctrine\Common\Collections\Collection
  91.      *
  92.      * @ORM\OneToMany(targetEntity="App\Entity\RelatedProduct", mappedBy="section", cascade={"persist", "remove"}, orphanRemoval=true)
  93.      */
  94.     private $relatedProducts;
  95.     public function __construct()
  96.     {
  97.         parent::__construct();
  98.         $this->tags         = new \Doctrine\Common\Collections\ArrayCollection();
  99.         $this->productFiles = new \Doctrine\Common\Collections\ArrayCollection();
  100.         $this->productAttributes = new \Doctrine\Common\Collections\ArrayCollection();
  101.         $this->relatedProducts = new \Doctrine\Common\Collections\ArrayCollection();
  102.     }
  103.     public function getCanBeEstimated(): bool
  104.     {
  105.         return $this->canBeEstimated;
  106.     }
  107.     public function setCanBeEstimated(bool $canBeEstimated): void
  108.     {
  109.         $this->canBeEstimated $canBeEstimated;
  110.     }
  111.     public function getCanBeSold(): bool
  112.     {
  113.         return $this->canBeSold;
  114.     }
  115.     public function setCanBeSold(bool $canBeSold): void
  116.     {
  117.         $this->canBeSold $canBeSold;
  118.     }
  119.     public function getCompany(): ?string
  120.     {
  121.         return $this->company;
  122.     }
  123.     public function setCompany(string $company): void
  124.     {
  125.         $this->company $company;
  126.     }
  127.     public function addTag(Tag $tag)
  128.     {
  129.         $this->tags[] = $tag;
  130.         return $this;
  131.     }
  132.     public function removeTag(Tag $tag)
  133.     {
  134.         return $this->tags->removeElement($tag);
  135.     }
  136.     public function getTags()
  137.     {
  138.         return $this->tags;
  139.     }
  140.     /**
  141.      * @return Collection|ProductFile[]
  142.      */
  143.     public function getProductFiles() : Collection
  144.     {
  145.         return $this->productFiles;
  146.     }
  147.     public function addProductFile(ProductFile $productFile): self
  148.     {
  149.         if (!$this->productFiles->contains($productFile)) {
  150.             $this->productFiles[] = $productFile;
  151.             $productFile->setProduct($this);
  152.         }
  153.         return $this;
  154.     }
  155.     public function removeProductFile(ProductFile $productFile): self
  156.     {
  157.         if ($this->productFiles->contains($productFile)) {
  158.             $this->productFiles->removeElement($productFile);
  159.             // set the owning side to null (unless already changed)
  160.             if ($productFile->getProduct() === $this) {
  161.                 $productFile->setProduct(null);
  162.             }
  163.         }
  164.         return $this;
  165.     }
  166.     public function setType(int $type): self
  167.     {
  168.         $this->type $type;
  169.         return $this;
  170.     }
  171.     public function getType(): ?int
  172.     {
  173.         return $this->type;
  174.     }
  175.     public static function getTypesList(bool $flip false): array
  176.     {
  177.         $list = [
  178.             self::SIMPLE   => 'simple',
  179.             self::BY_METRE => 'by_metre',
  180.             self::BY_BATCH => 'by_batch',
  181.             self::CUSTOM   => 'custom'
  182.         ];
  183.         return $flip array_flip($list) : $list;
  184.     }
  185.     public static function getTypeName(int $type): ?string
  186.     {
  187.         $types self::getTypesList();
  188.         $typeName $types[$type] ?? null;
  189.         return $typeName;
  190.     }
  191.     public function getCurrentTypeValue(): ?string
  192.     {
  193.         return self::getTypeName($this->type);
  194.     }
  195.     public function usesMetres(): bool
  196.     {
  197.         switch ($this->getType()) {
  198.             case self::SIMPLE:
  199.                 $usesMetres false;
  200.                 break;
  201.             case self::BY_METRE:
  202.                 $usesMetres true;
  203.                 break;
  204.             case self::BY_BATCH:
  205.                 if ($this->getByMetre()) {
  206.                     $usesMetres true;
  207.                 } else {
  208.                     $usesMetres false;
  209.                 }
  210.                 break;
  211.             case self::CUSTOM:
  212.                 if ($this->getByMetre()) {
  213.                     $usesMetres true;
  214.                 } else {
  215.                     $usesMetres false;
  216.                 }
  217.                 break;
  218.             default:
  219.                 $usesMetres false;
  220.                 break;
  221.         }
  222.         return $usesMetres;
  223.     }
  224.     public function addProductAttribute(ProductAttribute $productAttribute)
  225.     {
  226.         $productAttribute->setProduct($this);
  227.         $this->productAttributes[] = $productAttribute;
  228.         return $this;
  229.     }
  230.     public function removeProductAttribute(ProductAttribute $productAttribute)
  231.     {
  232.         return $this->productAttributes->removeElement($productAttribute);
  233.     }
  234.     public function getProductAttributes()
  235.     {
  236.         return $this->productAttributes;
  237.     }
  238.     public function getAdminEnabled(): bool
  239.     {
  240.         return $this->adminEnabled;
  241.     }
  242.     public function setAdminEnabled(bool $adminEnabled): void
  243.     {
  244.         $this->adminEnabled $adminEnabled;
  245.     }
  246.     public function getChildrenLeafOfTaxon(TaxonInterface $taxon): ?TaxonInterface
  247.     {
  248.         $result $taxon;
  249.         foreach ($this->getTaxons() as $productTaxon) {
  250.             if (!$productTaxon->hasChildren()) {
  251.                 $ancestors $productTaxon->getAncestors();
  252.                 if ($ancestors->contains($taxon)) {
  253.                     $result $productTaxon;
  254.                     break;
  255.                 }
  256.             }
  257.         }
  258.         return $result;
  259.     }
  260.     public function clearProductTaxons(): void
  261.     {
  262.         foreach ($this->getProductTaxons() as $productTaxon) {
  263.             $this->removeProductTaxon($productTaxon);
  264.         }
  265.     }
  266.     public function addProductTaxonFromTaxon(TaxonInterface $taxon): void
  267.     {
  268.         $newProductTaxon = new ProductTaxon;
  269.         $newProductTaxon->setTaxon($taxon);
  270.         $newProductTaxon->setProduct($this);
  271.         $this->addProductTaxon($newProductTaxon);
  272.     }
  273.     public function setProductTaxonsFromTaxons(array $taxons): void
  274.     {
  275.         $firstTaxon count($taxons) > $taxons[0] : null;
  276.         $this->clearProductTaxons();
  277.         foreach ($taxons as $taxon) {
  278.             $this->addProductTaxonFromTaxon($taxon);
  279.         }
  280.         if ($firstTaxon) {
  281.             $this->setMainTaxon($firstTaxon);
  282.         }
  283.     }
  284.     public function getStockNeeded(): bool
  285.     {
  286.         return $this->stockNeeded;
  287.     }
  288.     public function setStockNeeded(bool $stockNeeded): void
  289.     {
  290.         $this->stockNeeded $stockNeeded;
  291.     }
  292.     public function getByMetre(): bool
  293.     {
  294.         return $this->byMetre;
  295.     }
  296.     public function setByMetre(bool $byMetre): void
  297.     {
  298.         $this->byMetre $byMetre;
  299.     }
  300.     protected function createTranslation(): BaseProductTranslationInterface
  301.     {
  302.         return new ProductTranslation();
  303.     }
  304.     public function __toString(): string
  305.     {
  306.         return $this->code ' - ' $this->getName();
  307.     }
  308.     public function addRelatedProduct(RelatedProduct $relatedProduct)
  309.     {
  310.         $relatedProduct->setProduct($this);
  311.         $this->relatedProducts[] = $relatedProduct;
  312.         return $this;
  313.     }
  314.     public function removeRelatedProduct(RelatedProduct $relatedProduct)
  315.     {
  316.         return $this->relatedProducts->removeElement($relatedProduct);
  317.     }
  318.     public function getRelatedProducts()
  319.     {
  320.         return $this->relatedProducts;
  321.     }
  322. }