src/Entity/Finance/Proforma.php line 37

  1. <?php
  2. namespace App\Entity\Finance;
  3. use ApiPlatform\Metadata\ApiProperty;
  4. use ApiPlatform\Metadata\ApiResource;
  5. use App\Entity\Common;
  6. use App\Entity\Expediting\ShippingPlan;
  7. use App\Entity\Identity\User;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\DBAL\Types\Types;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Gedmo\Mapping\Annotation as Gedmo;
  13. use Symfony\Component\Uid\Ulid;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  16. #[ORM\Table(name'finance_invoice')]
  17. #[ORM\Index(columns: ['shipping_plan_id'], name'finance_invoice_shipping_plan_id_idx')]
  18. #[ORM\Index(columns: ['payable_by'], name'finance_invoice_payable_by_idx')]
  19. #[ORM\Index(columns: ['payable_to'], name'finance_invoice_payable_to_idx')]
  20. #[ORM\Index(columns: ['created_by'], name'finance_invoice_created_by_idx')]
  21. #[ORM\Index(columns: ['updated_by'], name'finance_invoice_updated_by_idx')]
  22. #[ORM\UniqueConstraint(name'finance_invoice_number_key'columns: ['number'])]
  23. #[ORM\UniqueConstraint(name'finance_invoice_external_reference_key'columns: ['external_reference'])]
  24. #[ORM\UniqueConstraint(name'finance_invoice_original_quote_id_key'columns: ['original_quote_id'])]
  25. #[ORM\UniqueConstraint(name'finance_invoice_identifier_key'columns: ['identifier'])]
  26. #[ORM\InheritanceType('SINGLE_TABLE')]
  27. #[ORM\DiscriminatorColumn(name'type')]
  28. #[ORM\DiscriminatorMap([
  29.     'INVOICE' => Invoice::class,
  30.     'PROFORMA' => Proforma::class,
  31. ])]
  32. #[ORM\Entity]
  33. #[ApiResource(routePrefix'/finance')]
  34. class Proforma
  35. {
  36.     use Quoteable;
  37.     use Common\Blameable;
  38.     use Common\Trackable;
  39.     #[ORM\Id]
  40.     #[ORM\GeneratedValue(strategy'IDENTITY')]
  41.     #[ORM\SequenceGenerator(sequenceName'finance_invoice_id_seq')]
  42.     #[ORM\Column(typeTypes::INTEGER)]
  43.     #[ApiProperty(identifierfalse)]
  44.     protected ?int $id null;
  45.     #[Assert\Ulid]
  46.     #[Assert\NotNull]
  47.     #[ORM\Column(type'ulid'uniquetrue)]
  48.     #[ApiProperty(identifiertrue)]
  49.     protected ?Ulid $identifier null;
  50.     #[Assert\Type(typeShippingPlan::class)]
  51.     #[ORM\ManyToOne(targetEntityShippingPlan::class, inversedBy'proformas')]
  52.     #[ORM\JoinColumn(name'shipping_plan_id'nullablefalse)]
  53.     protected ?ShippingPlan $shippingPlan null;
  54.     #[Assert\Type(typeProforma::class)]
  55.     #[ORM\ManyToOne(targetEntityProforma::class, inversedBy'editions')]
  56.     #[ORM\JoinColumn(name'original_quote_id')]
  57.     protected ?Proforma $originalForm null;
  58.     #[ORM\OneToMany(mappedBy'originalForm'targetEntityProforma::class)]
  59.     protected Collection $editions;
  60.     /** @var Collection<int, Collectable> */
  61.     #[ORM\OneToMany(mappedBy'form'targetEntityCollectable::class, cascade: ['persist''remove'])]
  62.     protected Collection $collectables;
  63.     /** @var Collection<int, Tax> */
  64.     #[ORM\OneToMany(mappedBy'invoice'targetEntityTax::class, cascade: ['persist''remove'])]
  65.     protected Collection $taxes;
  66.     #[Gedmo\Blameable(on'create')]
  67.     #[ORM\ManyToOne(targetEntityUser::class)]
  68.     #[ORM\JoinColumn(name'created_by')]
  69.     protected ?User $createdBy null;
  70.     public function __construct()
  71.     {
  72.         $this->identifier = new Ulid();
  73.         $this->collectables = new ArrayCollection();
  74.         $this->taxes = new ArrayCollection();
  75.         $this->editions = new ArrayCollection();
  76.     }
  77.     #[Assert\Callback]
  78.     public function validate(ExecutionContextInterface $context$payload): void
  79.     {
  80.         if (empty($this->workOrder) && empty($this->shipment)) {
  81.             $context->buildViolation('Invoice must be associated only with either, a work order, or a shipment')
  82.                 ->atPath('workOrder')
  83.                 ->addViolation();
  84.             $context->buildViolation('Invoice must be associated only with either, a work order, or a shipment')
  85.                 ->atPath('shipment')
  86.                 ->addViolation();
  87.         }
  88.     }
  89.     public function getId(): ?int
  90.     {
  91.         return $this->id;
  92.     }
  93.     public function getIdentifier(): ?Ulid
  94.     {
  95.         return $this->identifier;
  96.     }
  97.     public function setIdentifier($identifier): self
  98.     {
  99.         $this->identifier $identifier;
  100.         return $this;
  101.     }
  102.     public function getShippingPlan(): ?ShippingPlan
  103.     {
  104.         return $this->shippingPlan;
  105.     }
  106.     public function setShippingPlan(?ShippingPlan $shippingPlan): self
  107.     {
  108.         $this->shippingPlan $shippingPlan;
  109.         return $this;
  110.     }
  111.     public function getOriginalForm(): ?self
  112.     {
  113.         return $this->originalForm;
  114.     }
  115.     public function setOriginalForm(?self $originalForm): self
  116.     {
  117.         $form $originalForm;
  118.         while ($form?->getOriginalForm() instanceof Proforma) {
  119.             $form $form->getOriginalForm();
  120.         }
  121.         $this->originalForm $form;
  122.         return $this;
  123.     }
  124.     /**
  125.      * @return Collection<int, Proforma>
  126.      */
  127.     public function getEditions(): Collection
  128.     {
  129.         return $this->editions;
  130.     }
  131.     public function addEdition(Proforma $edition): self
  132.     {
  133.         if (!$this->editions->contains($edition)) {
  134.             $this->editions->add($edition);
  135.             $edition->setOriginalForm($this);
  136.         }
  137.         return $this;
  138.     }
  139.     public function removeEdition(Proforma $edition): self
  140.     {
  141.         if ($this->editions->removeElement($edition)) {
  142.             // set the owning side to null (unless already changed)
  143.             if ($edition->getOriginalForm() === $this) {
  144.                 $edition->setOriginalForm(null);
  145.             }
  146.         }
  147.         return $this;
  148.     }
  149.     /**
  150.      * @return Collection<int, Collectable>
  151.      */
  152.     public function getCollectables(): Collection
  153.     {
  154.         return $this->collectables;
  155.     }
  156.     public function addCollectable(Collectable $collectable): self
  157.     {
  158.         if (!$this->collectables->contains($collectable)) {
  159.             $this->collectables->add($collectable);
  160.             $collectable->setForm($this);
  161.         }
  162.         return $this;
  163.     }
  164.     public function removeCollectable(Collectable $collectable): self
  165.     {
  166.         if ($this->collectables->removeElement($collectable)) {
  167.             // set the owning side to null (unless already changed)
  168.             if ($collectable->getForm() === $this) {
  169.                 $collectable->setForm(null);
  170.             }
  171.         }
  172.         return $this;
  173.     }
  174.     /**
  175.      * @return Collection<int, Tax>
  176.      */
  177.     public function getTaxes(): Collection
  178.     {
  179.         return $this->taxes;
  180.     }
  181.     public function addTax(Tax $tax): self
  182.     {
  183.         if (!$this->taxes->contains($tax)) {
  184.             $this->taxes->add($tax);
  185.             $tax->setInvoice($this);
  186.         }
  187.         return $this;
  188.     }
  189.     public function removeTax(Tax $tax): self
  190.     {
  191.         if ($this->taxes->removeElement($tax)) {
  192.             // set the owning side to null (unless already changed)
  193.             if ($tax->getInvoice() === $this) {
  194.                 $tax->setInvoice(null);
  195.             }
  196.         }
  197.         return $this;
  198.     }
  199. }