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.

ProductFamily.php 20KB

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