<?php
namespace AdminBundle\Entity;
use Doctrine\Common\Util\Debug;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
/**
* PaymentExtraCharge
*/
#[ORM\Table(name: 'payment_extra_charge')]
#[ORM\Entity(repositoryClass: \AdminBundle\Repository\PaymentExtraChargeRepository::class)]
#[ORM\HasLifecycleCallbacks]
class PaymentExtraCharge
{
const TYPE_CASH = 1;
const TYPE_CARD = 2;
const TYPE_BACS = 3;
const TYPE_CREDIT = 4;
const TYPE = [
self::TYPE_CASH => 'Cash',
self::TYPE_CARD => 'Card',
self::TYPE_BACS => 'Bacs',
self::TYPE_CREDIT => 'Credit',
];
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;
/**
* @var int
*/
#[ORM\Column(name: 'type', type: 'integer')]
private $type;
/**
* @var \DateTime
*/
#[ORM\Column(name: 'create_date', type: 'datetime')]
private $createDate;
/**
* @var string
*/
#[ORM\Column(name: 'reason', type: 'text')]
private $reason;
/**
* @var float
*/
#[ORM\Column(name: 'amount', type: 'float')]
private $amount;
/**
* @var Payment
*/
#[ORM\JoinColumn(name: 'payment_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
#[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\Payment::class, inversedBy: 'extraCharges')]
private $payment;
/**
* @var User
*/
#[ORM\JoinColumn(name: 'created_by_user_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
#[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\User::class)]
private $createdByUser;
/**
* @var string
*/
#[ORM\Column(name: 'paid', type: 'boolean', nullable: true, options: ['default' => 0])]
protected $paid = false;
/**
* PaymentExtraCharge constructor.
*/
public function __construct()
{
$this->createDate = new \DateTime();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set type
*
* @param integer $type
*
* @return PaymentExtraCharge
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return int
*/
public function getType()
{
return $this->type;
}
/**
* @return string
*/
public function getTypeAsString() {
return self::TYPE[$this->type];
}
/**
* Set createDate
*
* @param \DateTime $createDate
*
* @return PaymentExtraCharge
*/
public function setCreateDate($createDate)
{
$this->createDate = $createDate;
return $this;
}
/**
* Get createDate
*
* @return \DateTime
*/
public function getCreateDate()
{
return $this->createDate;
}
/**
* Set reason
*
* @param string $reason
*
* @return PaymentExtraCharge
*/
public function setReason($reason)
{
$this->reason = $reason;
return $this;
}
/**
* Get reason
*
* @return string
*/
public function getReason()
{
return $this->reason;
}
/**
* Set amount
*
* @param float $amount
*
* @return PaymentExtraCharge
*/
public function setAmount($amount)
{
$this->amount = $amount;
return $this;
}
/**
* Get amount
*
* @return float
*/
public function getAmount()
{
return $this->amount;
}
/**
* Set payment
*
* @param \AdminBundle\Entity\Payment $payment
*
* @return PaymentExtraCharge
*/
public function setPayment(\AdminBundle\Entity\Payment $payment = null)
{
$this->payment = $payment;
return $this;
}
/**
* Get payment
*
* @return \AdminBundle\Entity\Payment
*/
public function getPayment()
{
return $this->payment;
}
/**
* Set createdByUser
*
* @param \AdminBundle\Entity\User $createdByUser
*
* @return PaymentExtraCharge
*/
public function setCreatedByUser(\AdminBundle\Entity\User $createdByUser = null)
{
$this->createdByUser = $createdByUser;
return $this;
}
/**
* Get createdByUser
*
* @return \AdminBundle\Entity\User
*/
public function getCreatedByUser()
{
return $this->createdByUser;
}
/**
* @return string
*/
public function __toString() {
return $this->getId().'.'.(!empty($this->type) ? ucfirst(self::TYPE[$this->type]) : '').
' amount:'. $this->getAmount().' ('.$this->getReason().')';
}
public function getStringType() {
return (!empty($this->type) ? ucfirst(self::TYPE[$this->type]) : '');
}
/**
* @return PaymentExtraCharge
*/
#[ORM\PostPersist]
public function updateAmountLeft(LifecycleEventArgs $args) {
$payment = $this->getPayment();
if(!empty($payment)) {
$booking = $payment->getBooking();
if (!empty($booking->getDriverPrice())) {
$booking->setDriverPrice($booking->getDriverPrice() + $this->getAmount());
}
$booking->setQuotePrice($booking->getQuotePrice() + $this->getAmount());
$booking->setOverridePrice($booking->getOverridePrice() + $this->getAmount());
if ($this->getType() === self::TYPE_CASH) {
$payment->setAmountLeftCash($payment->getAmountLeftCash() + $this->getAmount());
}
$args->getEntityManager()->flush();
}
return $this;
}
/**
*
* @param LifecycleEventArgs $args
* @return PaymentExtraCharge
*/
#[ORM\PreRemove]
public function updateAmountLeftBeforeRemove(LifecycleEventArgs $args) {
if (!empty($this->getPayment())) {
$booking = $this->getPayment()->getBooking();
if (!empty($booking->getDriverPrice())) {
$booking->setDriverPrice($booking->getDriverPrice() - $this->getAmount());
}
$booking->setQuotePrice($booking->getQuotePrice() - $this->getAmount());
$booking->setOverridePrice($booking->getOverridePrice() - $this->getAmount());
if ($this->getType() === self::TYPE_CASH) {
$this->getPayment()->setAmountLeftCash($this->getPayment()->getAmountLeftCash() - $this->getAmount());
}
$args->getEntityManager()->getUnitOfWork()->recomputeSingleEntityChangeSet($args->getEntityManager()->getClassMetadata(get_class($booking)), $booking);
$args->getEntityManager()->getUnitOfWork()->recomputeSingleEntityChangeSet($args->getEntityManager()->getClassMetadata(get_class($this->getPayment())), $this->getPayment());
}
return $this;
}
/**
* @return string
*/
public function getPaid()
{
return $this->paid;
}
/**
* @param string $paid
*/
public function setPaid($paid)
{
$this->paid = $paid;
}
}