<?php
namespace AdminBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Payment
*/
#[ORM\Table(name: 'payment')]
#[ORM\Index(name: 'amount_left_index', columns: ['amount_left'])]
#[ORM\Entity(repositoryClass: \AdminBundle\Repository\PaymentRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Payment
{
const TYPE_CASH = 1;
const TYPE_CARD = 2;
const TYPE_DEPOSIT = 3;
const TYPE_BACS = 4;
const TYPE_CREDIT = 5;
const TYPE_ACCOUNT = 6;
const TYPE = [
self::TYPE_CASH => 'Cash',
self::TYPE_CARD => 'Card',
self::TYPE_DEPOSIT => 'Deposit + Cash',
self::TYPE_BACS => 'Bacs',
self::TYPE_CREDIT => 'Credit',
self::TYPE_ACCOUNT => 'Account'
];
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;
/**
* @var integer
*/
#[ORM\Column(name: 'type', type: 'smallint', length: 20)]
private $type;
/**
* @var \DateTime
*/
#[ORM\Column(name: 'create_date', type: 'datetime')]
private $createDate;
/**
* @var float
*/
#[ORM\Column(name: 'amount', type: 'float')]
private $amount;
/**
* @var float
*/
#[ORM\Column(name: 'amount_left', type: 'float')]
private $amountLeft;
/**
* @var float
*/
#[ORM\Column(name: 'amount_left_cash', type: 'float')]
private $amountLeftCash;
/**
* @var string
*/
#[ORM\Column(name: 'notes', type: 'text', nullable: true)]
private $notes;
/**
* @var string
*/
#[ORM\Column(name: 'external_id', type: 'string', length: 255, unique: true)]
private $externalId;
/**
* @var ArrayCollection
*/
#[ORM\OneToMany(targetEntity: \Transaction::class, mappedBy: 'payment', cascade: ['persist'], orphanRemoval: true)]
private $transactions;
/**
* @var Booking
*/
#[ORM\OneToOne(targetEntity: \AdminBundle\Entity\Booking::class, mappedBy: 'payment')]
private $booking;
/**
* @var ArrayCollection
*/
#[ORM\OneToMany(targetEntity: \AdminBundle\Entity\PaymentExtraCharge::class, mappedBy: 'payment', cascade: ['persist'], orphanRemoval: true)]
private $extraCharges;
/**
* Payment constructor.
*/
public function __construct()
{
$this->amountLeftCash = 0;
$this->createDate = new \DateTime();
$this->transactions = new ArrayCollection();
$this->extraCharges = new ArrayCollection();
$this->generateExternalId();
}
/**
* @return ArrayCollection
*/
public function getTransactions()
{
return $this->transactions;
}
/**
* @param ArrayCollection $transactions
* @return Payment
*/
public function setTransactions($transactions)
{
$this->transactions = $transactions;
return $this;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set type
*
* @param integer $type
*
* @return Payment
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return integer
*/
public function getType()
{
return $this->type;
}
/**
* @return string
*/
public function getTypeAsString() {
return self::TYPE[$this->type];
}
/**
* Set createDate
*
* @param \DateTime $createDate
*
* @return Payment
*/
public function setCreateDate($createDate)
{
$this->createDate = $createDate;
return $this;
}
/**
* Get createDate
*
* @return \DateTime
*/
public function getCreateDate()
{
return $this->createDate;
}
/**
* Set amount
*
* @param float $amount
*
* @return Payment
*/
public function setAmount($amount)
{
$this->amount = $amount;
return $this;
}
/**
* Get amount
*
* @return float
*/
public function getAmount()
{
return $this->amount;
}
/**
* Set amountLeft
*
* @param float $amountLeft
*
* @return Payment
*/
public function setAmountLeft($amountLeft)
{
$this->amountLeft = $amountLeft;
return $this;
}
/**
* Get amountLeft
*
* @return float
*/
public function getAmountLeft()
{
return $this->amountLeft;
}
/**
* Set notes
*
* @param string $notes
*
* @return Payment
*/
public function setNotes($notes)
{
$this->notes = $notes;
return $this;
}
/**
* Get notes
*
* @return string
*/
public function getNotes()
{
return $this->notes;
}
/**
* @return string
*/
public function getExternalId()
{
return $this->externalId;
}
/**
* @param string $externalId
* @return Payment
*/
public function setExternalId($externalId)
{
$this->externalId = $externalId;
return $this;
}
/**
* @return $this
*/
public function generateExternalId() {
$this->externalId = sha1(md5($this->createDate->getTimestamp().mt_rand()));
return $this;
}
/**
* @return string
*/
public function __toString() {
return $this->id.'.'.
(!empty($this->type) ? ucfirst(self::TYPE[$this->type]) : '').
' '. $this->amount.'(left ot pay: '. $this->amountLeft . ')';
}
/**
* Add transaction
*
* @param \AdminBundle\Entity\Transaction $transaction
*
* @return Payment
*/
public function addTransaction(\AdminBundle\Entity\Transaction $transaction)
{
$this->transactions[] = $transaction;
return $this;
}
/**
* Remove transaction
*
* @param \AdminBundle\Entity\Transaction $transaction
*/
public function removeTransaction(\AdminBundle\Entity\Transaction $transaction)
{
$this->transactions->removeElement($transaction);
}
/**
*
* @return Payment
*/
#[ORM\PrePersist]
#[ORM\PreUpdate]
public function updateAmountLeft() {
$amountLeft = $this->amount;
/** @var Transaction $transaction */
foreach ($this->getTransactions() as $transaction) {
$amountLeft -= $transaction->getAmount();
}
if ($this->type === Payment::TYPE_ACCOUNT) {
return $this->setAmountLeft(0);
}
return $this->setAmountLeft($amountLeft);
}
/**
* @return Booking
*/
public function getBooking()
{
return $this->booking;
}
/**
* @param Booking $booking
* @return Payment
*/
public function setBooking($booking)
{
$this->booking = $booking;
return $this;
}
/**
* Add extraCharge
*
* @param \AdminBundle\Entity\PaymentExtraCharge $extraCharge
*
* @return Payment
*/
public function addExtraCharge(PaymentExtraCharge $extraCharge)
{
$this->extraCharges[] = $extraCharge;
return $this;
}
/**
* Remove extraCharge
*
* @param \AdminBundle\Entity\PaymentExtraCharge $extraCharge
*/
public function removeExtraCharge(PaymentExtraCharge $extraCharge)
{
$this->extraCharges->removeElement($extraCharge);
}
/**
* Get extraCharges
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getExtraCharges()
{
return $this->extraCharges;
}
/**
* Set amountLeftCash
*
* @param float $amountLeftCash
*
* @return Payment
*/
public function setAmountLeftCash($amountLeftCash)
{
$this->amountLeftCash = $amountLeftCash;
return $this;
}
/**
* Get amountLeftCash
*
* @return float
*/
public function getAmountLeftCash()
{
return $this->amountLeftCash;
}
}