src/Entity/Order.php line 75

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\Link;
  4. use App\Controller\Api\Order\ApiEstablishmentOrderInvoiceController;
  5. use App\Controller\Api\Order\ApiOrderShipmentTimerController;
  6. use App\Dto\CreateOrderInput;
  7. use App\Kimia;
  8. use ApiPlatform\Metadata\ApiResource;
  9. use ApiPlatform\Metadata\Get;
  10. use ApiPlatform\Metadata\GetCollection;
  11. use ApiPlatform\Metadata\Post;
  12. use App\Controller\Api\Order\ApiEstablishmentOrderNewController;
  13. use App\Controller\Api\Order\ApiOrderShipmentOverController;
  14. use App\Controller\Api\Order\ApiOrderShipmentStartController;
  15. use App\Repository\OrderRepository;
  16. use App\State\Order\OrderCreateProcessor;
  17. use App\Trait\ArrayableTrait;
  18. use App\Trait\CurrentStateTrait;
  19. use App\Trait\DateTrait;
  20. use DateTimeImmutable;
  21. use Doctrine\Common\Collections\ArrayCollection;
  22. use Doctrine\Common\Collections\Collection;
  23. use Doctrine\ORM\Mapping as ORM;
  24. use Symfony\Component\Serializer\Annotation\Groups;
  25. #[ORM\Entity(repositoryClassOrderRepository::class)]
  26. #[ORM\Table(name'`order`')]
  27. #[ApiResource(
  28.     operations: [
  29.         new GetCollection(
  30.             normalizationContext: ['groups' => ['order:list']]
  31.         ),
  32.         new GetCollection(
  33.             uriTemplate"/departments/{departmentId}/orders",
  34.             uriVariables: [
  35.                 'departmentId' => new Link(toProperty'department'fromClassDepartment::class),
  36.             ],
  37.             normalizationContext: ['groups' => ['order:list']]
  38.         ),
  39.         new GetCollection(
  40.             uriTemplate"/employees/{waiterId}/orders",
  41.             uriVariables: [
  42.                 'waiterId' => new Link(toProperty'waiter'fromClassEmployee::class),
  43.             ],
  44.             normalizationContext: ['groups' => ['order:list']]
  45.         ),
  46.         new Get(),
  47.         new Get(
  48.             uriTemplate"/orders/{id}/invoice",
  49.             controllerApiEstablishmentOrderInvoiceController::class,
  50.         ),
  51.         new Get(
  52.             uriTemplate"/orders/{id}/timer",
  53.             controllerApiOrderShipmentTimerController::class,
  54.             normalizationContext: ['groups' => ['order:timer']]
  55.         ),
  56.         new Post(
  57.             inputCreateOrderInput::class,
  58.             processorOrderCreateProcessor::class
  59.         ),
  60.         new Post(
  61.             uriTemplate"/orders/{id}/start",
  62.             controllerApiOrderShipmentStartController::class,
  63.         ),
  64.         new Post(
  65.             uriTemplate"/orders/{id}/over",
  66.             controllerApiOrderShipmentOverController::class,
  67.         ),
  68.     ],
  69.     normalizationContext: ['groups' => ['order:list''order:view']],
  70.     paginationClientEnabledtrue
  71. )]
  72. class Order
  73. {
  74.     use ArrayableTrait;
  75.     use DateTrait;
  76.     use CurrentStateTrait;
  77.     #[ORM\Id]
  78.     #[ORM\GeneratedValue]
  79.     #[ORM\Column]
  80.     #[Groups(['order:list'])]
  81.     private ?int $id null;
  82.     #[ORM\Column(length255)]
  83.     #[Groups(['order:list'])]
  84.     private ?string $reference null;
  85.     #[ORM\Column(length255nullabletrue)]
  86.     #[Groups(['order:list'])]
  87.     private ?string $name null;
  88.     #[ORM\Column(length255nullabletrue)]
  89.     #[Groups(['order:list'])]
  90.     private ?string $phone null;
  91.     #[ORM\Column]
  92.     #[Groups(['order:list'])]
  93.     private ?float $amount null;
  94.     #[ORM\Column(nullabletrue)]
  95.     #[Groups(['order:list'])]
  96.     private ?float $payout null;
  97.     #[ORM\Column(length255)]
  98.     #[Groups(['order:list'])]
  99.     private ?string $method null;
  100.     #[ORM\Column]
  101.     #[Groups(['order:list'])]
  102.     private ?int $status null;
  103.     #[ORM\OneToMany(mappedBy'command'targetEntityOrderItems::class, cascade: ["persist"], orphanRemovaltrue)]
  104.     #[Groups(['order:list'])]
  105.     private Collection $orderItems;
  106.     #[ORM\ManyToOne(inversedBy'orders')]
  107.     #[ORM\JoinColumn(nullabletrue)]
  108.     #[Groups(['order:list'])]
  109.     private ?Department $department null;
  110.     #[ORM\OneToMany(mappedBy'command'targetEntityInvoice::class, cascade: ["persist"], orphanRemovaltrue)]
  111.     private Collection $invoice;
  112.     #[ORM\ManyToOne]
  113.     #[ORM\JoinColumn(nullablefalse)]
  114.     #[Groups(['order:list'])]
  115.     private ?Currency $currency null;
  116.     #[ORM\ManyToOne]
  117.     #[ORM\JoinColumn(nullablefalse)]
  118.     private ?User $author null;
  119.     #[ORM\ManyToOne(inversedBy'orders')]
  120.     #[Groups(['order:view'])]
  121.     private ?Employee $waiter null;
  122.     #[ORM\Column]
  123.     #[Groups(['order:view'])]
  124.     private ?bool $isDeliverable;
  125.     #[ORM\ManyToOne]
  126.     #[ORM\JoinColumn(nullablefalse)]
  127.     #[Groups(['order:view'])]
  128.     private ?Rate $rate null;
  129.     #[ORM\OneToMany(mappedBy'command'targetEntityShipment::class, orphanRemovaltrue)]
  130.     #[Groups(['order:list''order:view'])]
  131.     private Collection $shipment;
  132.     #[ORM\ManyToOne(inversedBy'orders')]
  133.     #[ORM\JoinColumn(nullablefalse)]
  134.     private ?Establishment $establishment null;
  135.     #[ORM\Column(options: ['default' => false])]
  136.     private ?bool $isProforma false;
  137.     #[ORM\OneToOne(mappedBy'orderCommand'cascade: ['persist''remove'])]
  138.     private ?Transaction $transaction null;
  139.     #[ORM\ManyToMany(targetEntityClient::class, inversedBy'orders')]
  140.     #[Groups(['order:list'])]
  141.     private Collection $clients;
  142.     #[ORM\Column(options: ['default' => 1])]
  143.     private ?int $clientPrincipalI 1;
  144.     public function __construct()
  145.     {
  146.         $this->currentState 'new';
  147.         $this->isDeliverable 0;
  148.         if (is_null($this->reference)) $this->reference strtoupper(uniqid('OD-'));
  149.         if (is_null($this->createdAt)) $this->createdAt = new DateTimeImmutable();
  150.         $this->updatedAt = new DateTimeImmutable();
  151.         $this->orderItems = new ArrayCollection();
  152.         $this->invoice = new ArrayCollection();
  153.         $this->shipment = new ArrayCollection();
  154.         $this->status Kimia::STATUS_INIT;
  155.         $this->clients = new ArrayCollection();
  156.     }
  157.     public function getId(): ?int
  158.     {
  159.         return $this->id;
  160.     }
  161.     public function getReference(): ?string
  162.     {
  163.         return $this->reference;
  164.     }
  165.     public function setReference(string $reference): self
  166.     {
  167.         $this->reference $reference;
  168.         return $this;
  169.     }
  170.     public function getName(): ?string
  171.     {
  172.         return $this->name;
  173.     }
  174.     public function setName(?string $name): self
  175.     {
  176.         $this->name $name;
  177.         return $this;
  178.     }
  179.     public function getPhone(): ?string
  180.     {
  181.         return $this->phone;
  182.     }
  183.     public function setPhone(?string $phone): self
  184.     {
  185.         $this->phone $phone;
  186.         return $this;
  187.     }
  188.     public function getAmount(): ?float
  189.     {
  190.         return $this->amount;
  191.     }
  192.     public function getAmountRest(): ?float
  193.     {
  194.         return $this->amount $this->getPayout();
  195.     }
  196.     public function setAmount(float $amount): self
  197.     {
  198.         $this->amount $amount;
  199.         return $this;
  200.     }
  201.     public function getPayout(): ?float
  202.     {
  203.         //return count($this->invoice) != 0 ? $this->amount - $this->getAmountRest() : $this->payout;
  204.         return $this->payout;
  205.     }
  206.     public function setPayout(?float $payout): self
  207.     {
  208.         $this->payout $payout;
  209.         return $this;
  210.     }
  211.     public function getMethod(): ?string
  212.     {
  213.         return $this->method;
  214.     }
  215.     public function setMethod(string $method): self
  216.     {
  217.         $this->method $method;
  218.         return $this;
  219.     }
  220.     public function getStatus(): ?int
  221.     {
  222.         return $this->status;
  223.     }
  224.     public static function getStatusListForView(): array
  225.     {
  226.         return [
  227.             Kimia::STATUS_INIT => "<span class='badge badge-soft-info'>Initié</span>",
  228.             Kimia::STATUS_ACCEPTED => "<span class='badge badge-soft-info'>Accepté</span>",
  229.             Kimia::STATUS_SUCCESS => "<span class='badge badge-soft-success'>Succès</span>",
  230.             Kimia::STATUS_PROCESS => "<span class='badge badge-soft-warning'>En cours</span>",
  231.             Kimia::STATUS_ERROR => "<span class='badge badge-soft-danger'>Erreur</span>",
  232.             Kimia::STATUS_CANCEL => "<span class='badge badge-soft-secondary'>Annulé</span>",
  233.             Kimia::STATUS_FAILED => "<span class='badge badge-soft-secondary'>Échoué</span>",
  234.             Kimia::STATUS_PROFORMA => "<span class='badge badge-soft-warning'>Proforma</span>",
  235.         ];
  236.     }
  237.     public function setStatus(int $status): self
  238.     {
  239.         $this->status $status;
  240.         return $this;
  241.     }
  242.     /**
  243.      * @return Collection<int, OrderItems>
  244.      */
  245.     public function getOrderItems(): Collection
  246.     {
  247.         return $this->orderItems;
  248.     }
  249.     public function addOrderItem(OrderItems $orderItem): self
  250.     {
  251.         if (!$this->orderItems->contains($orderItem)) {
  252.             $this->orderItems->add($orderItem);
  253.             $orderItem->setCommand($this);
  254.         }
  255.         return $this;
  256.     }
  257.     public function removeOrderItem(OrderItems $orderItem): self
  258.     {
  259.         if ($this->orderItems->removeElement($orderItem)) {
  260.             // set the owning side to null (unless already changed)
  261.             if ($orderItem->getCommand() === $this) {
  262.                 $orderItem->setCommand(null);
  263.             }
  264.         }
  265.         return $this;
  266.     }
  267.     public function getDepartment(): ?Department
  268.     {
  269.         return $this->department;
  270.     }
  271.     public function setDepartment(?Department $department): self
  272.     {
  273.         $this->department $department;
  274.         return $this;
  275.     }
  276.     /**
  277.      * @return Collection<int, Invoice>
  278.      */
  279.     public function getInvoice(): Collection
  280.     {
  281.         return $this->invoice;
  282.     }
  283.     public function addInvoice(Invoice $invoice): self
  284.     {
  285.         if (!$this->invoice->contains($invoice)) {
  286.             $this->invoice->add($invoice);
  287.             $invoice->setCommand($this);
  288.         }
  289.         return $this;
  290.     }
  291.     public function removeInvoice(Invoice $invoice): self
  292.     {
  293.         if ($this->invoice->removeElement($invoice)) {
  294.             // set the owning side to null (unless already changed)
  295.             if ($invoice->getCommand() === $this) {
  296.                 $invoice->setCommand(null);
  297.             }
  298.         }
  299.         return $this;
  300.     }
  301.     public function getCurrency(): ?Currency
  302.     {
  303.         return $this->currency;
  304.     }
  305.     public function setCurrency(?Currency $currency): self
  306.     {
  307.         $this->currency $currency;
  308.         return $this;
  309.     }
  310.     public function getAuthor(): ?User
  311.     {
  312.         return $this->author;
  313.     }
  314.     public function setAuthor(?User $author): self
  315.     {
  316.         $this->author $author;
  317.         return $this;
  318.     }
  319.     public function getWaiter(): ?Employee
  320.     {
  321.         return $this->waiter;
  322.     }
  323.     public function setWaiter(?Employee $waiter): self
  324.     {
  325.         $this->waiter $waiter;
  326.         return $this;
  327.     }
  328.     public function isIsDeliverable(): ?bool
  329.     {
  330.         return $this->isDeliverable;
  331.     }
  332.     public function setIsDeliverable(bool $isDeliverable): self
  333.     {
  334.         $this->isDeliverable $isDeliverable;
  335.         return $this;
  336.     }
  337.     public function getIsClosed(): bool
  338.     {
  339.         return $this->payout >= $this->getAmount();
  340.     }
  341.     public function getRate(): ?Rate
  342.     {
  343.         return $this->rate;
  344.     }
  345.     public function setRate(?Rate $rate): self
  346.     {
  347.         $this->rate $rate;
  348.         return $this;
  349.     }
  350.     /**
  351.      * @return Collection<int, Shipment>
  352.      */
  353.     public function getShipment(): Collection
  354.     {
  355.         return $this->shipment;
  356.     }
  357.     #[Groups(['order:list'])]
  358.     public function getCurrentShipment(): ?Shipment
  359.     {
  360.         $shipments $this->getShipment()->map(function (Shipment $shipment) {
  361.             if ($shipment->getCurrentState() == "notShipped" || $shipment->getCurrentState() == Kimia::STATUS_INIT) {
  362.                 return $shipment;
  363.             }
  364.             return null;
  365.         });
  366.         return $shipments->first() ?: null;
  367.     }
  368.     #[Groups(['order:list'])]
  369.     public function getCurrentDestination(): ?string
  370.     {
  371.         return $this->getCurrentShipment()?->getDestination() ?: null;
  372.     }
  373.     public function addShipment(Shipment $shipment): self
  374.     {
  375.         if (!$this->shipment->contains($shipment)) {
  376.             $this->shipment->add($shipment);
  377.             $shipment->setCommand($this);
  378.         }
  379.         return $this;
  380.     }
  381.     public function removeShipment(Shipment $shipment): self
  382.     {
  383.         if ($this->shipment->removeElement($shipment)) {
  384.             // set the owning side to null (unless already changed)
  385.             if ($shipment->getCommand() === $this) {
  386.                 $shipment->setCommand(null);
  387.             }
  388.         }
  389.         return $this;
  390.     }
  391.     public function getEstablishment(): ?Establishment
  392.     {
  393.         return $this->establishment;
  394.     }
  395.     public function setEstablishment(?Establishment $establishment): self
  396.     {
  397.         $this->establishment $establishment;
  398.         return $this;
  399.     }
  400.     public function isIsProforma(): ?bool
  401.     {
  402.         return $this->isProforma;
  403.     }
  404.     public function setIsProforma(bool $isProforma): static
  405.     {
  406.         $this->isProforma $isProforma;
  407.         return $this;
  408.     }
  409.     public function getTransaction(): ?Transaction
  410.     {
  411.         return $this->transaction;
  412.     }
  413.     public function setTransaction(?Transaction $transaction): static
  414.     {
  415.         // unset the owning side of the relation if necessary
  416.         if ($transaction === null && $this->transaction !== null) {
  417.             $this->transaction->setOrderCommand(null);
  418.         }
  419.         // set the owning side of the relation if necessary
  420.         if ($transaction !== null && $transaction->getOrderCommand() !== $this) {
  421.             $transaction->setOrderCommand($this);
  422.         }
  423.         $this->transaction $transaction;
  424.         return $this;
  425.     }
  426.     public function __toString(): string
  427.     {
  428.         return $this->reference;
  429.     }
  430.     /**
  431.      * @return Collection<int, Client>
  432.      */
  433.     public function getClients(): Collection
  434.     {
  435.         return $this->clients;
  436.     }
  437.     public function addClient(Client $client): static
  438.     {
  439.         if (!$this->clients->contains($client)) {
  440.             $this->clients->add($client);
  441.         }
  442.         return $this;
  443.     }
  444.     public function removeClient(Client $client): static
  445.     {
  446.         $this->clients->removeElement($client);
  447.         return $this;
  448.     }
  449.     public function getClientPrincipalI(): ?int
  450.     {
  451.         return $this->clientPrincipalI;
  452.     }
  453.     public function getClientPrincipalInt(): ?int
  454.     {
  455.         return $this->clientPrincipalI 1;
  456.     }
  457.     public function setClientPrincipalI(int $clientPrincipalI): static
  458.     {
  459.         $this->clientPrincipalI $clientPrincipalI;
  460.         return $this;
  461.     }
  462. }