Skip to content
Code-Schnipsel Gruppen Projekte
ArticleListUnique.php 6,65 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    
    /**
     * This file contains QUI\ERP\Accounting\ArticleList
     */
    
    namespace QUI\ERP\Accounting;
    
    
     * - Nicht änderbare Artikel Liste
    
     *
     * @package QUI\ERP\Accounting
     */
    class ArticleListUnique
    {
        /**
         * @var array
         */
    
        protected $articles = [];
    
        protected $calculations = [];
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * @var bool|mixed
         */
        protected $showHeader;
    
    
        /**
         * PriceFactor List
         *
         * @var QUI\ERP\Products\Utils\PriceFactors
         */
        protected $PriceFactors = false;
    
    
        /**
         * ArticleList constructor.
         *
         * @param array $attributes
    
         * @throws QUI\ERP\Exception
    
        public function __construct($attributes = [])
    
            $needles = ['articles', 'calculations'];
    
    
            foreach ($needles as $needle) {
                if (!isset($attributes[$needle])) {
                    throw new QUI\ERP\Exception(
                        'Missing needle for ArticleListUnique',
                        400,
    
                            'class'   => 'ArticleListUnique',
                            'missing' => $needle
    
            $articles = $attributes['articles'];
    
            foreach ($articles as $article) {
                $this->articles[] = new Article($article);
            }
    
    
            $this->calculations = $attributes['calculations'];
    
            $this->showHeader   = isset($attributes['showHeader']) ? $attributes['showHeader'] : true;
    
    
    
            // price factors
            $this->PriceFactors = new QUI\ERP\Products\Utils\PriceFactors();
    
            if (isset($attributes['priceFactors'])) {
                $this->PriceFactors->importList($attributes['priceFactors']);
            }
    
        }
    
        /**
         * Creates a list from a stored representation
         *
         * @param string $data
         * @return ArticleListUnique
    
         *
         * @throws QUI\Exception
    
         */
        public static function unserialize($data)
        {
            if (is_string($data)) {
                $data = json_decode($data, true);
            }
    
            return new self($data);
        }
    
        /**
         * Generates a storable representation of the list
         *
         * @return string
         */
        public function serialize()
        {
            return json_encode($this->toArray());
        }
    
    
        /**
         * Return the calculation array
         *
         * @return array
         */
        public function getCalculations()
        {
            return $this->calculations;
        }
    
        /**
         * Return the list articles
         *
    
    Patrick Müller's avatar
    Patrick Müller committed
         * @return Article[]
    
         */
        public function getArticles()
        {
            return $this->articles;
        }
    
    
        /**
         * Return the number of articles
         *
         * @return int
         */
        public function count()
        {
            return count($this->articles);
        }
    
    
        /**
         * Generates a storable json representation of the list
         * Alias for serialize()
         *
         * @return string
         */
        public function toJSON()
        {
            return $this->serialize();
        }
    
    
        /**
         * Return the list as an array
         *
         * @return array
         */
        public function toArray()
        {
            $articles = array_map(function ($Article) {
                /* @var $Article Article */
                return $Article->toArray();
            }, $this->articles);
    
    
            $this->PriceFactors->sort();
    
            return [
    
                'articles'     => $articles,
    
                'calculations' => $this->calculations,
                'priceFactors' => $this->PriceFactors->toArray()
            ];
    
        /**
         * Display of the header = true
         */
        public function displayHeader()
        {
            $this->showHeader = true;
        }
    
        /**
         * Display of the header = false
         */
        public function hideHeader()
        {
            $this->showHeader = false;
        }
    
    
         * Return the Article List as HTML, without CSS
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @param string|bool $template - custom template
    
         *
         * @throws QUI\Exception
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function toHTML($template = false)
    
            $Engine   = QUI::getTemplateManager()->getEngine();
    
            $vatArray = [];
    
    
            $Currency = QUI\ERP\Currency\Handler::getCurrency(
                $this->calculations['currencyData']['code']
            );
    
            if ($this->calculations['vatArray']) {
                $vatArray = $this->calculations['vatArray'];
            }
    
            // price display
            foreach ($vatArray as $key => $vat) {
                $vatArray[$key]['sum'] = $Currency->format($vatArray[$key]['sum']);
            }
    
            $this->calculations['sum']         = $Currency->format($this->calculations['sum']);
            $this->calculations['subSum']      = $Currency->format($this->calculations['subSum']);
            $this->calculations['nettoSum']    = $Currency->format($this->calculations['nettoSum']);
            $this->calculations['nettoSubSum'] = $Currency->format($this->calculations['nettoSubSum']);
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            $pos = 1;
    
    Henning Leutz's avatar
    Henning Leutz committed
            $articles = array_map(function ($Article) use ($Currency, &$pos) {
    
                /* @var $Article Article */
    
    Henning Leutz's avatar
    Henning Leutz committed
                $View = $Article->getView();
                $View->setCurrency($Currency);
                $View->setPosition($pos);
    
    Henning Leutz's avatar
    Henning Leutz committed
                $pos++;
    
    Henning Leutz's avatar
    Henning Leutz committed
                return $View;
    
            }, $this->articles);
    
    
            // price factors
            $priceFactors = $this->PriceFactors->sort();
            $priceFactors = array_map(function ($Factor) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                /* @var $Factor QUI\ERP\Products\Utils\PriceFactor */
    
                return $Factor->toArray();
            }, $priceFactors);
    
    
            $Engine->assign([
                'priceFactors' => $priceFactors,
    
                'showHeader'   => $this->showHeader,
    
                'this'         => $this,
                'articles'     => $articles,
                'calculations' => $this->calculations,
                'vatArray'     => $vatArray
    
    Henning Leutz's avatar
    Henning Leutz committed
            if ($template && file_exists($template)) {
                return $Engine->fetch($template);
            }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            return $Engine->fetch(dirname(__FILE__).'/ArticleList.html');
    
        }
    
        /**
         * Return the Article List as HTML, with CSS
         *
         * @return string
    
         *
         * @throws QUI\Exception
    
         */
        public function toHTMLWithCSS()
        {
            $style = '<style>';
    
    Henning Leutz's avatar
    Henning Leutz committed
            $style .= file_get_contents(dirname(__FILE__).'/ArticleList.css');
    
            $style .= '</style>';
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            return $style.$this->toHTML();
    
    
        /**
         * Alias for toHTMLWithCSS
         *
         * @return string
    
         *
         * @throws QUI\Exception
    
         */
        public function render()
        {
            return $this->toHTMLWithCSS();
        }
    
    
        //region Price Factors
    
        /**
         * Return the price factors list (list of price indicators)
         *
         * @return QUI\ERP\Products\Utils\PriceFactors
         */
        public function getPriceFactors()
        {
            return $this->PriceFactors;
        }
    
        //endregion