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.

463 lines
13KB

  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. 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 getSupplierTaxRate(): ?TaxRate
  190. {
  191. return $this->supplierTaxRate;
  192. }
  193. public function setSupplierTaxRate(?TaxRate $supplierTaxRate): self
  194. {
  195. $this->supplierTaxRate = $supplierTaxRate;
  196. return $this;
  197. }
  198. public function getSubtitle(): ?string
  199. {
  200. return $this->subtitle;
  201. }
  202. public function setSubtitle(?string $subtitle): self
  203. {
  204. $this->subtitle = $subtitle;
  205. return $this;
  206. }
  207. public function getWarningMessage(): ?string
  208. {
  209. return $this->warningMessage;
  210. }
  211. public function setWarningMessage(?string $warningMessage): self
  212. {
  213. $this->warningMessage = $warningMessage;
  214. return $this;
  215. }
  216. public function getWarningMessageType(): ?string
  217. {
  218. return $this->warningMessageType;
  219. }
  220. public function setWarningMessageType(?string $warningMessageType): self
  221. {
  222. $this->warningMessageType = $warningMessageType;
  223. return $this;
  224. }
  225. public function getNote(): ?string
  226. {
  227. return $this->note;
  228. }
  229. public function setNote(?string $note): self
  230. {
  231. $this->note = $note;
  232. return $this;
  233. }
  234. public function getBehaviorOutOfStock(): ?string
  235. {
  236. return $this->behaviorOutOfStock;
  237. }
  238. public function setBehaviorOutOfStock(?string $behaviorOutOfStock): self
  239. {
  240. $this->behaviorOutOfStock = $behaviorOutOfStock;
  241. return $this;
  242. }
  243. public function getBehaviorCountStock(): ?string
  244. {
  245. return $this->behaviorCountStock;
  246. }
  247. public function setBehaviorCountStock(string $behaviorCountStock): self
  248. {
  249. $this->behaviorCountStock = $behaviorCountStock;
  250. return $this;
  251. }
  252. private function getCheapestOrMostExpensiveProduct($comparisonFunction, $returnSelfIfNotActiveProducts)
  253. {
  254. $products = $this->getProducts()->getValues() ;
  255. if(count($products) > 0) {
  256. usort($products, $comparisonFunction) ;
  257. return $products[0] ;
  258. }
  259. if($returnSelfIfNotActiveProducts) {
  260. return $this ;
  261. }
  262. else {
  263. return false ;
  264. }
  265. }
  266. public function getCheapestProduct()
  267. {
  268. return $this->getCheapestOrMostExpensiveProduct(function($a, $b) {
  269. return $a->getPriceInherited() > $b->getPriceInherited() ;
  270. },true) ;
  271. }
  272. public function getCheapestProductByUnitRef()
  273. {
  274. return $this->getCheapestOrMostExpensiveProduct(function($a, $b) {
  275. return $a->getPriceByUnitRef() > $b->getPriceByUnitRef() ;
  276. },false) ;
  277. }
  278. public function getMostExpensiveProductByUnitRef()
  279. {
  280. return $this->getCheapestOrMostExpensiveProduct(function($a, $b) {
  281. return $a->getPriceByUnitRef() < $b->getPriceByUnitRef() ;
  282. },false) ;
  283. }
  284. public function isPropertyNoveltyOnline(): ?bool
  285. {
  286. if($this->getPropertyNoveltyExpirationDate()) {
  287. $now = new \DateTime() ;
  288. if($now <= $this->getPropertyNoveltyExpirationDate()) {
  289. return true ;
  290. }
  291. }
  292. else {
  293. return false ;
  294. }
  295. }
  296. public function getPropertyNoveltyExpirationDate(): ?\DateTimeInterface
  297. {
  298. return $this->propertyNoveltyExpirationDate;
  299. }
  300. public function setPropertyNoveltyExpirationDate(?\DateTimeInterface $propertyNoveltyExpirationDate): self
  301. {
  302. $this->propertyNoveltyExpirationDate = $propertyNoveltyExpirationDate;
  303. return $this;
  304. }
  305. public function getPropertyOrganicLabel(): ?string
  306. {
  307. return $this->propertyOrganicLabel;
  308. }
  309. public function setPropertyOrganicLabel(?string $propertyOrganicLabel): self
  310. {
  311. $this->propertyOrganicLabel = $propertyOrganicLabel;
  312. return $this;
  313. }
  314. public function getPropertyAllergens(): ?string
  315. {
  316. return $this->propertyAllergens;
  317. }
  318. public function setPropertyAllergens(?string $propertyAllergens): self
  319. {
  320. $this->propertyAllergens = $propertyAllergens;
  321. return $this;
  322. }
  323. public function getPropertyComposition(): ?string
  324. {
  325. return $this->propertyComposition;
  326. }
  327. public function setPropertyComposition(?string $propertyComposition): self
  328. {
  329. $this->propertyComposition = $propertyComposition;
  330. return $this;
  331. }
  332. public function getPropertyFragrances(): ?string
  333. {
  334. return $this->propertyFragrances;
  335. }
  336. public function setPropertyFragrances(?string $propertyFragrances): self
  337. {
  338. $this->propertyFragrances = $propertyFragrances;
  339. return $this;
  340. }
  341. public function getBehaviorExpirationDate(): ?string
  342. {
  343. return $this->behaviorExpirationDate;
  344. }
  345. public function setBehaviorExpirationDate(?string $behaviorExpirationDate): self
  346. {
  347. $this->behaviorExpirationDate = $behaviorExpirationDate;
  348. return $this;
  349. }
  350. public function getTypeExpirationDate(): ?string
  351. {
  352. return $this->typeExpirationDate;
  353. }
  354. public function setTypeExpirationDate(?string $typeExpirationDate): self
  355. {
  356. $this->typeExpirationDate = $typeExpirationDate;
  357. return $this;
  358. }
  359. }