Newer
Older
<?php
/**
* This file contains QUI\ERP\User
*/
namespace QUI\ERP;
use QUI;
use QUI\ERP\Customer\NumberRange as CustomerNumberRange;
use QUI\Groups\Group;
use QUI\Interfaces\Users\User as UserInterface;
use function array_filter;
use function array_flip;
use function explode;
use function get_class;
use function is_array;
use function is_bool;
use function is_string;
/**
* Class User
* ERP User, a user object compatible to the QUIQQER User Interface
*
* @package QUI\ERP
*/
class User extends QUI\QDOM implements UserInterface
{
/**
* @var int
*/
/**
* @var string
*/
/**
* @var string
*/
/**
* @var string
*/
/**
* @var string
*/
/**
* @var string
*/
/**
* @var bool
*/
/**
* @var bool|null
*/
/**
* Address data
*
* @var array
*/
/**
* User constructor.
*
* @param array $attributes
* @throws QUI\ERP\Exception
*/
public function __construct(array $attributes)
{

Henning Leutz
committed
$needle = $this->getNeedles();
foreach ($needle as $attribute) {
if (!isset($attributes[$attribute])) {
throw new QUI\ERP\Exception(
);
}
}
if (!isset($attributes['id']) && !isset($attributes['uuid'])) {
throw new QUI\ERP\Exception(
'Missing attribute: id or uuid'
);
}
$this->isCompany = !empty($attributes['isCompany']) || !empty($attributes['company']);
$this->lang = $attributes['lang'];
$this->username = $attributes['username'];
$this->firstName = $attributes['firstname'];

Henning Leutz
committed
if ($attributes['country'] instanceof QUI\Countries\Country) {
$this->country = $attributes['country']->getCode();
} elseif (is_string($attributes['country'])) {
$this->country = $attributes['country'];
} elseif (is_array($attributes['country']) && !empty($attributes['country']['code'])) {
$this->country = $attributes['country']['code'];

Henning Leutz
committed
}
if (isset($attributes['id'])) {
$this->id = $attributes['id'];
}
if (isset($attributes['uuid'])) {
$this->uuid = $attributes['uuid'];
}
if (empty($this->uuid)) {
$this->uuid = $this->id;
}
if (isset($attributes['data']) && is_array($attributes['data'])) {

Patrick Müller
committed
$this->setAttributes($attributes['data']);
unset($attributes['data']);
if (isset($attributes['address']) && is_array($attributes['address'])) {
$this->address = $attributes['address'];
}

Henning Leutz
committed

Henning Leutz
committed
foreach ($attributes as $attribute => $value) {
if (!isset($needle[$attribute])) {
$this->setAttribute($attribute, $value);
}
}
$this->isNetto = $this->isNetto();

Henning Leutz
committed
}
/**
* Return the list of the needled attributes
* @return array
*/
public static function getNeedles(): array

Henning Leutz
committed
{

Henning Leutz
committed
'country',
'username',
'firstname',
'lastname',
'lang',
'isCompany'

Henning Leutz
committed
}
/**
* @param array $attributes - array('attribute' => 'value')
* @return array
*/
public static function getMissingAttributes(array $attributes): array

Henning Leutz
committed
{

Henning Leutz
committed
$needles = self::getNeedles();
foreach ($needles as $needle) {
if (!isset($attributes[$needle])) {
$missing[] = $needle;
}
}
return $missing;
}
/**
* Convert a User to an ERP user
*
* @return self
public static function convertUserToErpUser(UserInterface $User): User
{
$Country = $User->getCountry();
$country = '';
if ($Country) {
$country = $Country->getCode();
}
$address = false;
if (!QUI::getUsers()->isNobodyUser($User) && !QUI::getUsers()->isSystemUser($User)) {
/* @var $Address QUI\Users\Address */
$Address = $User->getStandardAddress();
if ($Address) {
$address = $Address->getAttributes();
}

Patrick Müller
committed
$data = $User->getAttributes();
unset($data['extra']);
'country' => $country,
'username' => $User->getUsername(),
'firstname' => $User->getAttribute('firstname'),
'lastname' => $User->getAttribute('lastname'),
'lang' => $User->getLang(),
'isCompany' => $User->isCompany(),
'isNetto' => $User->getAttribute('quiqqer.erp.isNettoUser'),
'data' => $data,
'address' => $address,
'quiqqer.erp.euVatId' => $User->getAttribute('quiqqer.erp.euVatId'),
'quiqqer.erp.taxId' => $User->getAttribute('quiqqer.erp.taxId')
}
/**
* Convert user data to an ERP user
*
* @param array $user {{$user.uid, $user.aid}}
* @return self
*
* @throws QUI\ERP\Exception
*/
public static function convertUserDataToErpUser(array $user): User
{
if (!isset($user['uid'])) {
throw new QUI\ERP\Exception('Need uid param');
}
if (!isset($user['aid'])) {
throw new QUI\ERP\Exception('Need aid param');
}
try {
$Address = $User->getAddress($user['aid']);
} catch (QUI\Exception $Exception) {
throw new QUI\ERP\Exception(
$Exception->getMessage(),
$Exception->getCode()
);
}
$ERPUser = self::convertUserToErpUser($User);
$ERPUser->setAddress($Address);
return $ERPUser;
}
/**

Henning Leutz
committed
* @deprecated use getUUID()
{
return $this->id;
}

Henning Leutz
committed
* @deprecated use getUUID()
*/
public function getUniqueId(): string
{
return $this->getUUID();
}
/**
* @return string

Henning Leutz
committed
* @return string
$Address = $this->getAddress();
$salutation = $Address->getAttribute('salutation');
$firstName = $Address->getAttribute('firstname');
if (empty($firstName)) {
$firstName = $this->firstName;
}
$lastName = $Address->getAttribute('lastname');
if (empty($lastName)) {
$lastName = $this->lastName;
}
switch ($salutation) {
case 'Herr':
case 'mr':
$salutation = QUI::getLocale()->get('quiqqer/core', 'address.salutation.male');
break;
case 'Frau':
case 'mrs':
$salutation = QUI::getLocale()->get('quiqqer/core', 'address.salutation.female');
/**
* Return the company if the customer has a company
* if not, the user will be returned
*
* @return mixed
*/
{
if ($this->isCompany()) {
$Address = $this->getAddress();
$company = $Address->getAttribute('company');
if (!empty($company)) {
return $company;
}
}
return $this->getName();
}
/**
* @return string
*/
{
return $this->username;
}
/**
* @return string
*/
{
return $this->lang;
}
/**
{
$Locale = new QUI\Locale();
$Locale->setCurrent($this->getLang());
return $Locale;
}
/**
* @return array
*/
{
$attributes = parent::getAttributes();
$attributes['country'] = $this->getCountry();
$attributes['isCompany'] = $this->isCompany();

Henning Leutz
committed
$attributes['firstname'] = $this->getAttribute('firstname');
$attributes['lastname'] = $this->getAttribute('lastname');
$attributes['username'] = $this->getAttribute('username');
$attributes['address'] = $this->getAddress()->getAttributes();
if ($this->getAttribute('quiqqer.erp.euVatId')) {
$attributes['quiqqer.erp.euVatId'] = $this->getAttribute('quiqqer.erp.euVatId');
}
if ($this->getAttribute('quiqqer.erp.taxId')) {
$attributes['quiqqer.erp.taxId'] = $this->getAttribute('quiqqer.erp.taxId');
}
return $attributes;
}
/**
* @return string
*/
public function getAttribute(string $name): mixed
{
return match ($name) {
'firstname' => $this->firstName,
'lastname' => $this->lastName,
'country' => $this->getCountry(),
default => parent::getAttribute($name),
};
}
/**
* @return mixed
*/
}
/**
* @return mixed
*/
{
return 0;
}
/**
* Return the current address data
*
* @param int|string $id - only for the interface, has no effect
* @return Address
public function getAddress(int|string $id = 0): QUI\Users\Address
return new Address($this->address, $this);
public function getAddressList(): array
{
$Address = $this->getAddress();
return [$Address->getUUID() => $Address];
}
/**
* @param QUI\Users\Address $Address
*/
public function setAddress(QUI\Users\Address $Address): void
$this->address = json_decode($Address->toJSON(), true);
}
/**
public function getCountry(): ?QUI\Countries\Country

Henning Leutz
committed
if (!empty($this->address) && isset($this->address['country'])) {
try {
return QUI\Countries\Manager::get($this->address['country']);

Henning Leutz
committed
}

Henning Leutz
committed
}

Henning Leutz
committed
if (!empty($this->country)) {
try {
return QUI\Countries\Manager::get($this->country);

Henning Leutz
committed
}
}
return QUI\ERP\Defaults::getCountry();
}
/**
* @return bool
*/
{
return $this->isCompany;
}
/**
* @return bool
*/
try {
$Package = QUI::getPackage('quiqqer/erp');
$Config = $Package->getConfig();
if ($Config->getValue('general', 'businessType') === 'B2B') {
return QUI\ERP\Utils\User::IS_NETTO_USER;
}
if ($this->existsAttribute('erp.isNettoUser')) {
return (int)$this->getAttribute('erp.isNettoUser') === QUI\ERP\Utils\User::IS_NETTO_USER;
if ($this->existsAttribute('quiqqer.erp.isNettoUser')) {
return (int)$this->getAttribute('quiqqer.erp.isNettoUser') === QUI\ERP\Utils\User::IS_NETTO_USER;
if ($this->isNetto === null) {
$this->isNetto = QUI\ERP\Utils\User::getBruttoNettoUserStatus($this) === QUI\ERP\Utils\User::IS_NETTO_USER;
}
return $this->isNetto;
}
/**
* @return bool
*/
public function hasBruttoNettoStatus(): bool
/**
* @return mixed
*/
{
return false;
}
/**
* @return mixed
*/
public function isInGroup(int|string $groupId): bool
{
return false;
}
/**
* @return mixed
*/
{
return false;
}
/**
* Does nothing
*/
public function logout(): void
{
}
/**
* @param string $code
* @param UserInterface|null $PermissionUser
public function activate(string $code = '', ?QUI\Interfaces\Users\User $PermissionUser = null): bool
{
return true;
}
/**
* @param UserInterface|null $PermissionUser
* @return bool
public function deactivate(?UserInterface $PermissionUser = null): bool
{
return true;
}
/**
* @param UserInterface|null $PermissionUser
* @return true
public function disable(UserInterface|null $PermissionUser = null): bool
{
return true;
}
/**
* Does nothing
* @param UserInterface|null $PermissionUser
public function save(?UserInterface $PermissionUser = null): void
{
}
/**
* @param UserInterface|null $PermissionUser
* @return bool
public function delete(?UserInterface $PermissionUser = null): bool
{
return false;
}
/**
* This user has nowhere permissions
*
* @param string $right
* @return bool
*/
public function getPermission(string $right, bool|array|string|callable $ruleset = false): bool
{
return false;
}
/**
* @return Address
public function getStandardAddress(): Address
{
return $this->getAddress();
}
public function addAddress(array $params = [], QUI\Interfaces\Users\User $ParentUser = null): ?Address
/**
* Does nothing
* @param array|string $groups
*/
public function setGroups(array|string $groups)
{
}
/**
public function getGroups(bool $array = true): array
$groupIds = $this->getAttribute('usergroup');
if (empty($groupIds)) {
return [];
}
if (!is_array($groupIds)) {
$groupIds = explode(',', $groupIds);
$groupIds = array_filter($groupIds, function ($groupId) {
return !empty($groupId);
});
return $groupIds;
}
$groups = [];
foreach ($groupIds as $groupId) {
try {
$groups[] = QUI::getGroups()->get($groupId);
} catch (\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
}
return $groups;
}
/**
* This user has no avatar, it returned the default placeholder image
*
* @throws QUI\Exception
public function getAvatar(): ?QUI\Projects\Media\Image
{
return QUI::getProjectManager()
->getStandard()
->getMedia()
->getPlaceholderImage();
}
/**
* Does nothing
* @param string $new
* @param UserInterface|null $PermissionUser
public function setPassword(string $new, ?UserInterface $PermissionUser = null)
{
}
public function changePassword(string $newPassword, string $oldPassword, UserInterface $ParentUser = null): void
/**
* Does nothing
* @param string $pass
* @param bool $encrypted
*/
public function checkPassword(string $pass, bool $encrypted = false)
{
}
/**
* @return bool
*/
{
return false;
}
/**
* @return bool
*/
{
return true;
}
/**
* @return bool
*/
{
return false;
}
/**
* Does nothing
* @param bool $status
*/
public function setCompanyStatus(bool $status)
{
}
/**
* Does nothing
* @param int $groupId
*/
{
}
/**
* Does nothing
public function removeGroup(Group|int $Group)
{
}
/**
* Does nothing
*/
public function refresh()
{
}
// region Special ERP User API
/**
* Get customer no. of this ERP User.
*
* @return string
*/
public function getCustomerNo(): string
{
$customerId = $this->getAttribute('customerId');
if (empty($customerId)) {
return '';
}
$NumberRange = new CustomerNumberRange();
return $NumberRange->getCustomerNoPrefix() . $customerId;
}
/**
* Get supplier no. of this ERP User.
*
* @return string
*/
{
$supplierNo = $this->getAttribute('supplierId');
if (empty($supplierNo)) {
return '';
}
return $supplierNo;
}
// endregion
// region authenticator
public function hasAuthenticator(string $authenticator): bool
{
return false;
}
public function getAuthenticators(): array
{
return [];
}
public function getAuthenticator(string $authenticator): AuthenticatorInterface
{
throw new QUI\Users\Exception(
['quiqqer/core', 'exception.authenticator.not.found'],
404
);
}
public function enableAuthenticator(string $authenticator, UserInterface $ParentUser = null): void
{
throw new QUI\Users\Exception(
['quiqqer/core', 'exception.authenticator.not.found'],
404
);
}
public function disableAuthenticator(string $authenticator, UserInterface $ParentUser = null): void
{
throw new QUI\Users\Exception(
['quiqqer/core', 'exception.authenticator.not.found'],
404
);
}
//endregion