src/Entity/Customer.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CustomerRepository;
  4. use DateTime;
  5. use DateTimeInterface;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Doctrine\Common\Collections\Criteria;
  10. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use App\Helper\Helper;
  13. /**
  14.  * @ORM\Entity(repositoryClass=CustomerRepository::class)
  15.  * @ORM\HasLifecycleCallbacks
  16.  */
  17. class Customer extends BaseEntity implements UserInterfacePasswordAuthenticatedUserInterface
  18. {
  19.     public const TERM_VERSION_DATE '2022-01-01';
  20.     public const FLAG_NATIVE       ** 0;
  21.     public const FLAG_LEGACY       ** 1;
  22.     /**
  23.      * @ORM\Id
  24.      * @ORM\GeneratedValue
  25.      * @ORM\Column(type="integer")
  26.      */
  27.     private $id;
  28.     /**
  29.      * @ORM\Column(type="integer")
  30.      */
  31.     private $customerNumber;
  32.     /**
  33.      * @ORM\Column(type="string", length=255, nullable=true)
  34.      */
  35.     private $vippsId;
  36.     /**
  37.      * @ORM\Column(type="string", length=180, unique=true)
  38.      */
  39.     private $email;
  40.     /**
  41.      * @ORM\Column(type="json")
  42.      */
  43.     private $roles = [];
  44.     /**
  45.      * @var string The hashed password
  46.      * @ORM\Column(type="string", nullable=true)
  47.      */
  48.     private $password;
  49.     /**
  50.      * @var string The authenticated JWT token
  51.      */
  52.     private $token;
  53.     /**
  54.      * @var string The authenticated JWT token
  55.      */
  56.     private $refreshToken;
  57.     /**
  58.      * @ORM\Column(type="datetime", nullable=true)
  59.      */
  60.     private $lastLoginDate;
  61.     /**
  62.      * @ORM\Column(type="datetime")
  63.      */
  64.     private $createdAt;
  65.     /**
  66.      * @ORM\Column(type="datetime")
  67.      */
  68.     private $updatedAt;
  69.     /**
  70.      * @ORM\Column(type="string", length=180, nullable=true)
  71.      */
  72.     private $name;
  73.     /**
  74.      * @ORM\Column(type="integer", options={"default" : 0})
  75.      */
  76.     private $flag;
  77.     /**
  78.      * @ORM\OneToMany(targetEntity=NotificationCustomerSetting::class, mappedBy="customer_id", cascade={"persist"})
  79.      */
  80.     private $notificationCustomerSettings;
  81.     /**
  82.      * @ORM\OneToMany(targetEntity="ObjectCustomer", mappedBy="customer")
  83.      */
  84.     private $objectCustomers;
  85.     /**
  86.      * @ORM\OneToMany(targetEntity=Favorite::class, mappedBy="customer", orphanRemoval=true)
  87.      */
  88.     private $favorites;
  89.     /**
  90.      * @ORM\OneToMany(targetEntity=NotificationSearch::class, mappedBy="customer")
  91.      */
  92.     private $notificationSearches;
  93.     /**
  94.      * @ORM\Column(type="boolean")
  95.      */
  96.     private $isVerified false;
  97.     /**
  98.      * @ORM\Column(type="string", length=20, nullable=true)
  99.      */
  100.     private $language;
  101.     /**
  102.      * @ORM\Column(type="datetime")
  103.      */
  104.     private $requestDeleteDate;
  105.     /**
  106.      * @ORM\Column(type="datetime")
  107.      */
  108.     private $acceptedTermDate;
  109.     /**
  110.      * @ORM\OneToMany(targetEntity=NotificationCustomer::class, mappedBy="customer", cascade={"remove"})
  111.      */
  112.     private $notifications;
  113.     /**
  114.      * @ORM\OneToMany(targetEntity=NotificationSystem::class, mappedBy="customer", cascade={"remove"})
  115.      */
  116.     private $notificationsSystem;
  117.     /**
  118.      * @ORM\Column(type="boolean")
  119.      */
  120.     private $blocked;
  121.     /**
  122.      * @ORM\Column(type="date")
  123.      */
  124.     private $birth_date;
  125.     /**
  126.      * @ORM\Column(type="boolean")
  127.      */
  128.     private $has_active_card;
  129.     private $activeFavorites;
  130.     public function __construct()
  131.     {
  132.         $this->favorites                    = new ArrayCollection();
  133.         $this->notificationCustomerSettings = new ArrayCollection();
  134.         $this->objectCustomers              = new ArrayCollection();
  135.         $this->notificationSearches         = new ArrayCollection();
  136.     }
  137.     private $first_name;
  138.     private $last_name;
  139.     private $mobile;
  140.     private $address;
  141.     private $companyName;
  142.     private $companyNumber;
  143.     private $wineFreight;
  144.     private $glprAcceptDate;
  145.     private $glprVersion;
  146.     public function getId(): ?int
  147.     {
  148.         return $this->id;
  149.     }
  150.     public function getCustomerNumber(): ?int
  151.     {
  152.         return $this->customerNumber;
  153.     }
  154.     public function setCustomerNumber(int $customerNumber): self
  155.     {
  156.         $this->customerNumber $customerNumber;
  157.         return $this;
  158.     }
  159.     public function getVippsId(): ?string
  160.     {
  161.         return $this->vippsId;
  162.     }
  163.     public function setVippsId(?string $vippsId): self
  164.     {
  165.         $this->vippsId $vippsId;
  166.         return $this;
  167.     }
  168.     public function getEmail(): ?string
  169.     {
  170.         return $this->email;
  171.     }
  172.     public function setEmail(string $email): self
  173.     {
  174.         $this->email $email;
  175.         return $this;
  176.     }
  177.     public function getLastLoginDate(): ?\DateTimeInterface
  178.     {
  179.         return $this->lastLoginDate;
  180.     }
  181.     public function setLastLoginDate(?\DateTimeInterface $lastLoginDate): self
  182.     {
  183.         $this->lastLoginDate $lastLoginDate;
  184.         return $this;
  185.     }
  186.     public function getCreatedAt(): ?\DateTimeInterface
  187.     {
  188.         return $this->createdAt;
  189.     }
  190.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  191.     {
  192.         $this->createdAt $createdAt;
  193.         return $this;
  194.     }
  195.     public function getUpdatedAt(): ?\DateTimeInterface
  196.     {
  197.         return $this->updatedAt;
  198.     }
  199.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  200.     {
  201.         $this->updatedAt $updatedAt;
  202.         return $this;
  203.     }
  204.     public function getName(): ?string
  205.     {
  206.         return $this->name;
  207.     }
  208.     public function setName(string $name): self
  209.     {
  210.         $this->name $name;
  211.         return $this;
  212.     }
  213.     public function getFlag(): ?int
  214.     {
  215.         return $this->flag;
  216.     }
  217.     public function setFlag(int $flag): self
  218.     {
  219.         $this->flag $flag;
  220.         return $this;
  221.     }
  222.     public function getObjectCustomers(): ArrayCollection
  223.     {
  224.         return $this->objectCustomers;
  225.     }
  226.     public function getBirthDate(): ?DateTimeInterface
  227.     {
  228.         return $this->birth_date;
  229.     }
  230.     public function setBirthDate(?DateTimeInterface $birth_date): self
  231.     {
  232.         $this->birth_date $birth_date;
  233.         return $this;
  234.     }
  235.     public function getHasActiveCard(): ?bool
  236.     {
  237.         return $this->has_active_card;
  238.     }
  239.     public function setHasActiveCard(?bool $has_active_card): self
  240.     {
  241.         $this->has_active_card $has_active_card;
  242.         return $this;
  243.     }
  244.     /**
  245.      * @ORM\PrePersist
  246.      * @ORM\PreUpdate
  247.      */
  248.     public function updatedTimestamps(): void
  249.     {
  250.         $now = new \DateTime('now');
  251.         $this->setUpdatedAt($now);
  252.         if (null === $this->getCreatedAt()) {
  253.             $this->setCreatedAt($now);
  254.         }
  255.     }
  256.     public function getFirstName(): ?string
  257.     {
  258.         return $this->first_name;
  259.     }
  260.     public function setFirstName(?string $first_name): self
  261.     {
  262.         $this->first_name $first_name;
  263.         return $this;
  264.     }
  265.     public function getLastName(): ?string
  266.     {
  267.         return $this->last_name;
  268.     }
  269.     public function setLastName(?string $last_name): self
  270.     {
  271.         $this->last_name $last_name;
  272.         return $this;
  273.     }
  274.     public function getMobile(): ?string
  275.     {
  276.         return $this->mobile;
  277.     }
  278.     public function setMobile(?string $mobile): self
  279.     {
  280.         $this->mobile $mobile;
  281.         return $this;
  282.     }
  283.     public function getAddress(string $type null): ?array
  284.     {
  285.         $addressTypes = ['invoice''delivery'];
  286.         if (!$this->address) {
  287.             return [];
  288.         }
  289.         if (
  290.             in_array($type$addressTypes) && $address array_column($this->addressnull'type')[$type]
  291.         ) {
  292.             return $address;
  293.         }
  294.         return $this->address;
  295.     }
  296.     public function setAddress(?array $address): self
  297.     {
  298.         $this->address array_values(array_map(fn ($add) => [
  299.             'type'         => $this->getType($add->Type),
  300.             'address'      => $add->Adresse,
  301.             'address2'     => $add->Adresse2 ?? null,
  302.             'co'           => $add->Co ?? null,
  303.             'post_code'    => $add->Postnr ?? null,
  304.             'post_office'  => $add->Poststed ?? null,
  305.             'country_code' => isset($add->land) ? $add->land->Landkode null,
  306.             'country'      => isset($add->land) ? $add->land->Landbeskrivelse null,
  307.             'full_address' => $add->full_address ?? null,
  308.         ], array_filter($address, fn ($data) => isset($data->Adresse))));
  309.         return $this;
  310.     }
  311.     public function getLanguage(): ?string
  312.     {
  313.         return $this->language;
  314.     }
  315.     public function setLanguage(?string $language): self
  316.     {
  317.         $this->language $language;
  318.         return $this;
  319.     }
  320.     public function getRequestDeleteDate(): ?\DateTimeInterface
  321.     {
  322.         return $this->requestDeleteDate;
  323.     }
  324.     public function setRequestDeleteDate(?\DateTimeInterface $requestDeleteDate): self
  325.     {
  326.         $this->requestDeleteDate $requestDeleteDate;
  327.         return $this;
  328.     }
  329.     public function getAcceptedTermDate(): ?\DateTimeInterface
  330.     {
  331.         return $this->acceptedTermDate;
  332.     }
  333.     public function setAcceptedTermDate(?\DateTimeInterface $acceptedTermDate): self
  334.     {
  335.         $this->acceptedTermDate $acceptedTermDate;
  336.         return $this;
  337.     }
  338.     public function getCompanyName(): ?string
  339.     {
  340.         return $this->companyName;
  341.     }
  342.     public function setCompanyName(?string $companyName): self
  343.     {
  344.         $this->companyName $companyName;
  345.         return $this;
  346.     }
  347.     public function getCompanyNumber(): ?string
  348.     {
  349.         return $this->companyNumber;
  350.     }
  351.     public function setCompanyNumber(?string $companyNumber): self
  352.     {
  353.         $this->companyNumber $companyNumber;
  354.         return $this;
  355.     }
  356.     public function getWineFreight(): ?array
  357.     {
  358.         return $this->wineFreight;
  359.     }
  360.     public function setWineFreight(?object $wineFreight): self
  361.     {
  362.         if($wineFreight){
  363.             $this->wineFreight = [
  364.                 'id'          => $wineFreight->id,
  365.                 'name'        => $wineFreight->Name,
  366.                 'description' => $wineFreight->Description,
  367.                 'base_price'  => $wineFreight->BasePrice,
  368.                 'price'       => $wineFreight->Price,
  369.             ];
  370.         }else{
  371.             $this->wineFreight null;
  372.         }
  373.         return $this;
  374.     }
  375.     public function getGlprAcceptDate(): ?\DateTimeInterface
  376.     {
  377.         return $this->glprAcceptDate;
  378.     }
  379.     public function setGlprAcceptDate(?\DateTimeInterface $glprAcceptDate): self
  380.     {
  381.         $this->glprAcceptDate $glprAcceptDate;
  382.         return $this;
  383.     }
  384.     public function getGlprVersion(): ?string
  385.     {
  386.         return $this->glprVersion;
  387.     }
  388.     public function setGlprVersion(?string $glprVersion): self
  389.     {
  390.         $this->glprVersion $glprVersion;
  391.         return $this;
  392.     }
  393.     public function getBlocked(): ?bool
  394.     {
  395.         return $this->blocked;
  396.     }
  397.     public function setBlocked(?bool $blocked): self
  398.     {
  399.         $this->blocked $blocked;
  400.         return $this;
  401.     }
  402.     public function mapping(object $data): object
  403.     {
  404.         $this->setCustomerNumber($data->Kundenr ?? null);
  405.         $this->setFirstName($data->Fnavn ?? null);
  406.         $this->setLastName($data->Enavn ?? null);
  407.         $this->setName($data->Fnavn ?? null ' ' $data->Enavn ?? null);
  408.         $this->setEmail($data->Email ?? null);
  409.         $this->setMobile($data->Mobil ?? null);
  410.         if (isset($data->Fodt)) {
  411.             $this->setBirthDate(DateTime::createFromFormat('Y-m-d'$data->Fodt));
  412.         }
  413.         $this->setLanguage($data->Sprak ?? null);
  414.         $this->setCompanyName($data->FirmaNavn ?? null);
  415.         $this->setCompanyNumber($data->Mvanr ?? null);
  416.         $this->setWineFreight($data->vinfrakt ?? null);
  417.         $this->setGlprAcceptDate(Helper::dateTimeCreate($data->Gdprdato ?? null));
  418.         $this->setGlprVersion($data->Gdprversjon ?? null);
  419.         if (isset($data->kundeadresse)) {
  420.             $this->setAddress($data->kundeadresse);
  421.         }
  422.         return $this;
  423.     }
  424.     /**
  425.      * A visual identifier that represents this user.
  426.      *
  427.      * @see UserInterface
  428.      */
  429.     public function getUserIdentifier(): string
  430.     {
  431.         return (string) $this->email;
  432.     }
  433.     /**
  434.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  435.      */
  436.     public function getUsername(): string
  437.     {
  438.         return (string) $this->email;
  439.     }
  440.     /**
  441.      * @see UserInterface
  442.      */
  443.     public function getRoles(): array
  444.     {
  445.         $roles $this->roles;
  446.         // guarantee every user at least has ROLE_USER
  447.         $roles[] = 'ROLE_USER';
  448.         return array_unique($roles);
  449.     }
  450.     public function setRoles(array $roles): self
  451.     {
  452.         $this->roles $roles;
  453.         return $this;
  454.     }
  455.     /**
  456.      * @see PasswordAuthenticatedUserInterface
  457.      */
  458.     public function getPassword(): string
  459.     {
  460.         return $this->password ?? '';
  461.     }
  462.     public function setPassword(string $password): self
  463.     {
  464.         $this->password $password;
  465.         return $this;
  466.     }
  467.     public function getToken(): ?string
  468.     {
  469.         return $this->token;
  470.     }
  471.     public function setToken(string $token): self
  472.     {
  473.         $this->token $token;
  474.         return $this;
  475.     }
  476.     public function getRefreshToken(): ?string
  477.     {
  478.         return $this->refreshToken;
  479.     }
  480.     public function setRefreshToken(string $refreshToken): self
  481.     {
  482.         $this->refreshToken $refreshToken;
  483.         return $this;
  484.     }
  485.     /**
  486.      * Returning a salt is only needed, if you are not using a modern
  487.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  488.      *
  489.      * @see UserInterface
  490.      */
  491.     public function getSalt(): ?string
  492.     {
  493.         return null;
  494.     }
  495.     /**
  496.      * @see UserInterface
  497.      */
  498.     public function eraseCredentials(): void
  499.     {
  500.         // If you store any temporary, sensitive data on the user, clear it here
  501.         // $this->plainPassword = null;
  502.     }
  503.     /**
  504.      * @return Collection|NotificationSearch[]
  505.      */
  506.     public function getNotificationSearches(): Collection
  507.     {
  508.         return $this->notificationSearches;
  509.     }
  510.     public function addNotificationSearch(NotificationSearch $notificationSearch): self
  511.     {
  512.         if (!$this->notificationSearches->contains($notificationSearch)) {
  513.             $this->notificationSearches[] = $notificationSearch;
  514.             $notificationSearch->setCustomer($this);
  515.         }
  516.         return $this;
  517.     }
  518.     public function removeNotificationSearch(NotificationSearch $notificationSearch): self
  519.     {
  520.         if ($this->notificationSearches->removeElement($notificationSearch)) {
  521.             // set the owning side to null (unless already changed)
  522.             if ($notificationSearch->getCustomer() === $this) {
  523.                 $notificationSearch->setCustomer(null);
  524.             }
  525.         }
  526.         return $this;
  527.     }
  528.     /**
  529.      * @return Collection|Favorite[]
  530.      */
  531.     public function getFavorites(): Collection
  532.     {
  533.         return $this->favorites;
  534.     }
  535.     public function addFavorite(Favorite $favorite): self
  536.     {
  537.         if (!$this->favorites->contains($favorite)) {
  538.             $favorite->setCustomer($this);
  539.             $this->favorites[] = $favorite;
  540.         }
  541.         return $this;
  542.     }
  543.     public function removeFavorite(Favorite $favorite): self
  544.     {
  545.         if ($this->favorites->removeElement($favorite)) {
  546.             // set the owning side to null (unless already changed)
  547.             if ($favorite->getCustomer() === $this) {
  548.                 $favorite->setCustomer(null);
  549.             }
  550.         }
  551.         return $this;
  552.     }
  553.     /**
  554.      * @return Collection|NotificationCustomerSetting[]
  555.      */
  556.     public function getNotificationCustomerSettings(): Collection
  557.     {
  558.         return $this->notificationCustomerSettings;
  559.     }
  560.     public function addNotificationCustomerSetting(NotificationCustomerSetting $notificationCustomerSetting): self
  561.     {
  562.         if (!$this->notificationCustomerSettings->contains($notificationCustomerSetting)) {
  563.             $this->notificationCustomerSettings[] = $notificationCustomerSetting;
  564.             $notificationCustomerSetting->setCustomer($this);
  565.         }
  566.         return $this;
  567.     }
  568.     public function removeNotificationCustomerSetting(NotificationCustomerSetting $notificationCustomerSetting): self
  569.     {
  570.         if ($this->notificationCustomerSettings->removeElement($notificationCustomerSetting)) {
  571.             // set the owning side to null (unless already changed)
  572.             if ($notificationCustomerSetting->getCustomer() === $this) {
  573.                 $notificationCustomerSetting->setCustomer(null);
  574.             }
  575.         }
  576.         return $this;
  577.     }
  578.     public function getIsVerified(): ?bool
  579.     {
  580.         return $this->isVerified;
  581.     }
  582.     public function setIsVerified(bool $isVerified): self
  583.     {
  584.         $this->isVerified $isVerified;
  585.         return $this;
  586.     }
  587.     private function getType(string $type): string
  588.     {
  589.         switch ($type) {
  590.             case 'levering':
  591.                 return 'delivery';
  592.             case 'faktura':
  593.                 return 'invoice';
  594.             default:
  595.                 return 'undefined';
  596.         }
  597.     }
  598.     public function getAcceptedTerm(): bool
  599.     {
  600.         return $this->acceptedTermDate instanceof DateTime && self::TERM_VERSION_DATE <= $this->acceptedTermDate->format('Y-m-d');
  601.     }
  602.     public function getTotalNotifications(): int
  603.     {
  604.         if (!$this->notificationsSystem) {
  605.             return 0;
  606.         }
  607.         $criteria Criteria::create();
  608.         $criteria->where(Criteria::expr()->eq('status'NotificationSystem::NOTIFICATION_SYSTEM_DONT_READ));
  609.         return $this->notificationsSystem->matching($criteria)->count();
  610.     }
  611.     public function getFavoritesIds(): array
  612.     {
  613.         if (!$this->getFavorites()) {
  614.             return [];
  615.         }
  616.         return array_map(fn (Favorite $favorite) => $favorite->getObjectId(), $this->getFavorites()->toArray());
  617.     }
  618.     public function getAge(): ?int
  619.     {
  620.         if (!$this->birth_date) {
  621.             return null;
  622.         }
  623.         $now = new DateTime();
  624.         $difference $now->diff($this->birth_date);
  625.         $age $difference->y;
  626.         return  $age;
  627.     }
  628.     public function getActiveFavorites(): ?array
  629.     {
  630.         return $this->activeFavorites;
  631.     }
  632.     public function setActiveFavorites(?array $activeFavorites): self
  633.     {
  634.         $this->activeFavorites $activeFavorites;
  635.         return $this;
  636.     }
  637.     public function isAddressValid(): bool
  638.     {
  639.         $address $this->getAddress('invoice');
  640.         if (!$address) {
  641.             return false;
  642.         }
  643.         if (empty($address['address']) OR empty($address['post_code']) OR empty($address['post_office']) OR empty($address['country_code'])) {
  644.             return false;
  645.         }
  646.         return true;
  647.     }
  648. }