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 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. //Champ hydraté par ProductFamilyUtils
  17. protected $reductionCatalog;
  18. /**
  19. * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\MerchantInterface", inversedBy="productFamilies")
  20. * @ORM\JoinColumn(nullable=false)
  21. */
  22. protected $merchant;
  23. /**
  24. * @ORM\ManyToMany(targetEntity="Lc\ShopBundle\Context\ProductCategoryInterface", inversedBy="productFamilies")
  25. */
  26. protected $productCategories;
  27. /**
  28. * @ORM\Column(type="boolean")
  29. */
  30. protected $activeProducts;
  31. /**
  32. * @ORM\Column(type="string", length=255, nullable=true)
  33. */
  34. protected $productsType;
  35. /**
  36. * @ORM\OneToMany(targetEntity="Lc\ShopBundle\Context\ProductInterface", mappedBy="productFamily", orphanRemoval=true, cascade={"persist"}, fetch="EAGER")
  37. */
  38. protected $products;
  39. /**
  40. * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\TaxRateInterface")
  41. */
  42. protected $supplierTaxRate;
  43. /**
  44. * @ORM\Column(type="string", length=255, nullable=true)
  45. */
  46. protected $subtitle;
  47. /**
  48. * @ORM\Column(type="text", nullable=true)
  49. */
  50. protected $warningMessage;
  51. /**
  52. * @ORM\Column(type="string", length=255, nullable=true)
  53. */
  54. protected $warningMessageType;
  55. /**
  56. * @ORM\Column(type="text", nullable=true)
  57. */
  58. protected $note;
  59. /**
  60. * @ORM\Column(type="string", length=31, nullable=true)
  61. */
  62. protected $behaviorOutOfStock;
  63. /**
  64. * @ORM\Column(type="string", length=31)
  65. */
  66. protected $behaviorCountStock;
  67. /**
  68. * @ORM\Column(type="date", nullable=true)
  69. */
  70. protected $propertyNoveltyExpirationDate;
  71. /**
  72. * @ORM\Column(type="string", length=255, nullable=true)
  73. */
  74. protected $propertyOrganicLabel;
  75. /**
  76. * @ORM\Column(type="text", nullable=true)
  77. */
  78. protected $propertyAllergens;
  79. /**
  80. * @ORM\Column(type="text", nullable=true)
  81. */
  82. protected $propertyComposition;
  83. /**
  84. * @ORM\Column(type="text", nullable=true)
  85. */
  86. protected $propertyFragrances;
  87. /**
  88. * @ORM\Column(type="text", nullable=true)
  89. */
  90. protected $propertyWeightQuantity;
  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="string", length=255, nullable=true)
  101. */
  102. protected $behaviorExpirationDate;
  103. /**
  104. * @ORM\Column(type="string", length=255, nullable=true)
  105. */
  106. protected $typeExpirationDate;
  107. /**
  108. * @ORM\Column(type="string", length=32, nullable=true)
  109. */
  110. protected $behaviorAddToCart;
  111. /**
  112. * @ORM\Column(type="string", length=31)
  113. */
  114. protected $behaviorPrice;
  115. public function __construct()
  116. {
  117. $this->productCategories = new ArrayCollection();
  118. $this->products = new ArrayCollection();
  119. }
  120. public function __toString()
  121. {
  122. return $this->getTitle();
  123. }
  124. public function getTaxRateInherited()
  125. {
  126. if ($this->getTaxRate()) {
  127. return $this->getTaxRate();
  128. } else {
  129. return $this->getMerchant()->getTaxRate();
  130. }
  131. }
  132. public function getMerchant(): ?Merchant
  133. {
  134. return $this->merchant;
  135. }
  136. public function setMerchant(?Merchant $merchant): self
  137. {
  138. $this->merchant = $merchant;
  139. return $this;
  140. }
  141. public function getActiveProducts(): ?bool
  142. {
  143. return $this->activeProducts;
  144. }
  145. public function setActiveProducts(bool $activeProducts): self
  146. {
  147. $this->activeProducts = $activeProducts;
  148. return $this;
  149. }
  150. public function getProductsType(): ?string
  151. {
  152. return $this->productsType;
  153. }
  154. public function setProductsType(?string $productsType): self
  155. {
  156. $this->productsType = $productsType;
  157. return $this;
  158. }
  159. /**
  160. * @return Collection|ProductInterface[]
  161. */
  162. public function getProducts(): Collection
  163. {
  164. return $this->products;
  165. }
  166. public function addProduct(ProductInterface $product): self
  167. {
  168. if (!$this->products->contains($product)) {
  169. $this->products[] = $product;
  170. $product->setProductFamily($this);
  171. }
  172. return $this;
  173. }
  174. public function removeProduct(ProductInterface $product): self
  175. {
  176. if ($this->products->contains($product)) {
  177. $this->products->removeElement($product);
  178. // set the owning side to null (unless already changed)
  179. if ($product->getProductFamily() === $this) {
  180. $product->setProductFamily(null);
  181. }
  182. }
  183. return $this;
  184. }
  185. public function getReductionCatalog(): ?ReductionCatalog
  186. {
  187. return $this->reductionCatalog;
  188. }
  189. public function setReductionCatalog(?ReductionCatalog $reductionCatalog): self
  190. {
  191. $this->reductionCatalog = $reductionCatalog;
  192. return $this;
  193. }
  194. /**
  195. * @return Collection|ProductCategory[]
  196. */
  197. public function getProductCategories(): Collection
  198. {
  199. return $this->productCategories;
  200. }
  201. public function initProductCategories()
  202. {
  203. $this->productCategories = new ArrayCollection();
  204. }
  205. public function addProductCategory(ProductCategory $productCategory): self
  206. {
  207. if (!$this->productCategories->contains($productCategory)) {
  208. $this->productCategories[] = $productCategory;
  209. }
  210. return $this;
  211. }
  212. public function removeProductCategory(ProductCategory $productCategory): self
  213. {
  214. if ($this->productCategories->contains($productCategory)) {
  215. $this->productCategories->removeElement($productCategory);
  216. }
  217. return $this;
  218. }
  219. public function getProductCategoryParent()
  220. {
  221. $productCategories = $this->getProductCategories();
  222. if (count($productCategories) > 0) {
  223. return $productCategories[0]->getParent();
  224. }
  225. return false;
  226. }
  227. public function getProductCategoryChild()
  228. {
  229. $productCategories = $this->getProductCategories();
  230. foreach ($productCategories as $productCategory) {
  231. if ($productCategory->getParent()) {
  232. return $productCategory;
  233. }
  234. }
  235. return false;
  236. }
  237. public function getSupplierTaxRate(): ?TaxRate
  238. {
  239. return $this->supplierTaxRate;
  240. }
  241. public function setSupplierTaxRate(?TaxRate $supplierTaxRate): self
  242. {
  243. $this->supplierTaxRate = $supplierTaxRate;
  244. return $this;
  245. }
  246. public function getSubtitle(): ?string
  247. {
  248. return $this->subtitle;
  249. }
  250. public function setSubtitle(?string $subtitle): self
  251. {
  252. $this->subtitle = $subtitle;
  253. return $this;
  254. }
  255. public function getWarningMessage(): ?string
  256. {
  257. return $this->warningMessage;
  258. }
  259. public function setWarningMessage(?string $warningMessage): self
  260. {
  261. $this->warningMessage = $warningMessage;
  262. return $this;
  263. }
  264. public function getWarningMessageType(): ?string
  265. {
  266. return $this->warningMessageType;
  267. }
  268. public function setWarningMessageType(?string $warningMessageType): self
  269. {
  270. $this->warningMessageType = $warningMessageType;
  271. return $this;
  272. }
  273. public function getNote(): ?string
  274. {
  275. return $this->note;
  276. }
  277. public function setNote(?string $note): self
  278. {
  279. $this->note = $note;
  280. return $this;
  281. }
  282. public function getBehaviorOutOfStock(): ?string
  283. {
  284. return $this->behaviorOutOfStock;
  285. }
  286. public function setBehaviorOutOfStock(?string $behaviorOutOfStock): self
  287. {
  288. $this->behaviorOutOfStock = $behaviorOutOfStock;
  289. return $this;
  290. }
  291. public function getBehaviorCountStock(): ?string
  292. {
  293. return $this->behaviorCountStock;
  294. }
  295. public function setBehaviorCountStock(string $behaviorCountStock): self
  296. {
  297. $this->behaviorCountStock = $behaviorCountStock;
  298. return $this;
  299. }
  300. private function getCheapestOrMostExpensiveProduct($comparisonFunction, $returnSelfIfNotActiveProducts)
  301. {
  302. $products = $this->getProducts()->getValues();
  303. if (count($products) > 0) {
  304. usort($products, $comparisonFunction);
  305. return $products[0];
  306. }
  307. if ($returnSelfIfNotActiveProducts) {
  308. return $this;
  309. } else {
  310. return false;
  311. }
  312. }
  313. public function getCheapestProduct()
  314. {
  315. return $this->getCheapestOrMostExpensiveProduct(function ($a, $b) {
  316. return $a->getPriceInherited() > $b->getPriceInherited();
  317. }, true);
  318. }
  319. public function getCheapestProductByUnitRef()
  320. {
  321. return $this->getCheapestOrMostExpensiveProduct(function ($a, $b) {
  322. return $a->getPriceByUnitRef() > $b->getPriceByUnitRef();
  323. }, false);
  324. }
  325. public function getMostExpensiveProductByUnitRef()
  326. {
  327. return $this->getCheapestOrMostExpensiveProduct(function ($a, $b) {
  328. return $a->getPriceByUnitRef() < $b->getPriceByUnitRef();
  329. }, false);
  330. }
  331. public function isPropertyNoveltyOnline(): ?bool
  332. {
  333. if ($this->getPropertyNoveltyExpirationDate()) {
  334. $now = new \DateTime();
  335. if ($now <= $this->getPropertyNoveltyExpirationDate()) {
  336. return true;
  337. }
  338. }
  339. return false;
  340. }
  341. public function getPropertyNoveltyExpirationDate(): ?\DateTimeInterface
  342. {
  343. return $this->propertyNoveltyExpirationDate;
  344. }
  345. public function setPropertyNoveltyExpirationDate(?\DateTimeInterface $propertyNoveltyExpirationDate): self
  346. {
  347. $this->propertyNoveltyExpirationDate = $propertyNoveltyExpirationDate;
  348. return $this;
  349. }
  350. public function getPropertyOrganicLabel(): ?string
  351. {
  352. return $this->propertyOrganicLabel;
  353. }
  354. public function setPropertyOrganicLabel(?string $propertyOrganicLabel): self
  355. {
  356. $this->propertyOrganicLabel = $propertyOrganicLabel;
  357. return $this;
  358. }
  359. public function getPropertyAllergens(): ?string
  360. {
  361. return $this->propertyAllergens;
  362. }
  363. public function setPropertyAllergens(?string $propertyAllergens): self
  364. {
  365. $this->propertyAllergens = $propertyAllergens;
  366. return $this;
  367. }
  368. public function getPropertyComposition(): ?string
  369. {
  370. return $this->propertyComposition;
  371. }
  372. public function setPropertyComposition(?string $propertyComposition): self
  373. {
  374. $this->propertyComposition = $propertyComposition;
  375. return $this;
  376. }
  377. public function getPropertyFragrances(): ?string
  378. {
  379. return $this->propertyFragrances;
  380. }
  381. public function setPropertyFragrances(?string $propertyFragrances): self
  382. {
  383. $this->propertyFragrances = $propertyFragrances;
  384. return $this;
  385. }
  386. public function countProperties(): bool
  387. {
  388. $count = 0;
  389. $count += (int)strlen($this->getPropertyAllergens()) > 0;
  390. $count += (int)strlen($this->getPropertyComposition()) > 0;
  391. $count += (int)strlen($this->getPropertyFragrances()) > 0;
  392. $count += (int)strlen($this->getPropertyOrganicLabel()) > 0;
  393. $count += (int)($this->getPropertyExpirationDate() != null);
  394. return $count;
  395. }
  396. public function getBehaviorExpirationDate(): ?string
  397. {
  398. return $this->behaviorExpirationDate;
  399. }
  400. public function setBehaviorExpirationDate(?string $behaviorExpirationDate): self
  401. {
  402. $this->behaviorExpirationDate = $behaviorExpirationDate;
  403. return $this;
  404. }
  405. public function getTypeExpirationDate(): ?string
  406. {
  407. return $this->typeExpirationDate;
  408. }
  409. public function setTypeExpirationDate(?string $typeExpirationDate): self
  410. {
  411. $this->typeExpirationDate = $typeExpirationDate;
  412. return $this;
  413. }
  414. public function getPropertyWeightQuantity(): ?string
  415. {
  416. return $this->propertyWeightQuantity;
  417. }
  418. public function setPropertyWeightQuantity(?string $propertyWeightQuantity): self
  419. {
  420. $this->propertyWeightQuantity = $propertyWeightQuantity;
  421. return $this;
  422. }
  423. public function getPropertyPackaging(): ?string
  424. {
  425. return $this->propertyPackaging;
  426. }
  427. public function setPropertyPackaging(?string $propertyPackaging): self
  428. {
  429. $this->propertyPackaging = $propertyPackaging;
  430. return $this;
  431. }
  432. public function getPropertyCharacteristics(): ?string
  433. {
  434. return $this->propertyCharacteristics;
  435. }
  436. public function setPropertyCharacteristics(?string $propertyCharacteristics): self
  437. {
  438. $this->propertyCharacteristics = $propertyCharacteristics;
  439. return $this;
  440. }
  441. public function getBehaviorAddToCart(): ?string
  442. {
  443. return $this->behaviorAddToCart;
  444. }
  445. public function setBehaviorAddToCart(?string $behaviorAddToCart): self
  446. {
  447. $this->behaviorAddToCart = $behaviorAddToCart;
  448. return $this;
  449. }
  450. public function getBehaviorPrice(): ?string
  451. {
  452. return $this->behaviorPrice;
  453. }
  454. public function setBehaviorPrice(?string $behaviorPrice): self
  455. {
  456. $this->behaviorPrice = $behaviorPrice;
  457. return $this;
  458. }
  459. public function hasProductsWithVariousWeight()
  460. {
  461. if ($this->getActiveProducts()) {
  462. $arrayCountProducts = [];
  463. $products = $this->getProducts();
  464. foreach ($products as $product) {
  465. $titleProduct = $product->getTitleInherited();
  466. if (!isset($arrayCountProducts[$titleProduct])) {
  467. $arrayCountProducts[$titleProduct] = [];
  468. }
  469. if (!in_array($product->getQuantityLabelInherited(), $arrayCountProducts[$titleProduct])) {
  470. $arrayCountProducts[$titleProduct][] = $product->getQuantityLabelInherited();
  471. }
  472. if (count($arrayCountProducts[$titleProduct]) > 1) {
  473. return true;
  474. }
  475. }
  476. }
  477. return false;
  478. }
  479. public function getProductsGroupByTitle()
  480. {
  481. $arrayProductsGroupByTitle = [];
  482. $products = $this->getProducts();
  483. foreach ($products as $product) {
  484. $titleProduct = $product->getTitleInherited();
  485. if (!isset($arrayProductsGroupByTitle[$titleProduct])) {
  486. $arrayProductsGroupByTitle[$titleProduct] = [];
  487. }
  488. $arrayProductsGroupByTitle[$titleProduct][] = $product;
  489. }
  490. return $arrayProductsGroupByTitle;
  491. }
  492. }