|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace App\Entity; |
| 6 | + |
| 7 | +use App\Enums\InvoiceStatus; |
| 8 | +use Doctrine\Common\Collections\ArrayCollection; |
| 9 | +use Doctrine\Common\Collections\Collection; |
| 10 | +use Doctrine\DBAL\Types\Types; |
| 11 | +use Doctrine\ORM\Event\LifecycleEventArgs; |
| 12 | +use Doctrine\ORM\Mapping\Column; |
| 13 | +use Doctrine\ORM\Mapping\Entity; |
| 14 | +use Doctrine\ORM\Mapping\GeneratedValue; |
| 15 | +use Doctrine\ORM\Mapping\HasLifecycleCallbacks; |
| 16 | +use Doctrine\ORM\Mapping\Id; |
| 17 | +use Doctrine\ORM\Mapping\OneToMany; |
| 18 | +use Doctrine\ORM\Mapping\PrePersist; |
| 19 | +use Doctrine\ORM\Mapping\Table; |
| 20 | + |
| 21 | +#[Entity] |
| 22 | +#[Table('invoices')] |
| 23 | +#[HasLifecycleCallbacks] |
| 24 | +class Invoice |
| 25 | +{ |
| 26 | + #[Id] |
| 27 | + #[Column, GeneratedValue] |
| 28 | + private int $id; |
| 29 | + |
| 30 | + #[Column(type: Types::DECIMAL, precision: 10, scale: 2)] |
| 31 | + private float $amount; |
| 32 | + |
| 33 | + #[Column(name: 'invoice_number')] |
| 34 | + private string $invoiceNumber; |
| 35 | + |
| 36 | + #[Column] |
| 37 | + private InvoiceStatus $status; |
| 38 | + |
| 39 | + #[Column(name: 'created_at')] |
| 40 | + private \DateTime $createdAt; |
| 41 | + |
| 42 | + #[Column(name: 'due_date')] |
| 43 | + private \DateTime $dueDate; |
| 44 | + |
| 45 | + #[OneToMany(targetEntity: InvoiceItem::class, mappedBy: 'invoice', cascade: ['persist', 'remove'])] |
| 46 | + private Collection $items; |
| 47 | + |
| 48 | + public function __construct() |
| 49 | + { |
| 50 | + $this->items = new ArrayCollection(); |
| 51 | + } |
| 52 | + |
| 53 | + #[PrePersist] |
| 54 | + public function onPrePersist(LifecycleEventArgs $args) |
| 55 | + { |
| 56 | + $this->createdAt = new \DateTime(); |
| 57 | + } |
| 58 | + |
| 59 | + public function getId(): int |
| 60 | + { |
| 61 | + return $this->id; |
| 62 | + } |
| 63 | + |
| 64 | + public function getAmount(): float |
| 65 | + { |
| 66 | + return $this->amount; |
| 67 | + } |
| 68 | + |
| 69 | + public function setAmount(float $amount): Invoice |
| 70 | + { |
| 71 | + $this->amount = $amount; |
| 72 | + |
| 73 | + return $this; |
| 74 | + } |
| 75 | + |
| 76 | + public function getInvoiceNumber(): string |
| 77 | + { |
| 78 | + return $this->invoiceNumber; |
| 79 | + } |
| 80 | + |
| 81 | + public function setInvoiceNumber(string $invoiceNumber): Invoice |
| 82 | + { |
| 83 | + $this->invoiceNumber = $invoiceNumber; |
| 84 | + |
| 85 | + return $this; |
| 86 | + } |
| 87 | + |
| 88 | + public function getStatus(): InvoiceStatus |
| 89 | + { |
| 90 | + return $this->status; |
| 91 | + } |
| 92 | + |
| 93 | + public function setStatus(InvoiceStatus $status): Invoice |
| 94 | + { |
| 95 | + $this->status = $status; |
| 96 | + |
| 97 | + return $this; |
| 98 | + } |
| 99 | + |
| 100 | + public function getCreatedAt(): \DateTime |
| 101 | + { |
| 102 | + return $this->createdAt; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return Collection<InvoiceItem> |
| 107 | + */ |
| 108 | + public function getItems(): Collection |
| 109 | + { |
| 110 | + return $this->items; |
| 111 | + } |
| 112 | + |
| 113 | + public function addItem(InvoiceItem $item): Invoice |
| 114 | + { |
| 115 | + $item->setInvoice($this); |
| 116 | + |
| 117 | + $this->items->add($item); |
| 118 | + |
| 119 | + return $this; |
| 120 | + } |
| 121 | +} |
0 commit comments