<?php
namespace App\Billing\Entity;
use App\Repository\PaymentRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: PaymentRepository::class)]
class Payment
{
const METHOD_CHECK = 'CHECK';
const METHOD_TRANSFER = 'TRANSFER';
const METHOD_CASH = 'CASH';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2)]
private ?string $amount = null;
#[ORM\Column(length: 50)]
private ?string $method = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $date = null;
/**
* Reference of the payment (e.g. Check Number, Transaction ID)
*/
#[ORM\Column(length: 255, nullable: true)]
private ?string $reference = null;
#[ORM\ManyToOne(inversedBy: 'payments')]
#[ORM\JoinColumn(nullable: true)]
private ?Invoice $invoice = null;
#[ORM\Column(type: Types::JSON, nullable: true)]
private ?array $customerSnapshot = [];
#[ORM\Column(type: Types::BOOLEAN, options: ['default' => false])]
private ?bool $isAdvance = false;
#[ORM\ManyToMany(targetEntity: Installment::class, inversedBy: 'payments')]
private Collection $installments;
public function __construct()
{
$this->installments = new ArrayCollection();
$this->date = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getAmount(): ?string
{
return $this->amount;
}
public function setAmount(string $amount): static
{
$this->amount = $amount;
return $this;
}
public function getMethod(): ?string
{
return $this->method;
}
public function setMethod(string $method): static
{
$this->method = $method;
return $this;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(\DateTimeInterface $date): static
{
$this->date = $date;
return $this;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(?string $reference): static
{
$this->reference = $reference;
return $this;
}
public function getInvoice(): ?Invoice
{
return $this->invoice;
}
public function setInvoice(?Invoice $invoice): static
{
$this->invoice = $invoice;
return $this;
}
/**
* @return Collection<int, Installment>
*/
public function getInstallments(): Collection
{
return $this->installments;
}
public function addInstallment(Installment $installment): static
{
if (!$this->installments->contains($installment)) {
$this->installments->add($installment);
}
return $this;
}
public function removeInstallment(Installment $installment): static
{
$this->installments->removeElement($installment);
return $this;
}
public function getCustomerSnapshot(): array
{
return $this->customerSnapshot ?? [];
}
public function setCustomerSnapshot(?array $customerSnapshot): static
{
$this->customerSnapshot = $customerSnapshot;
return $this;
}
public function setCustomer(\App\Billing\Model\BillablePartyInterface $customer): static
{
$type = null;
if ($customer instanceof \App\Entity\User) {
$type = 'User';
} elseif ($customer instanceof \App\Entity\Organization) {
$type = 'Organization';
}
$this->customerSnapshot = [
'id' => method_exists($customer, 'getId') ? $customer->getId() : null,
'type' => $type,
'name' => $customer->getBillingName(),
'address' => $customer->getBillingAddress(),
'email' => $customer->getBillingEmail(),
'tax_id' => $customer->getTaxId(),
];
return $this;
}
public function getCustomerName(): ?string
{
return $this->customerSnapshot['name'] ?? null;
}
public function isAdvance(): bool
{
return $this->isAdvance;
}
public function setIsAdvance(bool $isAdvance): static
{
$this->isAdvance = $isAdvance;
return $this;
}
}