Skip to content
Code-Schnipsel Gruppen Projekte
Handler.php 25,1 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    
    /**
    
    Henning Leutz's avatar
    Henning Leutz committed
     * This file contains QUI\ERP\Order\Handler
    
     */
    
    namespace QUI\ERP\Order;
    
    use QUI;
    
    use QUI\ERP\Customer\Utils as CustomerUtils;
    
    use QUI\ERP\Order\Basket\Basket;
    use QUI\ERP\Order\Basket\Exception;
    use QUI\ERP\Order\Basket\ExceptionBasketNotFound;
    use QUI\ExceptionStack;
    
    use function array_merge;
    use function class_exists;
    
    use function is_numeric;
    
    use function strtotime;
    use function trim;
    
    /**
    
     * Class Handler
     * - Handles orders and order in process
    
     *
     * @package QUI\ERP\Order
     */
    class Handler extends Singleton
    {
    
        const ERROR_ORDER_NOT_FOUND = 604; // a specific order wasn't found
        const ERROR_NO_ORDERS_FOUND = 605; // Search or last orders don't get results
    
        const ERROR_ORDER_ID_ALREADY_EXISTS = 606; // attempt to create a new order with an already existing id
    
    Henning Leutz's avatar
    Henning Leutz committed
    
        /**
         * Default empty value (placeholder for empty values)
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        const EMPTY_VALUE = '---';
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * @var array
         */
    
        protected array $cache = [];
    
        /**
         * Return all order process Provider
         *
         * @return array
         */
    
        public function getOrderProcessProvider(): array
    
        {
            $cacheProvider = 'package/quiqqer/order/providerOrderProcess';
    
            try {
                $providers = QUI\Cache\Manager::get($cacheProvider);
    
            } catch (QUI\Cache\Exception) {
    
                $packages = array_map(function ($package) {
                    return $package['name'];
                }, QUI::getPackageManager()->getInstalled());
    
    
    
                foreach ($packages as $package) {
                    try {
                        $Package = QUI::getPackage($package);
    
                        if ($Package->isQuiqqerPackage()) {
    
                            $providers = array_merge($providers, $Package->getProvider('orderProcess'));
    
                    } catch (QUI\Exception) {
    
                try {
                    QUI\Cache\Manager::set($cacheProvider, $providers);
                } catch (\Exception $Exception) {
                    QUI\System\Log::writeDebugException($Exception);
                }
    
    
            foreach ($providers as $provider) {
    
                if (!class_exists($provider)) {
    
                    continue;
                }
    
                $Provider = new $provider();
    
                if (!($Provider instanceof AbstractOrderProcessProvider)) {
                    continue;
                }
    
                $result[] = $Provider;
            }
    
            return $result;
        }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        //region Order
    
    
        /**
         * Return the order table
         *
         * @return string
         */
    
        public function table(): string
    
        {
            return QUI::getDBTableName('orders');
        }
    
        /**
         * Return a specific Order
         *
    
         * @param int|string $orderId
    
         * @return Order
    
         * @throws QUI\ERP\Order\Exception
         * @throws QUI\Exception
    
        public function get(int|string $orderId): Order
    
            return new Order($orderId);
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * Return the specific order via its hash
    
         * If an order exists with the hash, this will be returned
    
    Henning Leutz's avatar
    Henning Leutz committed
         * An order has higher priority as an order in process
         *
         * @param string $hash - Order Hash
    
         * @return Order|OrderInProcess
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @throws Exception
         */
    
        public function getOrderByHash(string $hash): OrderInProcess|Order
    
            $result = QUI::getDataBase()->fetch([
    
    Henning Leutz's avatar
    Henning Leutz committed
                'select' => 'id',
    
    Henning Leutz's avatar
    Henning Leutz committed
                    'hash' => $hash
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            if (isset($result[0])) {
                return $this->get($result[0]['id']);
            }
    
    
            $result = QUI::getDataBase()->fetch([
    
    Henning Leutz's avatar
    Henning Leutz committed
                'select' => 'id',
    
                'from' => $this->tableOrderProcess(),
                'where' => [
    
    Henning Leutz's avatar
    Henning Leutz committed
                    'hash' => $hash
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            if (!isset($result[0])) {
    
                throw new QUI\ERP\Order\Exception(
    
    Henning Leutz's avatar
    Henning Leutz committed
                    QUI::getLocale()->get('quiqqer/order', 'exception.order.not.found'),
                    self::ERROR_ORDER_NOT_FOUND
                );
            }
    
            return $this->getOrderInProcess($result[0]['id']);
        }
    
    
         * Return an order via its global process id
    
         * If an order exists with the id, this will be returned
    
         * An order has higher priority as an order in process
         *
    
         * If you want to get all orders, use getOrdersByGlobalProcessId()
         *
    
         * @param string|int $id - Global process id
         * @return Order
    
         *
         * @throws QUI\Exception
         * @throws Exception
         */
    
        public function getOrderByGlobalProcessId(int|string $id): Order
    
        {
            $result = QUI::getDataBase()->fetch([
    
                'select' => 'id',
                'from' => $this->table(),
    
                'where_or' => [
    
                    'global_process_id' => $id
                ],
    
            ]);
    
            if (!isset($result[0])) {
    
                throw new QUI\ERP\Order\Exception(
    
                    QUI::getLocale()->get('quiqqer/order', 'exception.order.not.found'),
                    self::ERROR_ORDER_NOT_FOUND
                );
            }
    
            return $this->get($result[0]['id']);
        }
    
    
        /**
         * Return all orders via its global process id
         *
         * @param string $id - Global process id
    
         * @return Order[]
         *<
    
         * @throws QUI\Database\Exception
    
        public function getOrdersByGlobalProcessId(string $id): array
    
        {
            $dbData = QUI::getDataBase()->fetch([
    
                'select' => 'id',
                'from' => $this->table(),
    
            if (!count($dbData)) {
                return [];
    
            foreach ($dbData as $entry) {
                try {
                    $result[] = $this->get($entry['id']);
                } catch (QUI\Exception $Exception) {
                    QUI\System\Log::writeDebugException($Exception);
                }
            }
    
            return $result;
        }
    
    
        /**
         * Return the specific order via its id
    
         * If an order exists with the hash, this will be returned
    
         * An order has higher priority as an order in process
         *
    
         * @param int|string $id - Order Id
    
         * @return Order|OrderInProcess
         *
         * @throws QUI\Exception
         * @throws Exception
         */
    
        public function getOrderById(int|string $id): OrderInProcess|Order
    
            $result = QUI::getDataBase()->fetch([
                'select' => 'id',
                'from' => $this->table(),
                'where' => [
                    'hash' => $id
                ],
                'limit' => 1
            ]);
    
            if (isset($result[0])) {
                return $this->get($result[0]['id']);
            }
    
    
    
            $result = QUI::getDataBase()->fetch([
    
                'select' => 'id',
    
                    'id' => $id
    
    
            if (isset($result[0])) {
                return $this->get($result[0]['id']);
            }
    
    
            $result = QUI::getDataBase()->fetch([
    
                'select' => 'id',
    
                'from' => $this->tableOrderProcess(),
                'where' => [
    
                    'id' => $id
    
    
            if (!isset($result[0])) {
    
                throw new QUI\ERP\Order\Exception(
    
                    QUI::getLocale()->get('quiqqer/order', 'exception.order.not.found'),
                    self::ERROR_ORDER_NOT_FOUND
                );
            }
    
            return $this->getOrderInProcess($result[0]['id']);
        }
    
    
        /**
         * Return the data of a wanted order
         *
    
         * @param integer|string $orderId
    
         * @return array
         *
    
         * @throws QUI\Database\Exception|QUI\ERP\Order\Exception
    
        public function getOrderData(int|string $orderId): array
    
            $result = QUI::getDataBase()->fetch([
    
                    'hash' => $orderId
    
                'limit' => 1
    
            if (empty($result)) {
                $result = QUI::getDataBase()->fetch([
                    'from' => $this->table(),
                    'where' => [
                        'id' => $orderId
                    ],
                    'limit' => 1
                ]);
            }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            if (!isset($result[0])) {
    
                throw new QUI\ERP\Order\Exception(
    
    Henning Leutz's avatar
    Henning Leutz committed
                    QUI::getLocale()->get('quiqqer/order', 'exception.order.not.found'),
                    self::ERROR_ORDER_NOT_FOUND
    
        /**
         * @param QUI\Interfaces\Users\User $User
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @param array $params
    
        public function getOrdersByUser(QUI\Interfaces\Users\User $User, array $params = []): array
    
    Henning Leutz's avatar
    Henning Leutz committed
            $query = [
    
                'select' => ['id', 'customerId', 'hash'],
    
                    'customerId' => $User->getUUID()
    
    Henning Leutz's avatar
    Henning Leutz committed
            ];
    
            if (isset($params['order'])) {
                switch ($params['order']) {
                    case 'id':
                    case 'id ASC':
                    case 'id DESC':
                    case 'status':
                    case 'status ASC':
                    case 'status DESC':
                    case 'c_date':
                    case 'c_date ASC':
                    case 'c_date DESC':
                    case 'paid_date':
                    case 'paid_date ASC':
                    case 'paid_date DESC':
                        $query['order'] = $params['order'];
                }
            }
    
            if (isset($params['limit'])) {
                $query['limit'] = $params['limit'];
            }
    
            try {
                $data = QUI::getDataBase()->fetch($query);
    
            } catch (QUI\Exception) {
    
            $result = [];
    
            foreach ($data as $entry) {
                try {
    
                    $result[] = new Order($entry['hash']);
    
                } catch (QUI\Exception $Exception) {
                    QUI\System\Log::writeException($Exception);
                }
            }
    
            return $result;
        }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * Return the number of orders from the user
         *
         * @param QUI\Interfaces\Users\User $User
         * @return int
    
         *
         * @throws QUI\Database\Exception
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
        public function countOrdersByUser(QUI\Interfaces\Users\User $User): int
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
            $data = QUI::getDataBase()->fetch([
    
    Henning Leutz's avatar
    Henning Leutz committed
                'select' => 'id',
    
                    'customerId' => $User->getUUID()
    
    Henning Leutz's avatar
    Henning Leutz committed
                ]
            ]);
    
            if (isset($data[0]['id'])) {
                return (int)$data[0]['id'];
            }
    
            return 0;
        }
    
    
        /**
         * Sends email to an order customer with successful (full) payment info.
         *
         * @param AbstractOrder $Order
         * @return void
         */
        public function sendOrderPaymentSuccessMail(AbstractOrder $Order): void
        {
    
            $CustomerLocale = $Customer->getLocale();
    
            $subject = $CustomerLocale->get(
                'quiqqer/order',
                'mail.payment_success.subject',
                $this->getLocaleVarsForOrderMail($Order)
            );
    
            $body = $CustomerLocale->get(
                'quiqqer/order',
                'mail.payment_success.body',
                $this->getLocaleVarsForOrderMail($Order)
            );
    
            $Mailer = QUI::getMailManager()->getMailer();
    
            $Mailer->setSubject($subject);
            $Mailer->setBody($body);
    
            $Mailer->addRecipient(CustomerUtils::getInstance()->getEmailByCustomer($Customer));
    
            try {
                $Mailer->send();
            } catch (\Exception $Exception) {
                QUI\System\Log::writeException($Exception);
            }
        }
    
        /**
         * Get all placeholder variables for order mails.
         *
         * @param AbstractOrder $Order
         * @return array
         */
        protected function getLocaleVarsForOrderMail(AbstractOrder $Order): array
        {
    
            $Customer = $Order->getCustomer();
            $CustomerLocale = $Customer->getLocale();
    
            $CustomerAddress = $Customer->getAddress();
    
            $user = $CustomerAddress->getAttribute('contactPerson');
    
    
            if (empty($user)) {
                $user = $Customer->getName();
            }
    
            if (empty($user)) {
                $user = $Customer->getAddress()->getName();
            }
    
    
            $user = trim($user);
    
    
            // contact person
            $ContactPersonAddress = CustomerUtils::getInstance()->getContactPersonAddress($Customer);
    
            if ($ContactPersonAddress) {
                $contactPerson = $ContactPersonAddress->getName();
            }
    
            if (empty($contactPerson)) {
                $contactPerson = $user;
            }
    
            $contactPersonOrName = $contactPerson;
    
            if (empty($contactPersonOrName)) {
                $contactPersonOrName = $user;
            }
    
            // Customer
            $Address = $Order->getInvoiceAddress();
    
            // customer name
            $user = $Address->getAttribute('contactPerson');
    
            if (empty($user)) {
                $user = $Customer->getName();
            }
    
            if (empty($user)) {
                $user = $Address->getName();
            }
    
    
            $user = trim($user);
    
    
            // email
            $email = $Customer->getAttribute('email');
    
            if (empty($email)) {
                $mailList = $Address->getMailList();
    
                if (isset($mailList[0])) {
                    $email = $mailList[0];
                }
            }
    
            // Customer company
            $customerCompany = $Address->getAttribute('company');
    
    
            if (empty($companyOrName)) {
                $companyOrName = $user;
            }
    
            // Shop company
            $company = '';
    
            try {
    
                $Conf = QUI::getPackage('quiqqer/erp')->getConfig();
    
                $company = $Conf->get('company', 'name');
            } catch (\Exception $Exception) {
                QUI\System\Log::writeException($Exception);
            }
    
            return [
    
                'orderId' => $Order->getPrefixedNumber(),
    
                'hash' => $Order->getUUID(),
                'date' => $CustomerLocale->formatDate(strtotime($Order->getCreateDate())),
    
                'systemCompany' => $company,
    
    
                'contactPersonOrName' => $contactPersonOrName,
    
    
                'user' => $user,
                'name' => $user,
                'company' => $Address->getAttribute('company'),
    
                'companyOrName' => $companyOrName,
    
                'address' => $Address->render(),
                'email' => $email,
                'salutation' => $Address->getAttribute('salutation'),
                'firstname' => $Address->getAttribute('firstname'),
                'lastname' => $Address->getAttribute('lastname')
    
    Henning Leutz's avatar
    Henning Leutz committed
        //endregion
    
        //region Order Process
    
    
    Henning Leutz's avatar
    Henning Leutz committed
         * Return the order process table
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @return string
    
        public function tableOrderProcess(): string
    
    Henning Leutz's avatar
    Henning Leutz committed
        }
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
    
         * Return an Order which is in processing
    
    Henning Leutz's avatar
    Henning Leutz committed
         *
         * @param $orderId
    
         * @return OrderInProcess
    
         * @throws QUI\ERP\Order\Exception
         * @throws QUI\ERP\Exception
    
         * @throws  QUI\Database\Exception
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
        public function getOrderInProcess($orderId): OrderInProcess
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
    
            return new OrderInProcess($orderId);
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
    
         * Return an Order which is in processing
    
    Henning Leutz's avatar
    Henning Leutz committed
         *
         * @param string $hash - hash of the order
         * @return OrderInProcess
         *
         * @throws QUI\ERP\Order\Exception
         * @throws QUI\ERP\Exception
    
         * @throws QUI\Database\Exception
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
        public function getOrderInProcessByHash(string $hash): OrderInProcess
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
            $result = QUI::getDataBase()->fetch([
                'select' => 'id',
    
                'from' => $this->tableOrderProcess(),
                'where' => [
    
    Henning Leutz's avatar
    Henning Leutz committed
                    'hash' => $hash
                ],
    
    Henning Leutz's avatar
    Henning Leutz committed
            ]);
    
            if (!isset($result[0])) {
    
                throw new QUI\ERP\Order\Exception(
    
    Henning Leutz's avatar
    Henning Leutz committed
                    QUI::getLocale()->get('quiqqer/order', 'exception.order.not.found'),
                    self::ERROR_ORDER_NOT_FOUND
                );
            }
    
            return $this->getOrderInProcess($result[0]['id']);
        }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * Return all orders in process from a user
         *
         * @param QUI\Interfaces\Users\User $User
         * @return array
    
         *
         * @throws QUI\Database\Exception
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
        public function getOrdersInProcessFromUser(QUI\Interfaces\Users\User $User): array
    
            $list = QUI::getDataBase()->fetch([
    
                'from' => $this->tableOrderProcess(),
    
                    'customerId' => $User->getUUID()
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            foreach ($list as $entry) {
                try {
    
                    $result[] = $this->getOrderInProcess($entry['hash']);
    
                } catch (\Exception) {
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * Return order in process number form a user
         *
         * @param QUI\Interfaces\Users\User $User
         * @return int
    
         *
         * @throws QUI\Database\Exception
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
        public function countOrdersInProcessFromUser(QUI\Interfaces\Users\User $User): int
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
            $data = QUI::getDataBase()->fetch([
    
                'count' => 'id',
                'select' => 'id',
    
                'from' => $this->tableOrderProcess(),
                'where' => [
    
                    'customerId' => $User->getUUID()
    
            if (isset($data[0]['id'])) {
                return (int)$data[0]['id'];
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * Return the last order in process from a user
         *
         * @param QUI\Interfaces\Users\User $User
    
         * @return OrderInProcess
    
         *
         * @throws QUI\ERP\Order\Exception
         * @throws QUI\ERP\Exception
    
         * @throws QUI\Database\Exception
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
        public function getLastOrderInProcessFromUser(QUI\Interfaces\Users\User $User): OrderInProcess
    
            $result = QUI::getDataBase()->fetch([
    
                'from' => $this->tableOrderProcess(),
    
                    'customerId' => $User->getUUID(),
    
                    'successful' => 0
    
    Henning Leutz's avatar
    Henning Leutz committed
                'limit' => 1,
                'order' => 'c_date DESC'
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            if (!isset($result[0])) {
    
                try {
                    $result = QUI::getEvents()->fireEvent('orderProcessGetOrder');
    
                    foreach ($result as $Order) {
                        if ($Order instanceof OrderInProcess) {
                            return $Order;
                        }
                    }
    
                } catch (\Exception) {
    
                throw new QUI\ERP\Order\Exception(
    
    Henning Leutz's avatar
    Henning Leutz committed
                    QUI::getLocale()->get('quiqqer/order', 'exception.no.orders.found'),
                    self::ERROR_NO_ORDERS_FOUND
    
            return $this->getOrderInProcess($result[0]['hash']);
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * Return the data of a wanted order
         *
    
         * @param integer|string $orderId
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @return array
         *
    
         * @throws QUI\Database\Exception
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
        public function getOrderProcessData(int|string $orderId): array
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
    
            $result = QUI::getDataBase()->fetch([
    
                'from' => $this->tableOrderProcess(),
    
                'where_or' => [
                    'id' => $orderId,
                    'hash' => $orderId
    
    Henning Leutz's avatar
    Henning Leutz committed
                'limit' => 1
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            if (!isset($result[0])) {
    
                throw new QUI\ERP\Order\Exception(
    
    Henning Leutz's avatar
    Henning Leutz committed
                    QUI::getLocale()->get('quiqqer/order', 'exception.order.not.found'),
                    self::ERROR_ORDER_NOT_FOUND
    
    Henning Leutz's avatar
    Henning Leutz committed
                );
            }
    
            return $result[0];
        }
    
        //endregion
    
    
        //region basket
    
        /**
         * Return the table for baskets
         *
         * @return string
         */
    
        public function tableBasket(): string
    
        {
            return QUI::getDBTableName('baskets');
        }
    
    
        /**
         * Return a basket by its string
         * Can be a basket id or a basket hash
         *
    
         * @param integer|string $str - hash or basket id
    
         * @param null $User - optional, user of the basket
    
         * @return Basket
    
         * @throws Exception
         * @throws ExceptionBasketNotFound
         * @throws ExceptionStack
    
         * @throws QUI\Database\Exception
    
         * @throws QUI\Exception
    
        public function getBasket(int|string $str, $User = null): Basket
    
            if (is_numeric($str)) {
    
                return self::getBasketById($str, $User);
            }
    
            return self::getBasketByHash($str, $User);
        }
    
    
    Patrick Müller's avatar
    Patrick Müller committed
        /**
    
         * @param int|string $basketId
    
         * @param null $User - optional, user of the basket
    
         * @return Basket
    
         * @throws ExceptionStack
    
         * @throws QUI\Database\Exception
    
         * @throws QUI\Exception
    
        public function getBasketById(int|string $basketId, $User = null): Basket
    
            $data = QUI::getDataBase()->fetch([
    
                'from' => QUI\ERP\Order\Handler::getInstance()->tableBasket(),
    
    Patrick Müller's avatar
    Patrick Müller committed
                    'id' => $basketId
    
    Patrick Müller's avatar
    Patrick Müller committed
                'limit' => 1
    
    Patrick Müller's avatar
    Patrick Müller committed
    
            if (!isset($data[0])) {
    
                throw new ExceptionBasketNotFound([
    
    Patrick Müller's avatar
    Patrick Müller committed
                    'quiqqer/order',
                    'exception.basket.not.found'
    
            if ($User === null) {
                $User = QUI::getUserBySession();
            } else {
                $basketData = $data[0];
    
                $User = QUI::getUsers()->get($basketData['uid']);
    
    Patrick Müller's avatar
    Patrick Müller committed
    
            $this->checkBasketPermissions($User);
    
    
            return new Basket($data[0]['id'], $User);
    
         * @param null $User - optional, user of the basket
    
         * @return Basket
    
         * @throws Exception
         * @throws ExceptionBasketNotFound
         * @throws ExceptionStack
    
         * @throws QUI\Database\Exception
    
         * @throws QUI\Exception
    
        public function getBasketByHash(string $hash, $User = null): Basket
    
            $data = QUI::getDataBase()->fetch([
    
                'from' => QUI\ERP\Order\Handler::getInstance()->tableBasket(),
    
                throw new ExceptionBasketNotFound([
    
                    'quiqqer/order',
    
    
            if ($User === null) {
                $User = QUI::getUserBySession();
            } else {
                $basketData = $data[0];
    
                $User = QUI::getUsers()->get($basketData['uid']);
    
            return new Basket($data[0]['id'], $User);
    
        }
    
        /**
         * @param QUI\Interfaces\Users\User $User
         * @return QUI\ERP\Order\Basket\Basket
         *
    
         * @throws Exception
         * @throws ExceptionBasketNotFound
    
         * @throws QUI\Database\Exception
    
         * @throws QUI\Exception
    
        public function getBasketFromUser(QUI\Interfaces\Users\User $User): Basket
    
            $data = QUI::getDataBase()->fetch([
    
                'select' => 'id',
    
                'from' => QUI\ERP\Order\Handler::getInstance()->tableBasket(),
                'where' => [
    
                    'uid' => $User->getUUID()
    
    
    
            if (!isset($data[0])) {
    
                throw new ExceptionBasketNotFound([
    
                    'quiqqer/order',
    
                    'exception.basket.not.found'
    
            return new Basket($data[0]['id'], $User);
    
         * @param integer|string $basketId
    
         * @param null|QUI\Interfaces\Users\User $User
         * @return array
    
         * @throws Exception
         * @throws ExceptionBasketNotFound
    
         * @throws QUI\Database\Exception
    
         * @throws QUI\Exception
    
        public function getBasketData(int|string $basketId, QUI\Interfaces\Users\User $User = null): array
    
        {
            if ($User === null) {
                $User = QUI::getUserBySession();
            }
    
    
            $data = QUI::getDataBase()->fetch([
    
                'from' => QUI\ERP\Order\Handler::getInstance()->tableBasket(),
    
                    'uid' => $User->getUUID()
    
                'limit' => 1
    
    
            if (!isset($data[0])) {
    
                throw new ExceptionBasketNotFound([
    
                    'quiqqer/order',
    
                    'exception.basket.not.found'
    
        /**
         * Basket permission check
         *
         * @param QUI\Interfaces\Users\User $User
    
         * @throws QUI\Exception
    
        protected function checkBasketPermissions(QUI\Interfaces\Users\User $User): void
    
        {
            $hasPermissions = function () use ($User) {
                if (QUI::getUserBySession()->isSU()) {
                    return true;
                }
    
                if (QUI::getUsers()->isSystemUser(QUI::getUserBySession())) {
                    return true;
                }
    
    
                if ($User->getUUID() === QUI::getUserBySession()->getUUID()) {
    
                    return true;
                }
    
                return false;
            };
    
            if ($hasPermissions() === false) {
    
                throw new QUI\Exception([
    
                    'quiqqer/order',
    
        //endregion