vendor/roothirsch/tuer24-bundle/src/Entity/Tuer24Product.php line 15

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\Tuer24Bundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Gedmo\Mapping\Annotation as Gedmo;
  8. use Gedmo\Timestampable\Traits\TimestampableEntity;
  9. #[ORM\Entity(repositoryClass: "Roothirsch\Tuer24Bundle\Repository\Tuer24ProductRepository")]
  10. #[ORM\Table(name: 'roothirsch_tuer24_product')]
  11. #[Assert\Callback(['Roothirsch\Tuer24Bundle\Validator\TranslationValidator', 'validateProductTranslations'])]
  12. class Tuer24Product
  13. {
  14. use TimestampableEntity;
  15. #[ORM\Id]
  16. #[ORM\GeneratedValue]
  17. #[ORM\Column(type: 'integer')]
  18. private $id;
  19. #[ORM\Column(type: 'string', length: 100, unique: true)]
  20. #[Assert\NotBlank]
  21. #[Assert\Length(max: 100)]
  22. private $sku;
  23. #[ORM\Column(type: 'string', length: 255)]
  24. #[Assert\Length(max: 255)]
  25. private $title = '';
  26. #[ORM\Column(type: 'text', nullable: true)]
  27. private $shortDescription;
  28. #[ORM\Column(type: 'text', nullable: true)]
  29. private $description;
  30. #[ORM\Column(type: 'string', length: 255, nullable: true)]
  31. private $imagePath;
  32. #[ORM\Column(type: 'decimal', precision: 15, scale: 2)]
  33. #[Assert\NotBlank]
  34. #[Assert\PositiveOrZero]
  35. private $price;
  36. #[ORM\Column(type: 'decimal', precision: 15, scale: 2)]
  37. #[Assert\NotBlank]
  38. #[Assert\PositiveOrZero]
  39. private $basePrice;
  40. #[ORM\Column(type: 'decimal', precision: 5, scale: 2)]
  41. #[Assert\NotBlank]
  42. #[Assert\Range(min: 0, max: 100)]
  43. private $taxRate;
  44. #[ORM\Column(type: 'string', length: 20, nullable: true)]
  45. #[Assert\Length(max: 20)]
  46. private $unit;
  47. #[ORM\Column(type: 'decimal', precision: 10, scale: 2, nullable: true)]
  48. #[Assert\PositiveOrZero]
  49. private $weight;
  50. #[ORM\Column(type: 'boolean')]
  51. private $enabled = true;
  52. #[ORM\OneToMany(targetEntity: Tuer24ProductTranslation::class, mappedBy: 'product', cascade: ['persist', 'remove'], orphanRemoval: true)]
  53. private $translations;
  54. #[ORM\ManyToMany(targetEntity: Tuer24Category::class, inversedBy: 'products')]
  55. #[ORM\JoinTable(name: 'tuer24_product_category')]
  56. private $categories;
  57. #[ORM\OneToMany(targetEntity: Tuer24ProductPrice::class, mappedBy: 'product', cascade: ['persist', 'remove'], orphanRemoval: true)]
  58. private $prices;
  59. #[ORM\OneToOne(targetEntity: Tuer24Stock::class, mappedBy: 'product', cascade: ['persist', 'remove'], orphanRemoval: true)]
  60. private $stock;
  61. /**
  62. * Constructor
  63. */
  64. public function __construct()
  65. {
  66. $this->translations = new ArrayCollection();
  67. $this->categories = new ArrayCollection();
  68. $this->prices = new ArrayCollection();
  69. $this->basePrice = 0.0; // Set default value for basePrice
  70. $this->price = 0.0; // Set default value for price
  71. $this->taxRate = 7.7; // Default Swiss VAT rate
  72. $this->weight = 0.0; // Set default value for weight
  73. }
  74. /**
  75. * Get id
  76. *
  77. * @return int|null
  78. */
  79. public function getId(): ?int
  80. {
  81. return $this->id;
  82. }
  83. /**
  84. * Get sku
  85. *
  86. * @return string|null
  87. */
  88. public function getSku(): ?string
  89. {
  90. return $this->sku;
  91. }
  92. /**
  93. * Set sku
  94. *
  95. * @param string $sku
  96. * @return $this
  97. */
  98. public function setSku(string $sku): self
  99. {
  100. $this->sku = $sku;
  101. return $this;
  102. }
  103. /**
  104. * Get title
  105. *
  106. * @return string|null
  107. */
  108. public function getTitle(): ?string
  109. {
  110. return $this->title;
  111. }
  112. /**
  113. * Set title
  114. *
  115. * @param string $title
  116. * @return $this
  117. */
  118. public function setTitle(string $title): self
  119. {
  120. $this->title = $title;
  121. return $this;
  122. }
  123. /**
  124. * Get short description
  125. *
  126. * @return string|null
  127. */
  128. public function getShortDescription(): ?string
  129. {
  130. return $this->shortDescription;
  131. }
  132. /**
  133. * Set short description
  134. *
  135. * @param string|null $shortDescription
  136. * @return $this
  137. */
  138. public function setShortDescription(?string $shortDescription): self
  139. {
  140. $this->shortDescription = $shortDescription;
  141. return $this;
  142. }
  143. /**
  144. * Get description
  145. *
  146. * @return string|null
  147. */
  148. public function getDescription(): ?string
  149. {
  150. return $this->description;
  151. }
  152. /**
  153. * Set description
  154. *
  155. * @param string|null $description
  156. * @return $this
  157. */
  158. public function setDescription(?string $description): self
  159. {
  160. $this->description = $description;
  161. return $this;
  162. }
  163. /**
  164. * Get price
  165. *
  166. * @return float|null
  167. */
  168. public function getPrice(): ?float
  169. {
  170. return $this->price;
  171. }
  172. /**
  173. * Set price
  174. *
  175. * @param float $price
  176. * @return $this
  177. */
  178. public function setPrice(float $price): self
  179. {
  180. $this->price = $price;
  181. return $this;
  182. }
  183. /**
  184. * Get base price
  185. *
  186. * @return float|null
  187. */
  188. public function getBasePrice(): ?float
  189. {
  190. return $this->basePrice;
  191. }
  192. /**
  193. * Set base price
  194. *
  195. * @param float $basePrice
  196. * @return $this
  197. */
  198. public function setBasePrice(float $basePrice): self
  199. {
  200. $this->basePrice = $basePrice;
  201. return $this;
  202. }
  203. /**
  204. * Get tax rate
  205. *
  206. * @return float|null
  207. */
  208. public function getTaxRate(): ?float
  209. {
  210. return $this->taxRate;
  211. }
  212. /**
  213. * Set tax rate
  214. *
  215. * @param float $taxRate
  216. * @return $this
  217. */
  218. public function setTaxRate(float $taxRate): self
  219. {
  220. $this->taxRate = $taxRate;
  221. return $this;
  222. }
  223. /**
  224. * Get unit
  225. *
  226. * @return string|null
  227. */
  228. public function getUnit(): ?string
  229. {
  230. return $this->unit;
  231. }
  232. /**
  233. * Set unit
  234. *
  235. * @param string|null $unit
  236. * @return $this
  237. */
  238. public function setUnit(?string $unit): self
  239. {
  240. $this->unit = $unit;
  241. return $this;
  242. }
  243. /**
  244. * Get weight
  245. *
  246. * @return float|null
  247. */
  248. public function getWeight(): ?float
  249. {
  250. return $this->weight;
  251. }
  252. /**
  253. * Set weight
  254. *
  255. * @param float|null $weight
  256. * @return $this
  257. */
  258. public function setWeight(?float $weight): self
  259. {
  260. $this->weight = $weight;
  261. return $this;
  262. }
  263. /**
  264. * Is enabled
  265. *
  266. * @return bool|null
  267. */
  268. public function isEnabled(): ?bool
  269. {
  270. return $this->enabled;
  271. }
  272. /**
  273. * Set enabled
  274. *
  275. * @param bool $enabled
  276. * @return $this
  277. */
  278. public function setEnabled(bool $enabled): self
  279. {
  280. $this->enabled = $enabled;
  281. return $this;
  282. }
  283. /**
  284. * Get translations
  285. *
  286. * @return Collection|Tuer24ProductTranslation[]
  287. */
  288. public function getTranslations(): Collection
  289. {
  290. return $this->translations;
  291. }
  292. /**
  293. * Add translation
  294. *
  295. * @param Tuer24ProductTranslation $translation
  296. * @return $this
  297. */
  298. public function addTranslation(Tuer24ProductTranslation $translation): self
  299. {
  300. if (!$this->translations->contains($translation)) {
  301. $this->translations[] = $translation;
  302. $translation->setProduct($this);
  303. }
  304. return $this;
  305. }
  306. /**
  307. * Remove translation
  308. *
  309. * @param Tuer24ProductTranslation $translation
  310. * @return $this
  311. */
  312. public function removeTranslation(Tuer24ProductTranslation $translation): self
  313. {
  314. if ($this->translations->removeElement($translation)) {
  315. // set the owning side to null (unless already changed)
  316. if ($translation->getProduct() === $this) {
  317. $translation->setProduct(null);
  318. }
  319. }
  320. return $this;
  321. }
  322. /**
  323. * Get categories
  324. *
  325. * @return Collection|Tuer24Category[]
  326. */
  327. public function getCategories(): Collection
  328. {
  329. return $this->categories;
  330. }
  331. /**
  332. * Add category
  333. *
  334. * @param Tuer24Category $category
  335. * @return $this
  336. */
  337. public function addCategory(Tuer24Category $category): self
  338. {
  339. if (!$this->categories->contains($category)) {
  340. $this->categories[] = $category;
  341. $category->addProduct($this);
  342. }
  343. return $this;
  344. }
  345. /**
  346. * Remove category
  347. *
  348. * @param Tuer24Category $category
  349. * @return $this
  350. */
  351. public function removeCategory(Tuer24Category $category): self
  352. {
  353. if ($this->categories->removeElement($category)) {
  354. $category->removeProduct($this);
  355. }
  356. return $this;
  357. }
  358. /**
  359. * Get prices
  360. *
  361. * @return Collection|Tuer24ProductPrice[]
  362. */
  363. public function getPrices(): Collection
  364. {
  365. return $this->prices;
  366. }
  367. /**
  368. * Add price
  369. *
  370. * @param Tuer24ProductPrice $price
  371. * @return $this
  372. */
  373. public function addPrice(Tuer24ProductPrice $price): self
  374. {
  375. if (!$this->prices->contains($price)) {
  376. $this->prices[] = $price;
  377. $price->setProduct($this);
  378. }
  379. return $this;
  380. }
  381. /**
  382. * Remove price
  383. *
  384. * @param Tuer24ProductPrice $price
  385. * @return $this
  386. */
  387. public function removePrice(Tuer24ProductPrice $price): self
  388. {
  389. if ($this->prices->removeElement($price)) {
  390. // set the owning side to null (unless already changed)
  391. if ($price->getProduct() === $this) {
  392. $price->setProduct(null);
  393. }
  394. }
  395. return $this;
  396. }
  397. /**
  398. * Get stock
  399. *
  400. * @return Tuer24Stock|null
  401. */
  402. public function getStock(): ?Tuer24Stock
  403. {
  404. return $this->stock;
  405. }
  406. /**
  407. * Set stock
  408. *
  409. * @param Tuer24Stock|null $stock
  410. * @return $this
  411. */
  412. public function setStock(?Tuer24Stock $stock): self
  413. {
  414. // unset the owning side of the relation if necessary
  415. if ($stock === null && $this->stock !== null) {
  416. $this->stock->setProduct(null);
  417. }
  418. // set the owning side of the relation if necessary
  419. if ($stock !== null && $stock->getProduct() !== $this) {
  420. $stock->setProduct($this);
  421. }
  422. $this->stock = $stock;
  423. return $this;
  424. }
  425. /**
  426. * Get translation by locale
  427. *
  428. * @param string $locale
  429. * @return Tuer24ProductTranslation|null
  430. */
  431. public function getTranslation(string $locale): ?Tuer24ProductTranslation
  432. {
  433. foreach ($this->translations as $translation) {
  434. if ($translation->getLocale() === $locale) {
  435. return $translation;
  436. }
  437. }
  438. return null;
  439. }
  440. /**
  441. * Get price for company
  442. *
  443. * @param int $companyId
  444. * @return Tuer24ProductPrice|null
  445. */
  446. public function getPriceForCompany(int $companyId): ?Tuer24ProductPrice
  447. {
  448. foreach ($this->prices as $price) {
  449. if ($price->getCompany()->getId() === $companyId) {
  450. return $price;
  451. }
  452. }
  453. return null;
  454. }
  455. /**
  456. * Get image path
  457. *
  458. * @return string|null
  459. */
  460. public function getImagePath(): ?string
  461. {
  462. return $this->imagePath;
  463. }
  464. /**
  465. * Set image path
  466. *
  467. * @param string|null $imagePath
  468. * @return $this
  469. */
  470. public function setImagePath(?string $imagePath): self
  471. {
  472. $this->imagePath = $imagePath;
  473. return $this;
  474. }
  475. }