support custom fields for invoices (#3077)

This commit is contained in:
Kevin Papst
2022-01-20 16:59:26 +01:00
committed by GitHub
parent 9731023014
commit 40242b7dcc
15 changed files with 396 additions and 6 deletions

View File

@@ -11,6 +11,8 @@ namespace App\Entity;
use App\Export\Annotation as Exporter;
use App\Invoice\InvoiceModel;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
@@ -34,7 +36,7 @@ use Symfony\Component\Validator\Constraints as Assert;
* @Exporter\Expose("user", label="label.username", type="string", exp="object.getUser() === null ? null : object.getUser().getDisplayName()")
* @Exporter\Expose("paymentDate", label="invoice.payment_date", type="date", exp="object.getPaymentDate() === null ? null : object.getPaymentDate()")
*/
class Invoice
class Invoice implements EntityWithMetaFields
{
public const STATUS_PENDING = 'pending';
public const STATUS_PAID = 'paid';
@@ -49,7 +51,6 @@ class Invoice
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @phpstan-ignore-next-line
*/
private $id;
@@ -196,6 +197,28 @@ class Invoice
*/
private $paymentDate;
/**
* Meta fields
*
* All visible meta (custom) fields registered with this invoice
*
* @var InvoiceMeta[]|Collection
*
* @Serializer\Expose()
* @Serializer\Groups({"Invoice"})
* @Serializer\Type(name="array<App\Entity\InvoiceMeta>")
* @Serializer\SerializedName("metaFields")
* @Serializer\Accessor(getter="getVisibleMetaFields")
*
* @ORM\OneToMany(targetEntity="App\Entity\InvoiceMeta", mappedBy="invoice", cascade={"persist"})
*/
private $meta;
public function __construct()
{
$this->meta = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
@@ -399,4 +422,68 @@ class Invoice
{
return $this->comment;
}
/**
* @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 (strtolower($field->getName()) === strtolower($name)) {
return $field;
}
}
return null;
}
public function setMetaField(MetaTableTypeInterface $meta): EntityWithMetaFields
{
if (null === ($current = $this->getMetaField($meta->getName()))) {
$meta->setEntity($this);
$this->meta->add($meta);
return $this;
}
$current->merge($meta);
return $this;
}
public function __clone()
{
if ($this->id) {
$this->id = null;
}
$currentMeta = $this->meta;
$this->meta = new ArrayCollection();
/** @var InvoiceMeta $meta */
foreach ($currentMeta as $meta) {
$newMeta = clone $meta;
$newMeta->setEntity($this);
$this->setMetaField($newMeta);
}
}
}