<?php
namespace Shopping\Entity;
use Core\Entity\Enum;
use Core\Entity\Traits\PageTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Persistence\ObjectManagerAware;
use Core\Entity\Traits\EntityTrait;
use Core\Entity\Traits\TranslatableTrait;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Enumération des redirection de produit
*/
abstract class ProductRedirect extends Enum
{
const NO_REDIRECTION = 0;
const TEMPORARY_PRODUCT = 1;
const PERMANENT_PRODUCT = 2;
// const TEMPORARY_CATEGORY = 3;
// const PERMANENT_CATEGORY = 4;
/**
* Nom d'affichage
*/
protected static $typeName = [
self::NO_REDIRECTION => 'Aucune redirection (404)',
self::TEMPORARY_PRODUCT => 'Temporaire (302)',
self::PERMANENT_PRODUCT => 'Permanente (301)',
// self::TEMPORARY_CATEGORY => 'Temporaire vers une catégorie (301)',
// self::PERMANENT_CATEGORY => 'Permanent vers une catégorie (301)',
];
}
/**
* @Vich\Uploadable
* @ORM\Entity(repositoryClass="Shopping\Repository\ProductRepository")
* @ORM\Table(name="shopping_product")
*/
class Product implements ObjectManagerAware
{
use TranslatableTrait;
use EntityTrait {
EntityTrait::__construct as private __entityConstruct;
}
use PageTrait {
PageTrait::__construct as private __pageConstruct;
}
/**
* @ORM\Column(type="string", length=255)
*/
private $reference;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $online_only;
/**
* Code-barre
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $ean13;
/**
*
* @ORM\Column(type="boolean", nullable=true)
*/
private $use_stock;
/**
*
* @ORM\Column(type="integer", nullable=true)
*/
private $stock;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $minimal_quantity;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $out_of_stock;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $weight;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $weight_price_unit;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $weight_price_unit_type;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $width;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $height;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $depth;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $price_ht;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $ugs;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $redirect;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $redirect_link;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $available_for_order;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $available_date;
/**
* Neuf / Occasion / ...
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $condition_type;
/**
* @ORM\ManyToMany(targetEntity="Category", inversedBy="products")
*/
private $categories;
/**
* @ORM\ManyToMany(targetEntity="Product")
*/
private $related_products;
/**
* @ORM\ManyToOne(targetEntity="Tax", inversedBy="products")
* @ORM\JoinColumn(name="tax", referencedColumnName="id", nullable=true)
*/
private $tax;
/**
* @Gedmo\Slug(fields={"reference"})
* @ORM\Column(length=128, unique=true)
*/
private $slug;
/**
* @ORM\OneToMany(targetEntity="Shopping\Entity\PageImage", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $images;
/**
* @ORM\OneToMany(targetEntity="Shopping\Entity\ProductSpecificPrice", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $specific_prices;
/**
* @ORM\OneToMany(targetEntity="Shopping\Entity\ProductFeature", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $features;
/**
* @ORM\OneToMany(targetEntity="Shopping\Entity\ProductDeclinaison", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $declinaisons;
/**
* @ORM\OneToMany(targetEntity="Shopping\Entity\ProductFaq", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $faqs;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @var string
*/
private $cover;
/**
* @Vich\UploadableField(mapping="images", fileNameProperty="cover")
* @var File
*/
private $coverFile;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @var string
*/
private $image;
/**
* @Vich\UploadableField(mapping="images", fileNameProperty="image")
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="integer", nullable=true)
* @var string
*/
private $expedition_delay;
/**
* @ORM\OneToMany(targetEntity="Shopping\Entity\CartProduct", mappedBy="product", cascade={"persist"}, orphanRemoval=true)
*/
private $cart_products;
/**
* @ORM\OneToMany(targetEntity="User\Entity\UserFavoris", mappedBy="product", cascade={"persist"}, orphanRemoval=true)
*/
private $user_favoris;
private $collections = [
'image', 'feature', 'specificprice', 'faq', 'declinaison'
];
/**
* Constructeur
* @throws \Exception
*/
public function __construct()
{
$this->__entityConstruct();
$this->__pageConstruct();
$this->categories = new ArrayCollection();
$this->related_products = new ArrayCollection();
$this->setReference(uniqid());
$this->setStock(1);
$this->setUseStock(false);
$this->setOnlineOnly(0);
$this->setMinimalQuantity(1);
$this->setOutOfStock(0);
$this->setPriceHT(0);
$this->setAvailableForOrder(1);
$this->specific_prices = new ArrayCollection();
$this->images = new ArrayCollection();
$this->features = new ArrayCollection();
$this->faqs = new ArrayCollection();
$this->declinaisons = new ArrayCollection();
}
public function getCollections()
{
return $this->collections;
}
public function getUseStock()
{
return $this->use_stock;
}
public function setUseStock($use_stock)
{
$this->use_stock = $use_stock;
}
public function getUgs()
{
return $this->ugs;
}
/**
* @param mixed $ugs
*/
public function setUgs($ugs): void
{
$this->ugs = $ugs;
}
/**
* Collection - Prix spécifiques
*/
public function getSpecificPrices()
{
return $this->specific_prices;
}
public function setSpecificPrices($items)
{
$this->specific_prices = $items;
return $this;
}
public function addSpecificPrice($item)
{
$this->specific_prices->add($item);
$item->setProduct($this);
return $this;
}
public function removeSpecificPrice($item)
{
$this->specific_prices->removeElement($item);
return $this;
}
/**
* Collection - Images
*/
public function getImages()
{
return $this->images;
}
public function setImages($items)
{
$this->images = $items;
return $this;
}
public function addImage($item)
{
$this->images->add($item);
$item->setProduct($this);
return $this;
}
public function removeImage($item)
{
$this->images->removeElement($item);
return $this;
}
/**
* Collection - Features
*/
public function getFeatures()
{
$array = $this->features->toArray();
usort($array, function ($a, $b) {
return strcmp($a->getSortorder(), $b->getSortorder());
});
$collection = new ArrayCollection();
foreach ($array as $item) {
$collection->add($item);
}
return $collection;
}
public function setFeatures($items)
{
$this->features = $items;
return $this;
}
public function addFeature($item)
{
$this->features->add($item);
$item->setProduct($this);
return $this;
}
public function removeFeature($item)
{
$this->features->removeElement($item);
return $this;
}
/**
* Collection - Declinaisons
*/
public function getDeclinaisons()
{
$array = $this->declinaisons->toArray();
usort($array, function ($a, $b) {
return $a->getSortorder() > $b->getSortorder();
});
$collection = new ArrayCollection();
foreach ($array as $item) {
$collection->add($item);
}
return $collection;
}
public function setDeclinaisons($items)
{
$this->declinaisons = $items;
return $this;
}
public function addDeclinaison($item)
{
$this->declinaisons->add($item);
$item->setProduct($this);
return $this;
}
public function removeDeclinaison($item)
{
$this->declinaisons->removeElement($item);
return $this;
}
/**
* Collection - Faqs
*/
public function getFaqs()
{
$array = $this->faqs->toArray();
usort($array, function ($a, $b) {
return strcmp($a->getSortorder(), $b->getSortorder());
});
$collection = new ArrayCollection();
foreach ($array as $item) {
$collection->add($item);
}
return $collection;
}
public function setFaqs($items)
{
$this->faqs = $items;
return $this;
}
public function addFaq($item)
{
$this->faqs->add($item);
$item->setProduct($this);
return $this;
}
public function removeFaq($item)
{
$this->faqs->removeElement($item);
return $this;
}
/**
* @return string
*/
public function getCover()
{
if($this->cover == ''){
return 'no_image.png';
}
return $this->cover;
}
/**
* @param string $cover
*/
public function setCover($cover)
{
$this->cover = $cover;
}
/**
* @return File
*/
public function getCoverFile()
{
return $this->coverFile;
}
/**
* @param File $coverFile
*/
public function setCoverFile($coverFile)
{
$this->coverFile = $coverFile;
if ($coverFile) {
$this->setUpdatedAt(new \DateTime('now'));
}
}
// public function getCover()
// {
// $images = $this->getImages();
//
// foreach ($images as $item) {
// if ($item->getIsCover()) {
// return $item;
// }
// }
//
// return null;
// }
/**
* @return mixed
*/
public function getMinimalQuantity()
{
return $this->minimal_quantity;
}
/**
* @param mixed $minimal_quantity
*/
public function setMinimalQuantity($minimal_quantity): void
{
$this->minimal_quantity = $minimal_quantity;
}
public function getOutOfStock()
{
return $this->out_of_stock;
}
public function setOutOfStock($out_of_stock)
{
$this->out_of_stock = $out_of_stock;
}
public function getAvailableForOrder()
{
return $this->available_for_order;
}
public function setAvailableForOrder($available_for_order)
{
$this->available_for_order = $available_for_order;
}
public function getStock()
{
return $this->stock;
}
public function setStock($stock)
{
$this->stock = $stock;
}
public function getWeight()
{
return $this->weight;
}
public function setWeight($weight)
{
$this->weight = $weight;
}
public function getConditionType()
{
return $this->condition_type;
}
public function setConditionType($conditionType)
{
$this->condition_type = $conditionType;
}
public function getWeightPriceUnit()
{
return $this->weight_price_unit;
}
public function setWeightPriceUnit($weight_price_unit): void
{
$this->weight_price_unit = $weight_price_unit;
}
public function getWeightPriceUnitType()
{
return $this->weight_price_unit_type;
}
public function setWeightPriceUnitType($weight_price_unit_type): void
{
$this->weight_price_unit_type = $weight_price_unit_type;
}
public function getWidth()
{
return $this->width;
}
public function setWidth($width)
{
$this->width = $width;
}
public function getHeight()
{
return $this->height;
}
public function setHeight($height)
{
$this->height = $height;
}
public function getDepth()
{
return $this->depth;
}
public function setDepth($depth)
{
$this->depth = $depth;
}
public function getReference()
{
return $this->reference;
}
public function setReference($reference): void
{
$this->reference = $reference;
}
public function getPriceHt()
{
return $this->price_ht;
}
public function setPriceHT($price_ht): void
{
$this->price_ht = $price_ht;
}
public function getAvailableDate()
{
return $this->available_date;
}
public function setAvailableDate($available_date)
{
$this->available_date = $available_date;
}
public function getRedirect()
{
return $this->redirect;
}
public function setRedirect($redirect): void
{
$this->redirect = $redirect;
}
public function getRedirectLink()
{
return $this->redirect_link;
}
public function setRedirectLink($redirect_link): void
{
$this->redirect_link = $redirect_link;
}
public function getCategories()
{
return $this->categories;
}
public function addCategory(Category $item)
{
if ($this->categories->contains($item)) {
return;
}
$this->categories->add($item);
$item->addProduct($this);
}
public function getRelatedProducts()
{
return $this->related_products;
}
public function addRelatedProduct(Product $item)
{
$this->related_products[] = $item;
$item->addRelatedProduct($this);
}
public function getTax()
{
return $this->tax;
}
public function setTax($tax): void
{
$this->tax = $tax;
}
public function getEan13()
{
return $this->ean13;
}
public function setEan13($ean13)
{
$this->ean13 = $ean13;
}
public function getOnlineOnly()
{
return $this->online_only;
}
public function setOnlineOnly($online_only)
{
$this->online_only = $online_only;
}
/**
* Retourne L'URL personnalisé si elle existe sinon le slug par defaut
*/
public function getSlug()
{
if ($this->getUri() != '' && $this->getUri() != '/') {
$slug = $this->getUri();
} else {
$slug = '/' . $this->slug;
}
return substr_replace($slug, '/' . $this->getId() . '-', 0, 1);
}
/**
* Retourne toute la file de catégorie parents
*/
public function getParentsCategory()
{
$categories = $this->getCategories();
$category = null;
if (count($categories) > 0) {
$category = $categories[0];
}
if ($category != null) {
return array_merge([$category], $category->getParentsCategory());
} else {
return [];
}
}
public function getExpeditionDelay()
{
return (int)$this->expedition_delay;
}
public function setExpeditionDelay($expedition_delay)
{
$this->expedition_delay = $expedition_delay;
}
public function getExpeditionDateFromNow()
{
if ($this->getExpeditionDelay() > 0) {
$last_hour_to_order = 18;
$expedition_date = new \DateTime('NOW');
// L'heure d'aujourd'hui > 18
if ($expedition_date->format('H') >= $last_hour_to_order) {
$expedition_date->modify('+1 day');
}
// On compte les jours ouvrables
for ($i = 1; $i <= $this->getExpeditionDelay(); $i++) {
// Jours normal
$expedition_date = $expedition_date->modify('+1 day');
while(!$this->checkDateOuvrable($expedition_date)){
$expedition_date = $expedition_date->modify('+1 day');
}
}
return $expedition_date->format('d/m/Y');
}
return '';
}
// Vérifie si la date est uvrable
private function checkDateOuvrable($expedition_date)
{
$currentDay = (int) $expedition_date->format('w');
if ($currentDay == 1) { // Lundi
} elseif ($currentDay == 2) { // Mardi
} elseif ($currentDay == 3) { // Mecredi
} elseif ($currentDay == 3) { // Jeudi
} elseif ($currentDay == 4) { // Vendredi
} elseif ($currentDay == 5) { // Samedi
return false;
} elseif ($currentDay == 6) { // Dimanche
return false;
}
return true;
}
/**
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* @param string $image
*/
public function setImage($image)
{
$this->image = $image;
}
/**
* @return File
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param File $imageFile
* @throws \Exception
*/
public function setImageFile($imageFile)
{
$this->imageFile = $imageFile;
if ($imageFile) {
$this->setUpdatedAt(new \DateTime('now'));
}
}
public function __toString()
{
return (string)$this->getDesignation();
}
}