src/Entity/Finance/Proforma.php line 37
<?php
namespace App\Entity\Finance;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Common;
use App\Entity\Expediting\ShippingPlan;
use App\Entity\Identity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
#[ORM\Table(name: 'finance_invoice')]
#[ORM\Index(columns: ['shipping_plan_id'], name: 'finance_invoice_shipping_plan_id_idx')]
#[ORM\Index(columns: ['payable_by'], name: 'finance_invoice_payable_by_idx')]
#[ORM\Index(columns: ['payable_to'], name: 'finance_invoice_payable_to_idx')]
#[ORM\Index(columns: ['created_by'], name: 'finance_invoice_created_by_idx')]
#[ORM\Index(columns: ['updated_by'], name: 'finance_invoice_updated_by_idx')]
#[ORM\UniqueConstraint(name: 'finance_invoice_number_key', columns: ['number'])]
#[ORM\UniqueConstraint(name: 'finance_invoice_external_reference_key', columns: ['external_reference'])]
#[ORM\UniqueConstraint(name: 'finance_invoice_original_quote_id_key', columns: ['original_quote_id'])]
#[ORM\UniqueConstraint(name: 'finance_invoice_identifier_key', columns: ['identifier'])]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'type')]
#[ORM\DiscriminatorMap([
'INVOICE' => Invoice::class,
'PROFORMA' => Proforma::class,
])]
#[ORM\Entity]
#[ApiResource(routePrefix: '/finance')]
class Proforma
{
use Quoteable;
use Common\Blameable;
use Common\Trackable;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
#[ORM\SequenceGenerator(sequenceName: 'finance_invoice_id_seq')]
#[ORM\Column(type: Types::INTEGER)]
#[ApiProperty(identifier: false)]
protected ?int $id = null;
#[Assert\Ulid]
#[Assert\NotNull]
#[ORM\Column(type: 'ulid', unique: true)]
#[ApiProperty(identifier: true)]
protected ?Ulid $identifier = null;
#[Assert\Type(type: ShippingPlan::class)]
#[ORM\ManyToOne(targetEntity: ShippingPlan::class, inversedBy: 'proformas')]
#[ORM\JoinColumn(name: 'shipping_plan_id', nullable: false)]
protected ?ShippingPlan $shippingPlan = null;
#[Assert\Type(type: Proforma::class)]
#[ORM\ManyToOne(targetEntity: Proforma::class, inversedBy: 'editions')]
#[ORM\JoinColumn(name: 'original_quote_id')]
protected ?Proforma $originalForm = null;
#[ORM\OneToMany(mappedBy: 'originalForm', targetEntity: Proforma::class)]
protected Collection $editions;
/** @var Collection<int, Collectable> */
#[ORM\OneToMany(mappedBy: 'form', targetEntity: Collectable::class, cascade: ['persist', 'remove'])]
protected Collection $collectables;
/** @var Collection<int, Tax> */
#[ORM\OneToMany(mappedBy: 'invoice', targetEntity: Tax::class, cascade: ['persist', 'remove'])]
protected Collection $taxes;
#[Gedmo\Blameable(on: 'create')]
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(name: 'created_by')]
protected ?User $createdBy = null;
public function __construct()
{
$this->identifier = new Ulid();
$this->collectables = new ArrayCollection();
$this->taxes = new ArrayCollection();
$this->editions = new ArrayCollection();
}
#[Assert\Callback]
public function validate(ExecutionContextInterface $context, $payload): void
{
if (empty($this->workOrder) && empty($this->shipment)) {
$context->buildViolation('Invoice must be associated only with either, a work order, or a shipment')
->atPath('workOrder')
->addViolation();
$context->buildViolation('Invoice must be associated only with either, a work order, or a shipment')
->atPath('shipment')
->addViolation();
}
}
public function getId(): ?int
{
return $this->id;
}
public function getIdentifier(): ?Ulid
{
return $this->identifier;
}
public function setIdentifier($identifier): self
{
$this->identifier = $identifier;
return $this;
}
public function getShippingPlan(): ?ShippingPlan
{
return $this->shippingPlan;
}
public function setShippingPlan(?ShippingPlan $shippingPlan): self
{
$this->shippingPlan = $shippingPlan;
return $this;
}
public function getOriginalForm(): ?self
{
return $this->originalForm;
}
public function setOriginalForm(?self $originalForm): self
{
$form = $originalForm;
while ($form?->getOriginalForm() instanceof Proforma) {
$form = $form->getOriginalForm();
}
$this->originalForm = $form;
return $this;
}
/**
* @return Collection<int, Proforma>
*/
public function getEditions(): Collection
{
return $this->editions;
}
public function addEdition(Proforma $edition): self
{
if (!$this->editions->contains($edition)) {
$this->editions->add($edition);
$edition->setOriginalForm($this);
}
return $this;
}
public function removeEdition(Proforma $edition): self
{
if ($this->editions->removeElement($edition)) {
// set the owning side to null (unless already changed)
if ($edition->getOriginalForm() === $this) {
$edition->setOriginalForm(null);
}
}
return $this;
}
/**
* @return Collection<int, Collectable>
*/
public function getCollectables(): Collection
{
return $this->collectables;
}
public function addCollectable(Collectable $collectable): self
{
if (!$this->collectables->contains($collectable)) {
$this->collectables->add($collectable);
$collectable->setForm($this);
}
return $this;
}
public function removeCollectable(Collectable $collectable): self
{
if ($this->collectables->removeElement($collectable)) {
// set the owning side to null (unless already changed)
if ($collectable->getForm() === $this) {
$collectable->setForm(null);
}
}
return $this;
}
/**
* @return Collection<int, Tax>
*/
public function getTaxes(): Collection
{
return $this->taxes;
}
public function addTax(Tax $tax): self
{
if (!$this->taxes->contains($tax)) {
$this->taxes->add($tax);
$tax->setInvoice($this);
}
return $this;
}
public function removeTax(Tax $tax): self
{
if ($this->taxes->removeElement($tax)) {
// set the owning side to null (unless already changed)
if ($tax->getInvoice() === $this) {
$tax->setInvoice(null);
}
}
return $this;
}
}