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.

731 lines
20KB

  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. const BEHAVIOR_COUNT_STOCK_UNLIMITED = 'unlimited' ;
  16. const BEHAVIOR_COUNT_STOCK_BY_MEASURE = 'by-measure' ;
  17. const BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY = 'by-product-family' ;
  18. const BEHAVIOR_COUNT_STOCK_BY_PRODUCT = 'by-product' ;
  19. const TYPE_CREDIT = 'credit' ;
  20. use ProductPropertyTrait;
  21. //Champ hydraté par ProductFamilyUtils
  22. protected $reductionCatalog;
  23. /**
  24. * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\MerchantInterface", inversedBy="productFamilies")
  25. * @ORM\JoinColumn(nullable=false)
  26. */
  27. protected $merchant;
  28. /**
  29. * @ORM\ManyToMany(targetEntity="Lc\ShopBundle\Context\ProductCategoryInterface", inversedBy="productFamilies")
  30. */
  31. protected $productCategories;
  32. /**
  33. * @ORM\Column(type="boolean")
  34. */
  35. protected $activeProducts;
  36. /**
  37. * @ORM\Column(type="string", length=255, nullable=true)
  38. */
  39. protected $productsType;
  40. /**
  41. * @ORM\Column(type="string", length=255, nullable=true)
  42. */
  43. protected $quantityLabel;
  44. /**
  45. * @ORM\OneToMany(targetEntity="Lc\ShopBundle\Context\ProductInterface", mappedBy="productFamily", orphanRemoval=true, cascade={"persist"})
  46. */
  47. protected $products;
  48. /**
  49. * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\TaxRateInterface")
  50. */
  51. protected $supplierTaxRate;
  52. /**
  53. * @ORM\Column(type="string", length=255, nullable=true)
  54. */
  55. protected $subtitle;
  56. /**
  57. * @ORM\Column(type="text", nullable=true)
  58. */
  59. protected $warningMessage;
  60. /**
  61. * @ORM\Column(type="string", length=255, nullable=true)
  62. */
  63. protected $warningMessageType;
  64. /**
  65. * @ORM\Column(type="text", nullable=true)
  66. */
  67. protected $note;
  68. /**
  69. * @ORM\Column(type="string", length=31, nullable=true)
  70. */
  71. protected $behaviorOutOfStock;
  72. /**
  73. * @ORM\Column(type="string", length=31)
  74. */
  75. protected $behaviorCountStock;
  76. /**
  77. * @ORM\Column(type="date", nullable=true)
  78. */
  79. protected $propertyNoveltyExpirationDate;
  80. /**
  81. * @ORM\Column(type="string", length=255, nullable=true)
  82. */
  83. protected $propertyOrganicLabel;
  84. /**
  85. * @ORM\Column(type="text", nullable=true)
  86. */
  87. protected $propertyAllergens;
  88. /**
  89. * @ORM\Column(type="text", nullable=true)
  90. */
  91. protected $propertyComposition;
  92. /**
  93. * @ORM\Column(type="text", nullable=true)
  94. */
  95. protected $propertyFragrances;
  96. /**
  97. * @ORM\Column(type="text", nullable=true)
  98. */
  99. protected $propertyPackaging;
  100. /**
  101. * @ORM\Column(type="text", nullable=true)
  102. */
  103. protected $propertyCharacteristics;
  104. /**
  105. * @ORM\Column(type="text", nullable=true)
  106. */
  107. protected $propertyWeight;
  108. /**
  109. * @ORM\Column(type="text", nullable=true)
  110. */
  111. protected $propertyQuantity;
  112. /**
  113. * @ORM\Column(type="text", nullable=true)
  114. */
  115. protected $propertyFeature;
  116. /**
  117. * @ORM\Column(type="text", nullable=true)
  118. */
  119. protected $propertyVariety;
  120. /**
  121. * @ORM\Column(type="text", nullable=true)
  122. */
  123. protected $propertyAlcoholLevel;
  124. /**
  125. * @ORM\Column(type="string", length=255, nullable=true)
  126. */
  127. protected $behaviorExpirationDate;
  128. /**
  129. * @ORM\Column(type="string", length=255, nullable=true)
  130. */
  131. protected $typeExpirationDate;
  132. /**
  133. * @ORM\Column(type="string", length=32, nullable=true)
  134. */
  135. protected $behaviorAddToCart;
  136. /**
  137. * @ORM\Column(type="string", length=31)
  138. */
  139. protected $behaviorPrice;
  140. public function __construct()
  141. {
  142. $this->productCategories = new ArrayCollection();
  143. $this->products = new ArrayCollection();
  144. }
  145. public function __toString()
  146. {
  147. return $this->getTitle();
  148. }
  149. public function getAvailableQuantityInherited()
  150. {
  151. $availableQuantity = 0 ;
  152. switch ($this->getBehaviorCountStock()) {
  153. case self::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
  154. case self::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
  155. $availableQuantity = $this->getAvailableQuantity() ;
  156. break;
  157. case self::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
  158. foreach($this->getProducts() as $product) {
  159. $availableQuantity += $product->getAvailableQuantityInherited() ;
  160. }
  161. break ;
  162. case self::BEHAVIOR_COUNT_STOCK_UNLIMITED :
  163. $availableQuantity = false;
  164. break;
  165. }
  166. return $availableQuantity ;
  167. }
  168. public function getTaxRateInherited()
  169. {
  170. if ($this->getTaxRate()) {
  171. return $this->getTaxRate();
  172. } else {
  173. return $this->getMerchant()->getTaxRate();
  174. }
  175. }
  176. public function getMerchant(): ?Merchant
  177. {
  178. return $this->merchant;
  179. }
  180. public function setMerchant(?Merchant $merchant): self
  181. {
  182. $this->merchant = $merchant;
  183. return $this;
  184. }
  185. public function getActiveProducts(): ?bool
  186. {
  187. return $this->activeProducts;
  188. }
  189. public function setActiveProducts(bool $activeProducts): self
  190. {
  191. $this->activeProducts = $activeProducts;
  192. return $this;
  193. }
  194. public function getProductsType(): ?string
  195. {
  196. return $this->productsType;
  197. }
  198. public function setProductsType(?string $productsType): self
  199. {
  200. $this->productsType = $productsType;
  201. return $this;
  202. }
  203. public function getQuantityLabel(): ?string
  204. {
  205. return $this->quantityLabel;
  206. }
  207. public function setQuantityLabel(?string $quantityLabel): self
  208. {
  209. $this->quantityLabel = $quantityLabel;
  210. return $this;
  211. }
  212. /**
  213. * @return Collection|ProductInterface[]
  214. */
  215. public function getProducts(): Collection
  216. {
  217. return $this->products;
  218. }
  219. public function addProduct(ProductInterface $product): self
  220. {
  221. if (!$this->products->contains($product)) {
  222. $this->products[] = $product;
  223. $product->setProductFamily($this);
  224. }
  225. return $this;
  226. }
  227. public function removeProduct(ProductInterface $product): self
  228. {
  229. if ($this->products->contains($product)) {
  230. $this->products->removeElement($product);
  231. // set the owning side to null (unless already changed)
  232. if ($product->getProductFamily() === $this) {
  233. $product->setProductFamily(null);
  234. }
  235. }
  236. return $this;
  237. }
  238. public function getReductionCatalog(): ?ReductionCatalog
  239. {
  240. return $this->reductionCatalog;
  241. }
  242. public function getReductionCatalogInherited(): ?ReductionCatalog
  243. {
  244. return $this->getReductionCatalog() ;
  245. }
  246. public function setReductionCatalog(?ReductionCatalog $reductionCatalog): self
  247. {
  248. $this->reductionCatalog = $reductionCatalog;
  249. return $this;
  250. }
  251. /**
  252. * @return Collection|ProductCategory[]
  253. */
  254. public function getProductCategories(): Collection
  255. {
  256. return $this->productCategories;
  257. }
  258. public function initProductCategories()
  259. {
  260. $this->productCategories = new ArrayCollection();
  261. }
  262. public function addProductCategory(ProductCategory $productCategory): self
  263. {
  264. if (!$this->productCategories->contains($productCategory)) {
  265. $this->productCategories[] = $productCategory;
  266. }
  267. return $this;
  268. }
  269. public function removeProductCategory(ProductCategory $productCategory): self
  270. {
  271. if ($this->productCategories->contains($productCategory)) {
  272. $this->productCategories->removeElement($productCategory);
  273. }
  274. return $this;
  275. }
  276. public function getProductCategoryParent()
  277. {
  278. $productCategories = $this->getProductCategories();
  279. if (count($productCategories) > 0) {
  280. return $productCategories[0]->getParent();
  281. }
  282. return false;
  283. }
  284. public function getProductCategoryChild()
  285. {
  286. $productCategories = $this->getProductCategories();
  287. foreach ($productCategories as $productCategory) {
  288. if ($productCategory->getParent()) {
  289. return $productCategory;
  290. }
  291. }
  292. return false;
  293. }
  294. public function getSupplierTaxRate(): ?TaxRate
  295. {
  296. return $this->supplierTaxRate;
  297. }
  298. public function setSupplierTaxRate(?TaxRate $supplierTaxRate): self
  299. {
  300. $this->supplierTaxRate = $supplierTaxRate;
  301. return $this;
  302. }
  303. public function getSubtitle(): ?string
  304. {
  305. return $this->subtitle;
  306. }
  307. public function setSubtitle(?string $subtitle): self
  308. {
  309. $this->subtitle = $subtitle;
  310. return $this;
  311. }
  312. public function getWarningMessage(): ?string
  313. {
  314. return $this->warningMessage;
  315. }
  316. public function setWarningMessage(?string $warningMessage): self
  317. {
  318. $this->warningMessage = $warningMessage;
  319. return $this;
  320. }
  321. public function getWarningMessageType(): ?string
  322. {
  323. return $this->warningMessageType;
  324. }
  325. public function setWarningMessageType(?string $warningMessageType): self
  326. {
  327. $this->warningMessageType = $warningMessageType;
  328. return $this;
  329. }
  330. public function getNote(): ?string
  331. {
  332. return $this->note;
  333. }
  334. public function setNote(?string $note): self
  335. {
  336. $this->note = $note;
  337. return $this;
  338. }
  339. public function getBehaviorOutOfStock(): ?string
  340. {
  341. return $this->behaviorOutOfStock;
  342. }
  343. public function setBehaviorOutOfStock(?string $behaviorOutOfStock): self
  344. {
  345. $this->behaviorOutOfStock = $behaviorOutOfStock;
  346. return $this;
  347. }
  348. public function getBehaviorCountStock(): ?string
  349. {
  350. return $this->behaviorCountStock;
  351. }
  352. public function setBehaviorCountStock(string $behaviorCountStock): self
  353. {
  354. $this->behaviorCountStock = $behaviorCountStock;
  355. return $this;
  356. }
  357. public function isPropertyNoveltyOnline(): ?bool
  358. {
  359. if ($this->getPropertyNoveltyExpirationDate()) {
  360. $now = new \DateTime();
  361. if ($now <= $this->getPropertyNoveltyExpirationDate()) {
  362. return true;
  363. }
  364. }
  365. return false;
  366. }
  367. public function getPropertyNoveltyExpirationDate(): ?\DateTimeInterface
  368. {
  369. return $this->propertyNoveltyExpirationDate;
  370. }
  371. public function setPropertyNoveltyExpirationDate(?\DateTimeInterface $propertyNoveltyExpirationDate): self
  372. {
  373. $this->propertyNoveltyExpirationDate = $propertyNoveltyExpirationDate;
  374. return $this;
  375. }
  376. public function getPropertyOrganicLabel(): ?string
  377. {
  378. return $this->propertyOrganicLabel;
  379. }
  380. public function setPropertyOrganicLabel(?string $propertyOrganicLabel): self
  381. {
  382. $this->propertyOrganicLabel = $propertyOrganicLabel;
  383. return $this;
  384. }
  385. public function getPropertyAllergens(): ?string
  386. {
  387. return $this->propertyAllergens;
  388. }
  389. public function setPropertyAllergens(?string $propertyAllergens): self
  390. {
  391. $this->propertyAllergens = $propertyAllergens;
  392. return $this;
  393. }
  394. public function getPropertyComposition(): ?string
  395. {
  396. return $this->propertyComposition;
  397. }
  398. public function setPropertyComposition(?string $propertyComposition): self
  399. {
  400. $this->propertyComposition = $propertyComposition;
  401. return $this;
  402. }
  403. public function getPropertyFragrances(): ?string
  404. {
  405. return $this->propertyFragrances;
  406. }
  407. public function setPropertyFragrances(?string $propertyFragrances): self
  408. {
  409. $this->propertyFragrances = $propertyFragrances;
  410. return $this;
  411. }
  412. public function countProperties(): bool
  413. {
  414. $count = 0;
  415. $count += (int)strlen($this->getPropertyAllergens()) > 0;
  416. $count += (int)strlen($this->getPropertyComposition()) > 0;
  417. $count += (int)strlen($this->getPropertyFragrances()) > 0;
  418. $count += (int)strlen($this->getPropertyOrganicLabel()) > 0;
  419. $count += (int)($this->getPropertyExpirationDate() != null);
  420. return $count;
  421. }
  422. public function getBehaviorExpirationDate(): ?string
  423. {
  424. return $this->behaviorExpirationDate;
  425. }
  426. public function setBehaviorExpirationDate(?string $behaviorExpirationDate): self
  427. {
  428. $this->behaviorExpirationDate = $behaviorExpirationDate;
  429. return $this;
  430. }
  431. public function getTypeExpirationDate(): ?string
  432. {
  433. return $this->typeExpirationDate;
  434. }
  435. public function setTypeExpirationDate(?string $typeExpirationDate): self
  436. {
  437. $this->typeExpirationDate = $typeExpirationDate;
  438. return $this;
  439. }
  440. public function getPropertyWeight(): ?string
  441. {
  442. return $this->propertyWeight;
  443. }
  444. public function setPropertyWeight(?string $propertyWeight): self
  445. {
  446. $this->propertyWeight = $propertyWeight;
  447. return $this;
  448. }
  449. public function getPropertyQuantity(): ?string
  450. {
  451. return $this->propertyQuantity;
  452. }
  453. public function setPropertyQuantity(?string $propertyQuantity): self
  454. {
  455. $this->propertyQuantity = $propertyQuantity;
  456. return $this;
  457. }
  458. public function getPropertyVariety(): ?string
  459. {
  460. return $this->propertyVariety;
  461. }
  462. public function setPropertyVariety(?string $propertyVariety): self
  463. {
  464. $this->propertyQuantity = $propertyVariety;
  465. return $this;
  466. }
  467. public function getPropertyFeature(): ?string
  468. {
  469. return $this->propertyFeature;
  470. }
  471. public function setPropertyFeature(?string $propertyFeature): self
  472. {
  473. $this->propertyFeature = $propertyFeature;
  474. return $this;
  475. }
  476. public function getPropertyAlcoholLevel(): ?string
  477. {
  478. return $this->propertyAlcoholLevel;
  479. }
  480. public function setPropertyAlcoholLevel(?string $propertyAlcoholLevel): self
  481. {
  482. $this->propertyAlcoholLevel = $propertyAlcoholLevel;
  483. return $this;
  484. }
  485. public function getPropertyPackaging(): ?string
  486. {
  487. return $this->propertyPackaging;
  488. }
  489. public function setPropertyPackaging(?string $propertyPackaging): self
  490. {
  491. $this->propertyPackaging = $propertyPackaging;
  492. return $this;
  493. }
  494. public function getPropertyCharacteristics(): ?string
  495. {
  496. return $this->propertyCharacteristics;
  497. }
  498. public function setPropertyCharacteristics(?string $propertyCharacteristics): self
  499. {
  500. $this->propertyCharacteristics = $propertyCharacteristics;
  501. return $this;
  502. }
  503. public function getBehaviorAddToCart(): ?string
  504. {
  505. return $this->behaviorAddToCart;
  506. }
  507. public function setBehaviorAddToCart(?string $behaviorAddToCart): self
  508. {
  509. $this->behaviorAddToCart = $behaviorAddToCart;
  510. return $this;
  511. }
  512. public function getBehaviorPrice(): ?string
  513. {
  514. return $this->behaviorPrice;
  515. }
  516. public function getBehaviorPriceInherited()
  517. {
  518. return $this->getBehaviorPrice() ;
  519. }
  520. public function setBehaviorPrice(?string $behaviorPrice): self
  521. {
  522. $this->behaviorPrice = $behaviorPrice;
  523. return $this;
  524. }
  525. public function hasProductsWithVariousWeight()
  526. {
  527. if ($this->getActiveProducts()) {
  528. $arrayCountProducts = [];
  529. $products = $this->getProducts();
  530. foreach ($products as $product) {
  531. $titleProduct = $product->getTitleInherited();
  532. if (!isset($arrayCountProducts[$titleProduct])) {
  533. $arrayCountProducts[$titleProduct] = [];
  534. }
  535. if (!in_array($product->getQuantityLabelInherited(), $arrayCountProducts[$titleProduct])) {
  536. $arrayCountProducts[$titleProduct][] = $product->getQuantityLabelInherited();
  537. }
  538. if (count($arrayCountProducts[$titleProduct]) > 1) {
  539. return true;
  540. }
  541. }
  542. }
  543. return false;
  544. }
  545. public function getProductsGroupByTitle()
  546. {
  547. $arrayProductsGroupByTitle = [];
  548. $products = $this->getProducts();
  549. foreach ($products as $product) {
  550. $titleProduct = $product->getTitleInherited();
  551. if (!isset($arrayProductsGroupByTitle[$titleProduct])) {
  552. $arrayProductsGroupByTitle[$titleProduct] = [];
  553. }
  554. $arrayProductsGroupByTitle[$titleProduct][] = $product;
  555. }
  556. return $arrayProductsGroupByTitle;
  557. }
  558. }