src/Entity/Department/Department.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Department;
  3. use App\Entity\Docs\Finance\Receipt;
  4. use App\Entity\Docs\Finance\Requisition;
  5. use App\Entity\Planning\Action;
  6. use App\Entity\User;
  7. use App\Entity\Company\Company;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use App\Entity\Department\Section;
  10. use App\Entity\Docs\Finance\Budget;
  11. use App\Entity\Company\Presentation;
  12. use App\Entity\Utils\TimestampTrait;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  18. use ApiPlatform\Metadata\Get;
  19. use ApiPlatform\Metadata\ApiResource;
  20. use ApiPlatform\Metadata\GetCollection;
  21. use App\State\Providers\AllDepartmentsByCompanyUserProcessor;
  22. use App\Repository\Department\DepartmentRepository;
  23. /**
  24.  * @ORM\Entity(repositoryClass="App\Repository\Department\DepartmentRepository")
  25.  * @ORM\HasLifecycleCallbacks()
  26.  */
  27. #[GetCollection(
  28.     uriTemplate:"/v2/departments/my",
  29.     provider:AllDepartmentsByCompanyUserProcessor::class,
  30.     normalizationContext:["groups" => ["budget:get:all"]],
  31. )]
  32. #[Get(
  33.     normalizationContext:["groups" => ["budget:get:all"]]
  34. )]
  35. class Department
  36. {
  37.     use TimestampTrait;
  38.     const TAGS = [
  39.         self::TAG_ADMIN,
  40.         self::TAG_FINANCE,
  41.         self::TAG_CEO_DEPARTMENT,
  42.         self::TAG_NORMAL,
  43.         self::TAG_HUMAN_RESOURCE,
  44.         self::TAG_OPERATION,
  45.         self::TAG_INTERNAL_CONTROL,
  46.     ];
  47.     const TAG_FINANCE"FINANCE";
  48.     const TAG_CEO_DEPARTMENT"CEO_DEPARTMENT";
  49.     const TAG_ADMIN"ADMIN";
  50.     const TAG_NORMAL"NORMAL";
  51.     const TAG_HUMAN_RESOURCE"HUMAN_RESOURCE";
  52.     const TAG_OPERATION"OPERATION";
  53.     const TAG_INTERNAL_CONTROL"INTERNAL_CONTROL";
  54.     /**
  55.      * @ORM\Id()
  56.      * @ORM\GeneratedValue()
  57.      * @ORM\Column(type="integer")
  58.      * @Groups({"rmq:user","budget:get:all","user.details", "requisition:fread","requisition:read"})
  59.      */
  60.     private $id;
  61.     /**
  62.      * @ORM\Column(type="string", length=255)
  63.      * @Assert\Length(min=3)
  64.      * @Groups({"user:fRead","user.details","requisition:fread","receipt:fRead","atime:fread","presence:read","rmq:user","budget:get:all", "requisition:fread","requisition:read"})
  65.      */
  66.     private $name;
  67.     /**
  68.      * @ORM\Column(type="text", nullable=true)
  69.      */
  70.     private $description;
  71.     /**
  72.      * @ORM\ManyToMany(targetEntity="App\Entity\Company\Company", mappedBy="departments")
  73.      */
  74.     private $companies;
  75.     /**
  76.      * @ORM\OneToMany(targetEntity="App\Entity\Department\Section", mappedBy="department")
  77.      */
  78.     private $sections;
  79.     /**
  80.      * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="department")
  81.      */
  82.     private $users;
  83.     /**
  84.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="departmentsAdded")
  85.      * @Groups({"rmq:user"})
  86.      */
  87.     private $company;
  88.     /**
  89.      * @ORM\ManyToMany(targetEntity=Presentation::class, inversedBy="departments")
  90.      */
  91.     private $natures;
  92.     /**
  93.      * @ORM\OneToMany(targetEntity=Budget::class, mappedBy="department")
  94.      */
  95.     private $budgets;
  96.     /**
  97.      * @ORM\OneToMany(targetEntity=Requisition::class, mappedBy="department")
  98.      */
  99.     private $requisitions;
  100.     /**
  101.      * @ORM\OneToMany(targetEntity=\App\Entity\Docs\Finance\V2\Budget\Budget::class, mappedBy="departement")
  102.      */
  103.     private $budgetsV2;
  104.     /**
  105.      * @Assert\Choice(choices=Department::TAGS, message="La valeur doit ĂȘtre soit FINANCE, CEO_DEPARTMENT, ADMIN ou NORMAL")
  106.      * @ORM\Column(type="string", length=255, nullable=true)
  107.      * @Groups({"user:fRead","requisition:fread","receipt:fRead","atime:fread","presence:read","rmq:user","budget:get:all", "requisition:fread","requisition:read"})
  108.      */
  109.     private $tag;
  110.     /**
  111.      * @ORM\OneToMany(targetEntity=Receipt::class, mappedBy="department")
  112.      */
  113.     private $receipts;
  114.     /**
  115.      * @Groups({"user:fRead","requisition:fread","receipt:fRead","atime:fread","presence:read","rmq:user","budget:get:all", "requisition:fread","requisition:read"})
  116.      * @ORM\Column(type="integer", nullable=true)
  117.      */
  118.     private $numOrderCompany;
  119.     /**
  120.      * @ORM\OneToMany(targetEntity=Action::class, mappedBy="department")
  121.      */
  122.     private $actions;
  123.     static public function getTagsLabels ()
  124.     {
  125.         $normalTags = [];
  126.         foreach (self::TAGS as $key => $tag) {
  127.             $normalTags[$tag] = $tag;
  128.         }
  129.         return $normalTags;
  130.     }
  131.     public function __construct()
  132.     {
  133.         $this->companies = new ArrayCollection();
  134.         $this->sections = new ArrayCollection();
  135.         $this->users = new ArrayCollection();
  136.         $this->natures = new ArrayCollection();
  137.         $this->projects = new ArrayCollection();
  138.         $this->budgets = new ArrayCollection();
  139.         $this->requisitions = new ArrayCollection();
  140.         $this->budgetsV2 = new ArrayCollection();
  141.         $this->tag self::TAG_NORMAL;
  142.         $this->receipts = new ArrayCollection();
  143.         $this->actions = new ArrayCollection();
  144.     }
  145.     public function getId(): ?int
  146.     {
  147.         return $this->id;
  148.     }
  149.     #[Groups(["budget:get:all"])]
  150.     public function getIri()
  151.     {
  152.         return "/api/departments/"$this->getId();
  153.     }
  154.     public function getName(): ?string
  155.     {
  156.         return $this->name;
  157.     }
  158.     public function setName(string $name): self
  159.     {
  160.         $this->name $name;
  161.         return $this;
  162.     }
  163.     public function getDescription(): ?string
  164.     {
  165.         return $this->description;
  166.     }
  167.     public function setDescription(string $description): self
  168.     {
  169.         $this->description $description;
  170.         return $this;
  171.     }
  172.     /**
  173.      * @return Collection|Company[]
  174.      */
  175.     public function getCompanies(): Collection
  176.     {
  177.         return $this->companies;
  178.     }
  179.     public function addCompany(Company $company): self
  180.     {
  181.         if (!$this->companies->contains($company)) {
  182.             $this->companies[] = $company;
  183.             $company->addDepartment($this);
  184.         }
  185.         return $this;
  186.     }
  187.     public function removeCompany(Company $company): self
  188.     {
  189.         if ($this->companies->contains($company)) {
  190.             $this->companies->removeElement($company);
  191.             $company->removeDepartment($this);
  192.         }
  193.         return $this;
  194.     }
  195.     /**
  196.      * @return Collection|Section[]
  197.      */
  198.     public function getSections(): Collection
  199.     {
  200.         return $this->sections;
  201.     }
  202.     public function addSection(Section $section): self
  203.     {
  204.         if (!$this->sections->contains($section)) {
  205.             $this->sections[] = $section;
  206.             $section->setDepartment($this);
  207.         }
  208.         return $this;
  209.     }
  210.     public function removeSection(Section $section): self
  211.     {
  212.         if ($this->sections->contains($section)) {
  213.             $this->sections->removeElement($section);
  214.             // set the owning side to null (unless already changed)
  215.             if ($section->getDepartment() === $this) {
  216.                 $section->setDepartment(null);
  217.             }
  218.         }
  219.         return $this;
  220.     }
  221.     /**
  222.      * @return Collection|User[]
  223.      */
  224.     public function getUsers(): Collection
  225.     {
  226.         return $this->users;
  227.     }
  228.     public function addUser(User $user): self
  229.     {
  230.         if (!$this->users->contains($user)) {
  231.             $this->users[] = $user;
  232.             $user->setDepartment($this);
  233.         }
  234.         return $this;
  235.     }
  236.     public function removeUser(User $user): self
  237.     {
  238.         if ($this->users->contains($user)) {
  239.             $this->users->removeElement($user);
  240.             // set the owning side to null (unless already changed)
  241.             if ($user->getDepartment() === $this) {
  242.                 $user->setDepartment(null);
  243.             }
  244.         }
  245.         return $this;
  246.     }
  247.     public function getCompany(): ?Company
  248.     {
  249.         return $this->company;
  250.     }
  251.     public function setCompany(?Company $company): self
  252.     {
  253.         $this->company $company;
  254.         return $this;
  255.     }
  256.     /**
  257.      * @return Collection|Presentation[]
  258.      */
  259.     public function getNatures(): Collection
  260.     {
  261.         return $this->natures;
  262.     }
  263.     public function addNature(Presentation $nature): self
  264.     {
  265.         if (!$this->natures->contains($nature)) {
  266.             $this->natures[] = $nature;
  267.         }
  268.         return $this;
  269.     }
  270.     public function removeNature(Presentation $nature): self
  271.     {
  272.         if ($this->natures->contains($nature)) {
  273.             $this->natures->removeElement($nature);
  274.         }
  275.         return $this;
  276.     }
  277.     /**
  278.      * @return Collection|Budget[]
  279.      */
  280.     public function getBudgets(): Collection
  281.     {
  282.         return $this->budgets;
  283.     }
  284.     public function addBudget(Budget $budget): self
  285.     {
  286.         if (!$this->budgets->contains($budget)) {
  287.             $this->budgets[] = $budget;
  288.             $budget->setDepartment($this);
  289.         }
  290.         return $this;
  291.     }
  292.     public function removeBudget(Budget $budget): self
  293.     {
  294.         if ($this->budgets->removeElement($budget)) {
  295.             // set the owning side to null (unless already changed)
  296.             if ($budget->getDepartment() === $this) {
  297.                 $budget->setDepartment(null);
  298.             }
  299.         }
  300.         return $this;
  301.     }
  302.     /**
  303.      * @return User
  304.      */
  305.     public function firstUser(): ?User
  306.     {
  307.         $user $this->getUsers()->first();
  308.         if ($user) {
  309.             return $user??null;
  310.         }
  311.         return null;
  312.     }
  313.     /**
  314.      * @return Collection|Requisition[]
  315.      */
  316.     public function getRequisitions(): Collection
  317.     {
  318.         return $this->requisitions;
  319.     }
  320.     public function addRequisition(Requisition $requisition): self
  321.     {
  322.         if (!$this->requisitions->contains($requisition)) {
  323.             $this->requisitions[] = $requisition;
  324.             $requisition->setDepartment($this);
  325.         }
  326.         return $this;
  327.     }
  328.     public function removeRequisition(Requisition $requisition): self
  329.     {
  330.         if ($this->requisitions->removeElement($requisition)) {
  331.             // set the owning side to null (unless already changed)
  332.             if ($requisition->getDepartment() === $this) {
  333.                 $requisition->setDepartment(null);
  334.             }
  335.         }
  336.         return $this;
  337.     }
  338.     /**
  339.      * @return Collection<int, \App\Entity\Docs\Finance\V2\Budget\Budget>
  340.      */
  341.     public function getBudgetsV2(): Collection
  342.     {
  343.         return $this->budgetsV2;
  344.     }
  345.     public function addBudgetsV2(\App\Entity\Docs\Finance\V2\Budget\Budget $budgetsV2): self
  346.     {
  347.         if (!$this->budgetsV2->contains($budgetsV2)) {
  348.             $this->budgetsV2[] = $budgetsV2;
  349.             $budgetsV2->setDepartement($this);
  350.         }
  351.         return $this;
  352.     }
  353.     public function removeBudgetsV2(\App\Entity\Docs\Finance\V2\Budget\Budget $budgetsV2): self
  354.     {
  355.         if ($this->budgetsV2->removeElement($budgetsV2)) {
  356.             // set the owning side to null (unless already changed)
  357.             if ($budgetsV2->getDepartement() === $this) {
  358.                 $budgetsV2->setDepartement(null);
  359.             }
  360.         }
  361.         return $this;
  362.     }
  363.     public function getTag(): ?string
  364.     {
  365.         return $this->tag;
  366.     }
  367.     public function setTag(string $tag): self
  368.     {
  369.         $this->tag $tag;
  370.         return $this;
  371.     }
  372.     /**
  373.      * @return Collection<int, Receipt>
  374.      */
  375.     public function getReceipts(): Collection
  376.     {
  377.         return $this->receipts;
  378.     }
  379.     public function addReceipt(Receipt $receipt): self
  380.     {
  381.         if (!$this->receipts->contains($receipt)) {
  382.             $this->receipts[] = $receipt;
  383.             $receipt->setDepartment($this);
  384.         }
  385.         return $this;
  386.     }
  387.     public function removeReceipt(Receipt $receipt): self
  388.     {
  389.         if ($this->receipts->removeElement($receipt)) {
  390.             // set the owning side to null (unless already changed)
  391.             if ($receipt->getDepartment() === $this) {
  392.                 $receipt->setDepartment(null);
  393.             }
  394.         }
  395.         return $this;
  396.     }
  397.     public function getNumOrderCompany(): ?int
  398.     {
  399.         return $this->numOrderCompany;
  400.     }
  401.     public function setNumOrderCompany(?int $numOrderCompany): self
  402.     {
  403.         $this->numOrderCompany $numOrderCompany;
  404.         return $this;
  405.     }
  406.     /**
  407.      * @return Collection<int, Action>
  408.      */
  409.     public function getActions(): Collection
  410.     {
  411.         return $this->actions;
  412.     }
  413.     public function addAction(Action $action): self
  414.     {
  415.         if (!$this->actions->contains($action)) {
  416.             $this->actions[] = $action;
  417.             $action->setDepartment($this);
  418.         }
  419.         return $this;
  420.     }
  421.     public function removeAction(Action $action): self
  422.     {
  423.         if ($this->actions->removeElement($action)) {
  424.             // set the owning side to null (unless already changed)
  425.             if ($action->getDepartment() === $this) {
  426.                 $action->setDepartment(null);
  427.             }
  428.         }
  429.         return $this;
  430.     }
  431. }