<?php
namespace App\Entity;
use App\Repository\CustomerRepository;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Criteria;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use App\Helper\Helper;
/**
* @ORM\Entity(repositoryClass=CustomerRepository::class)
* @ORM\HasLifecycleCallbacks
*/
class Customer extends BaseEntity implements UserInterface, PasswordAuthenticatedUserInterface
{
public const TERM_VERSION_DATE = '2022-01-01';
public const FLAG_NATIVE = 2 ** 0;
public const FLAG_LEGACY = 2 ** 1;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="integer")
*/
private $customerNumber;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $vippsId;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string", nullable=true)
*/
private $password;
/**
* @var string The authenticated JWT token
*/
private $token;
/**
* @var string The authenticated JWT token
*/
private $refreshToken;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lastLoginDate;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime")
*/
private $updatedAt;
/**
* @ORM\Column(type="string", length=180, nullable=true)
*/
private $name;
/**
* @ORM\Column(type="integer", options={"default" : 0})
*/
private $flag;
/**
* @ORM\OneToMany(targetEntity=NotificationCustomerSetting::class, mappedBy="customer_id", cascade={"persist"})
*/
private $notificationCustomerSettings;
/**
* @ORM\OneToMany(targetEntity="ObjectCustomer", mappedBy="customer")
*/
private $objectCustomers;
/**
* @ORM\OneToMany(targetEntity=Favorite::class, mappedBy="customer", orphanRemoval=true)
*/
private $favorites;
/**
* @ORM\OneToMany(targetEntity=NotificationSearch::class, mappedBy="customer")
*/
private $notificationSearches;
/**
* @ORM\Column(type="boolean")
*/
private $isVerified = false;
/**
* @ORM\Column(type="string", length=20, nullable=true)
*/
private $language;
/**
* @ORM\Column(type="datetime")
*/
private $requestDeleteDate;
/**
* @ORM\Column(type="datetime")
*/
private $acceptedTermDate;
/**
* @ORM\OneToMany(targetEntity=NotificationCustomer::class, mappedBy="customer", cascade={"remove"})
*/
private $notifications;
/**
* @ORM\OneToMany(targetEntity=NotificationSystem::class, mappedBy="customer", cascade={"remove"})
*/
private $notificationsSystem;
/**
* @ORM\Column(type="boolean")
*/
private $blocked;
/**
* @ORM\Column(type="date")
*/
private $birth_date;
/**
* @ORM\Column(type="boolean")
*/
private $has_active_card;
private $activeFavorites;
public function __construct()
{
$this->favorites = new ArrayCollection();
$this->notificationCustomerSettings = new ArrayCollection();
$this->objectCustomers = new ArrayCollection();
$this->notificationSearches = new ArrayCollection();
}
private $first_name;
private $last_name;
private $mobile;
private $address;
private $companyName;
private $companyNumber;
private $wineFreight;
private $glprAcceptDate;
private $glprVersion;
public function getId(): ?int
{
return $this->id;
}
public function getCustomerNumber(): ?int
{
return $this->customerNumber;
}
public function setCustomerNumber(int $customerNumber): self
{
$this->customerNumber = $customerNumber;
return $this;
}
public function getVippsId(): ?string
{
return $this->vippsId;
}
public function setVippsId(?string $vippsId): self
{
$this->vippsId = $vippsId;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getLastLoginDate(): ?\DateTimeInterface
{
return $this->lastLoginDate;
}
public function setLastLoginDate(?\DateTimeInterface $lastLoginDate): self
{
$this->lastLoginDate = $lastLoginDate;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getFlag(): ?int
{
return $this->flag;
}
public function setFlag(int $flag): self
{
$this->flag = $flag;
return $this;
}
public function getObjectCustomers(): ArrayCollection
{
return $this->objectCustomers;
}
public function getBirthDate(): ?DateTimeInterface
{
return $this->birth_date;
}
public function setBirthDate(?DateTimeInterface $birth_date): self
{
$this->birth_date = $birth_date;
return $this;
}
public function getHasActiveCard(): ?bool
{
return $this->has_active_card;
}
public function setHasActiveCard(?bool $has_active_card): self
{
$this->has_active_card = $has_active_card;
return $this;
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps(): void
{
$now = new \DateTime('now');
$this->setUpdatedAt($now);
if (null === $this->getCreatedAt()) {
$this->setCreatedAt($now);
}
}
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 getMobile(): ?string
{
return $this->mobile;
}
public function setMobile(?string $mobile): self
{
$this->mobile = $mobile;
return $this;
}
public function getAddress(string $type = null): ?array
{
$addressTypes = ['invoice', 'delivery'];
if (!$this->address) {
return [];
}
if (
in_array($type, $addressTypes) && $address = array_column($this->address, null, 'type')[$type]
) {
return $address;
}
return $this->address;
}
public function setAddress(?array $address): self
{
$this->address = array_values(array_map(fn ($add) => [
'type' => $this->getType($add->Type),
'address' => $add->Adresse,
'address2' => $add->Adresse2 ?? null,
'co' => $add->Co ?? null,
'post_code' => $add->Postnr ?? null,
'post_office' => $add->Poststed ?? null,
'country_code' => isset($add->land) ? $add->land->Landkode : null,
'country' => isset($add->land) ? $add->land->Landbeskrivelse : null,
'full_address' => $add->full_address ?? null,
], array_filter($address, fn ($data) => isset($data->Adresse))));
return $this;
}
public function getLanguage(): ?string
{
return $this->language;
}
public function setLanguage(?string $language): self
{
$this->language = $language;
return $this;
}
public function getRequestDeleteDate(): ?\DateTimeInterface
{
return $this->requestDeleteDate;
}
public function setRequestDeleteDate(?\DateTimeInterface $requestDeleteDate): self
{
$this->requestDeleteDate = $requestDeleteDate;
return $this;
}
public function getAcceptedTermDate(): ?\DateTimeInterface
{
return $this->acceptedTermDate;
}
public function setAcceptedTermDate(?\DateTimeInterface $acceptedTermDate): self
{
$this->acceptedTermDate = $acceptedTermDate;
return $this;
}
public function getCompanyName(): ?string
{
return $this->companyName;
}
public function setCompanyName(?string $companyName): self
{
$this->companyName = $companyName;
return $this;
}
public function getCompanyNumber(): ?string
{
return $this->companyNumber;
}
public function setCompanyNumber(?string $companyNumber): self
{
$this->companyNumber = $companyNumber;
return $this;
}
public function getWineFreight(): ?array
{
return $this->wineFreight;
}
public function setWineFreight(?object $wineFreight): self
{
if($wineFreight){
$this->wineFreight = [
'id' => $wineFreight->id,
'name' => $wineFreight->Name,
'description' => $wineFreight->Description,
'base_price' => $wineFreight->BasePrice,
'price' => $wineFreight->Price,
];
}else{
$this->wineFreight = null;
}
return $this;
}
public function getGlprAcceptDate(): ?\DateTimeInterface
{
return $this->glprAcceptDate;
}
public function setGlprAcceptDate(?\DateTimeInterface $glprAcceptDate): self
{
$this->glprAcceptDate = $glprAcceptDate;
return $this;
}
public function getGlprVersion(): ?string
{
return $this->glprVersion;
}
public function setGlprVersion(?string $glprVersion): self
{
$this->glprVersion = $glprVersion;
return $this;
}
public function getBlocked(): ?bool
{
return $this->blocked;
}
public function setBlocked(?bool $blocked): self
{
$this->blocked = $blocked;
return $this;
}
public function mapping(object $data): object
{
$this->setCustomerNumber($data->Kundenr ?? null);
$this->setFirstName($data->Fnavn ?? null);
$this->setLastName($data->Enavn ?? null);
$this->setName($data->Fnavn ?? null . ' ' . $data->Enavn ?? null);
$this->setEmail($data->Email ?? null);
$this->setMobile($data->Mobil ?? null);
if (isset($data->Fodt)) {
$this->setBirthDate(DateTime::createFromFormat('Y-m-d', $data->Fodt));
}
$this->setLanguage($data->Sprak ?? null);
$this->setCompanyName($data->FirmaNavn ?? null);
$this->setCompanyNumber($data->Mvanr ?? null);
$this->setWineFreight($data->vinfrakt ?? null);
$this->setGlprAcceptDate(Helper::dateTimeCreate($data->Gdprdato ?? null));
$this->setGlprVersion($data->Gdprversjon ?? null);
if (isset($data->kundeadresse)) {
$this->setAddress($data->kundeadresse);
}
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password ?? '';
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getToken(): ?string
{
return $this->token;
}
public function setToken(string $token): self
{
$this->token = $token;
return $this;
}
public function getRefreshToken(): ?string
{
return $this->refreshToken;
}
public function setRefreshToken(string $refreshToken): self
{
$this->refreshToken = $refreshToken;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
/**
* @return Collection|NotificationSearch[]
*/
public function getNotificationSearches(): Collection
{
return $this->notificationSearches;
}
public function addNotificationSearch(NotificationSearch $notificationSearch): self
{
if (!$this->notificationSearches->contains($notificationSearch)) {
$this->notificationSearches[] = $notificationSearch;
$notificationSearch->setCustomer($this);
}
return $this;
}
public function removeNotificationSearch(NotificationSearch $notificationSearch): self
{
if ($this->notificationSearches->removeElement($notificationSearch)) {
// set the owning side to null (unless already changed)
if ($notificationSearch->getCustomer() === $this) {
$notificationSearch->setCustomer(null);
}
}
return $this;
}
/**
* @return Collection|Favorite[]
*/
public function getFavorites(): Collection
{
return $this->favorites;
}
public function addFavorite(Favorite $favorite): self
{
if (!$this->favorites->contains($favorite)) {
$favorite->setCustomer($this);
$this->favorites[] = $favorite;
}
return $this;
}
public function removeFavorite(Favorite $favorite): self
{
if ($this->favorites->removeElement($favorite)) {
// set the owning side to null (unless already changed)
if ($favorite->getCustomer() === $this) {
$favorite->setCustomer(null);
}
}
return $this;
}
/**
* @return Collection|NotificationCustomerSetting[]
*/
public function getNotificationCustomerSettings(): Collection
{
return $this->notificationCustomerSettings;
}
public function addNotificationCustomerSetting(NotificationCustomerSetting $notificationCustomerSetting): self
{
if (!$this->notificationCustomerSettings->contains($notificationCustomerSetting)) {
$this->notificationCustomerSettings[] = $notificationCustomerSetting;
$notificationCustomerSetting->setCustomer($this);
}
return $this;
}
public function removeNotificationCustomerSetting(NotificationCustomerSetting $notificationCustomerSetting): self
{
if ($this->notificationCustomerSettings->removeElement($notificationCustomerSetting)) {
// set the owning side to null (unless already changed)
if ($notificationCustomerSetting->getCustomer() === $this) {
$notificationCustomerSetting->setCustomer(null);
}
}
return $this;
}
public function getIsVerified(): ?bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
private function getType(string $type): string
{
switch ($type) {
case 'levering':
return 'delivery';
case 'faktura':
return 'invoice';
default:
return 'undefined';
}
}
public function getAcceptedTerm(): bool
{
return $this->acceptedTermDate instanceof DateTime && self::TERM_VERSION_DATE <= $this->acceptedTermDate->format('Y-m-d');
}
public function getTotalNotifications(): int
{
if (!$this->notificationsSystem) {
return 0;
}
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('status', NotificationSystem::NOTIFICATION_SYSTEM_DONT_READ));
return $this->notificationsSystem->matching($criteria)->count();
}
public function getFavoritesIds(): array
{
if (!$this->getFavorites()) {
return [];
}
return array_map(fn (Favorite $favorite) => $favorite->getObjectId(), $this->getFavorites()->toArray());
}
public function getAge(): ?int
{
if (!$this->birth_date) {
return null;
}
$now = new DateTime();
$difference = $now->diff($this->birth_date);
$age = $difference->y;
return $age;
}
public function getActiveFavorites(): ?array
{
return $this->activeFavorites;
}
public function setActiveFavorites(?array $activeFavorites): self
{
$this->activeFavorites = $activeFavorites;
return $this;
}
public function isAddressValid(): bool
{
$address = $this->getAddress('invoice');
if (!$address) {
return false;
}
if (empty($address['address']) OR empty($address['post_code']) OR empty($address['post_office']) OR empty($address['country_code'])) {
return false;
}
return true;
}
}