Meta-fields for InvoiceTemplate, structured Customer address (#5519)
This commit is contained in:
@@ -10,6 +10,8 @@
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\InvoiceTemplateRepository;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||
@@ -20,7 +22,7 @@ use Symfony\Component\Validator\Constraints as Assert;
|
||||
#[ORM\Entity(repositoryClass: InvoiceTemplateRepository::class)]
|
||||
#[ORM\ChangeTrackingPolicy('DEFERRED_EXPLICIT')]
|
||||
#[UniqueEntity('name')]
|
||||
class InvoiceTemplate
|
||||
class InvoiceTemplate implements EntityWithMetaFields
|
||||
{
|
||||
#[ORM\Column(name: 'id', type: Types::INTEGER)]
|
||||
#[ORM\Id]
|
||||
@@ -34,9 +36,8 @@ class InvoiceTemplate
|
||||
#[Assert\Length(max: 255)]
|
||||
#[Assert\NotBlank]
|
||||
private ?string $title = null;
|
||||
#[ORM\Column(name: 'company', type: 'string', length: 255, nullable: false)]
|
||||
#[ORM\Column(name: 'company', type: Types::STRING, length: 255, nullable: true)]
|
||||
#[Assert\Length(max: 255)]
|
||||
#[Assert\NotBlank]
|
||||
private ?string $company = null;
|
||||
#[ORM\Column(name: 'vat_id', type: Types::STRING, length: 50, nullable: true)]
|
||||
#[Assert\Length(max: 50)]
|
||||
@@ -75,17 +76,32 @@ class InvoiceTemplate
|
||||
#[ORM\Column(name: 'language', type: Types::STRING, length: 6, nullable: false)]
|
||||
#[Assert\NotBlank]
|
||||
private ?string $language = 'en';
|
||||
/**
|
||||
* Customer for this invoice template
|
||||
*/
|
||||
#[ORM\ManyToOne(targetEntity: Customer::class)]
|
||||
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
|
||||
#[Assert\NotNull]
|
||||
private ?Customer $customer = null;
|
||||
/**
|
||||
* @var Collection<int, InvoiceTemplateMeta>
|
||||
*/
|
||||
#[ORM\OneToMany(mappedBy: 'template', targetEntity: InvoiceTemplateMeta::class, cascade: ['persist'])]
|
||||
private Collection $meta;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->meta = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setName(string $name): InvoiceTemplate
|
||||
public function setName(string $name): void
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getName(): ?string
|
||||
@@ -93,6 +109,16 @@ class InvoiceTemplate
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getCustomer(): ?Customer
|
||||
{
|
||||
return $this->customer;
|
||||
}
|
||||
|
||||
public function setCustomer(?Customer $customer): void
|
||||
{
|
||||
$this->customer = $customer;
|
||||
}
|
||||
|
||||
// ---- trait methods below ---
|
||||
|
||||
public function getTitle(): ?string
|
||||
@@ -100,23 +126,22 @@ class InvoiceTemplate
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function setTitle(?string $title): InvoiceTemplate
|
||||
public function setTitle(?string $title): void
|
||||
{
|
||||
$this->title = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAddress(): ?string
|
||||
{
|
||||
return $this->address;
|
||||
return $this->customer?->getFormattedAddress() ?? $this->address;
|
||||
}
|
||||
|
||||
public function setAddress(?string $address): InvoiceTemplate
|
||||
/**
|
||||
* @deprecated since 2.41
|
||||
*/
|
||||
public function setAddress(?string $address): void
|
||||
{
|
||||
$this->address = $address;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getNumberGenerator(): string
|
||||
@@ -124,11 +149,9 @@ class InvoiceTemplate
|
||||
return $this->numberGenerator;
|
||||
}
|
||||
|
||||
public function setNumberGenerator(string $numberGenerator): InvoiceTemplate
|
||||
public function setNumberGenerator(string $numberGenerator): void
|
||||
{
|
||||
$this->numberGenerator = $numberGenerator;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDueDays(): ?int
|
||||
@@ -136,11 +159,9 @@ class InvoiceTemplate
|
||||
return $this->dueDays;
|
||||
}
|
||||
|
||||
public function setDueDays(?int $dueDays): InvoiceTemplate
|
||||
public function setDueDays(?int $dueDays): void
|
||||
{
|
||||
$this->dueDays = $dueDays;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getVat(): ?float
|
||||
@@ -148,23 +169,22 @@ class InvoiceTemplate
|
||||
return $this->vat;
|
||||
}
|
||||
|
||||
public function setVat(?float $vat): InvoiceTemplate
|
||||
public function setVat(?float $vat): void
|
||||
{
|
||||
$this->vat = $vat;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCompany(): ?string
|
||||
{
|
||||
return $this->company;
|
||||
return $this->customer?->getCompany() ?? $this->customer?->getName() ?? $this->company;
|
||||
}
|
||||
|
||||
public function setCompany(?string $company): InvoiceTemplate
|
||||
/**
|
||||
* @deprecated since 2.41
|
||||
*/
|
||||
public function setCompany(?string $company): void
|
||||
{
|
||||
$this->company = $company;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getRenderer(): string
|
||||
@@ -172,11 +192,9 @@ class InvoiceTemplate
|
||||
return $this->renderer;
|
||||
}
|
||||
|
||||
public function setRenderer(string $renderer): InvoiceTemplate
|
||||
public function setRenderer(string $renderer): void
|
||||
{
|
||||
$this->renderer = $renderer;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCalculator(): string
|
||||
@@ -184,11 +202,9 @@ class InvoiceTemplate
|
||||
return $this->calculator;
|
||||
}
|
||||
|
||||
public function setCalculator(string $calculator): InvoiceTemplate
|
||||
public function setCalculator(string $calculator): void
|
||||
{
|
||||
$this->calculator = $calculator;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPaymentTerms(): ?string
|
||||
@@ -196,23 +212,22 @@ class InvoiceTemplate
|
||||
return $this->paymentTerms;
|
||||
}
|
||||
|
||||
public function setPaymentTerms(?string $paymentTerms): InvoiceTemplate
|
||||
public function setPaymentTerms(?string $paymentTerms): void
|
||||
{
|
||||
$this->paymentTerms = $paymentTerms;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getVatId(): ?string
|
||||
{
|
||||
return $this->vatId;
|
||||
return $this->customer?->getVatId() ?? $this->vatId;
|
||||
}
|
||||
|
||||
public function setVatId(?string $vatId): InvoiceTemplate
|
||||
/**
|
||||
* @deprecated since 2.41
|
||||
*/
|
||||
public function setVatId(?string $vatId): void
|
||||
{
|
||||
$this->vatId = $vatId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getContact(): ?string
|
||||
@@ -220,11 +235,9 @@ class InvoiceTemplate
|
||||
return $this->contact;
|
||||
}
|
||||
|
||||
public function setContact(?string $contact): InvoiceTemplate
|
||||
public function setContact(?string $contact): void
|
||||
{
|
||||
$this->contact = $contact;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPaymentDetails(): ?string
|
||||
@@ -232,11 +245,9 @@ class InvoiceTemplate
|
||||
return $this->paymentDetails;
|
||||
}
|
||||
|
||||
public function setPaymentDetails(?string $paymentDetails): InvoiceTemplate
|
||||
public function setPaymentDetails(?string $paymentDetails): void
|
||||
{
|
||||
$this->paymentDetails = $paymentDetails;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -252,9 +263,78 @@ class InvoiceTemplate
|
||||
return $this->language;
|
||||
}
|
||||
|
||||
public function setLanguage(?string $language): InvoiceTemplate
|
||||
public function setLanguage(?string $language): void
|
||||
{
|
||||
$this->language = $language;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Tax[]
|
||||
*/
|
||||
public function getTaxRates(): array
|
||||
{
|
||||
// TODO make me configurable via UI
|
||||
$tax = new Tax(
|
||||
TaxType::STANDARD,
|
||||
'VAT',
|
||||
$this->vat ?? 0.00
|
||||
);
|
||||
|
||||
return [$tax];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection|MetaTableTypeInterface[]
|
||||
*/
|
||||
public function getMetaFields(): Collection
|
||||
{
|
||||
return $this->meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MetaTableTypeInterface[]
|
||||
*/
|
||||
public function getVisibleMetaFields(): array
|
||||
{
|
||||
$all = [];
|
||||
foreach ($this->meta as $meta) {
|
||||
if ($meta->isVisible()) {
|
||||
$all[] = $meta;
|
||||
}
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
|
||||
public function getMetaField(string $name): ?MetaTableTypeInterface
|
||||
{
|
||||
foreach ($this->meta as $field) {
|
||||
if ($field->getName() !== null && strtolower($field->getName()) === strtolower($name)) {
|
||||
return $field;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function setMetaField(MetaTableTypeInterface $meta): EntityWithMetaFields
|
||||
{
|
||||
if ($meta->getName() === null) {
|
||||
throw new \InvalidArgumentException('Meta-field needs to have a name');
|
||||
}
|
||||
|
||||
if (!$meta instanceof InvoiceTemplateMeta) {
|
||||
throw new \InvalidArgumentException('Meta-field needs to be an instanceof InvoiceTemplateMeta');
|
||||
}
|
||||
|
||||
if (null === ($current = $this->getMetaField($meta->getName()))) {
|
||||
$meta->setEntity($this);
|
||||
$this->meta->add($meta);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$current->merge($meta);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user