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.

ProductFamily.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. return false ;
  293. }
  294. public function getPropertyNoveltyExpirationDate(): ?\DateTimeInterface
  295. {
  296. return $this->propertyNoveltyExpirationDate;
  297. }
  298. public function setPropertyNoveltyExpirationDate(?\DateTimeInterface $propertyNoveltyExpirationDate): self
  299. {
  300. $this->propertyNoveltyExpirationDate = $propertyNoveltyExpirationDate;
  301. return $this;
  302. }
  303. public function getPropertyOrganicLabel(): ?string
  304. {
  305. return $this->propertyOrganicLabel;
  306. }
  307. public function setPropertyOrganicLabel(?string $propertyOrganicLabel): self
  308. {
  309. $this->propertyOrganicLabel = $propertyOrganicLabel;
  310. return $this;
  311. }
  312. public function getPropertyAllergens(): ?string
  313. {
  314. return $this->propertyAllergens;
  315. }
  316. public function setPropertyAllergens(?string $propertyAllergens): self
  317. {
  318. $this->propertyAllergens = $propertyAllergens;
  319. return $this;
  320. }
  321. public function getPropertyComposition(): ?string
  322. {
  323. return $this->propertyComposition;
  324. }
  325. public function setPropertyComposition(?string $propertyComposition): self
  326. {
  327. $this->propertyComposition = $propertyComposition;
  328. return $this;
  329. }
  330. public function getPropertyFragrances(): ?string
  331. {
  332. return $this->propertyFragrances;
  333. }
  334. public function setPropertyFragrances(?string $propertyFragrances): self
  335. {
  336. $this->propertyFragrances = $propertyFragrances;
  337. return $this;
  338. }
  339. public function getBehaviorExpirationDate(): ?string
  340. {
  341. return $this->behaviorExpirationDate;
  342. }
  343. public function setBehaviorExpirationDate(?string $behaviorExpirationDate): self
  344. {
  345. $this->behaviorExpirationDate = $behaviorExpirationDate;
  346. return $this;
  347. }
  348. public function getTypeExpirationDate(): ?string
  349. {
  350. return $this->typeExpirationDate;
  351. }
  352. public function setTypeExpirationDate(?string $typeExpirationDate): self
  353. {
  354. $this->typeExpirationDate = $typeExpirationDate;
  355. return $this;
  356. }
  357. }