<?php
/**
* @author Léo BANNHOLTZER (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\CustomersBundle\Entity;
use App\Entity\Context;
use App\Entity\Country;
use App\Entity\CountryState;
use App\Entity\Currency;
use App\Entity\Traits\EntityManagerServiceEntity;
use App\Services\SoftdeleteFilter;
use DateTime;
use DateTimeInterface;
use Doctrine\ORM\NonUniqueResultException;
use Exception;
use Symfony\Component\Uid\Uuid;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\Common\Collections\Collection;
use JsonSerializable as JsonSerializableAlias;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Bluue\CustomersBundle\Repository\CustomerRepository;
use App\DoctrineExtensions\Timestampable\Traits\UserTimestampableEntity;
use App\DoctrineExtensions\SoftDeleteable\Traits\UserSoftDeleteableEntity;
use Bluue\CustomersBundle\EventListener\CustomerListener;
/**
* @ORM\Entity(repositoryClass=CustomerRepository::class)
* @ORM\Table(name="customers_bundle__customer", indexes={
* @ORM\Index(name="name", columns={"name"}),
* @ORM\Index(name="firstname", columns={"firstname"}),
* @ORM\Index(name="lastname", columns={"lastname"}),
* @ORM\Index(name="postcode", columns={"postcode"}),
* @ORM\Index(name="city", columns={"city"}),
* @ORM\Index(name="phone", columns={"phone"}),
* @ORM\Index(name="phone_mobile", columns={"phone_mobile"}),
* @ORM\Index(name="email", columns={"email"}),
* @ORM\Index(name="contact", columns={"contact"}),
* @ORM\Index(name="job", columns={"job"}),
* @ORM\Index(name="reference", columns={"reference"}),
* @ORM\Index(name="birthday", columns={"birthday"}),
* @ORM\Index(name="deleted_at", columns={"deleted_at"}),
* @ORM\Index(name="created_at", columns={"created_at"}),
* @ORM\Index(name="updated_at", columns={"updated_at"}),
* @ORM\Index(name="service_code", columns={"service_code"})
* })
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
* @ORM\EntityListeners({CustomerListener::class})
*/
class Customer implements JsonSerializableAlias
{
use UserTimestampableEntity;
use UserSoftDeleteableEntity;
use EntityManagerServiceEntity;
/**
* @ORM\Id
* @ORM\Column(type="uuid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
private ?Uuid $id = null;
/**
* @ORM\ManyToOne(targetEntity=CustomerType::class, inversedBy="customers")
*/
private ?CustomerType $customer_type = null;
/**
* @ORM\ManyToOne(targetEntity=CustomerGroup::class, inversedBy="default_customers")
*/
private ?CustomerGroup $customer_group = null;
/**
* @ORM\ManyToMany(targetEntity=CustomerGroup::class, inversedBy="customers")
* @ORM\JoinTable(name="customers_bundle__customer_group_customer")
*/
private $customerGroups;
/**
* @ORM\Column(type="string", length=255)
*/
private string $name;
/**
* @ORM\Column(type="string", length=128, nullable=true)
*/
private ?string $firstname = null;
/**
* @ORM\Column(type="string", length=128, nullable=true)
*/
private ?string $lastname = null;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private ?int $nature = null;// 1 => Professionnel | 2 => Particulier
/**
* @ORM\Column(type="string", length=128, nullable=true)
*/
private ?string $address = null;
/**
* @ORM\Column(type="string", length=128, nullable=true)
*/
private ?string $address_2 = null;
/**
* @ORM\Column(type="string", length=25, nullable=true)
*/
private ?string $postcode = null;
/**
* @ORM\Column(type="string", length=64, nullable=true)
*/
private ?string $city = null;
/**
* @ORM\ManyToOne(targetEntity=Country::class)
*/
private ?Country $country = null;
/**
* @ORM\ManyToOne(targetEntity=CountryState::class)
*/
private ?CountryState $country_state = null;
/**
* @ORM\Column(type="string", length=32, nullable=true)
*/
private ?string $vat_number = null;
/**
* @ORM\Column(type="string", length=14, nullable=true)
*/
private ?string $siret;
/**
* @ORM\Column(type="string", length=16, nullable=true)
*/
private ?string $siren;
/**
* @ORM\Column(type="string", length=5, nullable=true)
*/
private ?string $ape;
/**
* @ORM\Column(type="string", length=32, nullable=true)
*/
private ?string $phone = null;
/**
* @ORM\Column(type="string", length=32, nullable=true)
*/
private ?string $phone_mobile = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $email = null;
/**
* @ORM\Column(type="string", length=128, nullable=true)
*/
private ?string $website = null;
/**
* @ORM\Column(type="text", nullable=true)
*/
private ?string $internal_note = null;
/**
* @ORM\Column(type="boolean")
*/
private bool $blocked = false;
/**
* @ORM\OneToMany(targetEntity="Customer", mappedBy="parent")
*/
private ?Collection $children;
/**
* @ORM\ManyToOne(targetEntity="Customer", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
private ?Customer $parent = null;
/**
* @ORM\ManyToOne(targetEntity=Context::class)
*/
private ?Context $context = null;
/**
* @ORM\ManyToOne(targetEntity=Currency::class)
*/
private ?Currency $currency = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $relay_number = null;
/**
* @ORM\Column(type="boolean", options={"default":"0"})
*/
private bool $contact = false;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $job = null;
/**
* @ORM\Column(type="string", length=128, nullable="true")
*/
private ?string $reference = null;
/**
* @ORM\Column(type="date", nullable="true")
*/
private ?DateTimeInterface $birthday = null;
/**
* @ORM\Column(type="string", length=128, nullable="true")
*/
private ?string $serviceCode = null;
/**
* @ORM\Column(type="json", nullable="true")
*/
private ?array $options = [];
/**
* @ORM\Column(type="boolean", options={"default":"0"})
*/
private bool $isUnsubmit = false;
/**
* @ORM\ManyToOne(targetEntity=Context::class)
*/
private ?Context $purchasingContext = null;
public function __construct()
{
$this->children = new ArrayCollection();
$this->customerGroups = new ArrayCollection();
}
/**
* @return Uuid|null
*/
public function getId(): ?Uuid
{
return $this->id;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
* @return $this
*/
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return string|null
*/
public function getFirstname(): ?string
{
return $this->firstname;
}
/**
* @param string|null $firstname
* @return $this
*/
public function setFirstname(?string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
/**
* @return string|null
*/
public function getLastname(): ?string
{
return $this->lastname;
}
/**
* @param string|null $lastname
* @return $this
*/
public function setLastname(?string $lastname): self
{
if ($lastname) {
$lastname = mb_convert_case($lastname, MB_CASE_UPPER_SIMPLE, 'UTF-8');
}
$this->lastname = $lastname;
return $this;
}
/**
* 1 => Professionnel | 2 => Particulier
* @return int|null
*/
public function getNature(): ?int
{
return $this->nature;
}
/**
* 1 => Professionnel | 2 => Particulier
* @param int|null $nature
* @return $this
*/
public function setNature(?int $nature): self
{
$this->nature = $nature;
return $this;
}
/**
* @return string|null
*/
public function getAddress(): ?string
{
return $this->address;
}
/**
* @param string|null $address
* @return $this
*/
public function setAddress(?string $address): self
{
$this->address = $address;
return $this;
}
/**
* @return string|null
*/
public function getAddress2(): ?string
{
return $this->address_2;
}
/**
* @param string|null $address_2
* @return $this
*/
public function setAddress2(?string $address_2): self
{
$this->address_2 = $address_2;
return $this;
}
/**
* @return string|null
*/
public function getPostcode(): ?string
{
return $this->postcode;
}
/**
* @param string|null $postcode
* @return $this
*/
public function setPostcode(?string $postcode): self
{
$this->postcode = $postcode;
return $this;
}
/**
* @return string|null
*/
public function getCity(): ?string
{
return $this->city;
}
/**
* @param string|null $city
* @return $this
*/
public function setCity(?string $city): self
{
if ($city) {
$city = mb_convert_case($city, MB_CASE_UPPER_SIMPLE, 'UTF-8');
}
$this->city = $city;
return $this;
}
/**
* @return string|null
*/
public function getVatNumber(): ?string
{
return $this->vat_number;
}
/**
* @param string|null $vat_number
* @return $this
*/
public function setVatNumber(?string $vat_number): self
{
$this->vat_number = $vat_number;
return $this;
}
/**
* @return string|null
*/
public function getSiret(): ?string
{
return $this->siret;
}
/**
* @param string|null $siret
* @return $this
*/
public function setSiret(?string $siret): self
{
$this->siret = $siret;
return $this;
}
/**
* @return string|null
*/
public function getSiren(): ?string
{
return $this->siren;
}
/**
* @param string|null $siren
* @return $this
*/
public function setSiren(?string $siren): self
{
$this->siren = $siren;
return $this;
}
/**
* @return string|null
*/
public function getApe(): ?string
{
return $this->ape;
}
/**
* @param string|null $ape
* @return $this
*/
public function setApe(?string $ape): self
{
$this->ape = $ape;
return $this;
}
/**
* @return string|null
*/
public function getPhone(): ?string
{
return $this->phone;
}
/**
* @param string|null $phone
* @return $this
*/
public function setPhone(?string $phone): self
{
$this->phone = preg_replace('/[^0-9]/', '', $phone);
return $this;
}
/**
* @return string|null
*/
public function getPhoneMobile(): ?string
{
return $this->phone_mobile;
}
/**
* @param string|null $phone_mobile
* @return $this
*/
public function setPhoneMobile(?string $phone_mobile): self
{
$this->phone_mobile = preg_replace('/[^0-9]/', '', $phone_mobile);
return $this;
}
/**
* @return string|null
*/
public function getEmail(): ?string
{
return $this->email;
}
/**
* @param string|null $email
* @return $this
*/
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
/**
* @return string|null
*/
public function getWebsite(): ?string
{
return $this->website;
}
/**
* @param string|null $website
* @return $this
*/
public function setWebsite(?string $website): self
{
$this->website = $website;
return $this;
}
/**
* @return string|null
*/
public function getInternalNote(): ?string
{
return $this->internal_note;
}
/**
* @param string|null $internal_note
* @return $this
*/
public function setInternalNote(?string $internal_note): self
{
$this->internal_note = $internal_note;
return $this;
}
/**
* @return bool
*/
public function isBlocked(): bool
{
return $this->blocked;
}
/**
* @param bool $blocked
* @return $this
*/
public function setBlocked(bool $blocked): self
{
$this->blocked = $blocked;
return $this;
}
/**
* @return Country|null
* @throws NonUniqueResultException
*/
public function getCountry(): ?Country
{
if ($this->em && $this->country) {
SoftdeleteFilter::disable($this->em, [Country::class]);
$country = $this->em->getRepository(Country::class)
->createQueryBuilder('c')
->where('c.id = :countryId')
->setParameter('countryId', $this->country->getId()->toBinary())
->getQuery()
->getOneOrNullResult();
SoftdeleteFilter::enable($this->em, [Country::class]);
return $country;
}
return $this->country;
}
/**
* @param Country|null $country
* @return $this
*/
public function setCountry(?Country $country): self
{
$this->country = $country;
return $this;
}
/**
* @return CountryState|null
*/
public function getCountryState(): ?CountryState
{
return $this->country_state;
}
/**
* @param CountryState|null $country_state
* @return $this
*/
public function setCountryState(?CountryState $country_state): self
{
$this->country_state = $country_state;
return $this;
}
/**
* @return CustomerType|null
* @throws NonUniqueResultException
*/
public function getCustomerType(): ?CustomerType
{
if ($this->em && $this->customer_type) {
SoftdeleteFilter::disable($this->em, [CustomerType::class]);
$customerType = $this->em->getRepository(CustomerType::class)
->createQueryBuilder('ct')
->where('ct.id = :customerTypeId')
->setParameter('customerTypeId', $this->customer_type->getId()->toBinary())
->getQuery()
->getOneOrNullResult();
SoftdeleteFilter::enable($this->em, [CustomerType::class]);
return $customerType;
}
return $this->customer_type;
}
/**
* @param CustomerType|null $customer_type
* @return $this
*/
public function setCustomerType(?CustomerType $customer_type): self
{
$this->customer_type = $customer_type;
return $this;
}
/**
* @return Collection|Customer[]
*/
public function getChildren(): Collection
{
return $this->children;
}
/**
* @param Customer $child
* @return $this
*/
public function addChild(Customer $child): self
{
if (!$this->children->contains($child)) {
$this->children[] = $child;
$child->setParent($this);
}
return $this;
}
/**
* @param Customer $child
* @return $this
*/
public function removeChild(Customer $child): self
{
if ($this->children->removeElement($child)) {
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
/**
* @return $this|null
*/
public function getParent(): ?self
{
return $this->parent;
}
/**
* @param Customer|null $parent
* @return $this
*/
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Context|null
*/
public function getContext(): ?Context
{
return $this->context;
}
/**
* @param Context|null $context
* @return $this
*/
public function setContext(?Context $context): self
{
$this->context = $context;
return $this;
}
/**
* @return array
*/
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'address' => $this->address,
'address2' => $this->address_2,
'postcode' => $this->postcode,
'city' => $this->city,
'vat_number' => $this->vat_number,
'phone' => $this->phone,
'phone_mobile' => $this->phone_mobile,
'email' => $this->email,
'website' => $this->website,
'internal_note' => $this->internal_note
];
}
public function getCustomerGroup(): ?CustomerGroup
{
return $this->customer_group;
}
public function setCustomerGroup(?CustomerGroup $customer_group): self
{
$this->customer_group = $customer_group;
return $this;
}
/**
* @return Collection<int, CustomerGroup>
*/
public function getCustomerGroups(): Collection
{
return $this->customerGroups;
}
public function addCustomerGroup(CustomerGroup $customerGroup): self
{
if (!$this->customerGroups->contains($customerGroup)) {
$this->customerGroups[] = $customerGroup;
}
return $this;
}
public function removeCustomerGroup(CustomerGroup $customerGroup): self
{
$this->customerGroups->removeElement($customerGroup);
return $this;
}
/**
* @param Currency|null $currency
* @return Customer
*/
public function setCurrency(?Currency $currency): self
{
$this->currency = $currency;
return $this;
}
/**
* @return Currency|null
*/
public function getCurrency(): ?Currency
{
return $this->currency;
}
/**
* @return string|null
*/
public function getRelayNumber(): ?string
{
return $this->relay_number;
}
/**
* @param string|null $relay_number
* @return $this
*/
public function setRelayNumber(?string $relay_number): self
{
$this->relay_number = $relay_number;
return $this;
}
/**
* @return bool
*/
public function isContact(): bool
{
return $this->contact;
}
/**
* @param bool $contact
* @return $this
*/
public function setContact(bool $contact): self
{
$this->contact = $contact;
return $this;
}
/**
* @return string|null
*/
public function getJob(): ?string
{
return $this->job;
}
/**
* @param string|null $job
* @return $this
*/
public function setJob(?string $job): self
{
$this->job = $job;
return $this;
}
/**
* @return array
*/
public function getOptions(): ?array
{
return $this->options;
}
/**
* @param array $options
* @return $this
*/
public function setOptions(array $options): self
{
$this->options = $options;
return $this;
}
/**
* @param array $options
* @return $this
*/
public function addOptions(array $options): self
{
return $this->setOptions(array_merge_recursive($this->options, $options));
}
/**
* @param string $key
* @param array $options
* @return $this
*/
public function mergeOptions(string $key, array $options): self
{
$this->options[$key] = array_merge($options, $this->options[$key] ?? []);
return $this;
}
/**
* @return string
*/
public function getHtmlLabel(): string
{
$return = "
<div class='select2-result-repository'>
<div class='select2-result-repository__meta'>
<div class='select2-result-repository__title'>" . $this->getName();
if ($email = $this->getEmail()) {
$return .= ' - ' . $email;
}
$return .= "</div>";
if ($address = $this->getAddress()) {
$return .= "<div class='select2-result-repository__subtitle'>" . $address;
}
$postcode = $this->getPostcode();
$city = $this->getCity();
if ($postcode && $city) {
$stringPostcodeAndCity = $postcode . ' ' . $city;
} elseif ($postcode && !$city) {
$stringPostcodeAndCity = $postcode;
} elseif ($city && !$postcode) {
$stringPostcodeAndCity = $city;
} else {
$stringPostcodeAndCity = '';
}
if ($stringPostcodeAndCity) {
$return .= ' - ' . $stringPostcodeAndCity;
}
return $return . "</div</div></div>";
}
/**
* @return string
*/
public function getNameWithCivility(): string
{
$return = '';
if ($this->customer_type) {
$return .= $this->customer_type->getName();
}
return $return . ' ' . $this->name;
}
/**
* @return string|null
*/
public function getReference(): ?string
{
return $this->reference;
}
/**
* @param string|null $reference
* @return Customer
*/
public function setReference(?string $reference): self
{
$this->reference = $reference;
return $this;
}
/**
* @return DateTimeInterface|null
*/
public function getBirthday(): ?DateTimeInterface
{
return $this->birthday;
}
/**
* @param mixed $birthday
* @return Customer
* @throws Exception
*/
public function setBirthday($birthday): self
{
if ($birthday instanceof DateTimeInterface) {
$this->birthday = $birthday;
} elseif ($birthday) {
$this->birthday = new DateTime($birthday);
} else {
$this->birthday = null;
}
return $this;
}
/**
* @return string|null
*/
public function getServiceCode(): ?string
{
return $this->serviceCode;
}
/**
* @param string|null $serviceCode
* @return Customer
*/
public function setServiceCode(?string $serviceCode): Customer
{
$this->serviceCode = $serviceCode;
return $this;
}
/**
* @return bool
*/
public function getIsUnsubmit(): ?bool
{
return $this->isUnsubmit;
}
/**
* @param bool $isUnsubmit
* @return $this
*/
public function setIsUnsubmit(?bool $isUnsubmit): self
{
$this->isUnsubmit = $isUnsubmit;
return $this;
}
/**
* @return Context|null
*/
public function getPurchasingContext(): ?Context
{
return $this->purchasingContext;
}
/**
* @param Context|null $purchasingContext
* @return Customer
*/
public function setPurchasingContext(?Context $purchasingContext): Customer
{
$this->purchasingContext = $purchasingContext;
return $this;
}
}