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 10KB

4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 $note;
  53. /**
  54. * @ORM\Column(type="string", length=31, nullable=true)
  55. */
  56. protected $behaviorOutOfStock;
  57. /**
  58. * @ORM\Column(type="string", length=31)
  59. */
  60. protected $behaviorCountStock;
  61. /**
  62. * @ORM\Column(type="boolean", nullable=true)
  63. */
  64. protected $isNovelty;
  65. /**
  66. * @ORM\Column(type="date", nullable=true)
  67. */
  68. protected $noveltyExpirationDate;
  69. /**
  70. * @ORM\Column(type="boolean", nullable=true)
  71. */
  72. protected $isOrganic;
  73. /**
  74. * @ORM\Column(type="string", length=255, nullable=true)
  75. */
  76. protected $organicLabel;
  77. public function __construct()
  78. {
  79. $this->productCategories = new ArrayCollection();
  80. $this->products = new ArrayCollection();
  81. }
  82. public function getTaxRateInherited()
  83. {
  84. if($this->getTaxRate()) {
  85. return $this->getTaxRate() ;
  86. }
  87. else {
  88. return $this->getMerchant()->getTaxRate() ;
  89. }
  90. }
  91. public function getMerchant(): ?Merchant
  92. {
  93. return $this->merchant;
  94. }
  95. public function setMerchant(?Merchant $merchant): self
  96. {
  97. $this->merchant = $merchant;
  98. return $this;
  99. }
  100. public function getActiveProducts(): ?bool
  101. {
  102. return $this->activeProducts;
  103. }
  104. public function setActiveProducts(bool $activeProducts): self
  105. {
  106. $this->activeProducts = $activeProducts;
  107. return $this;
  108. }
  109. public function getProductsType(): ?string
  110. {
  111. return $this->productsType;
  112. }
  113. public function setProductsType(?string $productsType): self
  114. {
  115. $this->productsType = $productsType;
  116. return $this;
  117. }
  118. /**
  119. * @return Collection|ProductInterface[]
  120. */
  121. public function getProducts(): Collection
  122. {
  123. return $this->products;
  124. }
  125. public function addProduct(ProductInterface $product): self
  126. {
  127. if (!$this->products->contains($product)) {
  128. $this->products[] = $product;
  129. $product->setProductFamily($this);
  130. }
  131. return $this;
  132. }
  133. public function removeProduct(ProductInterface $product): self
  134. {
  135. if ($this->products->contains($product)) {
  136. $this->products->removeElement($product);
  137. // set the owning side to null (unless already changed)
  138. if ($product->getProductFamily() === $this) {
  139. $product->setProductFamily(null);
  140. }
  141. }
  142. return $this;
  143. }
  144. /**
  145. * @return Collection|ProductCategory[]
  146. */
  147. public function getProductCategories(): Collection
  148. {
  149. return $this->productCategories;
  150. }
  151. public function initProductCategories()
  152. {
  153. $this->productCategories = new ArrayCollection();
  154. }
  155. public function addProductCategory(ProductCategory $productCategory): self
  156. {
  157. if (!$this->productCategories->contains($productCategory)) {
  158. $this->productCategories[] = $productCategory;
  159. }
  160. return $this;
  161. }
  162. public function removeProductCategory(ProductCategory $productCategory): self
  163. {
  164. if ($this->productCategories->contains($productCategory)) {
  165. $this->productCategories->removeElement($productCategory);
  166. }
  167. return $this;
  168. }
  169. public function getSupplierTaxRate(): ?TaxRate
  170. {
  171. return $this->supplierTaxRate;
  172. }
  173. public function setSupplierTaxRate(?TaxRate $supplierTaxRate): self
  174. {
  175. $this->supplierTaxRate = $supplierTaxRate;
  176. return $this;
  177. }
  178. public function getSubtitle(): ?string
  179. {
  180. return $this->subtitle;
  181. }
  182. public function setSubtitle(?string $subtitle): self
  183. {
  184. $this->subtitle = $subtitle;
  185. return $this;
  186. }
  187. public function getNote(): ?string
  188. {
  189. return $this->note;
  190. }
  191. public function setNote(?string $note): self
  192. {
  193. $this->note = $note;
  194. return $this;
  195. }
  196. public function getBehaviorOutOfStock(): ?string
  197. {
  198. return $this->behaviorOutOfStock;
  199. }
  200. public function setBehaviorOutOfStock(?string $behaviorOutOfStock): self
  201. {
  202. $this->behaviorOutOfStock = $behaviorOutOfStock;
  203. return $this;
  204. }
  205. public function getBehaviorCountStock(): ?string
  206. {
  207. return $this->behaviorCountStock;
  208. }
  209. public function setBehaviorCountStock(string $behaviorCountStock): self
  210. {
  211. $this->behaviorCountStock = $behaviorCountStock;
  212. return $this;
  213. }
  214. public function getIsNovelty(): ?bool
  215. {
  216. return $this->isNovelty;
  217. }
  218. public function isNoveltyOnline(): ?bool
  219. {
  220. if($this->isNovelty) {
  221. if($this->getNoveltyExpirationDate()) {
  222. $now = new \DateTime() ;
  223. if($now <= $this->getNoveltyExpirationDate()) {
  224. return true ;
  225. }
  226. }
  227. else {
  228. return true ;
  229. }
  230. }
  231. return false ;
  232. }
  233. public function setIsNovelty(?bool $isNovelty): self
  234. {
  235. $this->isNovelty = $isNovelty;
  236. return $this;
  237. }
  238. public function getNoveltyExpirationDate(): ?\DateTimeInterface
  239. {
  240. return $this->noveltyExpirationDate;
  241. }
  242. public function setNoveltyExpirationDate(?\DateTimeInterface $noveltyExpirationDate): self
  243. {
  244. $this->noveltyExpirationDate = $noveltyExpirationDate;
  245. return $this;
  246. }
  247. public function getIsOrganic(): ?bool
  248. {
  249. return $this->isOrganic;
  250. }
  251. public function setIsOrganic(?bool $isOrganic): self
  252. {
  253. $this->isOrganic = $isOrganic;
  254. return $this;
  255. }
  256. public function getOrganicLabel(): ?string
  257. {
  258. return $this->organicLabel;
  259. }
  260. public function setOrganicLabel(?string $organicLabel): self
  261. {
  262. $this->organicLabel = $organicLabel;
  263. return $this;
  264. }
  265. private function getCheapestOrMostExpensiveProduct($comparisonFunction, $returnSelfIfNotActiveProducts)
  266. {
  267. $products = $this->getProducts()->getValues() ;
  268. if(count($products) > 0) {
  269. usort($products, $comparisonFunction) ;
  270. return $products[0] ;
  271. }
  272. if($returnSelfIfNotActiveProducts) {
  273. return $this ;
  274. }
  275. else {
  276. return false ;
  277. }
  278. }
  279. public function getCheapestProduct()
  280. {
  281. return $this->getCheapestOrMostExpensiveProduct(function($a, $b) {
  282. return $a->getPriceInherited() > $b->getPriceInherited() ;
  283. },true) ;
  284. }
  285. public function getCheapestProductByUnitRef()
  286. {
  287. return $this->getCheapestOrMostExpensiveProduct(function($a, $b) {
  288. return $a->getPriceByUnitRef() > $b->getPriceByUnitRef() ;
  289. },false) ;
  290. }
  291. public function getMostExpensiveProductByUnitRef()
  292. {
  293. return $this->getCheapestOrMostExpensiveProduct(function($a, $b) {
  294. return $a->getPriceByUnitRef() < $b->getPriceByUnitRef() ;
  295. },false) ;
  296. }
  297. }