src/AdminBundle/Entity/Payment.php line 15

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\Mapping as ORM;
  5. /**
  6. * Payment
  7. */
  8. #[ORM\Table(name: 'payment')]
  9. #[ORM\Index(name: 'amount_left_index', columns: ['amount_left'])]
  10. #[ORM\Entity(repositoryClass: \AdminBundle\Repository\PaymentRepository::class)]
  11. #[ORM\HasLifecycleCallbacks]
  12. class Payment
  13. {
  14. const TYPE_CASH = 1;
  15. const TYPE_CARD = 2;
  16. const TYPE_DEPOSIT = 3;
  17. const TYPE_BACS = 4;
  18. const TYPE_CREDIT = 5;
  19. const TYPE_ACCOUNT = 6;
  20. const TYPE = [
  21. self::TYPE_CASH => 'Cash',
  22. self::TYPE_CARD => 'Card',
  23. self::TYPE_DEPOSIT => 'Deposit + Cash',
  24. self::TYPE_BACS => 'Bacs',
  25. self::TYPE_CREDIT => 'Credit',
  26. self::TYPE_ACCOUNT => 'Account'
  27. ];
  28. /**
  29. * @var int
  30. */
  31. #[ORM\Column(name: 'id', type: 'integer')]
  32. #[ORM\Id]
  33. #[ORM\GeneratedValue(strategy: 'AUTO')]
  34. private $id;
  35. /**
  36. * @var integer
  37. */
  38. #[ORM\Column(name: 'type', type: 'smallint', length: 20)]
  39. private $type;
  40. /**
  41. * @var \DateTime
  42. */
  43. #[ORM\Column(name: 'create_date', type: 'datetime')]
  44. private $createDate;
  45. /**
  46. * @var float
  47. */
  48. #[ORM\Column(name: 'amount', type: 'float')]
  49. private $amount;
  50. /**
  51. * @var float
  52. */
  53. #[ORM\Column(name: 'amount_left', type: 'float')]
  54. private $amountLeft;
  55. /**
  56. * @var float
  57. */
  58. #[ORM\Column(name: 'amount_left_cash', type: 'float')]
  59. private $amountLeftCash;
  60. /**
  61. * @var string
  62. */
  63. #[ORM\Column(name: 'notes', type: 'text', nullable: true)]
  64. private $notes;
  65. /**
  66. * @var string
  67. */
  68. #[ORM\Column(name: 'external_id', type: 'string', length: 255, unique: true)]
  69. private $externalId;
  70. /**
  71. * @var ArrayCollection
  72. */
  73. #[ORM\OneToMany(targetEntity: \Transaction::class, mappedBy: 'payment', cascade: ['persist'], orphanRemoval: true)]
  74. private $transactions;
  75. /**
  76. * @var Booking
  77. */
  78. #[ORM\OneToOne(targetEntity: \AdminBundle\Entity\Booking::class, mappedBy: 'payment')]
  79. private $booking;
  80. /**
  81. * @var ArrayCollection
  82. */
  83. #[ORM\OneToMany(targetEntity: \AdminBundle\Entity\PaymentExtraCharge::class, mappedBy: 'payment', cascade: ['persist'], orphanRemoval: true)]
  84. private $extraCharges;
  85. /**
  86. * Payment constructor.
  87. */
  88. public function __construct()
  89. {
  90. $this->amountLeftCash = 0;
  91. $this->createDate = new \DateTime();
  92. $this->transactions = new ArrayCollection();
  93. $this->extraCharges = new ArrayCollection();
  94. $this->generateExternalId();
  95. }
  96. /**
  97. * @return ArrayCollection
  98. */
  99. public function getTransactions()
  100. {
  101. return $this->transactions;
  102. }
  103. /**
  104. * @param ArrayCollection $transactions
  105. * @return Payment
  106. */
  107. public function setTransactions($transactions)
  108. {
  109. $this->transactions = $transactions;
  110. return $this;
  111. }
  112. /**
  113. * Get id
  114. *
  115. * @return int
  116. */
  117. public function getId()
  118. {
  119. return $this->id;
  120. }
  121. /**
  122. * Set type
  123. *
  124. * @param integer $type
  125. *
  126. * @return Payment
  127. */
  128. public function setType($type)
  129. {
  130. $this->type = $type;
  131. return $this;
  132. }
  133. /**
  134. * Get type
  135. *
  136. * @return integer
  137. */
  138. public function getType()
  139. {
  140. return $this->type;
  141. }
  142. /**
  143. * @return string
  144. */
  145. public function getTypeAsString() {
  146. return self::TYPE[$this->type];
  147. }
  148. /**
  149. * Set createDate
  150. *
  151. * @param \DateTime $createDate
  152. *
  153. * @return Payment
  154. */
  155. public function setCreateDate($createDate)
  156. {
  157. $this->createDate = $createDate;
  158. return $this;
  159. }
  160. /**
  161. * Get createDate
  162. *
  163. * @return \DateTime
  164. */
  165. public function getCreateDate()
  166. {
  167. return $this->createDate;
  168. }
  169. /**
  170. * Set amount
  171. *
  172. * @param float $amount
  173. *
  174. * @return Payment
  175. */
  176. public function setAmount($amount)
  177. {
  178. $this->amount = $amount;
  179. return $this;
  180. }
  181. /**
  182. * Get amount
  183. *
  184. * @return float
  185. */
  186. public function getAmount()
  187. {
  188. return $this->amount;
  189. }
  190. /**
  191. * Set amountLeft
  192. *
  193. * @param float $amountLeft
  194. *
  195. * @return Payment
  196. */
  197. public function setAmountLeft($amountLeft)
  198. {
  199. $this->amountLeft = $amountLeft;
  200. return $this;
  201. }
  202. /**
  203. * Get amountLeft
  204. *
  205. * @return float
  206. */
  207. public function getAmountLeft()
  208. {
  209. return $this->amountLeft;
  210. }
  211. /**
  212. * Set notes
  213. *
  214. * @param string $notes
  215. *
  216. * @return Payment
  217. */
  218. public function setNotes($notes)
  219. {
  220. $this->notes = $notes;
  221. return $this;
  222. }
  223. /**
  224. * Get notes
  225. *
  226. * @return string
  227. */
  228. public function getNotes()
  229. {
  230. return $this->notes;
  231. }
  232. /**
  233. * @return string
  234. */
  235. public function getExternalId()
  236. {
  237. return $this->externalId;
  238. }
  239. /**
  240. * @param string $externalId
  241. * @return Payment
  242. */
  243. public function setExternalId($externalId)
  244. {
  245. $this->externalId = $externalId;
  246. return $this;
  247. }
  248. /**
  249. * @return $this
  250. */
  251. public function generateExternalId() {
  252. $this->externalId = sha1(md5($this->createDate->getTimestamp().mt_rand()));
  253. return $this;
  254. }
  255. /**
  256. * @return string
  257. */
  258. public function __toString() {
  259. return $this->id.'.'.
  260. (!empty($this->type) ? ucfirst(self::TYPE[$this->type]) : '').
  261. ' '. $this->amount.'(left ot pay: '. $this->amountLeft . ')';
  262. }
  263. /**
  264. * Add transaction
  265. *
  266. * @param \AdminBundle\Entity\Transaction $transaction
  267. *
  268. * @return Payment
  269. */
  270. public function addTransaction(\AdminBundle\Entity\Transaction $transaction)
  271. {
  272. $this->transactions[] = $transaction;
  273. return $this;
  274. }
  275. /**
  276. * Remove transaction
  277. *
  278. * @param \AdminBundle\Entity\Transaction $transaction
  279. */
  280. public function removeTransaction(\AdminBundle\Entity\Transaction $transaction)
  281. {
  282. $this->transactions->removeElement($transaction);
  283. }
  284. /**
  285. *
  286. * @return Payment
  287. */
  288. #[ORM\PrePersist]
  289. #[ORM\PreUpdate]
  290. public function updateAmountLeft() {
  291. $amountLeft = $this->amount;
  292. /** @var Transaction $transaction */
  293. foreach ($this->getTransactions() as $transaction) {
  294. $amountLeft -= $transaction->getAmount();
  295. }
  296. if ($this->type === Payment::TYPE_ACCOUNT) {
  297. return $this->setAmountLeft(0);
  298. }
  299. return $this->setAmountLeft($amountLeft);
  300. }
  301. /**
  302. * @return Booking
  303. */
  304. public function getBooking()
  305. {
  306. return $this->booking;
  307. }
  308. /**
  309. * @param Booking $booking
  310. * @return Payment
  311. */
  312. public function setBooking($booking)
  313. {
  314. $this->booking = $booking;
  315. return $this;
  316. }
  317. /**
  318. * Add extraCharge
  319. *
  320. * @param \AdminBundle\Entity\PaymentExtraCharge $extraCharge
  321. *
  322. * @return Payment
  323. */
  324. public function addExtraCharge(PaymentExtraCharge $extraCharge)
  325. {
  326. $this->extraCharges[] = $extraCharge;
  327. return $this;
  328. }
  329. /**
  330. * Remove extraCharge
  331. *
  332. * @param \AdminBundle\Entity\PaymentExtraCharge $extraCharge
  333. */
  334. public function removeExtraCharge(PaymentExtraCharge $extraCharge)
  335. {
  336. $this->extraCharges->removeElement($extraCharge);
  337. }
  338. /**
  339. * Get extraCharges
  340. *
  341. * @return \Doctrine\Common\Collections\Collection
  342. */
  343. public function getExtraCharges()
  344. {
  345. return $this->extraCharges;
  346. }
  347. /**
  348. * Set amountLeftCash
  349. *
  350. * @param float $amountLeftCash
  351. *
  352. * @return Payment
  353. */
  354. public function setAmountLeftCash($amountLeftCash)
  355. {
  356. $this->amountLeftCash = $amountLeftCash;
  357. return $this;
  358. }
  359. /**
  360. * Get amountLeftCash
  361. *
  362. * @return float
  363. */
  364. public function getAmountLeftCash()
  365. {
  366. return $this->amountLeftCash;
  367. }
  368. }