From 498470c8d0787189d5e1133391513c66be3e905a Mon Sep 17 00:00:00 2001 From: Thomas Peterson Date: Tue, 24 Sep 2024 13:45:17 +0200 Subject: [PATCH] Fixes --- src/Calc/Calc.php | 66 +++++++++++--------- tests/Customer/DD/CalcTest.php | 12 ++-- tests/Customer/R/RadioboxCalcTest.php | 55 ++++++++++++++++ tests/Customer/R/calc_radiobox_with_calc.xml | 33 ++++++++++ 4 files changed, 128 insertions(+), 38 deletions(-) create mode 100644 tests/Customer/R/RadioboxCalcTest.php create mode 100644 tests/Customer/R/calc_radiobox_with_calc.xml diff --git a/src/Calc/Calc.php b/src/Calc/Calc.php index 26c1249..9609a2e 100644 --- a/src/Calc/Calc.php +++ b/src/Calc/Calc.php @@ -16,6 +16,7 @@ use PSC\Library\Calc\General\Type\EdgeCollectionContainer; use PSC\Library\Calc\Option\Type\Base; use PSC\Library\Calc\Option\Type\Checkbox; use PSC\Library\Calc\Option\Type\PaperDbSelect; +use PSC\Library\Calc\Option\Type\Radio; use PSC\Library\Calc\Option\Type\Select; use PSC\Library\Calc\Tests\Mock\Paper; @@ -50,12 +51,12 @@ class Calc $gesamt = 0; /** @var Base $option */ - foreach($this->article->getOptions() as $option) { + foreach ($this->article->getOptions() as $option) { - if($option instanceof Select || $option instanceof Checkbox) { + if ($option instanceof Select || $option instanceof Checkbox || $option instanceof Radio) { /** @var Select\Opt $opt */ - foreach($option->getOptions() as $opt) { - if($opt->isValid() && $opt->isSelected()) { + foreach ($option->getOptions() as $opt) { + if ($opt->isValid() && $opt->isSelected()) { $gesamt = $this->parseEdgeCollection($gesamt, $option, $opt->getEdgesCollectionContainer(), [$option->getId()]); } } @@ -82,47 +83,49 @@ class Calc $calcValueAccount2 = 0; /** @var EdgeCollection $collection */ - foreach($container as $collection) { + foreach ($container as $collection) { $var = 'XXXXXXXXXXXX'; - if($collection->getName() == "opt") continue; - if($collection->getFormel() != "") { + if ($collection->getName() == "opt") { + continue; + } + if ($collection->getFormel() != "") { $formel = $this->formelCalc->parse($collection->getFormel()); eval('$var = ' . $formel . ';'); - }else{ - if(isset($this->engine->getVariables()[$collection->getName()])) { + } else { + if (isset($this->engine->getVariables()[$collection->getName()])) { $var = $this->engine->getVariables()[$collection->getName()]; } } /** @var Edge $edge */ - foreach($collection as $edge) { + foreach ($collection as $edge) { - if($var !== "XXXXXXXXXXXX" && $edge->isValid($var)) { + if ($var !== "XXXXXXXXXXXX" && $edge->isValid($var)) { - if($edge->getPauschale() != 0) { + if ($edge->getPauschale() != 0) { eval('$gesamt += ' . $edge->getPauschale() . ';'); $this->engine->addDebugFlatPrice($collection->getName(), $edge->getPauschale()); } - if($edge->getPreis() != 0) { - eval('$gesamt += ' . ($edge->getPreis()*$var) . ';'); - $this->engine->addDebugPrice($collection->getName(), $edge->getPreis(), $var, $edge->getPreis()*$var); + if ($edge->getPreis() != 0) { + eval('$gesamt += ' . ($edge->getPreis() * $var) . ';'); + $this->engine->addDebugPrice($collection->getName(), $edge->getPreis(), $var, $edge->getPreis() * $var); } - if($edge->getCalcValue() != "") { + if ($edge->getCalcValue() != "") { $cv = $this->formelCalc->parse($edge->getCalcValue()); $orgCv = $cv; try { eval('@$cv = ' . $cv . ';'); $this->engine->addDebugCalcVariables($option->getId() . '_' . $collection->getName(), $edge->getCalcValue(), $orgCv . ' = ' . $cv); - }catch (\Throwable $e) { + } catch (\Throwable $e) { $cv = 0; } $this->engine->addCalcVariable($option->getId() . '_' . $collection->getName(), $orgCv); $this->engine->setCalcVaribleStack($orgCv, $calcValueId); } - if($edge->getFormel() != "") { + if ($edge->getFormel() != "") { $formel = $this->formelCalc->parse($edge->getFormel()); if ($formel != "" && !in_array($option->getId(), ['weight', 'weight_single']) && !$option->isAjaxExport() && !$option->isDisplayOnly() && $option->isAmount()) { $p = 0; @@ -131,11 +134,11 @@ class Calc try { eval('@$p = ' . $this->eval_func($gesamt, $formel) . ';'); $this->engine->addDebugCalcFormel($edge->getFormel(), $formel . ' = ' . $p); - }catch (\Throwable $e) { + } catch (\Throwable $e) { $this->engine->addDebugCalcFormel($edge->getFormel(), $formel . ' = error'); $p = 0; } - if($p > 0 || $p < 0) { + if ($p > 0 || $p < 0) { $gesamt += $p; } @@ -148,7 +151,7 @@ class Calc eval('@$p = ' . $this->eval_func($gesamt, $formel) . ';'); $this->engine->addAjaxVariable($option->getId(), $p); - }catch (\Throwable $e) { + } catch (\Throwable $e) { $this->engine->addDebugCalcFormel($edge->getFormel(), $formel . ' = error'); $p = 0; } @@ -160,19 +163,19 @@ class Calc $this->engine->addDisplayVariable($option->getId(), $p); } - if($formel != "" && $option->getId() == "weight_single") { + if ($formel != "" && $option->getId() == "weight_single") { $p = 0; $formel = str_replace("tonumber", '$this->toNumber', $formel); eval('@$p = ' . $this->eval_func($gesamt, $formel) . ';'); $this->engine->setWeightSingle($p); } - if($formel != "" && $option->getId() == "weight") { + if ($formel != "" && $option->getId() == "weight") { $p = 0; $formel = str_replace("tonumber", '$this->toNumber', $formel); eval('@$p = ' . $this->eval_func($gesamt, $formel) . ';'); $this->engine->setWeight($p); } - if(!$option->isAmount()) { + if (!$option->isAmount()) { $p = 0; $formel = str_replace("tonumber", '$this->toNumber', $formel); @@ -181,9 +184,9 @@ class Calc } } - if($edge->getEdgesCollectionContainer()->count() > 0) { + if ($edge->getEdgesCollectionContainer()->count() > 0) { $calcValueId[] = $collection->getName(); - $gesamt = $this->parseEdgeCollection($gesamt, $option, $edge->getEdgesCollectionContainer(), $calcValueId); + $gesamt = $this->parseEdgeCollection($gesamt, $option, $edge->getEdgesCollectionContainer(), $calcValueId); } } } @@ -192,15 +195,16 @@ class Calc return $gesamt; } - private function eval_func($gesamt, string $formel) { + private function eval_func($gesamt, string $formel) + { $p = 0; try { eval('@$p=' . $formel . ';'); - }catch(\Throwable $e) { + } catch (\Throwable $e) { - if(str_contains($e->getMessage(), 'Undefined constant')) { + if (str_contains($e->getMessage(), 'Undefined constant')) { preg_match('/Undefined constant "(.*)"/', $e->getMessage(), $output_array); - if(isset($output_array[1])) { + if (isset($output_array[1])) { $formel = str_replace($output_array[1], "'" . $output_array[1] ."'", $formel); $p = $this->eval_func($gesamt, $formel); return $p; @@ -213,7 +217,7 @@ class Calc private function toNumber($value) { - if(strpos($value,',') == (strlen($value)-1)) { + if (strpos($value, ',') == (strlen($value) - 1)) { return str_replace(',', '', $value); } diff --git a/tests/Customer/DD/CalcTest.php b/tests/Customer/DD/CalcTest.php index be1e798..814fae7 100644 --- a/tests/Customer/DD/CalcTest.php +++ b/tests/Customer/DD/CalcTest.php @@ -1,4 +1,5 @@ '0', 'angebotsadresse_nopresentation' => '', 'eckenstanzecheckbox1' => - array ( + array( 0 => '1', ), 'eckenstanzecheckbox2' => - array ( + array( 0 => '1', ), 'eckenstanzecheckbox3' => - array ( + array( 0 => '1', ), 'eckenstanzecheckbox4' => - array ( + array( 0 => '1', ), ]); - echo $this->engine->getPrice().PHP_EOL; - var_dump($this->engine->getAjaxVariables()['ppreisI']); $this->assertSame(37.74, $this->engine->getPrice()); } diff --git a/tests/Customer/R/RadioboxCalcTest.php b/tests/Customer/R/RadioboxCalcTest.php new file mode 100644 index 0000000..5f9c670 --- /dev/null +++ b/tests/Customer/R/RadioboxCalcTest.php @@ -0,0 +1,55 @@ +parse(simplexml_load_string(file_get_contents(__DIR__ . '/papierContainer.xml'))); + + $this->engine = new Engine(); + $this->engine->setPaperContainer($paperContainer); + $this->engine->setPaperRepository($repository); + $this->engine->setFormulas(file_get_contents(__DIR__ . '/formels.txt')); + $this->engine->setParameters(file_get_contents(__DIR__ . '/parameters.txt')); + $this->engine->setTemplates(file_get_contents(__DIR__ . '/calcTemplates.xml')); + + $this->engine->loadString(file_get_contents(__DIR__ . '/calc_radiobox_with_calc.xml')); + + } + + public function tearDown(): void + { + $this->engine = null; + } + + public function testCalcDefault(): void + { + $this->assertSame(3.22, $this->engine->getPrice()); + } + + public function testCalcZweiseitig(): void + { + $this->engine->setVariable('bedruckung', 'beidseitig'); + $this->assertSame(5.97, $this->engine->getPrice()); + } + + public function testCalcZweiseitig20(): void + { + $this->engine->setVariable('auflage', '20'); + $this->engine->setVariable('bedruckung', 'beidseitig'); + $this->assertSame(62.86, $this->engine->getPrice()); + } + +} diff --git a/tests/Customer/R/calc_radiobox_with_calc.xml b/tests/Customer/R/calc_radiobox_with_calc.xml new file mode 100644 index 0000000..3a5a099 --- /dev/null +++ b/tests/Customer/R/calc_radiobox_with_calc.xml @@ -0,0 +1,33 @@ + + + + Checkbox Grenze Formel + + + +