Newer
Older
<?php
/**
* This file contains QUI\ERP\EventHandler
*/
namespace QUI\ERP;
use QUI;

Patrick Müller
committed
use QUI\ERP\Products\Handler\Fields as ProductFields;
use QUI\Package\Package;
use function array_flip;
use function class_exists;
use function dirname;

Henning Leutz
committed
use function explode;
use function is_array;
use function is_string;
use function json_decode;
/**
* Class EventHandler
*
* @package QUI\ERP
*/
class EventHandler
{
/**
* event : on admin load footer
*/
public static function onAdminLoadFooter(): void
echo '<script src="' . URL_OPT_DIR . 'quiqqer/erp/bin/load.js"></script>';
/**
* @param QUI\Template $Template
*/
public static function onTemplateGetHeader(QUI\Template $Template): void

Henning Leutz
committed
try {
$Package = QUI::getPackage('quiqqer/erp');
$areas = $Package->getConfig()->get('general', 'customerRequestWindow');
if (empty($areas)) {
return;
}

Henning Leutz
committed
$areas = explode(',', $areas);

Henning Leutz
committed
return;
}

Henning Leutz
committed
$Template->extendHeaderWithJavaScriptFile(
URL_OPT_DIR . 'quiqqer/erp/bin/frontend.js'
);
}
/**
* event: on package setup
*
* @param Package $Package
public static function onPackageSetup(Package $Package): void
if ($Package->getName() !== 'quiqqer/erp') {
return;
}

Patrick Müller
committed
self::createDefaultManufacturerGroup();

Patrick Müller
committed
}
/**
* Create a default manufacturer group if none exists yet.
*
* @return void
*/
public static function createDefaultManufacturerGroup(): void

