<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use JMS\Serializer\Annotation\Groups;
/**
* @ORM\Table("Person")
*
* @ORM\HasLifecycleCallbacks()
*
* @ORM\Entity(repositoryClass="App\Repository\PersonRepository")
*/
class Person
{
/**
* @Groups({"adminindex", "persondetail"})
*
* @ORM\Id()
*
* @ORM\GeneratedValue()
*
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Groups({"persondetail", "adminindex", "webmasterindex", "amateurupgradeindex"})
*
* @ORM\Column(type="string", length=255)
*/
private $first_name;
/**
* @Groups({"persondetail", "adminindex", "webmasterindex", "amateurupgradeindex"})
*
* @ORM\Column(type="string", length=255)
*/
private $last_name;
/**
* @Groups({"persondetail", "adminindex"})
*
* @ORM\Column(type="date", nullable=true)
*/
private $date_of_birth;
/**
* @Groups({"persondetail", "adminindex"})
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $cell_phone;
/**
* @Groups({"bankaccount"})
*
* @ManyToOne(targetEntity="App\Entity\BankAccount", cascade={"persist"})
*
* @JoinColumn(name="bank_account_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $bank_account;
/**
* @Groups({"persondetail", "adminindex"})
*
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @Groups({"adminindex", "commonids"})
*
* @ManyToOne(targetEntity="App\Entity\Account", cascade={"persist"})
*
* @JoinColumn(name="account_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $account;
/**
* @Groups({"adminindex", "addressindex"})
*
* @ManyToOne(targetEntity="App\Entity\Address", cascade={"persist"})
*
* @JoinColumn(name="address_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $address;
/**
* @Groups({"persondetail", "adminindex"})
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $company_name;
/**
* @Groups({"persondetail", "adminindex"})
*
* @ORM\ManyToMany(targetEntity="App\Entity\TaxNumber", mappedBy="person")
*
* @ORM\JoinTable(name="PersonTaxNumbers")
*/
private $taxNumbers;
public function __construct()
{
$this->taxNumbers = new ArrayCollection();
}
/**
* @ORM\PrePersist()
*/
public function prePersist()
{
$this->setCreatedAt(new \DateTime());
}
public function __toString(): string
{
return $this->getFullName();
}
public function getFullName(): string
{
return trim($this->getFirstName().' '.$this->getLastName());
}
public function getId()
{
return $this->id;
}
public function getFirstName(): ?string
{
return $this->first_name;
}
public function setFirstName(string $first_name): self
{
$this->first_name = $first_name;
return $this;
}
public function getLastName(): ?string
{
return $this->last_name;
}
public function setLastName(string $last_name): self
{
$this->last_name = $last_name;
return $this;
}
public function getDateOfBirth(): ?\DateTimeInterface
{
return $this->date_of_birth;
}
public function setDateOfBirth(?\DateTimeInterface $date_of_birth): self
{
$this->date_of_birth = $date_of_birth;
return $this;
}
public function getCellPhone(): ?string
{
return $this->cell_phone;
}
public function setCellPhone(?string $cell_phone): self
{
$this->cell_phone = $cell_phone;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeInterface $created_at): self
{
$this->created_at = $created_at;
return $this;
}
public function getAccount(): ?Account
{
return $this->account;
}
public function setAccount(?Account $account): self
{
$this->account = $account;
return $this;
}
public function getCompanyName(): ?string
{
return $this->company_name;
}
public function setCompanyName(?string $company_name): self
{
$this->company_name = $company_name;
return $this;
}
public function setAddress(?Address $address): self
{
$this->address = $address;
return $this;
}
public function getAddress(): ?Address
{
return $this->address;
}
public function setBankAccount(?BankAccount $bank_account): self
{
$this->bank_account = $bank_account;
return $this;
}
public function getBankAccount(): ?BankAccount
{
return $this->bank_account;
}
/**
* @return Collection|TaxNumber[]
*/
public function getTaxNumbers(): Collection
{
return $this->taxNumbers;
}
public function addTaxNumber(TaxNumber $taxNumber): self
{
if (!$this->taxNumbers->contains($taxNumber)) {
$this->taxNumbers[] = $taxNumber;
$taxNumber->addPerson($this);
}
return $this;
}
public function removeTaxNumber(TaxNumber $taxNumber): self
{
if ($this->taxNumbers->contains($taxNumber)) {
$this->taxNumbers->removeElement($taxNumber);
$taxNumber->removePerson($this);
}
return $this;
}
/**
* @throws \Exception
*/
public function hasChangedMainFields(Person $person, bool $includeAddress = true, bool $includeBankAccount = true, bool $includeTaxNumbers = true): bool
{
// $this is the new entity, person is the old one
$hasChangedPerson = $person->getFirstName() !== $this->getFirstName()
|| $person->getLastName() !== $this->getLastName()
|| $person->getCompanyName() !== $this->getCompanyName()
|| $person->getCellPhone() !== $this->getCellPhone();
if (null !== $person->getDateOfBirth() && null !== $this->getDateOfBirth()) {
$hasChangedPerson = $hasChangedPerson || $person->getDateOfBirth()->format('Ymd') !== $this->getDateOfBirth()->format('Ymd');
}
if (null === $person->getDateOfBirth() && null !== $this->getDateOfBirth()) {
$hasChangedPerson = true;
}
$hasChangedAddress = false;
if ($includeAddress) {
if (null === $person->getAddress() && null === $this->getAddress()) {
$hasChangedAddress = false;
} elseif ($person->getAddress() instanceof Address) {
$hasChangedAddress = $person->getAddress()->hasChangedMainFields($this->getAddress());
} elseif ($this->getAddress() instanceof Address) {
$hasChangedAddress = $this->getAddress()->hasChangedMainFields($person->getAddress());
} else {
throw new \Exception('impossible');
}
}
$hasChangedBankAccount = false;
if ($includeBankAccount) {
if (null === $this->getBankAccount() && null === $person->getBankAccount()) {
$hasChangedBankAccount = false;
} elseif ($person->getBankAccount() instanceof BankAccount) {
$hasChangedBankAccount = $person->getBankAccount()->hasChangedMainFields($this->getBankAccount());
} elseif ($this->getBankAccount() instanceof BankAccount) {
$hasChangedBankAccount = $this->getBankAccount()->hasChangedMainFields($person->getBankAccount());
} else {
throw new \Exception('impossible');
}
}
$hasChangedTaxNumbers = false;
if ($includeTaxNumbers) {
if ($this->getTaxNumbers()->count() !== $person->getTaxNumbers()->count()) {
$hasChangedTaxNumbers = true;
} else {
$newNumbers = [];
$oldNumbers = [];
foreach ($person->getTaxNumbers() as $taxNumber) {
$oldNumbers[] = $taxNumber->getTaxNumber().' / '.$taxNumber->getType();
}
foreach ($this->getTaxNumbers() as $taxNumber) {
$newNumbers[] = $taxNumber->getTaxNumber().' / '.$taxNumber->getType();
}
foreach ($newNumbers as $newNumber) {
if (!in_array($newNumber, $oldNumbers)) {
$hasChangedTaxNumbers = true;
break;
}
}
}
}
return $hasChangedPerson || $hasChangedAddress || $hasChangedBankAccount || $hasChangedTaxNumbers;
}
}