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.

756 lines
21KB

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