795 lines
23KB

  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 getProductsOnline(): Collection
  252. {
  253. $products = $this->getProducts() ;
  254. $productsOnlineArray = new ArrayCollection() ;
  255. foreach($products as $product) {
  256. if($product->getStatus() == 1) {
  257. $productsOnlineArray[] = $product ;
  258. }
  259. }
  260. return $productsOnlineArray ;
  261. }
  262. public function addProduct(ProductInterface $product): self
  263. {
  264. if (!$this->products->contains($product)) {
  265. $this->products[] = $product;
  266. $product->setProductFamily($this);
  267. }
  268. return $this;
  269. }
  270. public function removeProduct(ProductInterface $product): self
  271. {
  272. if ($this->products->contains($product)) {
  273. $this->products->removeElement($product);
  274. // set the owning side to null (unless already changed)
  275. if ($product->getProductFamily() === $this) {
  276. $product->setProductFamily(null);
  277. }
  278. }
  279. return $this;
  280. }
  281. public function getReductionCatalog(): ?ReductionCatalog
  282. {
  283. return $this->reductionCatalog;
  284. }
  285. public function getReductionCatalogInherited(): ?ReductionCatalog
  286. {
  287. return $this->getReductionCatalog() ;
  288. }
  289. public function setReductionCatalog(?ReductionCatalog $reductionCatalog): self
  290. {
  291. $this->reductionCatalog = $reductionCatalog;
  292. return $this;
  293. }
  294. /**
  295. * @return Collection|ProductCategory[]
  296. */
  297. public function getProductCategories(): Collection
  298. {
  299. return $this->productCategories;
  300. }
  301. public function initProductCategories()
  302. {
  303. $this->productCategories = new ArrayCollection();
  304. }
  305. public function addProductCategory(ProductCategory $productCategory): self
  306. {
  307. if (!$this->productCategories->contains($productCategory)) {
  308. $this->productCategories[] = $productCategory;
  309. }
  310. return $this;
  311. }
  312. public function removeProductCategory(ProductCategory $productCategory): self
  313. {
  314. if ($this->productCategories->contains($productCategory)) {
  315. $this->productCategories->removeElement($productCategory);
  316. }
  317. return $this;
  318. }
  319. public function getProductCategoryParent()
  320. {
  321. $productCategories = $this->getProductCategories();
  322. if (count($productCategories) > 0) {
  323. return $productCategories[0]->getParent();
  324. }
  325. return false;
  326. }
  327. public function getProductCategoryChild()
  328. {
  329. $productCategories = $this->getProductCategories();
  330. foreach ($productCategories as $productCategory) {
  331. if ($productCategory->getParent()) {
  332. return $productCategory;
  333. }
  334. }
  335. return false;
  336. }
  337. public function getSupplierTaxRate(): ?TaxRate
  338. {
  339. return $this->supplierTaxRate;
  340. }
  341. public function setSupplierTaxRate(?TaxRate $supplierTaxRate): self
  342. {
  343. $this->supplierTaxRate = $supplierTaxRate;
  344. return $this;
  345. }
  346. public function getSubtitle(): ?string
  347. {
  348. return $this->subtitle;
  349. }
  350. public function setSubtitle(?string $subtitle): self
  351. {
  352. $this->subtitle = $subtitle;
  353. return $this;
  354. }
  355. public function getWarningMessage(): ?string
  356. {
  357. return $this->warningMessage;
  358. }
  359. public function setWarningMessage(?string $warningMessage): self
  360. {
  361. $this->warningMessage = $warningMessage;
  362. return $this;
  363. }
  364. public function getWarningMessageType(): ?string
  365. {
  366. return $this->warningMessageType;
  367. }
  368. public function setWarningMessageType(?string $warningMessageType): self
  369. {
  370. $this->warningMessageType = $warningMessageType;
  371. return $this;
  372. }
  373. public function getNote(): ?string
  374. {
  375. return $this->note;
  376. }
  377. public function setNote(?string $note): self
  378. {
  379. $this->note = $note;
  380. return $this;
  381. }
  382. public function getBehaviorOutOfStock(): ?string
  383. {
  384. return $this->behaviorOutOfStock;
  385. }
  386. public function setBehaviorOutOfStock(?string $behaviorOutOfStock): self
  387. {
  388. $this->behaviorOutOfStock = $behaviorOutOfStock;
  389. return $this;
  390. }
  391. public function getBehaviorCountStock(): ?string
  392. {
  393. return $this->behaviorCountStock;
  394. }
  395. public function setBehaviorCountStock(string $behaviorCountStock): self
  396. {
  397. $this->behaviorCountStock = $behaviorCountStock;
  398. return $this;
  399. }
  400. public function isPropertyNoveltyOnline(): ?bool
  401. {
  402. if ($this->getPropertyNoveltyExpirationDate()) {
  403. $now = new \DateTime();
  404. if ($now <= $this->getPropertyNoveltyExpirationDate()) {
  405. return true;
  406. }
  407. }
  408. return false;
  409. }
  410. public function getPropertyNoveltyExpirationDate(): ?\DateTimeInterface
  411. {
  412. return $this->propertyNoveltyExpirationDate;
  413. }
  414. public function setPropertyNoveltyExpirationDate(?\DateTimeInterface $propertyNoveltyExpirationDate): self
  415. {
  416. $this->propertyNoveltyExpirationDate = $propertyNoveltyExpirationDate;
  417. return $this;
  418. }
  419. public function getPropertyOrganicLabel(): ?string
  420. {
  421. return $this->propertyOrganicLabel;
  422. }
  423. public function setPropertyOrganicLabel(?string $propertyOrganicLabel): self
  424. {
  425. $this->propertyOrganicLabel = $propertyOrganicLabel;
  426. return $this;
  427. }
  428. public function getPropertyAllergens(): ?string
  429. {
  430. return $this->propertyAllergens;
  431. }
  432. public function setPropertyAllergens(?string $propertyAllergens): self
  433. {
  434. $this->propertyAllergens = $propertyAllergens;
  435. return $this;
  436. }
  437. public function getPropertyComposition(): ?string
  438. {
  439. return $this->propertyComposition;
  440. }
  441. public function setPropertyComposition(?string $propertyComposition): self
  442. {
  443. $this->propertyComposition = $propertyComposition;
  444. return $this;
  445. }
  446. public function getPropertyFragrances(): ?string
  447. {
  448. return $this->propertyFragrances;
  449. }
  450. public function setPropertyFragrances(?string $propertyFragrances): self
  451. {
  452. $this->propertyFragrances = $propertyFragrances;
  453. return $this;
  454. }
  455. public function countProperties(): bool
  456. {
  457. $count = 0;
  458. $count += (int)strlen($this->getPropertyOrganicLabel()) > 0;
  459. $count += (int)strlen($this->getPropertyWeight()) > 0;
  460. $count += (int)strlen($this->getPropertyFragrances()) > 0;
  461. $count += (int)strlen($this->getPropertyComposition()) > 0;
  462. $count += (int)strlen($this->getPropertyAllergens()) > 0;
  463. $count += (int)strlen($this->getPropertyAlcoholLevel()) > 0;
  464. $count += (int)strlen($this->getPropertyCharacteristics()) > 0;
  465. $count += (int)strlen($this->getPropertyFeature()) > 0;
  466. $count += (int)strlen($this->getPropertyPackaging()) > 0;
  467. $count += (int)strlen($this->getPropertyQuantity()) > 0;
  468. $count += (int)strlen($this->getPropertyVariety()) > 0;
  469. $count += (int)($this->getPropertyExpirationDate() != null);
  470. return $count;
  471. }
  472. public function getBehaviorExpirationDate(): ?string
  473. {
  474. return $this->behaviorExpirationDate;
  475. }
  476. public function setBehaviorExpirationDate(?string $behaviorExpirationDate): self
  477. {
  478. $this->behaviorExpirationDate = $behaviorExpirationDate;
  479. return $this;
  480. }
  481. public function getTypeExpirationDate(): ?string
  482. {
  483. return $this->typeExpirationDate;
  484. }
  485. public function setTypeExpirationDate(?string $typeExpirationDate): self
  486. {
  487. $this->typeExpirationDate = $typeExpirationDate;
  488. return $this;
  489. }
  490. public function getPropertyWeight(): ?string
  491. {
  492. return $this->propertyWeight;
  493. }
  494. public function setPropertyWeight(?string $propertyWeight): self
  495. {
  496. $this->propertyWeight = $propertyWeight;
  497. return $this;
  498. }
  499. public function getPropertyQuantity(): ?string
  500. {
  501. return $this->propertyQuantity;
  502. }
  503. public function setPropertyQuantity(?string $propertyQuantity): self
  504. {
  505. $this->propertyQuantity = $propertyQuantity;
  506. return $this;
  507. }
  508. public function getPropertyVariety(): ?string
  509. {
  510. return $this->propertyVariety;
  511. }
  512. public function setPropertyVariety(?string $propertyVariety): self
  513. {
  514. $this->propertyVariety = $propertyVariety;
  515. return $this;
  516. }
  517. public function getPropertyFeature(): ?string
  518. {
  519. return $this->propertyFeature;
  520. }
  521. public function setPropertyFeature(?string $propertyFeature): self
  522. {
  523. $this->propertyFeature = $propertyFeature;
  524. return $this;
  525. }
  526. public function getPropertyAlcoholLevel(): ?string
  527. {
  528. return $this->propertyAlcoholLevel;
  529. }
  530. public function setPropertyAlcoholLevel(?string $propertyAlcoholLevel): self
  531. {
  532. $this->propertyAlcoholLevel = $propertyAlcoholLevel;
  533. return $this;
  534. }
  535. public function getPropertyPackaging(): ?string
  536. {
  537. return $this->propertyPackaging;
  538. }
  539. public function setPropertyPackaging(?string $propertyPackaging): self
  540. {
  541. $this->propertyPackaging = $propertyPackaging;
  542. return $this;
  543. }
  544. public function getPropertyCharacteristics(): ?string
  545. {
  546. return $this->propertyCharacteristics;
  547. }
  548. public function setPropertyCharacteristics(?string $propertyCharacteristics): self
  549. {
  550. $this->propertyCharacteristics = $propertyCharacteristics;
  551. return $this;
  552. }
  553. public function getBehaviorAddToCart(): ?string
  554. {
  555. return $this->behaviorAddToCart;
  556. }
  557. public function setBehaviorAddToCart(?string $behaviorAddToCart): self
  558. {
  559. $this->behaviorAddToCart = $behaviorAddToCart;
  560. return $this;
  561. }
  562. public function getBehaviorPrice(): ?string
  563. {
  564. return $this->behaviorPrice;
  565. }
  566. public function getBehaviorPriceInherited()
  567. {
  568. return $this->getBehaviorPrice() ;
  569. }
  570. public function setBehaviorPrice(?string $behaviorPrice): self
  571. {
  572. $this->behaviorPrice = $behaviorPrice;
  573. return $this;
  574. }
  575. public function hasProductsWithVariousWeight()
  576. {
  577. if ($this->getActiveProducts()) {
  578. $arrayCountProducts = [];
  579. $products = $this->getProducts();
  580. foreach ($products as $product) {
  581. $titleProduct = $product->getTitleInherited();
  582. if (!isset($arrayCountProducts[$titleProduct])) {
  583. $arrayCountProducts[$titleProduct] = [];
  584. }
  585. if (!in_array($product->getQuantityLabelInherited(), $arrayCountProducts[$titleProduct])) {
  586. $arrayCountProducts[$titleProduct][] = $product->getQuantityLabelInherited();
  587. }
  588. if (count($arrayCountProducts[$titleProduct]) > 1) {
  589. return true;
  590. }
  591. }
  592. }
  593. return false;
  594. }
  595. public function getProductsGroupByTitle()
  596. {
  597. $arrayProductsGroupByTitle = [];
  598. $products = $this->getProducts();
  599. foreach ($products as $product) {
  600. if($product->getStatus() == 1) {
  601. $titleProduct = $product->getTitleInherited();
  602. if (!isset($arrayProductsGroupByTitle[$titleProduct])) {
  603. $arrayProductsGroupByTitle[$titleProduct] = [];
  604. }
  605. $arrayProductsGroupByTitle[$titleProduct][] = $product;
  606. }
  607. }
  608. return $arrayProductsGroupByTitle;
  609. }
  610. }