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.

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