Skip to content
Code-Schnipsel Gruppen Projekte
AbstractOrder.php 60,8 KiB
Newer Older
  • Learn to ignore specific revisions
  • Henning Leutz's avatar
    Henning Leutz committed
    <?php
    
    /**
     * This file contains QUI\ERP\Order\AbstractOrder
     */
    
    namespace QUI\ERP\Order;
    
    use QUI;
    use QUI\ERP\Accounting\ArticleList;
    
    use QUI\ERP\Accounting\Payments\Payments;
    
    use QUI\ERP\Accounting\Payments\Transactions\Handler as TransactionHandler;
    
    Henning Leutz's avatar
    Henning Leutz committed
    use QUI\ERP\Accounting\Payments\Transactions\Transaction;
    
    use QUI\ERP\ErpTransactionsInterface;
    
    use QUI\ERP\Exception;
    
    use QUI\ERP\Order\ProcessingStatus\Handler as ProcessingHandler;
    
    use QUI\ERP\Order\ProcessingStatus\Status;
    use QUI\ERP\Order\ProcessingStatus\StatusUnknown;
    
    use QUI\ERP\Shipping\ShippingStatus\Handler as ShippingStatusHandler;
    
    use QUI\ERP\User;
    use QUI\ExceptionStack;
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    
    Henning Leutz's avatar
    Henning Leutz committed
    use function array_filter;
    
    Henning Leutz's avatar
    Henning Leutz committed
    use function array_flip;
    
    use function class_exists;
    
    Henning Leutz's avatar
    Henning Leutz committed
    use function date;
    use function is_array;
    use function is_numeric;
    use function is_string;
    use function json_decode;
    use function json_encode;
    
    use function method_exists;
    
    Henning Leutz's avatar
    Henning Leutz committed
    use function preg_replace;
    use function round;
    use function strip_tags;
    use function strtotime;
    use function time;
    
    
    Henning Leutz's avatar
    Henning Leutz committed
    /**
     * Class AbstractOrder
     *
     * Main parent class for order classes
     * - Order
     * - OrderProcess
     *
     * @package QUI\ERP\Order
     */
    
    abstract class AbstractOrder extends QUI\QDOM implements OrderInterface, ErpEntityInterface, ErpTransactionsInterface
    
    Henning Leutz's avatar
    Henning Leutz committed
    {
    
        /**
         * Article types
         */
        const ARTICLE_TYPE_PHYSICAL = 1;
    
        const ARTICLE_TYPE_DIGITAL = 2;
        const ARTICLE_TYPE_MIXED = 3;
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * order id
         *
         * @var integer
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected int $id;
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    
        /**
         * @var string
         */
    
        protected string $idStr = '';
    
        /**
         * @var ?string
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?string $idPrefix = null;
    
        protected string $globalProcessId;
    
        /**
         * @var int
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected int $status = 0;
    
         * @var Status|StatusUnknown|null
    
        protected Status|StatusUnknown|null $Status = null;
    
        /**
         * @var null|QUI\ERP\Shipping\ShippingStatus\Status
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?QUI\ERP\Shipping\ShippingStatus\Status $ShippingStatus = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected int $successful;
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * invoice ID
         *
    
         * @var int|bool|string
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
        protected string|int|bool|null $invoiceId = false;
    
    Henning Leutz's avatar
    Henning Leutz committed
    
        /**
    
         * @var string|int|null
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
        protected null|int|string $customerId;
    
    Henning Leutz's avatar
    Henning Leutz committed
    
        /**
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var array|null
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?array $customer = [];
    
    Henning Leutz's avatar
    Henning Leutz committed
    
        /**
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var array|null
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?array $addressInvoice = [];
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var array|null
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?array $addressDelivery = [];
    
    Henning Leutz's avatar
    Henning Leutz committed
    
        /**
         * @var array
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected array $articles = [];
    
    Henning Leutz's avatar
    Henning Leutz committed
    
        /**
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var array|null
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?array $data;
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected array $paymentData = [];
    
    Henning Leutz's avatar
    Henning Leutz committed
    
        /**
         * @var string
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected string $hash;
    
    Henning Leutz's avatar
    Henning Leutz committed
    
        /**
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var string
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected string $cDate;
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    
        /**
         * Create user id
         *
    
        protected int|string $cUser;
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var ArticleList|null
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?ArticleList $Articles = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var QUI\ERP\Comments|null
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?QUI\ERP\Comments $Comments = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var QUI\ERP\Comments|null
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?QUI\ERP\Comments $History = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var QUI\ERP\Comments|null
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?QUI\ERP\Comments $FrontendMessage = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var QUI\ERP\Comments|null
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?QUI\ERP\Comments $StatusMails = null;
    
        /**
         * @var null|QUI\ERP\User
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?QUI\ERP\User $Customer = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var ?int
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?int $paymentId = null;
    
         * @var string|null
    
        protected ?string $paymentMethod;
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected bool $statusChanged = false;
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @var QUI\ERP\Currency\Currency|null
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?QUI\ERP\Currency\Currency $Currency = null;
    
        //shipping
    
        /**
         * @var integer|null
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?int $shippingId = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * Order constructor.
         *
         * @param array $data
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @throws Exception
    
         * @throws QUI\ERP\Exception
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function __construct(array $data = [])
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
            $needles = Factory::getInstance()->getOrderConstructNeedles();
    
            foreach ($needles as $needle) {
    
                if (!isset($data[$needle]) && $data[$needle] !== null) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                        'quiqqer/order',
                        'exception.order.construct.needle.missing',
    
    Henning Leutz's avatar
    Henning Leutz committed
                }
            }
    
    
            $this->id = (int)$data['id'];
            $this->hash = $data['hash'];
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->cDate = $data['c_date'];
    
            $this->cUser = $data['c_user'];
    
            if (!empty($data['global_process_id'])) {
                $this->globalProcessId = $data['global_process_id'];
    
            } else {
                $this->globalProcessId = $this->hash;
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->setDataBaseData($data);
    
            if (!empty($data['id_str'])) {
                $this->idStr = $data['id_str'];
            } else {
                $this->idStr = $this->idPrefix . $this->id;
            }
    
    
            try {
                QUI::getEvents()->fireEvent('quiqqerOrderInit', [$this]);
            } catch (\Exception $Exception) {
                QUI\System\Log::addError($Exception->getMessage());
            }
    
    Henning Leutz's avatar
    Henning Leutz committed
        }
    
        /**
         * Set the db data to the order object
         *
         * @param array $data
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @throws QUI\ERP\Exception|\Exception
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
        protected function setDataBaseData(array $data): void
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->invoiceId = $data['invoice_id'];
    
            $this->idPrefix = $data['id_prefix'];
    
    
            $this->addressDelivery = json_decode($data['addressDelivery'] ?? 'null', true);
            $this->addressInvoice = json_decode($data['addressInvoice'] ?? 'null', true);
            $this->data = json_decode($data['data'] ?? 'null', true);
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    
            if (isset($data['status'])) {
                $this->status = $data['status'];
            }
    
    
            if (isset($data['project_name'])) {
                $this->setAttribute('project_name', $data['project_name']);
            }
    
    
            if (!empty($data['order_process_id'])) {
                $this->setAttribute('order_process_id', $data['order_process_id']);
            }
    
    
            $this->customerId = $data['customerId'];
    
    
            if (isset($data['customer'])) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                $this->customer = json_decode($data['customer'], true);
    
                if (!isset($this->customer['id'])) {
                    $this->customer['id'] = $this->customerId;
    
                $customerData = $this->customer;
    
                if (!isset($customerData['address'])) {
                    $customerData['address'] = $this->addressInvoice;
                }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                if (!isset($customerData['isCompany']) && isset($this->customer['company'])) {
                    $customerData['isCompany'] = !empty($this->customer['company']);
                }
    
                if (!isset($customerData['country']) && isset($customerData['address']['country'])) {
                    $customerData['country'] = $customerData['address']['country'];
                }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                try {
    
                    $this->setCustomer($customerData);
    
                } catch (QUI\Exception $Exception) {
    
                    QUI\System\Log::writeRecursive($this->customer);
    
    Henning Leutz's avatar
    Henning Leutz committed
                    QUI\System\Log::addWarning($Exception->getMessage());
                }
    
    Henning Leutz's avatar
    Henning Leutz committed
                if (isset($this->addressInvoice['id']) && $this->addressInvoice['id'] >= 0) {
                    $this->Customer->setAddress($this->getInvoiceAddress());
                } elseif (isset($customerData['address']['id']) && $customerData['address']['id']) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                    $this->Customer->setAddress($this->getInvoiceAddress());
                } elseif (isset($customerData['quiqqer.erp.address'])) {
                    try {
    
                        $User = QUI::getUsers()->get($this->Customer->getUUID());
    
    Henning Leutz's avatar
    Henning Leutz committed
                        $Address = $User->getAddress($customerData['quiqqer.erp.address']);
                        $this->Customer->setAddress($Address);
                    } catch (QUI\Exception $Exception) {
                        QUI\System\Log::writeDebugException($Exception);
                    }
                }
    
            // currency
            if (!empty($data['currency_data'])) {
                $currency = json_decode($data['currency_data'], true);
    
                if (is_string($currency)) {
                    $currency = json_decode($currency, true);
                }
    
                if ($currency && isset($currency['code'])) {
                    try {
                        $this->Currency = QUI\ERP\Currency\Handler::getCurrency($currency['code']);
                    } catch (QUI\Exception $Exception) {
                        QUI\System\Log::addDebug($Exception->getMessage());
                    }
                }
            }
    
            if ($this->Currency === null) {
                $this->Currency = QUI\ERP\Defaults::getCurrency();
            }
    
    
            // articles
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->Articles = new ArticleList();
    
            $this->Articles->setCurrency($this->Currency);
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            if (isset($data['articles'])) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                $articles = json_decode($data['articles'], true);
    
    Henning Leutz's avatar
    Henning Leutz committed
    
                if ($articles) {
                    try {
                        $this->Articles = new ArticleList($articles);
    
                        $this->Articles->setCurrency($this->Currency);
    
    Henning Leutz's avatar
    Henning Leutz committed
                    } catch (QUI\ERP\Exception $Exception) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                        QUI\System\Log::addError($Exception->getMessage());
                    }
                }
            }
    
    Henning Leutz's avatar
    Henning Leutz committed
            $Customer = $this->getCustomer();
            $Customer->setAddress($this->getDeliveryAddress());
    
            $this->Articles->setUser($Customer);
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->Articles->calc();
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            // comments
            $this->Comments = new QUI\ERP\Comments();
    
            if (isset($data['comments'])) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                $this->Comments = QUI\ERP\Comments::unserialize($data['comments']);
    
    Henning Leutz's avatar
    Henning Leutz committed
            // history
            $this->History = new QUI\ERP\Comments();
    
            if (isset($data['history'])) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                $this->History = QUI\ERP\Comments::unserialize($data['history']);
    
    Henning Leutz's avatar
    Henning Leutz committed
            // frontend messages
            $this->FrontendMessage = new QUI\ERP\Comments();
    
            if (isset($data['frontendMessages'])) {
                $this->FrontendMessage = QUI\ERP\Comments::unserialize($data['frontendMessages']);
            }
    
    
            // status mail
            $this->StatusMails = new QUI\ERP\Comments();
    
            if (isset($data['status_mails'])) {
                $this->StatusMails = QUI\ERP\Comments::unserialize($data['status_mails']);
            }
    
    
            $this->paymentId = $data['payment_id'];
    
            $this->successful = (int)$data['successful'];
    
            if ($data['paid_data'] === null) {
                $data['paid_data'] = '';
            }
    
    
                'paid_status' => (int)$data['paid_status'],
                'paid_data' => json_decode($data['paid_data'], true),
                'paid_date' => $data['paid_date'],
    
                'temporary_invoice_id' => $data['temporary_invoice_id'],
    
            if (isset($data['payment_data'])) {
                $paymentData = QUI\Security\Encryption::decrypt($data['payment_data']);
    
    Henning Leutz's avatar
    Henning Leutz committed
                $paymentData = json_decode($paymentData, true);
    
                if (is_array($paymentData)) {
                    $this->paymentData = $paymentData;
                }
    
    Henning Leutz's avatar
    Henning Leutz committed
            // shipping
    
            if (is_numeric($data['shipping_id'])) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                $this->shippingId = (int)$data['shipping_id'];
    
                // validate shipping
    
                try {
                    $this->validateShipping($this->getShipping());
    
                    $this->shippingId = null;
    
            try {
                $this->Status = ProcessingHandler::getInstance()->getProcessingStatus($data['status']);
                $this->status = (int)$data['status'];
    
            } catch (QUI\ERP\Order\ProcessingStatus\Exception) {
    
            if (
                QUI::getPackageManager()->isInstalled('quiqqer/shipping')
                && isset($data['shipping_status'])
                && class_exists('QUI\ERP\Shipping\ShippingStatus\Handler')
                && class_exists('QUI\ERP\Shipping\ShippingStatus\Exception')
            ) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                    $this->ShippingStatus = ShippingStatusHandler::getInstance()->getShippingStatus(
                        $data['shipping_status']
                    );
    
                } catch (QUI\ERP\Shipping\ShippingStatus\Exception $Exception) {
                    QUI\System\Log::addWarning($Exception->getMessage());
                }
            }
    
        /**
         * @param string $reason
         * @param QUI\Interfaces\Users\User|null $PermissionUser
         * @return ErpEntityInterface|null
         */
        public function reversal(string $reason = '', QUI\Interfaces\Users\User $PermissionUser = null): ?ErpEntityInterface
        {
            $this->delete($PermissionUser);
            return null;
        }
    
    
         * Set the successful status to the order
         *
    
         * @throws QUI\Exception
         * @throws QUI\ExceptionStack
         */
    
        public function setSuccessfulStatus(): void
    
        {
            if ($this->successful === 1) {
                return;
            }
    
    
            try {
                QUI::getEvents()->fireEvent('quiqqerOrderSuccessful', [$this]);
            } catch (\Exception $Exception) {
                QUI\System\Log::addError($Exception->getMessage());
            }
    
    Henning Leutz's avatar
    Henning Leutz committed
            if ($this->isApproved()) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                $this->triggerApprovalEvent();
    
            $this->addHistory(
                QUI::getLocale()->get(
                    'quiqqer/order',
                    'order.history.set_successful'
                )
            );
    
    
        /**
         * Recalculate all article prices
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @param $Basket - optional
         *
    
         * @throws Basket\Exception
         * @throws QUI\ERP\Exception
         * @throws QUI\Exception
         */
    
        public function recalculate($Basket = null): void
    
            if ($this instanceof Order) {
                $this->Articles->recalculate();
                $this->save();
    
                return;
            }
    
    
            if ($this instanceof OrderInProcess && $this->getOrderId()) {
                // if an order exists for the order in process
                // never recalculate it
                return;
            }
    
    
            if ($this instanceof OrderInProcess && $this->isSuccessful()) {
                // if the order is successful
                // never recalculate it
                return;
            }
    
    
            $Customer = $this->getCustomer();
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            if ($Basket === null) {
    
                $Basket = new QUI\ERP\Order\Basket\BasketOrder($this->getUUID(), $Customer);
    
    
            $ArticleList = new ArticleList();
    
            $ArticleList->setUser($Customer);
    
            $ArticleList->setCurrency($this->getCurrency());
    
    
            $ProductCalc = QUI\ERP\Products\Utils\Calc::getInstance();
    
            $ProductCalc->setUser($Customer);
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            $Products = $Basket->getProducts();
    
            $Products->setUser($Customer);
    
            $Products->calc($ProductCalc);
    
            $products = $Products->getProducts();
    
            foreach ($products as $Product) {
                try {
                    /* @var QUI\ERP\Order\Basket\Product $Product */
                    $ArticleList->addArticle($Product->toArticle(null, false));
    
                    // @todo hinweis an benutzer, das artikel nicht mit aufgenommen werden konnte
                }
            }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            $Products->getPriceFactors()->clear();
    
            QUI::getEvents()->fireEvent(
                'quiqqerOrderBasketToOrder',
    
            QUI::getEvents()->fireEvent(
                'quiqqerOrderBasketToOrderEnd',
    
            $ArticleList->importPriceFactors(
                $Products->getPriceFactors()->toErpPriceFactorList()
            );
    
    
            $ArticleList->calc();
    
            $this->Articles = $ArticleList;
    
            $this->setCustomer($Customer);
    
            $this->update(QUI::getUsers()->getSystemUser());
    
        /**
         * Clears the complete order
         *
         * @param QUI\Interfaces\Users\User|null $PermissionUser - optional, permission user, default = session user
         */
    
        abstract public function clear(null | QUI\Interfaces\Users\User $PermissionUser = null);
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * Refresh the order data
         * fetch the data from the database
         */
        abstract public function refresh();
    
    
        /**
         * Updates the order
         *
         * @param QUI\Interfaces\Users\User|null $PermissionUser - optional, permission user, default = session user
    
         * @throws QUI\Exception
    
        abstract public function update(null | QUI\Interfaces\Users\User $PermissionUser = null);
    
        /**
         * Delete the order
         *
         * @param QUI\Interfaces\Users\User|null $PermissionUser - optional, permission user, default = session user
         */
    
        abstract public function delete(null | QUI\Interfaces\Users\User $PermissionUser = null);
    
        /**
         * Is the order posted / submitted
         *
         * @return bool
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        abstract public function isPosted(): bool;
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    
        //region getter
    
    
        /**
         * Return the order as an array
         *
         * @return array
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function toArray(): array
    
            $status = '';
    
            $shippingStatus = false;
    
            $paymentId = '';
            $paidStatus = [];
    
            $articles = $this->getArticles()->toArray();
            $Payment = $this->getPayment();
    
            $ProcessingStatus = $this->getProcessingStatus();
    
            if ($ProcessingStatus) {
                $status = $ProcessingStatus->getId();
    
            try {
                $paidStatus = $this->getPaidStatusInformation();
            } catch (QUI\Exception $Exception) {
                QUI\System\Log::writeDebugException($Exception);
            }
    
    
            if ($this->getShippingStatus()) {
                $shippingStatus = $this->getShippingStatus()->getId();
            }
    
            $shipping = '';
    
            if ($this->getShipping()) {
                $shipping = $this->getShipping()->getId();
            }
    
    
                'uuid' => $this->getUUID(),
                'hash' => $this->hash, // @deprecated
                'entityType' => $this->getType(),
                'globalProcessId' => $this->getGlobalProcessId(),
    
    
                'id' => $this->id,
    
                'prefixedId' => $this->getPrefixedNumber(),
                'prefixedNumber' => $this->getPrefixedNumber(),
    
                'invoiceId' => $this->invoiceId,
    
                'cDate' => $this->cDate,
                'cUser' => $this->cUser,
                'cUsername' => $this->getCreateUser()->getName(),
                'data' => $this->data,
                'customerId' => $this->customerId,
                'customer' => $this->getCustomer()->getAttributes(),
                'comments' => $this->getComments()->toArray(),
    
                'statusMails' => $this->getStatusMails()->toArray(),
    
                'currency' => $this->getCurrency()->toArray(),
    
                'project_name' => $this->getAttribute('project_name'),
    
    
                'articles' => $articles,
                'hasDeliveryAddress' => $this->hasDeliveryAddress(),
                'addressDelivery' => $this->getDeliveryAddress()->getAttributes(),
                'addressInvoice' => $this->getInvoiceAddress()->getAttributes(),
                'paymentId' => $paymentId,
                'status' => $status,
                'paidStatus' => $paidStatus,
                'shippingStatus' => $shippingStatus,
                'shipping' => $shipping,
                'shippingTracking' => $this->getDataEntry('shippingTracking'),
    
                'shippingConfirmation' => $this->getDataEntry('shippingConfirmation')
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * Return the order id
         *
         * @return integer
    
         * @deprecated
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getId(): int
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
            return $this->id;
        }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @return string
    
        public function getIdPrefix(): string
    
        {
            if ($this->idPrefix !== null) {
                return $this->idPrefix;
            }
    
            return QUI\ERP\Order\Utils\Utils::getOrderPrefix();
        }
    
    
        /**
         * Return the real order id for the customer
         *
         * @return string
    
         * @deprecated use getPrefixedNumber())
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getPrefixedId(): string
    
        {
            return $this->getPrefixedNumber();
        }
    
        /**
         * @return string
         */
        public function getPrefixedNumber(): string
    
            return $this->idStr;
    
        /**
         * Return the order id
         * -> alias for getId(), this method is used in the calc classes
         *
         * @return integer
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getCleanId(): int
    
        public function getGlobalProcessId(): string
    
        /**
         * @return QUI\ERP\Accounting\Calculations
    
         * @throws QUI\ERP\Exception
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getPriceCalculation(): QUI\ERP\Accounting\Calculations
    
            return new QUI\ERP\Accounting\Calculations(
    
                $this->Articles->getCalculations(),
                $this->Articles->getArticles()
            );
    
        /**
         * Is the order successful
         * -> Ist der Bestellablauf erfolgreich abgeschlossen
         *
         * @return int
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function isSuccessful(): int
    
        {
            return $this->successful;
        }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
    
         * OrderProcess was successful and payment process was successful
    
    Henning Leutz's avatar
    Henning Leutz committed
         *
         * @return bool
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function isApproved(): bool
    
                if (!$this->getPayment()) {
                    return false;
                }
    
    
                $isApproved = $this->getPayment()->getPaymentType()->isApproved($this->getUUID());
    
                $isSuccessful = $this->isSuccessful();
    
                return $isApproved && $isSuccessful;
    
            } catch (\Exception $Exception) {
                QUI\System\Log::writeException($Exception);
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * @return string
         */
        public function getInvoiceType(): string
        {
            try {
                return $this->getInvoice()->getType();
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
    
         * Return invoice address
    
    Henning Leutz's avatar
    Henning Leutz committed
         *
    
         * @return QUI\ERP\Address
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getInvoiceAddress(): QUI\ERP\Address
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
    
            return new QUI\ERP\Address($this->addressInvoice, $this->getCustomer());
    
    Henning Leutz's avatar
    Henning Leutz committed
        }
    
        /**
    
         * Return delivery address
    
    Henning Leutz's avatar
    Henning Leutz committed
         *
    
         * @return QUI\ERP\Address
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getDeliveryAddress(): QUI\ERP\Address
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
    
            $delivery = $this->addressDelivery;
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            if (isset($delivery['id']) && $delivery['id'] === -1) { // quiqqer/order#156
                return $this->getInvoiceAddress();
            }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            // quiqqer/order#156
            // cleanup, to check the delivery address
    
    Henning Leutz's avatar
    Henning Leutz committed
            if (is_array($delivery)) {
                $delivery = array_filter($delivery);
            }
    
            if (isset($delivery['id'])) {
                unset($delivery['id']);
            }
    
            if (empty($delivery)) {
    
                return $this->getInvoiceAddress();
            }
    
            return new QUI\ERP\Address($this->addressDelivery, $this->getCustomer());
    
    Henning Leutz's avatar
    Henning Leutz committed
        }
    
        /**
         * Return the order articles list
         *
         * @return ArticleList
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getArticles(): ArticleList
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
    
            if (!$this->Articles) {
                $this->Articles = new ArticleList();
            }
    
    
            $this->Articles->setOrder($this);
    
            $this->Articles->setUser($this->getCustomer());
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->Articles->setCurrency($this->getCurrency());
    
            $this->Articles->calc();
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            return $this->Articles;
        }
    
    
        /**
         * Return the order create date
         *
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @return string (Y-m-d H:i:s)
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getCreateDate(): string
    
        {
            return $this->cDate;
        }
    
    
        /**
         * Return the order create date
         *
    
         * @return QUI\Interfaces\Users\User|null
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getCreateUser(): ?QUI\Interfaces\Users\User
    
        {
            try {
                return QUI::getUsers()->get($this->cUser);
            } catch (QUI\Exception $Exception) {
                QUI\System\Log::writeDebugException($Exception);
            }
    
            return QUI::getUsers()->getSystemUser();
        }
    
    
        /**
         * Return extra data array
         *
         * @return array
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getData(): array
    
        {
            return $this->data;
        }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * Return a data entry
         *
         * @param string $key
         * @return mixed|null
         */
    
        public function getDataEntry(string $key): mixed
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
            if (isset($this->data[$key])) {
                return $this->data[$key];
            }
    
            return null;
        }
    
    
        /**
         * @return string
         * @deprecated use getUUID()
         */
        public function getHash(): string
        {
            return $this->getUUID();
        }
    
    
        /**
         * Return the hash
         *
         * @return string
         */
    
        public function getUUID(): string
    
        {
            return $this->hash;
        }
    
        /**
    
         * Return the customer of the order
         *
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @return null|QUI\ERP\User
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getCustomer(): ?QUI\ERP\User
    
            $Nobody = QUI\ERP\User::convertUserToErpUser(QUI::getUsers()->getNobody());
    
            if (!$this->customerId && !$this->Customer) {
                return $Nobody;
            }
    
    
            if ($this->Customer) {
                $Address = $this->Customer->getStandardAddress();
    
    
                if (!$Address->getUUID()) {
    
                    $this->Customer->setAddress(
                        new QUI\ERP\Address($this->addressInvoice, $this->Customer)
                    );
                }
    
                return $this->Customer;
            }
    
    
            if ($this->customerId) {
                try {