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.

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