Newer
Older
<?php
/**
* This file contains QUI\ERP\Accounting\ArticleList
*/
namespace QUI\ERP\Accounting;
use QUI\ERP\Accounting\PriceFactors\FactorList as ErpFactorList;
/**
* Class ArticleListUnique
*
* @package QUI\ERP\Accounting
*/

Henning Leutz
committed
class ArticleListUnique implements \IteratorAggregate
{
/**
/**
* @var bool|mixed
*/
protected $showHeader;
* @var QUI\ERP\Accounting\PriceFactors\FactorList
*/
protected $PriceFactors = false;
/**
* @var null
*/
protected $Locale = null;

Henning Leutz
committed
/**
* @var QUI\Interfaces\Users\User
*/
protected $User = null;
/**
* ArticleList constructor.
*
* @param array $attributes

Henning Leutz
committed
* @param null|QUI\Interfaces\Users\User|QUI\Users\User $User

Henning Leutz
committed
public function __construct($attributes = [], $User = null)
$this->Locale = QUI::getLocale();
$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) {
if (!isset($article['class'])) {
$this->articles[] = new Article($article);
continue;
}
$class = $article['class'];
$this->articles[] = new Article($article);
continue;
}
if (isset($interfaces[ArticleInterface::class])) {
$this->articles[] = new $class($article);
continue;
}
$this->articles[] = new Article($article);
}

Henning Leutz
committed
if ($User) {
$this->User = $User;
foreach ($this->articles as $Article) {
$Article->setUser($this->User);
}
}
$this->calculations = $attributes['calculations'];
$this->showHeader = isset($attributes['showHeader']) ? $attributes['showHeader'] : true;
$this->PriceFactors = new ErpFactorList();
if (isset($attributes['priceFactors'])) {
$this->PriceFactors = new ErpFactorList($attributes['priceFactors']);
} catch (QUI\ERP\Exception $Exception) {
QUI\System\Log::writeRecursive(
$attributes['priceFactors'],
QUI\System\Log::LEVEL_DEBUG
);
QUI\System\Log::writeDebugException($Exception);
}
/**
* placeholder. unique list cant be recalculate
* recalculate makes the unique article list compatible to the article list
*
* @param $Calc
*/
public function recalculate($Calc = null)
{
// placeholder. unique list cant be recalculate
}
/**
* placeholder. unique list cant be calc
* calc makes the unique article list compatible to the article list
*
* @param $Calc
* @return $this
*/

Henning Leutz
committed
public function calc($Calc = null): ArticleListUnique
{
// placeholder. unique list cant be calc
return $this;
}
/**
* Set locale
*
* @param QUI\Locale $Locale
*/
public function setLocale(QUI\Locale $Locale)
{
$this->Locale = $Locale;
}
/**
* Creates a list from a stored representation
*
* @param string $data
* @return ArticleListUnique

Henning Leutz
committed
public static function unserialize(string $data): ArticleListUnique
if (\is_string($data)) {
$data = \json_decode($data, true);
}
return new self($data);
}
/**
* Generates a storable representation of the list
*
* @return string
*/

Henning Leutz
committed
public function serialize(): string
/**
* Return the calculation array
*
* @return array
*/

Henning Leutz
committed
public function getCalculations(): array
{
return $this->calculations;
}
/**
* Return the list articles
*

Henning Leutz
committed
public function getArticles(): array
{
return $this->articles;
}
/**
* Return the number of articles
*
* @return int
*/

Henning Leutz
committed
public function count(): int
/**
* Generates a storable json representation of the list
* Alias for serialize()
*
* @return string
*/

Henning Leutz
committed
public function toJSON(): string
{
return $this->serialize();
}
/**
* Return the list as an array
*
* @return array
*/

Henning Leutz
committed
public function toArray(): array
return $Article->toArray();
}, $this->articles);
$this->PriceFactors->toArray();
'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
* @return string

Henning Leutz
committed
public function toHTML($template = false): string
$Engine = QUI::getTemplateManager()->getEngine();

Henning Leutz
committed
if (!$this->count()) {
return '';
}
$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']);
$articles = \array_map(function ($Article) use ($Currency, &$pos) {
$View = $Article->getView();
$View->setCurrency($Currency);
$View->setPosition($pos);
}, $this->articles);
// output
'priceFactors' => $this->PriceFactors->toArray(),
'this' => $this,
'articles' => $articles,
'calculations' => $this->calculations,
'vatArray' => $vatArray,
'Locale' => $this->Locale
return $Engine->fetch(\dirname(__FILE__).'/ArticleList.html');
/**
* @return string
* @throws QUI\Exception
*/

Henning Leutz
committed
public function toMailHTML(): string
{
return $this->toHTML(\dirname(__FILE__).'/ArticleList.Mail.html');
}
/**
* Return the Article List as HTML, with CSS
*
* @return string

Henning Leutz
committed
public function toHTMLWithCSS(): string
$style .= \file_get_contents(\dirname(__FILE__).'/ArticleList.css');
/**
* Alias for toHTMLWithCSS
*
* @return string
*/
public function render()
{
return $this->toHTMLWithCSS();
}
/**
* Render the article list for mails
*
* @return string
*
* @throws QUI\Exception
*/

Henning Leutz
committed
public function renderForMail(): string
{
$style = '<style>';
$style .= \file_get_contents(\dirname(__FILE__).'/ArticleList.Mail.css');
$style .= '</style>';
return $style.$this->toMailHTML();
}
//region Price Factors
/**
* Return the price factors list (list of price indicators)
*
* @return QUI\ERP\Accounting\PriceFactors\FactorList
*/
public function getPriceFactors()
{
return $this->PriceFactors;
}
//endregion

Henning Leutz
committed
//region iterator
/**
* Iterator helper
*
* @return \ArrayIterator|\Traversable
*/
public function getIterator()
{
return new \ArrayIterator($this->articles);
}
//endregion