This commit is contained in:
DESKTOP-E17406D\boonkerz 2023-05-22 13:20:24 +02:00
parent 7d7c6d9499
commit f246bd00a4
4 changed files with 29 additions and 8 deletions

File diff suppressed because one or more lines are too long

View File

@ -129,7 +129,7 @@ class Calc
$formel = str_replace("tonumber", '$this->toNumber', $formel); $formel = str_replace("tonumber", '$this->toNumber', $formel);
try { try {
eval('@$p = ' . $formel . ';'); eval('@$p = ' . $this->eval_func($gesamt, $formel) . ';');
$this->engine->addDebugCalcFormel($edge->getFormel(), $formel . ' = ' . $p); $this->engine->addDebugCalcFormel($edge->getFormel(), $formel . ' = ' . $p);
}catch (\Throwable $e) { }catch (\Throwable $e) {
$this->engine->addDebugCalcFormel($edge->getFormel(), $formel . ' = error'); $this->engine->addDebugCalcFormel($edge->getFormel(), $formel . ' = error');
@ -145,7 +145,7 @@ class Calc
$p = 0; $p = 0;
$formel = str_replace("tonumber", '$this->toNumber', $formel); $formel = str_replace("tonumber", '$this->toNumber', $formel);
try { try {
eval('@$p = ' . $formel . ';'); eval('@$p = ' . $this->eval_func($gesamt, $formel) . ';');
$this->engine->addAjaxVariable($option->getId(), $p); $this->engine->addAjaxVariable($option->getId(), $p);
}catch (\Throwable $e) { }catch (\Throwable $e) {
@ -156,27 +156,27 @@ class Calc
if ($formel != "" && !in_array($option->getId(), ['weight', 'weight_single']) && !$option->isAjaxExport() && $option->isDisplayOnly() && $option->isAmount()) { if ($formel != "" && !in_array($option->getId(), ['weight', 'weight_single']) && !$option->isAjaxExport() && $option->isDisplayOnly() && $option->isAmount()) {
$p = 0; $p = 0;
$formel = str_replace("tonumber", '$this->toNumber', $formel); $formel = str_replace("tonumber", '$this->toNumber', $formel);
eval('@$p = ' . $formel . ';'); eval('@$p = ' . $this->eval_func($gesamt, $formel) . ';');
$this->engine->addDisplayVariable($option->getId(), $p); $this->engine->addDisplayVariable($option->getId(), $p);
} }
if($formel != "" && $option->getId() == "weight_single") { if($formel != "" && $option->getId() == "weight_single") {
$p = 0; $p = 0;
$formel = str_replace("tonumber", '$this->toNumber', $formel); $formel = str_replace("tonumber", '$this->toNumber', $formel);
eval('@$p = ' . $formel . ';'); eval('@$p = ' . $this->eval_func($gesamt, $formel) . ';');
$this->engine->setWeightSingle($p); $this->engine->setWeightSingle($p);
} }
if($formel != "" && $option->getId() == "weight") { if($formel != "" && $option->getId() == "weight") {
$p = 0; $p = 0;
$formel = str_replace("tonumber", '$this->toNumber', $formel); $formel = str_replace("tonumber", '$this->toNumber', $formel);
eval('@$p = ' . $formel . ';'); eval('@$p = ' . $this->eval_func($gesamt, $formel) . ';');
$this->engine->setWeight($p); $this->engine->setWeight($p);
} }
if(!$option->isAmount()) { if(!$option->isAmount()) {
$p = 0; $p = 0;
$formel = str_replace("tonumber", '$this->toNumber', $formel); $formel = str_replace("tonumber", '$this->toNumber', $formel);
eval('@$p = ' . $formel . ';'); eval('@$p = ' . $this->eval_func($gesamt, $formel) . ';');
$this->engine->addCalcVariable($option->getId(), $p); $this->engine->addCalcVariable($option->getId(), $p);
} }
} }
@ -192,6 +192,25 @@ class Calc
return $gesamt; return $gesamt;
} }
private function eval_func($gesamt, string $formel) {
$p = 0;
try {
eval('@$p=' . $formel . ';');
}catch(\Throwable $e) {
if(str_contains($e->getMessage(), 'Undefined constant')) {
preg_match('/Undefined constant "(.*)"/', $e->getMessage(), $output_array);
if(isset($output_array[1])) {
$formel = str_replace($output_array[1], "'" . $output_array[1] ."'", $formel);
$p = $this->eval_func($gesamt, $formel);
return $p;
}
}
}
return $p;
}
private function toNumber($value) private function toNumber($value)
{ {
if(strpos($value,',') == (strlen($value)-1)) { if(strpos($value,',') == (strlen($value)-1)) {

View File

@ -38,6 +38,7 @@ class CalcTest extends TestCase
public function testIfDefaultPriceIsOk(): void public function testIfDefaultPriceIsOk(): void
{ {
$this->engine->calc();
$this->assertEquals(467.89 , $this->engine->getPrice()); $this->assertEquals(467.89 , $this->engine->getPrice());
} }

View File

@ -38,7 +38,8 @@ class CalcTest extends TestCase
public function testPrice(): void public function testPrice(): void
{ {
self::assertSame(1234, $this->engine->getPrice()); self::assertSame(459.56, $this->engine->getPrice());
self::assertSame(82328.4, $this->engine->getWeight());
} }
} }