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.

595 lines
17KB

  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. public function __construct()
  110. {
  111. $this->productCategories = new ArrayCollection();
  112. $this->products = new ArrayCollection();
  113. }
  114. public function getTaxRateInherited()
  115. {
  116. if ($this->getTaxRate()) {
  117. return $this->getTaxRate();
  118. } else {
  119. return $this->getMerchant()->getTaxRate();
  120. }
  121. }
  122. public function getMerchant(): ?Merchant
  123. {
  124. return $this->merchant;
  125. }
  126. public function setMerchant(?Merchant $merchant): self
  127. {
  128. $this->merchant = $merchant;
  129. return $this;
  130. }
  131. public function getActiveProducts(): ?bool
  132. {
  133. return $this->activeProducts;
  134. }
  135. public function setActiveProducts(bool $activeProducts): self
  136. {
  137. $this->activeProducts = $activeProducts;
  138. return $this;
  139. }
  140. public function getProductsType(): ?string
  141. {
  142. return $this->productsType;
  143. }
  144. public function setProductsType(?string $productsType): self
  145. {
  146. $this->productsType = $productsType;
  147. return $this;
  148. }
  149. /**
  150. * @return Collection|ProductInterface[]
  151. */
  152. public function getProducts(): Collection
  153. {
  154. return $this->products;
  155. }
  156. public function addProduct(ProductInterface $product): self
  157. {
  158. if (!$this->products->contains($product)) {
  159. $this->products[] = $product;
  160. $product->setProductFamily($this);
  161. }
  162. return $this;
  163. }
  164. public function removeProduct(ProductInterface $product): self
  165. {
  166. if ($this->products->contains($product)) {
  167. $this->products->removeElement($product);
  168. // set the owning side to null (unless already changed)
  169. if ($product->getProductFamily() === $this) {
  170. $product->setProductFamily(null);
  171. }
  172. }
  173. return $this;
  174. }
  175. /**
  176. * @return Collection|ProductCategory[]
  177. */
  178. public function getProductCategories(): Collection
  179. {
  180. return $this->productCategories;
  181. }
  182. public function initProductCategories()
  183. {
  184. $this->productCategories = new ArrayCollection();
  185. }
  186. public function addProductCategory(ProductCategory $productCategory): self
  187. {
  188. if (!$this->productCategories->contains($productCategory)) {
  189. $this->productCategories[] = $productCategory;
  190. }
  191. return $this;
  192. }
  193. public function removeProductCategory(ProductCategory $productCategory): self
  194. {
  195. if ($this->productCategories->contains($productCategory)) {
  196. $this->productCategories->removeElement($productCategory);
  197. }
  198. return $this;
  199. }
  200. public function getProductCategoryParent()
  201. {
  202. $productCategories = $this->getProductCategories();
  203. if (count($productCategories) > 0) {
  204. return $productCategories[0]->getParent();
  205. }
  206. return false;
  207. }
  208. public function getProductCategoryChild()
  209. {
  210. $productCategories = $this->getProductCategories();
  211. foreach ($productCategories as $productCategory) {
  212. if ($productCategory->getParent()) {
  213. return $productCategory;
  214. }
  215. }
  216. return false;
  217. }
  218. public function getSupplierTaxRate(): ?TaxRate
  219. {
  220. return $this->supplierTaxRate;
  221. }
  222. public function setSupplierTaxRate(?TaxRate $supplierTaxRate): self
  223. {
  224. $this->supplierTaxRate = $supplierTaxRate;
  225. return $this;
  226. }
  227. public function getSubtitle(): ?string
  228. {
  229. return $this->subtitle;
  230. }
  231. public function setSubtitle(?string $subtitle): self
  232. {
  233. $this->subtitle = $subtitle;
  234. return $this;
  235. }
  236. public function getWarningMessage(): ?string
  237. {
  238. return $this->warningMessage;
  239. }
  240. public function setWarningMessage(?string $warningMessage): self
  241. {
  242. $this->warningMessage = $warningMessage;
  243. return $this;
  244. }
  245. public function getWarningMessageType(): ?string
  246. {
  247. return $this->warningMessageType;
  248. }
  249. public function setWarningMessageType(?string $warningMessageType): self
  250. {
  251. $this->warningMessageType = $warningMessageType;
  252. return $this;
  253. }
  254. public function getNote(): ?string
  255. {
  256. return $this->note;
  257. }
  258. public function setNote(?string $note): self
  259. {
  260. $this->note = $note;
  261. return $this;
  262. }
  263. public function getBehaviorOutOfStock(): ?string
  264. {
  265. return $this->behaviorOutOfStock;
  266. }
  267. public function setBehaviorOutOfStock(?string $behaviorOutOfStock): self
  268. {
  269. $this->behaviorOutOfStock = $behaviorOutOfStock;
  270. return $this;
  271. }
  272. public function getBehaviorCountStock(): ?string
  273. {
  274. return $this->behaviorCountStock;
  275. }
  276. public function setBehaviorCountStock(string $behaviorCountStock): self
  277. {
  278. $this->behaviorCountStock = $behaviorCountStock;
  279. return $this;
  280. }
  281. private function getCheapestOrMostExpensiveProduct($comparisonFunction, $returnSelfIfNotActiveProducts)
  282. {
  283. $products = $this->getProducts()->getValues();
  284. if (count($products) > 0) {
  285. usort($products, $comparisonFunction);
  286. return $products[0];
  287. }
  288. if ($returnSelfIfNotActiveProducts) {
  289. return $this;
  290. } else {
  291. return false;
  292. }
  293. }
  294. public function getCheapestProduct()
  295. {
  296. return $this->getCheapestOrMostExpensiveProduct(function ($a, $b) {
  297. return $a->getPriceInherited() > $b->getPriceInherited();
  298. }, true);
  299. }
  300. public function getCheapestProductByUnitRef()
  301. {
  302. return $this->getCheapestOrMostExpensiveProduct(function ($a, $b) {
  303. return $a->getPriceByUnitRef() > $b->getPriceByUnitRef();
  304. }, false);
  305. }
  306. public function getMostExpensiveProductByUnitRef()
  307. {
  308. return $this->getCheapestOrMostExpensiveProduct(function ($a, $b) {
  309. return $a->getPriceByUnitRef() < $b->getPriceByUnitRef();
  310. }, false);
  311. }
  312. public function isPropertyNoveltyOnline(): ?bool
  313. {
  314. if ($this->getPropertyNoveltyExpirationDate()) {
  315. $now = new \DateTime();
  316. if ($now <= $this->getPropertyNoveltyExpirationDate()) {
  317. return true;
  318. }
  319. }
  320. return false;
  321. }
  322. public function getPropertyNoveltyExpirationDate(): ?\DateTimeInterface
  323. {
  324. return $this->propertyNoveltyExpirationDate;
  325. }
  326. public function setPropertyNoveltyExpirationDate(?\DateTimeInterface $propertyNoveltyExpirationDate): self
  327. {
  328. $this->propertyNoveltyExpirationDate = $propertyNoveltyExpirationDate;
  329. return $this;
  330. }
  331. public function getPropertyOrganicLabel(): ?string
  332. {
  333. return $this->propertyOrganicLabel;
  334. }
  335. public function setPropertyOrganicLabel(?string $propertyOrganicLabel): self
  336. {
  337. $this->propertyOrganicLabel = $propertyOrganicLabel;
  338. return $this;
  339. }
  340. public function getPropertyAllergens(): ?string
  341. {
  342. return $this->propertyAllergens;
  343. }
  344. public function setPropertyAllergens(?string $propertyAllergens): self
  345. {
  346. $this->propertyAllergens = $propertyAllergens;
  347. return $this;
  348. }
  349. public function getPropertyComposition(): ?string
  350. {
  351. return $this->propertyComposition;
  352. }
  353. public function setPropertyComposition(?string $propertyComposition): self
  354. {
  355. $this->propertyComposition = $propertyComposition;
  356. return $this;
  357. }
  358. public function getPropertyFragrances(): ?string
  359. {
  360. return $this->propertyFragrances;
  361. }
  362. public function setPropertyFragrances(?string $propertyFragrances): self
  363. {
  364. $this->propertyFragrances = $propertyFragrances;
  365. return $this;
  366. }
  367. public function countProperties(): bool
  368. {
  369. $count = 0;
  370. $count += (int)strlen($this->getPropertyAllergens()) > 0;
  371. $count += (int)strlen($this->getPropertyComposition()) > 0;
  372. $count += (int)strlen($this->getPropertyFragrances()) > 0;
  373. $count += (int)strlen($this->getPropertyOrganicLabel()) > 0;
  374. $count += (int)($this->getPropertyExpirationDate() != null);
  375. return $count;
  376. }
  377. public function getBehaviorExpirationDate(): ?string
  378. {
  379. return $this->behaviorExpirationDate;
  380. }
  381. public function setBehaviorExpirationDate(?string $behaviorExpirationDate): self
  382. {
  383. $this->behaviorExpirationDate = $behaviorExpirationDate;
  384. return $this;
  385. }
  386. public function getTypeExpirationDate(): ?string
  387. {
  388. return $this->typeExpirationDate;
  389. }
  390. public function setTypeExpirationDate(?string $typeExpirationDate): self
  391. {
  392. $this->typeExpirationDate = $typeExpirationDate;
  393. return $this;
  394. }
  395. public function getPropertyWeightQuantity(): ?string
  396. {
  397. return $this->propertyWeightQuantity;
  398. }
  399. public function setPropertyWeightQuantity(?string $propertyWeightQuantity): self
  400. {
  401. $this->propertyWeightQuantity = $propertyWeightQuantity;
  402. return $this;
  403. }
  404. public function getPropertyPackaging(): ?string
  405. {
  406. return $this->propertyPackaging;
  407. }
  408. public function setPropertyPackaging(?string $propertyPackaging): self
  409. {
  410. $this->propertyPackaging = $propertyPackaging;
  411. return $this;
  412. }
  413. public function getPropertyCharacteristics(): ?string
  414. {
  415. return $this->propertyCharacteristics;
  416. }
  417. public function setPropertyCharacteristics(?string $propertyCharacteristics): self
  418. {
  419. $this->propertyCharacteristics = $propertyCharacteristics;
  420. return $this;
  421. }
  422. public function getBehaviorAddToCart(): ?string
  423. {
  424. return $this->behaviorAddToCart;
  425. }
  426. public function setBehaviorAddToCart(?string $behaviorAddToCart): self
  427. {
  428. $this->behaviorAddToCart = $behaviorAddToCart;
  429. return $this;
  430. }
  431. public function hasProductsWithVariousWeight()
  432. {
  433. if ($this->getActiveProducts()) {
  434. $arrayCountProducts = [];
  435. $products = $this->getProducts();
  436. foreach ($products as $product) {
  437. $titleProduct = $product->getTitleInherited();
  438. if (!isset($arrayCountProducts[$titleProduct])) {
  439. $arrayCountProducts[$titleProduct] = [];
  440. }
  441. if (!in_array($product->getQuantityLabelInherited(), $arrayCountProducts[$titleProduct])) {
  442. $arrayCountProducts[$titleProduct][] = $product->getQuantityLabelInherited();
  443. }
  444. if (count($arrayCountProducts[$titleProduct]) > 1) {
  445. return true;
  446. }
  447. }
  448. }
  449. return false;
  450. }
  451. public function getProductsGroupByTitle()
  452. {
  453. $arrayProductsGroupByTitle = [];
  454. $products = $this->getProducts();
  455. foreach ($products as $product) {
  456. $titleProduct = $product->getTitleInherited();
  457. if (!isset($arrayProductsGroupByTitle[$titleProduct])) {
  458. $arrayProductsGroupByTitle[$titleProduct] = [];
  459. }
  460. $arrayProductsGroupByTitle[$titleProduct][] = $product;
  461. }
  462. return $arrayProductsGroupByTitle;
  463. }
  464. }