src/AdminBundle/Entity/PaymentExtraCharge.php line 233

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Entity;
  3. use Doctrine\Common\Util\Debug;
  4. use Doctrine\ORM\Event\LifecycleEventArgs;
  5. use Doctrine\ORM\Mapping as ORM;
  6. /**
  7. * PaymentExtraCharge
  8. */
  9. #[ORM\Table(name: 'payment_extra_charge')]
  10. #[ORM\Entity(repositoryClass: \AdminBundle\Repository\PaymentExtraChargeRepository::class)]
  11. #[ORM\HasLifecycleCallbacks]
  12. class PaymentExtraCharge
  13. {
  14. const TYPE_CASH = 1;
  15. const TYPE_CARD = 2;
  16. const TYPE_BACS = 3;
  17. const TYPE_CREDIT = 4;
  18. const TYPE = [
  19. self::TYPE_CASH => 'Cash',
  20. self::TYPE_CARD => 'Card',
  21. self::TYPE_BACS => 'Bacs',
  22. self::TYPE_CREDIT => 'Credit',
  23. ];
  24. /**
  25. * @var int
  26. */
  27. #[ORM\Column(name: 'id', type: 'integer')]
  28. #[ORM\Id]
  29. #[ORM\GeneratedValue(strategy: 'AUTO')]
  30. private $id;
  31. /**
  32. * @var int
  33. */
  34. #[ORM\Column(name: 'type', type: 'integer')]
  35. private $type;
  36. /**
  37. * @var \DateTime
  38. */
  39. #[ORM\Column(name: 'create_date', type: 'datetime')]
  40. private $createDate;
  41. /**
  42. * @var string
  43. */
  44. #[ORM\Column(name: 'reason', type: 'text')]
  45. private $reason;
  46. /**
  47. * @var float
  48. */
  49. #[ORM\Column(name: 'amount', type: 'float')]
  50. private $amount;
  51. /**
  52. * @var Payment
  53. */
  54. #[ORM\JoinColumn(name: 'payment_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
  55. #[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\Payment::class, inversedBy: 'extraCharges')]
  56. private $payment;
  57. /**
  58. * @var User
  59. */
  60. #[ORM\JoinColumn(name: 'created_by_user_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
  61. #[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\User::class)]
  62. private $createdByUser;
  63. /**
  64. * @var string
  65. */
  66. #[ORM\Column(name: 'paid', type: 'boolean', nullable: true, options: ['default' => 0])]
  67. protected $paid = false;
  68. /**
  69. * PaymentExtraCharge constructor.
  70. */
  71. public function __construct()
  72. {
  73. $this->createDate = new \DateTime();
  74. }
  75. /**
  76. * Get id
  77. *
  78. * @return int
  79. */
  80. public function getId()
  81. {
  82. return $this->id;
  83. }
  84. /**
  85. * Set type
  86. *
  87. * @param integer $type
  88. *
  89. * @return PaymentExtraCharge
  90. */
  91. public function setType($type)
  92. {
  93. $this->type = $type;
  94. return $this;
  95. }
  96. /**
  97. * Get type
  98. *
  99. * @return int
  100. */
  101. public function getType()
  102. {
  103. return $this->type;
  104. }
  105. /**
  106. * @return string
  107. */
  108. public function getTypeAsString() {
  109. return self::TYPE[$this->type];
  110. }
  111. /**
  112. * Set createDate
  113. *
  114. * @param \DateTime $createDate
  115. *
  116. * @return PaymentExtraCharge
  117. */
  118. public function setCreateDate($createDate)
  119. {
  120. $this->createDate = $createDate;
  121. return $this;
  122. }
  123. /**
  124. * Get createDate
  125. *
  126. * @return \DateTime
  127. */
  128. public function getCreateDate()
  129. {
  130. return $this->createDate;
  131. }
  132. /**
  133. * Set reason
  134. *
  135. * @param string $reason
  136. *
  137. * @return PaymentExtraCharge
  138. */
  139. public function setReason($reason)
  140. {
  141. $this->reason = $reason;
  142. return $this;
  143. }
  144. /**
  145. * Get reason
  146. *
  147. * @return string
  148. */
  149. public function getReason()
  150. {
  151. return $this->reason;
  152. }
  153. /**
  154. * Set amount
  155. *
  156. * @param float $amount
  157. *
  158. * @return PaymentExtraCharge
  159. */
  160. public function setAmount($amount)
  161. {
  162. $this->amount = $amount;
  163. return $this;
  164. }
  165. /**
  166. * Get amount
  167. *
  168. * @return float
  169. */
  170. public function getAmount()
  171. {
  172. return $this->amount;
  173. }
  174. /**
  175. * Set payment
  176. *
  177. * @param \AdminBundle\Entity\Payment $payment
  178. *
  179. * @return PaymentExtraCharge
  180. */
  181. public function setPayment(\AdminBundle\Entity\Payment $payment = null)
  182. {
  183. $this->payment = $payment;
  184. return $this;
  185. }
  186. /**
  187. * Get payment
  188. *
  189. * @return \AdminBundle\Entity\Payment
  190. */
  191. public function getPayment()
  192. {
  193. return $this->payment;
  194. }
  195. /**
  196. * Set createdByUser
  197. *
  198. * @param \AdminBundle\Entity\User $createdByUser
  199. *
  200. * @return PaymentExtraCharge
  201. */
  202. public function setCreatedByUser(\AdminBundle\Entity\User $createdByUser = null)
  203. {
  204. $this->createdByUser = $createdByUser;
  205. return $this;
  206. }
  207. /**
  208. * Get createdByUser
  209. *
  210. * @return \AdminBundle\Entity\User
  211. */
  212. public function getCreatedByUser()
  213. {
  214. return $this->createdByUser;
  215. }
  216. /**
  217. * @return string
  218. */
  219. public function __toString() {
  220. return $this->getId().'.'.(!empty($this->type) ? ucfirst(self::TYPE[$this->type]) : '').
  221. ' amount:'. $this->getAmount().' ('.$this->getReason().')';
  222. }
  223. public function getStringType() {
  224. return (!empty($this->type) ? ucfirst(self::TYPE[$this->type]) : '');
  225. }
  226. /**
  227. * @return PaymentExtraCharge
  228. */
  229. #[ORM\PostPersist]
  230. public function updateAmountLeft(LifecycleEventArgs $args) {
  231. $payment = $this->getPayment();
  232. if(!empty($payment)) {
  233. $booking = $payment->getBooking();
  234. if (!empty($booking->getDriverPrice())) {
  235. $booking->setDriverPrice($booking->getDriverPrice() + $this->getAmount());
  236. }
  237. $booking->setQuotePrice($booking->getQuotePrice() + $this->getAmount());
  238. $booking->setOverridePrice($booking->getOverridePrice() + $this->getAmount());
  239. if ($this->getType() === self::TYPE_CASH) {
  240. $payment->setAmountLeftCash($payment->getAmountLeftCash() + $this->getAmount());
  241. }
  242. $args->getEntityManager()->flush();
  243. }
  244. return $this;
  245. }
  246. /**
  247. *
  248. * @param LifecycleEventArgs $args
  249. * @return PaymentExtraCharge
  250. */
  251. #[ORM\PreRemove]
  252. public function updateAmountLeftBeforeRemove(LifecycleEventArgs $args) {
  253. if (!empty($this->getPayment())) {
  254. $booking = $this->getPayment()->getBooking();
  255. if (!empty($booking->getDriverPrice())) {
  256. $booking->setDriverPrice($booking->getDriverPrice() - $this->getAmount());
  257. }
  258. $booking->setQuotePrice($booking->getQuotePrice() - $this->getAmount());
  259. $booking->setOverridePrice($booking->getOverridePrice() - $this->getAmount());
  260. if ($this->getType() === self::TYPE_CASH) {
  261. $this->getPayment()->setAmountLeftCash($this->getPayment()->getAmountLeftCash() - $this->getAmount());
  262. }
  263. $args->getEntityManager()->getUnitOfWork()->recomputeSingleEntityChangeSet($args->getEntityManager()->getClassMetadata(get_class($booking)), $booking);
  264. $args->getEntityManager()->getUnitOfWork()->recomputeSingleEntityChangeSet($args->getEntityManager()->getClassMetadata(get_class($this->getPayment())), $this->getPayment());
  265. }
  266. return $this;
  267. }
  268. /**
  269. * @return string
  270. */
  271. public function getPaid()
  272. {
  273. return $this->paid;
  274. }
  275. /**
  276. * @param string $paid
  277. */
  278. public function setPaid($paid)
  279. {
  280. $this->paid = $paid;
  281. }
  282. }