src/ShoppingBundle/Entity/Product.php line 45

Open in your IDE?
  1. <?php
  2. namespace Shopping\Entity;
  3. use Core\Entity\Enum;
  4. use Core\Entity\Traits\PageTrait;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Persistence\ObjectManagerAware;
  7. use Core\Entity\Traits\EntityTrait;
  8. use Core\Entity\Traits\TranslatableTrait;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Gedmo\Mapping\Annotation as Gedmo;
  11. use Symfony\Component\HttpFoundation\File\File;
  12. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  13. /**
  14.  * Enumération des redirection de produit
  15.  */
  16. abstract class ProductRedirect extends Enum
  17. {
  18.     const NO_REDIRECTION 0;
  19.     const TEMPORARY_PRODUCT 1;
  20.     const PERMANENT_PRODUCT 2;
  21. //    const TEMPORARY_CATEGORY = 3;
  22. //    const PERMANENT_CATEGORY = 4;
  23.     /**
  24.      * Nom d'affichage
  25.      */
  26.     protected static $typeName = [
  27.         self::NO_REDIRECTION => 'Aucune redirection (404)',
  28.         self::TEMPORARY_PRODUCT => 'Temporaire (302)',
  29.         self::PERMANENT_PRODUCT => 'Permanente (301)',
  30. //        self::TEMPORARY_CATEGORY => 'Temporaire vers une catégorie (301)',
  31. //        self::PERMANENT_CATEGORY => 'Permanent vers une catégorie (301)',
  32.     ];
  33. }
  34. /**
  35.  * @Vich\Uploadable
  36.  * @ORM\Entity(repositoryClass="Shopping\Repository\ProductRepository")
  37.  * @ORM\Table(name="shopping_product")
  38.  */
  39. class Product implements ObjectManagerAware
  40. {
  41.     use TranslatableTrait;
  42.     use EntityTrait {
  43.         EntityTrait::__construct as private __entityConstruct;
  44.     }
  45.     use PageTrait {
  46.         PageTrait::__construct as private __pageConstruct;
  47.     }
  48.     /**
  49.      * @ORM\Column(type="string", length=255)
  50.      */
  51.     private $reference;
  52.     /**
  53.      * @ORM\Column(type="integer", nullable=true)
  54.      */
  55.     private $online_only;
  56.     /**
  57.      * Code-barre
  58.      *
  59.      * @ORM\Column(type="string", length=255, nullable=true)
  60.      */
  61.     private $ean13;
  62.     /**
  63.      *
  64.      * @ORM\Column(type="boolean", nullable=true)
  65.      */
  66.     private $use_stock;
  67.     /**
  68.      *
  69.      * @ORM\Column(type="integer", nullable=true)
  70.      */
  71.     private $stock;
  72.     /**
  73.      * @ORM\Column(type="integer", nullable=true)
  74.      */
  75.     private $minimal_quantity;
  76.     /**
  77.      * @ORM\Column(type="integer", nullable=true)
  78.      */
  79.     private $out_of_stock;
  80.     /**
  81.      * @ORM\Column(type="float", nullable=true)
  82.      */
  83.     private $weight;
  84.     /**
  85.      * @ORM\Column(type="float", nullable=true)
  86.      */
  87.     private $weight_price_unit;
  88.     /**
  89.      * @ORM\Column(type="string", length=255, nullable=true)
  90.      */
  91.     private $weight_price_unit_type;
  92.     /**
  93.      * @ORM\Column(type="float", nullable=true)
  94.      */
  95.     private $width;
  96.     /**
  97.      * @ORM\Column(type="float", nullable=true)
  98.      */
  99.     private $height;
  100.     /**
  101.      * @ORM\Column(type="float", nullable=true)
  102.      */
  103.     private $depth;
  104.     /**
  105.      * @ORM\Column(type="float", nullable=true)
  106.      */
  107.     private $price_ht;
  108.     /**
  109.      * @ORM\Column(type="string", length=255, nullable=true)
  110.      */
  111.     private $ugs;
  112.     /**
  113.      * @ORM\Column(type="integer", nullable=true)
  114.      */
  115.     private $redirect;
  116.     /**
  117.      * @ORM\Column(type="string", length=255, nullable=true)
  118.      */
  119.     private $redirect_link;
  120.     /**
  121.      * @ORM\Column(type="integer", nullable=true)
  122.      */
  123.     private $available_for_order;
  124.     /**
  125.      * @ORM\Column(type="string", length=255, nullable=true)
  126.      */
  127.     private $available_date;
  128.     /**
  129.      * Neuf / Occasion / ...
  130.      *
  131.      * @ORM\Column(type="string", length=255, nullable=true)
  132.      */
  133.     private $condition_type;
  134.     /**
  135.      * @ORM\ManyToMany(targetEntity="Category", inversedBy="products")
  136.      */
  137.     private $categories;
  138.     /**
  139.      * @ORM\ManyToMany(targetEntity="Product")
  140.      */
  141.     private $related_products;
  142.     /**
  143.      * @ORM\ManyToOne(targetEntity="Tax", inversedBy="products")
  144.      * @ORM\JoinColumn(name="tax", referencedColumnName="id", nullable=true)
  145.      */
  146.     private $tax;
  147.     /**
  148.      * @Gedmo\Slug(fields={"reference"})
  149.      * @ORM\Column(length=128, unique=true)
  150.      */
  151.     private $slug;
  152.     /**
  153.      * @ORM\OneToMany(targetEntity="Shopping\Entity\PageImage", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
  154.      */
  155.     private $images;
  156.     /**
  157.      * @ORM\OneToMany(targetEntity="Shopping\Entity\ProductSpecificPrice", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
  158.      */
  159.     private $specific_prices;
  160.     /**
  161.      * @ORM\OneToMany(targetEntity="Shopping\Entity\ProductFeature", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
  162.      */
  163.     private $features;
  164.     /**
  165.      * @ORM\OneToMany(targetEntity="Shopping\Entity\ProductDeclinaison", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
  166.      */
  167.     private $declinaisons;
  168.     /**
  169.      * @ORM\OneToMany(targetEntity="Shopping\Entity\ProductFaq", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
  170.      */
  171.     private $faqs;
  172.     /**
  173.      * @ORM\Column(type="string", length=255, nullable=true)
  174.      * @var string
  175.      */
  176.     private $cover;
  177.     /**
  178.      * @Vich\UploadableField(mapping="images", fileNameProperty="cover")
  179.      * @var File
  180.      */
  181.     private $coverFile;
  182.     /**
  183.      * @ORM\Column(type="string", length=255, nullable=true)
  184.      * @var string
  185.      */
  186.     private $image;
  187.     /**
  188.      * @Vich\UploadableField(mapping="images", fileNameProperty="image")
  189.      * @var File
  190.      */
  191.     private $imageFile;
  192.     /**
  193.      * @ORM\Column(type="integer", nullable=true)
  194.      * @var string
  195.      */
  196.     private $expedition_delay;
  197.     /**
  198.      * @ORM\OneToMany(targetEntity="Shopping\Entity\CartProduct", mappedBy="product", cascade={"persist"}, orphanRemoval=true)
  199.      */
  200.     private $cart_products;
  201.     /**
  202.      * @ORM\OneToMany(targetEntity="User\Entity\UserFavoris", mappedBy="product", cascade={"persist"}, orphanRemoval=true)
  203.      */
  204.     private $user_favoris;
  205.     private $collections = [
  206.         'image''feature''specificprice''faq''declinaison'
  207.     ];
  208.     /**
  209.      * Constructeur
  210.      * @throws \Exception
  211.      */
  212.     public function __construct()
  213.     {
  214.         $this->__entityConstruct();
  215.         $this->__pageConstruct();
  216.         $this->categories = new ArrayCollection();
  217.         $this->related_products = new ArrayCollection();
  218.         $this->setReference(uniqid());
  219.         $this->setStock(1);
  220.         $this->setUseStock(false);
  221.         $this->setOnlineOnly(0);
  222.         $this->setMinimalQuantity(1);
  223.         $this->setOutOfStock(0);
  224.         $this->setPriceHT(0);
  225.         $this->setAvailableForOrder(1);
  226.         $this->specific_prices = new ArrayCollection();
  227.         $this->images = new ArrayCollection();
  228.         $this->features = new ArrayCollection();
  229.         $this->faqs = new ArrayCollection();
  230.         $this->declinaisons = new ArrayCollection();
  231.     }
  232.     public function getCollections()
  233.     {
  234.         return $this->collections;
  235.     }
  236.     public function getUseStock()
  237.     {
  238.         return $this->use_stock;
  239.     }
  240.     public function setUseStock($use_stock)
  241.     {
  242.         $this->use_stock $use_stock;
  243.     }
  244.     public function getUgs()
  245.     {
  246.         return $this->ugs;
  247.     }
  248.     /**
  249.      * @param mixed $ugs
  250.      */
  251.     public function setUgs($ugs): void
  252.     {
  253.         $this->ugs $ugs;
  254.     }
  255.     /**
  256.      * Collection - Prix spécifiques
  257.      */
  258.     public function getSpecificPrices()
  259.     {
  260.         return $this->specific_prices;
  261.     }
  262.     public function setSpecificPrices($items)
  263.     {
  264.         $this->specific_prices $items;
  265.         return $this;
  266.     }
  267.     public function addSpecificPrice($item)
  268.     {
  269.         $this->specific_prices->add($item);
  270.         $item->setProduct($this);
  271.         return $this;
  272.     }
  273.     public function removeSpecificPrice($item)
  274.     {
  275.         $this->specific_prices->removeElement($item);
  276.         return $this;
  277.     }
  278.     /**
  279.      * Collection - Images
  280.      */
  281.     public function getImages()
  282.     {
  283.         return $this->images;
  284.     }
  285.     public function setImages($items)
  286.     {
  287.         $this->images $items;
  288.         return $this;
  289.     }
  290.     public function addImage($item)
  291.     {
  292.         $this->images->add($item);
  293.         $item->setProduct($this);
  294.         return $this;
  295.     }
  296.     public function removeImage($item)
  297.     {
  298.         $this->images->removeElement($item);
  299.         return $this;
  300.     }
  301.     /**
  302.      * Collection - Features
  303.      */
  304.     public function getFeatures()
  305.     {
  306.         $array $this->features->toArray();
  307.         usort($array, function ($a$b) {
  308.             return strcmp($a->getSortorder(), $b->getSortorder());
  309.         });
  310.         $collection = new ArrayCollection();
  311.         foreach ($array as $item) {
  312.             $collection->add($item);
  313.         }
  314.         return $collection;
  315.     }
  316.     public function setFeatures($items)
  317.     {
  318.         $this->features $items;
  319.         return $this;
  320.     }
  321.     public function addFeature($item)
  322.     {
  323.         $this->features->add($item);
  324.         $item->setProduct($this);
  325.         return $this;
  326.     }
  327.     public function removeFeature($item)
  328.     {
  329.         $this->features->removeElement($item);
  330.         return $this;
  331.     }
  332.     /**
  333.      * Collection - Declinaisons
  334.      */
  335.     public function getDeclinaisons()
  336.     {
  337.         $array $this->declinaisons->toArray();
  338.         usort($array, function ($a$b) {
  339.             return $a->getSortorder() > $b->getSortorder();
  340.         });
  341.         $collection = new ArrayCollection();
  342.         foreach ($array as $item) {
  343.             $collection->add($item);
  344.         }
  345.         return $collection;
  346.     }
  347.     public function setDeclinaisons($items)
  348.     {
  349.         $this->declinaisons $items;
  350.         return $this;
  351.     }
  352.     public function addDeclinaison($item)
  353.     {
  354.         $this->declinaisons->add($item);
  355.         $item->setProduct($this);
  356.         return $this;
  357.     }
  358.     public function removeDeclinaison($item)
  359.     {
  360.         $this->declinaisons->removeElement($item);
  361.         return $this;
  362.     }
  363.     /**
  364.      * Collection - Faqs
  365.      */
  366.     public function getFaqs()
  367.     {
  368.         $array $this->faqs->toArray();
  369.         usort($array, function ($a$b) {
  370.             return strcmp($a->getSortorder(), $b->getSortorder());
  371.         });
  372.         $collection = new ArrayCollection();
  373.         foreach ($array as $item) {
  374.             $collection->add($item);
  375.         }
  376.         return $collection;
  377.     }
  378.     public function setFaqs($items)
  379.     {
  380.         $this->faqs $items;
  381.         return $this;
  382.     }
  383.     public function addFaq($item)
  384.     {
  385.         $this->faqs->add($item);
  386.         $item->setProduct($this);
  387.         return $this;
  388.     }
  389.     public function removeFaq($item)
  390.     {
  391.         $this->faqs->removeElement($item);
  392.         return $this;
  393.     }
  394.     /**
  395.      * @return string
  396.      */
  397.     public function getCover()
  398.     {
  399.         if($this->cover == ''){
  400.             return 'no_image.png';
  401.         }
  402.         return $this->cover;
  403.     }
  404.     /**
  405.      * @param string $cover
  406.      */
  407.     public function setCover($cover)
  408.     {
  409.         $this->cover $cover;
  410.     }
  411.     /**
  412.      * @return File
  413.      */
  414.     public function getCoverFile()
  415.     {
  416.         return $this->coverFile;
  417.     }
  418.     /**
  419.      * @param File $coverFile
  420.      */
  421.     public function setCoverFile($coverFile)
  422.     {
  423.         $this->coverFile $coverFile;
  424.         if ($coverFile) {
  425.             $this->setUpdatedAt(new \DateTime('now'));
  426.         }
  427.     }
  428. //    public function getCover()
  429. //    {
  430. //        $images = $this->getImages();
  431. //
  432. //        foreach ($images as $item) {
  433. //            if ($item->getIsCover()) {
  434. //                return $item;
  435. //            }
  436. //        }
  437. //
  438. //        return null;
  439. //    }
  440.     /**
  441.      * @return mixed
  442.      */
  443.     public function getMinimalQuantity()
  444.     {
  445.         return $this->minimal_quantity;
  446.     }
  447.     /**
  448.      * @param mixed $minimal_quantity
  449.      */
  450.     public function setMinimalQuantity($minimal_quantity): void
  451.     {
  452.         $this->minimal_quantity $minimal_quantity;
  453.     }
  454.     public function getOutOfStock()
  455.     {
  456.         return $this->out_of_stock;
  457.     }
  458.     public function setOutOfStock($out_of_stock)
  459.     {
  460.         $this->out_of_stock $out_of_stock;
  461.     }
  462.     public function getAvailableForOrder()
  463.     {
  464.         return $this->available_for_order;
  465.     }
  466.     public function setAvailableForOrder($available_for_order)
  467.     {
  468.         $this->available_for_order $available_for_order;
  469.     }
  470.     public function getStock()
  471.     {
  472.         return $this->stock;
  473.     }
  474.     public function setStock($stock)
  475.     {
  476.         $this->stock $stock;
  477.     }
  478.     public function getWeight()
  479.     {
  480.         return $this->weight;
  481.     }
  482.     public function setWeight($weight)
  483.     {
  484.         $this->weight $weight;
  485.     }
  486.     public function getConditionType()
  487.     {
  488.         return $this->condition_type;
  489.     }
  490.     public function setConditionType($conditionType)
  491.     {
  492.         $this->condition_type $conditionType;
  493.     }
  494.     public function getWeightPriceUnit()
  495.     {
  496.         return $this->weight_price_unit;
  497.     }
  498.     public function setWeightPriceUnit($weight_price_unit): void
  499.     {
  500.         $this->weight_price_unit $weight_price_unit;
  501.     }
  502.     public function getWeightPriceUnitType()
  503.     {
  504.         return $this->weight_price_unit_type;
  505.     }
  506.     public function setWeightPriceUnitType($weight_price_unit_type): void
  507.     {
  508.         $this->weight_price_unit_type $weight_price_unit_type;
  509.     }
  510.     public function getWidth()
  511.     {
  512.         return $this->width;
  513.     }
  514.     public function setWidth($width)
  515.     {
  516.         $this->width $width;
  517.     }
  518.     public function getHeight()
  519.     {
  520.         return $this->height;
  521.     }
  522.     public function setHeight($height)
  523.     {
  524.         $this->height $height;
  525.     }
  526.     public function getDepth()
  527.     {
  528.         return $this->depth;
  529.     }
  530.     public function setDepth($depth)
  531.     {
  532.         $this->depth $depth;
  533.     }
  534.     public function getReference()
  535.     {
  536.         return $this->reference;
  537.     }
  538.     public function setReference($reference): void
  539.     {
  540.         $this->reference $reference;
  541.     }
  542.     public function getPriceHt()
  543.     {
  544.         return $this->price_ht;
  545.     }
  546.     public function setPriceHT($price_ht): void
  547.     {
  548.         $this->price_ht $price_ht;
  549.     }
  550.     public function getAvailableDate()
  551.     {
  552.         return $this->available_date;
  553.     }
  554.     public function setAvailableDate($available_date)
  555.     {
  556.         $this->available_date $available_date;
  557.     }
  558.     public function getRedirect()
  559.     {
  560.         return $this->redirect;
  561.     }
  562.     public function setRedirect($redirect): void
  563.     {
  564.         $this->redirect $redirect;
  565.     }
  566.     public function getRedirectLink()
  567.     {
  568.         return $this->redirect_link;
  569.     }
  570.     public function setRedirectLink($redirect_link): void
  571.     {
  572.         $this->redirect_link $redirect_link;
  573.     }
  574.     public function getCategories()
  575.     {
  576.         return $this->categories;
  577.     }
  578.     public function addCategory(Category $item)
  579.     {
  580.         if ($this->categories->contains($item)) {
  581.             return;
  582.         }
  583.         $this->categories->add($item);
  584.         $item->addProduct($this);
  585.     }
  586.     public function getRelatedProducts()
  587.     {
  588.         return $this->related_products;
  589.     }
  590.     public function addRelatedProduct(Product $item)
  591.     {
  592.         $this->related_products[] = $item;
  593.         $item->addRelatedProduct($this);
  594.     }
  595.     public function getTax()
  596.     {
  597.         return $this->tax;
  598.     }
  599.     public function setTax($tax): void
  600.     {
  601.         $this->tax $tax;
  602.     }
  603.     public function getEan13()
  604.     {
  605.         return $this->ean13;
  606.     }
  607.     public function setEan13($ean13)
  608.     {
  609.         $this->ean13 $ean13;
  610.     }
  611.     public function getOnlineOnly()
  612.     {
  613.         return $this->online_only;
  614.     }
  615.     public function setOnlineOnly($online_only)
  616.     {
  617.         $this->online_only $online_only;
  618.     }
  619.     /**
  620.      * Retourne L'URL personnalisé si elle existe sinon le slug par defaut
  621.      */
  622.     public function getSlug()
  623.     {
  624.         if ($this->getUri() != '' && $this->getUri() != '/') {
  625.             $slug $this->getUri();
  626.         } else {
  627.             $slug '/' $this->slug;
  628.         }
  629.         return substr_replace($slug'/' $this->getId() . '-'01);
  630.     }
  631.     /**
  632.      * Retourne toute la file de catégorie parents
  633.      */
  634.     public function getParentsCategory()
  635.     {
  636.         $categories $this->getCategories();
  637.         $category null;
  638.         if (count($categories) > 0) {
  639.             $category $categories[0];
  640.         }
  641.         if ($category != null) {
  642.             return array_merge([$category], $category->getParentsCategory());
  643.         } else {
  644.             return [];
  645.         }
  646.     }
  647.     public function getExpeditionDelay()
  648.     {
  649.         return (int)$this->expedition_delay;
  650.     }
  651.     public function setExpeditionDelay($expedition_delay)
  652.     {
  653.         $this->expedition_delay $expedition_delay;
  654.     }
  655.     public function getExpeditionDateFromNow()
  656.     {
  657.         if ($this->getExpeditionDelay() > 0) {
  658.             $last_hour_to_order 18;
  659.             $expedition_date = new \DateTime('NOW');
  660.             // L'heure d'aujourd'hui > 18
  661.             if ($expedition_date->format('H') >= $last_hour_to_order) {
  662.                 $expedition_date->modify('+1 day');
  663.             }
  664.             // On compte les jours ouvrables
  665.             for ($i 1$i <= $this->getExpeditionDelay(); $i++) {
  666.                 // Jours normal
  667.                 $expedition_date $expedition_date->modify('+1 day');
  668.                 while(!$this->checkDateOuvrable($expedition_date)){
  669.                     $expedition_date $expedition_date->modify('+1 day');
  670.                 }
  671.             }
  672.             return $expedition_date->format('d/m/Y');
  673.         }
  674.         return '';
  675.     }
  676.     // Vérifie si la date est uvrable
  677.     private function checkDateOuvrable($expedition_date)
  678.     {
  679.         $currentDay = (int) $expedition_date->format('w');
  680.         if ($currentDay == 1) { // Lundi
  681.         } elseif ($currentDay == 2) { // Mardi
  682.         } elseif ($currentDay == 3) { // Mecredi
  683.         } elseif ($currentDay == 3) { // Jeudi
  684.         } elseif ($currentDay == 4) { // Vendredi
  685.         } elseif ($currentDay == 5) { // Samedi
  686.             return false;
  687.         } elseif ($currentDay == 6) { // Dimanche
  688.             return false;
  689.         }
  690.         return true;
  691.     }
  692.     /**
  693.      * @return string
  694.      */
  695.     public function getImage()
  696.     {
  697.         return $this->image;
  698.     }
  699.     /**
  700.      * @param string $image
  701.      */
  702.     public function setImage($image)
  703.     {
  704.         $this->image $image;
  705.     }
  706.     /**
  707.      * @return File
  708.      */
  709.     public function getImageFile()
  710.     {
  711.         return $this->imageFile;
  712.     }
  713.     /**
  714.      * @param File $imageFile
  715.      * @throws \Exception
  716.      */
  717.     public function setImageFile($imageFile)
  718.     {
  719.         $this->imageFile $imageFile;
  720.         if ($imageFile) {
  721.             $this->setUpdatedAt(new \DateTime('now'));
  722.         }
  723.     }
  724.     public function __toString()
  725.     {
  726.         return (string)$this->getDesignation();
  727.     }
  728. }