686 lines
19KB

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