StockEntry.php (5457B)
1 <?php 2 3 namespace PartKeepr\StockBundle\Entity; 4 5 use Doctrine\ORM\Mapping as ORM; 6 use PartKeepr\AuthBundle\Entity\User; 7 use PartKeepr\CoreBundle\Entity\BaseEntity; 8 use PartKeepr\DoctrineReflectionBundle\Annotation\TargetService; 9 use PartKeepr\PartBundle\Entity\Part; 10 use Symfony\Component\Serializer\Annotation\Groups; 11 12 /** 13 * @ORM\Entity 14 * @ORM\HasLifecycleCallbacks 15 * @TargetService(uri="/api/stock_entries") 16 */ 17 class StockEntry extends BaseEntity 18 { 19 /** 20 * @ORM\Column(type="integer") 21 * @Groups({"default"}) 22 */ 23 private $stockLevel; 24 25 /** 26 * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\Part", inversedBy="stockLevels") 27 * @Groups({"default"}) 28 */ 29 private $part; 30 31 /** 32 * @ORM\ManyToOne(targetEntity="PartKeepr\AuthBundle\Entity\User") 33 * @Groups({"default"}) 34 */ 35 private $user; 36 37 /** 38 * The price per item. 39 * 40 * @ORM\Column(type="decimal",precision=13,scale=4,nullable=true) 41 * @Groups({"default"}) 42 * 43 * @var float 44 */ 45 private $price; 46 47 /** 48 * @ORM\Column(type="datetime") 49 * @Groups({"default"}) 50 * 51 * @var \DateTime 52 */ 53 private $dateTime; 54 55 /** 56 * Indicates if the stock level is a correction entry. 57 * 58 * @ORM\Column(type="boolean") 59 * @Groups({"default"}) 60 * 61 * @var bool 62 */ 63 private $correction; 64 65 /** 66 * @ORM\Column(type="string",nullable=true) 67 * @Groups({"default"}) 68 * 69 * @var string 70 */ 71 private $comment; 72 73 /** 74 * Creates a new stock entry. A stock entry tracks how many parts 75 * were the stockLevel is the amount of items added/removed, 76 * by which user and how much the user paid for it (for adding parts only!). 77 */ 78 public function __construct() 79 { 80 $this->setDateTime(new \DateTime()); 81 $this->setCorrection(false); 82 } 83 84 /** 85 * Sets the date+time. 86 * 87 * @param \DateTime $dateTime The date+time 88 */ 89 private function setDateTime(\DateTime $dateTime) 90 { 91 $this->dateTime = $dateTime; 92 } 93 94 /** 95 * Returns the date+time when the record was created. 96 * 97 * @return \DateTime The date+time when the record was created 98 */ 99 public function getDateTime() 100 { 101 return $this->dateTime; 102 } 103 104 /** 105 * Sets if the stock entry is a correction record. 106 * 107 * @param $bCorrection boolean True if the record is a correction record, false otherwise 108 */ 109 public function setCorrection($bCorrection) 110 { 111 $this->correction = $bCorrection; 112 } 113 114 /** 115 * Returns if the entry is a correction entry. 116 * 117 * @return bool True if the entry is a correction entry, false otherwise 118 */ 119 public function getCorrection() 120 { 121 return $this->correction; 122 } 123 124 /** 125 * Sets the price for the item stored. 126 * 127 * Please note that the price is for a single item only, and can be null. 128 * 129 * @param float $price The price to set 130 */ 131 public function setPrice($price) 132 { 133 $this->price = $price; 134 } 135 136 /** 137 * Returns the price for this entry. The price is for a single item only. 138 * 139 * @return float The price for this entry. 140 */ 141 public function getPrice() 142 { 143 return $this->price; 144 } 145 146 /** 147 * Sets the stock level for this entry. 148 * 149 * Negative values means part removal, positive values means part adding. 150 * 151 * @param int $stockLevel The stock level 152 */ 153 public function setStockLevel($stockLevel) 154 { 155 $this->stockLevel = $stockLevel; 156 } 157 158 /** 159 * Returns the stock level for this entry. 160 * 161 * @return int The stock level 162 */ 163 public function getStockLevel() 164 { 165 return $this->stockLevel; 166 } 167 168 /** 169 * Sets the part assigned to this entry. 170 * 171 * @param Part $part The part to set 172 */ 173 public function setPart(Part $part = null) 174 { 175 $this->part = $part; 176 } 177 178 /** 179 * Returns the part assigned to this entry. 180 * 181 * @return Part $part The part 182 */ 183 public function getPart() 184 { 185 return $this->part; 186 } 187 188 /** 189 * Sets the user assigned to this entry. 190 * 191 * @param User $user The user The user to set 192 */ 193 public function setUser(User $user = null) 194 { 195 $this->user = $user; 196 } 197 198 /** 199 * Returns the user for this entry. 200 * 201 * @return User the user 202 */ 203 public function getUser() 204 { 205 return $this->user; 206 } 207 208 /** 209 * If the stock level is negative, we can't have a price. 210 * 211 * @ORM\PrePersist 212 */ 213 public function checkPrice() 214 { 215 if ($this->getStockLevel() < 0 && $this->getPrice() !== null) { 216 $this->setPrice(null); 217 } 218 } 219 220 /** 221 * Returns if the current stock entry is a removal. 222 * 223 * @return bool True if the entry is a removal, false otherwise 224 */ 225 public function isRemoval() 226 { 227 if ($this->getStockLevel() < 0) { 228 return true; 229 } else { 230 return false; 231 } 232 } 233 234 /** 235 * Sets a comment. 236 * 237 * @param string $comment 238 */ 239 public function setComment($comment) 240 { 241 $this->comment = $comment; 242 } 243 244 /** 245 * Returns the comment. 246 * 247 * @return string The comment 248 */ 249 public function getComment() 250 { 251 return $this->comment; 252 } 253 }