src/Billing/Entity/Invoice.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Billing\Entity;
  3. use App\Billing\Model\BillablePartyInterface;
  4. use App\Repository\InvoiceRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. #[ORM\Entity(repositoryClassInvoiceRepository::class)]
  10. #[ORM\HasLifecycleCallbacks]
  11. class Invoice
  12. {
  13.     const STATUS_DRAFT 'DRAFT';
  14.     const STATUS_VALIDATED 'VALIDATED';
  15.     const STATUS_PAID 'PAID';
  16.     const STATUS_CANCELLED 'CANCELLED';
  17.     const TYPE_INVOICE 'INVOICE';
  18.     const TYPE_CREDIT_NOTE 'CREDIT_NOTE'// Avoir
  19.     const TYPE_DOWN_PAYMENT 'DOWN_PAYMENT'// Facture d'acompte
  20.     #[ORM\Id]
  21.     #[ORM\GeneratedValue]
  22.     #[ORM\Column]
  23.     private ?int $id null;
  24.     #[ORM\Column(length20)]
  25.     private ?string $type self::TYPE_INVOICE;
  26.     #[ORM\ManyToOne(targetEntityself::class, inversedBy'children')]
  27.     private ?self $parentInvoice null;
  28.     #[ORM\OneToMany(mappedBy'parentInvoice'targetEntityself::class)]
  29.     private Collection $children;
  30.     #[ORM\Column(length255uniquetruenullabletrue)]
  31.     private ?string $reference null;
  32.     #[ORM\Column(length50)]
  33.     private ?string $status self::STATUS_DRAFT;
  34.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  35.     private ?\DateTimeInterface $issuedAt null;
  36.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  37.     private ?\DateTimeInterface $dueDate null;
  38.     #[ORM\Column(typeTypes::DECIMALprecision10scale2)]
  39.     private ?string $totalAmountHt '0.00';
  40.     #[ORM\Column(typeTypes::DECIMALprecision10scale2)]
  41.     private ?string $totalTaxAmount '0.00';
  42.     #[ORM\Column(typeTypes::DECIMALprecision10scale2)]
  43.     private ?string $totalAmountTtc '0.00';
  44.     #[ORM\Column(typeTypes::DECIMALprecision10scale3)]
  45.     private ?string $stampDuty '0.000'// Droit de timbre
  46.     #[ORM\OneToMany(mappedBy'invoice'targetEntityInvoiceLine::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  47.     private Collection $lines;
  48.     #[ORM\Column(length255)]
  49.     private ?string $customerName null;
  50.     #[ORM\Column(length255nullabletrue)]
  51.     private ?string $customerAddress null;
  52.     #[ORM\Column(length100nullabletrue)]
  53.     private ?string $customerTaxId null;
  54.     // We store the serialized data of the customer at the moment of billing to keep history
  55.     // even if the customer entity changes later.
  56.     #[ORM\Column(typeTypes::JSONnullabletrue)]
  57.     private array $customerSnapshot = [];
  58.     #[ORM\OneToOne(mappedBy'invoice'targetEntityPaymentSchedule::class, cascade: ['persist''remove'])]
  59.     private ?PaymentSchedule $paymentSchedule null;
  60.     #[ORM\OneToMany(mappedBy'invoice'targetEntityPayment::class, cascade: ['persist'])]
  61.     private Collection $payments;
  62.     public function __construct()
  63.     {
  64.         $this->lines = new ArrayCollection();
  65.         $this->payments = new ArrayCollection();
  66.         $this->children = new ArrayCollection();
  67.         $this->issuedAt = new \DateTime();
  68.     }
  69.     public function getPaymentSchedule(): ?PaymentSchedule
  70.     {
  71.         return $this->paymentSchedule;
  72.     }
  73.     public function setPaymentSchedule(PaymentSchedule $paymentSchedule): static
  74.     {
  75.         // set the owning side of the relation if necessary
  76.         if ($paymentSchedule->getInvoice() !== $this) {
  77.             $paymentSchedule->setInvoice($this);
  78.         }
  79.         $this->paymentSchedule $paymentSchedule;
  80.         return $this;
  81.     }
  82.     /**
  83.      * @return Collection<int, Payment>
  84.      */
  85.     public function getPayments(): Collection
  86.     {
  87.         return $this->payments;
  88.     }
  89.     public function addPayment(Payment $payment): static
  90.     {
  91.         if (!$this->payments->contains($payment)) {
  92.             $this->payments->add($payment);
  93.             $payment->setInvoice($this);
  94.         }
  95.         return $this;
  96.     }
  97.     public function removePayment(Payment $payment): static
  98.     {
  99.         if ($this->payments->removeElement($payment)) {
  100.             // set the owning side to null (unless already changed)
  101.             if ($payment->getInvoice() === $this) {
  102.                 $payment->setInvoice(null);
  103.             }
  104.         }
  105.         return $this;
  106.     }
  107.     public function getType(): ?string
  108.     {
  109.         return $this->type;
  110.     }
  111.     public function setType(string $type): static
  112.     {
  113.         $this->type $type;
  114.         return $this;
  115.     }
  116.     public function getParentInvoice(): ?self
  117.     {
  118.         return $this->parentInvoice;
  119.     }
  120.     public function setParentInvoice(?self $parentInvoice): static
  121.     {
  122.         $this->parentInvoice $parentInvoice;
  123.         return $this;
  124.     }
  125.     /**
  126.      * @return Collection<int, self>
  127.      */
  128.     public function getChildren(): Collection
  129.     {
  130.         return $this->children;
  131.     }
  132.     public function getId(): ?int
  133.     {
  134.         return $this->id;
  135.     }
  136.     public function getReference(): ?string
  137.     {
  138.         return $this->reference;
  139.     }
  140.     public function setReference(?string $reference): static
  141.     {
  142.         $this->reference $reference;
  143.         return $this;
  144.     }
  145.     public function getStatus(): ?string
  146.     {
  147.         return $this->status;
  148.     }
  149.     public function setStatus(string $status): static
  150.     {
  151.         $this->status $status;
  152.         return $this;
  153.     }
  154.     public function getIssuedAt(): ?\DateTimeInterface
  155.     {
  156.         return $this->issuedAt;
  157.     }
  158.     public function setIssuedAt(\DateTimeInterface $issuedAt): static
  159.     {
  160.         $this->issuedAt $issuedAt;
  161.         return $this;
  162.     }
  163.     public function getDueDate(): ?\DateTimeInterface
  164.     {
  165.         return $this->dueDate;
  166.     }
  167.     public function setDueDate(?\DateTimeInterface $dueDate): static
  168.     {
  169.         $this->dueDate $dueDate;
  170.         return $this;
  171.     }
  172.     public function getTotalAmountHt(): ?string
  173.     {
  174.         return $this->totalAmountHt;
  175.     }
  176.     public function setTotalAmountHt(string $totalAmountHt): static
  177.     {
  178.         $this->totalAmountHt $totalAmountHt;
  179.         return $this;
  180.     }
  181.     public function getTotalTaxAmount(): ?string
  182.     {
  183.         return $this->totalTaxAmount;
  184.     }
  185.     public function setTotalTaxAmount(string $totalTaxAmount): static
  186.     {
  187.         $this->totalTaxAmount $totalTaxAmount;
  188.         return $this;
  189.     }
  190.     public function getTotalAmountTtc(): ?string
  191.     {
  192.         return $this->totalAmountTtc;
  193.     }
  194.     public function setTotalAmountTtc(string $totalAmountTtc): static
  195.     {
  196.         $this->totalAmountTtc $totalAmountTtc;
  197.         return $this;
  198.     }
  199.     public function getStampDuty(): ?string
  200.     {
  201.         return $this->stampDuty;
  202.     }
  203.     public function setStampDuty(string $stampDuty): static
  204.     {
  205.         $this->stampDuty $stampDuty;
  206.         return $this;
  207.     }
  208.     /**
  209.      * @return Collection<int, InvoiceLine>
  210.      */
  211.     public function getLines(): Collection
  212.     {
  213.         return $this->lines;
  214.     }
  215.     public function addLine(InvoiceLine $line): static
  216.     {
  217.         if (!$this->lines->contains($line)) {
  218.             $this->lines->add($line);
  219.             $line->setInvoice($this);
  220.         }
  221.         return $this;
  222.     }
  223.     public function removeLine(InvoiceLine $line): static
  224.     {
  225.         if ($this->lines->removeElement($line)) {
  226.             // set the owning side to null (unless already changed)
  227.             if ($line->getInvoice() === $this) {
  228.                 $line->setInvoice(null);
  229.             }
  230.         }
  231.         return $this;
  232.     }
  233.     public function getCustomerName(): ?string
  234.     {
  235.         return $this->customerName;
  236.     }
  237.     public function setCustomerName(string $customerName): static
  238.     {
  239.         $this->customerName $customerName;
  240.         return $this;
  241.     }
  242.     public function getCustomerAddress(): ?string
  243.     {
  244.         return $this->customerAddress;
  245.     }
  246.     public function setCustomerAddress(?string $customerAddress): static
  247.     {
  248.         $this->customerAddress $customerAddress;
  249.         return $this;
  250.     }
  251.     public function getCustomerTaxId(): ?string
  252.     {
  253.         return $this->customerTaxId;
  254.     }
  255.     public function setCustomerTaxId(?string $customerTaxId): static
  256.     {
  257.         $this->customerTaxId $customerTaxId;
  258.         return $this;
  259.     }
  260.     public function getCustomerSnapshot(): array
  261.     {
  262.         return $this->customerSnapshot;
  263.     }
  264.     public function setCustomerSnapshot(array $customerSnapshot): static
  265.     {
  266.         $this->customerSnapshot $customerSnapshot;
  267.         return $this;
  268.     }
  269.     public function getCustomerType(): ?string
  270.     {
  271.         return $this->customerSnapshot['type'] ?? null;
  272.     }
  273.     public function getCustomerId(): ?int
  274.     {
  275.         return $this->customerSnapshot['id'] ?? null;
  276.     }
  277.     public function setCustomer(BillablePartyInterface $customer): static
  278.     {
  279.         $this->customerName $customer->getBillingName();
  280.         $this->customerAddress $customer->getBillingAddress();
  281.         $this->customerTaxId $customer->getTaxId();
  282.         $type null;
  283.         if ($customer instanceof \App\Entity\User) {
  284.             $type 'User';
  285.         } elseif ($customer instanceof \App\Entity\Organization) {
  286.             $type 'Organization';
  287.         }
  288.         // Store snapshot for history
  289.         $this->customerSnapshot = [
  290.             'id' => method_exists($customer'getId') ? $customer->getId() : null,
  291.             'type' => $type,
  292.             'name' => $customer->getBillingName(),
  293.             'address' => $customer->getBillingAddress(),
  294.             'email' => $customer->getBillingEmail(),
  295.             'tax_id' => $customer->getTaxId(),
  296.         ];
  297.         return $this;
  298.     }
  299. }