src/AdminBundle/Entity/Transaction.php line 266

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Entity;
  3. use Doctrine\ORM\Event\LifecycleEventArgs;
  4. use Doctrine\ORM\Mapping as ORM;
  5. /**
  6. * Invoice
  7. */
  8. #[ORM\Table(name: 'transaction')]
  9. #[ORM\UniqueConstraint(name: 'payment_braintree_transaction', columns: ['braintree_transaction_id', 'payment_id'])]
  10. #[ORM\Entity(repositoryClass: \AdminBundle\Repository\TransactionRepository::class)]
  11. #[ORM\HasLifecycleCallbacks]
  12. class Transaction
  13. {
  14. const TYPE_BRAINTREE = 1;
  15. const TYPE_SIMPLE_CASH = 2;
  16. const TYPE_SIMPLE_BACS = 3;
  17. const TYPE_SIMPLE_CREDIT = 4;
  18. const TYPE_STRIPE = 5;
  19. const TYPE_REFUND = 10;
  20. const TYPE = [
  21. self::TYPE_BRAINTREE => 'braintree',
  22. self::TYPE_SIMPLE_CASH => 'simple cash',
  23. self::TYPE_SIMPLE_BACS => 'simple bacs',
  24. self::TYPE_SIMPLE_CREDIT => 'simple credit',
  25. self::TYPE_REFUND => 'refund',
  26. self::TYPE_STRIPE => 'stripe'
  27. ];
  28. const CUSTOMER_TYPE = [
  29. self::TYPE_BRAINTREE => 'braintree',
  30. self::TYPE_STRIPE => 'stripe'
  31. ];
  32. /**
  33. * @var int
  34. */
  35. #[ORM\Column(name: 'id', type: 'integer')]
  36. #[ORM\Id]
  37. #[ORM\GeneratedValue(strategy: 'AUTO')]
  38. private $id;
  39. /**
  40. * @var \DateTime
  41. */
  42. #[ORM\Column(name: 'create_date', type: 'datetime')]
  43. private $createDate;
  44. /**
  45. * @var integer
  46. */
  47. #[ORM\Column(name: 'type', type: 'smallint', length: 20)]
  48. private $type;
  49. /**
  50. * @var string
  51. */
  52. #[ORM\Column(name: 'braintree_transaction_id', type: 'string', length: 255, nullable: true)]
  53. private $braintreeTransactionId;
  54. /**
  55. * @var string
  56. */
  57. #[ORM\Column(name: 'stripe_transaction_id', type: 'string', length: 255, nullable: true)]
  58. private $stripeTransactionId;
  59. /**
  60. * @var float
  61. */
  62. #[ORM\Column(name: 'amount', type: 'float')]
  63. private $amount;
  64. /**
  65. * @var string
  66. */
  67. #[ORM\Column(name: 'notes', type: 'text', nullable: true)]
  68. private $notes;
  69. /**
  70. * @var Payment
  71. */
  72. #[ORM\JoinColumn(name: 'payment_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
  73. #[ORM\ManyToOne(targetEntity: \Payment::class, inversedBy: 'transactions')]
  74. private $payment;
  75. /**
  76. * @var User
  77. */
  78. #[ORM\JoinColumn(name: 'created_by_user_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
  79. #[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\User::class)]
  80. private $createdByUser;
  81. /**
  82. * Invoice constructor.
  83. */
  84. public function __construct()
  85. {
  86. $this->createDate = new \DateTime();
  87. }
  88. /**
  89. * Get id
  90. *
  91. * @return int
  92. */
  93. public function getId()
  94. {
  95. return $this->id;
  96. }
  97. /**
  98. * Set createDate
  99. *
  100. * @param \DateTime $createDate
  101. *
  102. * @return Transaction
  103. */
  104. public function setCreateDate($createDate)
  105. {
  106. $this->createDate = $createDate;
  107. return $this;
  108. }
  109. /**
  110. * Get createDate
  111. *
  112. * @return \DateTime
  113. */
  114. public function getCreateDate()
  115. {
  116. return $this->createDate;
  117. }
  118. /**
  119. * Set type
  120. *
  121. * @param integer $type
  122. *
  123. * @return Transaction
  124. */
  125. public function setType($type)
  126. {
  127. $this->type = $type;
  128. return $this;
  129. }
  130. /**
  131. * Get type
  132. *
  133. * @return integer
  134. */
  135. public function getType()
  136. {
  137. return $this->type;
  138. }
  139. /**
  140. * Set braintreeTransactionId
  141. *
  142. * @param string $braintreeTransactionId
  143. *
  144. * @return Transaction
  145. */
  146. public function setBraintreeTransactionId($braintreeTransactionId)
  147. {
  148. $this->braintreeTransactionId = $braintreeTransactionId;
  149. return $this;
  150. }
  151. /**
  152. * Get braintreeTransactionId
  153. *
  154. * @return string
  155. */
  156. public function getBraintreeTransactionId()
  157. {
  158. return $this->braintreeTransactionId;
  159. }
  160. /**
  161. * Set stripeTransactionId
  162. *
  163. * @param string $stripeTransactionId
  164. *
  165. * @return Transaction
  166. */
  167. public function setStripeTransactionId($stripeTransactionId)
  168. {
  169. $this->stripeTransactionId = $stripeTransactionId;
  170. return $this;
  171. }
  172. /**
  173. * Get stripeTransactionId
  174. *
  175. * @return string
  176. */
  177. public function getStripeTransactionId()
  178. {
  179. return $this->stripeTransactionId;
  180. }
  181. /**
  182. * Set amount
  183. *
  184. * @param float $amount
  185. *
  186. * @return Transaction
  187. */
  188. public function setAmount($amount)
  189. {
  190. $this->amount = $amount;
  191. return $this;
  192. }
  193. /**
  194. * Get amount
  195. *
  196. * @return float
  197. */
  198. public function getAmount()
  199. {
  200. return $this->amount;
  201. }
  202. /**
  203. * Set notes
  204. *
  205. * @param string $notes
  206. *
  207. * @return Transaction
  208. */
  209. public function setNotes($notes)
  210. {
  211. $this->notes = $notes;
  212. return $this;
  213. }
  214. /**
  215. * Get notes
  216. *
  217. * @return string
  218. */
  219. public function getNotes()
  220. {
  221. return $this->notes;
  222. }
  223. /**
  224. * Set payment
  225. *
  226. * @param \AdminBundle\Entity\Payment $payment
  227. *
  228. * @return Transaction
  229. */
  230. public function setPayment(\AdminBundle\Entity\Payment $payment = null)
  231. {
  232. $this->payment = $payment;
  233. return $this;
  234. }
  235. /**
  236. * Get payment
  237. *
  238. * @return \AdminBundle\Entity\Payment
  239. */
  240. public function getPayment()
  241. {
  242. return $this->payment;
  243. }
  244. /**
  245. * @return User|null
  246. */
  247. public function getCreatedByUser()
  248. {
  249. return $this->createdByUser;
  250. }
  251. /**
  252. * @param User|null $createdByUser
  253. * @return Transaction
  254. */
  255. public function setCreatedByUser($createdByUser): Transaction
  256. {
  257. $this->createdByUser = $createdByUser;
  258. return $this;
  259. }
  260. public function __toString() {
  261. return $this->getId().'.'.(!empty($this->type) ? ucfirst(self::TYPE[$this->type]) : '').
  262. ($this->type === 1 ? '('.$this->braintreeTransactionId.')' : ''). ' amount:'.
  263. $this->getAmount() . (!empty($this->notes) ? ' ('.$this->notes.')' : '');
  264. }
  265. public function stringType() {
  266. return (!empty($this->type) ? ucfirst(self::TYPE[$this->type]) : '');
  267. }
  268. /**
  269. *
  270. * @return Transaction
  271. */
  272. #[ORM\PrePersist]
  273. #[ORM\PreUpdate]
  274. public function updateAmountLeft(LifecycleEventArgs $args) {
  275. if(!empty($this->getPayment())) {
  276. $this->getPayment()->updateAmountLeft();
  277. if ($this->getPayment()->getAmountLeftCash() > 0 && $this->type === self::TYPE_SIMPLE_CASH) {
  278. $this->getPayment()->setAmountLeftCash($this->getPayment()->getAmountLeftCash() - $this->getAmount());
  279. }
  280. }
  281. return $this;
  282. }
  283. public function getCreateDateFormated()
  284. {
  285. return $this->createDate->format('d/m/Y H:i:s');
  286. }
  287. }