src/Entity/Finance/Expense.php line 28

  1. <?php
  2. namespace App\Entity\Finance;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use ApiPlatform\Metadata\GetCollection;
  5. use ApiPlatform\Metadata\Link;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  9. #[ORM\Entity]
  10. #[ApiResource(routePrefix'/finance')]
  11. #[ApiResource(
  12.     uriTemplate'/finance/quotations/{identifier}/expenses',
  13.     operations: [new GetCollection()],
  14.     uriVariables: [
  15.         'identifier' => new Link(fromProperty'expenses'fromClassQuotation::class),
  16.     ],
  17. )]
  18. #[ApiResource(
  19.     uriTemplate'/finance/bills/{identifier}/expenses',
  20.     operations: [new GetCollection()],
  21.     uriVariables: [
  22.         'identifier' => new Link(fromProperty'expenses'fromClassBill::class),
  23.     ],
  24. )]
  25. class Expense extends Chargeable
  26. {
  27.     #[Assert\NotNull]
  28.     #[Assert\Type(typeBaseQuotation::class)]
  29.     #[ORM\ManyToOne(targetEntityBaseQuotation::class, inversedBy'expenses')]
  30.     #[ORM\JoinColumn(name'bill_id')]
  31.     private ?BaseQuotation $form null;
  32.     #[Assert\Callback]
  33.     public function validate(ExecutionContextInterface $context$payload): void
  34.     {
  35.         if (empty($this->form)) {
  36.             $context->buildViolation('An expense must be associated only with either, a quotation, or a bill')
  37.                 ->atPath('form')
  38.                 ->addViolation();
  39.         }
  40.     }
  41.     public function getForm(): ?BaseQuotation
  42.     {
  43.         return $this->form;
  44.     }
  45.     public function setForm(?BaseQuotation $form): self
  46.     {
  47.         $this->form $form;
  48.         return $this;
  49.     }
  50. }