Skip to content
Code-Schnipsel Gruppen Projekte
Process.php 7,13 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    
    /**
     * This file contains QUI\ERP\Process
     */
    
    namespace QUI\ERP;
    
    use QUI;
    
    
    Henning Leutz's avatar
    Henning Leutz committed
    use function count;
    
    use function strtotime;
    
     *
     * @package QUI\ERP
     */
    class Process
    {
        /**
         * @var string
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected string $processId;
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?array $transactions = null;
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected ?Comments $History = null;
    
        /**
         * Process constructor.
         *
         * @param string $processId - the global process id
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function __construct(string $processId)
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected function table(): string
    
        /**
         * Add a comment to the history for the complete process
         *
         * @param string $message
         * @param int|bool $time - optional, unix timestamp
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function addHistory(string $message, $time = false)
    
        {
            $this->getHistory()->addComment($message, $time);
    
    Henning Leutz's avatar
    Henning Leutz committed
            try {
                QUI::getDataBase()->update(
                    $this->table(),
                    ['history' => $this->getHistory()->toJSON()],
                    ['id' => $this->processId]
                );
            } catch (\QUI\Exception $Exception) {
                QUI\System\Log::addError($Exception->getMessage());
            }
    
         * Return the history of the process
         * This history only contains the process history
         *
         * If you want the complete history of all process objects, use getCompleteHistory()
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getHistory(): Comments
    
    Henning Leutz's avatar
    Henning Leutz committed
                try {
                    $result = QUI::getDataBase()->fetch([
    
                        'from' => $this->table(),
    
    Henning Leutz's avatar
    Henning Leutz committed
                        'where' => [
                            'id' => $this->processId
                        ],
                        'limit' => 1
    
    Henning Leutz's avatar
    Henning Leutz committed
    
                    if (isset($result[0]['history'])) {
                        $history = $result[0]['history'];
                    } else {
                        QUI::getDataBase()->insert($this->table(), [
                            'id' => $this->processId
                        ]);
                    }
                } catch (\QUI\Exception $Exception) {
                    QUI\System\Log::addError($Exception->getMessage());
    
                }
    
                $this->History = QUI\ERP\Comments::unserialize($history);
            }
    
        /**
         * Return a complete history of all process objects
    
    Henning Leutz's avatar
    Henning Leutz committed
         * invoices and orders
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getCompleteHistory(): Comments
    
        {
            $History = $this->getHistory();
    
            $invoices = $this->getInvoices();
    
            $orders = $this->getOrders();
    
                $History->addComment(
                    QUI::getLocale()->get('quiqqer/erp', 'created.invoice', [
                        'invoiceId' => $Invoice->getId()
                    ]),
                    strtotime($Invoice->getAttribute('date'))
                );
    
    
                $History->import($Invoice->getHistory());
            }
    
            foreach ($orders as $Order) {
    
                $History->addComment(
                    QUI::getLocale()->get('quiqqer/erp', 'created.order', [
                        'orderId' => $Order->getId()
                    ]),
                    strtotime($Order->getAttribute('date'))
                );
    
    
            QUI::getEvents()->fireEvent('quiqqerErpGetCompleteHistory', [$this]);
    
    
        //endregion
    
        //region invoice
    
        /**
         * Return if the process has invoices or not
         *
         * @return bool
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function hasInvoice(): bool
    
        {
            $invoices = $this->getInvoices();
    
            foreach ($invoices as $Invoice) {
                if ($Invoice instanceof QUI\ERP\Accounting\Invoice\Invoice) {
                    return true;
                }
            }
    
            return false;
        }
    
        /**
         * Return if the process has temporary invoices or not
         *
         * @return bool
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function hasTemporaryInvoice(): bool
    
        {
            $invoices = $this->getInvoices();
    
            foreach ($invoices as $Invoice) {
                if ($Invoice instanceof QUI\ERP\Accounting\Invoice\InvoiceTemporary) {
                    return true;
                }
            }
    
            return false;
        }
    
        /**
    
         * @return Accounting\Invoice\Invoice[]|Accounting\Invoice\InvoiceTemporary[]
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getInvoices(): array
    
    Henning Leutz's avatar
    Henning Leutz committed
            try {
                return QUI\ERP\Accounting\Invoice\Handler::getInstance()->getInvoicesByGlobalProcessId($this->processId);
            } catch (\QUI\Exception $Exception) {
                return [];
            }
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function hasOrder(): bool
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @return null|Order\Order|Order\OrderInProcess
    
         */
        public function getOrder()
        {
            try {
                QUI::getPackage('quiqqer/order');
            } catch (QUI\Exception $Exception) {
                return null;
            }
    
            $OrderHandler = QUI\ERP\Order\Handler::getInstance();
    
            try {
                return $OrderHandler->getOrderByGlobalProcessId($this->processId);
            } catch (QUI\Exception $Exception) {
                QUI\System\Log::writeDebugException($Exception);
            }
    
            try {
                return $OrderHandler->getOrderByHash($this->processId);
            } catch (QUI\Exception $Exception) {
                QUI\System\Log::writeDebugException($Exception);
            }
    
            return null;
        }
    
    
        /**
         * Return all orders from the process
         *
         * @return array|Order\Order|Order\Order[]|Order\OrderInProcess
         */
        public function getOrders()
        {
            try {
                QUI::getPackage('quiqqer/order');
    
    Henning Leutz's avatar
    Henning Leutz committed
    
                return QUI\ERP\Order\Handler::getInstance()->getOrdersByGlobalProcessId($this->processId);
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function hasTransactions(): bool
    
    Henning Leutz's avatar
    Henning Leutz committed
            return !!count($transactions);
    
        }
    
        /**
         * Return all related transactions
         *
         * @return QUI\ERP\Accounting\Payments\Transactions\Transaction[];
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function getTransactions(): ?array
    
        {
            try {
                QUI::getPackage('quiqqer/payment-transactions');
            } catch (QUI\Exception $Exception) {
                return [];
            }
    
            if ($this->transactions !== null) {
                return $this->transactions;
            }
    
    
            $Transactions = QUI\ERP\Accounting\Payments\Transactions\Handler::getInstance();
    
            $this->transactions = $Transactions->getTransactionsByProcessId($this->processId);
    
            return $this->transactions;
        }
    
        //endregion
    }