384 lines
7.9 KiB
PHP
384 lines
7.9 KiB
PHP
<?php
|
|
namespace PSC\Library\Calc;
|
|
|
|
use Doctrine\Common\Persistence\ObjectRepository;
|
|
use PSC\Library\Calc\Calc\Calc;
|
|
use PSC\Library\Calc\Calc\CalcValues;
|
|
use PSC\Library\Calc\Calc\Valid;
|
|
use PSC\Library\Calc\Option\Type\Base;
|
|
use PSC\Library\Calc\Option\Type\PaperDbSelect;
|
|
use PSC\Library\Calc\Option\Type\Select\Opt;
|
|
use PSC\Library\Calc\Option\Type\Text;
|
|
|
|
class Engine
|
|
{
|
|
|
|
/** @var \SimpleXMLElement $xml */
|
|
protected $xml;
|
|
|
|
/** @var \ArrayIterator $articles */
|
|
protected $articles;
|
|
|
|
/** @var PaperContainer $paperContainer */
|
|
protected $paperContainer;
|
|
|
|
/** @var ObjectRepository */
|
|
protected $paperRepository;
|
|
|
|
/** @var string */
|
|
protected $formulas = "";
|
|
|
|
/** @var string */
|
|
protected $parameters = "";
|
|
|
|
/** @var array */
|
|
protected $variables = array();
|
|
|
|
/** @var array */
|
|
protected $calcVariables = array();
|
|
|
|
/** @var float */
|
|
protected $price = 0;
|
|
|
|
/** @var float */
|
|
protected $tax = 0;
|
|
|
|
/** @var bool */
|
|
protected $dirty = true;
|
|
|
|
/** @var Article */
|
|
protected $article = null;
|
|
|
|
/** @var string */
|
|
protected $activeArticle = false;
|
|
|
|
/** @var \SimpleXMLElement $templates */
|
|
protected $templates;
|
|
|
|
/**
|
|
* Load XML From String
|
|
*
|
|
* @param $string XML String
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function loadString($string)
|
|
{
|
|
$this->articles = new \ArrayIterator();
|
|
$this->xml = simplexml_load_string($string);
|
|
$this->parse();
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Parse Xml
|
|
*/
|
|
protected function parse()
|
|
{
|
|
$parser = new Parser();
|
|
$parser->setPaperContainer($this->paperContainer);
|
|
$parser->setPaperRepository($this->paperRepository);
|
|
if($this->templates) {
|
|
$parser->setTemplates($this->templates);
|
|
}
|
|
foreach ($this->xml as $article) {
|
|
$this->articles->append($parser->parse($article));
|
|
|
|
}
|
|
$this->dirty = true;
|
|
}
|
|
|
|
/**
|
|
* @return \ArrayIterator
|
|
*/
|
|
public function getArticles()
|
|
{
|
|
return $this->articles;
|
|
}
|
|
|
|
/**
|
|
* @param bool $name
|
|
* @return Article
|
|
* @throws \Exception
|
|
*/
|
|
private function getArticleByName($name = false)
|
|
{
|
|
if($name === false && $this->activeArticle === false) {
|
|
$this->activeArticle = $this->articles[0]->getName();
|
|
return $this->articles[0];
|
|
|
|
}
|
|
|
|
if($name === false && $this->activeArticle !== false) {
|
|
$name = $this->activeArticle;
|
|
|
|
}
|
|
|
|
/** @var Article $article */
|
|
foreach($this->articles as $article)
|
|
{
|
|
if($article->getName() == $name) {
|
|
return $article;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return PaperContainer
|
|
*/
|
|
public function getPaperContainer()
|
|
{
|
|
return $this->paperContainer;
|
|
}
|
|
|
|
/**
|
|
* @param PaperContainer $paperContainer
|
|
*/
|
|
public function setPaperContainer($paperContainer)
|
|
{
|
|
$this->dirty = true;
|
|
$this->paperContainer = $paperContainer;
|
|
}
|
|
|
|
/**
|
|
* @return ObjectRepository
|
|
*/
|
|
public function getPaperRepository()
|
|
{
|
|
return $this->paperRepository;
|
|
}
|
|
|
|
/**
|
|
* @param ObjectRepository $paperRepository
|
|
*/
|
|
public function setPaperRepository($paperRepository)
|
|
{
|
|
$this->dirty = true;
|
|
$this->paperRepository = $paperRepository;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getFormulas()
|
|
{
|
|
return $this->formulas;
|
|
}
|
|
|
|
/**
|
|
* @param string $formulas
|
|
*/
|
|
public function setFormulas($formulas)
|
|
{
|
|
$this->dirty = true;
|
|
$this->formulas = $formulas;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getParameters()
|
|
{
|
|
return $this->parameters;
|
|
}
|
|
|
|
/**
|
|
* @param string $parameters
|
|
*/
|
|
public function setParameters($parameters)
|
|
{
|
|
$this->dirty = true;
|
|
$this->parameters = $parameters;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function getVariables()
|
|
{
|
|
return $this->variables;
|
|
}
|
|
|
|
/**
|
|
* @param array $variables
|
|
*/
|
|
public function setVariables($variables)
|
|
{
|
|
$this->dirty = true;
|
|
$this->variables = $variables;
|
|
}
|
|
|
|
public function calc() {
|
|
|
|
$this->price = 0;
|
|
|
|
$this->article = $this->getArticleByName($this->activeArticle);
|
|
|
|
// Prefill with defaults
|
|
/** @var Base $option */
|
|
foreach($this->article->getOptions() as $option) {
|
|
if(!isset($this->variables[$option->getId()]) && $option->getDefault() !== null && !$option instanceof Text) {
|
|
$this->variables[$option->getId()] = $option->getDefault();
|
|
}
|
|
|
|
if(isset($this->variables[$option->getId()])) {
|
|
$option->setRawValue($this->variables[$option->getId()]);
|
|
$option->processValue();
|
|
}
|
|
}
|
|
|
|
// Check if Option is valid
|
|
$calcValid = new Valid($this, $this->article);
|
|
$calcValid->perform();
|
|
|
|
// CALC Values
|
|
$calcValues = new CalcValues($this, $this->article);
|
|
$calcValues->calc();
|
|
|
|
// CALC Values
|
|
$calcValues = new CalcValues($this, $this->article);
|
|
$calcValues->calc();
|
|
|
|
// Check if Option is valid
|
|
$calcValid = new Valid($this, $this->article);
|
|
$calcValid->perform(true);
|
|
|
|
// CALC Formel
|
|
$calcFormel = new Calc($this, $this->article);
|
|
$this->price = $calcFormel->calc();
|
|
|
|
$this->dirty = false;
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function getPrice()
|
|
{
|
|
if($this->dirty) {
|
|
$this->calc();
|
|
}
|
|
return round($this->price,2);
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function getTaxPrice()
|
|
{
|
|
if($this->dirty) {
|
|
$this->calc();
|
|
}
|
|
return round(($this->price/100*$this->tax),2);
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function getCompletePrice()
|
|
{
|
|
if($this->dirty) {
|
|
$this->calc();
|
|
}
|
|
return round($this->price+($this->price/100*$this->tax),2);
|
|
}
|
|
|
|
/**
|
|
* @param mixed $price
|
|
*/
|
|
public function setPrice($price)
|
|
{
|
|
$this->price = $price;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function getCalcVariables()
|
|
{
|
|
return $this->calcVariables;
|
|
}
|
|
|
|
/**
|
|
* @param array $calcVariables
|
|
*/
|
|
public function setCalcVariables($calcVariables)
|
|
{
|
|
$this->calcVariables = $calcVariables;
|
|
}
|
|
|
|
public function addCalcVariable($id, $value) {
|
|
$this->calcVariables[$id] = $value;
|
|
}
|
|
|
|
public function setVariable($var, $value)
|
|
{
|
|
$this->variables[$var] = $value;
|
|
}
|
|
|
|
/**
|
|
* @return float
|
|
*/
|
|
public function getTax()
|
|
{
|
|
return $this->tax;
|
|
}
|
|
|
|
/**
|
|
* @param float $tax
|
|
*/
|
|
public function setTax($tax)
|
|
{
|
|
$this->tax = $tax;
|
|
}
|
|
|
|
/**
|
|
* @return Article
|
|
*/
|
|
public function getArticle()
|
|
{
|
|
if($this->dirty) {
|
|
$this->calc();
|
|
}
|
|
return $this->article;
|
|
}
|
|
|
|
/**
|
|
* @param string $activeArticle
|
|
*/
|
|
public function setActiveArticle($activeArticle)
|
|
{
|
|
$this->activeArticle = $activeArticle;
|
|
$this->dirty = true;
|
|
}
|
|
|
|
public function setSymfonyFormVariables($data)
|
|
{
|
|
$tmp = [];
|
|
|
|
foreach($data as $key => $value) {
|
|
if($value instanceof Opt) {
|
|
$tmp[$key] = $value->getId();
|
|
}else{
|
|
$tmp[$key] = $value;
|
|
}
|
|
}
|
|
|
|
return $tmp;
|
|
}
|
|
|
|
public function setTemplates($templates)
|
|
{
|
|
$this->templates = simplexml_load_string($templates);
|
|
}
|
|
|
|
/**
|
|
* @return \SimpleXMLElement
|
|
*/
|
|
public function getTemplates(): \SimpleXMLElement
|
|
{
|
|
return $this->templates;
|
|
}
|
|
|
|
} |