src/Entity/Invoice.php line 41

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use ApiPlatform\Metadata\GetCollection;
  5. use ApiPlatform\Metadata\Link;
  6. use App\Kimia;
  7. use App\Repository\InvoiceRepository;
  8. use App\Trait\CurrentStateTrait;
  9. use App\Trait\DateTrait;
  10. use App\Trait\ArrayableTrait;
  11. use DateTimeImmutable;
  12. use DateTimeInterface;
  13. use Doctrine\DBAL\Types\Types;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. #[ORM\Entity(repositoryClassInvoiceRepository::class)]
  17. #[ApiResource(
  18.     operations: [
  19.         new GetCollection(
  20.             normalizationContext: ['groups' => ['invoice:list']]
  21.         ),
  22.         new GetCollection(
  23.             uriTemplate"/orders/{orderId}/invoices",
  24.             uriVariables: [
  25.                 'orderId' => new Link(toProperty'command'fromClassOrder::class),
  26.             ],
  27.             normalizationContext: ['groups' => ['invoice:list']]
  28.         ),
  29.         new GetCollection(
  30.             uriTemplate"/clients/{clientId}/invoices",
  31.             uriVariables: [
  32.                 'clientId' => new Link(toProperty'client'fromClassClient::class),
  33.             ],
  34.             normalizationContext: ['groups' => ['invoice:list']]
  35.         ),
  36.     ]
  37. )]
  38. class Invoice
  39. {
  40.     use DateTrait;
  41.     use CurrentStateTrait;
  42.     use ArrayableTrait;
  43.     #[ORM\Id]
  44.     #[ORM\GeneratedValue]
  45.     #[ORM\Column]
  46.     #[Groups(['invoice:list''client:invoices'])]
  47.     private ?int $id null;
  48.     #[ORM\Column(length255)]
  49.     #[Groups(['invoice:list''client:invoices'])]
  50.     private ?string $reference null;
  51.     #[ORM\Column]
  52.     #[Groups(['invoice:list''client:invoices'])]
  53.     private ?float $amount null;
  54.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  55.     #[Groups(['invoice:list''client:invoices'])]
  56.     private ?DateTimeInterface $sentAt null;
  57.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  58.     #[Groups(['invoice:list''client:invoices'])]
  59.     private ?DateTimeInterface $paidAt null;
  60.     #[ORM\ManyToOne(cascade: ['persist'], inversedBy'invoice')]
  61.     #[ORM\JoinColumn(nullabletrue)]
  62.     private ?Order $command null;
  63.     #[ORM\ManyToOne]
  64.     #[ORM\JoinColumn(nullablefalse)]
  65.     #[Groups(['invoice:list''client:invoices'])]
  66.     private ?Currency $currency null;
  67.     #[ORM\ManyToOne]
  68.     #[ORM\JoinColumn(nullabletrue)]
  69.     #[Groups(['invoice:list''client:invoices'])]
  70.     private ?Rate $rate null;
  71.     #[ORM\Column(length255)]
  72.     private ?string $establishment null;
  73.     #[ORM\Column(length255)]
  74.     #[Groups(['invoice:list''client:invoices'])]
  75.     private ?string $type null;
  76.     #[ORM\Column(typeTypes::SMALLINToptions: ['default' => Kimia::FORMAT_A6])]
  77.     private ?int $format Kimia::FORMAT_A6;
  78.     #[ORM\ManyToOne(inversedBy'invoices')]
  79.     private ?Client $client null;
  80.     public function __construct()
  81.     {
  82.         $this->currentState 'notCreated';
  83.         if (is_null($this->reference)) $this->reference strtoupper(uniqid('FA-'false));
  84.         if (is_null($this->createdAt)) $this->createdAt = new DateTimeImmutable();
  85.         $this->updatedAt = new DateTimeImmutable();
  86.     }
  87.     public function getId(): ?int
  88.     {
  89.         return $this->id;
  90.     }
  91.     public function getReference(): ?string
  92.     {
  93.         return $this->reference;
  94.     }
  95.     public function setReference(string $reference): self
  96.     {
  97.         $this->reference $reference;
  98.         return $this;
  99.     }
  100.     public function getAmount(): ?float
  101.     {
  102.         return $this->amount;
  103.     }
  104.     public function getAmountWithCurrency(): ?string
  105.     {
  106.         return "$this->amount{$this->getCurrency()->getSymbol()}";
  107.     }
  108.     public function setAmount(float $amount): self
  109.     {
  110.         $this->amount $amount;
  111.         return $this;
  112.     }
  113.     public function getSentAt(): ?DateTimeInterface
  114.     {
  115.         return $this->sentAt;
  116.     }
  117.     public function setSentAt(?DateTimeInterface $sentAt): self
  118.     {
  119.         $this->sentAt $sentAt;
  120.         return $this;
  121.     }
  122.     public function getPaidAt(): ?DateTimeInterface
  123.     {
  124.         return $this->paidAt;
  125.     }
  126.     public function setPaidAt(?DateTimeInterface $paidAt): self
  127.     {
  128.         $this->paidAt $paidAt;
  129.         return $this;
  130.     }
  131.     public function getCommand(): ?Order
  132.     {
  133.         return $this->command;
  134.     }
  135.     public function setCommand(?Order $command): self
  136.     {
  137.         $this->command $command;
  138.         return $this;
  139.     }
  140.     public function getCurrency(): ?Currency
  141.     {
  142.         return $this->currency;
  143.     }
  144.     public function setCurrency(?Currency $currency): self
  145.     {
  146.         $this->currency $currency;
  147.         return $this;
  148.     }
  149.     public function getRate(): ?Rate
  150.     {
  151.         return $this->rate;
  152.     }
  153.     public function setRate(?Rate $rate): self
  154.     {
  155.         $this->rate $rate;
  156.         return $this;
  157.     }
  158.     public function getEstablishment(): ?string
  159.     {
  160.         return $this->establishment;
  161.     }
  162.     public function setEstablishment(string $establishment): static
  163.     {
  164.         $this->establishment $establishment;
  165.         return $this;
  166.     }
  167.     public function getType(): ?string
  168.     {
  169.         return $this->type;
  170.     }
  171.     public static function getTypes(): array
  172.     {
  173.         return [
  174.             'Simple' => Kimia::INVOICE_SIMPLE,
  175.             'Proforma' => Kimia::INVOICE_PROFORMA,
  176.         ];
  177.     }
  178.     public static function getTypesListForView(): array
  179.     {
  180.         return [
  181.             Kimia::INVOICE_SIMPLE => "<span class='badge badge-soft-secondary'>Simple</span>",
  182.             Kimia::INVOICE_PROFORMA => "<span class='badge badge-soft-secondary'>Proforma</span>",
  183.         ];
  184.     }
  185.     public function getTypeForView(): string
  186.     {
  187.         return self::getTypesListForView()[$this->getType()];
  188.     }
  189.     public function setType(string $type): static
  190.     {
  191.         $this->type $type;
  192.         return $this;
  193.     }
  194.     public function getFormat(): ?int
  195.     {
  196.         return $this->format;
  197.     }
  198.     public static function getFormats(): array
  199.     {
  200.         return [
  201.             'Format A6' => Kimia::FORMAT_A6,
  202.             'Format A4' => Kimia::FORMAT_A4,
  203.         ];
  204.     }
  205.     public static function getFormatsListForView(): array
  206.     {
  207.         return [
  208.             Kimia::FORMAT_A6 => "<span class='badge badge-soft-secondary'>A6</span>",
  209.             Kimia::FORMAT_A4 => "<span class='badge badge-soft-secondary'>A4</span>",
  210.         ];
  211.     }
  212.     public function getFormatForView(): string
  213.     {
  214.         return self::getFormatsListForView()[$this->getFormat()];
  215.     }
  216.     public function setFormat(int $format): static
  217.     {
  218.         $this->format $format;
  219.         return $this;
  220.     }
  221.     public function __toString(): string
  222.     {
  223.         return $this->reference;
  224.     }
  225.     public function getClient(): ?Client
  226.     {
  227.         return $this->client;
  228.     }
  229.     public function setClient(?Client $client): static
  230.     {
  231.         $this->client $client;
  232.         return $this;
  233.     }
  234. }