Newer
Older
<?php
/**
* This file contains QUI\ERP\Order\Controls\OrderProcess\CustomerData
*/
namespace QUI\ERP\Order\Controls\OrderProcess;
use QUI;
use QUI\Exception;
use QUI\Users\Address;
use QUI\Users\User;
use function dirname;
use function json_decode;
use function trim;
/**
* Class CustomerData
*
* @package QUI\ERP\Order\Controls\OrderProcess
*
* @event quiqqerOrderCustomerDataSave [ self ]
* @event quiqqerOrderCustomerDataSaveEnd [ self ]
*/
class CustomerData extends QUI\ERP\Order\Controls\AbstractOrderingStep
{
/**
* Basket constructor.
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->setAttributes([
'data-qui' => 'package/quiqqer/order/bin/frontend/controls/orderProcess/CustomerData',

Henning Leutz
committed
$this->addCSSClass('quiqqer-order-customerData-container');
$this->addCSSFile(dirname(__FILE__) . '/CustomerData.css');
public function getBody(): string
$Engine = QUI::getTemplateManager()->getEngine();
if (!$User) {
try {
$Customer = $this->getOrder()->getCustomer();
$User = QUI::getUsers()->get($Customer->getUUID());
} catch (QUI\Exception) {
$Address = $User->getStandardAddress();

Henning Leutz
committed
try {
$this->getOrder()->setInvoiceAddress($Address);

Henning Leutz
committed
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}

Henning Leutz
committed
$isUserB2B = function () use ($User) {
if ($User->getAttribute('quiqqer.erp.isNettoUser') === QUI\ERP\Utils\User::IS_NETTO_USER) {
return ' selected="selected"';
}
if ($User->getAttribute('quiqqer.erp.isNettoUser') !== false) {
return '';
}
if (
QUI\ERP\Utils\Shop::isB2CPrioritized() ||
QUI\ERP\Utils\Shop::isOnlyB2C()
) {
if (QUI\ERP\Utils\Shop::isB2B()) {
return ' selected="selected"';
}
return '';
};
$commentCustomer = QUI::getSession()->get('comment-customer');
$commentMessage = QUI::getSession()->get('comment-message');
if (!empty($commentCustomer)) {
$commentCustomer = QUI\Utils\Security\Orthos::clear($commentCustomer);
}
if (!empty($commentMessage)) {
$commentMessage = QUI\Utils\Security\Orthos::clear($commentMessage);
}
$this->setAttribute('data-validate', 1);
} catch (QUI\ERP\Order\Exception) {
$this->setAttribute('data-validate', 0);

Henning Leutz
committed
// frontend users address profile settings
try {
$Conf = QUI::getPackage('quiqqer/frontend-users')->getConfig();

Henning Leutz
committed
$settings = $Conf->getValue('profile', 'addressFields');
if (!empty($settings)) {

Henning Leutz
committed
}

Henning Leutz
committed
$settings = [];
}
if (empty($settings) || is_string($settings)) {
$settings = [];
}

Michael Danielczok
committed
if (!empty($settings['company']['required'])) {
$this->setJavaScriptControlOption('companyisrequired', 1);
}
$settings = QUI\FrontendUsers\Controls\Address\Address::checkSettingsArray($settings);

Henning Leutz
committed
$businessTypeIsChangeable = !(QUI\ERP\Utils\Shop::isOnlyB2C() || QUI\ERP\Utils\Shop::isOnlyB2B());
if ($this->existsAttribute('businessTypeIsChangeable')) {
$businessTypeIsChangeable = $this->getAttribute('businessTypeIsChangeable');
}
$isB2B = QUI\ERP\Utils\Shop::isB2B();
$isB2C = QUI\ERP\Utils\Shop::isB2C();

Henning Leutz
committed
$isOnlyB2B = QUI\ERP\Utils\Shop::isOnlyB2B();
$isOnlyB2C = QUI\ERP\Utils\Shop::isOnlyB2C();
'User' => $User,
'Address' => $Address,
'Order' => $this->getOrder(),
'b2bSelected' => $isUserB2B(),
'commentMessage' => $commentMessage,
'commentCustomer' => $commentCustomer,

Henning Leutz
committed
'businessTypeIsChangeable' => $businessTypeIsChangeable,
'isB2C' => $isB2C,
'isB2B' => $isB2B,
'isOnlyB2B' => $isOnlyB2B,
'isOnlyB2C' => $isOnlyB2C,
return $Engine->fetch(dirname(__FILE__) . '/CustomerData.html');
}
/**
* @param null|QUI\Locale $Locale
* @return string
*/
public function getName(null | QUI\Locale $Locale = null): string
{
return 'Customer';
}
/**
* @return string
*/
public function getIcon(): string
{
return 'fa-user-o';
}
/**
* @throws QUI\ERP\Order\Exception
*/
public function validate(): void

Henning Leutz
committed
$Address = $this->getInvoiceAddress();
try {
$Address->getUUID() !== $this->getOrder()->getInvoiceAddress()->getUUID()

Henning Leutz
committed
$this->getOrder()->setInvoiceAddress($Address);

Henning Leutz
committed
}
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
$this->validateAddress(
$this->getOrder()->getInvoiceAddress()
);
}
/**
* Checks if the address is valid for the order customer data
*
* @param QUI\Users\Address $Address
* @throws QUI\ERP\Order\Exception
*/
public static function validateAddress(QUI\Users\Address $Address): void
{
$firstName = $Address->getAttribute('firstname');
$street_no = $Address->getAttribute('street_no');
$country = $Address->getAttribute('country');
/**
* @param $field
* @throws QUI\ERP\Order\Exception
*/
$throwException = function ($field) {
throw new QUI\ERP\Order\Exception([
'quiqqer/order',
'exception.missing.address.field',
['field' => $field]
]);
};
if (empty($firstName)) {
$throwException(
);
}
if (empty($lastName)) {
$throwException(
QUI::getLocale()->get('quiqqer/order', 'lastname')
);
}
if (empty($street_no)) {
$throwException(
QUI::getLocale()->get('quiqqer/order', 'street_no')
);
}
// if (empty($zip)) {
// $throwException(
// QUI::getLocale()->get('quiqqer/order', 'zip')
// );
// }
if (empty($city)) {
$throwException(
QUI::getLocale()->get('quiqqer/order', 'city')
);
}
if (empty($country)) {
$throwException(
QUI::getLocale()->get('quiqqer/order', 'country')
);
}
/**
* @param QUI\Users\Address $Address
* @return bool
*/
protected function isAddressValid(QUI\Users\Address $Address): bool
{
try {
$this->validateAddress($Address);
return false;
}
return true;
}
/**
* @param QUI\Users\Address $Address
* @return bool
*/
protected function isAddressEmpty(QUI\Users\Address $Address): bool
{
$firstName = $Address->getAttribute('firstname');
$street_no = $Address->getAttribute('street_no');
$zip = $Address->getAttribute('zip');
$city = $Address->getAttribute('city');
$country = $Address->getAttribute('country');
&& empty($lastName)
&& empty($street_no)
&& empty($zip)
&& empty($city)
&& empty($country)
) {
return true;
}
return false;
}
/**
* Order was ordered with costs
*
* @return void
*
* @throws QUI\Permissions\Exception
* @throws QUI\Exception
*/
{
if (isset($_REQUEST['current']) && $_REQUEST['current'] !== $this->getName()) {
return;
}
if (!isset($_REQUEST['addressId'])) {
return;
}
QUI::getEvents()->fireEvent('quiqqerOrderCustomerDataSave', [$this]);
$Address = $this->getAddressById($_REQUEST['addressId']);
if ($Address === null) {
return;
}
$fields = [
'company',
'salutation',
'firstname',
'lastname',
'street_no',
'zip',
'city',
if (isset($_REQUEST['street']) || isset($_REQUEST['street_number'])) {
$street = '';
if (isset($_REQUEST['street'])) {
}
if (isset($_REQUEST['street_number'])) {
$street = $street . ' ' . trim($_REQUEST['street_number']);
$_REQUEST['street_no'] = $street;
}
foreach ($fields as $field) {
if (isset($_REQUEST[$field])) {
$Address->setAttribute($field, $_REQUEST[$field]);
}
}
if (isset($_REQUEST['tel'])) {
$Address->editPhone(0, $_REQUEST['tel']);
}
// comment
if (!empty($_REQUEST['comment-customer'])) {
QUI::getSession()->set('comment-customer', $_REQUEST['comment-customer']);
}
if (!empty($_REQUEST['comment-message'])) {
QUI::getSession()->set('comment-message', $_REQUEST['comment-message']);
}
// user data
$User = $Address->getUser();
if (isset($_REQUEST['businessType'])) {
if ($_REQUEST['businessType'] === 'b2b') {
$User->setAttribute('quiqqer.erp.isNettoUser', QUI\ERP\Utils\User::IS_NETTO_USER);
} else {
$User->setAttribute('quiqqer.erp.isNettoUser', QUI\ERP\Utils\User::IS_BRUTTO_USER);
} else {
$User->setCompanyStatus(!empty($Address->getAttribute('company')));
$addressId = $User->getAttribute('quiqqer.erp.address');
if (empty($addressId)) {
$User->setAttribute('quiqqer.erp.address', $Address->getUUID());
// @todo validate vat id??
$currentVat = $User->getAttribute('quiqqer.erp.euVatId');
if (isset($_REQUEST['vatId']) && empty($currentVat)) {
$User->setAttribute('quiqqer.erp.euVatId', $_REQUEST['vatId']);
}

Michael Danielczok
committed
// firstname lastname
// because user makes an update to the address object
if (!empty($_REQUEST['firstname']) && $User->getAttribute('firstname') === '') {
$User->setAttribute('firstname', $_REQUEST['firstname']);
}
if (!empty($_REQUEST['lastname']) && $User->getAttribute('lastname') === '') {
$User->setAttribute('lastname', $_REQUEST['lastname']);
}
$User->refresh();
$this->getOrder()->setInvoiceAddress($Address);
if (isset($_REQUEST['shipping-address']) && $_REQUEST['shipping-address'] == -1) {
$this->getOrder()->setDeliveryAddress([
'id' => -1
]);
}
$this->getOrder()->setCustomer($User);
QUI::getEvents()->fireEvent('quiqqerOrderCustomerDataSaveEnd', [$this]);
* @param int|string $addressId
* @return false|null|Address
* @throws Exception
* @throws QUI\Permissions\Exception
protected function getAddressById(int | string $addressId): bool | QUI\Users\Address | null
$Address = null;
try {
$Address = $User->getAddress($addressId);
} catch (QUI\Exception $Exception) {
if ($addressId === 0) {
try {
$Address = $User->getAddress($User->getAttribute('quiqqer.erp.address'));
} catch (QUI\Exception $Exception) {
if ($Address === null && $User instanceof User) {
try {
$Address = $User->getStandardAddress();
} catch (QUI\Users\Exception $Exception) {
}
}
return $Address;
}
/**
* @return null|QUI\Users\Address
protected function getInvoiceAddress(): Address | null
try {
$Address = $Order->getInvoiceAddress();
if ($Address->getUUID() && !$this->isAddressEmpty($Address)) {
return $User->getAddress($Address->getUUID());
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
try {
$Address = $Customer->getStandardAddress();
if ($Address->getUUID() && !$this->isAddressEmpty($Address)) {
return $User->getAddress($Address->getUUID());
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
if ($User->getAttribute('quiqqer.erp.address')) {
try {
return $User->getAddress($User->getAttribute('quiqqer.erp.address'));
QUI\System\Log::writeDebugException($Exception);
return $User->getStandardAddress();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
* event on order is successful
*
* @param QUI\ERP\Order\AbstractOrder $Order
public static function parseSessionOrderCommentsToOrder(QUI\ERP\Order\AbstractOrder $Order): void
{
$message = '';
if (QUI::getSession()->get('comment-customer')) {
$message .= QUI::getSession()->get('comment-customer') . "\n";
}
if (QUI::getSession()->get('comment-message')) {
$message .= QUI::getSession()->get('comment-message');
}
$comments = $Comments->toArray();
// look if the same comment already exists
foreach ($comments as $comment) {
if ($comment['message'] === $message) {
return;
$Order->update(QUI::getUsers()->getSystemUser());
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}