Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

570 lines
16KB

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