772 lines
22KB

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