Skip to content
Code-Schnipsel Gruppen Projekte
BasketOrder.php 12,3 KiB
Newer Older
  • Learn to ignore specific revisions
  •  * This file contains QUI\ERP\Order\Basket\BasketOrder
    
     */
    
    namespace QUI\ERP\Order\Basket;
    
    use QUI;
    
    Henning Leutz's avatar
    Henning Leutz committed
    use QUI\ERP\Order\Utils\Utils as OrderProductUtils;
    
    Henning Leutz's avatar
    Henning Leutz committed
    use QUI\ERP\Products\Field\UniqueField;
    
    use QUI\ERP\Products\Product\ProductList;
    
    Henning Leutz's avatar
    Henning Leutz committed
    use QUI\Exception;
    use QUI\ExceptionStack;
    
    
    Henning Leutz's avatar
    Henning Leutz committed
    use function boolval;
    
    Henning Leutz's avatar
    Henning Leutz committed
    use function is_array;
    
    /**
     * Class BasketOrder
     *
    
     * This is a helper to represent an already send order for the order process
     *
    
     * @package QUI\ERP\Order\Basket
     */
    class BasketOrder
    {
        /**
         * List of products
         *
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var ?QUI\ERP\Products\Product\ProductList
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?ProductList $List = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var ?QUI\Interfaces\Users\User
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?QUI\Interfaces\Users\User $User = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var ?QUI\ERP\Order\AbstractOrder
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?QUI\ERP\Order\AbstractOrder $Order = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var ?string
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?string $hash = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var int|null
    
        protected int | null $id = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * @var QUI\ERP\Comments|null
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?QUI\ERP\Comments $FrontendMessages = null;
    
        /**
         * Basket constructor.
         *
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @param string $orderHash - ID of the order
    
         * @param ?QUI\Interfaces\Users\User $User
    
         *
         * @throws Exception
         * @throws QUI\Exception
         */
    
        public function __construct(string $orderHash, null | QUI\Interfaces\Users\User $User = null)
    
        {
            if (!$User) {
                $User = QUI::getUserBySession();
            }
    
            if (!QUI::getUsers()->isUser($User) || $User->getType() == QUI\Users\Nobody::class) {
    
                    'quiqqer/order',
                    'exception.basket.not.found'
    
            $this->User = $User;
            $this->hash = $orderHash;
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->FrontendMessages = new QUI\ERP\Comments();
    
            $this->readOrder();
    
            // get basket id
            try {
                $Basket = QUI\ERP\Order\Handler::getInstance()->getBasketByHash(
    
    Henning Leutz's avatar
    Henning Leutz committed
                    $this->Order->getUUID(),
    
                    $this->User
                );
    
                $this->id = $Basket->getId();
            } catch (QUI\Exception $Exception) {
                QUI\System\Log::writeDebugException($Exception);
            }
        }
    
    
        /**
         * refresh the basket
         * - import the order stuff to the basket
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function refresh(): void
    
        {
            try {
                $this->readOrder();
            } catch (QUI\Exception $Exception) {
                QUI\System\Log::writeDebugException($Exception);
            }
        }
    
    
        /**
         * imports the order data into the basket
         *
         * @throws QUI\ERP\Exception
         * @throws QUI\ERP\Order\Exception
         * @throws QUI\Exception
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected function readOrder(): void
    
        {
            $this->Order = QUI\ERP\Order\Handler::getInstance()->getOrderByHash($this->hash);
    
            $data = $this->Order->getArticles()->toArray();
    
            $priceFactors = $this->Order->getArticles()->getPriceFactors()->toArray();
    
    
            $articles = $data['articles'];
    
    
            $this->List = new ProductList();
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->List->duplicate = true;
    
            $this->List->setOrder($this->Order);
    
            $this->List->setCurrency($this->Order->getCurrency());
    
            $this->List->getPriceFactors()->importList([
                'end' => $priceFactors
            ]);
    
            // note, import do a cleanup
    
            $this->import($articles);
        }
    
        /**
         * Return the order ID
         *
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @return int|null
    
        public function getId(): ?int
    
        }
    
        /**
         * Clear the basket
    
         * This method clears only  the order articles
         * if you want to clear the order, you must use Order->clear()
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function clear(): void
    
        {
            $this->List->clear();
    
    
            if ($this->hasOrder()) {
    
                $this->getOrder()->getArticles()->clear();
    
        public function count(): int
    
        {
            return $this->List->count();
        }
    
        //region product handling
    
        /**
         * Return the product list
         *
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @return ?ProductList
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getProducts(): ?ProductList
    
        {
            return $this->List;
        }
    
        /**
         * Add a product to the basket
         *
         * @param Product $Product
    
         * @throws QUI\Exception
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function addProduct(Product $Product): void
    
            $Package = QUI::getPackage('quiqqer/order');
    
            $Config = $Package->getConfig();
            $merge = boolval($Config->getValue('orderProcess', 'mergeSameProducts'));
    
    
            if (!$merge) {
                $this->List->addProduct($Product);
    
    
                if ($this->hasOrder()) {
                    $this->updateOrder();
    
            $Products = $this->List->getProducts();
    
            $foundProduct = false;
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            foreach ($Products as $P) {
    
                if (
                    !method_exists($Product, 'toArray')
                    || !method_exists($Product, 'getQuantity')
                    || !method_exists($P, 'toArray')
                    || !method_exists($P, 'getQuantity')
                    || !method_exists($P, 'setQuantity')
                ) {
                    continue;
                }
    
    
                $p1 = OrderProductUtils::getCompareProductArray($Product->toArray());
                $p2 = OrderProductUtils::getCompareProductArray($P->toArray());
    
                if ($p1 == $p2) {
                    $foundProduct = true;
    
                    $quantity = $P->getQuantity();
                    $quantity = $quantity + $Product->getQuantity();
    
    
                    $P->setQuantity($quantity);
                    break;
    
            if ($foundProduct === false) {
                $this->List->addProduct($Product);
            }
    
            if ($this->hasOrder()) {
                $this->updateOrder();
    
        /**
         * Removes a position from the products
         *
         * @param integer $pos
         *
         * @throws QUI\ERP\Exception
         * @throws QUI\ERP\Order\Exception
         * @throws QUI\Exception
         * @throws QUI\Permissions\Exception
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function removePosition(int $pos): void
    
            if (!$this->hasOrder()) {
                return;
            }
    
    
            $this->getOrder()->removeArticle($pos);
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->getOrder()->update();
    
        //endregion
    
        /**
         * Import the products to the basket
         *
         * @param array $products
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @throws Exception
         * @throws ExceptionStack
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function import(array $products = []): void
    
        {
            $this->clear();
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            if (!is_array($products)) {
    
            $Package = QUI::getPackage('quiqqer/order');
    
            $Config = $Package->getConfig();
            $merge = boolval($Config->getValue('orderProcess', 'mergeSameProducts'));
    
                $products = OrderProductUtils::getMergedProductList($products);
    
            QUI::getEvents()->fireEvent(
                'quiqqerOrderBasketToOrderBegin',
                [$this, $this->Order, &$products]
            );
    
    
            $this->List = QUI\ERP\Order\Utils\Utils::importProductsToBasketList(
                $this->List,
    
    Henning Leutz's avatar
    Henning Leutz committed
                $products,
                $this->getOrder()
    
    Henning Leutz's avatar
    Henning Leutz committed
                $this->List->recalculate();
    
                QUI::getEvents()->fireEvent(
                    'quiqqerOrderBasketToOrder',
                    [$this, $this->Order, $this->List]
                );
    
    
                QUI::getEvents()->fireEvent(
                    'quiqqerOrderBasketToOrderEnd',
                    [$this, $this->Order, $this->List]
                );
    
            } catch (\Exception $Exception) {
    
                QUI\System\Log::addDebug($Exception->getMessage());
    
    
            QUI::getEvents()->fireEvent(
                'quiqqerBasketImport',
                [$this, $this->List]
            );
    
         * Save the basket -> order
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function save(): void
    
        /**
         * @throws Exception
         * @throws ExceptionBasketNotFound
         * @throws QUI\Database\Exception
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function saveToSessionBasket(): void
    
        {
            $data = $this->toArray();
    
            $Basket = QUI\ERP\Order\Handler::getInstance()->getBasketFromUser($this->User);
            $Basket->import($data['products']);
        }
    
    
        /**
         * Return the basket as array
         *
         * @return array
         */
    
        public function toArray(): array
    
        {
            $Products = $this->getProducts();
            $products = $Products->getProducts();
    
    
            foreach ($products as $Product) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                /* @var $Field UniqueField */
    
                foreach ($Product->getFields() as $Field) {
                    if (!$Field->isPublic()) {
                        continue;
                    }
    
                    if (!$Field->isCustomField()) {
                        continue;
                    }
    
                    $fields[$Field->getId()] = $Field->getValue();
                }
    
    
                    'id' => $Product->getId(),
    
                    'quantity' => method_exists($Product, 'getQuantity') ? $Product->getQuantity() : 1,
    
    
                $result[] = $attributes;
    
            // calc data
            $calculations = [];
    
            try {
                $data = $Products->getFrontendView()->toArray();
    
                unset($data['attributes']);
                unset($data['products']);
    
                $calculations = $data;
            } catch (QUI\Exception $Exception) {
                QUI\System\Log::writeDebugException($Exception);
            }
    
    
    
                'id' => $this->getId(),
    
    Henning Leutz's avatar
    Henning Leutz committed
                'orderHash' => $this->getOrder()->getUUID(),
    
                'priceFactors' => $Products->getPriceFactors()->toArray(),
                'calculations' => $calculations
    
        }
    
        //region hash & orders
    
        /**
         * Set the process number
         * - Vorgangsnummer
         *
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @param string $hash
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function setHash(string $hash): void
    
        {
            $this->hash = $hash;
        }
    
        /**
         * Return the process number
         *
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @return string|null
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getHash(): ?string
    
        {
            return $this->hash;
        }
    
        /**
         * Does the basket have an assigned order?
         *
         * @return bool
         */
    
        public function hasOrder(): bool
    
        {
            return true;
        }
    
        /**
         * Return the assigned order from the basket
         *
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @return ?QUI\ERP\Order\AbstractOrder
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getOrder(): ?QUI\ERP\Order\AbstractOrder
    
        {
            return $this->Order;
        }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function updateOrder(): void
    
            $this->Order->getArticles()->clear();
            $products = $this->List->getProducts();
    
            foreach ($products as $Product) {
                try {
    
                    if (method_exists($Product, 'toArticle')) {
                        $this->Order->addArticle($Product->toArticle(null, false));
                    }
    
                } catch (QUI\Users\Exception $Exception) {
                    QUI\System\Log::writeDebugException($Exception);
                }
            }
    
    
    
            $Articles = $this->Order->getArticles();
            $OrderCurrency = $this->Order->getCurrency();
    
    
            $PriceFactors = $this->List->getPriceFactors();
    
            $PriceFactors->setCurrency($OrderCurrency);
    
            $Articles->setCurrency($OrderCurrency);
            $Articles->importPriceFactors($PriceFactors->toErpPriceFactorList());
            $Articles->recalculate();
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->Order->update();
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function toOrder(): void
    
        //endregion
    
    Henning Leutz's avatar
    Henning Leutz committed
    
        //region frontend message
    
        /**
         * Add a frontend message
         *
         * @param string $message
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function addFrontendMessage(string $message): void
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
            $this->FrontendMessages->addComment($message);
        }
    
        /**
         * Return the frontend message object
         *
         * @return null|QUI\ERP\Comments
         */
    
        public function getFrontendMessages(): ?QUI\ERP\Comments
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
            return $this->FrontendMessages;
        }
    
        /**
         * Clears the messages and save this status to the database
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function clearFrontendMessages(): void
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
            $this->FrontendMessages->clear();
        }
    
        //endregion