ProductFamily.php 16KB

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