You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

595 lines
17KB

  1. <?php
  2. namespace Lc\ShopBundle\Model;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Lc\ShopBundle\Context\FilterMerchantInterface;
  7. use Lc\ShopBundle\Context\PriceInterface;
  8. use Lc\ShopBundle\Context\ProductInterface;
  9. use Lc\ShopBundle\Context\ProductPropertyInterface;
  10. /**
  11. * @ORM\MappedSuperclass()
  12. */
  13. abstract class ProductFamily extends AbstractDocumentEntity implements ProductPropertyInterface, PriceInterface, FilterMerchantInterface
  14. {
  15. use ProductPropertyTrait;
  16. /**
  17. * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\MerchantInterface", inversedBy="productFamilies")
  18. * @ORM\JoinColumn(nullable=false)
  19. */
  20. protected $merchant;
  21. /**
  22. * @ORM\ManyToMany(targetEntity="Lc\ShopBundle\Context\ProductCategoryInterface", inversedBy="productFamilies")
  23. */
  24. protected $productCategories;
  25. /**
  26. * @ORM\ManyToMany(targetEntity="Lc\ShopBundle\Context\ReductionCatalogInterface", mappedBy="reductionCatalogs")
  27. */
  28. protected $reductionCatalogs;
  29. /**
  30. * @ORM\Column(type="boolean")
  31. */
  32. protected $activeProducts;
  33. /**
  34. * @ORM\Column(type="string", length=255, nullable=true)
  35. */
  36. protected $productsType;
  37. /**
  38. * @ORM\OneToMany(targetEntity="Lc\ShopBundle\Context\ProductInterface", mappedBy="productFamily", orphanRemoval=true, cascade={"persist"}, fetch="EAGER")
  39. */
  40. protected $products;
  41. /**
  42. * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\TaxRateInterface")
  43. */
  44. protected $supplierTaxRate;
  45. /**
  46. * @ORM\Column(type="string", length=255, nullable=true)
  47. */
  48. protected $subtitle;
  49. /**
  50. * @ORM\Column(type="text", nullable=true)
  51. */
  52. protected $warningMessage;
  53. /**
  54. * @ORM\Column(type="string", length=255, nullable=true)
  55. */
  56. private $warningMessageType;
  57. /**
  58. * @ORM\Column(type="text", nullable=true)
  59. */
  60. protected $note;
  61. /**
  62. * @ORM\Column(type="string", length=31, nullable=true)
  63. */
  64. protected $behaviorOutOfStock;
  65. /**
  66. * @ORM\Column(type="string", length=31)
  67. */
  68. protected $behaviorCountStock;
  69. /**
  70. * @ORM\Column(type="date", nullable=true)
  71. */
  72. protected $propertyNoveltyExpirationDate;
  73. /**
  74. * @ORM\Column(type="string", length=255, nullable=true)
  75. */
  76. protected $propertyOrganicLabel;
  77. /**
  78. * @ORM\Column(type="text", nullable=true)
  79. */
  80. protected $propertyAllergens;
  81. /**
  82. * @ORM\Column(type="text", nullable=true)
  83. */
  84. protected $propertyComposition;
  85. /**
  86. * @ORM\Column(type="text", nullable=true)
  87. */
  88. protected $propertyFragrances;
  89. /**
  90. * @ORM\Column(type="string", length=255, nullable=true)
  91. */
  92. protected $behaviorExpirationDate;
  93. /**
  94. * @ORM\Column(type="string", length=255, nullable=true)
  95. */
  96. protected $typeExpirationDate;
  97. /**
  98. * @ORM\Column(type="string", length=32, nullable=true)
  99. */
  100. protected $behaviorAddToCart;
  101. //Champ hydraté par ProductFamilyUtils
  102. public $reductionCatalog;
  103. public function __construct()
  104. {
  105. $this->productCategories = new ArrayCollection();
  106. $this->products = new ArrayCollection();
  107. }
  108. public function __toString()
  109. {
  110. return $this->getTitle();
  111. }
  112. public function getTaxRateInherited()
  113. {
  114. if($this->getTaxRate()) {
  115. return $this->getTaxRate() ;
  116. }
  117. else {
  118. return $this->getMerchant()->getTaxRate() ;
  119. }
  120. }
  121. public function getMerchant(): ?Merchant
  122. {
  123. return $this->merchant;
  124. }
  125. public function setMerchant(?Merchant $merchant): self
  126. {
  127. $this->merchant = $merchant;
  128. return $this;
  129. }
  130. public function getActiveProducts(): ?bool
  131. {
  132. return $this->activeProducts;
  133. }
  134. public function setActiveProducts(bool $activeProducts): self
  135. {
  136. $this->activeProducts = $activeProducts;
  137. return $this;
  138. }
  139. public function getProductsType(): ?string
  140. {
  141. return $this->productsType;
  142. }
  143. public function setProductsType(?string $productsType): self
  144. {
  145. $this->productsType = $productsType;
  146. return $this;
  147. }
  148. /**
  149. * @return Collection|ProductInterface[]
  150. */
  151. public function getProducts(): Collection
  152. {
  153. return $this->products;
  154. }
  155. public function addProduct(ProductInterface $product): self
  156. {
  157. if (!$this->products->contains($product)) {
  158. $this->products[] = $product;
  159. $product->setProductFamily($this);
  160. }
  161. return $this;
  162. }
  163. public function removeProduct(ProductInterface $product): self
  164. {
  165. if ($this->products->contains($product)) {
  166. $this->products->removeElement($product);
  167. // set the owning side to null (unless already changed)
  168. if ($product->getProductFamily() === $this) {
  169. $product->setProductFamily(null);
  170. }
  171. }
  172. return $this;
  173. }
  174. /**
  175. * @return Collection|ReductionCatalog[]
  176. */
  177. public function getReductionCatalogs(): Collection
  178. {
  179. return $this->reductionCatalogs;
  180. }
  181. public function initReductionCatalogs()
  182. {
  183. $this->reductionCatalogs = new ArrayCollection();
  184. }
  185. public function addReductionCatalog(ReductionCatalog $reductionCatalog): self
  186. {
  187. if (!$this->reductionCatalogs->contains($reductionCatalog)) {
  188. $this->reductionCatalogs[] = $reductionCatalog;
  189. }
  190. return $this;
  191. }
  192. public function removeReductionCatalog(ReductionCatalog $reductionCatalog): self
  193. {
  194. if ($this->reductionCatalogs->contains($reductionCatalog)) {
  195. $this->reductionCatalogs->removeElement($reductionCatalog);
  196. }
  197. return $this;
  198. }
  199. /**
  200. * @return Collection|ProductCategory[]
  201. */
  202. public function getProductCategories(): Collection
  203. {
  204. return $this->productCategories;
  205. }
  206. public function initProductCategories()
  207. {
  208. $this->productCategories = new ArrayCollection();
  209. }
  210. public function addProductCategory(ProductCategory $productCategory): self
  211. {
  212. if (!$this->productCategories->contains($productCategory)) {
  213. $this->productCategories[] = $productCategory;
  214. }
  215. return $this;
  216. }
  217. public function removeProductCategory(ProductCategory $productCategory): self
  218. {
  219. if ($this->productCategories->contains($productCategory)) {
  220. $this->productCategories->removeElement($productCategory);
  221. }
  222. return $this;
  223. }
  224. public function getProductCategoryParent()
  225. {
  226. $productCategories = $this->getProductCategories() ;
  227. if(count($productCategories) > 0) {
  228. return $productCategories[0]->getParent() ;
  229. }
  230. return false ;
  231. }
  232. public function getProductCategoryChild()
  233. {
  234. $productCategories = $this->getProductCategories() ;
  235. foreach($productCategories as $productCategory) {
  236. if($productCategory->getParent()) {
  237. return $productCategory ;
  238. }
  239. }
  240. return false ;
  241. }
  242. public function getSupplierTaxRate(): ?TaxRate
  243. {
  244. return $this->supplierTaxRate;
  245. }
  246. public function setSupplierTaxRate(?TaxRate $supplierTaxRate): self
  247. {
  248. $this->supplierTaxRate = $supplierTaxRate;
  249. return $this;
  250. }
  251. public function getSubtitle(): ?string
  252. {
  253. return $this->subtitle;
  254. }
  255. public function setSubtitle(?string $subtitle): self
  256. {
  257. $this->subtitle = $subtitle;
  258. return $this;
  259. }
  260. public function getWarningMessage(): ?string
  261. {
  262. return $this->warningMessage;
  263. }
  264. public function setWarningMessage(?string $warningMessage): self
  265. {
  266. $this->warningMessage = $warningMessage;
  267. return $this;
  268. }
  269. public function getWarningMessageType(): ?string
  270. {
  271. return $this->warningMessageType;
  272. }
  273. public function setWarningMessageType(?string $warningMessageType): self
  274. {
  275. $this->warningMessageType = $warningMessageType;
  276. return $this;
  277. }
  278. public function getNote(): ?string
  279. {
  280. return $this->note;
  281. }
  282. public function setNote(?string $note): self
  283. {
  284. $this->note = $note;
  285. return $this;
  286. }
  287. public function getBehaviorOutOfStock(): ?string
  288. {
  289. return $this->behaviorOutOfStock;
  290. }
  291. public function setBehaviorOutOfStock(?string $behaviorOutOfStock): self
  292. {
  293. $this->behaviorOutOfStock = $behaviorOutOfStock;
  294. return $this;
  295. }
  296. public function getBehaviorCountStock(): ?string
  297. {
  298. return $this->behaviorCountStock;
  299. }
  300. public function setBehaviorCountStock(string $behaviorCountStock): self
  301. {
  302. $this->behaviorCountStock = $behaviorCountStock;
  303. return $this;
  304. }
  305. private function getCheapestOrMostExpensiveProduct($comparisonFunction, $returnSelfIfNotActiveProducts)
  306. {
  307. $products = $this->getProducts()->getValues() ;
  308. if(count($products) > 0) {
  309. usort($products, $comparisonFunction) ;
  310. return $products[0] ;
  311. }
  312. if($returnSelfIfNotActiveProducts) {
  313. return $this ;
  314. }
  315. else {
  316. return false ;
  317. }
  318. }
  319. public function getCheapestProduct()
  320. {
  321. return $this->getCheapestOrMostExpensiveProduct(function($a, $b) {
  322. return $a->getPriceInherited() > $b->getPriceInherited() ;
  323. },true) ;
  324. }
  325. public function getCheapestProductByUnitRef()
  326. {
  327. return $this->getCheapestOrMostExpensiveProduct(function($a, $b) {
  328. return $a->getPriceByUnitRef() > $b->getPriceByUnitRef() ;
  329. },false) ;
  330. }
  331. public function getMostExpensiveProductByUnitRef()
  332. {
  333. return $this->getCheapestOrMostExpensiveProduct(function($a, $b) {
  334. return $a->getPriceByUnitRef() < $b->getPriceByUnitRef() ;
  335. },false) ;
  336. }
  337. public function isPropertyNoveltyOnline(): ?bool
  338. {
  339. if($this->getPropertyNoveltyExpirationDate()) {
  340. $now = new \DateTime() ;
  341. if($now <= $this->getPropertyNoveltyExpirationDate()) {
  342. return true ;
  343. }
  344. }
  345. return false ;
  346. }
  347. public function getPropertyNoveltyExpirationDate(): ?\DateTimeInterface
  348. {
  349. return $this->propertyNoveltyExpirationDate;
  350. }
  351. public function setPropertyNoveltyExpirationDate(?\DateTimeInterface $propertyNoveltyExpirationDate): self
  352. {
  353. $this->propertyNoveltyExpirationDate = $propertyNoveltyExpirationDate;
  354. return $this;
  355. }
  356. public function getPropertyOrganicLabel(): ?string
  357. {
  358. return $this->propertyOrganicLabel;
  359. }
  360. public function setPropertyOrganicLabel(?string $propertyOrganicLabel): self
  361. {
  362. $this->propertyOrganicLabel = $propertyOrganicLabel;
  363. return $this;
  364. }
  365. public function getPropertyAllergens(): ?string
  366. {
  367. return $this->propertyAllergens;
  368. }
  369. public function setPropertyAllergens(?string $propertyAllergens): self
  370. {
  371. $this->propertyAllergens = $propertyAllergens;
  372. return $this;
  373. }
  374. public function getPropertyComposition(): ?string
  375. {
  376. return $this->propertyComposition;
  377. }
  378. public function setPropertyComposition(?string $propertyComposition): self
  379. {
  380. $this->propertyComposition = $propertyComposition;
  381. return $this;
  382. }
  383. public function getPropertyFragrances(): ?string
  384. {
  385. return $this->propertyFragrances;
  386. }
  387. public function setPropertyFragrances(?string $propertyFragrances): self
  388. {
  389. $this->propertyFragrances = $propertyFragrances;
  390. return $this;
  391. }
  392. public function countProperties(): bool
  393. {
  394. $count = 0 ;
  395. $count += (int) strlen($this->getPropertyAllergens()) > 0 ;
  396. $count += (int) strlen($this->getPropertyComposition()) > 0 ;
  397. $count += (int) strlen($this->getPropertyFragrances()) > 0 ;
  398. $count += (int) strlen($this->getPropertyOrganicLabel()) > 0 ;
  399. $count += (int) ($this->getPropertyExpirationDate() != null) ;
  400. return $count ;
  401. }
  402. public function getBehaviorExpirationDate(): ?string
  403. {
  404. return $this->behaviorExpirationDate;
  405. }
  406. public function setBehaviorExpirationDate(?string $behaviorExpirationDate): self
  407. {
  408. $this->behaviorExpirationDate = $behaviorExpirationDate;
  409. return $this;
  410. }
  411. public function getTypeExpirationDate(): ?string
  412. {
  413. return $this->typeExpirationDate;
  414. }
  415. public function setTypeExpirationDate(?string $typeExpirationDate): self
  416. {
  417. $this->typeExpirationDate = $typeExpirationDate;
  418. return $this;
  419. }
  420. public function getBehaviorAddToCart(): ?string
  421. {
  422. return $this->behaviorAddToCart;
  423. }
  424. public function setBehaviorAddToCart(?string $behaviorAddToCart): self
  425. {
  426. $this->behaviorAddToCart = $behaviorAddToCart;
  427. return $this;
  428. }
  429. public function hasProductsWithVariousWeight()
  430. {
  431. if($this->getActiveProducts()) {
  432. $arrayCountProducts = [] ;
  433. $products = $this->getProducts() ;
  434. foreach($products as $product) {
  435. $titleProduct = $product->getTitleInherited() ;
  436. if(!isset($arrayCountProducts[$titleProduct])) {
  437. $arrayCountProducts[$titleProduct] = [] ;
  438. }
  439. if(!in_array($product->getQuantityLabelInherited(), $arrayCountProducts[$titleProduct])) {
  440. $arrayCountProducts[$titleProduct][] = $product->getQuantityLabelInherited() ;
  441. }
  442. if(count($arrayCountProducts[$titleProduct]) > 1) {
  443. return true ;
  444. }
  445. }
  446. }
  447. return false ;
  448. }
  449. public function getProductsGroupByTitle()
  450. {
  451. $arrayProductsGroupByTitle = [] ;
  452. $products = $this->getProducts() ;
  453. foreach($products as $product) {
  454. $titleProduct = $product->getTitleInherited() ;
  455. if(!isset($arrayProductsGroupByTitle[$titleProduct])) {
  456. $arrayProductsGroupByTitle[$titleProduct] = [] ;
  457. }
  458. $arrayProductsGroupByTitle[$titleProduct][] = $product ;
  459. }
  460. return $arrayProductsGroupByTitle ;
  461. }
  462. }