calc/src/Calc/CalcValues.php
2020-01-29 15:13:12 +01:00

135 lines
3.8 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: thomas
* Date: 16.04.18
* Time: 14:00
*/
namespace PSC\Library\Calc\Calc;
use PSC\Library\Calc\Article;
use PSC\Library\Calc\Engine;
use PSC\Library\Calc\General\Type\Edge;
use PSC\Library\Calc\General\Type\EdgeCollection;
use PSC\Library\Calc\General\Type\EdgeCollectionContainer;
use PSC\Library\Calc\Option\Type\Base;
use PSC\Library\Calc\Option\Type\PaperDbSelect;
use PSC\Library\Calc\Option\Type\Select;
use PSC\Library\Calc\Tests\Mock\Paper;
use SebastianBergmann\CodeCoverage\Report\PHP;
class CalcValues
{
/** @var Engine */
protected $engine = null;
/** @var Article */
protected $article = null;
protected $storageFormulas = [];
protected $storageCalcValues = [];
protected $formelCalc = null;
/**
* Calc constructor.
* @param Engine $engine
* @param Article $article
*/
public function __construct($engine, $article)
{
$this->engine = $engine;
$this->article = $article;
$this->formelCalc = new Formel($engine, $article);
}
public function calc()
{
$price = 0;
/** @var Base $option */
foreach($this->article->getOptions() as $option) {
if($option instanceof Select) {
/** @var Select\Opt $opt */
foreach($option->getOptions() as $opt) {
if($opt->isValid() && $opt->isSelected()) {
$price = $this->parseEdgeCollection($price, $option->getId(), $opt->getEdgesCollectionContainer());
}
}
}
$this->parseEdgeCollection($price, $option->getId(), $option->getEdgesCollectionContainer());
}
return $price;
}
/**
* @param $price
* @param id
* @param EdgeCollectionContainer $container
* @return int
*/
private function parseEdgeCollection($price, $id, EdgeCollectionContainer $container, $isSub = false)
{
$calcValue1 = 0;
$calcValue2 = 0;
$calcValueAccount1 = 0;
$calcValueAccount2 = 0;
eval($this->engine->getParameters());
eval($this->engine->getFormulas());
/** @var EdgeCollection $collection */
foreach($container as $collection) {
if($collection->getName() == "opt") continue;
if($collection->getFormel() != "") {
$formel = $this->formelCalc->parse($collection->getFormel());
if(preg_match("/^[a-z](.*)/", $formel)) {
eval('$var = "' . $formel . '";');
}else{
eval('$var = ' . $formel . ';');
}
}else{
if(!isset($this->engine->getVariables()[$collection->getName()])) {
$var = 'XXXXXXXX';
}else{
$var = $this->engine->getVariables()[$collection->getName()];
}
}
/** @var Edge $edge */
foreach($collection as $edge) {
if($edge->isValid($var)) {
if($edge->getCalcValue() != "") {
$cv = $this->formelCalc->parse($edge->getCalcValue());
eval('$cv = ' . $cv . ';');
//echo $id . ' '. $this->formelCalc->parse($edge->getCalcValue()) . ' ' . $cv . ' '. PHP_EOL.PHP_EOL;
$this->engine->addCalcVariable($id, $cv);
$this->engine->addCalcVariable($id . '_' . $collection->getName(), $cv);
}
if($edge->getEdgesCollectionContainer()->count() > 0) {
$this->parseEdgeCollection($price, $id . '_' . $collection->getName(), $edge->getEdgesCollectionContainer(), true);
}
}
}
}
return $price;
}
}