<?php
/**
* @author Quentin CHATELAIN (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\SalesBundle\Entity;
use App\Entity\Context;
use App\Entity\Currency;
use App\Entity\Establishment;
use App\Entity\Traits\EntityManagerServiceEntity;
use App\Services\SoftdeleteFilter;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Bluue\CustomersBundle\Entity\Customer;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\NonUniqueResultException;
use Symfony\Component\Uid\Uuid;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Gedmo\Mapping\Annotation as Gedmo;
use Bluue\SalesBundle\Repository\PaymentRepository;
use App\DoctrineExtensions\Timestampable\Traits\UserTimestampableEntity;
use App\DoctrineExtensions\SoftDeleteable\Traits\UserSoftDeleteableEntity;
/**
* @ORM\Entity(repositoryClass=PaymentRepository::class)
* @ORM\Table(name="sales_bundle__payment", indexes={
* @ORM\Index(name="payment_date", columns={"payment_date"}),
* @ORM\Index(name="reference", columns={"reference"}),
* @ORM\Index(name="receipt", columns={"receipt"}),
* @ORM\Index(name="refund", columns={"refund"}),
* @ORM\Index(name="deleted_at", columns={"deleted_at"}),
* @ORM\Index(name="created_at", columns={"created_at"}),
* @ORM\Index(name="updated_at", columns={"updated_at"})
* })
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
*/
class Payment
{
use UserTimestampableEntity;
use UserSoftDeleteableEntity;
use EntityManagerServiceEntity;
/**
* @ORM\Id
* @ORM\Column(type="uuid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
private ?Uuid $id = null;
/**
* @ORM\ManyToOne(targetEntity=Context::class)
*/
private ?Context $context = null;
/**
* @ORM\ManyToOne(targetEntity=Establishment::class)
*/
private ?Establishment $establishment = null;
/**
* @ORM\ManyToOne(targetEntity=Customer::class)
* @ORM\JoinColumn(nullable=false)
*/
private ?Customer $customer = null;
/**
* @ORM\ManyToOne(targetEntity=CreditNote::class)
*/
private ?CreditNote $credit_note = null;
/**
* @ORM\ManyToOne(targetEntity=Invoice::class)
*/
private ?Invoice $invoice = null;
/**
* @ORM\ManyToOne(targetEntity=PaymentMethod::class)
* @ORM\JoinColumn(nullable=false)
*/
private ?PaymentMethod $payment_method = null;
/**
* @ORM\ManyToOne(targetEntity=Bank::class)
*/
private ?Bank $bank = null;
/**
* @ORM\ManyToOne(targetEntity=Currency::class)
* @ORM\JoinColumn(nullable=false)
*/
private ?Currency $currency = null;
/**
* @ORM\Column(type="decimal", precision=20, scale=12)
*/
private ?string $currency_change_rate = null;
/**
* @ORM\Column(type="string", length=128, nullable="true")
*/
private ?string $reference = null;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
*/
private ?string $receipt = null;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
*/
private ?string $refund = null;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
*/
private ?string $residual = null;
/**
* @ORM\Column(type="datetime")
*/
private ?DateTimeInterface $payment_date = null;
/**
* @ORM\OneToMany(targetEntity=DocumentPayment::class, mappedBy="payment", fetch="EXTRA_LAZY")
*/
private Collection $document_payments;
public function __construct()
{
$this->document_payments = new ArrayCollection();
}
/**
* @return Uuid|null
*/
public function getId(): ?Uuid
{
return $this->id;
}
/**
* @return Context|null
*/
public function getContext(): ?Context
{
return $this->context;
}
/**
* @param Context|null $context
* @return Payment
*/
public function setContext(?Context $context): self
{
$this->context = $context;
return $this;
}
/**
* @return Establishment|null
*/
public function getEstablishment(): ?Establishment
{
return $this->establishment;
}
/**
* @param Establishment|null $establishment
* @return Payment
*/
public function setEstablishment(?Establishment $establishment): Payment
{
$this->establishment = $establishment;
return $this;
}
/**
* @return Customer|null
* @throws NonUniqueResultException
*/
public function getCustomer(): ?Customer
{
if ($this->em && $this->customer) {
SoftdeleteFilter::disable($this->em, [Customer::class]);
$customer = $this->em->getRepository(Customer::class)
->createQueryBuilder('c')
->where('c.id = :customer')
->setParameter('customer', $this->customer->getId()->toBinary())
->getQuery()
->getOneOrNullResult();
SoftdeleteFilter::enable($this->em, [Customer::class]);
return $customer;
}
return $this->customer;
}
/**
* @param Customer $customer
* @return $this
*/
public function setCustomer(Customer $customer): self
{
$this->customer = $customer;
return $this;
}
/**
* @return CreditNote|null
*/
public function getCreditNote(): ?CreditNote
{
return $this->credit_note;
}
/**
* @param CreditNote|null $credit_note
* @return Payment
* @throws NonUniqueResultException
*/
public function setCreditNote(?CreditNote $credit_note): self
{
$this->credit_note = $credit_note;
$this->setCurrency($credit_note->getCurrency());
$this->setCurrencyChangeRate($credit_note->getCurrencyChangeRate());
$this->setReference($credit_note->getReference());
return $this->setCustomer($credit_note->getCustomer());
}
/**
* @return Invoice|null
* @throws NonUniqueResultException
*/
public function getInvoice(): ?Invoice
{
if ($this->em && $this->invoice) {
SoftdeleteFilter::disable($this->em, [Invoice::class]);
$invoice = $this->em->getRepository(Invoice::class)
->createQueryBuilder('i')
->where('i.id = :invoiceId')
->setParameter('invoiceId', $this->invoice->getId()->toBinary())
->getQuery()
->getOneOrNullResult();
SoftdeleteFilter::enable($this->em, [Invoice::class]);
return $invoice;
}
return $this->invoice;
}
/**
* @param Invoice|null $invoice
* @return Payment
* @throws NonUniqueResultException
*/
public function setInvoice(?Invoice $invoice): self
{
$this->invoice = $invoice;
$this->setCurrency($invoice->getCurrency());
$this->setCurrencyChangeRate($invoice->getCurrencyChangeRate());
$this->setReference($invoice->getReference());
return $this->setCustomer($invoice->getCustomer());
}
/**
* @return PaymentMethod|null
* @throws NonUniqueResultException
*/
public function getPaymentMethod(): ?PaymentMethod
{
if ($this->em && $this->payment_method) {
SoftdeleteFilter::disable($this->em, [PaymentMethod::class]);
$payment_method = $this->em->getRepository(PaymentMethod::class)
->createQueryBuilder('pm')
->where('pm.id = :paymentMethodId')
->setParameter('paymentMethodId', $this->payment_method->getId()->toBinary())
->getQuery()
->getOneOrNullResult();
SoftdeleteFilter::enable($this->em, [PaymentMethod::class]);
return $payment_method;
}
return $this->payment_method;
}
/**
* @param PaymentMethod $payment_method
* @return $this
*/
public function setPaymentMethod(PaymentMethod $payment_method): self
{
$this->payment_method = $payment_method;
return $this;
}
/**
* @return Bank|null
* @throws NonUniqueResultException
*/
public function getBank(): ?Bank
{
if (!$this->bank) {
return null;
} elseif ($this->getId()) {
SoftdeleteFilter::disable($this->em, [Bank::class]);
$bank = $this->em->getRepository(Bank::class)
->createQueryBuilder('b')
->where('b.id = :bankId')
->setParameter('bankId', $this->bank->getId()->toBinary())
->getQuery()
->getOneOrNullResult();
SoftdeleteFilter::enable($this->em, [Bank::class]);
return $bank;
}
return null;
}
/**
* @param Bank|null $bank
*/
public function setBank(?Bank $bank): void
{
$this->bank = $bank;
}
/**
* @return Currency|null
*/
public function getCurrency(): ?Currency
{
return $this->currency;
}
/**
* @param Currency $currency
* @return $this
*/
public function setCurrency(Currency $currency): self
{
$this->currency = $currency;
return $this->setCurrencyChangeRate($currency->getChangeRate());
}
/**
* @return string|null
*/
public function getCurrencyChangeRate(): ?string
{
return $this->currency_change_rate;
}
/**
* @param string $currency_change_rate
* @return $this
*/
public function setCurrencyChangeRate(string $currency_change_rate): self
{
$this->currency_change_rate = $currency_change_rate;
return $this;
}
/**
* @return string|null
*/
public function getReference(): ?string
{
return $this->reference;
}
/**
* @param string|null $reference
* @return $this
*/
public function setReference(?string $reference): self
{
$this->reference = $reference;
return $this;
}
/**
* @return string|null
*/
public function getReceipt(): ?string
{
return $this->receipt;
}
/**
* @param string|null $receipt
* @return $this
*/
public function setReceipt(?string $receipt): self
{
$this->receipt = $receipt;
return $receipt ? $this->setResidual($receipt) : $this;
}
/**
* @return string|null
*/
public function getRefund(): ?string
{
return $this->refund;
}
/**
* @param string|null $refund
* @return $this
*/
public function setRefund(?string $refund): self
{
$this->refund = $refund;
return $refund ? $this->setResidual($refund) : $this;
}
/**
* @return string|null
*/
public function getResidual(): ?string
{
return $this->residual;
}
/**
* @param string|null $residual
* @return Payment
*/
public function setResidual(?string $residual): self
{
$this->residual = $residual;
return $this;
}
/**
* @return DateTimeInterface|null
*/
public function getPaymentDate(): ?DateTimeInterface
{
return $this->payment_date;
}
/**
* @param $payment_date
* @return $this
*/
public function setPaymentDate($payment_date): self
{
if ($payment_date instanceof DateTimeInterface) {
$this->payment_date = $payment_date;
} elseif ($payment_date) {
$this->payment_date = new \DateTime($payment_date);
} else {
$this->payment_date = null;
}
return $this;
}
/**
* @return Collection|DocumentPayment[]
*/
public function getDocumentPayments(): Collection
{
return $this->document_payments;
}
/**
* @return Invoice|CreditNote|null
* @throws NonUniqueResultException
*/
public function getDocumentOrigin()
{
return $this->getInvoice() ?? $this->getCreditNote();
}
/**
* @return string|null
*/
public function getAmount(): ?string
{
return $this->getReceipt() ?? $this->getRefund();
}
}