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.

631 lines
18KB

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