src/Entity/Office.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\OfficeRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use JMS\Serializer\Annotation as Serializer;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. /**
  10. *
  11. * @Serializer\ExclusionPolicy("ALL")
  12. * @UniqueEntity(fields="code", message="Ya existe un área registrada con este código")
  13. */
  14. #[ORM\Entity(repositoryClass: OfficeRepository::class)]
  15. #[ORM\Table(name: '`office`')]
  16. class Office
  17. {
  18. /**
  19. * @Serializer\Expose()
  20. */
  21. #[ORM\Id]
  22. #[ORM\GeneratedValue(strategy:"AUTO")]
  23. #[ORM\Column(type: "integer")]
  24. protected ?int $id = null;
  25. #[ORM\Column(type: "boolean")]
  26. protected $enabled = true;
  27. /**
  28. * @Serializer\Expose()
  29. */
  30. #[Assert\NotBlank(message:"El Nombre no puede estar en blanco")]
  31. #[ORM\Column(type: "string", length: 300)]
  32. protected $name;
  33. /**
  34. * @Serializer\Expose()
  35. */
  36. #[Assert\NotBlank(message:"El codigo no puede estar en blanco")]
  37. #[ORM\Column(type: "string", length: 50)]
  38. protected $code;
  39. /**
  40. * @Serializer\Expose()
  41. */
  42. #[Assert\NotBlank(message:"La dirección no puede estar en blanco")]
  43. #[ORM\Column(type: "string", length: 200)]
  44. protected $address;
  45. /**
  46. * @var City
  47. * @Serializer\Expose()
  48. */
  49. #[Assert\NotBlank(message:"Debe seleccionar una comuna")]
  50. #[ORM\ManyToOne(targetEntity:"City", fetch:"EAGER")]
  51. #[ORM\JoinColumn(name: "city_id", referencedColumnName: "id", nullable: true, onDelete: "SET NULL")]
  52. protected $city;
  53. /**
  54. * @Serializer\Expose()
  55. */
  56. #[ORM\Column(type: "string", nullable: false)]
  57. protected $costCenter;
  58. /**
  59. * @var ArrayCollection
  60. */
  61. #[Assert\Valid]
  62. #[ORM\OneToMany(targetEntity:"Employee", mappedBy:"office", fetch: "EXTRA_LAZY", orphanRemoval: true, cascade: ['persist'])]
  63. #[ORM\OrderBy(["id" => "DESC"])]
  64. protected $employees;
  65. public function __construct()
  66. {
  67. $this->employees = new ArrayCollection();
  68. }
  69. public function __toString()
  70. {
  71. return sprintf('%s - %s', $this->code, $this->name);
  72. }
  73. public function getId()
  74. {
  75. return $this->id;
  76. }
  77. public function setCode($code)
  78. {
  79. $this->code = $code;
  80. }
  81. public function getCode()
  82. {
  83. return $this->code;
  84. }
  85. public function setName($name)
  86. {
  87. $this->name = $name;
  88. }
  89. public function getName()
  90. {
  91. return $this->name;
  92. }
  93. public function setAddress($address)
  94. {
  95. $this->address = $address;
  96. }
  97. public function getAddress()
  98. {
  99. return $this->address;
  100. }
  101. public function setCity(City $city = null)
  102. {
  103. $this->city = $city;
  104. }
  105. public function getCity()
  106. {
  107. return $this->city;
  108. }
  109. public function setCostCenter($costCenter)
  110. {
  111. $this->costCenter = $costCenter;
  112. }
  113. public function getCostCenter()
  114. {
  115. return $this->costCenter;
  116. }
  117. public function getFullAddress()
  118. {
  119. return sprintf('%s, %s, %s', $this->address, $this->getCity(), $this->getCity()->getParent());
  120. }
  121. public function getEnabled()
  122. {
  123. return $this->enabled;
  124. }
  125. public function setEnabled($enabled)
  126. {
  127. $this->enabled = $enabled;
  128. }
  129. /**
  130. * @return ArrayCollection|Employee[]
  131. */
  132. public function getEmployees()
  133. {
  134. return $this->employees;
  135. }
  136. /**
  137. * @param Employee $employee
  138. * @return Office
  139. */
  140. public function addEmployee(Employee $employee): Office
  141. {
  142. if ($this->employees->contains($employee)) {
  143. return $this;
  144. }
  145. $this->employees[] = $employee;
  146. $employee->setOffice($this);
  147. return $this;
  148. }
  149. /**
  150. * @param ArrayCollection $employees
  151. * @return Office
  152. */
  153. public function setEmployees($employees): Office
  154. {
  155. $this->employees = $employees;
  156. return $this;
  157. }
  158. /**
  159. * @param Employee $employee
  160. * @return Office
  161. */
  162. public function removeEmployee(Employee $employee): Office
  163. {
  164. if (!$this->employees->contains($employee)) {
  165. return $this;
  166. }
  167. $this->employees->removeElement($employee);
  168. $employee->setOffice(null);
  169. return $this;
  170. }
  171. /**
  172. * @return ArrayCollection|User[]
  173. */
  174. public function getUsers()
  175. {
  176. $users = new ArrayCollection();
  177. $employees = $this->employees;
  178. foreach ($employees as $employee)
  179. {
  180. $user = $employee->getUser();
  181. if (!$users->contains($user)) {
  182. $users->add($user);
  183. }
  184. }
  185. return $users;
  186. }
  187. }