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

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