Patrick Müller
committed
{
try {
$Conf = QUI::getPackage('quiqqer/erp')->getConfig();

Patrick Müller
committed
$defaultGroupId = $Conf->get('manufacturers', 'groupId');
if (!empty($defaultGroupId)) {
return;
}
$Root = QUI::getGroups()->firstChild();
$Manufacturers = $Root->createChild(
QUI::getLocale()->get('quiqqer/erp', 'manufacturers.default_group_name'),
QUI::getUsers()->getSystemUser()
);
$Conf->setValue('manufacturers', 'groupId', $Manufacturers->getId());
$Conf->save();
$Manufacturers->activate();
// Add manufacturer group Id to product manufacturer field
if (QUI::getPackageManager()->isInstalled('quiqqer/products')) {
try {
/** @var QUI\ERP\Products\Field\Types\GroupList $ProductField */
$ProductField = ProductFields::getField(ProductFields::FIELD_MANUFACTURER);
$groupIds = $ProductField->getOption('groupIds');

Patrick Müller
committed
if (empty($groupIds)) {
$groupIds = [];
}
$groupIds[] = $Manufacturers->getId();
$ProductField->setOption('groupIds', $groupIds);
$ProductField->save();
} catch (\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
}
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
/**
* Patches old bank account data structure to new one.
*
* @return void
* @throws QUI\Exception
*/
public static function patchBankAccount(): void
$Conf = QUI::getPackage('quiqqer/erp')->getConfig();
$bankAccountPatched = $Conf->getValue('bankAccounts', 'isPatched');
if (!empty($bankAccountPatched)) {
return;
}
// Create new bank account with existing details
$bankName = $Conf->get('company', 'bankName');
$bankIban = $Conf->get('company', 'bankIban');
$bankBic = $Conf->get('company', 'bankBic');
$companyName = $Conf->get('company', 'name');
if (empty($bankIban) || empty($bankBic) || empty($companyName)) {
$Conf->setValue('bankAccounts', 'isPatched', true);
$Conf->save();
return;
}
'name' => $bankName ?: $bankIban,
'iban' => $bankIban,
'bic' => $bankBic,
'title' => $companyName,
'accountHolder' => $companyName,
try {
$bankAccount = BankAccounts::addBankAccount($bankAccountData);
$Conf->setValue('company', 'bankAccountId', $bankAccount['id']);
QUI\System\Log::addInfo(
QUI::getLocale()->get(
'quiqqer/erp',
'bankAccounts.patch.bankAccount_created'
)
);
} catch (\Exception $Exception) {
QUI\System\Log::writeException(
$Exception,
QUI\System\Log::LEVEL_ERROR,
[
'operation' => 'Creating default bank account based on ecoyn bank / company config.'
]
);
$Conf->setValue('bankAccounts', 'isPatched', true);
/**
* @param Package $Package
* @param array $params
*/
public static function onPackageConfigSave(QUI\Package\Package $Package, array $params): void
{
if ($Package->getName() !== 'quiqqer/erp') {
return;
}
$languages = QUI::availableLanguages();
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
try {
$Config = $Package->getConfig();
// timestampFormat
if (isset($params['timestampFormat'])) {
foreach ($params['timestampFormat'] as $language => $format) {
if (isset($languages[$language])) {
$Config->setValue('timestampFormat', $language, $format);
}
}
}
// dateFormat
if (isset($params['dateFormat'])) {
foreach ($params['dateFormat'] as $language => $format) {
if (isset($languages[$language])) {
$Config->setValue('dateFormat', $language, $format);
}
}
}
$Config->save();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
}
* @param QUI\Interfaces\Users\User $User
* @todo prüfung auch für steuernummer
*
*/
public static function onUserSave(QUI\Interfaces\Users\User $User): void
{
if (!QUI::getUsers()->isUser($User)) {
return;
}
// eu vat id validation
try {
$validate = $Package->getConfig()->getValue('shop', 'validateVatId');
$vatId = $User->getAttribute('quiqqer.erp.euVatId');
if ($validate && !empty($vatId)) {
try {
$vatId = QUI\ERP\Tax\Utils::validateVatId($vatId);
} catch (QUI\ERP\Tax\Exception $Exception) {
if ($Exception->getCode() !== 503) {
throw $Exception;
}
$vatId = QUI\ERP\Tax\Utils::cleanupVatId($vatId);
}
} elseif ($vatId) {
$vatId = QUI\ERP\Tax\Utils::cleanupVatId($vatId);
}
$User->setAttribute('quiqqer.erp.euVatId', $vatId);
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
// netto brutto user status
// @todo im admin muss dieser schritt seperat gemacht werden
// @todo im admin muss festgelegt werden was der nutzer ist
// @todo das muss in das customer modul rein
// $User->setAttribute('quiqqer.erp.isNettoUser', false); // reset status
//
// $User->setAttribute(
// 'quiqqer.erp.isNettoUser',
// QUI\ERP\Utils\User::getBruttoNettoUserStatus($User)
// );
/**
* event: on user save
* saves the vat number
*
*
* @param QUI\Users\User $User
* @throws QUI\Exception
*/
public static function onUserSaveBegin(QUI\Users\User $User): void
{
if (!QUI::getUsers()->isUser($User)) {
return;
}
if (QUI::isBackend()) {
return;
}
$Request = QUI::getRequest()->request;
if (empty($data)) {
return;
}
if (isset($data['data'])) {
if (isset($data['company'])) {
try {
$Address = $User->getStandardAddress();
$Address->setAttribute(
'company',
QUI\Utils\Security\Orthos::clear($data['company'])
);
$Address->save();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
}
if (isset($data['vatId'])) {
$vatId = $data['vatId'];
&& QUI\ERP\Tax\Utils::shouldVatIdValidationBeExecuted()
$vatId = QUI\ERP\Tax\Utils::validateVatId($vatId);
}
// save VAT ID
$User->setAttribute('quiqqer.erp.euVatId', QUI\Utils\Security\Orthos::clear($vatId));
}
if (isset($data['chUID'])) {
$User->setAttribute('quiqqer.erp.chUID', QUI\Utils\Security\Orthos::clear($data['chUID']));
/**
* event: on user address save
*
* @param QUI\Users\Address $Address
* @param QUI\Users\User $User
*/
public static function onUserAddressSave(QUI\Users\Address $Address, QUI\Users\User $User): void
{
if (!QUI::getUsers()->isUser($User)) {
return;
}
$Request = QUI::getRequest()->request;
if (is_string($data)) {
$data = json_decode($data, true);
return;
}
if (isset($data['vatId'])) {
$vatId = $data['vatId'];
try {
&& QUI\ERP\Tax\Utils::shouldVatIdValidationBeExecuted()
$vatId = QUI\ERP\Tax\Utils::validateVatId($vatId);
}
// save VAT ID
$User->setAttribute('quiqqer.erp.euVatId', $vatId);
$User->save();
} catch (QUI\ERP\Tax\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
}
}
//region user profile extension
/**
* @param Collector $Collector
* @param QUI\Interfaces\Users\User $User
* @param QUI\Users\Address $Address
public static function onFrontendUserCustomerBegin(
Collector $Collector,
QUI\Interfaces\Users\User $User,
QUI\Users\Address $Address
): void {
if (!QUI::getUsers()->isUser($User)) {
return;
}
try {
$Engine = QUI::getTemplateManager()->getEngine();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception);
return;
}
// business type
$businessType = 'b2c';
if (!empty($company)) {
$businessType = 'b2b';
}
if ($User->getAttribute('quiqqer.erp.euVatId')) {
$businessType = 'b2b';
}
// template data
$Engine->assign([
'businessType' => $businessType,
'businessTypeIsChangeable' => !(QUI\ERP\Utils\Shop::isOnlyB2C() || QUI\ERP\Utils\Shop::isOnlyB2B()),
'isB2C' => QUI\ERP\Utils\Shop::isB2C(),
'isB2B' => QUI\ERP\Utils\Shop::isB2B(),
'isOnlyB2B' => QUI\ERP\Utils\Shop::isOnlyB2B(),
]);
try {
$Collector->append(
$Engine->fetch(dirname(__FILE__) . '/FrontendUsers/customerData.html')
);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
}
/**
* @param QUI\Interfaces\Users\User $User
* @param QUI\Users\Address $Address
public static function onFrontendUserDataMiddle(
Collector $Collector,
QUI\Interfaces\Users\User $User,
QUI\Users\Address $Address
): void {
if (!QUI::getUsers()->isUser($User)) {
return;
}
try {
$Engine = QUI::getTemplateManager()->getEngine();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception);
return;
}
$Engine->assign([
'Address' => $Address,
'businessTypeIsChangeable' => !(QUI\ERP\Utils\Shop::isOnlyB2C() || QUI\ERP\Utils\Shop::isOnlyB2B()),
'isB2C' => QUI\ERP\Utils\Shop::isB2C(),
'isB2B' => QUI\ERP\Utils\Shop::isB2B(),
'isOnlyB2B' => QUI\ERP\Utils\Shop::isOnlyB2B(),
try {
$Collector->append(
$Engine->fetch(dirname(__FILE__) . '/FrontendUsers/profileData.html')
);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
}
/**
* @param Collector $Collector
* @param QUI\Interfaces\Users\User $User
public static function onFrontendUserAddressCreateBegin(Collector $Collector, QUI\Interfaces\Users\User $User): void
{
if (!QUI::getUsers()->isUser($User)) {
return;
}
try {
$Engine = QUI::getTemplateManager()->getEngine();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception);
return;
}
$Conf = QUI::getPackage('quiqqer/frontend-users')->getConfig();
$settings = $Conf->getValue('profile', 'addressFields');
if (!empty($settings)) {
} else {
$settings = [];
}
$Engine->assign('settings', QUI\FrontendUsers\Controls\Address\Address::checkSettingsArray($settings));
$Engine->assign('settings', QUI\FrontendUsers\Controls\Address\Address::checkSettingsArray([]));
}
'User' => $User,
'businessTypeIsChangeable' => !(QUI\ERP\Utils\Shop::isOnlyB2C() || QUI\ERP\Utils\Shop::isOnlyB2B()),
'isB2C' => QUI\ERP\Utils\Shop::isB2C(),
'isB2B' => QUI\ERP\Utils\Shop::isB2B(),
'isOnlyB2B' => QUI\ERP\Utils\Shop::isOnlyB2B(),
'isOnlyB2C' => QUI\ERP\Utils\Shop::isOnlyB2C(),
]);
try {
$Collector->append(
$Engine->fetch(dirname(__FILE__) . '/FrontendUsers/createAddressBegin.html')
);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
}
/**
* @param Collector $Collector
* @param QUI\Interfaces\Users\User $User
public static function onFrontendUserAddressCreateEnd(Collector $Collector, QUI\Interfaces\Users\User $User): void
{
if (!QUI::getUsers()->isUser($User)) {
return;
}
try {
$Engine = QUI::getTemplateManager()->getEngine();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception);
return;
}
$Conf = QUI::getPackage('quiqqer/frontend-users')->getConfig();
$settings = $Conf->getValue('profile', 'addressFields');
if (!empty($settings)) {
} else {
$settings = [];
}
$Engine->assign('settings', QUI\FrontendUsers\Controls\Address\Address::checkSettingsArray($settings));
$Engine->assign('settings', QUI\FrontendUsers\Controls\Address\Address::checkSettingsArray([]));
}
'User' => $User,
'businessTypeIsChangeable' => !(QUI\ERP\Utils\Shop::isOnlyB2C() || QUI\ERP\Utils\Shop::isOnlyB2B()),
'isB2C' => QUI\ERP\Utils\Shop::isB2C(),
'isB2B' => QUI\ERP\Utils\Shop::isB2B(),
'isOnlyB2B' => QUI\ERP\Utils\Shop::isOnlyB2B(),
'isOnlyB2C' => QUI\ERP\Utils\Shop::isOnlyB2C(),
]);
try {
$Collector->append(
$Engine->fetch(dirname(__FILE__) . '/FrontendUsers/createAddressEnd.html')
);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
}
/**
* @param Collector $Collector
* @param QUI\Interfaces\Users\User $User
* @param QUI\Users\Address $Address
*/
public static function onFrontendUserAddressEditBegin(
Collector $Collector,
QUI\Interfaces\Users\User $User,
QUI\Users\Address $Address
): void {
if (!QUI::getUsers()->isUser($User)) {
return;
}
try {
$Engine = QUI::getTemplateManager()->getEngine();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception);
return;
}
$Conf = QUI::getPackage('quiqqer/frontend-users')->getConfig();
$settings = $Conf->getValue('profile', 'addressFields');
if (!empty($settings)) {
} else {
$settings = [];
}
$Engine->assign('settings', QUI\FrontendUsers\Controls\Address\Address::checkSettingsArray($settings));
$Engine->assign('settings', QUI\FrontendUsers\Controls\Address\Address::checkSettingsArray([]));
}
// business type
$businessType = 'b2c';
if (!empty($company)) {
$businessType = 'b2b';
}
if ($User->getAttribute('quiqqer.erp.euVatId')) {
$businessType = 'b2b';
}
// template data
$Engine->assign([
'businessType' => $businessType,
'businessTypeIsChangeable' => !(QUI\ERP\Utils\Shop::isOnlyB2C() || QUI\ERP\Utils\Shop::isOnlyB2B()),
'isB2C' => QUI\ERP\Utils\Shop::isB2C(),
'isB2B' => QUI\ERP\Utils\Shop::isB2B(),
'isOnlyB2B' => QUI\ERP\Utils\Shop::isOnlyB2B(),
'isOnlyB2C' => QUI\ERP\Utils\Shop::isOnlyB2C(),
]);
try {
$Collector->append(
$Engine->fetch(dirname(__FILE__) . '/FrontendUsers/editAddressBegin.html')
);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
}
/**
* @param Collector $Collector
* @param QUI\Interfaces\Users\User $User
* @param QUI\Users\Address $Address
public static function onFrontendUserAddressEditEnd(
Collector $Collector,
QUI\Interfaces\Users\User $User,
QUI\Users\Address $Address
): void {
if (!QUI::getUsers()->isUser($User)) {
return;
}
try {
$Engine = QUI::getTemplateManager()->getEngine();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception);
return;
}
$Conf = QUI::getPackage('quiqqer/frontend-users')->getConfig();
$settings = $Conf->getValue('profile', 'addressFields');
if (!empty($settings)) {
} else {
$settings = [];
}
$Engine->assign('settings', QUI\FrontendUsers\Controls\Address\Address::checkSettingsArray($settings));
$Engine->assign('settings', QUI\FrontendUsers\Controls\Address\Address::checkSettingsArray([]));
}
'Address' => $Address,
'businessTypeIsChangeable' => !(QUI\ERP\Utils\Shop::isOnlyB2C() || QUI\ERP\Utils\Shop::isOnlyB2B()),
'isB2C' => QUI\ERP\Utils\Shop::isB2C(),
'isB2B' => QUI\ERP\Utils\Shop::isB2B(),
'isOnlyB2B' => QUI\ERP\Utils\Shop::isOnlyB2B(),
'isOnlyB2C' => QUI\ERP\Utils\Shop::isOnlyB2C(),
]);
try {
$Collector->append(
$Engine->fetch(dirname(__FILE__) . '/FrontendUsers/editAddressEnd.html')
);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}
}
//endregion