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

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\Tuer24Bundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Validator\Constraints as Assert;
  5. use Gedmo\Mapping\Annotation as Gedmo;
  6. use Gedmo\Timestampable\Traits\TimestampableEntity;
  7. use Roothirsch\Tuer24Bundle\Repository\Tuer24StockRepository;
  8. use Roothirsch\PimBundle\Entity\Product;
  9. #[ORM\Entity(repositoryClass: "Roothirsch\Tuer24Bundle\Repository\Tuer24StockRepository")]
  10. #[ORM\Table(name: 'roothirsch_tuer24_stock')]
  11. #[ORM\HasLifecycleCallbacks]
  12. class Tuer24Stock
  13. {
  14. use TimestampableEntity;
  15. #[ORM\Id]
  16. #[ORM\GeneratedValue]
  17. #[ORM\Column(type: 'integer')]
  18. private $id;
  19. #[ORM\OneToOne(targetEntity: Product::class)]
  20. #[ORM\JoinColumn(name: "product_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE")]
  21. private $product;
  22. #[ORM\Column(type: 'integer')]
  23. #[Assert\NotBlank]
  24. #[Assert\PositiveOrZero]
  25. private $quantity = 0;
  26. #[ORM\Column(type: 'integer')]
  27. #[Assert\PositiveOrZero]
  28. private $reservedQuantity = 0;
  29. #[ORM\Column(type: 'integer', nullable: true)]
  30. #[Assert\PositiveOrZero]
  31. private $lowStockThreshold;
  32. #[ORM\Column(type: 'boolean')]
  33. private $trackInventory = true;
  34. #[ORM\Column(type: 'string', length: 255, nullable: true)]
  35. private $stockStatus;
  36. #[ORM\Column(type: 'text', nullable: true)]
  37. private $stockNotes;
  38. #[ORM\Column(type: 'boolean')]
  39. private $allowBackorders = false;
  40. /**
  41. * Get id
  42. *
  43. * @return int|null
  44. */
  45. public function getId(): ?int
  46. {
  47. return $this->id;
  48. }
  49. /**
  50. * Get product
  51. *
  52. * @return Product|null
  53. */
  54. public function getProduct(): ?Product
  55. {
  56. return $this->product;
  57. }
  58. /**
  59. * Set product
  60. *
  61. * @param Product|null $product
  62. * @return $this
  63. */
  64. public function setProduct(?Product $product): self
  65. {
  66. $this->product = $product;
  67. return $this;
  68. }
  69. /**
  70. * Get quantity
  71. *
  72. * @return int|null
  73. */
  74. public function getQuantity(): ?int
  75. {
  76. return $this->quantity;
  77. }
  78. /**
  79. * Set quantity
  80. *
  81. * @param int $quantity
  82. * @return $this
  83. */
  84. public function setQuantity(int $quantity): self
  85. {
  86. $this->quantity = $quantity;
  87. return $this;
  88. }
  89. /**
  90. * Get reserved quantity
  91. *
  92. * @return int|null
  93. */
  94. public function getReservedQuantity(): ?int
  95. {
  96. return $this->reservedQuantity;
  97. }
  98. /**
  99. * Set reserved quantity
  100. *
  101. * @param int $reservedQuantity
  102. * @return $this
  103. */
  104. public function setReservedQuantity(int $reservedQuantity): self
  105. {
  106. $this->reservedQuantity = $reservedQuantity;
  107. return $this;
  108. }
  109. /**
  110. * Get available quantity (total minus reserved)
  111. *
  112. * @return int
  113. */
  114. public function getAvailableQuantity(): int
  115. {
  116. return max(0, $this->quantity - $this->reservedQuantity);
  117. }
  118. /**
  119. * Get low stock threshold
  120. *
  121. * @return int|null
  122. */
  123. public function getLowStockThreshold(): ?int
  124. {
  125. return $this->lowStockThreshold;
  126. }
  127. /**
  128. * Set low stock threshold
  129. *
  130. * @param int|null $lowStockThreshold
  131. * @return $this
  132. */
  133. public function setLowStockThreshold(?int $lowStockThreshold): self
  134. {
  135. $this->lowStockThreshold = $lowStockThreshold;
  136. return $this;
  137. }
  138. /**
  139. * Check if stock is low
  140. *
  141. * @return bool
  142. */
  143. public function isLowStock(): bool
  144. {
  145. if ($this->lowStockThreshold === null) {
  146. return false;
  147. }
  148. return $this->getAvailableQuantity() <= $this->lowStockThreshold;
  149. }
  150. /**
  151. * Get track inventory
  152. *
  153. * @return bool|null
  154. */
  155. public function getTrackInventory(): ?bool
  156. {
  157. return $this->trackInventory;
  158. }
  159. /**
  160. * Set track inventory
  161. *
  162. * @param bool $trackInventory
  163. * @return $this
  164. */
  165. public function setTrackInventory(bool $trackInventory): self
  166. {
  167. $this->trackInventory = $trackInventory;
  168. return $this;
  169. }
  170. /**
  171. * Get stock status
  172. *
  173. * @return string|null
  174. */
  175. public function getStockStatus(): ?string
  176. {
  177. return $this->stockStatus;
  178. }
  179. /**
  180. * Set stock status
  181. *
  182. * @param string|null $stockStatus
  183. * @return $this
  184. */
  185. public function setStockStatus(?string $stockStatus): self
  186. {
  187. $this->stockStatus = $stockStatus;
  188. return $this;
  189. }
  190. /**
  191. * Get stock notes
  192. *
  193. * @return string|null
  194. */
  195. public function getStockNotes(): ?string
  196. {
  197. return $this->stockNotes;
  198. }
  199. /**
  200. * Set stock notes
  201. *
  202. * @param string|null $stockNotes
  203. * @return $this
  204. */
  205. public function setStockNotes(?string $stockNotes): self
  206. {
  207. $this->stockNotes = $stockNotes;
  208. return $this;
  209. }
  210. /**
  211. * Get allow backorders
  212. *
  213. * @return bool|null
  214. */
  215. public function getAllowBackorders(): ?bool
  216. {
  217. return $this->allowBackorders;
  218. }
  219. /**
  220. * Set allow backorders
  221. *
  222. * @param bool $allowBackorders
  223. * @return $this
  224. */
  225. public function setAllowBackorders(bool $allowBackorders): self
  226. {
  227. $this->allowBackorders = $allowBackorders;
  228. return $this;
  229. }
  230. /**
  231. * Check if product is in stock
  232. *
  233. * @return bool
  234. */
  235. public function isInStock(): bool
  236. {
  237. // If not tracking inventory, always return true
  238. if (!$this->trackInventory) {
  239. return true;
  240. }
  241. // If allowing backorders, always return true
  242. if ($this->allowBackorders) {
  243. return true;
  244. }
  245. // Otherwise, check if available quantity is greater than 0
  246. return $this->getAvailableQuantity() > 0;
  247. }
  248. /**
  249. * Reserve quantity for an order
  250. *
  251. * @param int $quantity
  252. * @return bool
  253. */
  254. public function reserveQuantity(int $quantity): bool
  255. {
  256. // If not tracking inventory, always allow
  257. if (!$this->trackInventory) {
  258. return true;
  259. }
  260. // If quantity is available, reserve it
  261. if ($this->getAvailableQuantity() >= $quantity || $this->allowBackorders) {
  262. $this->reservedQuantity += $quantity;
  263. return true;
  264. }
  265. // Not enough stock
  266. return false;
  267. }
  268. /**
  269. * Release reserved quantity
  270. *
  271. * @param int $quantity
  272. * @return $this
  273. */
  274. public function releaseReservedQuantity(int $quantity): self
  275. {
  276. $this->reservedQuantity = max(0, $this->reservedQuantity - $quantity);
  277. return $this;
  278. }
  279. /**
  280. * Adjust stock (increase or decrease)
  281. *
  282. * @param int $adjustment
  283. * @return $this
  284. */
  285. public function adjustStock(int $adjustment): self
  286. {
  287. $newQuantity = $this->quantity + $adjustment;
  288. $this->quantity = max(0, $newQuantity);
  289. return $this;
  290. }
  291. /**
  292. * String representation of stock
  293. *
  294. * @return string
  295. */
  296. public function __toString(): string
  297. {
  298. return (string)$this->getQuantity();
  299. }
  300. }