<?php
namespace App\Entity;
use App\Repository\OfficeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
*
* @Serializer\ExclusionPolicy("ALL")
* @UniqueEntity(fields="code", message="Ya existe un área registrada con este código")
*/
#[ORM\Entity(repositoryClass: OfficeRepository::class)]
#[ORM\Table(name: '`office`')]
class Office
{
/**
* @Serializer\Expose()
*/
#[ORM\Id]
#[ORM\GeneratedValue(strategy:"AUTO")]
#[ORM\Column(type: "integer")]
protected ?int $id = null;
#[ORM\Column(type: "boolean")]
protected $enabled = true;
/**
* @Serializer\Expose()
*/
#[Assert\NotBlank(message:"El Nombre no puede estar en blanco")]
#[ORM\Column(type: "string", length: 300)]
protected $name;
/**
* @Serializer\Expose()
*/
#[Assert\NotBlank(message:"El codigo no puede estar en blanco")]
#[ORM\Column(type: "string", length: 50)]
protected $code;
/**
* @Serializer\Expose()
*/
#[Assert\NotBlank(message:"La dirección no puede estar en blanco")]
#[ORM\Column(type: "string", length: 200)]
protected $address;
/**
* @var City
* @Serializer\Expose()
*/
#[Assert\NotBlank(message:"Debe seleccionar una comuna")]
#[ORM\ManyToOne(targetEntity:"City", fetch:"EAGER")]
#[ORM\JoinColumn(name: "city_id", referencedColumnName: "id", nullable: true, onDelete: "SET NULL")]
protected $city;
/**
* @Serializer\Expose()
*/
#[ORM\Column(type: "string", nullable: false)]
protected $costCenter;
/**
* @var ArrayCollection
*/
#[Assert\Valid]
#[ORM\OneToMany(targetEntity:"Employee", mappedBy:"office", fetch: "EXTRA_LAZY", orphanRemoval: true, cascade: ['persist'])]
#[ORM\OrderBy(["id" => "DESC"])]
protected $employees;
public function __construct()
{
$this->employees = new ArrayCollection();
}
public function __toString()
{
return sprintf('%s - %s', $this->code, $this->name);
}
public function getId()
{
return $this->id;
}
public function setCode($code)
{
$this->code = $code;
}
public function getCode()
{
return $this->code;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setAddress($address)
{
$this->address = $address;
}
public function getAddress()
{
return $this->address;
}
public function setCity(City $city = null)
{
$this->city = $city;
}
public function getCity()
{
return $this->city;
}
public function setCostCenter($costCenter)
{
$this->costCenter = $costCenter;
}
public function getCostCenter()
{
return $this->costCenter;
}
public function getFullAddress()
{
return sprintf('%s, %s, %s', $this->address, $this->getCity(), $this->getCity()->getParent());
}
public function getEnabled()
{
return $this->enabled;
}
public function setEnabled($enabled)
{
$this->enabled = $enabled;
}
/**
* @return ArrayCollection|Employee[]
*/
public function getEmployees()
{
return $this->employees;
}
/**
* @param Employee $employee
* @return Office
*/
public function addEmployee(Employee $employee): Office
{
if ($this->employees->contains($employee)) {
return $this;
}
$this->employees[] = $employee;
$employee->setOffice($this);
return $this;
}
/**
* @param ArrayCollection $employees
* @return Office
*/
public function setEmployees($employees): Office
{
$this->employees = $employees;
return $this;
}
/**
* @param Employee $employee
* @return Office
*/
public function removeEmployee(Employee $employee): Office
{
if (!$this->employees->contains($employee)) {
return $this;
}
$this->employees->removeElement($employee);
$employee->setOffice(null);
return $this;
}
/**
* @return ArrayCollection|User[]
*/
public function getUsers()
{
$users = new ArrayCollection();
$employees = $this->employees;
foreach ($employees as $employee)
{
$user = $employee->getUser();
if (!$users->contains($user)) {
$users->add($user);
}
}
return $users;
}
}