src/Entity/Finance/BaseQuotation.php line 34

  1. <?php
  2. namespace App\Entity\Finance;
  3. use ApiPlatform\Metadata\ApiProperty;
  4. use App\Entity\Common;
  5. use App\Entity\Common\Observation;
  6. use App\Entity\Expediting\ShippingPlan;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\DBAL\Types\Types;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Symfony\Component\Uid\Ulid;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  14. #[ORM\Table(name'finance_bill')]
  15. #[ORM\Index(columns: ['original_bill_id'], name'finance_bill_original_bill_id_idx')]
  16. #[ORM\Index(columns: ['shipping_plan_id'], name'finance_bill_shipping_plan_id_idx')]
  17. #[ORM\Index(columns: ['payable_by'], name'finance_bill_payable_by_idx')]
  18. #[ORM\Index(columns: ['payable_to'], name'finance_bill_payable_to_idx')]
  19. #[ORM\Index(columns: ['created_by'], name'finance_bill_created_by_idx')]
  20. #[ORM\Index(columns: ['updated_by'], name'finance_bill_updated_by_idx')]
  21. #[ORM\UniqueConstraint(name'finance_bill_number_key'columns: ['number'])]
  22. #[ORM\UniqueConstraint(name'finance_bill_external_reference_key'columns: ['external_reference'])]
  23. #[ORM\UniqueConstraint(name'finance_bill_identifier_key'columns: ['identifier'])]
  24. #[ORM\InheritanceType('SINGLE_TABLE')]
  25. #[ORM\DiscriminatorColumn(name'type')]
  26. #[ORM\DiscriminatorMap([
  27.     'BILL' => Bill::class,
  28.     'QUOTATION' => Quotation::class,
  29. ])]
  30. #[ORM\Entity]
  31. Abstract class BaseQuotation
  32. {
  33.     use Quoteable;
  34.     use Common\Blameable;
  35.     use Common\Trackable;
  36.     #[ORM\Id]
  37.     #[ORM\GeneratedValue(strategy'IDENTITY')]
  38.     #[ORM\SequenceGenerator(sequenceName'finance_bill_id_seq')]
  39.     #[ORM\Column(typeTypes::INTEGER)]
  40.     #[ApiProperty(identifierfalse)]
  41.     protected ?int $id null;
  42.     #[Assert\Ulid]
  43.     #[Assert\NotNull]
  44.     #[ORM\Column(type'ulid'uniquetrue)]
  45.     #[ApiProperty(identifiertrue)]
  46.     protected ?Ulid $identifier null;
  47.     #[Assert\Type(typeShippingPlan::class)]
  48.     #[ORM\ManyToOne(targetEntityShippingPlan::class, inversedBy'quotations')]
  49.     #[ORM\JoinColumn(name'shipping_plan_id'nullablefalse)]
  50.     #[ApiProperty(readableLinktrue)]
  51.     protected ?ShippingPlan $shippingPlan null;
  52.     #[Assert\Type(typeBaseQuotation::class)]
  53.     #[ORM\ManyToOne(targetEntityBaseQuotation::class, inversedBy'editions')]
  54.     #[ORM\JoinColumn(name'original_bill_id')]
  55.     protected ?BaseQuotation $originalForm null;
  56.     #[ORM\OneToMany(mappedBy'originalForm'targetEntityBaseQuotation::class)]
  57.     protected Collection $editions;
  58.     /** @var Collection<int, Expense> */
  59.     #[ORM\OneToMany(mappedBy'form'targetEntityExpense::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  60.     protected Collection $expenses;
  61.     /** @var Collection<int, Tax> */
  62.     #[ORM\OneToMany(mappedBy'quotation'targetEntityTax::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  63.     protected Collection $taxes;
  64.     /** @var Collection<int, Observation> */
  65.     #[ORM\OneToMany(mappedBy'quotation'targetEntityObservation::class, cascade: ['persist''remove'])]
  66.     protected Collection $observations;
  67.     public function __construct()
  68.     {
  69.         $this->identifier = new Ulid();
  70.         $this->expenses = new ArrayCollection();
  71.         $this->taxes = new ArrayCollection();
  72.         $this->observations = new ArrayCollection();
  73.         $this->editions = new ArrayCollection();
  74.     }
  75.     #[Assert\Callback]
  76.     public function validate(ExecutionContextInterface $context$payload): void
  77.     {
  78.         if (empty($this->workOrder) && empty($this->shipment)) {
  79.             $context->buildViolation('Quotation must be associated only with either, a work order, or a shipment')
  80.                 ->atPath('workOrder')
  81.                 ->addViolation();
  82.             $context->buildViolation('Quotation must be associated only with either, a work order, or a shipment')
  83.                 ->atPath('shipment')
  84.                 ->addViolation();
  85.         }
  86.     }
  87.     public function getId(): ?int
  88.     {
  89.         return $this->id;
  90.     }
  91.     public function getIdentifier(): ?Ulid
  92.     {
  93.         return $this->identifier;
  94.     }
  95.     public function setIdentifier($identifier): self
  96.     {
  97.         $this->identifier $identifier;
  98.         return $this;
  99.     }
  100.     public function getShippingPlan(): ?ShippingPlan
  101.     {
  102.         return $this->shippingPlan;
  103.     }
  104.     public function setShippingPlan(?ShippingPlan $shippingPlan): self
  105.     {
  106.         $this->shippingPlan $shippingPlan;
  107.         return $this;
  108.     }
  109.     public function getOriginalForm(): ?self
  110.     {
  111.         return $this->originalForm;
  112.     }
  113.     public function setOriginalForm(?self $originalForm): self
  114.     {
  115.         $form $originalForm;
  116.         while ($form?->getOriginalForm() instanceof BaseQuotation) {
  117.             $form $form->getOriginalForm();
  118.         }
  119.         $this->originalForm $form;
  120.         return $this;
  121.     }
  122.     /**
  123.      * @return Collection<int, BaseQuotation>
  124.      */
  125.     public function getEditions(): Collection
  126.     {
  127.         return $this->editions;
  128.     }
  129.     public function addEdition(BaseQuotation $edition): self
  130.     {
  131.         if (!$this->editions->contains($edition)) {
  132.             $this->editions->add($edition);
  133.             $edition->setOriginalForm($this);
  134.         }
  135.         return $this;
  136.     }
  137.     public function removeEdition(BaseQuotation $edition): self
  138.     {
  139.         if ($this->editions->removeElement($edition)) {
  140.             // set the owning side to null (unless already changed)
  141.             if ($edition->getOriginalForm() === $this) {
  142.                 $edition->setOriginalForm(null);
  143.             }
  144.         }
  145.         return $this;
  146.     }
  147.     /**
  148.      * @return Collection<int, Expense>
  149.      */
  150.     public function getExpenses(): Collection
  151.     {
  152.         return $this->expenses;
  153.     }
  154.     public function addExpense(Expense $expense): self
  155.     {
  156.         if (!$this->expenses->contains($expense)) {
  157.             $this->expenses->add($expense);
  158.             $expense->setForm($this);
  159.         }
  160.         return $this;
  161.     }
  162.     public function removeExpense(Expense $expense): self
  163.     {
  164.         if ($this->expenses->removeElement($expense)) {
  165.             // set the owning side to null (unless already changed)
  166.             if ($expense->getForm() === $this) {
  167.                 $expense->setForm(null);
  168.             }
  169.         }
  170.         return $this;
  171.     }
  172.     /**
  173.      * @return Collection<int, Tax>
  174.      */
  175.     public function getTaxes(): Collection
  176.     {
  177.         return $this->taxes;
  178.     }
  179.     public function addTax(Tax $tax): self
  180.     {
  181.         if (!$this->taxes->contains($tax)) {
  182.             $this->taxes->add($tax);
  183.             $tax->setQuotation($this);
  184.         }
  185.         return $this;
  186.     }
  187.     public function removeTax(Tax $tax): self
  188.     {
  189.         if ($this->taxes->removeElement($tax)) {
  190.             // set the owning side to null (unless already changed)
  191.             if ($tax->getQuotation() === $this) {
  192.                 $tax->setQuotation(null);
  193.             }
  194.         }
  195.         return $this;
  196.     }
  197.     public function isFilled(): bool
  198.     {
  199.         return ($this->expenses->count() > 0);
  200.     }
  201.     /**
  202.      * @return Collection<int, Observation>
  203.      */
  204.     public function getObservations(): Collection
  205.     {
  206.         return $this->observations;
  207.     }
  208.     public function addObservation(Observation $observation): self
  209.     {
  210.         if (!$this->observations->contains($observation)) {
  211.             $this->observations->add($observation);
  212.             $observation->setQuotation($this);
  213.         }
  214.         return $this;
  215.     }
  216.     public function removeObservation(Observation $observation): self
  217.     {
  218.         if ($this->observations->removeElement($observation)) {
  219.             // set the owning side to null (unless already changed)
  220.             if ($observation->getQuotation() === $this) {
  221.                 $observation->setQuotation(null);
  222.             }
  223.         }
  224.         return $this;
  225.     }
  226. }