src/Entity/HtMenu.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\HtMenuRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Serializer\Annotation\Groups;
  8. /**
  9.  * @ORM\Entity(repositoryClass=HtMenuRepository::class)
  10.  * @ORM\HasLifecycleCallbacks()
  11.  */
  12. class HtMenu
  13. {
  14.     /**
  15.      * @ORM\Id
  16.      * @ORM\GeneratedValue
  17.      * @ORM\Column(type="integer")
  18.      * @Groups("htmenu:read")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @ORM\ManyToMany(targetEntity=Escandallo::class, inversedBy="htMenus")
  23.      */
  24.     private $escandallo;
  25.     /**
  26.      * @ORM\Column(type="float")
  27.      */
  28.     private $beneficio;
  29.     /**
  30.      * @ORM\Column(type="float")
  31.      */
  32.     private $totalPrice;
  33.     /**
  34.      * @ORM\Column(type="datetime_immutable")
  35.      */
  36.     private $createdAt;
  37.     /**
  38.      * @ORM\Column(type="datetime_immutable")
  39.      */
  40.     private $updatedAt;
  41.     /**
  42.      * @ORM\ManyToOne(targetEntity=User::class)
  43.      * @ORM\JoinColumn(nullable=false)
  44.      */
  45.     private $createdId;
  46.     /**
  47.      * @ORM\ManyToOne(targetEntity=User::class)
  48.      * @ORM\JoinColumn(nullable=false)
  49.      */
  50.     private $updatedId;
  51.     /**
  52.      * @ORM\Column(type="boolean")
  53.      */
  54.     private $template;
  55.     /**
  56.      * @ORM\Column(type="string", length=255)
  57.      * @Groups("htmenu:read")
  58.      */
  59.     private $name;
  60.     /**
  61.      * @ORM\ManyToOne(targetEntity=HtItem::class, inversedBy="htMenus", cascade={"persist"})
  62.      */
  63.     private $htItem;
  64.     /**
  65.      * @ORM\Column(type="float")
  66.      */
  67.     private $iva;
  68.     /**
  69.      * @ORM\OneToMany(targetEntity=HtExtra::class, mappedBy="htMenu", cascade={"persist", "remove"})
  70.      */
  71.     private $htExtras;
  72.     /**
  73.      * @ORM\Column(type="json", nullable=true)
  74.      */
  75.     private $escandallosOrder = [];
  76.     public function __construct()
  77.     {
  78.         $this->escandallo = new ArrayCollection();
  79.         $this->htExtras = new ArrayCollection();
  80.     }
  81.     public function getId(): ?int
  82.     {
  83.         return $this->id;
  84.     }
  85.     public function setId(int $id null): self
  86.     {
  87.         $this->id $id;
  88.         return $this;
  89.     }
  90.     /**
  91.      * @return Collection<int, Escandallo>
  92.      */
  93.     public function getEscandallo(): Collection
  94.     {
  95.         return $this->escandallo;
  96.     }
  97.     public function addEscandallo(Escandallo $escandallo): self
  98.     {
  99.         if (!$this->escandallo->contains($escandallo)) {
  100.             $this->escandallo[] = $escandallo;
  101.         }
  102.         return $this;
  103.     }
  104.     public function removeEscandallo(Escandallo $escandallo): self
  105.     {
  106.         $this->escandallo->removeElement($escandallo);
  107.         return $this;
  108.     }
  109.     public function getBeneficio(): ?float
  110.     {
  111.         return $this->beneficio;
  112.     }
  113.     public function setBeneficio(float $beneficio): self
  114.     {
  115.         $this->beneficio $beneficio;
  116.         return $this;
  117.     }
  118.     public function getTotalPrice(): ?float
  119.     {
  120.         return $this->totalPrice;
  121.     }
  122.     public function setTotalPrice(float $totalPrice): self
  123.     {
  124.         $this->totalPrice $totalPrice;
  125.         return $this;
  126.     }
  127.     public function getCreatedAt(): ?\DateTimeImmutable
  128.     {
  129.         return $this->createdAt;
  130.     }
  131.     public function setCreatedAt(\DateTimeImmutable $createdAt): self
  132.     {
  133.         $this->createdAt $createdAt;
  134.         return $this;
  135.     }
  136.     public function getUpdatedAt(): ?\DateTimeImmutable
  137.     {
  138.         return $this->updatedAt;
  139.     }
  140.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
  141.     {
  142.         $this->updatedAt $updatedAt;
  143.         return $this;
  144.     }
  145.     /**
  146.      * @ORM\PrePersist
  147.      */
  148.     public function setCreatedAtValue()
  149.     {
  150.         $this->createdAt = new \DateTimeImmutable();
  151.     }
  152.     /**
  153.      * @ORM\PrePersist
  154.      * @ORM\PreUpdate
  155.      */
  156.     public function setUpdatedAtValue()
  157.     {
  158.         $this->updatedAt = new \DateTimeImmutable();
  159.     }
  160.     public function getCreatedId(): ?User
  161.     {
  162.         return $this->createdId;
  163.     }
  164.     public function setCreatedId(?User $createdId): self
  165.     {
  166.         $this->createdId $createdId;
  167.         return $this;
  168.     }
  169.     public function getUpdatedId(): ?User
  170.     {
  171.         return $this->updatedId;
  172.     }
  173.     public function setUpdatedId(?User $updatedId): self
  174.     {
  175.         $this->updatedId $updatedId;
  176.         return $this;
  177.     }
  178.     public function isTemplate(): ?bool
  179.     {
  180.         return $this->template;
  181.     }
  182.     public function setTemplate(bool $template): self
  183.     {
  184.         $this->template $template;
  185.         return $this;
  186.     }
  187.     public function getName(): ?string
  188.     {
  189.         return $this->name;
  190.     }
  191.     public function setName(string $name): self
  192.     {
  193.         $this->name $name;
  194.         return $this;
  195.     }
  196.     public function getHtItem(): ?HtItem
  197.     {
  198.         return $this->htItem;
  199.     }
  200.     public function setHtItem(?HtItem $htItem): self
  201.     {
  202.         $this->htItem $htItem;
  203.         return $this;
  204.     }
  205.     public function getIva(): ?float
  206.     {
  207.         return $this->iva;
  208.     }
  209.     public function setIva(float $iva): self
  210.     {
  211.         $this->iva $iva;
  212.         return $this;
  213.     }
  214.     /**
  215.      * @return Collection<int, HtExtra>
  216.      */
  217.     public function getHtExtras(): Collection
  218.     {
  219.         return $this->htExtras;
  220.     }
  221.     public function addHtExtra(HtExtra $htExtra): self
  222.     {
  223.         if (!$this->htExtras->contains($htExtra)) {
  224.             $this->htExtras[] = $htExtra;
  225.             $htExtra->setHtMenu($this);
  226.         }
  227.         return $this;
  228.     }
  229.     public function removeHtExtra(HtExtra $htExtra): self
  230.     {
  231.         if ($this->htExtras->removeElement($htExtra)) {
  232.             // set the owning side to null (unless already changed)
  233.             if ($htExtra->getHtMenu() === $this) {
  234.                 $htExtra->setHtMenu(null);
  235.             }
  236.         }
  237.         return $this;
  238.     }
  239.     public function getEscandallosOrder(): ?array
  240.     {
  241.         return $this->escandallosOrder;
  242.     }
  243.     public function setEscandallosOrder(?array $escandallosOrder): self
  244.     {
  245.         $this->escandallosOrder $escandallosOrder;
  246.         return $this;
  247.     }
  248.     /**
  249.      * Devuelve los escandallos ordenados según el array de IDs en escandallosOrder.
  250.      * Si no hay orden guardado, los devuelve tal y como los sacó Doctrine.
  251.      * Si hay escandallos nuevos que no están en el array de orden, los pone al final.
  252.      *
  253.      * @return array
  254.      */
  255.     public function getEscandallosOrdered(): array
  256.     {
  257.         $escandallos $this->escandallo->toArray();
  258.         $order $this->escandallosOrder ?? [];
  259.         if (empty($order)) {
  260.             return $escandallos;
  261.         }
  262.         $orderedEscandallos = [];
  263.         // Almacenamos los escandallos indexados por ID para un acceso fácil
  264.         $escandallosById = [];
  265.         foreach ($escandallos as $escandallo) {
  266.             $escandallosById[$escandallo->getId()] = $escandallo;
  267.         }
  268.         // Añadimos en el orden dictado por el array guardado
  269.         foreach ($order as $id) {
  270.             if (isset($escandallosById[$id])) {
  271.                 $orderedEscandallos[] = $escandallosById[$id];
  272.                 unset($escandallosById[$id]);
  273.             }
  274.         }
  275.         // Cualquiera que sobre (agregado nuevo y no ordenado aún) va al final
  276.         foreach ($escandallosById as $escandallo) {
  277.             $orderedEscandallos[] = $escandallo;
  278.         }
  279.         return $orderedEscandallos;
  280.     }
  281. }